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