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#include <QMessageBox>
12
16#include "dbe/ObjectCreator.hpp"
17#include "dbe/ObjectEditor.hpp"
18#include "dbe/messenger.hpp"
19
20//------------------------------------------------------------------------------------------
22 : QTreeView ( Parent ),
23 contextMenu ( nullptr )
24{
25 setDragEnabled ( true );
26 setSortingEnabled ( true );
27 setExpandsOnDoubleClick ( false );
29 setSelectionMode ( QAbstractItemView::ExtendedSelection );
30 setUniformRowHeights(true);
31}
32//------------------------------------------------------------------------------------------
33
34//------------------------------------------------------------------------------------------
35void dbe::CustomTreeView::contextMenuEvent ( QContextMenuEvent * Event )
36{
37 if ( contextMenu == nullptr )
38 {
39 contextMenu = new QMenu ( this );
40 contextMenu->addAction ( editObjectAc );
41 contextMenu->addAction ( deleteObjectAc );
42 contextMenu->addAction ( createObjectAc );
43 contextMenu->addAction ( deleteObjectWidgetAc );
44 contextMenu->addAction ( copyObjectAc );
45 contextMenu->addAction ( buildTableFromClassAc );
46 contextMenu->addAction ( expandAllAc );
47 contextMenu->addAction ( collapseAllAc );
48 contextMenu->addAction ( refByAc );
49 contextMenu->addAction ( refByAcOnlyComp );
50 }
51
52 model_common_interface * modelInterface =
53 dynamic_cast<model_common_interface *> ( this->model() );
54 QModelIndex index = this->currentIndex();
55
56 if ( index.isValid() )
57 {
58 if ( modelInterface )
59 {
60
61 try
62 {
63 tref obj = modelInterface->getobject ( index );
64 ( contextMenu->actions() ).at ( 0 )->setVisible ( true );
65 ( contextMenu->actions() ).at ( 1 )->setVisible ( true );
66 ( contextMenu->actions() ).at ( 2 )->setVisible ( true );
67 ( contextMenu->actions() ).at ( 3 )->setVisible ( false );
68 ( contextMenu->actions() ).at ( 4 )->setVisible ( true );
69 ( contextMenu->actions() ).at ( 5 )->setVisible ( false );
70 ( contextMenu->actions() ).at ( 6 )->setVisible ( false );
71 ( contextMenu->actions() ).at ( 7 )->setVisible ( false );
72 ( contextMenu->actions() ).at ( 8 )->setVisible ( true );
73 ( contextMenu->actions() ).at ( 9 )->setVisible ( true );
74 }
75 catch ( daq::dbe::cannot_handle_invalid_qmodelindex const & e )
76 {
77 ( contextMenu->actions() ).at ( 0 )->setVisible ( false );
78 ( contextMenu->actions() ).at ( 1 )->setVisible ( false );
79 ( contextMenu->actions() ).at ( 2 )->setVisible ( true );
80 ( contextMenu->actions() ).at ( 3 )->setVisible ( false );
81 ( contextMenu->actions() ).at ( 4 )->setVisible ( false );
82 ( contextMenu->actions() ).at ( 5 )->setVisible ( false );
83 ( contextMenu->actions() ).at ( 6 )->setVisible ( false );
84 ( contextMenu->actions() ).at ( 7 )->setVisible ( false );
85 ( contextMenu->actions() ).at ( 8 )->setVisible ( false );
86 ( contextMenu->actions() ).at ( 9 )->setVisible ( false );
87 }
88 }
89
90 contextMenu->exec ( Event->globalPos() );
91 }
92}
93//------------------------------------------------------------------------------------------
94
95//------------------------------------------------------------------------------------------
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 (
159 "Find objects (ONLY Composite ones) which reference the selecetd object" );
160 refByAcOnlyComp->setStatusTip ( refByAcOnlyComp->toolTip() );
161 connect ( refByAcOnlyComp, SIGNAL ( triggered() ), this,
162 SLOT ( referencedbyOnlycomposite() ) );
163 addAction ( refByAcOnlyComp );
164}
165//------------------------------------------------------------------------------------------
166
167//------------------------------------------------------------------------------------------
169{
170 // To create an object we only need to extract the information of the class that the
171 // user had clicked onto.
172 model_common_interface * common_model = dynamic_cast<model_common_interface *> ( model() );
173 QModelIndex const & treeindex = currentIndex();
174 treenode * cnode = nullptr;
175
176 if ( common_model && treeindex.isValid() )
177 {
178 dunedaq::conffwk::class_t classinfo = common_model->getclass ( treeindex );
179
180 if ( dynamic_cast<models::treeselection *> ( model() ) )
181 {
182 models::treeselection * selectionmodel = dynamic_cast<models::treeselection *> ( model() );
183
184 if ( selectionmodel )
185 {
186 QModelIndex const & srcindex = selectionmodel->mapToSource ( treeindex );
187
188 if ( dbe::models::tree * tree_model = dynamic_cast<dbe::models::tree *>
189 ( selectionmodel->sourceModel() )
190 )
191 {
192 cnode = tree_model->getnode ( srcindex );
193 }
194 }
195 }
196 else if ( dbe::models::subtree_proxy * subtree_model =
197 dynamic_cast<dbe::models::subtree_proxy *> ( model() ) )
198 {
199 cnode = subtree_model->getnode ( treeindex );
200 }
201
202 if ( dynamic_cast<RelationshipNode *> ( cnode ) )
203 {
204 treenode * parent = cnode->GetParent();
205 ObjectNode * NodeObject = dynamic_cast<ObjectNode *> ( parent );
206 RelationshipNode * RelationshipTreeNode = dynamic_cast<RelationshipNode *> ( cnode );
207
208 ( new ObjectCreator ( NodeObject->GetObject(),
209 RelationshipTreeNode->relation_t() ) )->show();
210 }
211 else
212 {
213 ( new ObjectCreator ( classinfo ) )->show();
214 }
215 }
216}
217//------------------------------------------------------------------------------------------
218
219//------------------------------------------------------------------------------------------
221{
222 if ( this->model() != nullptr )
223 {
224 QModelIndexList qindices = this->selectedIndexes();
225 std::vector<QModelIndex> indices;
226
227 for ( QModelIndex const & q : qindices )
228 {
229 if ( q.isValid() and q.column() == 0 )
230 {
231 indices.push_back ( q );
232 }
233 }
234
235 if ( models::treeselection * casted_model = dynamic_cast<models::treeselection *> ( this
236 ->model() )
237 )
238 {
239 casted_model->delete_objects ( indices.begin(), indices.end() );
240 }
241 else if ( dbe::models::subtree_proxy * casted_model =
242 dynamic_cast<dbe::models::subtree_proxy *>
243 ( this->model() ) )
244 {
245 casted_model->delete_objects ( indices.begin(), indices.end() );
246 }
247 else
248 {
249 WARN ( "Object Deleting",
250 "Object deletion failed due to the internal model being in invalid state",
251 "You are advised to restart the application before proceeding any further" );
252 }
253
254 }
255}
256//------------------------------------------------------------------------------------------
257
258//------------------------------------------------------------------------------------------
260{
261 if ( this->model() != nullptr )
262 {
263 QModelIndexList indices = this->selectedIndexes();
264
265 for ( QModelIndex const & q : indices )
266 {
267 edit_object ( q );
268 }
269 }
270}
271//------------------------------------------------------------------------------------------
272
273//------------------------------------------------------------------------------------------
274void dbe::CustomTreeView::edit_object ( QModelIndex const & index )
275{
276 model_common_interface * filtermodel = dynamic_cast<model_common_interface *> ( model() );
277
278 if ( !index.isValid() )
279 {
280 return;
281 }
282
283 if ( filtermodel )
284 {
285 try
286 {
287 tref obj = filtermodel->getobject ( index );
288 bool WidgetFound = false;
289 QString ObjectEditorName = QString ( "%1@%2" ).arg ( obj.UID().c_str() ).arg (
290 obj.class_name().c_str() );
291
292 for ( QWidget * Editor : QApplication::allWidgets() )
293 {
294 ObjectEditor * Widget = dynamic_cast<ObjectEditor *> ( Editor );
295
296 if ( Widget != nullptr )
297 {
298 if ( ( Widget->objectName() ).compare ( ObjectEditorName ) == 0 )
299 {
300 Widget->raise();
301 Widget->setVisible ( true );
302 WidgetFound = true;
303 }
304 }
305 }
306
307 if ( !WidgetFound )
308 {
309 ( new ObjectEditor ( obj ) )->show();
310 }
311 }
312 catch ( daq::dbe::cannot_handle_invalid_qmodelindex const & e )
313 {
314 // nothing to do , maybe the user just clicked the wrong place
315 }
316 }
317}
318//------------------------------------------------------------------------------------------
319
320//------------------------------------------------------------------------------------------
322{
323 model_common_interface * filtermodel = dynamic_cast<model_common_interface *> ( model() );
324 QModelIndex i = currentIndex();
325
326 if ( i.isValid() and filtermodel )
327 {
328 try
329 {
330 ( new ObjectCreator ( filtermodel->getobject ( i ) ) )->show();
331 }
332 catch ( daq::dbe::cannot_handle_invalid_qmodelindex const & e )
333 {
334 //TODO the index can be invalid in this case
335 }
336 }
337}
338//------------------------------------------------------------------------------------------
340{
341 referencedBy ( true );
342}
343
345{
346 referencedBy ( false );
347}
348//------------------------------------------------------------------------------------------
349void dbe::CustomTreeView::closeEvent(QCloseEvent * event) {
350 // Reset the model when the widget is closed
351 // If not done, reference to removed objects may create issues
352 models::treeselection* filtermodel = dynamic_cast<models::treeselection*>(model());
353
354 setModel(nullptr);
355
356 if(filtermodel != nullptr) {
357 filtermodel->ResetQueryObjects();
358 filtermodel->deleteLater();
359 }
360
361 event->accept();
362}
363
364//------------------------------------------------------------------------------------------
366{
367 model_common_interface * filtermodel = dynamic_cast<model_common_interface *> ( model() );
368 QModelIndex Index = currentIndex();
369
370 if ( !Index.isValid() )
371 {
372 return;
373 }
374
375 if ( filtermodel )
376 {
377
378 try
379 {
380 tref obj = filtermodel->getobject ( Index );
381 std::vector<tref> objects;
382
383 if ( not obj.is_null() )
384 {
385 objects = obj.referenced_by ( "*", All );
386 }
387
388 if ( objects.size() > 0 )
389 {
390 dbe::models::tree * Source = dynamic_cast<dbe::models::tree *> ( filtermodel
391 ->ReturnSourceModel() );
393 Selection->SetFilterType ( models::treeselection::ObjectFilterType );
394 Selection->SetQueryObjects ( objects );
395 Selection->setFilterRegExp ( "Dummy" );
396 Selection->setSourceModel ( Source );
397
398 CustomTreeView * tView = new CustomTreeView ( 0 );
399 tView->setModel ( Selection );
400 tView->setWindowTitle ( QString ( "Objects referencing '%1'" ).arg ( obj.UID().c_str() ) );
401 tView->setSortingEnabled ( true );
402 tView->setAlternatingRowColors ( true );
403 tView->resizeColumnToContents ( 0 );
404 tView->resizeColumnToContents ( 1 );
405 tView->setAttribute(Qt::WA_DeleteOnClose);
406
407 connect ( tView, SIGNAL ( doubleClicked ( QModelIndex ) ),
408 tView, SLOT ( slot_edit_object () ) );
409
410 tView->show();
411 }
412 else
413 {
414 if ( All )
415 {
416 WARN ( "No references of only composite objects have been found for object", " ",
417 "with UID:", obj.UID(), "of class", obj.class_name() );
418 }
419 else
420 {
421 WARN ( "No references of objects have been found for object", " ", "with UID:",
422 obj.UID(), "of class", obj.class_name() );
423 }
424 }
425 }
426 catch ( daq::dbe::cannot_handle_invalid_qmodelindex const & e )
427 {}
428 }
429}
430//------------------------------------------------------------------------------------------
431
432//------------------------------------------------------------------------------------------
434{
435 model_common_interface * filtermodel = dynamic_cast<model_common_interface *> ( model() );
436
437 if ( filtermodel )
438 {
439 std::vector<tref> objects;
440
441 if ( !obj.is_null() )
442 {
443 objects = obj.referenced_by ( "*", All );
444 }
445
446 if ( objects.size() > 0 )
447 {
448 dbe::models::tree * Source = dynamic_cast<dbe::models::tree *> ( filtermodel
449 ->ReturnSourceModel() );
451 Selection->SetFilterType ( models::treeselection::ObjectFilterType );
452 Selection->SetQueryObjects ( objects );
453 Selection->setFilterRegExp ( "Dummy" );
454 Selection->setSourceModel ( Source );
455
456 CustomTreeView * tView = new CustomTreeView ( 0 );
457 tView->setModel ( Selection );
458 tView->setSortingEnabled ( true );
459 tView->setAlternatingRowColors ( true );
460 tView->resizeColumnToContents ( 0 );
461 tView->hideColumn ( 1 );
462 //tView->resizeColumnToContents(1);
463 tView->setWindowTitle ( QString ( "Objects referencing '%1'" ).arg ( obj.UID().c_str() ) );
464 tView->setAttribute(Qt::WA_DeleteOnClose);
465
466 connect ( tView, SIGNAL ( doubleClicked ( QModelIndex ) ), tView,
467 SLOT ( slot_edit_object () ) );
468
469 tView->show();
470 }
471 else
472 {
473 if ( All )
474 {
475 WARN ( "No references for object", "Only Composite Objects - Not found", "with UID:",
476 obj.UID() );
477 }
478 else
479 {
480 WARN ( "No references for object", "All Objects - Not found", "with UID:", obj.UID() );
481 }
482 }
483 }
484}
485//------------------------------------------------------------------------------------------
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:274
dunedaq::conffwk::relationship_t relation_t() const
Definition treenode.cpp:406
treenode * GetParent() const
Definition treenode.cpp:111
#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