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