Salome HOME
Copyright update 2020
[modules/shaper.git] / src / ModuleBase / ModuleBase_Dialog.cpp
1 // Copyright (C) 2014-2020  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 #include "ModuleBase_Tools.h"
27
28 #include <ModelAPI_Session.h>
29 #include <ModelAPI_Events.h>
30 #include <Events_Loop.h>
31
32 #include <Config_WidgetAPI.h>
33 #include <Config_Keywords.h>
34
35 #include <QMainWindow>
36 #include <QLayout>
37 #include <QDialogButtonBox>
38 #include <QPushButton>
39
40
41 ModuleBase_Dialog::ModuleBase_Dialog(ModuleBase_IWorkshop* theParent, const QString& theId,
42                                      const std::string& theDescription) :
43   QDialog(theParent->desktop(),
44     Qt::Dialog | Qt::CustomizeWindowHint | Qt::WindowTitleHint | Qt::WindowCloseButtonHint),
45   myId(theId),
46   myDescription(theDescription),
47   myWorkshop(theParent),
48   myActiveWidget(0)
49 {
50   ModuleBase_WidgetFactory aFactory(myDescription, myWorkshop);
51   QString aTitle = ModuleBase_Tools::translate("ModuleBase_Dialog",
52       aFactory.widgetAPI()->getProperty(FEATURE_TEXT));
53
54   setWindowTitle(aTitle);
55
56   SessionPtr aMgr = ModelAPI_Session::get();
57   std::shared_ptr<ModelAPI_Document> aDoc = aMgr->activeDocument();
58
59   myFeature = aDoc->addFeature(myId.toStdString());
60   if (!myFeature.get())
61     return;
62   Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
63   Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
64
65   QVBoxLayout* aLayout = new QVBoxLayout(this);
66   aLayout->setContentsMargins(0, 0, 0, 0);
67   aLayout->setSpacing(1);
68   //setSizePolicy(QSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred));
69
70   ModuleBase_PageWidget* aPage = new ModuleBase_PageWidget(this);
71   aLayout->addWidget(aPage);
72
73   aFactory.createWidget(aPage, false);
74   myWidgets = aFactory.getModelWidgets();
75
76   QFrame* aFrame = new QFrame(this);
77   aFrame->setFrameStyle(QFrame::WinPanel | QFrame::Raised);
78   aLayout->addWidget(aFrame);
79
80   QHBoxLayout* aBtnLayout = new QHBoxLayout(aFrame);
81   ModuleBase_Tools::adjustMargins(aBtnLayout);
82
83   myButtonsBox = new QDialogButtonBox(
84     QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal, aFrame);
85   aBtnLayout->addWidget(myButtonsBox);
86
87   myButtonsBox->button(QDialogButtonBox::Ok)->setIcon(QIcon(":pictures/button_ok.png"));
88   myButtonsBox->button(QDialogButtonBox::Cancel)->setIcon(QIcon(":pictures/button_cancel.png"));
89
90   connect(myButtonsBox, SIGNAL(accepted()), this, SLOT(accept()));
91   connect(myButtonsBox, SIGNAL(rejected()), this, SLOT(reject()));
92
93   foreach (ModuleBase_ModelWidget* aWidget, myWidgets) {
94     initializeWidget(aWidget);
95   }
96 }
97
98 void ModuleBase_Dialog::initializeWidget(ModuleBase_ModelWidget* theWidget)
99 {
100   ModuleBase_ModelDialogWidget* aDlgWgt = dynamic_cast<ModuleBase_ModelDialogWidget*>(theWidget);
101   if (aDlgWgt)
102     aDlgWgt->setDialogButtons(myButtonsBox);
103
104   theWidget->setFeature(myFeature);
105   theWidget->restoreValue();
106 }
107
108 void ModuleBase_Dialog::showEvent(QShowEvent* theEvent)
109 {
110   QDialog::showEvent(theEvent);
111   if (myWidgets.length() > 0) {
112     myActiveWidget = myWidgets.first();
113     myActiveWidget->activate();
114   }
115 }
116
117 void ModuleBase_Dialog::accept()
118 {
119   if (myActiveWidget) {
120     if (!myActiveWidget->storeValue())
121       return;
122   }
123   QDialog::accept();
124 }