Salome HOME
Merge branch 'Dev_1.1.0' of newgeom:newgeom.git into Dev_1.1.0
[modules/shaper.git] / src / ModuleBase / ModuleBase_WidgetToolbox.cpp
1 /*
2  * ModuleBase_WidgetToolbox.cpp
3  *
4  *  Created on: Feb 27, 2015
5  *      Author: sbh
6  */
7
8 #include <ModuleBase_WidgetToolbox.h>
9 #include <ModuleBase_PageBase.h>
10 #include <ModuleBase_Tools.h>
11
12 #include <ModelAPI_AttributeString.h>
13
14 #include <QWidget>
15 #include <QVBoxLayout>
16
17 ModuleBase_WidgetToolbox::ModuleBase_WidgetToolbox(QWidget* theParent, const Config_WidgetAPI* theData,
18                                                    const std::string& theParentId)
19 : ModuleBase_ModelWidget(theParent, theData, theParentId)
20 {
21   QVBoxLayout* aMainLayout = new QVBoxLayout(this);
22   ModuleBase_Tools::zeroMargins(aMainLayout);
23   myToolBox = new QToolBox(this);
24   // Dark-grey rounded tabs with button-like border #and bold font
25   QString css = "QToolBox::tab{background-color:#c8c8c8;"
26                               "border-radius:5px;"
27                               "border:1px inset;"
28                               //"font-weight:700;"
29                               "border-color:#fff #505050 #505050 #fff;}";
30   myToolBox->setStyleSheet(css);
31   // default vertical size policy is preferred
32   QSizePolicy aSizePolicy = myToolBox->sizePolicy();
33   aSizePolicy.setVerticalPolicy(QSizePolicy::MinimumExpanding);
34   myToolBox->setSizePolicy(aSizePolicy);
35   aMainLayout->addWidget(myToolBox);
36
37   connect(myToolBox, SIGNAL(currentChanged(int)), this, SLOT(onPageChanged()));
38 }
39
40 ModuleBase_WidgetToolbox::~ModuleBase_WidgetToolbox()
41 {
42 }
43
44 int ModuleBase_WidgetToolbox::addPage(QWidget* theWidget,
45                                       const QString& theName, const QString& theCaseId)
46 {
47   myCaseIds << theCaseId;
48   return myToolBox->addItem(theWidget, theName);
49 }
50
51 bool ModuleBase_WidgetToolbox::restoreValue()
52 {
53   // A rare case when plugin was not loaded.
54   if(!myFeature)
55     return false;
56   DataPtr aData = myFeature->data();
57   AttributeStringPtr aStringAttr = aData->string(attributeID());
58   QString aCaseId = QString::fromStdString(aStringAttr->value());
59   int idx = myCaseIds.indexOf(aCaseId);
60   if (idx == -1)
61     return false;
62   bool isSignalsBlocked = myToolBox->blockSignals(true);
63   myToolBox->setCurrentIndex(idx);
64   myToolBox->blockSignals(isSignalsBlocked);
65   return true;
66 }
67
68 QList<QWidget*> ModuleBase_WidgetToolbox::getControls() const
69 {
70   QList<QWidget*> aList;
71   aList << myToolBox;
72   return aList;
73 }
74
75 bool ModuleBase_WidgetToolbox::focusTo()
76 {
77   return false;
78 }
79
80 bool ModuleBase_WidgetToolbox::storeValueCustom() const
81 {
82   // A rare case when plugin was not loaded.
83   if(!myFeature)
84     return false;
85   DataPtr aData = myFeature->data();
86   AttributeStringPtr aStringAttr = aData->string(attributeID());
87   QString aWidgetValue = myCaseIds.at(myToolBox->currentIndex());
88   aStringAttr->setValue(aWidgetValue.toStdString());
89   return true;
90 }
91
92 void ModuleBase_WidgetToolbox::onPageChanged()
93 {
94   storeValue();
95 }