profile_tree_widget.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 // *****************************************************************************
31 
32 #include <QVBoxLayout>
33 #include <QTreeWidget>
34 #include <QMenu>
35 
38 
39 namespace swri_profiler_tools
40 {
42  ProfileNodeType = QTreeWidgetItem::UserType,
43 };
44 
46  ProfileKeyRole = Qt::UserRole,
48 };
49 
51  :
52  QWidget(parent),
53  db_(NULL)
54 {
55  tree_widget_ = new QTreeWidget(this);
56  tree_widget_->setFont(QFont("Ubuntu Mono", 9));
57  tree_widget_->setContextMenuPolicy(Qt::CustomContextMenu);
58  tree_widget_->setExpandsOnDoubleClick(false);
59 
60  QObject::connect(tree_widget_, SIGNAL(customContextMenuRequested(const QPoint&)),
61  this, SLOT(handleTreeContextMenuRequest(const QPoint&)));
62 
63  QObject::connect(tree_widget_, SIGNAL(itemActivated(QTreeWidgetItem*, int)),
64  this, SLOT(handleItemActivated(QTreeWidgetItem*, int)));
65 
66  auto *main_layout = new QVBoxLayout();
67  main_layout->addWidget(tree_widget_);
68  main_layout->setContentsMargins(0,0,0,0);
69  setLayout(main_layout);
70 }
71 
73 {
74 }
75 
77 {
78  if (db_) {
79  // note(exjohnson): we can implement this later if desired, but
80  // currently no use case for it.
81  qWarning("ProfileTreeWidget: Cannot change the profile database.");
82  return;
83  }
84 
85  db_ = db;
86 
88 
89  QObject::connect(db_, SIGNAL(profileModified(int)),
90  this, SLOT(handleProfileAdded(int)));
91  QObject::connect(db_, SIGNAL(profileAdded(int)),
92  this, SLOT(handleProfileAdded(int)));
93  QObject::connect(db_, SIGNAL(nodesAdded(int)),
94  this, SLOT(handleNodesAdded(int)));
95 }
96 
98 {
99  // We can optimize these to be specific later if necessary.
101 }
102 
104 {
105  // We can optimize these to be specific later if necessary.
107 }
108 
110 {
111  tree_widget_->clear();
112  items_.clear();
113 
114  if (!db_) {
115  return;
116  }
117 
118  std::vector<int> keys = db_->profileKeys();
119  for (auto key : keys) {
120  addProfile(key);
121  }
122 }
123 
124 void ProfileTreeWidget::addProfile(int profile_key)
125 {
126  const Profile &profile = db_->profile(profile_key);
127  if (!profile.isValid()) {
128  qWarning("Invald profile for key %d.", profile_key);
129  return;
130  }
131 
132  QTreeWidgetItem *item = new QTreeWidgetItem(ProfileNodeType);
133 
134 
135 
136  item->setText(0, profile.name());
137  item->setData(0, ProfileKeyRole, profile_key);
138  item->setData(0, NodeKeyRole, profile.rootKey());
139  tree_widget_->addTopLevelItem(item);
140  items_[DatabaseKey(profile.profileKey(), profile.rootKey())] = item;
141 
142  for (auto child_key : profile.rootNode().childKeys()) {
143  addNode(item, profile, child_key);
144  }
145 
146  if (active_key_.isValid()) {
147  if (items_.count(active_key_)) {
149  } else {
151  emit activeNodeChanged(-1,-1);
152  }
153  }
154 }
155 
156 void ProfileTreeWidget::addNode(QTreeWidgetItem *parent,
157  const Profile &profile,
158  const int node_key)
159 {
160  const ProfileNode &node = profile.node(node_key);
161  if (!node.isValid()) {
162  qWarning("Invalid node for key %d", node_key);
163  return;
164  }
165 
166  QTreeWidgetItem *item = new QTreeWidgetItem(ProfileNodeType);
167  item->setText(0, node.name());
168  item->setData(0, ProfileKeyRole, profile.profileKey());
169  item->setData(0, NodeKeyRole, node.nodeKey());
170  parent->addChild(item);
171  items_[DatabaseKey(profile.profileKey(), node.nodeKey())] = item;
172 
173  for (auto child_key : node.childKeys()) {
174  addNode(item, profile, child_key);
175  }
176 }
177 
179 {
180  QTreeWidgetItem *item = tree_widget_->itemAt(pos);
181 
182  auto menu = new QMenu(this);
183  auto expand_all_action = menu->addAction("Expand All");
184  QObject::connect(expand_all_action, SIGNAL(triggered()),
185  tree_widget_, SLOT(expandAll()));
186 
187  auto collapse_all_action = menu->addAction("Collapse All");
188  QObject::connect(collapse_all_action, SIGNAL(triggered()),
189  tree_widget_, SLOT(collapseAll()));
190 
191  menu->popup(tree_widget_->mapToGlobal(pos));
192  QObject::connect(menu, SIGNAL(aboutToHide()), menu, SLOT(deleteLater()));
193 
194  if (!item) {
195  qWarning("No item under mouse");
196  } else if (item->type() == ProfileNodeType) {
197  int profile_key = item->data(0, ProfileKeyRole).toInt();
198  int node_key = item->data(0, NodeKeyRole).toInt();
199  qWarning("node %d--%d", profile_key, node_key);
200  } else {
201  qWarning("Unknown item type: %d", item->type());
202  }
203 }
204 
205 void ProfileTreeWidget::handleItemActivated(QTreeWidgetItem *item, int column)
206 {
207  if (item->type() == ProfileNodeType) {
208  int profile_key = item->data(0, ProfileKeyRole).toInt();
209  int node_key = item->data(0, NodeKeyRole).toInt();
210  setActiveNode(profile_key, node_key);
211  }
212 }
213 
215 {
216  if (!key.isValid()) {
217  return "<INVALID KEY>";
218  }
219 
220  const Profile &profile = db_->profile(key.profileKey());
221  if (key.nodeKey() == profile.rootKey()) {
222  return profile.name();
223  } else {
224  return profile.node(key.nodeKey()).name();
225  }
226 }
227 
229 {
230  if (items_.count(key) == 0) {
231  return;
232  }
233 
234  items_.at(key)->setText(0, "[" + nameForKey(key) + "]");
235 }
236 
238 {
239  if (items_.count(key) == 0) {
240  return;
241  }
242 
243  items_.at(key)->setText(0, nameForKey(key));
244 }
245 
246 void ProfileTreeWidget::setActiveNode(int profile_key, int node_key)
247 {
248  const DatabaseKey new_key(profile_key, node_key);
249  if (new_key == active_key_) {
250  return;
251  }
252 
254  active_key_ = new_key;
256  emit activeNodeChanged(profile_key, node_key);
257 }
258 } // namespace swri_profiler_tools
std::unordered_map< DatabaseKey, QTreeWidgetItem * > items_
const ProfileNode & rootNode() const
Definition: profile.cpp:467
void addNode(QTreeWidgetItem *parent, const Profile &profile, const int node_key)
const std::vector< int > & childKeys() const
Definition: profile.h:137
const ProfileNode & node(int node_key) const
Definition: profile.cpp:457
void markItemInactive(const DatabaseKey &key)
void markItemActive(const DatabaseKey &key)
const QString & name() const
Definition: profile.h:211
void handleItemActivated(QTreeWidgetItem *item, int column)
std::vector< int > profileKeys() const
const QString & name() const
Definition: profile.h:131
void handleTreeContextMenuRequest(const QPoint &pos)
QString nameForKey(const DatabaseKey &key) const
void activeNodeChanged(int profile_key, int node_key)
void setActiveNode(int profile_key, int node_key)


swri_profiler_tools
Author(s):
autogenerated on Fri Nov 27 2020 03:44:18