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