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