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