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