Salome HOME
Add copyright header according to request of CEA from 06.06.2017
[modules/shaper.git] / src / ModuleBase / ModuleBase_WidgetValidator.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_WidgetValidator.h>
22 #include <ModuleBase_WidgetSelectorStore.h>
23 #include <ModuleBase_WidgetValidated.h>
24
25 #include <ModuleBase_ModelWidget.h>
26 #include <ModuleBase_ViewerPrs.h>
27 #include <ModuleBase_IViewer.h>
28 #include <ModuleBase_IWorkshop.h>
29
30 ModuleBase_WidgetValidator::ModuleBase_WidgetValidator(ModuleBase_ModelWidget* theModelWidget,
31                                                        ModuleBase_IWorkshop* theWorkshop)
32 : myModelWidget(theModelWidget), myWorkshop(theWorkshop), myIsInValidate(false)
33 {
34   myAttributeStore = new ModuleBase_WidgetSelectorStore();
35 }
36
37 ModuleBase_WidgetValidator::~ModuleBase_WidgetValidator()
38 {
39   delete myAttributeStore;
40 }
41
42 //********************************************************************
43 bool ModuleBase_WidgetValidator::isValidSelection(const ModuleBase_ViewerPrsPtr& theValue)
44 {
45   bool aValid = false;
46   if (getValidState(theValue, aValid))
47     return aValid;
48
49   aValid = myModelWidget->isValidSelectionCustom(theValue);
50
51   storeValidState(theValue, aValid);
52   return aValid;
53 }
54
55 bool ModuleBase_WidgetValidator::activateFilters(const bool toActivate)
56 {
57   ModuleBase_IViewer* aViewer = myWorkshop->viewer();
58
59   Handle(SelectMgr_Filter) aSelFilter = myWorkshop->validatorFilter();
60   bool aHasSelectionFilter = aViewer->hasSelectionFilter(aSelFilter);
61
62   if (toActivate)
63     aViewer->addSelectionFilter(aSelFilter);
64   else {
65     aViewer->removeSelectionFilter(aSelFilter);
66     clearValidatedCash();
67   }
68
69   return aHasSelectionFilter;
70 }
71
72 void ModuleBase_WidgetValidator::storeAttributeValue(const AttributePtr& theAttribute)
73 {
74   myIsInValidate = true;
75   myAttributeStore->storeAttributeValue(theAttribute, myWorkshop);
76 }
77
78 void ModuleBase_WidgetValidator::restoreAttributeValue(const AttributePtr& theAttribute,
79                                                        const bool theValid)
80 {
81   myIsInValidate = false;
82   myAttributeStore->restoreAttributeValue(theAttribute, myWorkshop);
83 }
84
85 bool ModuleBase_WidgetValidator::isValidAttribute(const AttributePtr& theAttribute) const
86 {
87   return ModuleBase_WidgetValidated::isValidAttribute(theAttribute);
88 }
89
90 bool ModuleBase_WidgetValidator::isFilterActivated() const
91 {
92   bool isActivated = false;
93
94   Handle(SelectMgr_Filter) aSelFilter = myWorkshop->validatorFilter();
95   ModuleBase_IViewer* aViewer = myWorkshop->viewer();
96
97   return aViewer->hasSelectionFilter(aSelFilter);
98 }
99
100 void ModuleBase_WidgetValidator::clearValidatedCash()
101 {
102   myValidPrs.clear();
103   myInvalidPrs.clear();
104 }
105
106 bool ModuleBase_WidgetValidator::getValidState(const ModuleBase_ViewerPrsPtr& theValue,
107                                                bool& theValid)
108 {
109   bool aValidPrs = myValidPrs.contains(theValue);
110   bool anInvalidPrs = myInvalidPrs.contains(theValue);
111
112   if (aValidPrs)
113     theValid = true;
114   else if (anInvalidPrs)
115     theValid = false;
116
117   return aValidPrs || anInvalidPrs;
118 }
119
120 //********************************************************************
121 void ModuleBase_WidgetValidator::storeValidState(const ModuleBase_ViewerPrsPtr& theValue,
122                                                  const bool theValid)
123 {
124   bool aValidPrs = myInvalidPrs.contains(theValue);
125   bool anInvalidPrs = myInvalidPrs.contains(theValue);
126
127   if (theValid) {
128     if (!aValidPrs)
129       myValidPrs.append(theValue);
130     // the commented code will be useful when the valid state of the presentation
131     // will be changable between activate/deactivate. Currently it does not happen.
132     //if (anInvalidPrs)
133     //  myInvalidPrs.removeOne(theValue);
134   }
135   else { // !theValid
136     if (!anInvalidPrs)
137       myInvalidPrs.append(theValue);
138     //if (!aValidPrs)
139     //  myValidPrs.removeOne(theValue);
140   }
141 #ifdef DEBUG_VALID_STATE
142   qDebug(QString("storeValidState: myValidPrs.size() = %1, myInvalidPrs.size() = %2")
143                  .arg(myValidPrs.count())
144                  .arg(myInvalidPrs.count()).toStdString().c_str());
145 #endif
146 }