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