Salome HOME
b1a33cbeebae616dcf5bc5483163330c7a7e3cc4
[modules/shaper.git] / src / ModuleBase / ModuleBase_Dialog.cpp
1 // Copyright (C) 2014-2023  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,
42   const std::string& theDescription) :
43   QDialog(theParent->desktop(),
44     Qt::Dialog | Qt::CustomizeWindowHint | Qt::WindowTitleHint | Qt::WindowCloseButtonHint),
45   myDescription(theDescription),
46   myWorkshop(theParent),
47   myActiveWidget(0)
48 {
49   Config_WidgetAPI aApi(myDescription, "");
50   myId = aApi.getProperty("id");
51
52   std::shared_ptr<Config_FeatureMessage> aFeatureInfo = myWorkshop->featureInfo(myId.c_str());
53   myHelpPage = aFeatureInfo->helpFileName();
54
55   ModuleBase_WidgetFactory aFactory(myDescription, myWorkshop);
56   QString aTitle = ModuleBase_Tools::translate("ModuleBase_Dialog",
57       aFactory.widgetAPI()->getProperty(FEATURE_TEXT));
58
59   setWindowTitle(aTitle);
60
61   SessionPtr aMgr = ModelAPI_Session::get();
62   std::shared_ptr<ModelAPI_Document> aDoc = aMgr->activeDocument();
63
64   myFeature = aDoc->addFeature(myId);
65   if (!myFeature.get())
66     return;
67   Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
68   Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
69
70   QVBoxLayout* aLayout = new QVBoxLayout(this);
71   aLayout->setContentsMargins(0, 0, 0, 0);
72   aLayout->setSpacing(1);
73
74   ModuleBase_PageWidget* aPage = new ModuleBase_PageWidget(this);
75   aLayout->addWidget(aPage);
76
77   aFactory.createWidget(aPage, false);
78   myWidgets = aFactory.getModelWidgets();
79
80   QFrame* aFrame = new QFrame(this);
81   aFrame->setFrameStyle(QFrame::WinPanel | QFrame::Raised);
82   aLayout->addWidget(aFrame);
83
84   QHBoxLayout* aBtnLayout = new QHBoxLayout(aFrame);
85   ModuleBase_Tools::adjustMargins(aBtnLayout);
86
87   myButtonsBox = new QDialogButtonBox(
88     QDialogButtonBox::Ok | QDialogButtonBox::Cancel | QDialogButtonBox::Help,
89     Qt::Horizontal, aFrame);
90   aBtnLayout->addWidget(myButtonsBox);
91
92   myButtonsBox->button(QDialogButtonBox::Ok)->setIcon(QIcon(":pictures/button_ok.png"));
93   myButtonsBox->button(QDialogButtonBox::Cancel)->setIcon(QIcon(":pictures/button_cancel.png"));
94   myButtonsBox->button(QDialogButtonBox::Help)->setIcon(QIcon(":pictures/button_help.png"));
95
96   connect(myButtonsBox, SIGNAL(accepted()), this, SLOT(accept()));
97   connect(myButtonsBox, SIGNAL(rejected()), this, SLOT(reject()));
98   connect(myButtonsBox, SIGNAL(helpRequested()), this, SLOT(onHelpRequest()));
99
100   foreach (ModuleBase_ModelWidget* aWidget, myWidgets) {
101     initializeWidget(aWidget);
102   }
103 }
104
105 void ModuleBase_Dialog::initializeWidget(ModuleBase_ModelWidget* theWidget)
106 {
107   ModuleBase_ModelDialogWidget* aDlgWgt = dynamic_cast<ModuleBase_ModelDialogWidget*>(theWidget);
108   if (aDlgWgt)
109     aDlgWgt->setDialogButtons(myButtonsBox);
110
111   theWidget->setFeature(myFeature);
112   theWidget->restoreValue();
113 }
114
115 void ModuleBase_Dialog::showEvent(QShowEvent* theEvent)
116 {
117   QDialog::showEvent(theEvent);
118   if (myWidgets.length() > 0) {
119     myActiveWidget = myWidgets.first();
120     myActiveWidget->activate();
121   }
122 }
123
124 void ModuleBase_Dialog::accept()
125 {
126   if (myActiveWidget) {
127     if (!myActiveWidget->storeValue())
128       return;
129   }
130   QDialog::accept();
131 }
132
133 void ModuleBase_Dialog::onHelpRequest()
134 {
135   myWorkshop->showHelpPage(myHelpPage.c_str());
136 }