1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
3 // File: ModuleBase_WidgetOptionalBox.cpp
4 // Created: 13 Dec 2015
5 // Author: Natalia ERMOLAEVA
7 #include <ModuleBase_WidgetOptionalBox.h>
8 #include <ModelAPI_AttributeString.h>
10 #include <Config_WidgetAPI.h>
11 #include <Config_Keywords.h>
16 #include <QGridLayout>
17 #include <QVBoxLayout>
21 ModuleBase_WidgetOptionalBox::ModuleBase_WidgetOptionalBox(QWidget* theParent,
22 const Config_WidgetAPI* theData)
23 : ModuleBase_ModelWidget(theParent, theData),
24 ModuleBase_PageBase(),
25 myOptionType(CheckBox),
33 myToolTip = theData->widgetTooltip();
34 myGroupTitle = theData->getProperty(CONTAINER_PAGE_NAME);
36 bool isChecked = theData->getBooleanAttribute(ATTR_DEFAULT, false);
37 setDefaultValue(isChecked ? "true" : "false");
39 myMainLayout = new QVBoxLayout(this);
40 ModuleBase_Tools::adjustMargins(myMainLayout);
43 ModuleBase_WidgetOptionalBox::~ModuleBase_WidgetOptionalBox()
47 QWidget* ModuleBase_WidgetOptionalBox::pageWidget()
49 return myOptionType == GroupBox ? myGroupBox : (QWidget*)myCheckBoxFrame;
52 QList<QWidget*> ModuleBase_WidgetOptionalBox::getControls() const
54 QList<QWidget*> aControls;
55 if (myOptionType == GroupBox)
56 aControls.append(myGroupBox);
58 aControls.append(myCheckBoxFrame);
63 void ModuleBase_WidgetOptionalBox::onPageClicked()
66 updateControlsVisibility();
69 emit focusOutWidget(this);
72 void ModuleBase_WidgetOptionalBox::addPageStretch()
76 void ModuleBase_WidgetOptionalBox::placeModelWidget(ModuleBase_ModelWidget* theWidget)
78 if(!isCheckBoxFilled() && myOptionType == CheckBox) {
79 createControl(CheckBox);
80 setOptionType(CheckBox);
82 myCheckBoxWidget = theWidget; /// check box frame becomes filled
83 myCheckBoxLayout->addWidget(theWidget);
86 createControl(GroupBox);
87 setOptionType(GroupBox);
89 ModuleBase_ModelWidget* aCheckBoxWidget = myCheckBoxWidget;
91 if (aCheckBoxWidget) // move the model widget from check box frame to group box frame
92 placeModelWidget(aCheckBoxWidget);
95 const int kRow = myGroupBoxLayout->count();
96 // it seems, that the align on left is not necessary here, but leads to widgets, which are
97 // not extended on full width of the parent page. The case is grouped widgets in
98 // the sketch translation operation
99 myGroupBoxLayout->addWidget(theWidget, kRow, kCol, Qt::AlignTop);// | Qt::AlignLeft);
100 myGroupBoxLayout->setRowStretch(kRow, 0);
104 void ModuleBase_WidgetOptionalBox::placeWidget(QWidget* theWidget)
106 createControl(GroupBox);
107 setOptionType(GroupBox);
108 if (isCheckBoxFilled())
109 placeModelWidget(myCheckBoxWidget);
114 std::cout << "ModuleBase_PageGroupBox::placePageWidget: can not cast page" << std::endl;
119 const int kRow = myGroupBoxLayout->count();
120 myGroupBoxLayout->addWidget(theWidget, kRow, kCol);
121 myGroupBoxLayout->setRowStretch(kRow, 0);
124 QLayout* ModuleBase_WidgetOptionalBox::pageLayout()
126 return myOptionType == GroupBox ? myGroupBoxLayout : (QLayout*)myCheckBoxLayout;
129 void ModuleBase_WidgetOptionalBox::createControl(const OptionType& theType)
131 if (theType == GroupBox && !myGroupBox) {
132 // group box: more than one model widget is inside
133 myGroupBox = new QGroupBox(this);
134 myGroupBox->setTitle(translate(myGroupTitle));
135 myGroupBox->setVisible(false);
136 myGroupBox->setCheckable(true);
137 myGroupBox->setChecked(getDefaultValue() == "true");
138 myGroupBox->setToolTip(translate(myToolTip));
140 myGroupBoxLayout = new QGridLayout(myGroupBox);
141 ModuleBase_Tools::zeroMargins(myGroupBoxLayout);
142 myGroupBox->setLayout(myGroupBoxLayout);
144 // default vertical size policy is preferred
145 myMainLayout->addWidget(myGroupBox);
146 connect(myGroupBox, SIGNAL(clicked(bool)), this, SLOT(onPageClicked()));
148 else if (theType == CheckBox && !myCheckBoxFrame) {
149 myCheckBoxFrame = new QFrame(this);
150 myMainLayout->addWidget(myCheckBoxFrame);
152 myCheckBoxLayout = new QHBoxLayout(myCheckBoxFrame);
153 ModuleBase_Tools::adjustMargins(myCheckBoxLayout);
154 myCheckBox = new QCheckBox(myCheckBoxFrame);
155 myCheckBox->setChecked(getDefaultValue() == "true");
156 myCheckBoxLayout->addWidget(myCheckBox);
158 connect(myCheckBox, SIGNAL(clicked(bool)), this, SLOT(onPageClicked()));
162 bool ModuleBase_WidgetOptionalBox::storeValueCustom()
164 DataPtr aData = myFeature->data();
165 AttributeStringPtr aStringAttr = aData->string(attributeID());
166 aStringAttr->setValue(getCurrentValue() ? attributeID() : "");
168 updateObject(myFeature);
173 bool ModuleBase_WidgetOptionalBox::restoreValueCustom()
175 DataPtr aData = myFeature->data();
176 AttributeStringPtr aStringAttr = aData->string(attributeID());
177 setCurrentValue(!aStringAttr->value().empty());
182 void ModuleBase_WidgetOptionalBox::setOptionType(
183 const ModuleBase_WidgetOptionalBox::OptionType& theType)
185 myOptionType = theType;
187 bool isGroupBox = myOptionType == GroupBox;
189 myCheckBoxFrame->setVisible(!isGroupBox);
191 myGroupBox->setVisible(isGroupBox);
194 bool ModuleBase_WidgetOptionalBox::isCheckBoxFilled() const
196 return myCheckBoxWidget != 0;
199 bool ModuleBase_WidgetOptionalBox::getCurrentValue() const
201 bool isGroupBox = myOptionType == GroupBox;
202 return isGroupBox ? myGroupBox->isChecked() : myCheckBox->isChecked();
205 void ModuleBase_WidgetOptionalBox::setCurrentValue(const bool& theValue)
207 bool isGroupBox = myOptionType == GroupBox;
209 bool isBlocked = myGroupBox->blockSignals(true);
210 myGroupBox->setChecked(theValue);
211 myGroupBox->blockSignals(isBlocked);
214 bool isBlocked = myCheckBox->blockSignals(true);
215 myCheckBox->setChecked(theValue);
216 myCheckBox->blockSignals(isBlocked);
218 updateControlsVisibility();
221 void ModuleBase_WidgetOptionalBox::updateControlsVisibility()
223 if (myOptionType == GroupBox) {
224 bool aChecked = myGroupBox->isChecked();
225 ModuleBase_Tools::adjustMargins(myGroupBoxLayout);
227 int aNbSubControls = myGroupBoxLayout->count();
228 for (int i = 0; i < aNbSubControls; i++) {
229 QWidget* aWidget = myGroupBoxLayout->itemAt(i)->widget();
231 aWidget->setEnabled(aChecked);
235 bool aChecked = myCheckBox->isChecked();
236 myCheckBoxWidget->setEnabled(aChecked);