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