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