Salome HOME
Issue 1299 Angle presentation: "Cut"/"Fuse"/"Common" should not be shown in the PP...
[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   if (theData->getBooleanAttribute("use_in_title", false))
37     myButtonTitles = QString(aTypes.c_str()).split(" ");
38
39   // Widget type can be combobox or radiobuttons
40   std::string aWgtType = theData->getProperty("widget_type");
41   if ((aWgtType.length() > 0) && (aWgtType == "radiobuttons")) {
42     myButtons = new QButtonGroup(this);
43     QGroupBox* aGroupBox = new QGroupBox(aLabelText, this);
44     aLayout->addWidget(aGroupBox);
45
46
47     QLayout* aBtnLayout = 0;
48     std::string aWgtDir = theData->getProperty("buttons_dir");
49     if (aWgtDir == "horizontal")
50       aBtnLayout = new QHBoxLayout(aGroupBox);
51     else 
52       aBtnLayout = new QVBoxLayout(aGroupBox);
53     ModuleBase_Tools::adjustMargins(aBtnLayout);
54
55     std::string aIcons = theData->getProperty("icons_list");
56     QStringList aIconList = QString(aIcons.c_str()).split(' ');
57     if (aIconList.length() == aList.length()) {
58       int aId = 0;
59       foreach(QString aBtnTxt, aList) {
60         QToolButton* aBtn = new QToolButton(aGroupBox);
61         aBtn->setFocusPolicy(Qt::StrongFocus);
62         aBtn->setCheckable(true);
63         aBtn->setToolTip(aBtnTxt);
64
65         QPixmap aIcon(aIconList.at(aId));
66         aBtn->setIcon(aIcon);
67         aBtn->setIconSize(aIcon.size());
68         
69         aBtnLayout->addWidget(aBtn);
70         myButtons->addButton(aBtn, aId++);
71       }
72
73     } else {
74       int aId = 0;
75       foreach(QString aBtnTxt, aList) {
76         QRadioButton* aBtn = new QRadioButton(aBtnTxt, aGroupBox);
77         aBtnLayout->addWidget(aBtn);
78         myButtons->addButton(aBtn, aId++);
79       }
80     }
81     myButtons->button(0)->setChecked(true);
82     connect(myButtons, SIGNAL(buttonClicked(int)), this, SLOT(onCurrentIndexChanged(int)));
83   } else {
84     myLabel = new QLabel(aLabelText, this);
85     if (!aLabelIcon.isEmpty())
86       myLabel->setPixmap(QPixmap(aLabelIcon));
87     aLayout->addWidget(myLabel);
88
89     std::string aToolstr = theData->widgetTooltip();
90     if (!aToolstr.empty()) {
91       myLabel->setToolTip(QString::fromStdString(aToolstr));
92     }
93
94     myCombo = new QComboBox(this);
95     aLayout->addWidget(myCombo, 1);
96  
97     myCombo->addItems(aList);
98
99     connect(myCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(onCurrentIndexChanged(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(this, 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 QString ModuleBase_WidgetChoice::getPropertyPanelTitle(int theIndex)
163 {
164   QString aTitle;
165   if (myButtonTitles.length() > theIndex)
166     aTitle = tr(myButtonTitles[theIndex].toStdString().c_str());
167   return aTitle;
168 }
169
170 void ModuleBase_WidgetChoice::onCurrentIndexChanged(int theIndex)
171 {
172   emit valuesChanged();
173   // Don't transfer focus
174   // emit focusOutWidget(this);
175
176   emit itemSelected(this, theIndex);
177 }