DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
ObjectCreator.cpp
Go to the documentation of this file.
1#include "dbe/messenger.hpp"
4#include "dbe/Validator.hpp"
6#include "ui_ObjectCreator.h"
10#include "dbe/config_api.hpp"
11
12#include "logging/Logging.hpp"
13
14#include <boost/scope_exit.hpp>
15
16#include <QCompleter>
17#include <QMenu>
18#include <QFileDialog>
19#include <QFileInfo>
20#include <QCloseEvent>
21#include <QMessageBox>
22
31
32//------------------------------------------------------------------------------------------
34//------------------------------------------------------------------------------------------
35
36//------------------------------------------------------------------------------------------
37/*
38 * Create an object in a class
39 */
41 :
42 QWidget ( parent ),
43 ui ( new Ui::ObjectCreator ),
44 this_object_class ( cinfo ),
45 this_target_object ( nullptr ),
46 this_relation ( dunedaq::conffwk::relationship_t() ),
47 this_files ( nullptr ),
48 this_status_bar ( nullptr ),
49 this_state ( 0 ),
50 UidSet ( false ),
51 this_associated_editor ( nullptr ),
52 ContextMenu ( nullptr ),
53 this_is_temporary ( false ),
54 this_object_changed ( false ),
55 this_create_copy ( false ),
56 uuid ( QUuid::createUuid() )
57{
58 ui->setupUi ( this );
59 this_status_bar = new QStatusBar ( ui->StatusFrame );
60
61 QString cname = QString::fromStdString ( this_object_class.p_name );
62 setWindowTitle ( QString ( "Create New Object of Class %1" ).arg ( cname ) );
63
65 ui->LineEditUID->setToolTip ( QString ( "Type Object unique ID and press ENTER" ) );
66
67 ui->ComboBoxForbiddenUid->hide();
68 ui->SetFileButton->hide();
69
74
75 int index = ui->ComboBoxClass->findText ( cname );
76 ui->ComboBoxClass->setCurrentIndex ( 1 ); // If index is 0 somehow this doesnt work
77 ui->ComboBoxClass->setCurrentIndex ( index ); // Now set it to the right value posssibly 0
79 ui->ComboBoxClass->setEditText ( cname );
80
81 ui->SetUidButton->setVisible ( false );
83
84 QSize SplitterSize = ui->splitter->size();
85 int Width = SplitterSize.width() / 2;
86
87 QList<int> List;
88 List.push_back ( Width );
89 List.push_back ( Width );
90
91 ui->splitter->setSizes ( List );
92}
93
94//------------------------------------------------------------------------------------------
95
96//------------------------------------------------------------------------------------------
97/*
98 * Clone an object in a class , uid will be required to be set explicitly
99 */
101 dunedaq::conffwk::relationship_t const & the_relation,
102 QWidget * parent )
103 :
105 config::api::info::onclass::definition ( clonefrom.class_name(), false ), parent )
106{
107 this_target_object.reset ( new tref ( clonefrom ) );
108 this_relation = the_relation;
109}
110
111//------------------------------------------------------------------------------------------
112
113//------------------------------------------------------------------------------------------
114dbe::ObjectCreator::ObjectCreator ( tref const & clonefrom, QWidget * parent )
115 :
116 QWidget ( parent ),
117 ui ( new Ui::ObjectCreator ),
118 this_object_class (
119 config::api::info::onclass::definition ( clonefrom.class_name(), false ) ),
120 this_src_object ( new tref ( clonefrom ) ),
121 this_files ( nullptr ),
122 this_status_bar ( nullptr ),
123 UidSet ( false ),
124 this_associated_editor ( nullptr ),
125 ContextMenu ( nullptr ),
126 this_is_temporary ( false ),
127 this_object_changed ( false ),
128 this_create_copy ( true ),
129 uuid ( QUuid::createUuid() )
130{
131 ui->setupUi ( this );
132 this_status_bar = new QStatusBar ( ui->StatusFrame );
133
134 setWindowTitle (
135 QString ( "Create New Object of Class %1" ).arg (
136 QString::fromStdString ( this_object_class.p_name ) ) );
137
139 ui->LineEditUID->setToolTip ( QString ( "Type Object unique ID and press ENTER" ) );
140
141 ui->ComboBoxForbiddenUid->hide();
142 ui->SetFileButton->hide();
143
146 SetStatusBar();
148
149 int index = ui->ComboBoxClass->findText ( QString::fromStdString (
150 clonefrom.class_name() ) );
151
152 ui->ComboBoxClass->setCurrentIndex ( index );
154
156 QString const & fn = QString::fromStdString ( this_src_object->contained_in() );
157 QFileInfo fileinfo ( fn );
158 QModelIndexList ListOfMatch = this_files->match (
159 this_files->index ( 0, 0 ),
160 Qt::DisplayRole, fileinfo.fileName(), 1000,
161 Qt::MatchContains | Qt::MatchWrap | Qt::MatchRecursive );
162
163 ui->FileView->selectRow ( ListOfMatch.at ( 0 ).row() );
164
165 ActiveFileChanged ( fn );
166
167 // Set uid to the that of the object being copied
168 ui->LineEditUID->setText ( QString::fromStdString ( clonefrom.UID() ) );
169 ui->ComboBoxClass->setEditText ( QString::fromStdString ( clonefrom.class_name() ) );
170
171 ui->SetUidButton->setVisible ( false );
173
174 QSize spliter_size = ui->splitter->size();
175 int w = spliter_size.width() / 2;
176 ui->splitter->setSizes ( QList<int>
177 { w, w } );
178}
179
180//------------------------------------------------------------------------------------------
181
182//------------------------------------------------------------------------------------------
184{
185 QStringList ListOfClasses ( dbe::config::api::info::onclass::allnames<QStringList>() );
186 ui->ComboBoxClass->addItems ( ListOfClasses );
187
188 QCompleter * completer = new QCompleter ( ui->ComboBoxClass->model(), ui->ComboBoxClass );
189 completer->setCaseSensitivity ( Qt::CaseInsensitive );
190 completer->setFilterMode(Qt::MatchContains);
191 ui->ComboBoxClass->setCompleter ( completer );
192 ui->ComboBoxClass->setEditable ( true );
193
194 QVariant VarFromList ( ListOfClasses );
195 ValidatorAcceptMatch * Validator = new ValidatorAcceptMatch ( VarFromList, this );
196 ui->ComboBoxClass->setValidator ( Validator );
197}
198
199
201{
202 connect ( ui->FileView, SIGNAL ( stateChanged ( const QString & ) ),
203 this, SLOT ( ActiveFileChanged ( const QString & ) ),
204 Qt::UniqueConnection );
205
206 connect ( ui->CreateObjectButton, SIGNAL ( clicked() ), this, SLOT ( CreateObject() ),
207 Qt::UniqueConnection );
208
209 connect ( ui->CreateOpenObjectButton, SIGNAL ( clicked() ), this, SLOT ( CreateOpenObject() ),
210 Qt::UniqueConnection );
211
212 connect ( ui->ExitButton, SIGNAL ( clicked() ), this, SLOT ( close() ),
213 Qt::UniqueConnection );
214
215 connect ( ui->LineEditUID, SIGNAL ( textChanged ( QString ) ), this,
216 SLOT ( UpdateActions ( QString ) ),
217 Qt::UniqueConnection );
218
219 connect ( ui->LineEditUID, SIGNAL ( textEdited ( const QString & ) ),
220 this, SLOT ( MustPressReturn ( const QString & ) ),
221 Qt::UniqueConnection );
222
223 connect ( this, SIGNAL ( stateChanged() ), this, SLOT ( UpdateActions() ),
224 Qt::UniqueConnection );
225
226 connect ( this, SIGNAL ( stateChanged() ), this, SLOT ( SetObjectChanged() ),
227 Qt::UniqueConnection );
228}
229
231{
232 if ( not confaccessor::db_implementation_name().contains ( "roksconflibs" ) )
233 {
234 if ( this_files != nullptr )
235 {
236 delete this_files;
237 this_files = nullptr;
238 }
239
240 this_files = new FileModel ( confaccessor::ref().GetIncludedFileCache() );
241
242 this_sort.setSourceModel ( this_files );
243 ui->FileView->setModel ( &this_sort );
244 ui->FileView->sortByColumn ( 2, Qt::DescendingOrder );
245 }
246}
247
249{
250 this_status_bar->setSizeGripEnabled ( false );
251 this_status_bar->setAutoFillBackground ( true );
252 this_status_bar->showMessage (
253 QString ( "Select class, set new object UID, select file where to create object!" ) );
254 ui->StatusFrame->setFrameStyle ( QFrame::NoFrame );
255 ui->StatusLayout->addWidget ( this_status_bar );
256}
257
259{
260 bool ReleaseEditor = true;
261 this_state = editorOk;
262
263 if ( !ui->LineEditUID->IsValid() || ui->LineEditUID->text().isEmpty() )
264 {
265 this_state |= uidNotSet;
266 ReleaseEditor = false;
267 this_newuid = QString();
268 }
269 else
270 {
271 this_state &= ~uidNotSet;
272 this_newuid = ui->LineEditUID->text();
273 }
274
275 if ( this_file_for_new_object.isEmpty() )
276 {
277 this_state |= fileNotSet;
278 ReleaseEditor = false;
279 }
280 else
281 {
282 this_state &= ~fileNotSet;
283 }
284
285 if ( ReleaseEditor )
286 {
287 this_associated_editor->setEnabled ( true );
288 }
289
290 if ( this_associated_editor != nullptr )
291 {
292 if ( !this_associated_editor->IsEditorValid() )
293 {
294 this_state |= nullNotFilled;
295 }
296 else
297 {
298 this_state &= ~nullNotFilled;
299 }
300 }
301
302 if ( this_state == editorOk )
303 {
304 ui->CreateObjectButton->setEnabled ( true );
305 ui->CreateObjectButton->setPalette ( StyleUtility::PaleGreenPalleteButton );
306
307 ui->CreateOpenObjectButton->setEnabled ( true );
308 ui->CreateOpenObjectButton->setPalette ( StyleUtility::PaleGreenPalleteButton );
309
310 this_status_bar->setPalette ( QApplication::palette ( this ) );
311 this_status_bar->showMessage ( GetMessage() );
312 }
313 else
314 {
315 ui->CreateObjectButton->setEnabled ( false );
316 ui->CreateOpenObjectButton->setEnabled ( false );
317 QPalette buttonAlert;
318 buttonAlert.setColor ( QPalette::Disabled, QPalette::Button, QColor ( "grey" ) );
319 ui->CreateObjectButton->setPalette ( buttonAlert );
320 ui->CreateOpenObjectButton->setPalette ( buttonAlert );
321 this_status_bar->setPalette ( StyleUtility::WarningStatusBarPalleteWindow );
322 this_status_bar->showMessage ( GetMessage() );
323 }
324}
325
327{
328 Q_UNUSED ( Dummy )
329 UpdateActions();
330}
331
333{
334 if ( this_state == editorOk )
335 return QString (
336 "Now you can create object '%1' of class '%2'." ).arg ( this_newuid ).arg (
337 QString ( ( this_object_class.p_name ).c_str() ) );
338 else if ( GetState ( classNotSelected | uidNotSet | fileNotSet ) )
339 return QString (
340 "Select class, set unique ID, select file where to create object and set obligatory attributes and relationships!" );
341 else if ( GetState ( classNotSelected ) )
342 {
343 return QString ( "First select class!" );
344 }
345 else if ( GetState ( fileNotSet ) )
346 return QString (
347 "Set active file where to create new object" );
348 else if ( GetState ( uidNotSet ) )
349 {
350 return QString ( "Set the object UID" );
351 }
352 else if ( GetState ( nullNotFilled ) )
353 return QString (
354 "All NOT NULL attributes or relationships must be set!" );
355
356 return QString ( "Problem to process Object Create operation!" );
357}
358
360{
361 return ( ( this_state & Flags ) == Flags );
362}
363
365{
366 ContextMenu = new QMenu ( this );
367 ContextMenu->addAction ( tr ( "&Edit Include Files" ), this, SLOT ( AddInclude() ),
368 QKeySequence ( tr ( "Ctrl+Z" ) ) );
369}
370
373
375{
376 ui->ComboBoxForbiddenUid->clear();
377
378 QStringList listOfUID;
379 QString nameOfClass = QString ( this_object_class.p_name.c_str() );
380
381 std::vector<tref> const & vec = dbe::config::api::info::onclass::objects (
382 nameOfClass.toStdString(),
383 false );
384
385 for ( size_t i = 0; i < vec.size(); ++i )
386 {
387 ui->ComboBoxForbiddenUid->addItem ( QString ( vec.at ( i ).UID().c_str() ) );
388 listOfUID << QString ( vec.at ( i ).UID().c_str() );
389 }
390
391 if ( !listOfUID.isEmpty() )
392 {
393 ui->ComboBoxForbiddenUid->setToolTip (
394 QString ( "Already used object unique ID of class %1 " ).arg ( nameOfClass ) );
395 ui->ComboBoxForbiddenUid->show();
396 }
397
398 QVariant varFromList ( listOfUID );
399 ValidatorAcceptNoMatch * myval = new ValidatorAcceptNoMatch ( varFromList, this );
400 ui->LineEditUID->SetValidator ( myval );
401}
402
404 CreateObject(true);
405}
406
408{
409 setCursor ( Qt::WaitCursor );
410
411 BOOST_SCOPE_EXIT(this_)
412 {
413 this_->unsetCursor();
414 }
415 BOOST_SCOPE_EXIT_END
416
417 if ( this_file_for_new_object.isEmpty() )
418 {
419 ERROR ( "Object creation not feasible", "Object file must be selected" )
420 }
421 else if ( this_newuid.isEmpty() )
422 {
423 ERROR ( "Object creation not feasible", "Object name (uid) cannot be an empty string" );
424 }
425 else if ( this_associated_editor )
426 {
427 this_associated_editor->SetUsedForCopy ( true );
428
429 bool done = this_associated_editor->ParseToCreate ( this_newuid.toStdString(),
430 this_file_for_new_object.toStdString() );
431 this_associated_editor->SetUsedForCopy ( false );
432 this_associated_editor->ResetObjectChanged();
433 this_object_changed = false;
434
435 if ( done )
436 {
437 close();
438
439 if ( openEditor == true ) {
440 ( new ObjectEditor ( dbe::inner::dbcontroller::get({this_newuid.toStdString(), this_object_class.p_name}) ) )->show();
441 }
442 }
443 }
444}
445
447{
448 this_object_changed = true;
449}
450
451void dbe::ObjectCreator::ActiveFileChanged ( const QString & fname )
452{
453 if ( fname.isEmpty() or not confaccessor::check_file_rw ( fname ) )
454 {
455 this_file_for_new_object = QString();
456 emit stateChanged();
457 return;
458 }
459
460 this_file_for_new_object = fname;
461 ui->FileView->setToolTip (
462 QString ( "Activate file which will store the new object: " ).append (
463 this_file_for_new_object ) );
464 QString setFilesWithPath = QString ( "Selected File: %1" ).arg ( this_file_for_new_object );
465 QString setOnlyFiles = QString ( "Selected File: %1" ).arg (
466 QFileInfo ( this_file_for_new_object ).fileName() );
467 ui->FileLabel->setText ( setOnlyFiles );
468 ui->FileLabel->setToolTip ( setFilesWithPath );
469 ui->FileView->setStyleSheet ( "selection-background-color : rgb(190,238,158)" );
470 emit stateChanged();
471}
472
474{
475 if ( ui->ComboBoxClass->IsValid() )
476 {
477
478 this_newuid = QString();
479 QString cname = QString::fromStdString ( this_object_class.p_name );
480
481 // Create the editor for the new object to be able to modify attributes before creation
482 this_associated_editor = new ObjectEditor ( cname.toStdString(), this );
483 this_associated_editor->setWindowModality ( Qt::NonModal );
484 this_associated_editor->HideDetailWidget ( true );
485 this_associated_editor->setDisabled ( true );
486 ui->EditorLayout->addWidget ( this_associated_editor );
487
488 {
489 // Set appropriately the stretch factor such that contents are resized
490 // favoring the object editor side
491 QSizePolicy pol = ui->splitter->widget ( 1 )->sizePolicy();
492 pol.setHorizontalStretch ( 2 );
493// pol.setHorizontalPolicy(QSizePolicy::Maximum);
494 ui->splitter->widget ( 1 )->setSizePolicy ( pol );
495 }
496
497 {
498 // Set horizontal size of the editor pane to the first widget size
499 this_associated_editor->resize ( this_associated_editor->sizeHint() );
500 QList<QTableWidget *> const & editor_widgets
501 {
502 this_associated_editor->findChildren<QTableWidget *>()
503 };
504
505 this_associated_editor->setMinimumWidth ( editor_widgets[0]->width() / 2 );
506
507 QRect this_widget_geometry = this->geometry();
508 this_widget_geometry.setWidth ( editor_widgets[0]->width() +
509 ui->ObjectCreatorWidget->width() );
510 this->setGeometry ( this_widget_geometry );
511
512 }
513
514 // Set the labels to the class name
515 ui->ClassLabel->setText ( QString ( "Selected Class: %1" ).arg ( cname ) );
516 setWindowTitle ( QString ( "Create New Object of class %1" ).arg ( cname ) );
517
518 connect ( this_associated_editor, SIGNAL ( WidgetUpdated() ), this,
519 SLOT ( UpdateActions() ),
520 Qt::UniqueConnection );
521
522 FillUidComboBox();
523 ui->ComboBoxForbiddenUid->hide();
524
525 QString const & linetext = ui->LineEditUID->text();
526
527 if ( !linetext.isEmpty() )
528 {
529 ui->LineEditUID->clear();
530 ui->LineEditUID->setText ( linetext );
531 SetUID();
532 }
533
534 emit stateChanged();
535 this_object_changed = false;
536 }
537}
538
540{
541 if ( ui->ComboBoxClass->IsValid() )
542 {
543 QString cname = QString::fromStdString ( this_object_class.p_name );
544
545 ui->ClassLabel->setText ( QString ( "Selected Class: %1" ).arg ( cname ) );
546 setWindowTitle ( QString ( "Create New Object of Class %1" ).arg ( cname ) );
547
548 this_associated_editor = new ObjectEditor ( *this_src_object, this, true );
549 this_associated_editor->setWindowModality ( Qt::NonModal );
550 this_associated_editor->SetUsedForCopy ( true );
551 this_associated_editor->HideDetailWidget ( true );
552 this_associated_editor->setDisabled ( true );
553
554 ui->EditorLayout->addWidget ( this_associated_editor );
555 connect ( this_associated_editor, SIGNAL ( WidgetUpdated() ), this,
556 SLOT ( UpdateActions() ),
557 Qt::UniqueConnection );
558
559 FillUidComboBox();
560 ui->ComboBoxForbiddenUid->hide();
561
562 QString tmpSt = ui->LineEditUID->text();
563
564 if ( !tmpSt.isEmpty() )
565 {
566 ui->LineEditUID->clear();
567 ui->LineEditUID->setText ( tmpSt );
568 SetUID();
569 }
570
571 emit stateChanged();
572 this_object_changed = false;
573 }
574}
575
577{
578 if ( this_file_for_new_object.isEmpty() )
579 {
580 ERROR ( "Object creation is not feasible", "Database file not selected" );
581 return;
582 }
583
584 if ( ui->LineEditUID->IsValid() )
585 {
586 this_newuid = ui->LineEditUID->text();
587
588 QString textForUser = QString ( "New Object ID: %1" ).arg ( this_newuid );
589 ui->UidLabel->setText ( textForUser );
590 }
591 else
592 {
593 WARN ( "Object creation is not feasible", "Invalid UID provided" );
594 return;
595 }
596
597 if ( !this_file_for_new_object.isEmpty() )
598 {
599 UidSet = true;
600 this_associated_editor->setDisabled ( false );
601 emit stateChanged();
602 }
603 else
604 {
605 TLOG_DEBUG(0) << "active file empty!! check!!" ;
606 }
607}
608
610{
611 QPalette yellow;
612 yellow.setColor ( QPalette::Active, QPalette::Window, QColor ( "yellow" ) );
613
614 this_status_bar->setPalette ( yellow );
615 this_status_bar->showMessage ( QString ( "Press ENTER to set new UID!" ) );
616}
617
619{
620
621 if ( ( this_object_changed and ui->CreateObjectButton->isEnabled() ) or
622 ( this_associated_editor and this_associated_editor->WasObjectChanged() ) )
623 {
624 int ret =
625 QMessageBox::question (
626 0,
627 tr ( "DBE - ObjectCreate" ),
628 QString ( "There are pending actions for this dialogue" ),
629 QMessageBox::Discard | QMessageBox::Cancel, QMessageBox::Cancel );
630
631 if ( ret == QMessageBox::Discard )
632 {
633 this_object_changed = false;
634 this_associated_editor->ResetObjectChanged();
635
636 try
637 {
639 inner::dbcontroller::get ( { this_newuid.toStdString(), this_object_class.p_name } ),
640 uuid );
641 }
642 catch ( daq::dbe::config_object_retrieval_result_is_null const & e )
643 {
644 // nothing specific handle needed in this , it is possible that there is no resul
645 }
646
647 return true;
648 }
649 else if ( ret == QMessageBox::Cancel )
650 {
651 return false;
652 }
653
654 return true;
655 }
656 else
657 {
658 return true;
659 }
660}
661
662void dbe::ObjectCreator::closeEvent ( QCloseEvent * event )
663{
664 if ( CanClose() )
665 {
666 if ( this_associated_editor )
667 {
668 this_associated_editor->close();
669 this_associated_editor = nullptr;
670 }
671
672 event->accept();
673 }
674 else
675 {
676 event->ignore();
677 }
678}
EditStates
@ editorOk
@ nullNotFilled
@ classNotSelected
@ uidNotSet
@ fileNotSet
void MustPressReturn(QString const &)
dunedaq::conffwk::relationship_t this_relation
void ActiveFileChanged(QString const &)
ObjectCreator(dunedaq::conffwk::class_t const &classinfo, QWidget *parent=0)
void closeEvent(QCloseEvent *event)
std::unique_ptr< dbe::Ui::ObjectCreator > ui
dunedaq::conffwk::class_t this_object_class
std::unique_ptr< tref > this_src_object
void CreateObject(bool openEditor=false)
std::unique_ptr< tref > this_target_object
QStatusBar * this_status_bar
bool GetState(int Flags)
static QPalette PaleGreenPalleteButton
static QPalette WarningStatusBarPalleteWindow
static bool check_file_rw(const QString &FileName)
static confaccessor & ref()
static QString db_implementation_name()
static std::vector< dbe::inner::configobject::tref > objects(std::string const &cname, bool const keep_inherited=true)
static configobject::tref get(dbe::cokey const &desc)
#define TLOG_DEBUG(lvl,...)
Definition Logging.hpp:112
#define ERROR(...)
Definition messenger.hpp:88
#define WARN(...)
Definition messenger.hpp:80
bool delobj(inner::configobject::tref obj, QUuid const &src)
Including Qt Headers.