Salome HOME
Merge branch 'master' into cgt/devCEA
[modules/shaper.git] / src / ModuleBase / ModuleBase_Operation.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_Operation.h"
22
23 #include "ModuleBase_OperationDescription.h"
24 #include "ModuleBase_ModelWidget.h"
25 #include "ModuleBase_ViewerPrs.h"
26 #include "ModuleBase_IPropertyPanel.h"
27 #include "ModuleBase_ISelection.h"
28 #include "ModuleBase_IViewer.h"
29
30 #include <ModelAPI_AttributeDouble.h>
31 #include <ModelAPI_Document.h>
32 #include <ModelAPI_Feature.h>
33 #include <ModelAPI_Data.h>
34 #include <ModelAPI_Document.h>
35 #include <ModelAPI_Events.h>
36 #include <ModelAPI_Result.h>
37 #include <ModelAPI_Object.h>
38 #include <ModelAPI_Validator.h>
39 #include <ModelAPI_Session.h>
40
41 #include <GeomAPI_Pnt2d.h>
42
43 #include <Events_Loop.h>
44
45 #include <QTimer>
46
47 #ifdef _DEBUG
48 #include <QDebug>
49 #endif
50
51 ModuleBase_Operation::ModuleBase_Operation(const QString& theId, QObject* theParent)
52     : QObject(theParent),
53       myIsModified(false),
54       myPropertyPanel(NULL)
55 {
56   myDescription = new ModuleBase_OperationDescription(theId);
57 }
58
59 ModuleBase_Operation::~ModuleBase_Operation()
60 {
61   delete myDescription;
62 }
63
64 const QStringList& ModuleBase_Operation::grantedOperationIds() const
65 {
66   return myGrantedIds;
67 }
68
69 void ModuleBase_Operation::setGrantedOperationIds(const QStringList& theList)
70 {
71   myGrantedIds = theList;
72 }
73
74 QString ModuleBase_Operation::id() const
75 {
76   return getDescription()->operationId();
77 }
78
79 bool ModuleBase_Operation::isValid() const
80 {
81   return true;
82 }
83
84 bool ModuleBase_Operation::canBeCommitted() const
85 {
86   return isValid();
87 }
88
89 bool ModuleBase_Operation::start()
90 {
91   myIsModified = false;
92
93   ModelAPI_Session::get()->startOperation(id().toStdString());
94
95   startOperation();
96   emit started();
97
98   return true;
99 }
100
101 void ModuleBase_Operation::postpone()
102 {
103   postponeOperation();
104   emit postponed();
105 }
106
107 void ModuleBase_Operation::resume()
108 {
109   resumeOperation();
110   emit resumed();
111 }
112
113 void ModuleBase_Operation::abort()
114 {
115   // the viewer update should be blocked in order to avoid the features blinking before they are
116   // hidden
117   //std::shared_ptr<Events_Message> aMsg = std::shared_ptr<Events_Message>(
118   //    new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_BLOCKED)));
119   //Events_Loop::loop()->send(aMsg);
120
121   ModelAPI_Session::get()->abortOperation();
122
123   emit stopped();
124   // the viewer update should be unblocked in order to avoid the features blinking before they are
125   // hidden
126   //aMsg = std::shared_ptr<Events_Message>(
127   //              new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_UNBLOCKED)));
128   //Events_Loop::loop()->send(aMsg);
129
130   emit aborted();
131 }
132
133 bool ModuleBase_Operation::commit()
134 {
135   if (canBeCommitted()) {
136     SessionPtr aMgr = ModelAPI_Session::get();
137
138     commitOperation();
139     aMgr->finishOperation();
140
141     stopOperation();
142     emit stopped();
143     emit committed();
144
145     afterCommitOperation();
146     return true;
147   }
148   return false;
149 }
150
151 void ModuleBase_Operation::onValuesChanged()
152 {
153   myIsModified = true;
154 }
155
156 void ModuleBase_Operation::onValueStateChanged(int thePreviousState)
157 {
158   if (propertyPanel()) {
159     ModuleBase_ModelWidget* aWidget = propertyPanel()->activeWidget();
160     if (aWidget) {
161       if (aWidget->getValueState() == ModuleBase_ModelWidget::ModifiedInPP)
162         myIsModified = true;
163     }
164   }
165 }
166
167 void ModuleBase_Operation::setPropertyPanel(ModuleBase_IPropertyPanel* theProp)
168 {
169   myPropertyPanel = theProp;
170 }
171
172 bool ModuleBase_Operation::isGranted(QString theId) const
173 {
174   return myGrantedIds.contains(theId);
175 }