Salome HOME
Minor corrections
[modules/shaper.git] / src / ModuleBase / ModuleBase_PagedContainer.cpp
1 /*
2  * ModuleBase_PagedContainer.cpp
3  *
4  *  Created on: Mar 13, 2015
5  *      Author: sbh
6  */
7
8 #include <ModuleBase_PagedContainer.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
20 ModuleBase_PagedContainer::ModuleBase_PagedContainer(QWidget* theParent, const Config_WidgetAPI* theData,
21                                                      const std::string& theParentId)
22 : ModuleBase_ModelWidget(theParent, theData, theParentId),
23   myIsFocusOnCurrentPage(false)
24 {
25 }
26
27 ModuleBase_PagedContainer::~ModuleBase_PagedContainer()
28 {
29 }
30
31 int ModuleBase_PagedContainer::addPage(ModuleBase_PageBase* thePage,
32                                       const QString& theName, const QString& theCaseId)
33 {
34   myCaseIds << theCaseId;
35   myPages << thePage;
36   storeValueCustom(); // to store the initial state
37   return myPages.count();
38 }
39
40 QList<QWidget*> ModuleBase_PagedContainer::getControls() const
41 {
42   QList<QWidget*> aResult;
43   int anIndex = currentPageIndex();
44   QList<ModuleBase_ModelWidget*> aModelWidgets = myPages[anIndex]->modelWidgets();
45   foreach(ModuleBase_ModelWidget* eachModelWidget, aModelWidgets) {
46     aResult << eachModelWidget->getControls();
47   }
48   return aResult;
49 }
50
51 bool ModuleBase_PagedContainer::focusTo()
52 {
53   int anIndex = currentPageIndex();
54   if (anIndex > myPages.count())
55     return false;
56   return myPages[anIndex]->takeFocus();
57 }
58
59 void ModuleBase_PagedContainer::setHighlighted(bool)
60 {
61   //page containers should not be highlighted, do nothing
62 }
63
64 void ModuleBase_PagedContainer::enableFocusProcessing()
65 {
66   myIsFocusOnCurrentPage = true;
67 }
68
69 bool ModuleBase_PagedContainer::restoreValue()
70 {
71   // A rare case when plugin was not loaded.
72   if(!myFeature)
73     return false;
74   DataPtr aData = myFeature->data();
75   AttributeStringPtr aStringAttr = aData->string(attributeID());
76   QString aCaseId = QString::fromStdString(aStringAttr->value());
77   int idx = myCaseIds.indexOf(aCaseId);
78   if (idx == -1)
79     return false;
80   setCurrentPageIndex(idx);
81   return true;
82 }
83
84 void ModuleBase_PagedContainer::activateCustom()
85 {
86   // activate current page
87   focusTo();
88 }
89
90 bool ModuleBase_PagedContainer::storeValueCustom() const
91 {
92   // A rare case when plugin was not loaded.
93   if(!myFeature)
94     return false;
95   DataPtr aData = myFeature->data();
96   AttributeStringPtr aStringAttr = aData->string(attributeID());
97   QString aWidgetValue = myCaseIds.at(currentPageIndex());
98   aStringAttr->setValue(aWidgetValue.toStdString());
99   updateObject(myFeature); // for preview
100   return true;
101 }
102
103
104 void ModuleBase_PagedContainer::onPageChanged()
105 {
106   storeValue();
107   if (myIsFocusOnCurrentPage) focusTo();
108 }
109
110