Salome HOME
6ca136d04e358cf52080a9e0937ed2fc76d8e3fa
[modules/shaper.git] / src / ModuleBase / ModuleBase_WidgetCheckGroupBox.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
2
3 // File:        ModuleBase_WidgetCheckGroupBox.cpp
4 // Created:     13 Dec 2015
5 // Author:      Natalia ERMOLAEVA
6
7 #include <ModuleBase_WidgetCheckGroupBox.h>
8 #include <ModelAPI_AttributeString.h>
9
10 #include <Config_WidgetAPI.h>
11 #include <Config_Keywords.h>
12
13 #include <QWidget>
14 #include <QGroupBox>
15 #include <QCheckBox>
16 #include <QGridLayout>
17 #include <QVBoxLayout>
18
19 #include <QList>
20
21 ModuleBase_WidgetCheckGroupBox::ModuleBase_WidgetCheckGroupBox(QWidget* theParent,
22                                                                const Config_WidgetAPI* theData)
23 : ModuleBase_ModelWidget(theParent, theData),
24   ModuleBase_PageBase(),
25   myOptionType(CheckBox),
26   myCheckBoxFrame(0),
27   myCheckBox(0),
28   myCheckBoxLayout(0),
29   myCheckBoxWidget(0),
30   myGroupBox(0),
31   myGroupBoxLayout(0)
32 {
33   myToolTip = theData->widgetTooltip();
34   myGroupTitle = theData->getProperty(CONTAINER_PAGE_NAME);
35
36   bool isChecked = theData->getBooleanAttribute(ATTR_DEFAULT, false);
37   setDefaultValue(isChecked ? "true" : "false");
38
39   myMainLayout = new QVBoxLayout(this);
40   ModuleBase_Tools::adjustMargins(myMainLayout);
41 }
42
43 ModuleBase_WidgetCheckGroupBox::~ModuleBase_WidgetCheckGroupBox()
44 {
45 }
46
47 QWidget* ModuleBase_WidgetCheckGroupBox::pageWidget()
48 {
49   return myOptionType == GroupBox ? myGroupBox : (QWidget*)myCheckBoxFrame;
50 }
51
52 QList<QWidget*> ModuleBase_WidgetCheckGroupBox::getControls() const
53 {
54   QList<QWidget*> aControls;
55   if (myOptionType == GroupBox)
56     aControls.append(myGroupBox);
57   else
58     aControls.append(myCheckBoxFrame);
59
60   return aControls;
61 }
62
63 void ModuleBase_WidgetCheckGroupBox::onPageClicked()
64 {
65   storeValue();
66   updateControlsVisibility();
67
68   if (!isEditingMode())
69     emit focusOutWidget(this);
70 }
71
72 void ModuleBase_WidgetCheckGroupBox::addPageStretch()
73 {
74 }
75
76 void ModuleBase_WidgetCheckGroupBox::placeModelWidget(ModuleBase_ModelWidget* theWidget)
77 {
78   if(!isCheckBoxFilled() && myOptionType == CheckBox) {
79     createControl(CheckBox);
80     setOptionType(CheckBox);
81
82     myCheckBoxWidget = theWidget; /// check box frame becomes filled
83     myCheckBoxLayout->addWidget(theWidget);
84   }
85   else {
86     createControl(GroupBox);
87     setOptionType(GroupBox);
88
89     ModuleBase_ModelWidget* aCheckBoxWidget = myCheckBoxWidget;
90     myCheckBoxWidget = 0;
91     if (aCheckBoxWidget) // move the model widget from check box frame to group box frame
92       placeModelWidget(aCheckBoxWidget);
93
94     const int kCol = 0;
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);
101   }
102 }
103
104 void ModuleBase_WidgetCheckGroupBox::placeWidget(QWidget* theWidget)
105 {
106   createControl(GroupBox);
107   setOptionType(GroupBox);
108   if (isCheckBoxFilled())
109     placeModelWidget(myCheckBoxWidget);
110
111
112   if (!theWidget) {
113 #ifdef _DEBUG
114     std::cout << "ModuleBase_PageGroupBox::placePageWidget: can not cast page" << std::endl;
115 #endif
116     return;
117   }
118   const int kCol = 0;
119   const int kRow = myGroupBoxLayout->count();
120   myGroupBoxLayout->addWidget(theWidget, kRow, kCol);
121   myGroupBoxLayout->setRowStretch(kRow, 0);
122 }
123
124 QLayout* ModuleBase_WidgetCheckGroupBox::pageLayout()
125 {
126   return myOptionType == GroupBox ? myGroupBoxLayout : (QLayout*)myCheckBoxLayout;
127 }
128
129 void ModuleBase_WidgetCheckGroupBox::createControl(const OptionType& theType)
130 {
131   if (theType == GroupBox && !myGroupBox) {
132     // group box: more than one model widget is inside
133     myGroupBox = new QGroupBox(this);
134     myGroupBox->setTitle(QString::fromStdString(myGroupTitle));
135     myGroupBox->setVisible(false);
136     myGroupBox->setCheckable(true);
137     myGroupBox->setChecked(getDefaultValue() == "true");
138     myGroupBox->setToolTip(QString::fromStdString(myToolTip));
139
140     myGroupBoxLayout = new QGridLayout(myGroupBox);
141     ModuleBase_Tools::zeroMargins(myGroupBoxLayout);
142     myGroupBox->setLayout(myGroupBoxLayout);
143
144     // default vertical size policy is preferred
145     myMainLayout->addWidget(myGroupBox);
146     connect(myGroupBox, SIGNAL(clicked(bool)), this, SLOT(onPageClicked()));
147   }
148   else if (theType == CheckBox && !myCheckBoxFrame) {
149     myCheckBoxFrame = new QFrame(this);
150     myMainLayout->addWidget(myCheckBoxFrame);
151
152     myCheckBoxLayout = new QHBoxLayout(myCheckBoxFrame);
153     ModuleBase_Tools::adjustMargins(myCheckBoxLayout);
154     myCheckBox = new QCheckBox(myCheckBoxFrame);
155     myCheckBox->setChecked(getDefaultValue() == "true");
156     myCheckBoxLayout->addWidget(myCheckBox);
157
158     connect(myCheckBox, SIGNAL(clicked(bool)), this, SLOT(onPageClicked()));
159   }
160 }
161
162 bool ModuleBase_WidgetCheckGroupBox::storeValueCustom()
163 {
164   DataPtr aData = myFeature->data();
165   AttributeStringPtr aStringAttr = aData->string(attributeID());
166   aStringAttr->setValue(getCurrentValue() ? attributeID() : "");
167
168   updateObject(myFeature);
169
170   return true;
171 }
172
173 bool ModuleBase_WidgetCheckGroupBox::restoreValueCustom()
174 {
175   DataPtr aData = myFeature->data();
176   AttributeStringPtr aStringAttr = aData->string(attributeID());
177   setCurrentValue(!aStringAttr->value().empty());
178
179   return true;
180 }
181
182 void ModuleBase_WidgetCheckGroupBox::setOptionType(
183                          const ModuleBase_WidgetCheckGroupBox::OptionType& theType)
184 {
185   myOptionType = theType;
186
187   bool isGroupBox = myOptionType == GroupBox;
188   if (myCheckBoxFrame)
189     myCheckBoxFrame->setVisible(!isGroupBox);
190   if (myGroupBox)
191     myGroupBox->setVisible(isGroupBox);
192 }
193
194 bool ModuleBase_WidgetCheckGroupBox::isCheckBoxFilled() const
195 {
196   return myCheckBoxWidget != 0;
197 }
198
199 bool ModuleBase_WidgetCheckGroupBox::getCurrentValue() const
200 {
201   bool isGroupBox = myOptionType == GroupBox;
202   return isGroupBox ? myGroupBox->isChecked() : myCheckBox->isChecked();
203 }
204
205 void ModuleBase_WidgetCheckGroupBox::setCurrentValue(const bool& theValue)
206 {
207   bool isGroupBox = myOptionType == GroupBox;
208   if (isGroupBox) {
209     bool isBlocked = myGroupBox->blockSignals(true);
210     myGroupBox->setChecked(theValue);
211     myGroupBox->blockSignals(isBlocked);
212   }
213   else {
214     bool isBlocked = myCheckBox->blockSignals(true);
215     myCheckBox->setChecked(theValue);
216     myCheckBox->blockSignals(isBlocked);
217   }
218   updateControlsVisibility();
219 }
220
221 void ModuleBase_WidgetCheckGroupBox::updateControlsVisibility()
222 {
223   if (myOptionType == GroupBox) {
224     bool aChecked = myGroupBox->isChecked();
225     if (aChecked)
226       ModuleBase_Tools::zeroMargins(myGroupBoxLayout);
227     else
228       ModuleBase_Tools::adjustMargins(myGroupBoxLayout);
229
230     int aNbSubControls = myGroupBoxLayout->count();
231     for (int i = 0; i < aNbSubControls; i++) {
232       QWidget* aWidget = myGroupBoxLayout->itemAt(i)->widget();
233       if (aWidget)
234         aWidget->setVisible(aChecked);
235     }
236   }
237   else {
238     bool aChecked = myCheckBox->isChecked();
239     myCheckBoxWidget->setEnabled(aChecked);
240   }
241 }