node_list_model.cpp
Go to the documentation of this file.
00001 // *****************************************************************************
00002 //
00003 // Copyright (c) 2015, Southwest Research Institute® (SwRI®)
00004 // All rights reserved.
00005 //
00006 // Redistribution and use in source and binary forms, with or without
00007 // modification, are permitted provided that the following conditions are met:
00008 //     * Redistributions of source code must retain the above copyright
00009 //       notice, this list of conditions and the following disclaimer.
00010 //     * Redistributions in binary form must reproduce the above copyright
00011 //       notice, this list of conditions and the following disclaimer in the
00012 //       documentation and/or other materials provided with the distribution.
00013 //     * Neither the name of Southwest Research Institute® (SwRI®) nor the
00014 //       names of its contributors may be used to endorse or promote products
00015 //       derived from this software without specific prior written permission.
00016 //
00017 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00018 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020 // ARE DISCLAIMED. IN NO EVENT SHALL Southwest Research Institute® BE LIABLE 
00021 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 
00022 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
00023 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
00024 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
00025 // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 
00026 // OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
00027 // DAMAGE.
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   // When the database is cleared, we reset all of the counts to zero
00102   // instead of deleting them from the list.  This allows a user to
00103   // clear out the logs while retaining their node selection so that
00104   // they can easily reset the data without having to choose the
00105   // selection again.  
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 }  // namespace swri_console


swri_console
Author(s):
autogenerated on Sat Jun 8 2019 18:46:13