DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
CustomTreeView.cpp
Go to the documentation of this file.
1// DUNE DAQ modification notice:
2// This file has been modified from the original ATLAS dbe source for the DUNE DAQ project.
3// Fork baseline commit: dbe-02-12-17 (2022-05-12).
4// Renamed since fork: no.
5
8#include "dbe/tree.hpp"
9
10#include "dbe/treenode.hpp"
11#include "dbe/treeselection.hpp"
12
13#include <QAction>
14#include <QContextMenuEvent>
15#include <QMenu>
16#include <QMessageBox>
17
20#include "dbe/ObjectCreator.hpp"
21#include "dbe/ObjectEditor.hpp"
23#include "dbe/messenger.hpp"
24
25//------------------------------------------------------------------------------------------
27 : QTreeView(Parent)
28 , contextMenu(nullptr)
29{
30 setDragEnabled(true);
31 setSortingEnabled(true);
32 setExpandsOnDoubleClick(false);
34 setSelectionMode(QAbstractItemView::ExtendedSelection);
35 setUniformRowHeights(true);
36}
37//------------------------------------------------------------------------------------------
38
39//------------------------------------------------------------------------------------------
40void
42{
43 if (contextMenu == nullptr) {
44 contextMenu = new QMenu(this);
45 contextMenu->addAction(editObjectAc);
46 contextMenu->addAction(deleteObjectAc);
47 contextMenu->addAction(createObjectAc);
49 contextMenu->addAction(copyObjectAc);
51 contextMenu->addAction(expandAllAc);
52 contextMenu->addAction(collapseAllAc);
53 contextMenu->addAction(refByAc);
54 contextMenu->addAction(refByAcOnlyComp);
55 }
56
57 model_common_interface* modelInterface = dynamic_cast<model_common_interface*>(this->model());
58 QModelIndex index = this->currentIndex();
59
60 if (index.isValid()) {
61 if (modelInterface) {
62
63 try {
64 tref obj = modelInterface->getobject(index);
65 (contextMenu->actions()).at(0)->setVisible(true);
66 (contextMenu->actions()).at(1)->setVisible(true);
67 (contextMenu->actions()).at(2)->setVisible(true);
68 (contextMenu->actions()).at(3)->setVisible(false);
69 (contextMenu->actions()).at(4)->setVisible(true);
70 (contextMenu->actions()).at(5)->setVisible(false);
71 (contextMenu->actions()).at(6)->setVisible(false);
72 (contextMenu->actions()).at(7)->setVisible(false);
73 (contextMenu->actions()).at(8)->setVisible(true);
74 (contextMenu->actions()).at(9)->setVisible(true);
75 } catch (daq::dbe::cannot_handle_invalid_qmodelindex const& e) {
76 (contextMenu->actions()).at(0)->setVisible(false);
77 (contextMenu->actions()).at(1)->setVisible(false);
78 (contextMenu->actions()).at(2)->setVisible(true);
79 (contextMenu->actions()).at(3)->setVisible(false);
80 (contextMenu->actions()).at(4)->setVisible(false);
81 (contextMenu->actions()).at(5)->setVisible(false);
82 (contextMenu->actions()).at(6)->setVisible(false);
83 (contextMenu->actions()).at(7)->setVisible(false);
84 (contextMenu->actions()).at(8)->setVisible(false);
85 (contextMenu->actions()).at(9)->setVisible(false);
86 }
87 }
88
89 contextMenu->exec(Event->globalPos());
90 }
91}
92//------------------------------------------------------------------------------------------
93
94//------------------------------------------------------------------------------------------
95void
97{
98
99 editObjectAc = new QAction(tr("&Edit Object"), this);
100 editObjectAc->setShortcut(tr("Ctrl+E"));
101 editObjectAc->setShortcutContext(Qt::WidgetShortcut);
102 connect(editObjectAc, SIGNAL(triggered()), this, SLOT(slot_edit_object()));
103 addAction(editObjectAc);
104
105 deleteObjectAc = new QAction(tr("&Delete Object"), this);
106 deleteObjectAc->setShortcut(tr("Ctrl+D"));
107 deleteObjectAc->setShortcutContext(Qt::WidgetShortcut);
108 connect(deleteObjectAc, SIGNAL(triggered()), this, SLOT(slot_delete_objects()));
109 addAction(deleteObjectAc);
110
111 createObjectAc = new QAction(tr("Create &New Object"), this);
112 createObjectAc->setShortcut(tr("Ctrl+N"));
113 createObjectAc->setShortcutContext(Qt::WidgetShortcut);
114 connect(createObjectAc, SIGNAL(triggered()), this, SLOT(slot_create_object()));
115 addAction(createObjectAc);
116
117 copyObjectAc = new QAction(tr("&Copy This Object Into A New One"), this);
118 copyObjectAc->setShortcut(tr("Ctrl+Shift+N"));
119 copyObjectAc->setShortcutContext(Qt::WidgetShortcut);
120 connect(copyObjectAc, SIGNAL(triggered()), this, SLOT(slot_copy_object()));
121 addAction(copyObjectAc);
122
123 deleteObjectWidgetAc = new QAction(tr("Delete Object &Widget"), this);
124 deleteObjectWidgetAc->setShortcut(tr("Ctrl+W"));
125 deleteObjectWidgetAc->setShortcutContext(Qt::WidgetShortcut);
126 addAction(deleteObjectWidgetAc);
127
128 buildTableFromClassAc = new QAction(tr("Build Tab&le From Class"), this);
129 buildTableFromClassAc->setShortcut(tr("Ctrl+L"));
130 buildTableFromClassAc->setShortcutContext(Qt::WidgetShortcut);
131 addAction(buildTableFromClassAc);
132
133 expandAllAc = new QAction(tr("Expand &All"), this);
134 expandAllAc->setShortcut(tr("Ctrl+A"));
135 expandAllAc->setShortcutContext(Qt::WidgetShortcut);
136 connect(expandAllAc, SIGNAL(triggered()), this,
137 SLOT(expandAll())); // QTreeView slot
138 addAction(expandAllAc);
139
140 collapseAllAc = new QAction(tr("C&ollapse All"), this);
141 collapseAllAc->setShortcut(tr("Ctrl+Shift+C"));
142 collapseAllAc->setShortcutContext(Qt::WidgetShortcut);
143 connect(collapseAllAc, SIGNAL(triggered()), this,
144 SLOT(collapseAll())); // QTreeView slot
145 addAction(collapseAllAc);
146
147 refByAc = new QAction(tr("&Referenced By (All objects)"), this);
148 refByAc->setShortcut(tr("Ctrl+Y"));
149 refByAc->setShortcutContext(Qt::WidgetShortcut);
150 refByAc->setToolTip("Find all objects which reference the selecetd object");
151 refByAc->setStatusTip(refByAc->toolTip());
152 connect(refByAc, SIGNAL(triggered()), this, SLOT(referencedByAll()));
153 addAction(refByAc);
154
155 refByAcOnlyComp = new QAction(tr("Referenced B&y (Only Composite)"), this);
156 refByAcOnlyComp->setShortcut(tr("Ctrl+Shift+Y"));
157 refByAcOnlyComp->setShortcutContext(Qt::WidgetShortcut);
158 refByAcOnlyComp->setToolTip("Find objects (ONLY Composite ones) which reference the selecetd object");
159 refByAcOnlyComp->setStatusTip(refByAcOnlyComp->toolTip());
160 connect(refByAcOnlyComp, SIGNAL(triggered()), this, SLOT(referencedbyOnlycomposite()));
161 addAction(refByAcOnlyComp);
162}
163//------------------------------------------------------------------------------------------
164
165//------------------------------------------------------------------------------------------
166void
168{
169 // To create an object we only need to extract the information of the class that the
170 // user had clicked onto.
171 model_common_interface* common_model = dynamic_cast<model_common_interface*>(model());
172 QModelIndex const& treeindex = currentIndex();
173 treenode* cnode = nullptr;
174
175 if (common_model && treeindex.isValid()) {
176 dunedaq::conffwk::class_t classinfo = common_model->getclass(treeindex);
177
178 if (dynamic_cast<models::treeselection*>(model())) {
179 models::treeselection* selectionmodel = dynamic_cast<models::treeselection*>(model());
180
181 if (selectionmodel) {
182 QModelIndex const& srcindex = selectionmodel->mapToSource(treeindex);
183
184 if (dbe::models::tree* tree_model = dynamic_cast<dbe::models::tree*>(selectionmodel->sourceModel())) {
185 cnode = tree_model->getnode(srcindex);
186 }
187 }
188 } else if (dbe::models::subtree_proxy* subtree_model = dynamic_cast<dbe::models::subtree_proxy*>(model())) {
189 cnode = subtree_model->getnode(treeindex);
190 }
191
192 if (dynamic_cast<RelationshipNode*>(cnode)) {
193 treenode* parent = cnode->GetParent();
194 ObjectNode* NodeObject = dynamic_cast<ObjectNode*>(parent);
195 RelationshipNode* RelationshipTreeNode = dynamic_cast<RelationshipNode*>(cnode);
196
197 (new ObjectCreator(NodeObject->GetObject(), RelationshipTreeNode->relation_t()))->show();
198 } else {
199 (new ObjectCreator(classinfo))->show();
200 }
201 }
202}
203//------------------------------------------------------------------------------------------
204
205//------------------------------------------------------------------------------------------
206void
208{
209 if (this->model() != nullptr) {
210 QModelIndexList qindices = this->selectedIndexes();
211 std::vector<QModelIndex> indices;
212
213 for (QModelIndex const& q : qindices) {
214 if (q.isValid() and q.column() == 0) {
215 indices.push_back(q);
216 }
217 }
218
219 if (models::treeselection* casted_model = dynamic_cast<models::treeselection*>(this->model())) {
220 casted_model->delete_objects(indices.begin(), indices.end());
221 } else if (dbe::models::subtree_proxy* casted_model = dynamic_cast<dbe::models::subtree_proxy*>(this->model())) {
222 casted_model->delete_objects(indices.begin(), indices.end());
223 } else {
224 WARN("Object Deleting",
225 "Object deletion failed due to the internal model being in invalid state",
226 "You are advised to restart the application before proceeding any further");
227 }
228 }
229}
230//------------------------------------------------------------------------------------------
231
232//------------------------------------------------------------------------------------------
233void
235{
236 if (this->model() != nullptr) {
237 QModelIndexList indices = this->selectedIndexes();
238
239 for (QModelIndex const& q : indices) {
240 edit_object(q);
241 }
242 }
243}
244//------------------------------------------------------------------------------------------
245
246//------------------------------------------------------------------------------------------
247void
248dbe::CustomTreeView::edit_object(QModelIndex const& index)
249{
250 model_common_interface* filtermodel = dynamic_cast<model_common_interface*>(model());
251
252 if (!index.isValid()) {
253 return;
254 }
255
256 if (filtermodel) {
257 try {
258 tref obj = filtermodel->getobject(index);
259 bool WidgetFound = false;
260 QString ObjectEditorName = QString("%1@%2").arg(obj.UID().c_str()).arg(obj.class_name().c_str());
261
262 for (QWidget* Editor : QApplication::allWidgets()) {
263 ObjectEditor* Widget = dynamic_cast<ObjectEditor*>(Editor);
264
265 if (Widget != nullptr) {
266 if ((Widget->objectName()).compare(ObjectEditorName) == 0) {
267 Widget->raise();
268 Widget->setVisible(true);
269 WidgetFound = true;
270 }
271 }
272 }
273
274 if (!WidgetFound) {
275 (new ObjectEditor(obj))->show();
276 }
277 } catch (daq::dbe::cannot_handle_invalid_qmodelindex const& e) {
278 // nothing to do , maybe the user just clicked the wrong place
279 }
280 }
281}
282//------------------------------------------------------------------------------------------
283
284//------------------------------------------------------------------------------------------
285void
287{
288 model_common_interface* filtermodel = dynamic_cast<model_common_interface*>(model());
289 QModelIndex i = currentIndex();
290
291 if (i.isValid() and filtermodel) {
292 try {
293 (new ObjectCreator(filtermodel->getobject(i)))->show();
294 } catch (daq::dbe::cannot_handle_invalid_qmodelindex const& e) {
295 // TODO the index can be invalid in this case
296 }
297 }
298}
299//------------------------------------------------------------------------------------------
300void
305
306void
311//------------------------------------------------------------------------------------------
312void
314{
315 // Reset the model when the widget is closed
316 // If not done, reference to removed objects may create issues
317 models::treeselection* filtermodel = dynamic_cast<models::treeselection*>(model());
318
319 setModel(nullptr);
320
321 if (filtermodel != nullptr) {
322 filtermodel->ResetQueryObjects();
323 filtermodel->deleteLater();
324 }
325
326 event->accept();
327}
328
329//------------------------------------------------------------------------------------------
330void
332{
333 model_common_interface* filtermodel = dynamic_cast<model_common_interface*>(model());
334 QModelIndex Index = currentIndex();
335
336 if (!Index.isValid()) {
337 return;
338 }
339
340 if (filtermodel) {
341
342 try {
343 tref obj = filtermodel->getobject(Index);
344 std::vector<tref> objects;
345
346 if (not obj.is_null()) {
347 objects = obj.referenced_by("*", All);
348 }
349
350 if (objects.size() > 0) {
351 dbe::models::tree* Source = dynamic_cast<dbe::models::tree*>(filtermodel->ReturnSourceModel());
354 Selection->SetQueryObjects(objects);
355 Selection->setFilterRegExp("Dummy");
356 Selection->setSourceModel(Source);
357
358 CustomTreeView* tView = new CustomTreeView(0);
359 tView->setModel(Selection);
360 tView->setWindowTitle(QString("Objects referencing '%1'").arg(obj.UID().c_str()));
361 tView->setSortingEnabled(true);
362 tView->setAlternatingRowColors(true);
363 tView->resizeColumnToContents(0);
364 tView->resizeColumnToContents(1);
365 tView->setAttribute(Qt::WA_DeleteOnClose);
366
367 connect(tView, SIGNAL(doubleClicked(QModelIndex)), tView, SLOT(slot_edit_object()));
368
369 tView->show();
370 } else {
371 if (All) {
372 WARN("No references of only composite objects have been found for object",
373 " ",
374 "with UID:",
375 obj.UID(),
376 "of class",
377 obj.class_name());
378 } else {
379 WARN("No references of objects have been found for object",
380 " ",
381 "with UID:",
382 obj.UID(),
383 "of class",
384 obj.class_name());
385 }
386 }
387 } catch (daq::dbe::cannot_handle_invalid_qmodelindex const& e) {
388 }
389 }
390}
391//------------------------------------------------------------------------------------------
392
393//------------------------------------------------------------------------------------------
394void
396{
397 model_common_interface* filtermodel = dynamic_cast<model_common_interface*>(model());
398
399 if (filtermodel) {
400 std::vector<tref> objects;
401
402 if (!obj.is_null()) {
403 objects = obj.referenced_by("*", All);
404 }
405
406 if (objects.size() > 0) {
407 dbe::models::tree* Source = dynamic_cast<dbe::models::tree*>(filtermodel->ReturnSourceModel());
410 Selection->SetQueryObjects(objects);
411 Selection->setFilterRegExp("Dummy");
412 Selection->setSourceModel(Source);
413
414 CustomTreeView* tView = new CustomTreeView(0);
415 tView->setModel(Selection);
416 tView->setSortingEnabled(true);
417 tView->setAlternatingRowColors(true);
418 tView->resizeColumnToContents(0);
419 tView->hideColumn(1);
420 // tView->resizeColumnToContents(1);
421 tView->setWindowTitle(QString("Objects referencing '%1'").arg(obj.UID().c_str()));
422 tView->setAttribute(Qt::WA_DeleteOnClose);
423
424 connect(tView, SIGNAL(doubleClicked(QModelIndex)), tView, SLOT(slot_edit_object()));
425
426 tView->show();
427 } else {
428 if (All) {
429 WARN("No references for object", "Only Composite Objects - Not found", "with UID:", obj.UID());
430 } else {
431 WARN("No references for object", "All Objects - Not found", "with UID:", obj.UID());
432 }
433 }
434 }
435}
436//------------------------------------------------------------------------------------------
void referencedBy(bool All)
void edit_object(QModelIndex const &)
QAction * buildTableFromClassAc
void closeEvent(QCloseEvent *event) override
CustomTreeView(QWidget *Parent=nullptr)
Including QT Headers.
void contextMenuEvent(QContextMenuEvent *Event)
ObjectCreator(dunedaq::conffwk::class_t const &classinfo, QWidget *parent=0)
ObjectEditor(QWidget *parent=nullptr)
tref GetObject() const
Definition treenode.cpp:276
dunedaq::conffwk::relationship_t relation_t() const
Definition treenode.cpp:384
void SetFilterType(FilterType Filter)
void SetQueryObjects(std::vector< tref > Objects)
treenode * GetParent() const
Definition treenode.cpp:116
#define WARN(...)
Definition messenger.hpp:82
inner::configobject::tref tref
Definition tref.hpp:36
virtual type_class_info getclass(type_index const &index) const =0
virtual type_object_ref getobject(type_index const &index) const =0
virtual QAbstractItemModel * ReturnSourceModel() const =0