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