node_list_model.cpp
Go to the documentation of this file.
1 // *****************************************************************************
2 //
3 // Copyright (c) 2015, Southwest Research Institute® (SwRI®)
4 // All rights reserved.
5 //
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions are met:
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of Southwest Research Institute® (SwRI®) nor the
14 // names of its contributors may be used to endorse or promote products
15 // derived from this software without specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 // ARE DISCLAIMED. IN NO EVENT SHALL Southwest Research Institute® BE LIABLE
21 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
24 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 // OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
27 // DAMAGE.
28 //
29 // *****************************************************************************
30 
31 #include <stdio.h>
32 #include <vector>
33 
36 
37 namespace swri_console
38 {
40  :
41  db_(db)
42 {
43  QObject::connect(db_, SIGNAL(databaseCleared()),
44  this, SLOT(handleDatabaseCleared()));
45  QObject::connect(db_, SIGNAL(messagesAdded()),
46  this, SLOT(handleMessagesAdded()));
47 }
48 
50 {
51 }
52 
53 int NodeListModel::rowCount(const QModelIndex &parent) const
54 {
55  return ordering_.size();
56 }
57 
58 std::string NodeListModel::nodeName(const QModelIndex &index) const
59 {
60  if (index.parent().isValid() ||
61  static_cast<size_t>(index.row()) > ordering_.size()) {
62  return "";
63  }
64 
65  return ordering_[index.row()];
66 }
67 
68 QVariant NodeListModel::data(const QModelIndex &index, int role) const
69 {
70  if (index.parent().isValid() ||
71  static_cast<size_t>(index.row()) > ordering_.size()) {
72  return QVariant();
73  }
74 
75  std::string name = ordering_[index.row()];
76 
77  if (role == Qt::DisplayRole) {
78  char buffer[1023];
79  snprintf(buffer, sizeof(buffer), "%s (%lu)",
80  name.c_str(),
81  data_.find(name)->second);
82  return QVariant(QString(buffer));
83  }
84 
85  return QVariant();
86 }
87 
89 {
90  if (ordering_.empty()) {
91  return;
92  }
93  beginRemoveRows(QModelIndex(), 0, ordering_.size()-1);
94  data_.clear();
95  ordering_.clear();
96  endRemoveRows();
97 }
98 
100 {
101  // When the database is cleared, we reset all of the counts to zero
102  // instead of deleting them from the list. This allows a user to
103  // clear out the logs while retaining their node selection so that
104  // they can easily reset the data without having to choose the
105  // selection again.
106  std::map<std::string, size_t>::iterator iter;
107  for (iter = data_.begin(); iter != data_.end(); ++iter) {
108  (*iter).second = 0;
109  }
110 
111  Q_EMIT dataChanged(index(0), index(data_.size()-1));
112 }
113 
115 {
116  const std::map<std::string, size_t> &msg_counts = db_->messageCounts();
117 
118  size_t i = 0;
119  for (std::map<std::string, size_t>::const_iterator it = msg_counts.begin();
120  it != msg_counts.end();
121  ++it)
122  {
123  if (!data_.count(it->first)) {
124  beginInsertRows(QModelIndex(), i, i);
125  data_[it->first] = it->second;
126  ordering_.insert(ordering_.begin() + i, it->first);
127  endInsertRows();
128  } else {
129  data_[it->first] = it->second;
130  }
131  i++;
132  }
133 
134  Q_EMIT dataChanged(index(0),
135  index(ordering_.size()-1));
136 }
137 } // namespace swri_console
std::vector< std::string > ordering_
virtual int rowCount(const QModelIndex &parent) const
std::string nodeName(const QModelIndex &index) const
std::map< std::string, size_t > data_
const std::map< std::string, size_t > & messageCounts() const
Definition: log_database.h:67
virtual QVariant data(const QModelIndex &index, int role) const


swri_console
Author(s):
autogenerated on Wed Jun 5 2019 21:25:59