DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
table.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/table.hpp"
8#include "dbe/treenode.hpp"
10#include "dbe/StyleUtility.hpp"
11#include "dbe/Conversion.hpp"
12#include "dbe/messenger.hpp"
13#include "dbe/Exceptions.hpp"
14#include "dbe/dbcontroller.hpp"
16
17#include <QFont>
18#include <QBrush>
19#include <QMimeData>
20
21#include <bitset>
22
23dbe::models::table::table ( QObject * parent )
24 : QAbstractTableModel ( parent ),
25 enabled ( false )
26{
28}
29
32
33int dbe::models::table::rowCount ( const QModelIndex & parent ) const
34{
35 if ( !parent.isValid() )
36 {
37 return this_structure.size();
38 }
39
40 return 0;
41}
42
43int dbe::models::table::columnCount ( const QModelIndex & parent ) const
44{
45 if( !parent.isValid() ) {
46 return this_headers.size();
47 }
48
49 return 0;
50}
51
52QVariant dbe::models::table::data ( const QModelIndex & index, int role ) const
53{
54
55 if ( index.isValid() )
56 {
57 TableNode * TableItem = getnode ( index );
58
59 if ( role == Qt::DisplayRole )
60 {
61 QString Data;
62
63 for ( QString const & i : TableItem->GetData() )
64 {
65 static QString space
66 { ", " };
67 Data.append(i).append(space);
68 }
69 Data.remove(Data.length() - 2, 2);
70
71 return QVariant ( Data );
72 }
73
74 if ( role == Qt::ToolTipRole )
75 {
76 return TableItem->get_tooltip();
77 }
78 if ( role == Qt::FontRole )
79 {
80 if ( dynamic_cast<TableAttributeNode *> ( TableItem ) )
81 return QFont ( "Helvetica", 10, -1,
82 false );
83 else if ( dynamic_cast<TableRelationshipNode *> ( TableItem ) )
84 return QFont ( "Courier", 10,
85 QFont::Bold );
86 else
87 {
88 return QFont ( "SansSerif", 10, QFont::Bold );
89 }
90 }
91
92 if ( role == Qt::ForegroundRole )
93 {
94 if ( dynamic_cast<TableAttributeNode *> ( TableItem ) )
95 return QBrush (
97 else if ( dynamic_cast<TableRelationshipNode *> ( TableItem ) )
98 return QBrush (
100 else
101 {
102 return QVariant();
103 }
104 }
105
106 if ( role == Qt::BackgroundRole )
107 {
108 auto attr_node = dynamic_cast<TableAttributeNode *> ( TableItem );
109 if ( attr_node != nullptr ) {
110 auto val = attr_node->GetData();
111 if (val.size() == 1 &&
112 val[0].toStdString() == attr_node->GetAttribute().p_default_value) {
113 return QBrush (
115 }
116 }
117 return QBrush (
119 }
120 }
121
122 return QVariant();
123}
124
125bool dbe::models::table::setData ( const QModelIndex & index, const QVariant & value,
126 int role )
127{
128 if ( !index.isValid() || role != Qt::EditRole )
129 {
130 return false;
131 }
132
133 TableNode * TableItem = getnode ( index );
134
135 QStringList OldDataList = TableItem->GetData();
136
137 QStringList NewDataList = value.toStringList();
138
139 if ( NewDataList == OldDataList )
140 {
141 return false;
142 }
143
144 dref obj_desc = this_objects[index.row()];
145
147 { obj_desc.UID(), obj_desc.class_name() } );
148
149 if ( dynamic_cast<TableRelationshipNode *> ( TableItem ) )
150 {
152 dynamic_cast<TableRelationshipNode *> ( TableItem );
153 dunedaq::conffwk::relationship_t RelationshipData = RelationshipNode->GetRelationship();
154 dbe::config::api::set::relation ( Object, RelationshipData, NewDataList );
155 }
156 else if ( dynamic_cast<TableAttributeNode *> ( TableItem ) )
157 {
158 TableAttributeNode * AttributeNode = dynamic_cast<TableAttributeNode *> ( TableItem );
159 dunedaq::conffwk::attribute_t AttributeData = AttributeNode->GetAttribute();
160 dbe::config::api::set::attribute ( Object, AttributeData, NewDataList );
161 }
162
163 return true;
164}
165
166QVariant dbe::models::table::headerData ( int section, Qt::Orientation orientation,
167 int role ) const
168{
169 if ( role == Qt::DisplayRole )
170 {
171 if ( orientation == Qt::Horizontal )
172 {
173 return this_headers.at ( section );
174 }
175 if ( orientation == Qt::Vertical )
176 {
177 return section + 1;
178 }
179 }
180
181 if ( role == Qt::FontRole )
182 {
183 return QFont ( "Helvetica [Cronyx]", 10 );
184 }
185
186 return QVariant();
187}
188
189Qt::ItemFlags dbe::models::table::flags ( const QModelIndex & index ) const
190{
191 if(index.isValid()) {
192 dref obj_desc = this_objects[index.row()];
193 tref Object = dbe::inner::dbcontroller::get ( { obj_desc.UID(), obj_desc.class_name() } );
194
195 if ( confaccessor::check_file_rw ( QString::fromStdString ( Object.contained_in() ) ) )
196 {
197 return ( Qt::ItemIsEditable | Qt::ItemIsEnabled | Qt::ItemIsSelectable |
198 Qt::ItemIsDropEnabled );
199 }
200 }
201
202 return ( Qt::ItemIsEnabled | Qt::ItemIsSelectable );
203}
204
206{
207 return Qt::CopyAction;
208}
209
211{
212 QStringList types;
213 types << "application/vnd.text.list";
214 return types;
215}
216
217bool dbe::models::table::dropMimeData ( const QMimeData * data, Qt::DropAction action,
218 int row,
219 int column, const QModelIndex & parent )
220{
221 Q_UNUSED ( row )
222 Q_UNUSED ( column )
223 Q_UNUSED ( parent )
224
225 bool Accept = true;
226
227 if ( action == Qt::IgnoreAction )
228 {
229 return true;
230 }
231
232 if ( !data->hasFormat ( "application/vnd.text.list" ) )
233 {
234 return false;
235 }
236
238 QByteArray encodedData = data->data ( "application/vnd.text.list" );
239
240 QDataStream stream ( &encodedData, QIODevice::ReadOnly );
241
242 QList<QStringList> newItems;
243
244 while ( !stream.atEnd() )
245 {
246 QStringList text;
247 stream >> text;
248 newItems << text;
249 }
250
251 for ( int i = 0; i < newItems.size(); ++i )
252 {
253 if ( newItems.at ( 0 ).at ( 1 ) != newItems.at ( i ).at ( 1 ) )
254 {
255 Accept = false;
256 }
257 }
258
259 if ( Accept )
260 {
261 BuildTableFromObject ( newItems );
262 emit ResetTab();
263 }
264
265 return Accept;
266}
267
268bool dbe::models::table::BuildTableFromClass ( const QString & cname, bool include_derived )
269{
270 reset ( cname );
271
273
275 cname.toStdString(),
276 false );
277
278 setheader ( classinfo );
279
280 if ( treenode * NodeClass = dbaccess_guard->getnode ( cname ) )
281 {
282 std::vector<treenode *> classnodes
283 { NodeClass };
284
285 if ( include_derived )
286 {
287
288 for ( std::string const & sbcname : classinfo.p_subclasses )
289 {
290 if ( treenode * sbcnode = dbaccess_guard->getnode ( sbcname ) )
291 {
292 classnodes.push_back ( sbcnode );
293 }
294 }
295 }
296
299
300 for ( treenode * clelement : classnodes )
301 {
303
304 for ( treenode * child : clelement->GetChildren() )
305 {
306 this_structure.append ( createrow ( child ) );
307 }
308 }
309
310 enabled = true;
311 return true;
312 }
313 else
314 {
315 return false;
316 }
317}
318
319QList<dbe::models::table::type_datum *> dbe::models::table::createrow (
320 treenode const * rownode )
321{
322
323 dref obj = rownode->GetObject();
324
325 this_objects.append ( obj );
326
328 obj.class_name(),
329 false );
330 std::vector<dunedaq::conffwk::attribute_t> const & attributes = cdef.p_attributes;
331 std::vector<dunedaq::conffwk::relationship_t> const & relations = cdef.p_relationships;
332
333 assert ( attributes.size() + relations.size() < 1025 );
334 std::bitset<1024> hindex; // maximum number of columns to display
335
336 {
337 int column = 0;
338
339 for ( dunedaq::conffwk::attribute_t const & attr : attributes )
340 {
341 hindex.set ( column++, this_headers.contains ( QString::fromStdString ( attr.p_name ) ) );
342 }
343
344 for ( dunedaq::conffwk::relationship_t const & rel : relations )
345 {
346 hindex.set ( column++, this_headers.contains ( QString::fromStdString ( rel.p_name ) ) );
347 }
348 }
349
350 // Create the row for this object
351 QList<TableNode *> Row;
352 Row.append ( new TableNode (
353 QStringList {rownode->GetData ( 0 ).toString()},
354 QVariant(QString::fromStdString(cdef.p_description))));
355 {
356 // Loop over object values and add them to the row
357 // Values are represent as nodes ( attributes or relations ) and these contain
358 // the structured data associated either with a attribute / multi-attribute or a relation
359 std::size_t column = 0;
360
361 for ( treenode * valuenode : rownode->GetChildren() )
362 {
363 QStringList values;
364 // Every valuenode has its attributes and relations defined as its childs
365
366 if ( hindex[column++] )
367 {
368
369 for ( treenode * nodevalues : valuenode->GetChildren() )
370 {
371 // Here we need to filter such that only values corresponding to the header are kept
372 // Add only the first of values in a list of values
373 values.append ( nodevalues->GetData ( 0 ).toString() );
374 }
375
376 if ( AttributeNode * NodeAttribute = dynamic_cast<AttributeNode *> ( valuenode ) )
377 {
378 Row.append ( new TableAttributeNode ( NodeAttribute->attribute_t(), values ) );
379 }
380 else if ( RelationshipNode * NodeRelationship =
381 dynamic_cast<RelationshipNode *> ( valuenode ) )
382 {
383 Row.append ( new TableRelationshipNode ( NodeRelationship->relation_t(), values ) );
384 }
385 }
386 }
387 }
388
389 return Row;
390}
391
392void dbe::models::table::reset ( QString const & cname )
393{
394
395 for ( auto & List : this_structure )
396 {
397 qDeleteAll ( List );
398 }
399
400 this_objects.clear();
401 this_structure.clear();
402 this_headers.clear();
403 this_class_name = cname;
404}
405
407{
408 if ( !this_headers.contains ( "Object Name" ) )
409 {
410 this_headers.append ( "Object Name" );
411 }
412
413 for ( auto & i : cinfo.p_attributes )
414 {
415 if ( !this_headers.contains ( QString::fromStdString ( i.p_name ) ) )
416 {
417 this_headers.append ( QString::fromStdString ( i.p_name ) );
418 }
419 }
420
421 for ( auto & i : cinfo.p_relationships )
422 {
423 if ( !this_headers.contains ( QString::fromStdString ( i.p_name ) ) )
424 {
425 this_headers.append ( QString::fromStdString ( i.p_name ) );
426 }
427 }
428
429}
430
431bool dbe::models::table::BuildTableFromObject ( QList<QStringList> BuildList )
432{
433 reset ( BuildList.at ( 0 ).at ( 1 ) );
434
435 treenode * classnode = confaccessor::gethandler()->getnode ( this_class_name );
436
438 this_class_name.toStdString(),
439 false );
440
441 setheader ( classinfo );
442
443 confaccessor::gethandler()->FetchMore ( classnode );
444
445 for ( const QStringList & i : BuildList )
446 {
447 QString name = i.at ( 0 );
448 treenode * node = confaccessor::gethandler()->getnode ( this_class_name, name );
449 this_structure.append ( createrow ( node ) );
450 }
451
452 enabled = false;
453 return true;
454}
455
457{
458 return this_objects.at ( ObjectIndex ).ref();
459}
460
462{
463 return enabled;
464}
465
467{
468 return this_class_name;
469}
470
471QAbstractItemModel * dbe::models::table::ReturnSourceModel() const
472{
473 return nullptr;
474}
475
477{
478 return &this_objects;
479}
480
481dbe::TableNode * dbe::models::table::getnode ( const QModelIndex & Index ) const
482{
483 if ( Index.isValid() )
484 {
485 return this_structure.at ( Index.row() ).at ( Index.column() );
486 }
487
488 return nullptr;
489}
490
492{
493 beginResetModel();
494 endResetModel();
495}
496
497void dbe::models::table::slot_data_dropped ( QMimeData const & data, Qt::DropAction action )
498{
499 QModelIndex dum;
500 this->dropMimeData ( &data, action, 0, 0, dum );
501}
502
503dbe::tref dbe::models::table::getobject ( QModelIndex const & index ) const
504{
505 if ( index.isValid() )
506 {
507 return this_objects[index.row()].ref();
508 }
509
510 throw daq::dbe::cannot_handle_invalid_qmodelindex ( ERS_HERE );
511}
512
513dunedaq::conffwk::class_t dbe::models::table::getclass ( QModelIndex const & index ) const
514{
515 if ( index.isValid() )
516 {
517 return class_type_info;
518 }
519
520 return dunedaq::conffwk::class_t();
521}
522
523void dbe::models::table::objectsUpdated(const std::vector<dbe::dref>& objects) {
525}
526
527//-----------------------------------------------------------------------------------------------------
529{
530 Q_UNUSED(index);
531 if ( treenode * handlerclass = confaccessor::gethandler()->getnode ( obj.class_name() ) )
532 {
533 if ( obj.class_name() == this_class_name.toStdString()
535 this_class_name.toStdString(), obj.class_name() ) )
536 {
537
538 dbe::treenode const * handlernode = dbe::datahandler::findchild (
539 handlerclass, QString::fromStdString ( obj.UID() ) );
540
541 if ( handlernode == nullptr )
542 {
543 handlernode = new ObjectNode ( obj, false, handlerclass );
544 }
545
546 emit layoutAboutToBeChanged();
547
548 // We assume that the objects are sorted and we want to insert a new element
549
550 tref const handlerobj = handlernode->GetObject();
551 auto sit = this_structure.begin();
552 auto it = this_objects.begin();
553
554 for ( ;
555 it != this_objects.end() and sit != this_structure.end()
556 and it->UID() < handlerobj.UID(); ++it, ++sit )
557 {}
558
559 this_structure.insert ( ++sit, createrow ( handlernode ) );
560
561 this_objects.insert ( ++it, handlerobj );
562
563 // Normally we would have to call changePersistentIndex.
564 // Because an objectnode is created which had no index
565 // before this creation had occured there is no need to call it.
566 emit layoutChanged();
567 }
568 }
569}
570
572{
573 if ( index.isValid() )
574 {
575 try
576 {
577 this->removeRows ( index.row(), 1, index.parent() );
578 }
579 catch ( daq::dbe::ObjectChangeWasNotSuccessful const & err )
580 {
581 WARN ( "Object cannot be deleted", dbe::config::errors::parse ( err ).c_str() );
582 }
583 }
584}
585
587{
588 if ( index.isValid() )
589 {
590 type_datum * element = getnode ( index );
591 element->resetdata ( QStringList ( QString::fromStdString ( obj.ref().UID() ) ) );
592 this_objects[index.row()] = obj.ref();
593 emit dataChanged ( index, index );
594 }
595}
596
598{
599 if ( treenode * handlerclass = confaccessor::gethandler()->getnode ( obj.class_name() ) )
600 {
601 if ( obj.class_name() == this_class_name.toStdString()
603 this_class_name.toStdString(), obj.class_name() ) )
604 {
605 dbe::treenode const * handlernode = dbe::datahandler::findchild (
606 handlerclass, QString::fromStdString ( obj.UID() ) );
607
608 tref handlerobj = handlernode->GetObject();
609
610 auto sit = this_structure.begin();
611 auto it = this_objects.begin();
612
613 // find the object to be updated by looping through both objects and nodes
614
615 for ( ;
616 it != this_objects.end() and sit != this_structure.end()
617 and it->UID() != handlerobj.UID(); ++it, ++sit )
618
619 ;
620
621 // delete all table nodes in the list ( remove elements of the row )
622 for ( TableNode * x : *sit )
623 {
624 delete x;
625 }
626
627 // Recreate the row
628 *sit = createrow ( handlernode );
629
630 int row = index.row() == 0 ? 0 : index.row() - 1;
631
632 int column = index.column() == 0 ? 0 : index.column() - 1;
633
634 emit dataChanged ( createIndex ( row, column ), createIndex ( row + 1, column + 1 ) );
635 }
636 }
637}
638
639//----------------------------------------------------------------------------------------------------
640
641//-----------------------------------------------------------------------------------------------------
642MODEL_COMMON_INTERFACE_SLOTS_DEF ( dbe::models::table )
643//-----------------------------------------------------------------------------------------------------
644
645//-----------------------------------------------------------------------------------------------------
647{
648 for ( int row = 0; row < this_objects.size(); ++row )
649 {
650 dref ListElement = this_objects.at ( row );
651
652 if ( ListElement.UID() == obj.UID() )
653 {
654 return this->index ( row, 0 );
655 }
656 }
657
658 return QModelIndex();
659}
660
661//-----------------------------------------------------------------------------------------------------
662
663//-----------------------------------------------------------------------------------------------------
665{
666 beginRemoveRows ( parent, row, row + count - 1 );
667
668 for ( ; count != 0; --count )
669 {
670 this_structure.removeOne ( this_structure.at ( row + count - 1 ) );
671 this_objects.removeAt ( row + count - 1 );
672 }
673
674 endRemoveRows();
675 return true;
676}
677
678//-----------------------------------------------------------------------------------------------------
#define ERS_HERE
Definition cptr.hpp:55
static QColor TableAttributeHighlightBackground
static QColor TableColorAttribute
Including DBE.
static QColor TableAttributeBackground
static QColor TableColorRelationship
const QVariant & get_tooltip() const
Definition TableNode.hpp:19
virtual QStringList GetData() const
Definition TableNode.cpp:18
static cptr< datahandler > gethandler()
static bool check_file_rw(const QString &FileName)
static bool derived(std::string const &fromclass, std::string const &aclass)
static dunedaq::conffwk::class_t definition(std::string const &cn, bool direct_only)
static treenode * findchild(treenode *top, QString const &name)
static configobject::tref get(dbe::cokey const &desc)
void update_multiple_objects(std::vector< type_object_info > const &)
table(QObject *parent=nullptr)
Including QT Headers.
Definition table.cpp:23
QList< type_datum * > createrow(treenode const *)
Definition table.cpp:319
TableNode * getnode(const QModelIndex &Index) const
Definition table.cpp:481
void reset(QString const &)
Definition table.cpp:392
bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, type_index const &parent) override
Definition table.cpp:217
QList< dbe::dref > * GetTableObjects()
Definition table.cpp:476
Qt::DropActions supportedDropActions() const override
Definition table.cpp:205
int rowCount(type_index const &parent) const override
Definition table.cpp:33
QStringList mimeTypes() const override
Definition table.cpp:210
void setheader(dunedaq::conffwk::class_t const &)
Definition table.cpp:406
QVariant data(type_index const &index, int role) const override
Definition table.cpp:52
bool BuildTableFromObject(QList< QStringList > BuildList)
Definition table.cpp:431
bool is_built() const
Definition table.cpp:461
QVariant headerData(int section, Qt::Orientation orientation, int role) const override
Definition table.cpp:166
Qt::ItemFlags flags(type_index const &index) const override
Definition table.cpp:189
bool setData(type_index const &index, const QVariant &value, int role) override
Definition table.cpp:125
QString get_class_name() const
Definition table.cpp:466
QStringList this_headers
Definition table.hpp:79
QString this_class_name
Definition table.hpp:76
void slot_data_dropped(QMimeData const &, Qt::DropAction)
Definition table.cpp:497
tref GetTableObject(int ObjectIndex) const
Definition table.cpp:456
int columnCount(type_index const &parent) const override
Definition table.cpp:43
bool BuildTableFromClass(const QString &ClassName, bool BuildSubClasses=false)
Definition table.cpp:268
QList< dref > this_objects
Definition table.hpp:80
void objectsUpdated(const std::vector< dbe::dref > &objects)
Definition table.cpp:523
QList< QList< TableNode * > > this_structure
Definition table.hpp:81
virtual tref GetObject() const
Definition treenode.cpp:81
QList< treenode * > GetChildren() const
Definition treenode.cpp:111
virtual QVariant GetData(const int Column, int role=Qt::DisplayRole) const
Definition treenode.cpp:64
#define WARN(...)
Definition messenger.hpp:85
#define MODEL_COMMON_INTERFACE_LOOKUP_IMPL(classname)
#define MODEL_REMOVE_ROWS_DEF(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)
void relation(dbe::inner::configobject::tref src, dunedaq::conffwk::relationship_t const &edge, QStringList const &targets)
void attribute(dbe::inner::configobject::tref objectref, dunedaq::conffwk::attribute_t const &attribute_info, QStringList const &attribute_values)
std::string const parse(ers::Issue const &)
Include QT Headers.
inner::configobject::tref tref
Definition tref.hpp:35
config_object_description dref
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
const std::vector< attribute_t > p_attributes
Definition Schema.hpp:170
const std::vector< relationship_t > p_relationships
Definition Schema.hpp:171
const std::vector< std::string > p_subclasses
Definition Schema.hpp:169
std::string p_description
Definition Schema.hpp:165