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