Line data Source code
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 :
6 : #include "dbe/StyleUtility.hpp"
7 : #include "dbe/config_api.hpp"
8 : #include "dbe/confaccessor.hpp"
9 : #include "dbe/config_reference.hpp"
10 : #include "dbe/tree.hpp"
11 : #include "dbe/treenode.hpp"
12 : #include "dbe/ui_constants.hpp"
13 : #include "dbe/version.hpp"
14 :
15 : #include <QMimeData>
16 :
17 : char const * const dbe_lib_structure_version = dbe_compiled_version;
18 :
19 0 : dbe::models::tree::tree ( const QStringList & Headers, QObject * parent )
20 : : QAbstractItemModel ( parent ),
21 0 : abstract_classes_selectable ( false )
22 : {
23 0 : confaccessor::gethandler()->root = new dbe::treenode ( Headers );
24 :
25 0 : for ( std::string const & aclass : dbe::config::api::info::onclass::allnames <
26 0 : std::vector<std::string >> () )
27 : {
28 0 : new ClassNode ( dbe::config::api::info::onclass::definition ( aclass, false ),
29 0 : confaccessor::gethandler()->root );
30 0 : }
31 :
32 0 : model_common_connections();
33 0 : }
34 :
35 0 : dbe::models::tree::~tree()
36 0 : {}
37 :
38 0 : dbe::models::tree::type_index dbe::models::tree::index ( int row, int column,
39 : type_index const & parent ) const
40 : {
41 : // This is standard qt implementation for a tree mode;
42 0 : dbe::treenode * up{parent.isValid() ? getnode ( parent ) : confaccessor::gethandler()->root};
43 :
44 0 : if ( dbe::treenode * down = up->GetChild ( row ) )
45 : {
46 0 : return createIndex ( row, column, down );
47 : }
48 : else
49 : {
50 0 : return QModelIndex();
51 : }
52 : }
53 :
54 0 : dbe::models::tree::type_index dbe::models::tree::parent ( const type_index & child ) const
55 : {
56 0 : if ( child.isValid() )
57 : {
58 0 : dbe::treenode * up = getnode ( child )->GetParent();
59 :
60 0 : if ( up != confaccessor::gethandler()->root )
61 : {
62 0 : return createIndex ( up->GetRow(), 0, up );
63 : }
64 : }
65 :
66 0 : return QModelIndex();
67 : }
68 :
69 0 : int dbe::models::tree::rowCount ( const QModelIndex & parent ) const
70 : {
71 0 : dbe::treenode * ParentNode;
72 :
73 0 : if ( !parent.isValid() )
74 : {
75 0 : ParentNode = confaccessor::gethandler()->getnode();
76 : }
77 : else
78 : {
79 0 : ParentNode = getnode ( parent );
80 : }
81 :
82 0 : return ParentNode->ChildCount();
83 : }
84 :
85 0 : int dbe::models::tree::columnCount ( const type_index & parent ) const
86 : {
87 0 : if ( parent.isValid() )
88 : {
89 0 : dbe::treenode * DataNode = getnode ( parent );
90 :
91 0 : return DataNode->ColumnCount();
92 : }
93 : else
94 : {
95 0 : return confaccessor::gethandler()->root->ColumnCount();
96 : }
97 : }
98 :
99 0 : Qt::ItemFlags dbe::models::tree::flags ( type_index const & index ) const
100 : {
101 0 : treenode * node = getnode ( index );
102 :
103 0 : if ( ClassNode * classnode = dynamic_cast<ClassNode *> ( node ) )
104 : {
105 0 : dunedaq::conffwk::class_t classinfo = classnode->GetClassInfo();
106 :
107 0 : if ( classinfo.p_abstract )
108 : {
109 0 : return
110 0 : abstract_classes_selectable ?
111 0 : ( Qt::ItemIsEnabled | Qt::ItemIsSelectable ) : ( Qt::ItemIsSelectable );
112 : }
113 : else
114 : {
115 0 : return ( Qt::ItemIsEnabled | Qt::ItemIsSelectable );
116 : }
117 0 : }
118 :
119 0 : return ( Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsDragEnabled );
120 : }
121 :
122 0 : QVariant dbe::models::tree::data ( type_index const & index, int role ) const
123 : {
124 0 : if ( index.isValid() )
125 : {
126 0 : switch ( role )
127 : {
128 0 : case Qt::ForegroundRole:
129 0 : if ( ObjectNode * onode = dynamic_cast<ObjectNode *> ( getnode ( index ) ) )
130 : {
131 0 : try
132 : {
133 0 : tref const & obj = onode->GetObject();
134 :
135 0 : if ( confaccessor::check_file_rw ( QString::fromStdString ( obj.contained_in() ) ) )
136 : {
137 0 : return QVariant ( StyleUtility::ObjectForeground );
138 : }
139 : else
140 : {
141 0 : return QVariant ( StyleUtility::FileReadOnlyForeground );
142 : }
143 0 : }
144 0 : catch ( daq::dbe::config_object_retrieval_result_is_null const & e )
145 : {
146 : // nothing to do the onode refers to a removed object and has not yet been removed
147 0 : return QVariant();
148 0 : }
149 : }
150 :
151 : break;
152 :
153 0 : case Qt::BackgroundRole:
154 0 : if ( ObjectNode * onode = dynamic_cast<ObjectNode *> ( getnode ( index ) ) )
155 : {
156 0 : try
157 : {
158 0 : tref const & obj = onode->GetObject();
159 :
160 0 : if ( confaccessor::check_file_rw ( QString::fromStdString ( obj.contained_in() ) ) )
161 : {
162 0 : return QVariant ( StyleUtility::ObjectBackground );
163 : }
164 : else
165 : {
166 0 : return QVariant ( StyleUtility::FileReadOnlyBackground );
167 : }
168 0 : }
169 0 : catch ( daq::dbe::config_object_retrieval_result_is_null const & e )
170 : {
171 : // nothing to do the onode refers to a removed object and has not yet been removed
172 0 : return QVariant();
173 0 : }
174 : }
175 :
176 : break;
177 :
178 0 : case Qt::ToolTipRole:
179 0 : if ( auto cnode = dynamic_cast<ClassNode *> ( getnode ( index ) ) )
180 : {
181 0 : return cnode->GetData ( index.column(), role );
182 : }
183 : break;
184 :
185 0 : case Qt::DisplayRole:
186 0 : case Qt::DecorationRole:
187 0 : return getnode ( index )->GetData ( index.column(), role );
188 : }
189 : }
190 :
191 0 : return QVariant();
192 : }
193 :
194 0 : QVariant dbe::models::tree::headerData ( int section, Qt::Orientation orientation,
195 : int role ) const
196 : {
197 0 : if ( ( orientation == Qt::Horizontal ) && ( role == Qt::DisplayRole ) )
198 : {
199 0 : return confaccessor::gethandler()->root->GetData ( section );
200 : }
201 :
202 0 : return QVariant();
203 : }
204 :
205 0 : bool dbe::models::tree::insertRows ( int position, int rows, const QModelIndex & parent )
206 : {
207 0 : if ( ClassNode * onode = dynamic_cast<ClassNode *> ( getnode ( parent ) ) )
208 : {
209 0 : rows = onode->ChildCount();
210 :
211 0 : beginInsertRows ( parent, position, position + rows - 1 );
212 :
213 0 : std::string const & classname = onode->GetData (
214 0 : static_cast<int> ( tablepositions::classname ) ).toString().toStdString();
215 :
216 0 : std::vector<tref> const & objects = config::api::info::onclass::objects ( classname,
217 0 : false );
218 :
219 0 : for ( auto const & i : objects )
220 : {
221 0 : new ObjectNode ( i, false, onode );
222 0 : emit ObjectFile(QString::fromStdString(i.contained_in()));
223 : }
224 :
225 0 : endInsertRows();
226 :
227 0 : return true;
228 0 : }
229 : else
230 : {
231 : return false;
232 : }
233 : }
234 :
235 0 : bool dbe::models::tree::canFetchMore ( type_index const & parent ) const
236 : {
237 0 : if ( parent.isValid() )
238 : {
239 0 : treenode * ParentNode = getnode ( parent );
240 0 : return ( !ParentNode->GetWasFetched() );
241 : }
242 :
243 : return false;
244 : }
245 :
246 0 : void dbe::models::tree::fetchMore ( type_index const & parent )
247 : {
248 0 : if ( parent.isValid() )
249 : {
250 0 : treenode * ParentNode = getnode ( parent );
251 :
252 0 : if ( !ParentNode->GetWasFetched() )
253 : {
254 0 : insertRows ( 0, 0, parent );
255 0 : ParentNode->SetWasFetched ( true );
256 : }
257 : }
258 0 : }
259 :
260 0 : bool dbe::models::tree::hasChildren ( const QModelIndex & parent ) const
261 : {
262 0 : if ( parent.isValid() )
263 : {
264 0 : treenode * ParentNode = getnode ( parent );
265 0 : return ( ParentNode->GetHasStructure() );
266 : }
267 :
268 : return true;
269 : }
270 :
271 0 : bool dbe::models::tree::setData ( type_index const & index, const QVariant & value,
272 : int role )
273 : {
274 0 : Q_UNUSED ( index )
275 0 : Q_UNUSED ( value )
276 0 : Q_UNUSED ( role )
277 :
278 0 : return true;
279 : }
280 :
281 0 : QStringList dbe::models::tree::mimeTypes() const
282 : {
283 0 : QStringList types;
284 0 : types << "application/vnd.text.list";
285 0 : return types;
286 0 : }
287 :
288 0 : QMimeData * dbe::models::tree::mimeData ( const QModelIndexList & indexes ) const
289 : {
290 0 : QMimeData * mimeData = new QMimeData();
291 0 : QByteArray encodedData;
292 :
293 0 : QDataStream stream ( &encodedData, QIODevice::WriteOnly );
294 :
295 0 : foreach ( QModelIndex index, indexes )
296 : {
297 0 : if ( index.isValid() && index.column() == 0 )
298 : {
299 0 : QStringList Text;
300 0 : treenode * NodeObject = getnode ( index );
301 :
302 0 : tref Object = NodeObject->GetObject();
303 0 : QString ObjectUid = QString::fromStdString ( Object.UID() );
304 0 : QString ObjectClassName = QString::fromStdString ( Object.class_name() );
305 :
306 0 : Text.append ( ObjectUid );
307 0 : Text.append ( ObjectClassName );
308 :
309 0 : stream << Text;
310 0 : }
311 0 : }
312 :
313 0 : mimeData->setData ( "application/vnd.text.list", encodedData );
314 0 : return mimeData;
315 0 : }
316 :
317 0 : QModelIndex dbe::models::tree::getindex ( treenode * NodeItem,
318 : type_index const & RootIndex ) const
319 : {
320 0 : for ( int i = 0; i < rowCount ( RootIndex ); ++i )
321 : {
322 0 : QModelIndex ChildIndex = index ( i, 0, RootIndex );
323 0 : treenode * ChildNode = getnode ( ChildIndex );
324 :
325 0 : if ( NodeItem == ChildNode )
326 : {
327 0 : return ChildIndex;
328 : }
329 :
330 0 : QModelIndex ChildChildIndex = getindex ( NodeItem, ChildIndex );
331 :
332 0 : if ( ChildChildIndex.isValid() )
333 : {
334 0 : return ChildChildIndex;
335 : }
336 : }
337 :
338 0 : return QModelIndex();
339 : }
340 :
341 0 : void dbe::models::tree::ResetModel()
342 : {
343 0 : beginResetModel();
344 0 : endResetModel();
345 0 : }
346 :
347 0 : dbe::models::tree::type_datum * dbe::models::tree::getnode ( type_index const & index )
348 : const
349 : {
350 0 : if ( index.isValid() )
351 : {
352 0 : return static_cast<treenode *> ( index.internalPointer() );
353 : }
354 :
355 0 : return confaccessor::gethandler()->root;
356 : }
357 :
358 0 : dbe::tref dbe::models::tree::getobject ( const QModelIndex & index ) const
359 : {
360 0 : if ( index.isValid() )
361 : {
362 0 : if ( ObjectNode * ObjectItem = dynamic_cast<ObjectNode *> ( getnode ( index ) ) )
363 : {
364 0 : return ObjectItem->GetObject();
365 : }
366 : }
367 :
368 0 : throw daq::dbe::cannot_handle_invalid_qmodelindex ( ERS_HERE );
369 :
370 : }
371 :
372 0 : dunedaq::conffwk::class_t dbe::models::tree::getclass ( type_index const & index ) const
373 : {
374 0 : treenode * Item = getnode ( index );
375 :
376 0 : if ( Item != 0 )
377 : {
378 0 : ClassNode * ClassItem = dynamic_cast<ClassNode *> ( Item );
379 :
380 0 : if ( ClassItem != nullptr )
381 : {
382 0 : return ClassItem->GetClassInfo();
383 : }
384 :
385 0 : ObjectNode * ObjectItem = dynamic_cast<ObjectNode *> ( Item );
386 :
387 0 : if ( ObjectItem != nullptr )
388 : {
389 0 : tref Object = ObjectItem->GetObject();
390 0 : return dbe::config::api::info::onclass::definition ( Object.class_name(), false );
391 0 : }
392 :
393 0 : RelationshipNode * RelationshipItem = dynamic_cast<RelationshipNode *> ( Item );
394 :
395 0 : if ( RelationshipItem != nullptr )
396 : {
397 0 : return dbe::config::api::info::onclass::definition (
398 0 : RelationshipItem->relation_t().p_type,
399 0 : false );
400 : }
401 : }
402 :
403 0 : return dunedaq::conffwk::class_t();
404 : }
405 :
406 0 : QAbstractItemModel * dbe::models::tree::ReturnSourceModel() const
407 : {
408 0 : return nullptr;
409 : }
410 :
411 0 : void dbe::models::tree::ToggleAbstractClassesSelectable ( bool const val )
412 : {
413 0 : abstract_classes_selectable = val;
414 0 : ResetModel();
415 0 : }
416 :
417 0 : void dbe::models::tree::objectsUpdated(const std::vector<dbe::dref>& objects) {
418 0 : update_multiple_objects(objects);
419 0 : }
420 :
421 : //----------------------------------------------------------------------------------------------------
422 :
423 : //----------------------------------------------------------------------------------------------------
424 0 : MODEL_COMMON_INTERFACE_LOOKUP_IMPL ( dbe::models::tree )
425 : {
426 0 : if ( treenode * classnode = confaccessor::gethandler()->getnode (
427 0 : QString::fromStdString ( obj.class_name() ) ) )
428 : {
429 :
430 0 : auto found = [&obj, classnode] ( int i )
431 : {
432 0 : return obj.UID() == classnode->GetChild ( i )->GetData ( 0 ).toString().toStdString();
433 0 : };
434 :
435 0 : int i = 0;
436 0 : int const childs = classnode->ChildCount();
437 :
438 0 : for ( ; i < childs and not found ( i ); ++i )
439 :
440 : ;
441 :
442 0 : QModelIndex parent_index = getindex ( classnode );
443 :
444 0 : return childs == i ? QModelIndex() : index ( i, parent_index.column(), parent_index );
445 : }
446 :
447 0 : return QModelIndex();
448 : }
449 :
450 0 : MODEL_COMMON_INTERFACE_CREATE_THAT_OBJ_IMPL ( dbe::models::tree )
451 : {
452 0 : Q_UNUSED(index);
453 0 : if ( treenode * classnode = confaccessor::gethandler()->getnode ( obj.class_name() ) )
454 : {
455 0 : if ( not dbe::datahandler::findchild ( classnode, QString::fromStdString ( obj.UID() ) ) )
456 : {
457 0 : layoutAboutToBeChanged();
458 0 : new ObjectNode ( obj, false, classnode );
459 : // Normally we would have to call changePersistentIndex.
460 : // Because an objectnode is created which had no index
461 : // before this creation had occured there is no need to call it.
462 0 : emit layoutChanged();
463 : }
464 : }
465 0 : }
466 :
467 0 : MODEL_COMMON_INTERFACE_DELETE_THAT_OBJ_IMPL ( dbe::models::tree )
468 : {
469 0 : if ( index.isValid() )
470 : {
471 0 : this->removeRows ( index.row(), 1, index.parent() );
472 : }
473 0 : }
474 :
475 0 : MODEL_COMMON_INTERFACE_RENAME_THAT_OBJ_IMPL ( dbe::models::tree )
476 : {
477 0 : if ( index.isValid() )
478 : {
479 0 : treenode * child = getnode ( index );
480 0 : child->rename ( QString::fromStdString ( obj.ref().UID() ) );
481 0 : emit dataChanged ( index, index );
482 : }
483 0 : }
484 :
485 0 : MODEL_COMMON_INTERFACE_UPDATE_THAT_OBJ_IMPL ( dbe::models::tree )
486 : {
487 : // Some form of change has occured , we remove and insert a new object node
488 : // for the specified object
489 0 : if ( index.isValid() )
490 : {
491 : // This in the worst case return a pointer to root in the tree
492 0 : treenode * child = getnode ( index );
493 :
494 0 : if ( treenode * parent = child->GetParent() )
495 : {
496 0 : emit layoutAboutToBeChanged();
497 0 : parent->RemoveChild ( child );
498 0 : new ObjectNode ( obj, false, parent );
499 0 : emit layoutChanged();
500 : }
501 : }
502 0 : }
503 :
504 : //----------------------------------------------------------------------------------------------------
505 :
506 : //----------------------------------------------------------------------------------------------------
507 0 : TREEMODEL_REMOVE_ROWS_DEF ( dbe::models::tree )
508 0 : MODEL_COMMON_INTERFACE_SLOTS_DEF ( dbe::models::tree )
509 : //----------------------------------------------------------------------------------------------------
|