Salome HOME
Merge remote-tracking branch 'remotes/origin/Dev_UnitTestsForFill'
[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 void ModuleBase_WidgetValidator::selectionFilters(QIntList& theModuleSelectionFilters,
44                                                   SelectMgr_ListOfFilter& theSelectionFilters)
45 {
46   theSelectionFilters.Append(myWorkshop->validatorFilter());
47 }
48
49 //********************************************************************
50 bool ModuleBase_WidgetValidator::isValidSelection(const ModuleBase_ViewerPrsPtr& theValue)
51 {
52   bool aValid = false;
53   if (getValidState(theValue, aValid))
54     return aValid;
55
56   aValid = myModelWidget->isValidSelectionCustom(theValue);
57
58   storeValidState(theValue, aValid);
59   return aValid;
60 }
61
62 void ModuleBase_WidgetValidator::storeAttributeValue(const AttributePtr& theAttribute)
63 {
64   myIsInValidate = true;
65   myAttributeStore->storeAttributeValue(theAttribute, myWorkshop);
66 }
67
68 void ModuleBase_WidgetValidator::restoreAttributeValue(const AttributePtr& theAttribute,
69                                                        const bool theValid)
70 {
71   myIsInValidate = false;
72   myAttributeStore->restoreAttributeValue(theAttribute, myWorkshop);
73 }
74
75 bool ModuleBase_WidgetValidator::isValidAttribute(const AttributePtr& theAttribute) const
76 {
77   return ModuleBase_WidgetValidated::isValidAttribute(theAttribute);
78 }
79
80 bool ModuleBase_WidgetValidator::isFilterActivated() const
81 {
82   bool isActivated = false;
83
84   Handle(SelectMgr_Filter) aSelFilter = myWorkshop->validatorFilter();
85   ModuleBase_IViewer* aViewer = myWorkshop->viewer();
86
87   return aViewer->hasSelectionFilter(aSelFilter);
88 }
89
90 void ModuleBase_WidgetValidator::clearValidatedCash()
91 {
92   myValidPrs.clear();
93   myInvalidPrs.clear();
94 }
95
96 bool ModuleBase_WidgetValidator::getValidState(const ModuleBase_ViewerPrsPtr& theValue,
97                                                bool& theValid)
98 {
99   bool aValidPrs = myValidPrs.contains(theValue);
100   bool anInvalidPrs = myInvalidPrs.contains(theValue);
101
102   if (aValidPrs)
103     theValid = true;
104   else if (anInvalidPrs)
105     theValid = false;
106
107   return aValidPrs || anInvalidPrs;
108 }
109
110 //********************************************************************
111 void ModuleBase_WidgetValidator::storeValidState(const ModuleBase_ViewerPrsPtr& theValue,
112                                                  const bool theValid)
113 {
114   bool aValidPrs = myInvalidPrs.contains(theValue);
115   bool anInvalidPrs = myInvalidPrs.contains(theValue);
116
117   if (theValid) {
118     if (!aValidPrs)
119       myValidPrs.append(theValue);
120     // the commented code will be useful when the valid state of the presentation
121     // will be changable between activate/deactivate. Currently it does not happen.
122     //if (anInvalidPrs)
123     //  myInvalidPrs.removeOne(theValue);
124   }
125   else { // !theValid
126     if (!anInvalidPrs)
127       myInvalidPrs.append(theValue);
128     //if (!aValidPrs)
129     //  myValidPrs.removeOne(theValue);
130   }
131 #ifdef DEBUG_VALID_STATE
132   qDebug(QString("storeValidState: myValidPrs.size() = %1, myInvalidPrs.size() = %2")
133                  .arg(myValidPrs.count())
134                  .arg(myInvalidPrs.count()).toStdString().c_str());
135 #endif
136 }