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 email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
18 //
19
20 /*
21  * ModuleBase_PagedContainer.cpp
22  *
23  *  Created on: Mar 13, 2015
24  *      Author: sbh
25  */
26
27 #include <ModuleBase_PagedContainer.h>
28 #include <ModuleBase_PageBase.h>
29 #include <ModuleBase_ModelWidget.h>
30 #include <ModuleBase_Tools.h>
31
32 #include <ModelAPI_AttributeString.h>
33
34 #include <QWidget>
35 #include <QList>
36 #include <QVBoxLayout>
37
38
39 ModuleBase_PagedContainer::ModuleBase_PagedContainer(QWidget* theParent,
40                                                      const Config_WidgetAPI* theData)
41 : ModuleBase_ModelWidget(theParent, theData),
42   myIsFocusOnCurrentPage(false)
43 {
44   // it is not obligatory to be ignored when property panel tries to activate next active widget
45   // but if focus is moved to this control, it can accept it.
46   myIsObligatory = false;
47 }
48
49 ModuleBase_PagedContainer::~ModuleBase_PagedContainer()
50 {
51 }
52
53 int ModuleBase_PagedContainer::addPage(ModuleBase_PageBase* thePage,
54                                       const QString& theName, const QString& theCaseId,
55                                       const QPixmap& theIcon )
56 {
57   if (!myPages.count()) {
58     setDefaultValue(theCaseId.toStdString());
59   }
60   myCaseIds << theCaseId;
61   myPages << thePage;
62
63   return myPages.count();
64 }
65
66 QList<QWidget*> ModuleBase_PagedContainer::getControls() const
67 {
68   QList<QWidget*> aResult;
69   int anIndex = currentPageIndex();
70   QList<ModuleBase_ModelWidget*> aModelWidgets = myPages[anIndex]->modelWidgets();
71   foreach(ModuleBase_ModelWidget* eachModelWidget, aModelWidgets) {
72     aResult << eachModelWidget->getControls();
73   }
74   return aResult;
75 }
76
77 bool ModuleBase_PagedContainer::focusTo()
78 {
79   int anIndex = currentPageIndex();
80   if (anIndex > myPages.count())
81     return false;
82   return myPages[anIndex]->takeFocus();
83 }
84
85 void ModuleBase_PagedContainer::setHighlighted(bool)
86 {
87   //page containers should not be highlighted, do nothing
88 }
89
90 void ModuleBase_PagedContainer::enableFocusProcessing()
91 {
92   myIsFocusOnCurrentPage = true;
93 }
94
95 bool ModuleBase_PagedContainer::restoreValueCustom()
96 {
97   // A rare case when plugin was not loaded.
98   if(!myFeature)
99     return false;
100   DataPtr aData = myFeature->data();
101   AttributeStringPtr aStringAttr = aData->string(attributeID());
102   QString aCaseId = QString::fromStdString(aStringAttr->value());
103   int idx = myCaseIds.indexOf(aCaseId);
104   if (idx == -1)
105     return false;
106   setCurrentPageIndex(idx);
107   return true;
108 }
109
110 void ModuleBase_PagedContainer::activateCustom()
111 {
112   // activate current page
113   focusTo();
114 }
115
116 bool ModuleBase_PagedContainer::storeValueCustom()
117 {
118   // A rare case when plugin was not loaded.
119   if(!myFeature)
120     return false;
121   DataPtr aData = myFeature->data();
122   AttributeStringPtr aStringAttr = aData->string(attributeID());
123   QString aWidgetValue = myCaseIds.at(currentPageIndex());
124   aStringAttr->setValue(aWidgetValue.toStdString());
125   updateObject(myFeature); // for preview
126   return true;
127 }
128
129
130 void ModuleBase_PagedContainer::onPageChanged()
131 {
132   if (!storeValue())
133     return;
134   // focus might be changed only if the value is correcly stored
135   // if it is not stored, reentrant manager will handle by this widget
136   // after it will restart operation, the widget might be removed
137   if (myIsFocusOnCurrentPage)
138     focusTo();
139 }
140
141