1 // Copyright (C) 2014-2021 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),
49 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);
57 myAlwaysShowTitle = theData->getBooleanAttribute("show_title", false);
59 bool isChecked = theData->getBooleanAttribute(ATTR_DEFAULT, false);
60 setDefaultValue(isChecked ? "true" : "false");
62 myMainLayout = new QVBoxLayout(this);
63 ModuleBase_Tools::adjustMargins(myMainLayout);
66 ModuleBase_WidgetOptionalBox::~ModuleBase_WidgetOptionalBox()
70 QWidget* ModuleBase_WidgetOptionalBox::pageWidget()
72 return myOptionType == GroupBox ? (myGroupBox? myGroupBox : myCheckGroup) :
73 (QWidget*)myCheckBoxFrame;
76 QList<QWidget*> ModuleBase_WidgetOptionalBox::getControls() const
78 QList<QWidget*> aControls;
79 if (myOptionType == GroupBox) {
81 aControls.append(myGroupBox);
83 aControls.append(myCheckGroup);
86 aControls.append(myCheckBoxFrame);
91 void ModuleBase_WidgetOptionalBox::onPageClicked()
94 updateControlsVisibility();
96 // The widget doesn't accept a focus
97 //if (!isEditingMode())
98 // emit focusOutWidget(this);
101 void ModuleBase_WidgetOptionalBox::addPageStretch()
105 void ModuleBase_WidgetOptionalBox::placeModelWidget(ModuleBase_ModelWidget* theWidget)
107 if(!isCheckBoxFilled() && myOptionType == CheckBox) {
108 createControl(CheckBox);
109 setOptionType(CheckBox);
111 myCheckBoxWidget = theWidget; /// check box frame becomes filled
112 myCheckBoxLayout->addWidget(theWidget, 1);
115 createControl(GroupBox);
116 setOptionType(GroupBox);
118 ModuleBase_ModelWidget* aCheckBoxWidget = myCheckBoxWidget;
119 myCheckBoxWidget = 0;
120 if (aCheckBoxWidget) // move the model widget from check box frame to group box frame
121 placeModelWidget(aCheckBoxWidget);
124 const int kRow = myGroupBoxLayout->count();
125 // it seems, that the align on left is not necessary here, but leads to widgets, which are
126 // not extended on full width of the parent page. The case is grouped widgets in
127 // the sketch translation operation
128 myGroupBoxLayout->addWidget(theWidget, kRow, kCol, Qt::AlignTop);// | Qt::AlignLeft);
129 myGroupBoxLayout->setRowStretch(kRow, 0);
133 void ModuleBase_WidgetOptionalBox::placeWidget(QWidget* theWidget)
135 createControl(GroupBox);
136 setOptionType(GroupBox);
137 if (isCheckBoxFilled())
138 placeModelWidget(myCheckBoxWidget);
143 std::cout << "ModuleBase_PageGroupBox::placePageWidget: can not cast page" << std::endl;
147 if (myGroupBoxLayout) {
149 const int kRow = myGroupBoxLayout->count();
150 myGroupBoxLayout->addWidget(theWidget, kRow, kCol);
151 myGroupBoxLayout->setRowStretch(kRow, 0);
154 myCheckGroupLayout->addWidget(theWidget);
158 QLayout* ModuleBase_WidgetOptionalBox::pageLayout()
160 return myOptionType == GroupBox ? myGroupBoxLayout : (QLayout*)myCheckBoxLayout;
163 void ModuleBase_WidgetOptionalBox::createControl(const OptionType& theType)
165 if (theType == GroupBox && !myGroupBox) {
166 // group box: more than one model widget is inside
168 myGroupBox = new QGroupBox(this);
169 myGroupBox->setTitle(translate(myGroupTitle));
170 myGroupBox->setVisible(false);
171 myGroupBox->setCheckable(true);
172 myGroupBox->setChecked(getDefaultValue() == "true");
173 myGroupBox->setToolTip(translate(myToolTip));
175 myGroupBoxLayout = new QGridLayout(myGroupBox);
176 ModuleBase_Tools::zeroMargins(myGroupBoxLayout);
177 myGroupBox->setLayout(myGroupBoxLayout);
179 // default vertical size policy is preferred
180 myMainLayout->addWidget(myGroupBox);
181 connect(myGroupBox, SIGNAL(clicked(bool)), this, SLOT(onPageClicked()));
184 myCheckGroup = new QWidget(this);
185 QVBoxLayout* aLayout = new QVBoxLayout(myCheckGroup);
186 ModuleBase_Tools::zeroMargins(aLayout);
188 myCheckGroupBtn = new QCheckBox(translate(myGroupTitle), myCheckGroup);
189 aLayout->addWidget(myCheckGroupBtn);
191 myCheckContent = new QWidget(myCheckGroup);
192 myCheckGroupLayout = new QVBoxLayout(myCheckContent);
193 ModuleBase_Tools::zeroMargins(myCheckGroupLayout);
194 aLayout->addWidget(myCheckContent);
196 myMainLayout->addWidget(myCheckGroup);
197 connect(myCheckGroupBtn, SIGNAL(toggled(bool)), this, SLOT(onPageClicked()));
200 else if (theType == CheckBox && !myCheckBoxFrame) {
201 myCheckBoxFrame = new QFrame(this);
202 myMainLayout->addWidget(myCheckBoxFrame);
204 myCheckBoxLayout = new QHBoxLayout(myCheckBoxFrame);
205 ModuleBase_Tools::adjustMargins(myCheckBoxLayout);
206 if (myAlwaysShowTitle)
207 myCheckBox = new QCheckBox(translate(myGroupTitle), myCheckBoxFrame);
209 myCheckBox = new QCheckBox(myCheckBoxFrame);
210 myCheckBox->setChecked(getDefaultValue() == "true");
211 myCheckBoxLayout->addWidget(myCheckBox);
213 connect(myCheckBox, SIGNAL(clicked(bool)), this, SLOT(onPageClicked()));
217 bool ModuleBase_WidgetOptionalBox::storeValueCustom()
219 DataPtr aData = myFeature->data();
220 AttributeStringPtr aStringAttr = aData->string(attributeID());
221 if (aStringAttr.get())
222 aStringAttr->setValue(getCurrentValue() ? attributeID() : "");
224 AttributeBooleanPtr aBoolAtr = aData->boolean(attributeID());
225 aBoolAtr->setValue(getCurrentValue());
228 updateObject(myFeature);
233 bool ModuleBase_WidgetOptionalBox::restoreValueCustom()
235 DataPtr aData = myFeature->data();
236 AttributeStringPtr aStringAttr = aData->string(attributeID());
237 if (aStringAttr.get())
238 setCurrentValue(!aStringAttr->value().empty());
240 AttributeBooleanPtr aBoolAtr = aData->boolean(attributeID());
241 setCurrentValue(aBoolAtr->value());
246 void ModuleBase_WidgetOptionalBox::setOptionType(
247 const ModuleBase_WidgetOptionalBox::OptionType& theType)
249 myOptionType = theType;
251 bool isGroupBox = myOptionType == GroupBox;
253 myCheckBoxFrame->setVisible(!isGroupBox);
255 myGroupBox->setVisible(isGroupBox);
256 else if (myCheckContent)
257 myCheckGroup->setVisible(isGroupBox);
260 bool ModuleBase_WidgetOptionalBox::isCheckBoxFilled() const
262 return myCheckBoxWidget != 0;
265 bool ModuleBase_WidgetOptionalBox::getCurrentValue() const
267 bool isGroupBox = myOptionType == GroupBox;
268 return isGroupBox ? (myGroupBox? myGroupBox->isChecked() : myCheckGroupBtn->isChecked()) :
269 myCheckBox->isChecked();
272 void ModuleBase_WidgetOptionalBox::setCurrentValue(const bool& theValue)
274 bool isGroupBox = myOptionType == GroupBox;
277 bool isBlocked = myGroupBox->blockSignals(true);
278 myGroupBox->setChecked(theValue);
279 myGroupBox->blockSignals(isBlocked);
282 bool isBlocked = myCheckGroupBtn->blockSignals(true);
283 myCheckGroupBtn->setChecked(theValue);
284 myCheckGroupBtn->blockSignals(isBlocked);
288 bool isBlocked = myCheckBox->blockSignals(true);
289 myCheckBox->setChecked(theValue);
290 myCheckBox->blockSignals(isBlocked);
292 updateControlsVisibility();
295 void ModuleBase_WidgetOptionalBox::updateControlsVisibility()
297 if (myOptionType == GroupBox) {
298 bool aChecked = toEnableWidgets();
299 ModuleBase_Tools::adjustMargins(myGroupBoxLayout);
301 QLayout* aLayout = myGroupBoxLayout ? myGroupBoxLayout : (QLayout*)myCheckGroupLayout;
303 int aNbSubControls = aLayout->count();
304 for (int i = 0; i < aNbSubControls; i++) {
305 QWidget* aWidget = aLayout->itemAt(i)->widget();
307 aWidget->setEnabled(aChecked);
311 myCheckBoxWidget->setEnabled(toEnableWidgets());
315 bool ModuleBase_WidgetOptionalBox::toEnableWidgets() const
318 if (myOptionType == GroupBox)
319 aChecked = myGroupBox ? myGroupBox->isChecked() : myCheckGroupBtn->isChecked();
321 aChecked = myCheckBox->isChecked();