1 // Copyright (C) 2014-2017 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<mailto:webmaster.salome@opencascade.com>
20 #include <ModuleBase_WidgetOptionalBox.h>
21 #include <ModelAPI_AttributeString.h>
23 #include <Config_WidgetAPI.h>
24 #include <Config_Keywords.h>
29 #include <QGridLayout>
30 #include <QVBoxLayout>
34 ModuleBase_WidgetOptionalBox::ModuleBase_WidgetOptionalBox(QWidget* theParent,
35 const Config_WidgetAPI* theData)
36 : ModuleBase_ModelWidget(theParent, theData),
37 ModuleBase_PageBase(),
38 myOptionType(CheckBox),
46 myToolTip = theData->widgetTooltip();
47 myGroupTitle = theData->getProperty(CONTAINER_PAGE_NAME);
49 bool isChecked = theData->getBooleanAttribute(ATTR_DEFAULT, false);
50 setDefaultValue(isChecked ? "true" : "false");
52 myMainLayout = new QVBoxLayout(this);
53 ModuleBase_Tools::adjustMargins(myMainLayout);
56 ModuleBase_WidgetOptionalBox::~ModuleBase_WidgetOptionalBox()
60 QWidget* ModuleBase_WidgetOptionalBox::pageWidget()
62 return myOptionType == GroupBox ? myGroupBox : (QWidget*)myCheckBoxFrame;
65 QList<QWidget*> ModuleBase_WidgetOptionalBox::getControls() const
67 QList<QWidget*> aControls;
68 if (myOptionType == GroupBox)
69 aControls.append(myGroupBox);
71 aControls.append(myCheckBoxFrame);
76 void ModuleBase_WidgetOptionalBox::onPageClicked()
79 updateControlsVisibility();
82 emit focusOutWidget(this);
85 void ModuleBase_WidgetOptionalBox::addPageStretch()
89 void ModuleBase_WidgetOptionalBox::placeModelWidget(ModuleBase_ModelWidget* theWidget)
91 if(!isCheckBoxFilled() && myOptionType == CheckBox) {
92 createControl(CheckBox);
93 setOptionType(CheckBox);
95 myCheckBoxWidget = theWidget; /// check box frame becomes filled
96 myCheckBoxLayout->addWidget(theWidget);
99 createControl(GroupBox);
100 setOptionType(GroupBox);
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);
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);
117 void ModuleBase_WidgetOptionalBox::placeWidget(QWidget* theWidget)
119 createControl(GroupBox);
120 setOptionType(GroupBox);
121 if (isCheckBoxFilled())
122 placeModelWidget(myCheckBoxWidget);
127 std::cout << "ModuleBase_PageGroupBox::placePageWidget: can not cast page" << std::endl;
132 const int kRow = myGroupBoxLayout->count();
133 myGroupBoxLayout->addWidget(theWidget, kRow, kCol);
134 myGroupBoxLayout->setRowStretch(kRow, 0);
137 QLayout* ModuleBase_WidgetOptionalBox::pageLayout()
139 return myOptionType == GroupBox ? myGroupBoxLayout : (QLayout*)myCheckBoxLayout;
142 void ModuleBase_WidgetOptionalBox::createControl(const OptionType& theType)
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));
153 myGroupBoxLayout = new QGridLayout(myGroupBox);
154 ModuleBase_Tools::zeroMargins(myGroupBoxLayout);
155 myGroupBox->setLayout(myGroupBoxLayout);
157 // default vertical size policy is preferred
158 myMainLayout->addWidget(myGroupBox);
159 connect(myGroupBox, SIGNAL(clicked(bool)), this, SLOT(onPageClicked()));
161 else if (theType == CheckBox && !myCheckBoxFrame) {
162 myCheckBoxFrame = new QFrame(this);
163 myMainLayout->addWidget(myCheckBoxFrame);
165 myCheckBoxLayout = new QHBoxLayout(myCheckBoxFrame);
166 ModuleBase_Tools::adjustMargins(myCheckBoxLayout);
167 myCheckBox = new QCheckBox(myCheckBoxFrame);
168 myCheckBox->setChecked(getDefaultValue() == "true");
169 myCheckBoxLayout->addWidget(myCheckBox);
171 connect(myCheckBox, SIGNAL(clicked(bool)), this, SLOT(onPageClicked()));
175 bool ModuleBase_WidgetOptionalBox::storeValueCustom()
177 DataPtr aData = myFeature->data();
178 AttributeStringPtr aStringAttr = aData->string(attributeID());
179 aStringAttr->setValue(getCurrentValue() ? attributeID() : "");
181 updateObject(myFeature);
186 bool ModuleBase_WidgetOptionalBox::restoreValueCustom()
188 DataPtr aData = myFeature->data();
189 AttributeStringPtr aStringAttr = aData->string(attributeID());
190 setCurrentValue(!aStringAttr->value().empty());
195 void ModuleBase_WidgetOptionalBox::setOptionType(
196 const ModuleBase_WidgetOptionalBox::OptionType& theType)
198 myOptionType = theType;
200 bool isGroupBox = myOptionType == GroupBox;
202 myCheckBoxFrame->setVisible(!isGroupBox);
204 myGroupBox->setVisible(isGroupBox);
207 bool ModuleBase_WidgetOptionalBox::isCheckBoxFilled() const
209 return myCheckBoxWidget != 0;
212 bool ModuleBase_WidgetOptionalBox::getCurrentValue() const
214 bool isGroupBox = myOptionType == GroupBox;
215 return isGroupBox ? myGroupBox->isChecked() : myCheckBox->isChecked();
218 void ModuleBase_WidgetOptionalBox::setCurrentValue(const bool& theValue)
220 bool isGroupBox = myOptionType == GroupBox;
222 bool isBlocked = myGroupBox->blockSignals(true);
223 myGroupBox->setChecked(theValue);
224 myGroupBox->blockSignals(isBlocked);
227 bool isBlocked = myCheckBox->blockSignals(true);
228 myCheckBox->setChecked(theValue);
229 myCheckBox->blockSignals(isBlocked);
231 updateControlsVisibility();
234 void ModuleBase_WidgetOptionalBox::updateControlsVisibility()
236 if (myOptionType == GroupBox) {
237 bool aChecked = myGroupBox->isChecked();
238 ModuleBase_Tools::adjustMargins(myGroupBoxLayout);
240 int aNbSubControls = myGroupBoxLayout->count();
241 for (int i = 0; i < aNbSubControls; i++) {
242 QWidget* aWidget = myGroupBoxLayout->itemAt(i)->widget();
244 aWidget->setEnabled(aChecked);
248 bool aChecked = myCheckBox->isChecked();
249 myCheckBoxWidget->setEnabled(aChecked);