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