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