Salome HOME
Merge branch 'V9_2_2_BR'
[modules/shaper.git] / src / ModuleBase / ModuleBase_Dialog.cpp
1 // Copyright (C) 2014-2019  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "ModuleBase_Dialog.h"
21 #include "ModuleBase_WidgetFactory.h"
22 #include "ModuleBase_IWorkshop.h"
23 #include "ModuleBase_IPropertyPanel.h"
24 #include "ModuleBase_PageWidget.h"
25 #include "ModuleBase_ModelDialogWidget.h"
26
27 #include <ModelAPI_Session.h>
28 #include <ModelAPI_Events.h>
29 #include <Events_Loop.h>
30
31 #include <Config_WidgetAPI.h>
32 #include <Config_Keywords.h>
33
34 #include <QMainWindow>
35 #include <QLayout>
36 #include <QDialogButtonBox>
37 #include <QPushButton>
38
39
40 ModuleBase_Dialog::ModuleBase_Dialog(ModuleBase_IWorkshop* theParent, const QString& theId,
41                                      const std::string& theDescription) :
42                                      QDialog(theParent->desktop()),
43                                      myId(theId),
44                                      myDescription(theDescription),
45                                      myWorkshop(theParent),
46                                      myActiveWidget(0)
47 {
48   ModuleBase_WidgetFactory aFactory(myDescription, myWorkshop);
49   std::string aTitle = aFactory.widgetAPI()->getProperty(FEATURE_TEXT);
50
51   setWindowTitle(aTitle.c_str());
52
53   SessionPtr aMgr = ModelAPI_Session::get();
54   std::shared_ptr<ModelAPI_Document> aDoc = aMgr->activeDocument();
55
56   myFeature = aDoc->addFeature(myId.toStdString());
57   if (!myFeature.get())
58     return;
59   Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
60   Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
61
62   QVBoxLayout* aLayout = new QVBoxLayout(this);
63   aLayout->setContentsMargins(0, 0, 0, 0);
64   aLayout->setSpacing(1);
65   //setSizePolicy(QSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred));
66
67   ModuleBase_PageWidget* aPage = new ModuleBase_PageWidget(this);
68   aLayout->addWidget(aPage);
69
70   aFactory.createWidget(aPage, false);
71   myWidgets = aFactory.getModelWidgets();
72
73   QFrame* aFrame = new QFrame(this);
74   aFrame->setFrameStyle(QFrame::WinPanel | QFrame::Raised);
75   aLayout->addWidget(aFrame);
76
77   QHBoxLayout* aBtnLayout = new QHBoxLayout(aFrame);
78   ModuleBase_Tools::adjustMargins(aBtnLayout);
79
80   myButtonsBox = new QDialogButtonBox(
81     QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal, aFrame);
82   aBtnLayout->addWidget(myButtonsBox);
83
84   myButtonsBox->button(QDialogButtonBox::Ok)->setIcon(QIcon(":pictures/button_ok.png"));
85   myButtonsBox->button(QDialogButtonBox::Cancel)->setIcon(QIcon(":pictures/button_cancel.png"));
86
87   connect(myButtonsBox, SIGNAL(accepted()), this, SLOT(accept()));
88   connect(myButtonsBox, SIGNAL(rejected()), this, SLOT(reject()));
89
90   foreach (ModuleBase_ModelWidget* aWidget, myWidgets) {
91     initializeWidget(aWidget);
92   }
93 }
94
95 void ModuleBase_Dialog::initializeWidget(ModuleBase_ModelWidget* theWidget)
96 {
97   ModuleBase_ModelDialogWidget* aDlgWgt = dynamic_cast<ModuleBase_ModelDialogWidget*>(theWidget);
98   if (aDlgWgt)
99     aDlgWgt->setDialogButtons(myButtonsBox);
100
101   theWidget->setFeature(myFeature);
102   theWidget->restoreValue();
103 }
104
105 void ModuleBase_Dialog::showEvent(QShowEvent* theEvent)
106 {
107   QDialog::showEvent(theEvent);
108   if (myWidgets.length() > 0) {
109     myActiveWidget = myWidgets.first();
110     myActiveWidget->activate();
111   }
112 }
113
114 void ModuleBase_Dialog::accept()
115 {
116   if (myActiveWidget) {
117     if (!myActiveWidget->storeValue())
118       return;
119   }
120   QDialog::accept();
121 }