]> SALOME platform Git repositories - modules/shaper.git/blob - src/XGUI/XGUI_OperationMgr.cpp
Salome HOME
88e625e36297aa367d362e2d540796259eb35786
[modules/shaper.git] / src / XGUI / XGUI_OperationMgr.cpp
1 // File:        XGUI_OperationMgr.h
2 // Created:     20 Apr 2014
3 // Author:      Natalia ERMOLAEVA
4
5 #include "XGUI_OperationMgr.h"
6
7 #include "ModuleBase_Operation.h"
8 #include <ModelAPI_Validator.h>
9 #include <ModelAPI_FeatureValidator.h>
10
11 #include <QMessageBox>
12 #include <QApplication>
13 #include <QKeyEvent>
14
15 XGUI_OperationMgr::XGUI_OperationMgr(QObject* theParent)
16     : QObject(theParent)
17 {
18   // listen to Escape signal to stop the current operation
19   qApp->installEventFilter(this);
20 }
21
22 XGUI_OperationMgr::~XGUI_OperationMgr()
23 {
24 }
25
26 ModuleBase_Operation* XGUI_OperationMgr::currentOperation() const
27 {
28   return myOperations.count() > 0 ? myOperations.last() : 0;
29 }
30
31 bool XGUI_OperationMgr::hasOperation() const
32 {
33   return (myOperations.count() > 0) && (myOperations.last() != NULL);
34 }
35
36 int XGUI_OperationMgr::operationsCount() const
37 {
38   return myOperations.count();
39 }
40
41 bool XGUI_OperationMgr::startOperation(ModuleBase_Operation* theOperation)
42 {
43   if (!canStartOperation(theOperation))
44     return false;
45
46   myOperations.append(theOperation);
47
48   connect(theOperation, SIGNAL(stopped()), this, SLOT(onOperationStopped()));
49   connect(theOperation, SIGNAL(started()), this, SIGNAL(operationStarted()));
50   connect(theOperation, SIGNAL(resumed()), this, SIGNAL(operationResumed()));
51   connect(theOperation, SIGNAL(activateNextWidget(ModuleBase_ModelWidget*)), this,
52           SIGNAL(activateNextWidget(ModuleBase_ModelWidget*)));
53
54   theOperation->start();
55   validateCurrentOperation();
56   return true;
57 }
58
59 bool XGUI_OperationMgr::abortOperation()
60 {
61   ModuleBase_Operation* aCurrentOp = currentOperation();
62   if (!aCurrentOp || !canStopOperation())
63     return false;
64
65   aCurrentOp->abort();
66   return true;
67 }
68
69 QStringList XGUI_OperationMgr::operationList()
70 {
71   QStringList result;
72   foreach(ModuleBase_Operation* eachOperation, myOperations)
73   {
74     result << eachOperation->id();
75   }
76   return result;
77 }
78
79 void XGUI_OperationMgr::validateOperation(ModuleBase_Operation* theOperation)
80 {
81   //Get operation Id and feature to validate
82   QString anOperationId = theOperation->id();
83   FeaturePtr aFeature = theOperation->feature();
84   //Get validators for the Id
85   PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
86   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
87
88   bool isValid = aFactory->validate(aFeature);
89   emit operationValidated(isValid);
90 }
91
92 void XGUI_OperationMgr::validateCurrentOperation()
93 {
94   if (!hasOperation())
95     return;
96   ModuleBase_Operation* anOperation = currentOperation();
97   validateOperation(currentOperation());
98 }
99
100 bool XGUI_OperationMgr::eventFilter(QObject *theObject, QEvent *theEvent)
101 {
102   if (theEvent->type() == QEvent::KeyRelease) {
103     QKeyEvent* aKeyEvent = (QKeyEvent*) theEvent;
104     if (aKeyEvent && aKeyEvent->key() == Qt::Key_Escape) {
105       // TODO: this is Escape button processing when the property panel has empty content,
106       // but the operation should be stopped by the Enter has been clicked
107       onKeyReleased("", aKeyEvent);
108       return true;
109     }
110   }
111   return QObject::eventFilter(theObject, theEvent);
112 }
113
114 void XGUI_OperationMgr::resumeOperation(ModuleBase_Operation* theOperation)
115 {
116   theOperation->resume();
117 }
118
119 bool XGUI_OperationMgr::canStartOperation(ModuleBase_Operation* theOperation)
120 {
121   bool aCanStart = true;
122   ModuleBase_Operation* aCurrentOp = currentOperation();
123   if (aCurrentOp) {
124     if (!theOperation->isGranted()) {
125       if (!aCurrentOp->isValid(theOperation)) {
126         if (canStopOperation()) {
127           aCurrentOp->abort();
128         } else {
129           aCanStart = false;
130         }
131       }
132     }
133   }
134   return aCanStart;
135 }
136
137 bool XGUI_OperationMgr::canStopOperation()
138 {
139   ModuleBase_Operation* anOperation = currentOperation();
140   if (anOperation) {
141     if (anOperation->isModified()) {
142       int anAnswer = QMessageBox::question(
143           qApp->activeWindow(), tr("Operation launch"),
144           tr("Previous operation is not finished and will be aborted"), QMessageBox::Ok,
145           QMessageBox::Cancel);
146       return anAnswer == QMessageBox::Ok;
147     }
148   }
149   return true;
150 }
151
152 void XGUI_OperationMgr::onCommitOperation()
153 {
154   ModuleBase_Operation* anOperation = currentOperation();
155   if (anOperation)
156     anOperation->commit();
157 }
158
159 void XGUI_OperationMgr::onAbortOperation()
160 {
161   ModuleBase_Operation* anOperation = currentOperation();
162   if (anOperation && canAbortOperation())
163     anOperation->abort();
164 }
165
166 bool XGUI_OperationMgr::canAbortOperation()
167 {
168   ModuleBase_Operation* anOperation = currentOperation();
169   if (anOperation && anOperation->isModified()) {
170     int anAnswer = QMessageBox::question(
171         qApp->activeWindow(), tr("Cancel operation"),
172         tr("Operation %1 will be cancelled. Continue?").arg(anOperation->id()), QMessageBox::Yes,
173         QMessageBox::No);
174     return anAnswer == QMessageBox::Yes;
175   }
176   return true;
177 }
178
179 void XGUI_OperationMgr::onOperationStopped()
180 {
181   ModuleBase_Operation* aSenderOperation = dynamic_cast<ModuleBase_Operation*>(sender());
182   ModuleBase_Operation* anOperation = currentOperation();
183   if (!aSenderOperation || !anOperation || aSenderOperation != anOperation)
184     return;
185
186   myOperations.removeAll(anOperation);
187   anOperation->deleteLater();
188
189   emit operationStopped(anOperation);
190
191   // get last operation which can be resumed
192   ModuleBase_Operation* aResultOp = 0;
193   QListIterator<ModuleBase_Operation*> anIt(myOperations);
194   anIt.toBack();
195   while (anIt.hasPrevious()) {
196     ModuleBase_Operation* anOp = anIt.previous();
197     if (anOp) {
198       aResultOp = anOp;
199       break;
200     }
201   }
202   if (aResultOp) {
203     resumeOperation(aResultOp);
204     validateCurrentOperation();
205   }
206 }
207
208 void XGUI_OperationMgr::onKeyReleased(const std::string& theName, QKeyEvent* theEvent)
209 {
210   ModuleBase_Operation* anOperation = currentOperation();
211   if (anOperation)
212     anOperation->keyReleased(theName, theEvent);
213 }
214
215 void XGUI_OperationMgr::onWidgetActivated(ModuleBase_ModelWidget* theWidget)
216 {
217   ModuleBase_Operation* anOperation = currentOperation();
218   if (anOperation)
219     anOperation->onWidgetActivated(theWidget);
220 }