1 // Copyright (C) 2014-2019 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "ModuleBase_ChoiceCtrl.h"
21 #include "ModuleBase_Tools.h"
22 #include "ModuleBase_IconFactory.h"
28 #include <QButtonGroup>
29 #include <QRadioButton>
30 #include <QToolButton>
32 const QString AStyle = "QToolButton:checked {border: 1px solid black; background-color:#C0DCF3}";
35 ModuleBase_ChoiceCtrl::ModuleBase_ChoiceCtrl(QWidget* theParent,
36 const QStringList& theChoiceList,
37 const QStringList& theIconsList,
39 Qt::Orientation theButtonsDir)
40 : QWidget(theParent), myType(theType)
42 QHBoxLayout* aLayout = new QHBoxLayout(this);
43 aLayout->setContentsMargins(0, 0, 0, 0);
48 myButtons = new QButtonGroup(this);
49 myGroupBox = new QGroupBox("", this);
50 aLayout->addWidget(myGroupBox);
52 QLayout* aBtnLayout = 0;
53 switch (theButtonsDir) {
55 aBtnLayout = new QHBoxLayout(myGroupBox);
58 aBtnLayout = new QVBoxLayout(myGroupBox);
61 ModuleBase_Tools::adjustMargins(aBtnLayout);
63 if (theIconsList.length() == theChoiceList.length()) {
65 foreach(QString aBtnTxt, theChoiceList) {
66 QToolButton* aBtn = new QToolButton(myGroupBox);
67 aBtn->setFocusPolicy(Qt::StrongFocus);
68 aBtn->setCheckable(true);
69 aBtn->setToolTip(aBtnTxt);
71 QPixmap aIcon = ModuleBase_IconFactory::loadPixmap(theIconsList.at(aId));
73 aBtn->setIconSize(aIcon.size());
74 aBtn->setStyleSheet(AStyle);
76 aBtnLayout->addWidget(aBtn);
77 myButtons->addButton(aBtn, aId++);
81 foreach(QString aBtnTxt, theChoiceList) {
82 QRadioButton* aBtn = new QRadioButton(aBtnTxt, myGroupBox);
83 aBtnLayout->addWidget(aBtn);
84 myButtons->addButton(aBtn, aId++);
87 connect(myButtons, SIGNAL(buttonClicked(int)), this, SIGNAL(valueChanged(int)));
91 myLabel = new QLabel("", this);
92 aLayout->addWidget(myLabel);
94 //std::string aToolstr = theData->widgetTooltip();
95 //if (!aToolstr.empty()) {
96 // myLabel->setToolTip(QString::fromStdString(aToolstr));
99 myCombo = new QComboBox(this);
100 aLayout->addWidget(myCombo, 1);
102 myCombo->addItems(theChoiceList);
103 connect(myCombo, SIGNAL(currentIndexChanged(int)), this, SIGNAL(valueChanged(int)));
108 void ModuleBase_ChoiceCtrl::setLabel(const QString& theText)
112 myGroupBox->setTitle(theText);
115 myLabel->setText(theText);
120 void ModuleBase_ChoiceCtrl::setLabelIcon(const QString& theIcon)
122 if (myType == ComboBox)
123 myLabel->setPixmap(ModuleBase_IconFactory::loadPixmap(theIcon));
126 void ModuleBase_ChoiceCtrl::setValue(int theVal)
130 myButtons->button(theVal)->setChecked(true);
133 myCombo->setCurrentIndex(theVal);
138 void ModuleBase_ChoiceCtrl::setValue(const QString& theVal)
142 foreach (QAbstractButton* aBtn, myButtons->buttons()) {
143 aBtn->setChecked(aBtn->toolTip() == theVal);
147 myCombo->setCurrentText(theVal);
153 void ModuleBase_ChoiceCtrl::setTooltip(QString theTip)
155 if (myType == ComboBox)
156 myLabel->setToolTip(theTip);
159 int ModuleBase_ChoiceCtrl::value() const
163 return myButtons->checkedId();
165 return myCombo->currentIndex();
170 bool ModuleBase_ChoiceCtrl::focusTo()
172 if (myType == ComboBox)
173 ModuleBase_Tools::setFocus(myCombo, "ModuleBase_WidgetChoice::focusTo()");
179 QList<QWidget*> ModuleBase_ChoiceCtrl::getControls() const
181 QList<QWidget*> aControls;
182 if (myType == ComboBox)
183 aControls.append(myCombo);
187 void ModuleBase_ChoiceCtrl::setChoiceList(const QStringList& theChoiceList)
189 if (myType == ComboBox) {
191 myCombo->addItems(theChoiceList);
195 QString ModuleBase_ChoiceCtrl::textValue() const
199 return myButtons->checkedButton()->toolTip();
201 return myCombo->currentText();