1 // Copyright (C) 2014-2019 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include <ModuleBase_WidgetOptionalBox.h>
21 #include <ModelAPI_AttributeString.h>
22 #include <ModelAPI_AttributeBoolean.h>
24 #include <Config_WidgetAPI.h>
25 #include <Config_Keywords.h>
30 #include <QGridLayout>
31 #include <QVBoxLayout>
35 ModuleBase_WidgetOptionalBox::ModuleBase_WidgetOptionalBox(QWidget* theParent,
36 const Config_WidgetAPI* theData)
37 : ModuleBase_ModelWidget(theParent, theData),
38 ModuleBase_PageBase(),
39 myOptionType(CheckBox),
47 myCheckGroupLayout(0),
52 myToolTip = theData->widgetTooltip();
53 myGroupTitle = theData->getProperty(CONTAINER_PAGE_NAME);
55 myHaveFrame = theData->getBooleanAttribute("has_frame", true);
56 myEnableOnCheck = theData->getBooleanAttribute("enable_on_check", true);
58 bool isChecked = theData->getBooleanAttribute(ATTR_DEFAULT, false);
59 setDefaultValue(isChecked ? "true" : "false");
61 myMainLayout = new QVBoxLayout(this);
62 ModuleBase_Tools::adjustMargins(myMainLayout);
65 ModuleBase_WidgetOptionalBox::~ModuleBase_WidgetOptionalBox()
69 QWidget* ModuleBase_WidgetOptionalBox::pageWidget()
71 return myOptionType == GroupBox ? (myGroupBox? myGroupBox : myCheckGroup) :
72 (QWidget*)myCheckBoxFrame;
75 QList<QWidget*> ModuleBase_WidgetOptionalBox::getControls() const
77 QList<QWidget*> aControls;
78 if (myOptionType == GroupBox) {
80 aControls.append(myGroupBox);
82 aControls.append(myCheckGroup);
85 aControls.append(myCheckBoxFrame);
90 void ModuleBase_WidgetOptionalBox::onPageClicked()
93 updateControlsVisibility();
96 emit focusOutWidget(this);
99 void ModuleBase_WidgetOptionalBox::addPageStretch()
103 void ModuleBase_WidgetOptionalBox::placeModelWidget(ModuleBase_ModelWidget* theWidget)
105 if(!isCheckBoxFilled() && myOptionType == CheckBox) {
106 createControl(CheckBox);
107 setOptionType(CheckBox);
109 myCheckBoxWidget = theWidget; /// check box frame becomes filled
110 myCheckBoxLayout->addWidget(theWidget, 1);
113 createControl(GroupBox);
114 setOptionType(GroupBox);
116 ModuleBase_ModelWidget* aCheckBoxWidget = myCheckBoxWidget;
117 myCheckBoxWidget = 0;
118 if (aCheckBoxWidget) // move the model widget from check box frame to group box frame
119 placeModelWidget(aCheckBoxWidget);
122 const int kRow = myGroupBoxLayout->count();
123 // it seems, that the align on left is not necessary here, but leads to widgets, which are
124 // not extended on full width of the parent page. The case is grouped widgets in
125 // the sketch translation operation
126 myGroupBoxLayout->addWidget(theWidget, kRow, kCol, Qt::AlignTop);// | Qt::AlignLeft);
127 myGroupBoxLayout->setRowStretch(kRow, 0);
131 void ModuleBase_WidgetOptionalBox::placeWidget(QWidget* theWidget)
133 createControl(GroupBox);
134 setOptionType(GroupBox);
135 if (isCheckBoxFilled())
136 placeModelWidget(myCheckBoxWidget);
141 std::cout << "ModuleBase_PageGroupBox::placePageWidget: can not cast page" << std::endl;
145 if (myGroupBoxLayout) {
147 const int kRow = myGroupBoxLayout->count();
148 myGroupBoxLayout->addWidget(theWidget, kRow, kCol);
149 myGroupBoxLayout->setRowStretch(kRow, 0);
152 myCheckGroupLayout->addWidget(theWidget);
156 QLayout* ModuleBase_WidgetOptionalBox::pageLayout()
158 return myOptionType == GroupBox ? myGroupBoxLayout : (QLayout*)myCheckBoxLayout;
161 void ModuleBase_WidgetOptionalBox::createControl(const OptionType& theType)
163 if (theType == GroupBox && !myGroupBox) {
164 // group box: more than one model widget is inside
166 myGroupBox = new QGroupBox(this);
167 myGroupBox->setTitle(translate(myGroupTitle));
168 myGroupBox->setVisible(false);
169 myGroupBox->setCheckable(true);
170 myGroupBox->setChecked(getDefaultValue() == "true");
171 myGroupBox->setToolTip(translate(myToolTip));
173 myGroupBoxLayout = new QGridLayout(myGroupBox);
174 ModuleBase_Tools::zeroMargins(myGroupBoxLayout);
175 myGroupBox->setLayout(myGroupBoxLayout);
177 // default vertical size policy is preferred
178 myMainLayout->addWidget(myGroupBox);
179 connect(myGroupBox, SIGNAL(clicked(bool)), this, SLOT(onPageClicked()));
182 myCheckGroup = new QWidget(this);
183 QVBoxLayout* aLayout = new QVBoxLayout(myCheckGroup);
184 ModuleBase_Tools::zeroMargins(aLayout);
186 myCheckGroupBtn = new QCheckBox(translate(myGroupTitle), myCheckGroup);
187 aLayout->addWidget(myCheckGroupBtn);
189 myCheckContent = new QWidget(myCheckGroup);
190 myCheckGroupLayout = new QVBoxLayout(myCheckContent);
191 ModuleBase_Tools::zeroMargins(myCheckGroupLayout);
192 aLayout->addWidget(myCheckContent);
194 myMainLayout->addWidget(myCheckGroup);
195 connect(myCheckGroupBtn, SIGNAL(toggled(bool)), this, SLOT(onPageClicked()));
198 else if (theType == CheckBox && !myCheckBoxFrame) {
199 myCheckBoxFrame = new QFrame(this);
200 myMainLayout->addWidget(myCheckBoxFrame);
202 myCheckBoxLayout = new QHBoxLayout(myCheckBoxFrame);
203 ModuleBase_Tools::adjustMargins(myCheckBoxLayout);
204 myCheckBox = new QCheckBox(myCheckBoxFrame);
205 myCheckBox->setChecked(getDefaultValue() == "true");
206 myCheckBoxLayout->addWidget(myCheckBox);
208 connect(myCheckBox, SIGNAL(clicked(bool)), this, SLOT(onPageClicked()));
212 bool ModuleBase_WidgetOptionalBox::storeValueCustom()
214 DataPtr aData = myFeature->data();
215 AttributeStringPtr aStringAttr = aData->string(attributeID());
216 if (aStringAttr.get())
217 aStringAttr->setValue(getCurrentValue() ? attributeID() : "");
219 AttributeBooleanPtr aBoolAtr = aData->boolean(attributeID());
220 aBoolAtr->setValue(getCurrentValue());
223 updateObject(myFeature);
228 bool ModuleBase_WidgetOptionalBox::restoreValueCustom()
230 DataPtr aData = myFeature->data();
231 AttributeStringPtr aStringAttr = aData->string(attributeID());
232 if (aStringAttr.get())
233 setCurrentValue(!aStringAttr->value().empty());
235 AttributeBooleanPtr aBoolAtr = aData->boolean(attributeID());
236 setCurrentValue(aBoolAtr->value());
241 void ModuleBase_WidgetOptionalBox::setOptionType(
242 const ModuleBase_WidgetOptionalBox::OptionType& theType)
244 myOptionType = theType;
246 bool isGroupBox = myOptionType == GroupBox;
248 myCheckBoxFrame->setVisible(!isGroupBox);
250 myGroupBox->setVisible(isGroupBox);
251 else if (myCheckContent)
252 myCheckGroup->setVisible(isGroupBox);
255 bool ModuleBase_WidgetOptionalBox::isCheckBoxFilled() const
257 return myCheckBoxWidget != 0;
260 bool ModuleBase_WidgetOptionalBox::getCurrentValue() const
262 bool isGroupBox = myOptionType == GroupBox;
263 return isGroupBox ? (myGroupBox? myGroupBox->isChecked() : myCheckGroupBtn->isChecked()) :
264 myCheckBox->isChecked();
267 void ModuleBase_WidgetOptionalBox::setCurrentValue(const bool& theValue)
269 bool isGroupBox = myOptionType == GroupBox;
272 bool isBlocked = myGroupBox->blockSignals(true);
273 myGroupBox->setChecked(theValue);
274 myGroupBox->blockSignals(isBlocked);
277 bool isBlocked = myCheckGroupBtn->blockSignals(true);
278 myCheckGroupBtn->setChecked(theValue);
279 myCheckGroupBtn->blockSignals(isBlocked);
283 bool isBlocked = myCheckBox->blockSignals(true);
284 myCheckBox->setChecked(theValue);
285 myCheckBox->blockSignals(isBlocked);
287 updateControlsVisibility();
290 void ModuleBase_WidgetOptionalBox::updateControlsVisibility()
292 if (myOptionType == GroupBox) {
293 bool aChecked = toEnableWidgets();
294 ModuleBase_Tools::adjustMargins(myGroupBoxLayout);
296 QLayout* aLayout = myGroupBoxLayout ? myGroupBoxLayout : (QLayout*)myCheckGroupLayout;
298 int aNbSubControls = aLayout->count();
299 for (int i = 0; i < aNbSubControls; i++) {
300 QWidget* aWidget = aLayout->itemAt(i)->widget();
302 aWidget->setEnabled(aChecked);
306 myCheckBoxWidget->setEnabled(toEnableWidgets());
310 bool ModuleBase_WidgetOptionalBox::toEnableWidgets() const
313 if (myOptionType == GroupBox)
314 aChecked = myGroupBox ? myGroupBox->isChecked() : myCheckGroupBtn->isChecked();
316 aChecked = myCheckBox->isChecked();