Salome HOME
Issue #1368: Creation of a Qt panel. Code improvement.
[modules/shaper.git] / src / ModuleBase / ModuleBase_WidgetChoice.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        ModuleBase_WidgetChoice.cpp
4 // Created:     03 Sept 2014
5 // Author:      Vitaly Smetannikov
6
7 #include "ModuleBase_WidgetChoice.h"
8 #include <ModuleBase_Tools.h>
9
10 #include <ModelAPI_AttributeInteger.h>
11 #include <ModelAPI_Data.h>
12 #include <Config_WidgetAPI.h>
13
14 #include <QWidget>
15 #include <QLayout>
16 #include <QLabel>
17 #include <QComboBox>
18 #include <QButtonGroup>
19 #include <QGroupBox>
20 #include <QRadioButton>
21 #include <QToolButton>
22
23
24 ModuleBase_WidgetChoice::ModuleBase_WidgetChoice(QWidget* theParent, 
25                                                  const Config_WidgetAPI* theData)
26 : ModuleBase_ModelWidget(theParent, theData), myCombo(0), myButtons(0)
27 {
28   QHBoxLayout* aLayout = new QHBoxLayout(this);
29   ModuleBase_Tools::adjustMargins(aLayout);
30
31   QString aLabelText = QString::fromStdString(theData->widgetLabel());
32   QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
33   std::string aTypes = theData->getProperty("string_list");
34   QStringList aList = QString(aTypes.c_str()).split(' ');
35
36   // Widget type can be combobox or radiobuttons
37   std::string aWgtType = theData->getProperty("widget_type");
38   if ((aWgtType.length() > 0) && (aWgtType == "radiobuttons")) {
39     myButtons = new QButtonGroup(this);
40     QGroupBox* aGroupBox = new QGroupBox(aLabelText, this);
41     aLayout->addWidget(aGroupBox);
42
43
44     QLayout* aBtnLayout = 0;
45     std::string aWgtDir = theData->getProperty("buttons_dir");
46     if (aWgtDir == "horizontal")
47       aBtnLayout = new QHBoxLayout(aGroupBox);
48     else 
49       aBtnLayout = new QVBoxLayout(aGroupBox);
50     ModuleBase_Tools::adjustMargins(aBtnLayout);
51
52     std::string aIcons = theData->getProperty("icons_list");
53     QStringList aIconList = QString(aIcons.c_str()).split(' ');
54     if (aIconList.length() == aList.length()) {
55       int aId = 0;
56       foreach(QString aBtnTxt, aList) {
57         QToolButton* aBtn = new QToolButton(aGroupBox);
58         aBtn->setFocusPolicy(Qt::StrongFocus);
59         aBtn->setCheckable(true);
60         aBtn->setToolTip(aBtnTxt);
61
62         QPixmap aIcon(aIconList.at(aId));
63         aBtn->setIcon(aIcon);
64         aBtn->setIconSize(aIcon.size());
65         
66         aBtnLayout->addWidget(aBtn);
67         myButtons->addButton(aBtn, aId++);
68       }
69
70     } else {
71       int aId = 0;
72       foreach(QString aBtnTxt, aList) {
73         QRadioButton* aBtn = new QRadioButton(aBtnTxt, aGroupBox);
74         aBtnLayout->addWidget(aBtn);
75         myButtons->addButton(aBtn, aId++);
76       }
77     }
78     myButtons->button(0)->setChecked(true);
79     connect(myButtons, SIGNAL(buttonClicked(int)), this, SLOT(onCurrentIndexChanged(int)));
80     connect(myButtons, SIGNAL(buttonClicked(int)), this, SIGNAL(itemSelected(int)));
81   } else {
82     myLabel = new QLabel(aLabelText, this);
83     if (!aLabelIcon.isEmpty())
84       myLabel->setPixmap(QPixmap(aLabelIcon));
85     aLayout->addWidget(myLabel);
86
87     std::string aToolstr = theData->widgetTooltip();
88     if (!aToolstr.empty()) {
89       myLabel->setToolTip(QString::fromStdString(aToolstr));
90     }
91
92     myCombo = new QComboBox(this);
93     aLayout->addWidget(myCombo, 1);
94  
95     myCombo->addItems(aList);
96
97     connect(myCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(onCurrentIndexChanged(int)));
98     connect(myCombo, SIGNAL(currentIndexChanged(int)), this, SIGNAL(itemSelected(int)));
99   }
100 }
101
102 ModuleBase_WidgetChoice::~ModuleBase_WidgetChoice()
103 {
104 }
105   
106 bool ModuleBase_WidgetChoice::storeValueCustom() const
107 {
108   DataPtr aData = myFeature->data();
109   std::shared_ptr<ModelAPI_AttributeInteger> aIntAttr = aData->integer(attributeID());
110
111   if (myCombo)
112     aIntAttr->setValue(myCombo->currentIndex());
113   else
114     aIntAttr->setValue(myButtons->checkedId());
115   updateObject(myFeature);
116   return true;
117 }
118
119 bool ModuleBase_WidgetChoice::restoreValueCustom()
120 {
121   DataPtr aData = myFeature->data();
122   std::shared_ptr<ModelAPI_AttributeInteger> aIntAttr = aData->integer(attributeID());
123
124   if (aIntAttr->value() != -1) {
125     if (myCombo) {
126       bool isBlocked = myCombo->blockSignals(true);
127       myCombo->setCurrentIndex(aIntAttr->value());
128       myCombo->blockSignals(isBlocked);
129     } else {
130       bool isBlocked = myButtons->blockSignals(true);
131       myButtons->button(aIntAttr->value())->setChecked(true);
132       myButtons->blockSignals(isBlocked);
133       emit itemSelected(aIntAttr->value());
134     }
135   }
136   return true;
137 }
138
139 bool ModuleBase_WidgetChoice::focusTo()
140 {
141   if (myCombo)
142     ModuleBase_Tools::setFocus(myCombo, "ModuleBase_WidgetChoice::focusTo()");
143   else
144     return false;
145   return true;
146 }
147
148 QList<QWidget*> ModuleBase_WidgetChoice::getControls() const
149 {
150   QList<QWidget*> aControls;
151   if (myCombo)
152     aControls.append(myCombo);
153   //else {
154   //  //foreach(QAbstractButton* aBtn, myButtons->buttons())
155   //  //if (myButtons->checkedId() != -1)
156   //  //  aControls.append(myButtons->button(myButtons->checkedId()));
157   //}
158   return aControls;
159 }
160
161 void ModuleBase_WidgetChoice::onCurrentIndexChanged(int theIndex)
162 {
163   emit valuesChanged();
164   // Don't transfer focus
165   // emit focusOutWidget(this);
166 }