]> SALOME platform Git repositories - modules/shaper.git/blob - src/ModuleBase/ModuleBase_WidgetToolbox.cpp
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_ModelWidget.h>
11 #include <ModuleBase_Tools.h>
12
13 #include <ModelAPI_AttributeString.h>
14
15 #include <QWidget>
16 #include <Qlist>
17 #include <QVBoxLayout>
18
19 ModuleBase_WidgetToolbox::ModuleBase_WidgetToolbox(QWidget* theParent, const Config_WidgetAPI* theData,
20                                                    const std::string& theParentId)
21 : ModuleBase_ModelWidget(theParent, theData, theParentId),
22   myIsPassFocusToCurrentPage(false)
23 {
24   QVBoxLayout* aMainLayout = new QVBoxLayout(this);
25   ModuleBase_Tools::zeroMargins(aMainLayout);
26   myToolBox = new QToolBox(this);
27   // Dark-grey rounded tabs with button-like border #and bold font
28   QString css = "QToolBox::tab{background-color:#c8c8c8;"
29                               "border-radius:5px;"
30                               "border:1px inset;"
31                               //"font-weight:700;"
32                               "border-color:#fff #505050 #505050 #fff;}";
33   myToolBox->setStyleSheet(css);
34   // default vertical size policy is preferred
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(ModuleBase_PageBase* thePage,
45                                       const QString& theName, const QString& theCaseId)
46 {
47   myCaseIds << theCaseId;
48   myPages << thePage;
49   QFrame* aFrame = dynamic_cast<QFrame*>(thePage);
50   aFrame->setFrameShape(QFrame::Box);
51   aFrame->setFrameStyle(QFrame::Sunken);
52   return myToolBox->addItem(aFrame, theName);
53 }
54
55 bool ModuleBase_WidgetToolbox::restoreValue()
56 {
57   // A rare case when plugin was not loaded.
58   if(!myFeature)
59     return false;
60   DataPtr aData = myFeature->data();
61   AttributeStringPtr aStringAttr = aData->string(attributeID());
62   QString aCaseId = QString::fromStdString(aStringAttr->value());
63   int idx = myCaseIds.indexOf(aCaseId);
64   if (idx == -1)
65     return false;
66   bool isSignalsBlocked = myToolBox->blockSignals(true);
67   myToolBox->setCurrentIndex(idx);
68   myToolBox->blockSignals(isSignalsBlocked);
69   return true;
70 }
71
72 QList<QWidget*> ModuleBase_WidgetToolbox::getControls() const
73 {
74   QList<QWidget*> aResult;
75   int idx = myToolBox->currentIndex();
76   QList<ModuleBase_ModelWidget*> aModelWidgets = myPages[idx]->modelWidgets();
77   foreach(ModuleBase_ModelWidget* eachModelWidget, aModelWidgets) {
78     aResult << eachModelWidget->getControls();
79   }
80   return aResult;
81 }
82
83 bool ModuleBase_WidgetToolbox::focusTo()
84 {
85   int idx = myToolBox->currentIndex();
86   if (idx > myPages.count())
87     return false;
88   return myPages[idx]->takeFocus();
89 }
90
91 void ModuleBase_WidgetToolbox::setHighlighted(bool)
92 {
93   //page containers sould not be highlighted, do nothing
94 }
95
96 void ModuleBase_WidgetToolbox::enableFocusProcessing()
97 {
98   myIsPassFocusToCurrentPage = true;
99 }
100
101
102 void ModuleBase_WidgetToolbox::activateCustom()
103 {
104   // activate current page
105   focusTo();
106 }
107
108 bool ModuleBase_WidgetToolbox::storeValueCustom() const
109 {
110   // A rare case when plugin was not loaded.
111   if(!myFeature)
112     return false;
113   DataPtr aData = myFeature->data();
114   AttributeStringPtr aStringAttr = aData->string(attributeID());
115   QString aWidgetValue = myCaseIds.at(myToolBox->currentIndex());
116   aStringAttr->setValue(aWidgetValue.toStdString());
117   return true;
118 }
119
120 void ModuleBase_WidgetToolbox::onPageChanged()
121 {
122   storeValue();
123   if (myIsPassFocusToCurrentPage) focusTo();
124 }