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