1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
3 // File: ModuleBase_WidgetCheckGroupBox.cpp
4 // Created: 13 Dec 2015
5 // Author: Natalia ERMOLAEVA
7 #include <ModuleBase_WidgetCheckGroupBox.h>
8 #include <ModelAPI_AttributeString.h>
10 #include <Config_WidgetAPI.h>
11 #include <Config_Keywords.h>
16 #include <QGridLayout>
17 #include <QVBoxLayout>
21 ModuleBase_WidgetCheckGroupBox::ModuleBase_WidgetCheckGroupBox(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_WidgetCheckGroupBox::~ModuleBase_WidgetCheckGroupBox()
47 QWidget* ModuleBase_WidgetCheckGroupBox::pageWidget()
49 return myOptionType == GroupBox ? myGroupBox : (QWidget*)myCheckBoxFrame;
52 QList<QWidget*> ModuleBase_WidgetCheckGroupBox::getControls() const
54 QList<QWidget*> aControls;
55 if (myOptionType == GroupBox)
56 aControls.append(myGroupBox);
58 aControls.append(myCheckBoxFrame);
63 void ModuleBase_WidgetCheckGroupBox::onPageClicked()
66 updateControlsVisibility();
69 void ModuleBase_WidgetCheckGroupBox::addPageStretch()
73 void ModuleBase_WidgetCheckGroupBox::placeModelWidget(ModuleBase_ModelWidget* theWidget)
75 if(!isCheckBoxFilled() && myOptionType == CheckBox) {
76 createControl(CheckBox);
77 setOptionType(CheckBox);
79 myCheckBoxWidget = theWidget; /// check box frame becomes filled
80 myCheckBoxLayout->addWidget(theWidget);
83 createControl(GroupBox);
84 setOptionType(GroupBox);
86 ModuleBase_ModelWidget* aCheckBoxWidget = myCheckBoxWidget;
88 if (aCheckBoxWidget) // move the model widget from check box frame to group box frame
89 placeModelWidget(aCheckBoxWidget);
92 const int kRow = myGroupBoxLayout->count();
93 // it seems, that the align on left is not necessary here, but leads to widgets, which are
94 // not extended on full width of the parent page. The case is grouped widgets in
95 // the sketch translation operation
96 myGroupBoxLayout->addWidget(theWidget, kRow, kCol, Qt::AlignTop);// | Qt::AlignLeft);
97 myGroupBoxLayout->setRowStretch(kRow, 0);
101 void ModuleBase_WidgetCheckGroupBox::placeWidget(QWidget* theWidget)
103 createControl(GroupBox);
104 setOptionType(GroupBox);
105 if (isCheckBoxFilled())
106 placeModelWidget(myCheckBoxWidget);
111 std::cout << "ModuleBase_PageGroupBox::placePageWidget: can not cast page" << std::endl;
116 const int kRow = myGroupBoxLayout->count();
117 myGroupBoxLayout->addWidget(theWidget, kRow, kCol);
118 myGroupBoxLayout->setRowStretch(kRow, 0);
121 QLayout* ModuleBase_WidgetCheckGroupBox::pageLayout()
123 return myOptionType == GroupBox ? myGroupBoxLayout : (QLayout*)myCheckBoxLayout;
126 void ModuleBase_WidgetCheckGroupBox::createControl(const OptionType& theType)
128 if (theType == GroupBox && !myGroupBox) {
129 // group box: more than one model widget is inside
130 myGroupBox = new QGroupBox(this);
131 myGroupBox->setTitle(QString::fromStdString(myGroupTitle));
132 myGroupBox->setVisible(false);
133 myGroupBox->setCheckable(true);
134 myGroupBox->setChecked(getDefaultValue() == "true");
135 myGroupBox->setToolTip(QString::fromStdString(myToolTip));
137 myGroupBoxLayout = new QGridLayout(myGroupBox);
138 ModuleBase_Tools::zeroMargins(myGroupBoxLayout);
139 myGroupBox->setLayout(myGroupBoxLayout);
141 // default vertical size policy is preferred
142 myMainLayout->addWidget(myGroupBox);
143 connect(myGroupBox, SIGNAL(clicked(bool)), this, SLOT(onPageClicked()));
145 else if (theType == CheckBox && !myCheckBoxFrame) {
146 myCheckBoxFrame = new QFrame(this);
147 myMainLayout->addWidget(myCheckBoxFrame);
149 myCheckBoxLayout = new QHBoxLayout(myCheckBoxFrame);
150 ModuleBase_Tools::adjustMargins(myCheckBoxLayout);
151 myCheckBox = new QCheckBox(myCheckBoxFrame);
152 myCheckBox->setChecked(getDefaultValue() == "true");
153 myCheckBoxLayout->addWidget(myCheckBox);
155 connect(myCheckBox, SIGNAL(clicked(bool)), this, SLOT(onPageClicked()));
159 bool ModuleBase_WidgetCheckGroupBox::storeValueCustom()
161 DataPtr aData = myFeature->data();
162 AttributeStringPtr aStringAttr = aData->string(attributeID());
163 aStringAttr->setValue(getCurrentValue() ? attributeID() : "");
165 updateObject(myFeature);
170 bool ModuleBase_WidgetCheckGroupBox::restoreValueCustom()
172 DataPtr aData = myFeature->data();
173 AttributeStringPtr aStringAttr = aData->string(attributeID());
174 setCurrentValue(!aStringAttr->value().empty());
179 void ModuleBase_WidgetCheckGroupBox::setOptionType(
180 const ModuleBase_WidgetCheckGroupBox::OptionType& theType)
182 myOptionType = theType;
184 bool isGroupBox = myOptionType == GroupBox;
186 myCheckBoxFrame->setVisible(!isGroupBox);
188 myGroupBox->setVisible(isGroupBox);
191 bool ModuleBase_WidgetCheckGroupBox::isCheckBoxFilled() const
193 return myCheckBoxWidget != 0;
196 bool ModuleBase_WidgetCheckGroupBox::getCurrentValue() const
198 bool isGroupBox = myOptionType == GroupBox;
199 return isGroupBox ? myGroupBox->isChecked() : myCheckBox->isChecked();
202 void ModuleBase_WidgetCheckGroupBox::setCurrentValue(const bool& theValue)
204 bool isGroupBox = myOptionType == GroupBox;
206 bool isBlocked = myGroupBox->blockSignals(true);
207 myGroupBox->setChecked(theValue);
208 myGroupBox->blockSignals(isBlocked);
211 bool isBlocked = myCheckBox->blockSignals(true);
212 myCheckBox->setChecked(theValue);
213 myCheckBox->blockSignals(isBlocked);
215 updateControlsVisibility();
218 void ModuleBase_WidgetCheckGroupBox::updateControlsVisibility()
220 if (myOptionType == GroupBox) {
221 bool aChecked = myGroupBox->isChecked();
223 ModuleBase_Tools::zeroMargins(myGroupBoxLayout);
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->setVisible(aChecked);
235 bool aChecked = myCheckBox->isChecked();
236 myCheckBoxWidget->setEnabled(aChecked);