Salome HOME
Change color for construction/body/group.
[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   aMainLayout->addWidget(myToolBox);
33
34   connect(myToolBox, SIGNAL(currentChanged(int)), this, SLOT(onPageChanged()));
35 }
36
37 ModuleBase_WidgetToolbox::~ModuleBase_WidgetToolbox()
38 {
39 }
40
41 int ModuleBase_WidgetToolbox::addPage(ModuleBase_PageBase* thePage,
42                                       const QString& theName, const QString& theCaseId)
43 {
44   myCaseIds << theCaseId;
45   myPages << thePage;
46   QFrame* aFrame = dynamic_cast<QFrame*>(thePage);
47   aFrame->setFrameShape(QFrame::Box);
48   aFrame->setFrameStyle(QFrame::Sunken);
49   return myToolBox->addItem(aFrame, theName);
50 }
51
52 bool ModuleBase_WidgetToolbox::restoreValue()
53 {
54   // A rare case when plugin was not loaded.
55   if(!myFeature)
56     return false;
57   DataPtr aData = myFeature->data();
58   AttributeStringPtr aStringAttr = aData->string(attributeID());
59   QString aCaseId = QString::fromStdString(aStringAttr->value());
60   int idx = myCaseIds.indexOf(aCaseId);
61   if (idx == -1)
62     return false;
63   bool isSignalsBlocked = myToolBox->blockSignals(true);
64   myToolBox->setCurrentIndex(idx);
65   myToolBox->blockSignals(isSignalsBlocked);
66   focusTo();
67   return true;
68 }
69
70 QList<QWidget*> ModuleBase_WidgetToolbox::getControls() const
71 {
72   QList<QWidget*> aList;
73   aList << myToolBox;
74   return aList;
75 }
76
77 bool ModuleBase_WidgetToolbox::focusTo()
78 {
79   int idx = myToolBox->currentIndex();
80   if (idx > myPages.count())
81     return false;
82   myPages[idx]->takeFocus();
83   repaint();
84   return true;
85 }
86
87 void ModuleBase_WidgetToolbox::activateCustom()
88 {
89 }
90
91 bool ModuleBase_WidgetToolbox::storeValueCustom() const
92 {
93   // A rare case when plugin was not loaded.
94   if(!myFeature)
95     return false;
96   DataPtr aData = myFeature->data();
97   AttributeStringPtr aStringAttr = aData->string(attributeID());
98   QString aWidgetValue = myCaseIds.at(myToolBox->currentIndex());
99   aStringAttr->setValue(aWidgetValue.toStdString());
100   return true;
101 }
102
103 void ModuleBase_WidgetToolbox::onPageChanged()
104 {
105   storeValue();
106   focusTo();
107 }