Salome HOME
Issue #1477 Build Vertex - wrong selection in viewer
[modules/shaper.git] / src / ModuleBase / ModuleBase_Dialog.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3
4 #include "ModuleBase_Dialog.h"
5 #include "ModuleBase_WidgetFactory.h"
6 #include "ModuleBase_IWorkshop.h"
7 #include "ModuleBase_IPropertyPanel.h"
8 #include "ModuleBase_PageWidget.h"
9
10 #include <ModelAPI_Session.h>
11 #include <ModelAPI_Events.h>
12 #include <Events_Loop.h>
13
14 #include <Config_WidgetAPI.h>
15 #include <Config_Keywords.h>
16
17 #include <QMainWindow>
18 #include <QLayout>
19 #include <QDialogButtonBox>
20 #include <QPushButton>
21
22
23 ModuleBase_Dialog::ModuleBase_Dialog(ModuleBase_IWorkshop* theParent, const QString& theId, 
24                                      const std::string& theDescription) : 
25                                      QDialog(theParent->desktop()), 
26                                      myId(theId), 
27                                      myDescription(theDescription), 
28                                      myWorkshop(theParent),
29                                      myActiveWidget(0)
30 {
31   ModuleBase_WidgetFactory aFactory(myDescription, myWorkshop);
32   std::string aTitle = aFactory.widgetAPI()->getProperty(FEATURE_TEXT);
33
34   setWindowTitle(aTitle.c_str());
35
36   SessionPtr aMgr = ModelAPI_Session::get();
37   std::shared_ptr<ModelAPI_Document> aDoc = aMgr->activeDocument();
38
39   myFeature = aDoc->addFeature(myId.toStdString());
40   if (!myFeature.get())
41     return;
42   Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
43   Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
44
45   QVBoxLayout* aLayout = new QVBoxLayout(this);
46   aLayout->setContentsMargins(0, 0, 0, 0);
47   aLayout->setSpacing(1);
48   //setSizePolicy(QSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred));
49
50   ModuleBase_PageWidget* aPage = new ModuleBase_PageWidget(this);
51   aLayout->addWidget(aPage);
52
53   aFactory.createWidget(aPage, false);
54   myWidgets = aFactory.getModelWidgets();
55   foreach (ModuleBase_ModelWidget* aWidget, myWidgets) {
56     initializeWidget(aWidget);
57   }
58
59   QFrame* aFrame = new QFrame(this);
60   aFrame->setFrameStyle(QFrame::WinPanel | QFrame::Raised);
61   aLayout->addWidget(aFrame);
62
63   QVBoxLayout* aBtnLayout = new QVBoxLayout(aFrame);
64   ModuleBase_Tools::adjustMargins(aBtnLayout);
65
66   QDialogButtonBox* aBtnBox = new QDialogButtonBox(
67     QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal, aFrame);
68   aBtnLayout->addWidget(aBtnBox);
69
70   aBtnBox->button(QDialogButtonBox::Ok)->setIcon(QIcon(":pictures/button_ok.png"));
71   aBtnBox->button(QDialogButtonBox::Cancel)->setIcon(QIcon(":pictures/button_cancel.png"));
72
73   connect(aBtnBox, SIGNAL(accepted()), this, SLOT(accept()));
74   connect(aBtnBox, SIGNAL(rejected()), this, SLOT(reject()));
75 }
76
77 void ModuleBase_Dialog::initializeWidget(ModuleBase_ModelWidget* theWidget)
78 {
79   theWidget->setFeature(myFeature);
80   theWidget->restoreValue();
81 }
82
83 void ModuleBase_Dialog::showEvent(QShowEvent* theEvent)
84 {
85   QDialog::showEvent(theEvent);
86   if (myWidgets.length() > 0) {
87     myActiveWidget = myWidgets.first();
88     myActiveWidget->activate();
89   }
90 }
91
92 void ModuleBase_Dialog::accept()
93 {
94   if (myActiveWidget) {
95     if (!myActiveWidget->storeValue())
96       return;
97   }
98   QDialog::accept();
99 }