Salome HOME
db0a7920a2da17cc40b2e8ca59b68f131002478b
[modules/shaper.git] / src / ModuleBase / ModuleBase_ChoiceCtrl.cpp
1 // Copyright (C) 2014-2023  CEA, EDF
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "ModuleBase_ChoiceCtrl.h"
21 #include "ModuleBase_Tools.h"
22 #include "ModuleBase_IconFactory.h"
23
24 #include <QLayout>
25 #include <QLabel>
26 #include <QComboBox>
27 #include <QGroupBox>
28 #include <QButtonGroup>
29 #include <QRadioButton>
30 #include <QToolButton>
31
32 const QString AStyle = "QToolButton:checked {border: 1px solid black; background-color:#C0DCF3}";
33
34
35 ModuleBase_ChoiceCtrl::ModuleBase_ChoiceCtrl(QWidget* theParent,
36                                              const QStringList& theChoiceList,
37                                              const QStringList& theIconsList,
38                                              ControlType theType,
39                                              Qt::Orientation theButtonsDir)
40  : QWidget(theParent), myType(theType)
41 {
42   QHBoxLayout* aLayout = new QHBoxLayout(this);
43   aLayout->setContentsMargins(0, 0, 0, 0);
44
45   switch (myType) {
46   case RadioButtons:
47     {
48       myButtons = new QButtonGroup(this);
49       myGroupBox = new QGroupBox("", this);
50       aLayout->addWidget(myGroupBox);
51
52       QLayout* aBtnLayout = 0;
53       switch (theButtonsDir) {
54       case Qt::Horizontal:
55         aBtnLayout = new QHBoxLayout(myGroupBox);
56         break;
57       case Qt::Vertical:
58         aBtnLayout = new QVBoxLayout(myGroupBox);
59         break;
60       }
61       ModuleBase_Tools::adjustMargins(aBtnLayout);
62
63       if (theIconsList.length() == theChoiceList.length()) {
64         int aId = 0;
65         foreach(QString aBtnTxt, theChoiceList) {
66           QToolButton* aBtn = new QToolButton(myGroupBox);
67           aBtn->setFocusPolicy(Qt::StrongFocus);
68           aBtn->setCheckable(true);
69           aBtn->setToolTip(aBtnTxt);
70
71           QPixmap aIcon = ModuleBase_IconFactory::loadPixmap(theIconsList.at(aId));
72           aBtn->setIcon(aIcon);
73           aBtn->setIconSize(aIcon.size());
74           aBtn->setStyleSheet(AStyle);
75
76           aBtnLayout->addWidget(aBtn);
77           myButtons->addButton(aBtn, aId++);
78         }
79       } else {
80         int aId = 0;
81         foreach(QString aBtnTxt, theChoiceList) {
82           QRadioButton* aBtn = new QRadioButton(aBtnTxt, myGroupBox);
83           aBtnLayout->addWidget(aBtn);
84           myButtons->addButton(aBtn, aId++);
85         }
86       }
87       connect(myButtons, SIGNAL(buttonClicked(int)), this, SIGNAL(valueChanged(int)));
88     }
89     break;
90   case ComboBox:
91     myLabel = new QLabel("", this);
92     aLayout->addWidget(myLabel);
93
94     //std::string aToolstr = theData->widgetTooltip();
95     //if (!aToolstr.empty()) {
96     //  myLabel->setToolTip(QString::fromStdString(aToolstr));
97     //}
98
99     myCombo = new QComboBox(this);
100     aLayout->addWidget(myCombo, 1);
101
102     myCombo->addItems(theChoiceList);
103     connect(myCombo, SIGNAL(currentIndexChanged(int)), this, SIGNAL(valueChanged(int)));
104     break;
105   }
106 }
107
108 void ModuleBase_ChoiceCtrl::setLabel(const QString& theText)
109 {
110   switch (myType) {
111   case RadioButtons:
112     myGroupBox->setTitle(theText);
113     break;
114   case ComboBox:
115     myLabel->setText(theText);
116     break;
117   }
118 }
119
120 void ModuleBase_ChoiceCtrl::setLabelIcon(const QString& theIcon)
121 {
122   if (myType == ComboBox)
123     myLabel->setPixmap(ModuleBase_IconFactory::loadPixmap(theIcon));
124 }
125
126 void ModuleBase_ChoiceCtrl::setValue(int theVal)
127 {
128   switch (myType) {
129   case RadioButtons:
130     myButtons->button(theVal)->setChecked(true);
131     break;
132   case ComboBox:
133     myCombo->setCurrentIndex(theVal);
134     break;
135   }
136 }
137
138 void ModuleBase_ChoiceCtrl::setValue(const QString& theVal)
139 {
140   switch (myType) {
141   case RadioButtons:
142     foreach (QAbstractButton* aBtn, myButtons->buttons()) {
143       aBtn->setChecked(aBtn->toolTip() == theVal);
144     }
145     break;
146   case ComboBox:
147     myCombo->setCurrentText(theVal);
148     break;
149   }
150 }
151
152
153 void ModuleBase_ChoiceCtrl::setTooltip(QString theTip)
154 {
155   if (myType == ComboBox)
156     myLabel->setToolTip(theTip);
157 }
158
159 int ModuleBase_ChoiceCtrl::value() const
160 {
161   switch (myType) {
162   case RadioButtons:
163     return myButtons->checkedId();
164   case ComboBox:
165     return myCombo->currentIndex();
166   }
167   return -1;
168 }
169
170 bool ModuleBase_ChoiceCtrl::focusTo()
171 {
172   if (myType == ComboBox)
173     ModuleBase_Tools::setFocus(myCombo, "ModuleBase_WidgetChoice::focusTo()");
174   else
175     return false;
176   return true;
177 }
178
179 QList<QWidget*> ModuleBase_ChoiceCtrl::getControls() const
180 {
181   QList<QWidget*> aControls;
182   if (myType == ComboBox)
183     aControls.append(myCombo);
184   return aControls;
185 }
186
187 void ModuleBase_ChoiceCtrl::setChoiceList(const QStringList& theChoiceList)
188 {
189   if (myType == ComboBox) {
190     myCombo->clear();
191     myCombo->addItems(theChoiceList);
192   }
193 }
194
195 QString ModuleBase_ChoiceCtrl::textValue() const
196 {
197   switch (myType) {
198   case RadioButtons:
199     return myButtons->checkedButton()->toolTip();
200   case ComboBox:
201     return myCombo->currentText();
202   }
203   return "";
204 }