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