Salome HOME
Merge branch 'Dev_1.1.0' of newgeom:newgeom into Dev_1.1.0
[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   if (!myPages.count()) {
35     setDefaultValue(theCaseId.toStdString());
36   }
37   myCaseIds << theCaseId;
38   myPages << thePage;
39
40   return myPages.count();
41 }
42
43 QList<QWidget*> ModuleBase_PagedContainer::getControls() const
44 {
45   QList<QWidget*> aResult;
46   int anIndex = currentPageIndex();
47   QList<ModuleBase_ModelWidget*> aModelWidgets = myPages[anIndex]->modelWidgets();
48   foreach(ModuleBase_ModelWidget* eachModelWidget, aModelWidgets) {
49     aResult << eachModelWidget->getControls();
50   }
51   return aResult;
52 }
53
54 bool ModuleBase_PagedContainer::focusTo()
55 {
56   int anIndex = currentPageIndex();
57   if (anIndex > myPages.count())
58     return false;
59   return myPages[anIndex]->takeFocus();
60 }
61
62 void ModuleBase_PagedContainer::setHighlighted(bool)
63 {
64   //page containers should not be highlighted, do nothing
65 }
66
67 void ModuleBase_PagedContainer::enableFocusProcessing()
68 {
69   myIsFocusOnCurrentPage = true;
70 }
71
72 bool ModuleBase_PagedContainer::restoreValue()
73 {
74   // A rare case when plugin was not loaded.
75   if(!myFeature)
76     return false;
77   DataPtr aData = myFeature->data();
78   AttributeStringPtr aStringAttr = aData->string(attributeID());
79   QString aCaseId = QString::fromStdString(aStringAttr->value());
80   int idx = myCaseIds.indexOf(aCaseId);
81   if (idx == -1)
82     return false;
83   setCurrentPageIndex(idx);
84   return true;
85 }
86
87 void ModuleBase_PagedContainer::activateCustom()
88 {
89   // activate current page
90   focusTo();
91 }
92
93 bool ModuleBase_PagedContainer::storeValueCustom() const
94 {
95   // A rare case when plugin was not loaded.
96   if(!myFeature)
97     return false;
98   DataPtr aData = myFeature->data();
99   AttributeStringPtr aStringAttr = aData->string(attributeID());
100   QString aWidgetValue = myCaseIds.at(currentPageIndex());
101   aStringAttr->setValue(aWidgetValue.toStdString());
102   updateObject(myFeature); // for preview
103   return true;
104 }
105
106
107 void ModuleBase_PagedContainer::onPageChanged()
108 {
109   storeValue();
110   if (myIsFocusOnCurrentPage) focusTo();
111 }
112
113