Salome HOME
Add copyright header according to request of CEA from 06.06.2017
[modules/shaper.git] / src / ModuleBase / ModuleBase_PagedContainer.cpp
1 // Copyright (C) 2014-2017  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include <ModuleBase_PagedContainer.h>
22 #include <ModuleBase_PageBase.h>
23 #include <ModuleBase_ModelWidget.h>
24 #include <ModuleBase_Tools.h>
25
26 #include <ModelAPI_AttributeString.h>
27
28 #include <QWidget>
29 #include <QList>
30 #include <QVBoxLayout>
31
32
33 ModuleBase_PagedContainer::ModuleBase_PagedContainer(QWidget* theParent,
34                                                      const Config_WidgetAPI* theData)
35 : ModuleBase_ModelWidget(theParent, theData),
36   myIsFocusOnCurrentPage(false)
37 {
38   // it is not obligatory to be ignored when property panel tries to activate next active widget
39   // but if focus is moved to this control, it can accept it.
40   myIsObligatory = false;
41 }
42
43 ModuleBase_PagedContainer::~ModuleBase_PagedContainer()
44 {
45 }
46
47 int ModuleBase_PagedContainer::addPage(ModuleBase_PageBase* thePage,
48                                       const QString& theName, const QString& theCaseId,
49                                       const QPixmap& theIcon )
50 {
51   if (!myPages.count()) {
52     setDefaultValue(theCaseId.toStdString());
53   }
54   myCaseIds << theCaseId;
55   myPages << thePage;
56
57   return myPages.count();
58 }
59
60 QList<QWidget*> ModuleBase_PagedContainer::getControls() const
61 {
62   QList<QWidget*> aResult;
63   int anIndex = currentPageIndex();
64   QList<ModuleBase_ModelWidget*> aModelWidgets = myPages[anIndex]->modelWidgets();
65   foreach(ModuleBase_ModelWidget* eachModelWidget, aModelWidgets) {
66     aResult << eachModelWidget->getControls();
67   }
68   return aResult;
69 }
70
71 bool ModuleBase_PagedContainer::focusTo()
72 {
73   int anIndex = currentPageIndex();
74   if (anIndex > myPages.count())
75     return false;
76   return myPages[anIndex]->takeFocus();
77 }
78
79 void ModuleBase_PagedContainer::setHighlighted(bool)
80 {
81   //page containers should not be highlighted, do nothing
82 }
83
84 void ModuleBase_PagedContainer::enableFocusProcessing()
85 {
86   myIsFocusOnCurrentPage = true;
87 }
88
89 bool ModuleBase_PagedContainer::restoreValueCustom()
90 {
91   // A rare case when plugin was not loaded.
92   if(!myFeature)
93     return false;
94   DataPtr aData = myFeature->data();
95   AttributeStringPtr aStringAttr = aData->string(attributeID());
96   QString aCaseId = QString::fromStdString(aStringAttr->value());
97   int idx = myCaseIds.indexOf(aCaseId);
98   if (idx == -1)
99     return false;
100   setCurrentPageIndex(idx);
101   return true;
102 }
103
104 void ModuleBase_PagedContainer::activateCustom()
105 {
106   // activate current page
107   focusTo();
108 }
109
110 bool ModuleBase_PagedContainer::storeValueCustom()
111 {
112   // A rare case when plugin was not loaded.
113   if(!myFeature)
114     return false;
115   DataPtr aData = myFeature->data();
116   AttributeStringPtr aStringAttr = aData->string(attributeID());
117   QString aWidgetValue = myCaseIds.at(currentPageIndex());
118   aStringAttr->setValue(aWidgetValue.toStdString());
119   updateObject(myFeature); // for preview
120   return true;
121 }
122
123
124 void ModuleBase_PagedContainer::onPageChanged()
125 {
126   if (!storeValue())
127     return;
128   // focus might be changed only if the value is correcly stored
129   // if it is not stored, reentrant manager will handle by this widget
130   // after it will restart operation, the widget might be removed
131   if (myIsFocusOnCurrentPage)
132     focusTo();
133 }
134
135