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::ToolTipRole:
148 if ( auto cnode = dynamic_cast<ClassNode *> ( getnode ( index ) ) )
149 {
150 return cnode->GetData ( index.column(), role );
151 }
152 break;
153
154 case Qt::DisplayRole:
155 case Qt::DecorationRole:
156 return getnode ( index )->GetData ( index.column(), role );
157 }
158 }
159
160 return QVariant();
161}
162
163QVariant dbe::models::tree::headerData ( int section, Qt::Orientation orientation,
164 int role ) const
165{
166 if ( ( orientation == Qt::Horizontal ) && ( role == Qt::DisplayRole ) )
167 {
168 return confaccessor::gethandler()->root->GetData ( section );
169 }
170
171 return QVariant();
172}
173
174bool dbe::models::tree::insertRows ( int position, int rows, const QModelIndex & parent )
175{
176 if ( ClassNode * onode = dynamic_cast<ClassNode *> ( getnode ( parent ) ) )
177 {
178 rows = onode->ChildCount();
179
180 beginInsertRows ( parent, position, position + rows - 1 );
181
182 std::string const & classname = onode->GetData (
183 static_cast<int> ( tablepositions::classname ) ).toString().toStdString();
184
185 std::vector<tref> const & objects = config::api::info::onclass::objects ( classname,
186 false );
187
188 for ( auto const & i : objects )
189 {
190 new ObjectNode ( i, false, onode );
191 emit ObjectFile(QString::fromStdString(i.contained_in()));
192 }
193
194 endInsertRows();
195
196 return true;
197 }
198 else
199 {
200 return false;
201 }
202}
203
204bool dbe::models::tree::canFetchMore ( type_index const & parent ) const
205{
206 if ( parent.isValid() )
207 {
208 treenode * ParentNode = getnode ( parent );
209 return ( !ParentNode->GetWasFetched() );
210 }
211
212 return false;
213}
214
215void dbe::models::tree::fetchMore ( type_index const & parent )
216{
217 if ( parent.isValid() )
218 {
219 treenode * ParentNode = getnode ( parent );
220
221 if ( !ParentNode->GetWasFetched() )
222 {
223 insertRows ( 0, 0, parent );
224 ParentNode->SetWasFetched ( true );
225 }
226 }
227}
228
229bool dbe::models::tree::hasChildren ( const QModelIndex & parent ) const
230{
231 if ( parent.isValid() )
232 {
233 treenode * ParentNode = getnode ( parent );
234 return ( ParentNode->GetHasStructure() );
235 }
236
237 return true;
238}
239
240bool dbe::models::tree::setData ( type_index const & index, const QVariant & value,
241 int role )
242{
243 Q_UNUSED ( index )
244 Q_UNUSED ( value )
245 Q_UNUSED ( role )
246
247 return true;
248}
249
251{
252 QStringList types;
253 types << "application/vnd.text.list";
254 return types;
255}
256
257QMimeData * dbe::models::tree::mimeData ( const QModelIndexList & indexes ) const
258{
259 QMimeData * mimeData = new QMimeData();
260 QByteArray encodedData;
261
262 QDataStream stream ( &encodedData, QIODevice::WriteOnly );
263
264 foreach ( QModelIndex index, indexes )
265 {
266 if ( index.isValid() && index.column() == 0 )
267 {
268 QStringList Text;
269 treenode * NodeObject = getnode ( index );
270
271 tref Object = NodeObject->GetObject();
272 QString ObjectUid = QString::fromStdString ( Object.UID() );
273 QString ObjectClassName = QString::fromStdString ( Object.class_name() );
274
275 Text.append ( ObjectUid );
276 Text.append ( ObjectClassName );
277
278 stream << Text;
279 }
280 }
281
282 mimeData->setData ( "application/vnd.text.list", encodedData );
283 return mimeData;
284}
285
286QModelIndex dbe::models::tree::getindex ( treenode * NodeItem,
287 type_index const & RootIndex ) const
288{
289 for ( int i = 0; i < rowCount ( RootIndex ); ++i )
290 {
291 QModelIndex ChildIndex = index ( i, 0, RootIndex );
292 treenode * ChildNode = getnode ( ChildIndex );
293
294 if ( NodeItem == ChildNode )
295 {
296 return ChildIndex;
297 }
298
299 QModelIndex ChildChildIndex = getindex ( NodeItem, ChildIndex );
300
301 if ( ChildChildIndex.isValid() )
302 {
303 return ChildChildIndex;
304 }
305 }
306
307 return QModelIndex();
308}
309
311{
312 beginResetModel();
313 endResetModel();
314}
315
317const
318{
319 if ( index.isValid() )
320 {
321 return static_cast<treenode *> ( index.internalPointer() );
322 }
323
324 return confaccessor::gethandler()->root;
325}
326
327dbe::tref dbe::models::tree::getobject ( const QModelIndex & index ) const
328{
329 if ( index.isValid() )
330 {
331 if ( ObjectNode * ObjectItem = dynamic_cast<ObjectNode *> ( getnode ( index ) ) )
332 {
333 return ObjectItem->GetObject();
334 }
335 }
336
337 throw daq::dbe::cannot_handle_invalid_qmodelindex ( ERS_HERE );
338
339}
340
341dunedaq::conffwk::class_t dbe::models::tree::getclass ( type_index const & index ) const
342{
343 treenode * Item = getnode ( index );
344
345 if ( Item != 0 )
346 {
347 ClassNode * ClassItem = dynamic_cast<ClassNode *> ( Item );
348
349 if ( ClassItem != nullptr )
350 {
351 return ClassItem->GetClassInfo();
352 }
353
354 ObjectNode * ObjectItem = dynamic_cast<ObjectNode *> ( Item );
355
356 if ( ObjectItem != nullptr )
357 {
358 tref Object = ObjectItem->GetObject();
359 return dbe::config::api::info::onclass::definition ( Object.class_name(), false );
360 }
361
362 RelationshipNode * RelationshipItem = dynamic_cast<RelationshipNode *> ( Item );
363
364 if ( RelationshipItem != nullptr )
365 {
367 RelationshipItem->relation_t().p_type,
368 false );
369 }
370 }
371
373}
374
375QAbstractItemModel * dbe::models::tree::ReturnSourceModel() const
376{
377 return nullptr;
378}
379
381{
382 abstract_classes_selectable = val;
383 ResetModel();
384}
385
386void dbe::models::tree::objectsUpdated(const std::vector<dbe::dref>& objects) {
387 update_multiple_objects(objects);
388}
389
390//----------------------------------------------------------------------------------------------------
391
392//----------------------------------------------------------------------------------------------------
394{
395 if ( treenode * classnode = confaccessor::gethandler()->getnode (
396 QString::fromStdString ( obj.class_name() ) ) )
397 {
398
399 auto found = [&obj, classnode] ( int i )
400 {
401 return obj.UID() == classnode->GetChild ( i )->GetData ( 0 ).toString().toStdString();
402 };
403
404 int i = 0;
405 int const childs = classnode->ChildCount();
406
407 for ( ; i < childs and not found ( i ); ++i )
408
409 ;
410
411 QModelIndex parent_index = getindex ( classnode );
412
413 return childs == i ? QModelIndex() : index ( i, parent_index.column(), parent_index );
414 }
415
416 return QModelIndex();
417}
418
420{
421 Q_UNUSED(index);
422 if ( treenode * classnode = confaccessor::gethandler()->getnode ( obj.class_name() ) )
423 {
424 if ( not dbe::datahandler::findchild ( classnode, QString::fromStdString ( obj.UID() ) ) )
425 {
426 layoutAboutToBeChanged();
427 new ObjectNode ( obj, false, classnode );
428 // Normally we would have to call changePersistentIndex.
429 // Because an objectnode is created which had no index
430 // before this creation had occured there is no need to call it.
431 emit layoutChanged();
432 }
433 }
434}
435
437{
438 if ( index.isValid() )
439 {
440 this->removeRows ( index.row(), 1, index.parent() );
441 }
442}
443
445{
446 if ( index.isValid() )
447 {
448 treenode * child = getnode ( index );
449 child->rename ( QString::fromStdString ( obj.ref().UID() ) );
450 emit dataChanged ( index, index );
451 }
452}
453
455{
456 // Some form of change has occured , we remove and insert a new object node
457 // for the specified object
458 if ( index.isValid() )
459 {
460 // This in the worst case return a pointer to root in the tree
461 treenode * child = getnode ( index );
462
463 if ( treenode * parent = child->GetParent() )
464 {
465 emit layoutAboutToBeChanged();
466 parent->RemoveChild ( child );
467 new ObjectNode ( obj, false, parent );
468 emit layoutChanged();
469 }
470 }
471}
472
473//----------------------------------------------------------------------------------------------------
474
475//----------------------------------------------------------------------------------------------------
478//----------------------------------------------------------------------------------------------------
#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:386
void fetchMore(const type_index &parent) override
Definition tree.cpp:215
QVariant data(const type_index &index, int role=Qt::DisplayRole) const override
Definition tree.cpp:116
void ResetModel()
Definition tree.cpp:310
QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const override
Definition tree.cpp:163
type_index getindex(treenode *NodeItem, type_index const &RootIndex=QModelIndex()) const
Definition tree.cpp:286
bool setData(const type_index &index, const QVariant &value, int role) override
Definition tree.cpp:240
type_datum * getnode(const type_index &index) const override
Definition tree.cpp:316
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:204
bool insertRows(int position, int rows, const type_index &parent) override
Definition tree.cpp:174
void ToggleAbstractClassesSelectable(bool)
Definition tree.cpp:380
QStringList mimeTypes() const override
Definition tree.cpp:250
bool hasChildren(const type_index &parent) const override
Definition tree.cpp:229
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:257
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:121
treenode * GetParent() const
Definition treenode.cpp:111
bool GetHasStructure() const
Definition treenode.cpp:131
virtual tref GetObject() const
Definition treenode.cpp:76
treenode * GetChild(const int Row) const
Definition treenode.cpp:101
int ChildCount() const
Definition treenode.cpp:116
bool GetWasFetched() const
Definition treenode.cpp:141
void SetWasFetched(bool Fetched)
Definition treenode.cpp:136
int GetRow() const
Definition treenode.cpp:81
#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