]> SALOME platform Git repositories - modules/shaper.git/blob - src/ModuleBase/ModuleBase_WidgetOptionalBox.cpp
Salome HOME
Add copyright header according to request of CEA from 06.06.2017
[modules/shaper.git] / src / ModuleBase / ModuleBase_WidgetOptionalBox.cpp
1 // Copyright (C) 2014-2017  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<mailto:webmaster.salome@opencascade.com>
18 //
19
20 #include <ModuleBase_WidgetOptionalBox.h>
21 #include <ModelAPI_AttributeString.h>
22
23 #include <Config_WidgetAPI.h>
24 #include <Config_Keywords.h>
25
26 #include <QWidget>
27 #include <QGroupBox>
28 #include <QCheckBox>
29 #include <QGridLayout>
30 #include <QVBoxLayout>
31
32 #include <QList>
33
34 ModuleBase_WidgetOptionalBox::ModuleBase_WidgetOptionalBox(QWidget* theParent,
35                                                                const Config_WidgetAPI* theData)
36 : ModuleBase_ModelWidget(theParent, theData),
37   ModuleBase_PageBase(),
38   myOptionType(CheckBox),
39   myCheckBoxFrame(0),
40   myCheckBox(0),
41   myCheckBoxLayout(0),
42   myCheckBoxWidget(0),
43   myGroupBox(0),
44   myGroupBoxLayout(0)
45 {
46   myToolTip = theData->widgetTooltip();
47   myGroupTitle = theData->getProperty(CONTAINER_PAGE_NAME);
48
49   bool isChecked = theData->getBooleanAttribute(ATTR_DEFAULT, false);
50   setDefaultValue(isChecked ? "true" : "false");
51
52   myMainLayout = new QVBoxLayout(this);
53   ModuleBase_Tools::adjustMargins(myMainLayout);
54 }
55
56 ModuleBase_WidgetOptionalBox::~ModuleBase_WidgetOptionalBox()
57 {
58 }
59
60 QWidget* ModuleBase_WidgetOptionalBox::pageWidget()
61 {
62   return myOptionType == GroupBox ? myGroupBox : (QWidget*)myCheckBoxFrame;
63 }
64
65 QList<QWidget*> ModuleBase_WidgetOptionalBox::getControls() const
66 {
67   QList<QWidget*> aControls;
68   if (myOptionType == GroupBox)
69     aControls.append(myGroupBox);
70   else
71     aControls.append(myCheckBoxFrame);
72
73   return aControls;
74 }
75
76 void ModuleBase_WidgetOptionalBox::onPageClicked()
77 {
78   storeValue();
79   updateControlsVisibility();
80
81   if (!isEditingMode())
82     emit focusOutWidget(this);
83 }
84
85 void ModuleBase_WidgetOptionalBox::addPageStretch()
86 {
87 }
88
89 void ModuleBase_WidgetOptionalBox::placeModelWidget(ModuleBase_ModelWidget* theWidget)
90 {
91   if(!isCheckBoxFilled() && myOptionType == CheckBox) {
92     createControl(CheckBox);
93     setOptionType(CheckBox);
94
95     myCheckBoxWidget = theWidget; /// check box frame becomes filled
96     myCheckBoxLayout->addWidget(theWidget);
97   }
98   else {
99     createControl(GroupBox);
100     setOptionType(GroupBox);
101
102     ModuleBase_ModelWidget* aCheckBoxWidget = myCheckBoxWidget;
103     myCheckBoxWidget = 0;
104     if (aCheckBoxWidget) // move the model widget from check box frame to group box frame
105       placeModelWidget(aCheckBoxWidget);
106
107     const int kCol = 0;
108     const int kRow = myGroupBoxLayout->count();
109     // it seems, that the align on left is not necessary here, but leads to widgets, which are
110     // not extended on full width of the parent page. The case is grouped widgets in
111     // the sketch translation operation
112     myGroupBoxLayout->addWidget(theWidget, kRow, kCol, Qt::AlignTop);// | Qt::AlignLeft);
113     myGroupBoxLayout->setRowStretch(kRow, 0);
114   }
115 }
116
117 void ModuleBase_WidgetOptionalBox::placeWidget(QWidget* theWidget)
118 {
119   createControl(GroupBox);
120   setOptionType(GroupBox);
121   if (isCheckBoxFilled())
122     placeModelWidget(myCheckBoxWidget);
123
124
125   if (!theWidget) {
126 #ifdef _DEBUG
127     std::cout << "ModuleBase_PageGroupBox::placePageWidget: can not cast page" << std::endl;
128 #endif
129     return;
130   }
131   const int kCol = 0;
132   const int kRow = myGroupBoxLayout->count();
133   myGroupBoxLayout->addWidget(theWidget, kRow, kCol);
134   myGroupBoxLayout->setRowStretch(kRow, 0);
135 }
136
137 QLayout* ModuleBase_WidgetOptionalBox::pageLayout()
138 {
139   return myOptionType == GroupBox ? myGroupBoxLayout : (QLayout*)myCheckBoxLayout;
140 }
141
142 void ModuleBase_WidgetOptionalBox::createControl(const OptionType& theType)
143 {
144   if (theType == GroupBox && !myGroupBox) {
145     // group box: more than one model widget is inside
146     myGroupBox = new QGroupBox(this);
147     myGroupBox->setTitle(translate(myGroupTitle));
148     myGroupBox->setVisible(false);
149     myGroupBox->setCheckable(true);
150     myGroupBox->setChecked(getDefaultValue() == "true");
151     myGroupBox->setToolTip(translate(myToolTip));
152
153     myGroupBoxLayout = new QGridLayout(myGroupBox);
154     ModuleBase_Tools::zeroMargins(myGroupBoxLayout);
155     myGroupBox->setLayout(myGroupBoxLayout);
156
157     // default vertical size policy is preferred
158     myMainLayout->addWidget(myGroupBox);
159     connect(myGroupBox, SIGNAL(clicked(bool)), this, SLOT(onPageClicked()));
160   }
161   else if (theType == CheckBox && !myCheckBoxFrame) {
162     myCheckBoxFrame = new QFrame(this);
163     myMainLayout->addWidget(myCheckBoxFrame);
164
165     myCheckBoxLayout = new QHBoxLayout(myCheckBoxFrame);
166     ModuleBase_Tools::adjustMargins(myCheckBoxLayout);
167     myCheckBox = new QCheckBox(myCheckBoxFrame);
168     myCheckBox->setChecked(getDefaultValue() == "true");
169     myCheckBoxLayout->addWidget(myCheckBox);
170
171     connect(myCheckBox, SIGNAL(clicked(bool)), this, SLOT(onPageClicked()));
172   }
173 }
174
175 bool ModuleBase_WidgetOptionalBox::storeValueCustom()
176 {
177   DataPtr aData = myFeature->data();
178   AttributeStringPtr aStringAttr = aData->string(attributeID());
179   aStringAttr->setValue(getCurrentValue() ? attributeID() : "");
180
181   updateObject(myFeature);
182
183   return true;
184 }
185
186 bool ModuleBase_WidgetOptionalBox::restoreValueCustom()
187 {
188   DataPtr aData = myFeature->data();
189   AttributeStringPtr aStringAttr = aData->string(attributeID());
190   setCurrentValue(!aStringAttr->value().empty());
191
192   return true;
193 }
194
195 void ModuleBase_WidgetOptionalBox::setOptionType(
196                          const ModuleBase_WidgetOptionalBox::OptionType& theType)
197 {
198   myOptionType = theType;
199
200   bool isGroupBox = myOptionType == GroupBox;
201   if (myCheckBoxFrame)
202     myCheckBoxFrame->setVisible(!isGroupBox);
203   if (myGroupBox)
204     myGroupBox->setVisible(isGroupBox);
205 }
206
207 bool ModuleBase_WidgetOptionalBox::isCheckBoxFilled() const
208 {
209   return myCheckBoxWidget != 0;
210 }
211
212 bool ModuleBase_WidgetOptionalBox::getCurrentValue() const
213 {
214   bool isGroupBox = myOptionType == GroupBox;
215   return isGroupBox ? myGroupBox->isChecked() : myCheckBox->isChecked();
216 }
217
218 void ModuleBase_WidgetOptionalBox::setCurrentValue(const bool& theValue)
219 {
220   bool isGroupBox = myOptionType == GroupBox;
221   if (isGroupBox) {
222     bool isBlocked = myGroupBox->blockSignals(true);
223     myGroupBox->setChecked(theValue);
224     myGroupBox->blockSignals(isBlocked);
225   }
226   else {
227     bool isBlocked = myCheckBox->blockSignals(true);
228     myCheckBox->setChecked(theValue);
229     myCheckBox->blockSignals(isBlocked);
230   }
231   updateControlsVisibility();
232 }
233
234 void ModuleBase_WidgetOptionalBox::updateControlsVisibility()
235 {
236   if (myOptionType == GroupBox) {
237     bool aChecked = myGroupBox->isChecked();
238     ModuleBase_Tools::adjustMargins(myGroupBoxLayout);
239
240     int aNbSubControls = myGroupBoxLayout->count();
241     for (int i = 0; i < aNbSubControls; i++) {
242       QWidget* aWidget = myGroupBoxLayout->itemAt(i)->widget();
243       if (aWidget)
244         aWidget->setEnabled(aChecked);
245     }
246   }
247   else {
248     bool aChecked = myCheckBox->isChecked();
249     myCheckBoxWidget->setEnabled(aChecked);
250   }
251 }