Salome HOME
Merge branch 'master' into cgt/devCEA
[modules/shaper.git] / src / ModuleBase / ModuleBase_PagedContainer.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2 /*
3  * ModuleBase_PagedContainer.cpp
4  *
5  *  Created on: Mar 13, 2015
6  *      Author: sbh
7  */
8
9 #include <ModuleBase_PagedContainer.h>
10 #include <ModuleBase_PageBase.h>
11 #include <ModuleBase_ModelWidget.h>
12 #include <ModuleBase_Tools.h>
13
14 #include <ModelAPI_AttributeString.h>
15
16 #include <QWidget>
17 #include <QList>
18 #include <QVBoxLayout>
19
20
21 ModuleBase_PagedContainer::ModuleBase_PagedContainer(QWidget* theParent,
22                                                      const Config_WidgetAPI* theData)
23 : ModuleBase_ModelWidget(theParent, theData),
24   myIsFocusOnCurrentPage(false)
25 {
26   // it is not obligatory to be ignored when property panel tries to activate next active widget
27   // but if focus is moved to this control, it can accept it.
28   myIsObligatory = false;
29 }
30
31 ModuleBase_PagedContainer::~ModuleBase_PagedContainer()
32 {
33 }
34
35 int ModuleBase_PagedContainer::addPage(ModuleBase_PageBase* thePage,
36                                       const QString& theName, const QString& theCaseId,
37                                       const QPixmap& theIcon )
38 {
39   if (!myPages.count()) {
40     setDefaultValue(theCaseId.toStdString());
41   }
42   myCaseIds << theCaseId;
43   myPages << thePage;
44
45   return myPages.count();
46 }
47
48 QList<QWidget*> ModuleBase_PagedContainer::getControls() const
49 {
50   QList<QWidget*> aResult;
51   int anIndex = currentPageIndex();
52   QList<ModuleBase_ModelWidget*> aModelWidgets = myPages[anIndex]->modelWidgets();
53   foreach(ModuleBase_ModelWidget* eachModelWidget, aModelWidgets) {
54     aResult << eachModelWidget->getControls();
55   }
56   return aResult;
57 }
58
59 bool ModuleBase_PagedContainer::focusTo()
60 {
61   int anIndex = currentPageIndex();
62   if (anIndex > myPages.count())
63     return false;
64   return myPages[anIndex]->takeFocus();
65 }
66
67 void ModuleBase_PagedContainer::setHighlighted(bool)
68 {
69   //page containers should not be highlighted, do nothing
70 }
71
72 void ModuleBase_PagedContainer::enableFocusProcessing()
73 {
74   myIsFocusOnCurrentPage = true;
75 }
76
77 bool ModuleBase_PagedContainer::restoreValueCustom()
78 {
79   // A rare case when plugin was not loaded.
80   if(!myFeature)
81     return false;
82   DataPtr aData = myFeature->data();
83   AttributeStringPtr aStringAttr = aData->string(attributeID());
84   QString aCaseId = QString::fromStdString(aStringAttr->value());
85   int idx = myCaseIds.indexOf(aCaseId);
86   if (idx == -1)
87     return false;
88   setCurrentPageIndex(idx);
89   return true;
90 }
91
92 void ModuleBase_PagedContainer::activateCustom()
93 {
94   // activate current page
95   focusTo();
96 }
97
98 bool ModuleBase_PagedContainer::storeValueCustom()
99 {
100   // A rare case when plugin was not loaded.
101   if(!myFeature)
102     return false;
103   DataPtr aData = myFeature->data();
104   AttributeStringPtr aStringAttr = aData->string(attributeID());
105   QString aWidgetValue = myCaseIds.at(currentPageIndex());
106   aStringAttr->setValue(aWidgetValue.toStdString());
107   updateObject(myFeature); // for preview
108   return true;
109 }
110
111
112 void ModuleBase_PagedContainer::onPageChanged()
113 {
114   if (!storeValue())
115     return;
116   // focus might be changed only if the value is correcly stored
117   // if it is not stored, reentrant manager will handle by this widget
118   // after it will restart operation, the widget might be removed
119   if (myIsFocusOnCurrentPage)
120     focusTo();
121 }
122
123