Go to the documentation of this file.00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 
00029 
00030 
00031 #include <stdio.h>
00032 #include <vector>
00033 
00034 #include <swri_console/node_list_model.h>
00035 #include <swri_console/log_database.h>
00036 
00037 namespace swri_console
00038 {
00039 NodeListModel::NodeListModel(LogDatabase *db)
00040   :
00041   db_(db)
00042 {
00043   QObject::connect(db_, SIGNAL(databaseCleared()),
00044                    this, SLOT(handleDatabaseCleared()));
00045   QObject::connect(db_, SIGNAL(messagesAdded()),
00046                    this, SLOT(handleMessagesAdded()));
00047 }
00048 
00049 NodeListModel::~NodeListModel()
00050 {
00051 }
00052 
00053 int NodeListModel::rowCount(const QModelIndex &parent) const
00054 {
00055   return ordering_.size();
00056 }
00057 
00058 std::string NodeListModel::nodeName(const QModelIndex &index) const
00059 {
00060   if (index.parent().isValid() ||
00061       static_cast<size_t>(index.row()) > ordering_.size()) {
00062     return "";
00063   }
00064 
00065   return ordering_[index.row()];
00066 }
00067 
00068 QVariant NodeListModel::data(const QModelIndex &index, int role) const
00069 {
00070   if (index.parent().isValid() ||
00071       static_cast<size_t>(index.row()) > ordering_.size()) {
00072     return QVariant();
00073   } 
00074 
00075   std::string name = ordering_[index.row()];
00076   
00077   if (role == Qt::DisplayRole) {
00078     char buffer[1023];
00079     snprintf(buffer, sizeof(buffer), "%s (%lu)",
00080              name.c_str(),
00081              data_.find(name)->second);
00082     return QVariant(QString(buffer));
00083   }
00084 
00085   return QVariant();
00086 }
00087 
00088 void NodeListModel::clear()
00089 {
00090   if (ordering_.empty()) {
00091     return;
00092   }
00093   beginRemoveRows(QModelIndex(), 0, ordering_.size()-1);
00094   data_.clear();
00095   ordering_.clear();
00096   endRemoveRows();
00097 }
00098 
00099 void NodeListModel::handleDatabaseCleared()
00100 {
00101   
00102   
00103   
00104   
00105   
00106   std::map<std::string, size_t>::iterator iter;
00107   for (iter = data_.begin(); iter != data_.end(); ++iter) {
00108     (*iter).second = 0;
00109   }
00110 
00111   Q_EMIT dataChanged(index(0), index(data_.size()-1));
00112 }
00113 
00114 void NodeListModel::handleMessagesAdded()
00115 {
00116   const std::map<std::string, size_t> &msg_counts = db_->messageCounts();
00117   
00118   size_t i = 0;
00119   for (std::map<std::string, size_t>::const_iterator it = msg_counts.begin();
00120        it != msg_counts.end();
00121        ++it)
00122   {
00123     if (!data_.count(it->first)) {
00124       beginInsertRows(QModelIndex(), i, i);
00125       data_[it->first] = it->second;
00126       ordering_.insert(ordering_.begin() + i, it->first);
00127       endInsertRows();
00128     } else {
00129       data_[it->first] = it->second;
00130     }
00131     i++;
00132   }
00133   
00134   Q_EMIT dataChanged(index(0),
00135                      index(ordering_.size()-1));
00136 }
00137 }