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: yes (from src/SchemaEditor/SchemaClassEditor.cpp to apps/SchemaEditor/SchemaClassEditor.cpp).
5 :
6 : /// Including Qt
7 : #include <QMessageBox>
8 : #include <QLineEdit>
9 : #include <QListWidget>
10 : #include <QListWidgetItem>
11 : #include <QInputDialog>
12 : #include <QString>
13 : #include <QStringList>
14 :
15 : /// Including Schema
16 : #include "dbe/SchemaClassEditor.hpp"
17 : #include "dbe/SchemaAttributeEditor.hpp"
18 : #include "dbe/SchemaRelationshipEditor.hpp"
19 : #include "dbe/SchemaMethodEditor.hpp"
20 : #include "dbe/SchemaKernelWrapper.hpp"
21 : /// Including Ui file
22 : #include "ui_SchemaClassEditor.h"
23 :
24 : #include "oks/file.hpp"
25 :
26 : #include <map>
27 :
28 : using namespace dunedaq::oks;
29 :
30 0 : dbse::SchemaClassEditor::SchemaClassEditor ( OksClass * ClassInfo, QWidget * parent )
31 : : QWidget ( parent ),
32 0 : ui ( new Ui::SchemaClassEditor ),
33 0 : SchemaClass ( ClassInfo ),
34 0 : MethodModel ( nullptr ),
35 0 : AttributeModel ( nullptr ),
36 0 : RelationshipModel ( nullptr ),
37 0 : SuperClassModel ( nullptr ),
38 0 : SubClassModel ( nullptr ),
39 0 : ContextMenuAttribute ( nullptr ),
40 0 : ContextMenuRelationship ( nullptr ),
41 0 : ContextMenuMethod ( nullptr ),
42 0 : ContextMenuClass ( nullptr )
43 : {
44 0 : QWidget::setAttribute(Qt::WA_DeleteOnClose);
45 :
46 : /// Settings
47 0 : ui->setupUi ( this );
48 0 : setWindowTitle (
49 0 : QString ( "Class Editor : %1" ).arg ( QString::fromStdString (
50 0 : SchemaClass->get_name() ) ) );
51 0 : setObjectName ( QString::fromStdString ( SchemaClass->get_name() ) );
52 : /// Settings
53 0 : BuildModels();
54 0 : SetController();
55 : /// Editing Settings
56 0 : InitialSettings();
57 0 : }
58 :
59 0 : dbse::SchemaClassEditor::~SchemaClassEditor() = default;
60 :
61 0 : void dbse::SchemaClassEditor::keyPressEvent(QKeyEvent* event) {
62 0 : if (event->key() == Qt::Key_Escape) {
63 0 : close();
64 : }
65 0 : QWidget::keyPressEvent(event);
66 0 : }
67 0 : void dbse::SchemaClassEditor::SetController()
68 : {
69 0 : connect ( ui->buttonBox, SIGNAL ( accepted() ), this, SLOT ( ParseToSave() ) );
70 0 : connect ( ui->buttonBox, SIGNAL ( rejected() ), this, SLOT ( close_slot() ) );
71 0 : connect ( ui->moveButton, SIGNAL(clicked()), this, SLOT (move_class()));
72 0 : connect ( ui->AddButtonAttribute, SIGNAL ( clicked() ), this, SLOT ( AddNewAttribute() ) );
73 0 : connect ( ui->AddButtonSuperClass, SIGNAL ( clicked() ), this, SLOT ( AddNewSuperClass() ) );
74 0 : connect ( ui->AddButtonRelationship, SIGNAL ( clicked() ), this,
75 : SLOT ( AddNewRelationship() ) );
76 0 : connect ( ui->AddButtonMethod, SIGNAL ( clicked() ), this, SLOT ( AddNewMethod() ) );
77 0 : connect ( ui->RelationshipView, SIGNAL ( activated ( QModelIndex ) ), this,
78 : SLOT ( OpenRelationshipEditor ( QModelIndex ) ) );
79 0 : connect ( ui->MethodsView, SIGNAL ( activated ( QModelIndex ) ), this,
80 : SLOT ( OpenMethodEditor ( QModelIndex ) ) );
81 0 : connect ( ui->AttributeView, SIGNAL ( activated ( QModelIndex ) ), this,
82 : SLOT ( OpenAttributeEditor ( QModelIndex ) ) );
83 0 : connect ( ui->SuperClassView, SIGNAL ( activated ( QModelIndex ) ), this,
84 : SLOT ( OpenSuperClass ( QModelIndex ) ) );
85 0 : connect ( ui->SubClassView, SIGNAL ( activated ( QModelIndex ) ), this,
86 : SLOT ( OpenSubClass ( QModelIndex ) ) );
87 0 : connect ( ui->AttributeView, SIGNAL ( customContextMenuRequested ( QPoint ) ), this,
88 : SLOT ( CustomMenuAttributeView ( QPoint ) ) );
89 0 : connect ( ui->RelationshipView, SIGNAL ( customContextMenuRequested ( QPoint ) ), this,
90 : SLOT ( CustomMenuRelationshipView ( QPoint ) ) );
91 0 : connect ( ui->MethodsView, SIGNAL ( customContextMenuRequested ( QPoint ) ), this,
92 : SLOT ( CustomMenuMethodView ( QPoint ) ) );
93 0 : connect ( ui->SuperClassView, SIGNAL ( customContextMenuRequested ( QPoint ) ), this,
94 : SLOT ( CustomMenuClassView ( QPoint ) ) );
95 0 : connect ( ui->ShowDerivedAttributes, &QCheckBox::toggled, this, &SchemaClassEditor::BuildAttributeModelSlot );
96 0 : connect ( ui->ShowDerivedRelationships, &QCheckBox::toggled, this, &SchemaClassEditor::BuildRelationshipModelSlot );
97 0 : connect ( ui->ShowDerivedMethods, &QCheckBox::toggled, this, &SchemaClassEditor::BuildMethodModelSlot );
98 0 : connect ( ui->ShowAllSuperClasses, &QCheckBox::toggled, this, &SchemaClassEditor::BuildSuperClassModelSlot );
99 0 : connect ( &KernelWrapper::GetInstance(), SIGNAL ( ClassRemoved ( QString ) ), this,
100 : SLOT ( ClassRemoved ( QString ) ) );
101 0 : connect ( &KernelWrapper::GetInstance(), SIGNAL ( ClassUpdated ( QString ) ), this,
102 : SLOT ( ClassUpdated ( QString ) ) );
103 0 : }
104 :
105 0 : void dbse::SchemaClassEditor::ClassRemoved( QString className )
106 : {
107 0 : if(className == objectName()) {
108 0 : QWidget::close();
109 : }
110 0 : }
111 :
112 0 : void dbse::SchemaClassEditor::ClassUpdated( QString className )
113 : {
114 0 : if(className == objectName()) {
115 0 : BuildModels();
116 0 : InitialSettings();
117 : }
118 0 : }
119 :
120 0 : void dbse::SchemaClassEditor::BuildModels()
121 : {
122 0 : BuildSubClassModelSlot();
123 0 : BuildSuperClassModelSlot();
124 0 : BuildMethodModelSlot();
125 0 : BuildAttributeModelSlot();
126 0 : BuildRelationshipModelSlot();
127 0 : }
128 :
129 0 : void dbse::SchemaClassEditor::InitialSettings()
130 : {
131 0 : ui->AttributeView->setContextMenuPolicy ( Qt::ContextMenuPolicy::CustomContextMenu );
132 0 : ui->RelationshipView->setContextMenuPolicy ( Qt::ContextMenuPolicy::CustomContextMenu );
133 0 : ui->MethodsView->setContextMenuPolicy ( Qt::ContextMenuPolicy::CustomContextMenu );
134 0 : ui->SuperClassView->setContextMenuPolicy ( Qt::ContextMenuPolicy::CustomContextMenu );
135 0 : ui->SubClassView->setContextMenuPolicy ( Qt::ContextMenuPolicy::CustomContextMenu );
136 :
137 : /// Class Name
138 0 : ui->ClassNameLineEdit->setText ( QString::fromStdString ( SchemaClass->get_name() ) );
139 : //ui->ClassNameLineEdit->setEnabled ( false );
140 : /// Schema File
141 0 : ui->SchemaFileLineEdit->setText ( QString::fromStdString ( SchemaClass->get_file()->get_short_file_name() ) );
142 : //ui->SchemaFileLineEdit->setEnabled ( false );
143 :
144 0 : if (KernelWrapper::GetInstance().IsFileWritable(
145 0 : SchemaClass->get_file()->get_full_file_name())) {
146 0 : ui->AddButtonAttribute->setEnabled (true);
147 0 : ui->AddButtonSuperClass->setEnabled (true);
148 0 : ui->AddButtonRelationship->setEnabled (true);
149 0 : ui->AddButtonMethod->setEnabled (true);
150 0 : ui->moveButton->setEnabled (true);
151 : }
152 : else {
153 0 : ui->SchemaFileLineEdit->setStyleSheet("color:rgb(128,0,0);");
154 : // ui->DescriptionTextEdit->setEnabled(false);
155 0 : ui->DescriptionTextEdit->setReadOnly(true);
156 :
157 0 : ui->AddButtonAttribute->setEnabled (false);
158 0 : ui->AddButtonSuperClass->setEnabled (false);
159 0 : ui->AddButtonRelationship->setEnabled (false);
160 0 : ui->AddButtonMethod->setEnabled (false);
161 0 : ui->moveButton->setEnabled (false);
162 0 : ui->AbstractComboBox->setEnabled(false);
163 : }
164 :
165 : /// Description
166 0 : ui->DescriptionTextEdit->setPlainText ( QString::fromStdString ( SchemaClass->get_description() ) );
167 0 : ui->DescriptionTextEdit->setTabChangesFocus (true);
168 : /// Abstract
169 0 : if ( SchemaClass->get_is_abstract() )
170 : {
171 0 : ui->AbstractComboBox->setCurrentIndex ( 0 );
172 : }
173 : else
174 : {
175 0 : ui->AbstractComboBox->setCurrentIndex ( 1 );
176 : }
177 0 : }
178 :
179 0 : void dbse::SchemaClassEditor::move_class() {
180 0 : move_class(SchemaClass, this);
181 0 : }
182 :
183 :
184 0 : void dbse::SchemaClassEditor::move_class(dunedaq::oks::OksClass* schema_class,
185 : QWidget* pwidget) {
186 0 : std::string current_file = schema_class->get_file()->get_full_file_name();
187 0 : std::vector<OksFile*> files;
188 0 : KernelWrapper::GetInstance().GetSchemaFiles(files);
189 0 : QStringList writable_files;
190 0 : std::map<QString, OksFile*> file_map;
191 0 : int current_row = -1;
192 0 : for (auto file: files) {
193 0 : auto fn = file->get_full_file_name();
194 0 : if (KernelWrapper::GetInstance().IsFileWritable(fn)) {
195 0 : if (fn == current_file) {
196 0 : current_row = writable_files.size();
197 : }
198 0 : writable_files.append(QString::fromStdString(fn));
199 0 : file_map[QString::fromStdString(fn)] = file;
200 : }
201 0 : }
202 0 : if (writable_files.empty()) {
203 0 : QMessageBox::warning ( 0, "Schema editor",
204 0 : QString ( "No writable schema files to move class to." ) );
205 0 : return;
206 : }
207 :
208 0 : QWidget* widget = new QWidget();
209 0 : QString text = "Select file to hold class " +
210 0 : QString::fromStdString(schema_class->get_name());
211 0 : QLabel* label = new QLabel(text);
212 0 : QListWidget* qlw = new QListWidget();
213 0 : qlw->addItems(writable_files);
214 0 : if (current_row != -1) {
215 0 : qlw->setCurrentRow(current_row);
216 : }
217 :
218 0 : connect (qlw, &QListWidget::itemActivated,
219 0 : pwidget, [=] () {
220 0 : QListWidgetItem* it = qlw->currentItem();
221 0 : auto fn = it->text();
222 0 : if (fn.toStdString() != current_file) {
223 0 : schema_class->set_file(file_map.at(fn));
224 0 : emit KernelWrapper::GetInstance().ClassUpdated ( QString::fromStdString(schema_class->get_name()) );
225 : }
226 0 : delete widget;
227 0 : }
228 : );
229 :
230 0 : auto bb = new QDialogButtonBox(QDialogButtonBox::Ok|QDialogButtonBox::Cancel);
231 0 : connect (bb, &QDialogButtonBox::accepted, pwidget, [=] () {
232 0 : auto it = qlw->currentItem();
233 0 : if (it != nullptr) {
234 0 : auto fn = it->text();
235 0 : if (fn.toStdString() != current_file) {
236 0 : schema_class->set_file(file_map.at(fn));
237 0 : emit KernelWrapper::GetInstance().ClassUpdated ( QString::fromStdString(schema_class->get_name()) );
238 : }
239 0 : delete widget;
240 0 : }
241 : else {
242 0 : QMessageBox::warning ( 0, "Schema editor",
243 0 : QString ( "No schema file selected to move class to." ) );
244 0 : return;
245 : }
246 : });
247 0 : connect ( bb, &QDialogButtonBox::rejected, pwidget, [=] () {delete widget;} );
248 :
249 0 : QVBoxLayout* layout = new QVBoxLayout();
250 0 : layout->addWidget(label);
251 0 : layout->addWidget(qlw);
252 0 : layout->addWidget(bb);
253 0 : widget->setWindowTitle("Select new file for class");
254 0 : widget->setLayout(layout);
255 0 : widget->setParent(pwidget, Qt::Dialog);
256 0 : widget->show();
257 0 : }
258 :
259 0 : bool dbse::SchemaClassEditor::ShouldOpenAttributeEditor ( QString attrName )
260 : {
261 0 : QString name = QString::fromStdString(SchemaClass->get_name() + "::") +
262 0 : attrName;
263 0 : for (QWidget* widget : QApplication::allWidgets())
264 : {
265 0 : if (dynamic_cast<SchemaAttributeEditor *> (widget) != nullptr)
266 : {
267 0 : if ( (widget->objectName()).compare (name) == 0 )
268 : {
269 0 : widget->raise();
270 0 : widget->setVisible ( true );
271 0 : widget->activateWindow();
272 0 : return false;
273 : }
274 : }
275 0 : }
276 :
277 0 : return true;
278 0 : }
279 :
280 0 : bool dbse::SchemaClassEditor::ShouldOpenRelationshipEditor ( QString relName )
281 : {
282 0 : QString name = QString::fromStdString(SchemaClass->get_name() + "::") +
283 0 : relName;
284 :
285 0 : for ( QWidget* widget : QApplication::allWidgets() )
286 : {
287 0 : if (dynamic_cast<SchemaRelationshipEditor *> (widget) != nullptr)
288 : {
289 0 : if ( ( widget->objectName() ).compare ( name ) == 0 )
290 : {
291 0 : widget->raise();
292 0 : widget->setVisible ( true );
293 0 : widget->activateWindow();
294 0 : return false;
295 : }
296 : }
297 0 : }
298 :
299 0 : return true;
300 0 : }
301 :
302 0 : bool dbse::SchemaClassEditor::ShouldOpenMethodEditor ( QString Name )
303 : {
304 0 : bool WidgetFound = false;
305 :
306 0 : for ( QWidget * Editor : QApplication::allWidgets() )
307 : {
308 0 : SchemaMethodEditor * Widget = dynamic_cast<SchemaMethodEditor *> ( Editor );
309 :
310 0 : if ( Widget != nullptr )
311 : {
312 0 : if ( ( Widget->objectName() ).compare ( Name ) == 0 )
313 : {
314 0 : Widget->raise();
315 0 : Widget->setVisible ( true );
316 0 : Widget->activateWindow();
317 : WidgetFound = true;
318 : }
319 : }
320 0 : }
321 :
322 0 : return !WidgetFound;
323 : }
324 :
325 0 : void dbse::SchemaClassEditor::RemoveAttribute()
326 : {
327 0 : const std::string& attributeName = CurrentRow.at ( 0 ).toStdString();
328 0 : OksAttribute * SchemaAttribute = SchemaClass->find_direct_attribute( attributeName );
329 0 : if(SchemaAttribute != nullptr) {
330 0 : KernelWrapper::GetInstance().PushRemoveAttributeCommand (
331 0 : SchemaClass, SchemaAttribute, SchemaAttribute->get_name(), SchemaAttribute->get_type(),
332 0 : SchemaAttribute->get_is_multi_values(), SchemaAttribute->get_range(),
333 0 : SchemaAttribute->get_init_value(), SchemaAttribute->get_description(),
334 0 : SchemaAttribute->get_is_no_null(), SchemaAttribute->get_format() );
335 : } else {
336 0 : QMessageBox::warning ( 0, "Schema editor",
337 0 : QString::fromStdString( "Cannot remove attribute \"" + attributeName +
338 0 : "\" because it is not a direct attribute of \"" + SchemaClass->get_name() + "\"") );
339 : }
340 0 : }
341 :
342 0 : void dbse::SchemaClassEditor::RemoveRelationship()
343 : {
344 0 : const std::string& relationshipName = CurrentRow.at ( 0 ).toStdString();
345 0 : OksRelationship * SchemaRelationship = SchemaClass->find_direct_relationship( relationshipName );
346 0 : if(SchemaRelationship != nullptr) {
347 0 : KernelWrapper::GetInstance().PushRemoveRelationship (
348 0 : SchemaClass, SchemaRelationship, SchemaRelationship->get_name(),
349 0 : SchemaRelationship->get_description(), SchemaRelationship->get_type(),
350 0 : SchemaRelationship->get_is_composite(), SchemaRelationship->get_is_exclusive(),
351 0 : SchemaRelationship->get_is_dependent(),
352 : SchemaRelationship->get_low_cardinality_constraint(),
353 : SchemaRelationship->get_high_cardinality_constraint() );
354 : } else {
355 0 : QMessageBox::warning ( 0, "Schema editor",
356 0 : QString::fromStdString( "Cannot remove relationship \"" + relationshipName +
357 0 : "\" because it is not a direct relationship of \"" + SchemaClass->get_name() + "\"") );
358 : }
359 0 : }
360 :
361 0 : void dbse::SchemaClassEditor::RemoveMethod()
362 : {
363 0 : const std::string& methodName = CurrentRow.at ( 0 ).toStdString();
364 0 : OksMethod * SchemaMethod = SchemaClass->find_direct_method ( methodName );
365 0 : if(SchemaMethod != nullptr) {
366 0 : KernelWrapper::GetInstance().PushRemoveMethodCommand ( SchemaClass, SchemaMethod,
367 0 : SchemaMethod->get_name(),
368 0 : SchemaMethod->get_description() );
369 : } else {
370 0 : QMessageBox::warning ( 0, "Schema editor",
371 0 : QString::fromStdString( "Cannot remove method \"" + methodName +
372 0 : "\" because it is not a direct method of \"" + SchemaClass->get_name() + "\"") );
373 : }
374 0 : }
375 :
376 0 : void dbse::SchemaClassEditor::RemoveSuperClass()
377 : {
378 0 : const std::string& superClassName = CurrentRow.at ( 0 ).toStdString();
379 0 : if(SchemaClass->has_direct_super_class(superClassName))
380 : {
381 0 : KernelWrapper::GetInstance().PushRemoveSuperClassCommand(SchemaClass, superClassName);
382 : }
383 : else
384 : {
385 0 : QMessageBox::warning ( 0, "Schema editor",
386 0 : QString::fromStdString( "Cannot remove super-class \"" + superClassName +
387 0 : "\" because it is not a direct super-class of \"" + SchemaClass->get_name() + "\"") );
388 : }
389 0 : }
390 :
391 :
392 0 : void dbse::SchemaClassEditor::ParseToSave()
393 : {
394 0 : std::string NewDescription = ui->DescriptionTextEdit->toPlainText().toStdString();
395 :
396 0 : bool Abstract = false;
397 :
398 0 : if ( ui->AbstractComboBox->currentIndex() == 0 )
399 : {
400 : Abstract = true;
401 : }
402 : else
403 : {
404 0 : Abstract = false;
405 : }
406 :
407 0 : if ( Abstract != SchemaClass->get_is_abstract() )
408 : {
409 0 : KernelWrapper::GetInstance().PushSetAbstractClassCommand ( SchemaClass, Abstract );
410 : }
411 :
412 0 : if ( NewDescription != SchemaClass->get_description() )
413 : {
414 0 : KernelWrapper::GetInstance().PushSetDescriptionClassCommand ( SchemaClass, NewDescription );
415 : }
416 :
417 0 : close();
418 0 : }
419 :
420 0 : void dbse::SchemaClassEditor::AddNewSuperClass()
421 : {
422 0 : bool widgetFound=false;
423 0 : QString class_name = QString::fromStdString(SchemaClass->get_name());
424 :
425 0 : QString wname = "add_sc_"+class_name;
426 0 : for ( QWidget* widget : QApplication::allWidgets() ) {
427 0 : if (widget->objectName().compare(wname) == 0 ) {
428 0 : widget->raise();
429 0 : widget->setVisible ( true );
430 0 : widget->activateWindow();
431 : widgetFound = true;
432 : break;
433 : }
434 0 : }
435 :
436 0 : if (!widgetFound) {
437 0 : QWidget* widget = new QWidget();
438 0 : widget->setObjectName(wname);
439 0 : QLabel* label = new QLabel("Select a super-class to add to " + class_name);
440 0 : label->setWordWrap(true);
441 :
442 : // QLabel* status = new QLabel();
443 : // status->setStyleSheet("QLabel { color : green; }");
444 :
445 0 : auto search = new QLineEdit(widget);
446 0 : search->setPlaceholderText("Search classes");
447 0 : search->setClearButtonEnabled(true);
448 :
449 0 : QListWidget* qlwidget = new QListWidget();
450 0 : QStringList allClasses;
451 0 : KernelWrapper::GetInstance().GetClassListString(allClasses);
452 0 : qlwidget->addItems(allClasses);
453 :
454 0 : connect ( qlwidget, &QListWidget::itemActivated,
455 0 : this, [=] () {
456 0 : const std::string& className = qlwidget->currentItem()->text().toStdString();
457 0 : KernelWrapper::GetInstance().PushAddSuperClassCommand(SchemaClass, className);
458 0 : BuildModels();
459 : // status->setText(QString::fromStdString(className + " added as a super-class of " + SchemaClass->get_name()));
460 0 : });
461 :
462 0 : connect (search, &QLineEdit::textChanged, this, [search, qlwidget] () {
463 0 : auto items = qlwidget->findItems(search->text(), Qt::MatchRegularExpression);
464 0 : if (!items.empty()) {
465 0 : qlwidget->setCurrentItem(items[0]);
466 : }
467 0 : });
468 :
469 0 : auto close_button = new QPushButton("Close");
470 0 : connect (close_button, SIGNAL (clicked()), widget, SLOT(close()));
471 0 : QVBoxLayout* lout = new QVBoxLayout();
472 0 : lout->addWidget(label);
473 0 : lout->addWidget(qlwidget);
474 0 : lout->addWidget(search);
475 0 : lout->addWidget(close_button);
476 : // l->addWidget(status);
477 :
478 0 : widget->setWindowTitle("Select super-class(es) for " + class_name);
479 0 : widget->setLayout(lout);
480 0 : widget->setParent(this, Qt::Dialog);
481 0 : widget->show();
482 0 : }
483 0 : }
484 :
485 0 : void dbse::SchemaClassEditor::AddNewAttribute()
486 : {
487 0 : if (ShouldOpenAttributeEditor("")) {
488 0 : SchemaAttributeEditor * Editor = new SchemaAttributeEditor ( SchemaClass );
489 0 : connect ( Editor, SIGNAL ( RebuildModel() ), this, SLOT ( BuildAttributeModelSlot() ) );
490 0 : Editor->show();
491 : }
492 0 : }
493 :
494 0 : void dbse::SchemaClassEditor::AddNewRelationship()
495 : {
496 0 : if (ShouldOpenRelationshipEditor("")) {
497 0 : SchemaRelationshipEditor * Editor = new SchemaRelationshipEditor ( SchemaClass );
498 0 : connect ( Editor, SIGNAL ( RebuildModel() ), this, SLOT ( BuildRelationshipModelSlot() ) );
499 0 : Editor->show();
500 : }
501 0 : }
502 :
503 0 : void dbse::SchemaClassEditor::AddNewMethod()
504 : {
505 0 : SchemaMethodEditor * Editor = new SchemaMethodEditor ( SchemaClass );
506 0 : connect ( Editor, SIGNAL ( RebuildModel() ), this, SLOT ( BuildMethodModelSlot() ) );
507 0 : Editor->show();
508 0 : }
509 :
510 0 : void dbse::SchemaClassEditor::OpenAttributeEditor ( QModelIndex Index )
511 : {
512 0 : QStringList Row = AttributeModel->getRowFromIndex ( Index );
513 0 : bool ShouldOpen = ShouldOpenAttributeEditor ( Row.at ( 0 ) );
514 :
515 0 : if ( !Row.isEmpty() && ShouldOpen )
516 : {
517 0 : SchemaAttributeEditor * Editor = new SchemaAttributeEditor (
518 0 : SchemaClass, SchemaClass->find_attribute ( Row.at ( 0 ).toStdString() ) );
519 0 : connect ( Editor, SIGNAL ( RebuildModel() ), this, SLOT ( BuildAttributeModelSlot() ) );
520 0 : Editor->show();
521 : }
522 0 : }
523 :
524 0 : void dbse::SchemaClassEditor::OpenRelationshipEditor ( QModelIndex Index )
525 : {
526 0 : QStringList Row = RelationshipModel->getRowFromIndex ( Index );
527 0 : bool ShouldOpen = ShouldOpenRelationshipEditor ( Row.at ( 0 ) );
528 :
529 0 : if ( !Row.isEmpty() && ShouldOpen )
530 : {
531 0 : SchemaRelationshipEditor * Editor = new SchemaRelationshipEditor (
532 0 : SchemaClass, SchemaClass->find_relationship ( Row.at ( 0 ).toStdString() ) );
533 0 : connect ( Editor, SIGNAL ( RebuildModel() ), this, SLOT ( BuildRelationshipModelSlot() ) );
534 0 : Editor->show();
535 : }
536 0 : }
537 :
538 0 : void dbse::SchemaClassEditor::OpenMethodEditor ( QModelIndex Index )
539 : {
540 0 : QStringList Row = MethodModel->getRowFromIndex ( Index );
541 0 : auto method = Row.at(0).toStdString();
542 0 : if (SchemaClass->find_direct_method(method) == nullptr) {
543 0 : std::string message{"Method '" + method + "' is not a direct method of " +
544 0 : SchemaClass->get_name() +
545 0 : ". Please open the method editor from the base class or add a local implementation to overrride it."};
546 0 : QMessageBox::warning (0,
547 : "Schema editor",
548 0 : QString::fromStdString(message));
549 0 : return;
550 0 : }
551 :
552 0 : bool ShouldOpen = ShouldOpenMethodEditor ( Row.at ( 0 ) );
553 :
554 0 : if ( !Row.isEmpty() && ShouldOpen )
555 : {
556 0 : SchemaMethodEditor * Editor = new SchemaMethodEditor (
557 0 : SchemaClass, SchemaClass->find_method ( method ) );
558 0 : connect ( Editor, SIGNAL ( RebuildModel() ), this, SLOT ( BuildMethodModelSlot() ) );
559 0 : Editor->show();
560 : }
561 0 : }
562 :
563 0 : void dbse::SchemaClassEditor::OpenSuperClass ( QModelIndex Index )
564 : {
565 0 : QStringList Row = SuperClassModel->getRowFromIndex ( Index );
566 :
567 0 : if ( !Row.isEmpty() )
568 : {
569 0 : QString ClassName = Row.at ( 0 );
570 0 : OpenNewClassEditor(ClassName);
571 0 : }
572 0 : }
573 :
574 0 : void dbse::SchemaClassEditor::OpenSubClass ( QModelIndex Index )
575 : {
576 0 : QStringList Row = SubClassModel->getRowFromIndex ( Index );
577 :
578 0 : if ( !Row.isEmpty() )
579 : {
580 0 : QString ClassName = Row.at ( 0 );
581 0 : OpenNewClassEditor(ClassName);
582 0 : }
583 0 : }
584 :
585 0 : void dbse::SchemaClassEditor::OpenNewClassEditor ( const QString& ClassName )
586 : {
587 0 : bool WidgetFound = false;
588 0 : OksClass * ClassInfo = KernelWrapper::GetInstance().FindClass ( ClassName.toStdString() );
589 :
590 0 : for ( QWidget * Editor : QApplication::allWidgets() )
591 : {
592 0 : SchemaClassEditor * Widget = dynamic_cast<SchemaClassEditor *> ( Editor );
593 :
594 0 : if ( Widget != nullptr )
595 : {
596 0 : if ( ( Widget->objectName() ).compare ( ClassName ) == 0 )
597 : {
598 0 : Widget->raise();
599 0 : Widget->setVisible ( true );
600 0 : Widget->activateWindow();
601 : WidgetFound = true;
602 : }
603 : }
604 0 : }
605 :
606 0 : if ( !WidgetFound )
607 : {
608 0 : SchemaClassEditor * Editor = new SchemaClassEditor ( ClassInfo );
609 0 : Editor->show();
610 : }
611 0 : }
612 :
613 0 : void dbse::SchemaClassEditor::BuildAttributeModelSlot()
614 : {
615 0 : QStringList AttributeHeaders
616 0 : { "Name", "Type" };
617 :
618 0 : if ( AttributeModel != nullptr ) {
619 0 : delete AttributeModel;
620 : }
621 0 : AttributeModel = new CustomAttributeModel (
622 : SchemaClass,
623 : AttributeHeaders,
624 0 : ui->ShowDerivedAttributes->isChecked());
625 :
626 0 : ui->AttributeView->setModel ( AttributeModel );
627 0 : ui->AttributeView->horizontalHeader()->setSectionResizeMode ( QHeaderView::Stretch );
628 0 : ui->AttributeView->setSelectionBehavior ( QAbstractItemView::SelectRows );
629 0 : }
630 :
631 0 : void dbse::SchemaClassEditor::BuildRelationshipModelSlot()
632 : {
633 0 : QStringList RelationshipHeaders
634 0 : { "Name", "Type", "Low cc", "High cc" };
635 :
636 0 : if ( RelationshipModel == nullptr ) RelationshipModel = new CustomRelationshipModel (
637 0 : SchemaClass, RelationshipHeaders, ui->ShowDerivedRelationships->isChecked() );
638 : else
639 : {
640 0 : delete RelationshipModel;
641 0 : RelationshipModel = new CustomRelationshipModel ( SchemaClass, RelationshipHeaders, ui->ShowDerivedRelationships->isChecked() );
642 : }
643 :
644 0 : ui->RelationshipView->setModel ( RelationshipModel );
645 0 : ui->RelationshipView->horizontalHeader()->setSectionResizeMode ( QHeaderView::Stretch );
646 0 : ui->RelationshipView->setSelectionBehavior ( QAbstractItemView::SelectRows );
647 0 : }
648 :
649 0 : void dbse::SchemaClassEditor::BuildMethodModelSlot()
650 : {
651 0 : QStringList MethodHeaders
652 0 : { "Method Name" };
653 :
654 0 : if ( MethodModel == nullptr )
655 : {
656 0 : MethodModel = new CustomMethodModel ( SchemaClass, MethodHeaders, ui->ShowDerivedMethods->isChecked() );
657 : }
658 : else
659 : {
660 0 : delete MethodModel;
661 0 : MethodModel = new CustomMethodModel ( SchemaClass, MethodHeaders, ui->ShowDerivedMethods->isChecked() );
662 : }
663 :
664 0 : ui->MethodsView->setModel ( MethodModel );
665 0 : ui->MethodsView->horizontalHeader()->setSectionResizeMode ( QHeaderView::Stretch );
666 0 : ui->MethodsView->setSelectionBehavior ( QAbstractItemView::SelectRows );
667 0 : }
668 :
669 0 : void dbse::SchemaClassEditor::BuildSuperClassModelSlot()
670 : {
671 0 : QStringList SuperClassHeaders
672 0 : { "Class Name" };
673 :
674 0 : if ( SuperClassModel != nullptr )
675 : {
676 0 : delete SuperClassModel;
677 : }
678 0 : SuperClassModel = new CustomSuperClassModel ( SchemaClass, SuperClassHeaders, ui->ShowAllSuperClasses->isChecked() );
679 :
680 0 : ui->SuperClassView->setModel ( SuperClassModel );
681 0 : ui->SuperClassView->horizontalHeader()->setSectionResizeMode ( QHeaderView::Stretch );
682 0 : ui->SuperClassView->setSelectionBehavior ( QAbstractItemView::SelectRows );
683 0 : }
684 :
685 0 : void dbse::SchemaClassEditor::BuildSubClassModelSlot()
686 : {
687 0 : QStringList SubClassHeaders
688 0 : { "Class Name" };
689 :
690 0 : if ( SubClassModel != nullptr )
691 : {
692 0 : delete SubClassModel;
693 : }
694 0 : SubClassModel = new CustomSubClassModel ( SchemaClass, SubClassHeaders );
695 :
696 0 : ui->SubClassView->setModel ( SubClassModel );
697 0 : ui->SubClassView->horizontalHeader()->setSectionResizeMode ( QHeaderView::Stretch );
698 0 : ui->SubClassView->setSelectionBehavior ( QAbstractItemView::SelectRows );
699 0 : }
700 :
701 0 : void dbse::SchemaClassEditor::CustomMenuClassView ( QPoint pos )
702 : {
703 0 : if ( ContextMenuClass == nullptr )
704 : {
705 0 : ContextMenuClass = new QMenu ( this );
706 :
707 0 : QAction * Add = new QAction ( tr ( "&Add" ), this );
708 0 : Add->setShortcut ( tr ( "Ctrl+A" ) );
709 0 : Add->setShortcutContext ( Qt::WidgetShortcut );
710 0 : connect ( Add, SIGNAL ( triggered() ), this, SLOT ( AddNewSuperClass() ) );
711 :
712 0 : QAction * Remove = new QAction ( tr ( "&Remove" ), this );
713 0 : Remove->setShortcut ( tr ( "Ctrl+R" ) );
714 0 : Remove->setShortcutContext ( Qt::WidgetShortcut );
715 0 : connect ( Remove, SIGNAL ( triggered() ), this, SLOT ( RemoveSuperClass() ) );
716 :
717 0 : ContextMenuClass->addAction ( Add );
718 0 : ContextMenuClass->addAction ( Remove );
719 : }
720 :
721 0 : QModelIndex Index = ui->SuperClassView->currentIndex();
722 :
723 0 : if ( Index.isValid() )
724 : {
725 0 : CurrentRow = SuperClassModel->getRowFromIndex ( Index );
726 0 : ContextMenuClass->exec ( ui->SuperClassView->mapToGlobal ( pos ) );
727 : }
728 0 : }
729 :
730 0 : void dbse::SchemaClassEditor::CustomMenuAttributeView ( QPoint pos )
731 : {
732 0 : if ( ContextMenuAttribute == nullptr )
733 : {
734 0 : ContextMenuAttribute = new QMenu ( this );
735 :
736 0 : QAction * Add = new QAction ( tr ( "&Add" ), this );
737 0 : Add->setShortcut ( tr ( "Ctrl+A" ) );
738 0 : Add->setShortcutContext ( Qt::WidgetShortcut );
739 0 : connect ( Add, SIGNAL ( triggered() ), this, SLOT ( AddNewAttribute() ) );
740 :
741 0 : QAction * Remove = new QAction ( tr ( "&Remove" ), this );
742 0 : Remove->setShortcut ( tr ( "Ctrl+R" ) );
743 0 : Remove->setShortcutContext ( Qt::WidgetShortcut );
744 0 : connect ( Remove, SIGNAL ( triggered() ), this, SLOT ( RemoveAttribute() ) );
745 :
746 0 : ContextMenuAttribute->addAction ( Add );
747 0 : ContextMenuAttribute->addAction ( Remove );
748 : }
749 :
750 0 : QModelIndex Index = ui->AttributeView->currentIndex();
751 :
752 0 : if ( Index.isValid() )
753 : {
754 0 : CurrentRow = AttributeModel->getRowFromIndex ( Index );
755 0 : ContextMenuAttribute->exec ( ui->AttributeView->mapToGlobal ( pos ) );
756 : }
757 0 : }
758 :
759 0 : void dbse::SchemaClassEditor::CustomMenuRelationshipView ( QPoint pos )
760 : {
761 0 : if ( ContextMenuRelationship == nullptr )
762 : {
763 0 : ContextMenuRelationship = new QMenu ( this );
764 :
765 0 : QAction * Add = new QAction ( tr ( "&Add" ), this );
766 0 : Add->setShortcut ( tr ( "Ctrl+A" ) );
767 0 : Add->setShortcutContext ( Qt::WidgetShortcut );
768 0 : connect ( Add, SIGNAL ( triggered() ), this, SLOT ( AddNewRelationship() ) );
769 :
770 0 : QAction * Remove = new QAction ( tr ( "&Remove" ), this );
771 0 : Remove->setShortcut ( tr ( "Ctrl+R" ) );
772 0 : Remove->setShortcutContext ( Qt::WidgetShortcut );
773 0 : connect ( Remove, SIGNAL ( triggered() ), this, SLOT ( RemoveRelationship() ) );
774 :
775 0 : ContextMenuRelationship->addAction ( Add );
776 0 : ContextMenuRelationship->addAction ( Remove );
777 : }
778 :
779 0 : QModelIndex Index = ui->RelationshipView->currentIndex();
780 :
781 0 : if ( Index.isValid() )
782 : {
783 0 : CurrentRow = RelationshipModel->getRowFromIndex ( Index );
784 0 : ContextMenuRelationship->exec ( ui->RelationshipView->mapToGlobal ( pos ) );
785 : }
786 0 : }
787 :
788 0 : void dbse::SchemaClassEditor::CustomMenuMethodView ( QPoint pos )
789 : {
790 0 : if ( ContextMenuMethod == nullptr )
791 : {
792 0 : ContextMenuMethod = new QMenu ( this );
793 :
794 0 : QAction * Add = new QAction ( tr ( "&Add" ), this );
795 0 : Add->setShortcut ( tr ( "Ctrl+A" ) );
796 0 : Add->setShortcutContext ( Qt::WidgetShortcut );
797 0 : connect ( Add, SIGNAL ( triggered() ), this, SLOT ( AddNewMethod() ) );
798 :
799 0 : QAction * Remove = new QAction ( tr ( "&Remove" ), this );
800 0 : Remove->setShortcut ( tr ( "Ctrl+R" ) );
801 0 : Remove->setShortcutContext ( Qt::WidgetShortcut );
802 0 : connect ( Remove, SIGNAL ( triggered() ), this, SLOT ( RemoveMethod() ) );
803 :
804 0 : ContextMenuMethod->addAction ( Add );
805 0 : ContextMenuMethod->addAction ( Remove );
806 : }
807 :
808 0 : QModelIndex Index = ui->MethodsView->currentIndex();
809 :
810 0 : if ( Index.isValid() )
811 : {
812 0 : CurrentRow = MethodModel->getRowFromIndex ( Index );
813 0 : const std::string& methodName = CurrentRow.at ( 0 ).toStdString();
814 0 : OksMethod * SchemaMethod = SchemaClass->find_direct_method ( methodName );
815 0 : if(SchemaMethod == nullptr) {
816 : // Can't remove inherited method
817 0 : ContextMenuMethod->actions().at ( 1 )->setVisible ( false );
818 : }
819 : else {
820 0 : ContextMenuMethod->actions().at ( 1 )->setVisible ( true );
821 : }
822 0 : ContextMenuMethod->exec ( ui->MethodsView->mapToGlobal ( pos ) );
823 0 : }
824 0 : }
825 :
826 0 : QString dbse::SchemaClassEditor::createNewClass ()
827 : {
828 0 : auto createNewClass = [] (const std::string& className) -> bool {
829 0 : if ( !KernelWrapper::GetInstance().IsActive() )
830 : {
831 0 : QMessageBox::warning (
832 : 0,
833 : "Schema editor",
834 0 : QString (
835 0 : "There is no active schema set!\n\nPlease set a schema file as active and continue!" ) );
836 :
837 0 : return false;
838 : }
839 :
840 0 : if ( !KernelWrapper::GetInstance().FindClass ( className ) )
841 : {
842 0 : KernelWrapper::GetInstance().PushCreateClassCommand ( className,
843 : "", false );
844 0 : return true;
845 : }
846 : else
847 : {
848 0 : QMessageBox::warning ( 0, "Schema editor",
849 0 : QString ( "Can not create class because class already exist !" ) );
850 :
851 0 : return false;
852 : }
853 : };
854 :
855 0 : bool ok;
856 0 : QString text = QInputDialog::getText(nullptr, "Schema editor: create new class",
857 : "New class name:", QLineEdit::Normal,
858 0 : "NewOksClass", &ok);
859 :
860 0 : if(ok && !text.isEmpty()) {
861 0 : if(createNewClass(text.toStdString())) {
862 0 : SchemaClassEditor * Editor = new SchemaClassEditor(KernelWrapper::GetInstance().FindClass(text.toStdString()));
863 0 : Editor->show();
864 : }
865 : }
866 0 : return text;
867 0 : }
868 :
869 0 : void dbse::SchemaClassEditor::launch(QString class_name) {
870 0 : bool widgetFound=false;
871 0 : dunedaq::oks::OksClass* class_info =
872 0 : KernelWrapper::GetInstance().FindClass (class_name.toStdString());
873 0 : for ( QWidget* widget : QApplication::allWidgets() ) {
874 0 : SchemaClassEditor* editor = dynamic_cast<SchemaClassEditor *> (widget);
875 0 : if (editor != nullptr) {
876 0 : if ((editor->objectName()).compare(class_name) == 0 ) {
877 0 : widget->raise();
878 0 : widget->setVisible ( true );
879 0 : widget->activateWindow();
880 : widgetFound = true;
881 : break;
882 : }
883 : }
884 0 : }
885 :
886 0 : if ( !widgetFound ) {
887 0 : SchemaClassEditor* editor = new SchemaClassEditor (class_info);
888 0 : editor->show();
889 : }
890 0 : }
891 :
|