Salome HOME
Processing of complex objects in data tree
[modules/shaper.git] / src / XGUI / XGUI_WorkshopListener.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
2
3 #include "XGUI_WorkshopListener.h"
4 #include "XGUI_Workshop.h"
5 #include "XGUI_Displayer.h"
6 #include "XGUI_OperationMgr.h"
7 #include "XGUI_SalomeConnector.h"
8 #include "XGUI_ActionsMgr.h"
9 #include "XGUI_PropertyPanel.h"
10 #include "XGUI_ModuleConnector.h"
11 #include "XGUI_QtEvents.h"
12
13 #include <AppElements_Workbench.h>
14 #include <AppElements_Command.h>
15 #include <AppElements_MainMenu.h>
16 #include <AppElements_MainWindow.h>
17 #include <AppElements_MenuGroupPanel.h>
18 #include <AppElements_Button.h>
19
20 #include <ModuleBase_IModule.h>
21
22 #include <ModelAPI_Object.h>
23 #include <ModelAPI_Events.h>
24 #include <ModelAPI_Session.h>
25 #include <ModelAPI_Result.h>
26 #include <ModelAPI_Feature.h>
27 #include <ModelAPI_Data.h>
28 #include <ModelAPI_ResultBody.h>
29 #include <ModelAPI_ResultCompSolid.h>
30
31 #include <Events_Loop.h>
32 #include <Events_Error.h>
33 #include <Events_LongOp.h>
34
35 #include <ModuleBase_IWorkshop.h>
36
37 #include <ModuleBase_Operation.h>
38 #include <ModuleBase_OperationDescription.h>
39 #include <ModuleBase_Tools.h>
40 #include <ModuleBase_IViewer.h>
41 #include <ModuleBase_FilterFactory.h>
42
43 #include <Config_FeatureMessage.h>
44 #include <Config_PointerMessage.h>
45 #include <Config_SelectionFilterMessage.h>
46
47 #include <QApplication>
48 #include <QMainWindow>
49 #include <QThread>
50 #include <QAction>
51
52 #ifdef _DEBUG
53 #include <QDebug>
54 #include <iostream>
55 #endif
56
57 //#define DEBUG_CANDISPLAY
58 //#define DEBUG_FEATURE_CREATED
59 //#define DEBUG_FEATURE_REDISPLAY
60
61 XGUI_WorkshopListener::XGUI_WorkshopListener(ModuleBase_IWorkshop* theWorkshop)
62   : myWorkshop(theWorkshop),
63     myUpdatePrefs(false)
64 {
65 }
66
67 //******************************************************
68 XGUI_WorkshopListener::~XGUI_WorkshopListener(void)
69 {
70 }
71
72 //******************************************************
73 void XGUI_WorkshopListener::initializeEventListening()
74 {
75   //Initialize event listening
76   Events_Loop* aLoop = Events_Loop::loop();
77   aLoop->registerListener(this, Events_Error::errorID());  //!< Listening application errors.
78   aLoop->registerListener(this, Events_Loop::eventByName(Config_FeatureMessage::GUI_EVENT()));
79   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OPERATION_LAUNCHED));
80   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
81   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_CREATED));
82   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
83   aLoop->registerListener(this, Events_LongOp::eventID());
84   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_PLUGIN_LOADED));
85   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_SELFILTER_LOADED));
86
87   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_UPDATE_VIEWER_BLOCKED));
88   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_UPDATE_VIEWER_UNBLOCKED));
89 }
90
91 //******************************************************
92 void XGUI_WorkshopListener::processEvent(const std::shared_ptr<Events_Message>& theMessage)
93 {
94   if (QApplication::instance()->thread() != QThread::currentThread()) {
95     #ifdef _DEBUG
96     std::cout << "XGUI_Workshop::processEvent: " << "Working in another thread." << std::endl;
97     #endif
98     SessionPtr aMgr = ModelAPI_Session::get();
99     PostponeMessageQtEvent* aPostponeEvent = new PostponeMessageQtEvent(theMessage);
100     QApplication::postEvent(this, aPostponeEvent);
101     return;
102   }
103
104   //A message to start feature creation received.
105   if (theMessage->eventID() == Events_Loop::loop()->eventByName(Config_FeatureMessage::GUI_EVENT())) {
106     std::shared_ptr<Config_FeatureMessage> aFeatureMsg =
107        std::dynamic_pointer_cast<Config_FeatureMessage>(theMessage);
108     if (!aFeatureMsg->isInternal()) {
109       addFeature(aFeatureMsg);
110     }
111   }
112   // Process creation of Part
113   else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_CREATED)) {
114     std::shared_ptr<ModelAPI_ObjectUpdatedMessage> aUpdMsg =
115         std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
116     onFeatureCreatedMsg(aUpdMsg);
117     if (myUpdatePrefs) {
118       XGUI_SalomeConnector* aSalomeConnector = workshop()->salomeConnector();
119       if (aSalomeConnector)
120         aSalomeConnector->createPreferences();
121       myUpdatePrefs = false;
122     }
123   }
124   else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_PLUGIN_LOADED)) {
125     myUpdatePrefs = true;
126   }
127   // Redisplay feature
128   else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY)) {
129     std::shared_ptr<ModelAPI_ObjectUpdatedMessage> aUpdMsg =
130         std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
131     onFeatureRedisplayMsg(aUpdMsg);
132   }
133   //Update property panel on corresponding message. If there is no current operation (no
134   //property panel), or received message has different feature to the current - do nothing.
135   else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_UPDATED)) {
136     std::shared_ptr<ModelAPI_ObjectUpdatedMessage> anUpdateMsg =
137         std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
138     onFeatureUpdatedMsg(anUpdateMsg);
139   } else if (theMessage->eventID() == Events_LongOp::eventID()) {
140     if (Events_LongOp::isPerformed()) {
141       QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
142     } else {
143       QApplication::restoreOverrideCursor();
144     }
145   }
146   //An operation passed by message. Start it, process and commit.
147   else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OPERATION_LAUNCHED)) {
148     std::shared_ptr<Config_PointerMessage> aPartSetMsg =
149         std::dynamic_pointer_cast<Config_PointerMessage>(theMessage);
150     //myPropertyPanel->cleanContent();
151     ModuleBase_Operation* anOperation = (ModuleBase_Operation*) aPartSetMsg->pointer();
152     XGUI_OperationMgr* anOperationMgr = workshop()->operationMgr();
153
154     if (anOperationMgr->startOperation(anOperation)) {
155       workshop()->propertyPanel()->updateContentWidget(anOperation->feature());
156       if (!anOperation->getDescription()->hasXmlRepresentation()) {
157         if (anOperation->commit())
158           workshop()->updateCommandStatus();
159       }
160     }
161   } 
162   else if (theMessage->eventID() == Events_Loop::eventByName(EVENT_SELFILTER_LOADED)) {
163     std::shared_ptr<Config_SelectionFilterMessage> aMsg = 
164       std::dynamic_pointer_cast<Config_SelectionFilterMessage>(theMessage);
165     if (aMsg) {
166       ModuleBase_FilterFactory* aFactory = myWorkshop->selectionFilters();
167       if (!aMsg->attributeId().empty()) {
168         aFactory->assignFilter(aMsg->selectionFilterId(), aMsg->featureId(), aMsg->attributeId(),
169                                aMsg->parameters());
170       }
171     }
172   } else if (theMessage->eventID() == Events_Loop::eventByName(EVENT_UPDATE_VIEWER_BLOCKED)) {
173     // the viewer's update context will not happens until viewer updated is emitted
174       workshop()->displayer()->enableUpdateViewer(false);
175   } else if (theMessage->eventID() == Events_Loop::eventByName(EVENT_UPDATE_VIEWER_UNBLOCKED)) {
176     // the viewer's update context is unblocked, the viewer's update works
177     XGUI_Displayer* aDisplayer = workshop()->displayer();
178     aDisplayer->enableUpdateViewer(true);
179     aDisplayer->updateViewer();
180   } else {
181     //Show error dialog if error message received.
182     std::shared_ptr<Events_Error> anAppError = std::dynamic_pointer_cast<Events_Error>(theMessage);
183     if (anAppError) {
184       emit errorOccurred(QString::fromLatin1(anAppError->description()));
185     }
186     return;
187   }
188   if (!workshop()->isSalomeMode()) {
189     SessionPtr aMgr = ModelAPI_Session::get();
190     AppElements_MainWindow* aMainWindow = workshop()->mainWindow();
191     if (aMgr->isModified() != aMainWindow->isModifiedState())
192       aMainWindow->setModifiedState(aMgr->isModified());
193   }
194 }
195
196 //******************************************************
197 void XGUI_WorkshopListener::onFeatureUpdatedMsg(const std::shared_ptr<ModelAPI_ObjectUpdatedMessage>& theMsg)
198 {
199   std::set<ObjectPtr> aFeatures = theMsg->objects();
200   XGUI_OperationMgr* anOperationMgr = workshop()->operationMgr();
201   if (anOperationMgr->hasOperation()) {
202     FeaturePtr aCurrentFeature = anOperationMgr->currentOperation()->feature();
203     std::set<ObjectPtr>::const_iterator aIt;
204     for (aIt = aFeatures.begin(); aIt != aFeatures.end(); ++aIt) {
205       ObjectPtr aNewFeature = (*aIt);
206       if (aNewFeature == aCurrentFeature) {
207         workshop()->propertyPanel()->updateContentWidget(aCurrentFeature);
208         break;
209       }
210     }
211   }
212   anOperationMgr->onValidateOperation();
213   //if (myObjectBrowser)
214   //  myObjectBrowser->processEvent(theMsg);
215 }
216
217 //******************************************************
218 void XGUI_WorkshopListener::onFeatureRedisplayMsg(const std::shared_ptr<ModelAPI_ObjectUpdatedMessage>& theMsg)
219 {
220   std::set<ObjectPtr> aObjects = theMsg->objects();
221   std::set<ObjectPtr>::const_iterator aIt;
222
223 #ifdef DEBUG_FEATURE_REDISPLAY
224   QStringList anInfo;
225   for (aIt = aObjects.begin(); aIt != aObjects.end(); ++aIt) {
226     anInfo.append(ModuleBase_Tools::objectInfo((*aIt)));
227   }
228   QString anInfoStr = anInfo.join(", ");
229   qDebug(QString("onFeatureRedisplayMsg: %1, %2").arg(aObjects.size()).arg(anInfoStr).toStdString().c_str());
230 #endif
231
232   XGUI_Workshop* aWorkshop = workshop();
233   XGUI_Displayer* aDisplayer = aWorkshop->displayer();
234   for (aIt = aObjects.begin(); aIt != aObjects.end(); ++aIt) {
235     ObjectPtr aObj = (*aIt);
236
237     // Hide the object if it is invalid or concealed one
238     bool aHide = !aObj->data() || !aObj->data()->isValid() || 
239       aObj->isDisabled() || (!aObj->isDisplayed());
240     if (!aHide) { // check that this is not hidden result
241       ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(aObj);
242       aHide = aRes && aRes->isConcealed();
243     }
244     if (aHide) {
245       aDisplayer->erase(aObj, false);
246       #ifdef DEBUG_FEATURE_REDISPLAY
247         // Redisplay the visible object or the object of the current operation
248         bool isVisibleObject = aDisplayer->isVisible(aObj);
249
250         QString anObjInfo = ModuleBase_Tools::objectInfo((aObj));
251         qDebug(QString("visible=%1 : erase  = %2").arg(isVisibleObject).arg(anObjInfo).toStdString().c_str());
252       #endif
253     }
254     else {
255       // Redisplay the visible object or the object of the current operation
256       bool isVisibleObject = aDisplayer->isVisible(aObj);
257       #ifdef DEBUG_FEATURE_REDISPLAY
258         QString anObjInfo = ModuleBase_Tools::objectInfo((aObj));
259         qDebug(QString("visible=%1 : display= %2").arg(isVisibleObject).arg(anObjInfo).toStdString().c_str());
260         /*FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
261         if (aFeature.get()) {
262           std::string aKind = aFeature->getKind();
263           if (aKind == "SketchMultiRotation")
264             bool aValue = true;
265         }*/
266       #endif
267
268       if (isVisibleObject)  { // redisplay visible object
269         //displayObject(aObj);  // In order to update presentation
270         // in order to avoid the check whether the object can be redisplayed, the exact method
271         // of redisplay is called. This modification is made in order to have the line is updated
272         // by creation of a horizontal constraint on the line by preselection
273         /*ResultCompSolidPtr aCompSolid = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>
274                                                                                    (aObj);
275         if (aCompSolid.get() && aCompSolid->numberOfSubs() > 0) {
276           aDisplayer->erase(aObj, false);
277         }*/
278
279         aDisplayer->redisplay(aObj, false);
280         // Deactivate object of current operation from selection
281         aWorkshop->deactivateActiveObject(aObj, false);
282       } else { // display object if the current operation has it
283         if (displayObject(aObj)) {
284           // Deactivate object of current operation from selection
285           aWorkshop->deactivateActiveObject(aObj, false);
286         }
287       }
288     }
289   }
290   aDisplayer->updateViewer();
291 }
292 //******************************************************
293 void XGUI_WorkshopListener::onFeatureCreatedMsg(const std::shared_ptr<ModelAPI_ObjectUpdatedMessage>& theMsg)
294 {
295   std::set<ObjectPtr> aObjects = theMsg->objects();
296   std::set<ObjectPtr>::const_iterator aIt;
297 #ifdef DEBUG_FEATURE_CREATED
298   QStringList anInfo;
299   for (aIt = aObjects.begin(); aIt != aObjects.end(); ++aIt) {
300     anInfo.append(ModuleBase_Tools::objectInfo((*aIt)));
301   }
302   QString anInfoStr = anInfo.join(", ");
303   qDebug(QString("onFeatureCreatedMsg: %1, %2").arg(aObjects.size()).arg(anInfoStr).toStdString().c_str());
304 #endif
305
306   //bool aHasPart = false;
307   bool isDisplayed = false;
308   for (aIt = aObjects.begin(); aIt != aObjects.end(); ++aIt) {
309     ObjectPtr anObject = *aIt;
310     // the validity of the data should be checked here in order to avoid display of the objects,
311     // which were created, then deleted, but flush for the creation event happens after that
312     // we should not display disabled objects
313     bool aHide = !anObject->data()->isValid() || 
314                  anObject->isDisabled() ||
315                  !anObject->isDisplayed();
316     if (!aHide) {
317       // setDisplayed has to be called in order to synchronize internal state of the object 
318       // with list of displayed objects
319 #ifdef DEBUG_CANDISPLAY
320       if (displayObject(anObject)/*myWorkshop->module()->canDisplayObject(anObject)*/) {
321         anObject->setDisplayed(true);
322         //isDisplayed = displayObject(anObject);
323 #else
324       if (myWorkshop->module()->canDisplayObject(anObject)) {
325         anObject->setDisplayed(true);
326         isDisplayed = displayObject(anObject);
327 #endif
328       } else 
329         anObject->setDisplayed(false);
330     }
331   }
332   //if (myObjectBrowser)
333   //  myObjectBrowser->processEvent(theMsg);
334   if (isDisplayed)
335     workshop()->displayer()->updateViewer();
336   //if (aHasPart) { // TODO: Avoid activate last part on loading of document
337   //  activateLastPart();
338   //}
339 }
340
341 bool XGUI_WorkshopListener::event(QEvent * theEvent)
342 {
343   PostponeMessageQtEvent* aPostponedEv = dynamic_cast<PostponeMessageQtEvent*>(theEvent);
344   if (aPostponedEv) {
345     std::shared_ptr<Events_Message> aEventPtr = aPostponedEv->postponedMessage();
346     processEvent(aEventPtr);
347     return true;
348   }
349   return false;
350 }
351
352 void XGUI_WorkshopListener::addFeature(const std::shared_ptr<Config_FeatureMessage>& theMessage)
353 {
354   if (!theMessage) {
355 #ifdef _DEBUG
356     qDebug() << "XGUI_WorkshopListener::addFeature: NULL message.";
357 #endif
358     return;
359   }
360   ActionInfo aFeatureInfo;
361   aFeatureInfo.initFrom(theMessage);
362
363   XGUI_Workshop* aWorkshop = workshop();
364
365   QString aWchName = QString::fromStdString(theMessage->workbenchId());
366   QStringList aNestedFeatures =
367       QString::fromStdString(theMessage->nestedFeatures()).split(" ", QString::SkipEmptyParts);
368   QString aDocKind = QString::fromStdString(theMessage->documentKind());
369   QList<QAction*> aNestedActList;
370   bool isColumnButton = !aNestedFeatures.isEmpty();
371   if (isColumnButton) {
372     QString aNestedActions = QString::fromStdString(theMessage->actionsWhenNested());
373     XGUI_OperationMgr* anOperationMgr = aWorkshop->operationMgr();
374     XGUI_ActionsMgr* anActionsMgr = aWorkshop->actionsMgr();
375     if (aNestedActions.contains("accept")) {
376       QAction* anAction = anActionsMgr->operationStateAction(XGUI_ActionsMgr::AcceptAll, NULL);
377       connect(anAction, SIGNAL(triggered()), anOperationMgr, SLOT(commitAllOperations()));
378       aNestedActList << anAction;
379     }
380     if (aNestedActions.contains("abort")) {
381       QAction* anAction = anActionsMgr->operationStateAction(XGUI_ActionsMgr::AbortAll, NULL);
382       connect(anAction, SIGNAL(triggered()), anOperationMgr, SLOT(abortAllOperations()));
383       aNestedActList << anAction;
384     }
385   }
386
387   if (aWorkshop->isSalomeMode()) {
388     XGUI_SalomeConnector* aSalomeConnector = aWorkshop->salomeConnector();
389     QAction* aAction;
390     if (isColumnButton) {
391       aAction = aSalomeConnector->addNestedFeature(aWchName, aFeatureInfo, aNestedActList);
392     } else {
393       //Issue #650: in the SALOME mode the tooltip should be same as text
394       aFeatureInfo.toolTip = aFeatureInfo.text;
395       aAction = aSalomeConnector->addFeature(aWchName, aFeatureInfo);
396     }
397     aSalomeConnector->setNestedActions(aFeatureInfo.id, aNestedFeatures);
398     aSalomeConnector->setDocumentKind(aFeatureInfo.id, aDocKind);
399
400     aWorkshop->actionsMgr()->addCommand(aAction);
401     aWorkshop->module()->actionCreated(aAction);
402   } else {
403     //Find or create Workbench
404     AppElements_MainMenu* aMenuBar = aWorkshop->mainWindow()->menuObject();
405     AppElements_Workbench* aPage = aMenuBar->findWorkbench(aWchName);
406     if (!aPage) {
407       aPage = aWorkshop->addWorkbench(aWchName);
408     }
409     //Find or create Group
410     QString aGroupName = QString::fromStdString(theMessage->groupId());
411     AppElements_MenuGroupPanel* aGroup = aPage->findGroup(aGroupName);
412     if (!aGroup) {
413       aGroup = aPage->addGroup(aGroupName);
414     }
415     // Check if hotkey sequence is already defined:
416     XGUI_ActionsMgr* anActionsMgr = aWorkshop->actionsMgr();
417     QKeySequence aHotKey = anActionsMgr->registerShortcut(aFeatureInfo.shortcut);
418     if(aHotKey != aFeatureInfo.shortcut) {
419       aFeatureInfo.shortcut = aHotKey;
420     }
421     // Create feature...
422     AppElements_Command* aCommand = aGroup->addFeature(aFeatureInfo,
423                                                        aDocKind,
424                                                        aNestedFeatures);
425     // Enrich created button with accept/abort buttons if necessary
426     AppElements_Button* aButton = aCommand->button();
427     if (aButton->isColumnButton()) {
428       aButton->setAdditionalButtons(aNestedActList);
429     }
430     aWorkshop->actionsMgr()->addCommand(aCommand);
431     aWorkshop->module()->actionCreated(aCommand);
432   }
433 }
434
435
436 //**************************************************************
437 bool XGUI_WorkshopListener::displayObject(ObjectPtr theObj)
438 {
439    XGUI_Workshop* aWorkshop = workshop();
440   if (!aWorkshop->module()->canDisplayObject(theObj))
441     return false;
442
443   XGUI_Displayer* aDisplayer = aWorkshop->displayer();
444   ResultBodyPtr aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(theObj);
445   if (aBody.get() != NULL) {
446     int aNb = aDisplayer->objectsCount();
447     aDisplayer->display(theObj, false);
448     if (aNb == 0)
449       myWorkshop->viewer()->fitAll();
450   } else 
451     aDisplayer->display(theObj, false);
452
453   return true;
454 }
455
456 XGUI_Workshop* XGUI_WorkshopListener::workshop() const
457 {
458   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
459   return aConnector->workshop();
460 }