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