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 // ui->FileView->HideReadOnlyFilesSlot(true);
247 ui->FileView->setColumnHidden(2, true);
248 ui->FileView->setColumnHidden(3, true);
249 ui->FileView->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
250 }
251}
252
254{
255 this_status_bar->setSizeGripEnabled ( false );
256 this_status_bar->setAutoFillBackground ( true );
257 this_status_bar->showMessage (
258 QString ( "Select class, set new object UID, select file where to create object!" ) );
259 ui->StatusFrame->setFrameStyle ( QFrame::NoFrame );
260 ui->StatusLayout->addWidget ( this_status_bar );
261}
262
264{
265 bool ReleaseEditor = true;
266 this_state = editorOk;
267
268 if ( !ui->LineEditUID->IsValid() || ui->LineEditUID->text().isEmpty() )
269 {
270 this_state |= uidNotSet;
271 ReleaseEditor = false;
272 this_newuid = QString();
273 }
274 else
275 {
276 this_state &= ~uidNotSet;
277 this_newuid = ui->LineEditUID->text();
278 }
279
280 if ( this_file_for_new_object.isEmpty() )
281 {
282 this_state |= fileNotSet;
283 ReleaseEditor = false;
284 }
285 else
286 {
287 this_state &= ~fileNotSet;
288 }
289
290 if ( ReleaseEditor )
291 {
292 this_associated_editor->setEnabled ( true );
293 }
294
295 if ( this_associated_editor != nullptr )
296 {
297 if ( !this_associated_editor->IsEditorValid() )
298 {
299 this_state |= nullNotFilled;
300 }
301 else
302 {
303 this_state &= ~nullNotFilled;
304 }
305 }
306
307 if ( this_state == editorOk )
308 {
309 ui->CreateObjectButton->setEnabled ( true );
310 ui->CreateObjectButton->setPalette ( StyleUtility::PaleGreenPalleteButton );
311
312 ui->CreateOpenObjectButton->setEnabled ( true );
313 ui->CreateOpenObjectButton->setPalette ( StyleUtility::PaleGreenPalleteButton );
314
315 this_status_bar->setPalette ( QApplication::palette ( this ) );
316 this_status_bar->showMessage ( GetMessage() );
317 }
318 else
319 {
320 ui->CreateObjectButton->setEnabled ( false );
321 ui->CreateOpenObjectButton->setEnabled ( false );
322 QPalette buttonAlert;
323 buttonAlert.setColor ( QPalette::Disabled, QPalette::Button, QColor ( "grey" ) );
324 ui->CreateObjectButton->setPalette ( buttonAlert );
325 ui->CreateOpenObjectButton->setPalette ( buttonAlert );
326 this_status_bar->setPalette ( StyleUtility::WarningStatusBarPalleteWindow );
327 this_status_bar->showMessage ( GetMessage() );
328 }
329}
330
332{
333 Q_UNUSED ( Dummy )
334 UpdateActions();
335}
336
338{
339 if ( this_state == editorOk )
340 return QString (
341 "Now you can create object '%1' of class '%2'." ).arg ( this_newuid ).arg (
342 QString ( ( this_object_class.p_name ).c_str() ) );
343 else if ( GetState ( classNotSelected | uidNotSet | fileNotSet ) )
344 return QString (
345 "Select class, set unique ID, select file where to create object and set obligatory attributes and relationships!" );
346 else if ( GetState ( classNotSelected ) )
347 {
348 return QString ( "First select class!" );
349 }
350 else if ( GetState ( fileNotSet ) )
351 return QString (
352 "Set active file where to create new object" );
353 else if ( GetState ( uidNotSet ) )
354 {
355 return QString ( "Set the object UID" );
356 }
357 else if ( GetState ( nullNotFilled ) )
358 return QString (
359 "All NOT NULL attributes or relationships must be set!" );
360
361 return QString ( "Problem to process Object Create operation!" );
362}
363
365{
366 return ( ( this_state & Flags ) == Flags );
367}
368
370{
371 ContextMenu = new QMenu ( this );
372 ContextMenu->addAction ( tr ( "&Edit Include Files" ), this, SLOT ( AddInclude() ),
373 QKeySequence ( tr ( "Ctrl+Z" ) ) );
374}
375
378
380{
381 ui->ComboBoxForbiddenUid->clear();
382
383 QStringList listOfUID;
384 QString nameOfClass = QString ( this_object_class.p_name.c_str() );
385
386 std::vector<tref> const & vec = dbe::config::api::info::onclass::objects (
387 nameOfClass.toStdString(),
388 false );
389
390 for ( size_t i = 0; i < vec.size(); ++i )
391 {
392 ui->ComboBoxForbiddenUid->addItem ( QString ( vec.at ( i ).UID().c_str() ) );
393 listOfUID << QString ( vec.at ( i ).UID().c_str() );
394 }
395
396 if ( !listOfUID.isEmpty() )
397 {
398 ui->ComboBoxForbiddenUid->setToolTip (
399 QString ( "Already used object unique ID of class %1 " ).arg ( nameOfClass ) );
400 ui->ComboBoxForbiddenUid->show();
401 }
402
403 QVariant varFromList ( listOfUID );
404 ValidatorAcceptNoMatch * myval = new ValidatorAcceptNoMatch ( varFromList, this );
405 ui->LineEditUID->SetValidator ( myval );
406}
407
409 CreateObject(true);
410}
411
413{
414 setCursor ( Qt::WaitCursor );
415
416 BOOST_SCOPE_EXIT(this_)
417 {
418 this_->unsetCursor();
419 }
420 BOOST_SCOPE_EXIT_END
421
422 if ( this_file_for_new_object.isEmpty() )
423 {
424 ERROR ( "Object creation not feasible", "Object file must be selected" )
425 }
426 else if ( this_newuid.isEmpty() )
427 {
428 ERROR ( "Object creation not feasible", "Object name (uid) cannot be an empty string" );
429 }
430 else if ( this_associated_editor )
431 {
432 this_associated_editor->SetUsedForCopy ( true );
433
434 bool done = this_associated_editor->ParseToCreate ( this_newuid.toStdString(),
435 this_file_for_new_object.toStdString() );
436 this_associated_editor->SetUsedForCopy ( false );
437 this_associated_editor->ResetObjectChanged();
438 this_object_changed = false;
439
440 if ( done )
441 {
442 close();
443
444 if ( openEditor == true ) {
445 ( new ObjectEditor ( dbe::inner::dbcontroller::get({this_newuid.toStdString(), this_object_class.p_name}) ) )->show();
446 }
447 }
448 }
449}
450
452{
453 this_object_changed = true;
454}
455
456void dbe::ObjectCreator::ActiveFileChanged ( const QString & fname )
457{
458 if ( fname.isEmpty() or not confaccessor::check_file_rw ( fname ) )
459 {
460 this_file_for_new_object = QString();
461 emit stateChanged();
462 return;
463 }
464
465 this_file_for_new_object = fname;
466 ui->FileView->setToolTip (
467 QString ( "Activate file which will store the new object: " ).append (
468 this_file_for_new_object ) );
469 QString setFilesWithPath = QString ( "Selected File: %1" ).arg ( this_file_for_new_object );
470 QString setOnlyFiles = QString ( "Selected File: %1" ).arg (
471 QFileInfo ( this_file_for_new_object ).fileName() );
472 ui->FileLabel->setText ( setOnlyFiles );
473 ui->FileLabel->setToolTip ( setFilesWithPath );
474 ui->FileView->setStyleSheet ( "selection-background-color : rgb(190,238,158)" );
475 emit stateChanged();
476}
477
479{
480 if ( ui->ComboBoxClass->IsValid() )
481 {
482
483 this_newuid = QString();
484 QString cname = QString::fromStdString ( this_object_class.p_name );
485
486 // Create the editor for the new object to be able to modify attributes before creation
487 this_associated_editor = new ObjectEditor ( cname.toStdString(), this );
488 this_associated_editor->setWindowModality ( Qt::NonModal );
489 this_associated_editor->HideDetailWidget ( true );
490 this_associated_editor->setDisabled ( true );
491 ui->EditorLayout->addWidget ( this_associated_editor );
492
493 {
494 // Set appropriately the stretch factor such that contents are resized
495 // favoring the object editor side
496 QSizePolicy pol = ui->splitter->widget ( 1 )->sizePolicy();
497 pol.setHorizontalStretch ( 2 );
498// pol.setHorizontalPolicy(QSizePolicy::Maximum);
499 ui->splitter->widget ( 1 )->setSizePolicy ( pol );
500 }
501
502 {
503 // Set horizontal size of the editor pane to the first widget size
504 this_associated_editor->resize ( this_associated_editor->sizeHint() );
505 QList<QTableWidget *> const & editor_widgets
506 {
507 this_associated_editor->findChildren<QTableWidget *>()
508 };
509
510 this_associated_editor->setMinimumWidth ( editor_widgets[0]->width() / 2 );
511
512 QRect this_widget_geometry = this->geometry();
513 this_widget_geometry.setWidth ( editor_widgets[0]->width() +
514 ui->ObjectCreatorWidget->width() );
515 this->setGeometry ( this_widget_geometry );
516
517 }
518
519 // Set the labels to the class name
520 ui->ClassLabel->setText ( QString ( "Selected Class: %1" ).arg ( cname ) );
521 setWindowTitle ( QString ( "Create New Object of class %1" ).arg ( cname ) );
522
523 connect ( this_associated_editor, SIGNAL ( WidgetUpdated() ), this,
524 SLOT ( UpdateActions() ),
525 Qt::UniqueConnection );
526
527 FillUidComboBox();
528 ui->ComboBoxForbiddenUid->hide();
529
530 QString const & linetext = ui->LineEditUID->text();
531
532 if ( !linetext.isEmpty() )
533 {
534 ui->LineEditUID->clear();
535 ui->LineEditUID->setText ( linetext );
536 SetUID();
537 }
538
539 emit stateChanged();
540 this_object_changed = false;
541 }
542}
543
545{
546 if ( ui->ComboBoxClass->IsValid() )
547 {
548 QString cname = QString::fromStdString ( this_object_class.p_name );
549
550 ui->ClassLabel->setText ( QString ( "Selected Class: %1" ).arg ( cname ) );
551 setWindowTitle ( QString ( "Create New Object of Class %1" ).arg ( cname ) );
552
553 this_associated_editor = new ObjectEditor ( *this_src_object, this, true );
554 this_associated_editor->setWindowModality ( Qt::NonModal );
555 this_associated_editor->SetUsedForCopy ( true );
556 this_associated_editor->HideDetailWidget ( true );
557 this_associated_editor->setDisabled ( true );
558
559 ui->EditorLayout->addWidget ( this_associated_editor );
560 connect ( this_associated_editor, SIGNAL ( WidgetUpdated() ), this,
561 SLOT ( UpdateActions() ),
562 Qt::UniqueConnection );
563
564 FillUidComboBox();
565 ui->ComboBoxForbiddenUid->hide();
566
567 QString tmpSt = ui->LineEditUID->text();
568
569 if ( !tmpSt.isEmpty() )
570 {
571 ui->LineEditUID->clear();
572 ui->LineEditUID->setText ( tmpSt );
573 SetUID();
574 }
575
576 emit stateChanged();
577 this_object_changed = false;
578 }
579}
580
582{
583 if ( this_file_for_new_object.isEmpty() )
584 {
585 ERROR ( "Object creation is not feasible", "Database file not selected" );
586 return;
587 }
588
589 if ( ui->LineEditUID->IsValid() )
590 {
591 this_newuid = ui->LineEditUID->text();
592
593 QString textForUser = QString ( "New Object ID: %1" ).arg ( this_newuid );
594 ui->UidLabel->setText ( textForUser );
595 }
596 else
597 {
598 WARN ( "Object creation is not feasible", "Invalid UID provided" );
599 return;
600 }
601
602 if ( !this_file_for_new_object.isEmpty() )
603 {
604 UidSet = true;
605 this_associated_editor->setDisabled ( false );
606 emit stateChanged();
607 }
608 else
609 {
610 TLOG_DEBUG(0) << "active file empty!! check!!" ;
611 }
612}
613
615{
616 QPalette yellow;
617 yellow.setColor ( QPalette::Active, QPalette::Window, QColor ( "yellow" ) );
618
619 this_status_bar->setPalette ( yellow );
620 this_status_bar->showMessage ( QString ( "Press ENTER to set new UID!" ) );
621}
622
624{
625
626 if ( ( this_object_changed and ui->CreateObjectButton->isEnabled() ) or
627 ( this_associated_editor and this_associated_editor->WasObjectChanged() ) )
628 {
629 int ret =
630 QMessageBox::question (
631 0,
632 tr ( "DBE - ObjectCreate" ),
633 QString ( "There are pending actions for this dialogue" ),
634 QMessageBox::Discard | QMessageBox::Cancel, QMessageBox::Cancel );
635
636 if ( ret == QMessageBox::Discard )
637 {
638 this_object_changed = false;
639 this_associated_editor->ResetObjectChanged();
640
641 try
642 {
644 inner::dbcontroller::get ( { this_newuid.toStdString(), this_object_class.p_name } ),
645 uuid );
646 }
647 catch ( daq::dbe::config_object_retrieval_result_is_null const & e )
648 {
649 // nothing specific handle needed in this , it is possible that there is no resul
650 }
651
652 return true;
653 }
654 else if ( ret == QMessageBox::Cancel )
655 {
656 return false;
657 }
658
659 return true;
660 }
661 else
662 {
663 return true;
664 }
665}
666
667void dbe::ObjectCreator::closeEvent ( QCloseEvent * event )
668{
669 if ( CanClose() )
670 {
671 if ( this_associated_editor )
672 {
673 this_associated_editor->close();
674 this_associated_editor = nullptr;
675 }
676
677 event->accept();
678 }
679 else
680 {
681 event->ignore();
682 }
683}
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)
The DUNE-DAQ namespace.