Salome HOME
d069b57f604a783df4b4960bf4a91e1dff638657
[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 : ModuleBase_ModelWidget(theParent, theData),
22   myIsFocusOnCurrentPage(false)
23 {
24   // it is not obligatory to be ignored when property panel tries to activate next active widget
25   // but if focus is moved to this control, it can accept it.
26   myIsObligatory = false;
27 }
28
29 ModuleBase_PagedContainer::~ModuleBase_PagedContainer()
30 {
31 }
32
33 int ModuleBase_PagedContainer::addPage(ModuleBase_PageBase* thePage,
34                                       const QString& theName, const QString& theCaseId,
35                                       const QPixmap& theIcon )
36 {
37   if (!myPages.count()) {
38     setDefaultValue(theCaseId.toStdString());
39   }
40   myCaseIds << theCaseId;
41   myPages << thePage;
42
43   return myPages.count();
44 }
45
46 QList<QWidget*> ModuleBase_PagedContainer::getControls() const
47 {
48   QList<QWidget*> aResult;
49   int anIndex = currentPageIndex();
50   QList<ModuleBase_ModelWidget*> aModelWidgets = myPages[anIndex]->modelWidgets();
51   foreach(ModuleBase_ModelWidget* eachModelWidget, aModelWidgets) {
52     aResult << eachModelWidget->getControls();
53   }
54   return aResult;
55 }
56
57 bool ModuleBase_PagedContainer::focusTo()
58 {
59   int anIndex = currentPageIndex();
60   if (anIndex > myPages.count())
61     return false;
62   return myPages[anIndex]->takeFocus();
63 }
64
65 void ModuleBase_PagedContainer::setHighlighted(bool)
66 {
67   //page containers should not be highlighted, do nothing
68 }
69
70 void ModuleBase_PagedContainer::enableFocusProcessing()
71 {
72   myIsFocusOnCurrentPage = true;
73 }
74
75 bool ModuleBase_PagedContainer::restoreValueCustom()
76 {
77   // A rare case when plugin was not loaded.
78   if(!myFeature)
79     return false;
80   DataPtr aData = myFeature->data();
81   AttributeStringPtr aStringAttr = aData->string(attributeID());
82   QString aCaseId = QString::fromStdString(aStringAttr->value());
83   int idx = myCaseIds.indexOf(aCaseId);
84   if (idx == -1)
85     return false;
86   setCurrentPageIndex(idx);
87   return true;
88 }
89
90 void ModuleBase_PagedContainer::activateCustom()
91 {
92   // activate current page
93   focusTo();
94 }
95
96 bool ModuleBase_PagedContainer::storeValueCustom()
97 {
98   // A rare case when plugin was not loaded.
99   if(!myFeature)
100     return false;
101   DataPtr aData = myFeature->data();
102   AttributeStringPtr aStringAttr = aData->string(attributeID());
103   QString aWidgetValue = myCaseIds.at(currentPageIndex());
104   aStringAttr->setValue(aWidgetValue.toStdString());
105   updateObject(myFeature); // for preview
106   return true;
107 }
108
109
110 void ModuleBase_PagedContainer::onPageChanged()
111 {
112   storeValue();
113   if (myIsFocusOnCurrentPage) focusTo();
114 }
115
116