1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
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"
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>
20 #include <ModuleBase_IModule.h>
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 #include <ModelAPI_Tools.h>
32 #include <Events_Loop.h>
33 #include <Events_Error.h>
34 #include <Events_LongOp.h>
36 #include <ModuleBase_IWorkshop.h>
38 #include <ModuleBase_Operation.h>
39 #include <ModuleBase_OperationDescription.h>
40 #include <ModuleBase_Tools.h>
41 #include <ModuleBase_IViewer.h>
42 #include <ModuleBase_FilterFactory.h>
44 #include <Config_FeatureMessage.h>
45 #include <Config_PointerMessage.h>
46 #include <Config_SelectionFilterMessage.h>
47 #include <Config_Keywords.h>
49 #include <QApplication>
50 #include <QMainWindow>
59 //#define DEBUG_FEATURE_CREATED
60 //#define DEBUG_FEATURE_REDISPLAY
61 //#define DEBUG_RESULT_COMPSOLID
63 XGUI_WorkshopListener::XGUI_WorkshopListener(ModuleBase_IWorkshop* theWorkshop)
64 : myWorkshop(theWorkshop),
69 //******************************************************
70 XGUI_WorkshopListener::~XGUI_WorkshopListener(void)
74 //******************************************************
75 void XGUI_WorkshopListener::initializeEventListening()
77 //Initialize event listening
78 Events_Loop* aLoop = Events_Loop::loop();
79 aLoop->registerListener(this, Events_Error::errorID()); //!< Listening application errors.
80 aLoop->registerListener(this, Events_Loop::eventByName(Config_FeatureMessage::GUI_EVENT()));
81 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OPERATION_LAUNCHED));
82 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
83 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_CREATED));
84 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
85 aLoop->registerListener(this, Events_LongOp::eventID());
86 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_PLUGIN_LOADED));
87 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_SELFILTER_LOADED));
89 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_UPDATE_VIEWER_BLOCKED));
90 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_UPDATE_VIEWER_UNBLOCKED));
93 //******************************************************
94 void XGUI_WorkshopListener::processEvent(const std::shared_ptr<Events_Message>& theMessage)
96 if (QApplication::instance()->thread() != QThread::currentThread()) {
98 std::cout << "XGUI_Workshop::processEvent: " << "Working in another thread." << std::endl;
100 SessionPtr aMgr = ModelAPI_Session::get();
101 PostponeMessageQtEvent* aPostponeEvent = new PostponeMessageQtEvent(theMessage);
102 QApplication::postEvent(this, aPostponeEvent);
106 //A message to start feature creation received.
107 if (theMessage->eventID() == Events_Loop::loop()->eventByName(Config_FeatureMessage::GUI_EVENT())) {
108 std::shared_ptr<Config_FeatureMessage> aFeatureMsg =
109 std::dynamic_pointer_cast<Config_FeatureMessage>(theMessage);
110 if (!aFeatureMsg->isInternal()) {
111 addFeature(aFeatureMsg);
114 // Process creation of Part
115 else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_CREATED)) {
116 std::shared_ptr<ModelAPI_ObjectUpdatedMessage> aUpdMsg =
117 std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
118 onFeatureCreatedMsg(aUpdMsg);
120 XGUI_SalomeConnector* aSalomeConnector = workshop()->salomeConnector();
121 if (aSalomeConnector)
122 aSalomeConnector->createPreferences();
123 myUpdatePrefs = false;
126 else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_PLUGIN_LOADED)) {
127 myUpdatePrefs = true;
130 else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY)) {
131 std::shared_ptr<ModelAPI_ObjectUpdatedMessage> aUpdMsg =
132 std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
133 onFeatureRedisplayMsg(aUpdMsg);
135 //Update property panel on corresponding message. If there is no current operation (no
136 //property panel), or received message has different feature to the current - do nothing.
137 else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_UPDATED)) {
138 std::shared_ptr<ModelAPI_ObjectUpdatedMessage> anUpdateMsg =
139 std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
140 onFeatureUpdatedMsg(anUpdateMsg);
141 } else if (theMessage->eventID() == Events_LongOp::eventID()) {
142 if (Events_LongOp::isPerformed()) {
143 QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
145 QApplication::restoreOverrideCursor();
148 //An operation passed by message. Start it, process and commit.
149 else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OPERATION_LAUNCHED)) {
150 std::shared_ptr<Config_PointerMessage> aPartSetMsg =
151 std::dynamic_pointer_cast<Config_PointerMessage>(theMessage);
152 //myPropertyPanel->cleanContent();
153 ModuleBase_Operation* anOperation = (ModuleBase_Operation*) aPartSetMsg->pointer();
154 XGUI_OperationMgr* anOperationMgr = workshop()->operationMgr();
156 if (anOperationMgr->startOperation(anOperation)) {
157 workshop()->propertyPanel()->updateContentWidget(anOperation->feature());
158 if (!anOperation->getDescription()->hasXmlRepresentation()) {
159 if (anOperation->commit())
160 workshop()->updateCommandStatus();
164 else if (theMessage->eventID() == Events_Loop::eventByName(EVENT_SELFILTER_LOADED)) {
165 std::shared_ptr<Config_SelectionFilterMessage> aMsg =
166 std::dynamic_pointer_cast<Config_SelectionFilterMessage>(theMessage);
168 ModuleBase_FilterFactory* aFactory = myWorkshop->selectionFilters();
169 if (!aMsg->attributeId().empty()) {
170 aFactory->assignFilter(aMsg->selectionFilterId(), aMsg->featureId(), aMsg->attributeId(),
174 } else if (theMessage->eventID() == Events_Loop::eventByName(EVENT_UPDATE_VIEWER_BLOCKED)) {
175 // the viewer's update context will not happens until viewer updated is emitted
176 workshop()->displayer()->enableUpdateViewer(false);
177 } else if (theMessage->eventID() == Events_Loop::eventByName(EVENT_UPDATE_VIEWER_UNBLOCKED)) {
178 // the viewer's update context is unblocked, the viewer's update works
179 XGUI_Displayer* aDisplayer = workshop()->displayer();
180 aDisplayer->enableUpdateViewer(true);
181 aDisplayer->updateViewer();
183 //Show error dialog if error message received.
184 std::shared_ptr<Events_Error> anAppError = std::dynamic_pointer_cast<Events_Error>(theMessage);
186 emit errorOccurred(QString::fromLatin1(anAppError->description()));
190 if (!workshop()->isSalomeMode()) {
191 SessionPtr aMgr = ModelAPI_Session::get();
192 AppElements_MainWindow* aMainWindow = workshop()->mainWindow();
193 if (aMgr->isModified() != aMainWindow->isModifiedState())
194 aMainWindow->setModifiedState(aMgr->isModified());
198 //******************************************************
199 void XGUI_WorkshopListener::onFeatureUpdatedMsg(const std::shared_ptr<ModelAPI_ObjectUpdatedMessage>& theMsg)
201 std::set<ObjectPtr> aFeatures = theMsg->objects();
202 XGUI_OperationMgr* anOperationMgr = workshop()->operationMgr();
203 if (anOperationMgr->hasOperation()) {
204 FeaturePtr aCurrentFeature = anOperationMgr->currentOperation()->feature();
205 std::set<ObjectPtr>::const_iterator aIt;
206 for (aIt = aFeatures.begin(); aIt != aFeatures.end(); ++aIt) {
207 ObjectPtr aNewFeature = (*aIt);
208 if (aNewFeature == aCurrentFeature) {
209 workshop()->propertyPanel()->updateContentWidget(aCurrentFeature);
214 anOperationMgr->onValidateOperation();
215 //if (myObjectBrowser)
216 // myObjectBrowser->processEvent(theMsg);
219 //******************************************************
220 void XGUI_WorkshopListener::onFeatureRedisplayMsg(const std::shared_ptr<ModelAPI_ObjectUpdatedMessage>& theMsg)
222 std::set<ObjectPtr> aObjects = theMsg->objects();
223 std::set<ObjectPtr>::const_iterator aIt;
225 #ifdef DEBUG_FEATURE_REDISPLAY
227 for (aIt = aObjects.begin(); aIt != aObjects.end(); ++aIt) {
228 anInfo.append(ModuleBase_Tools::objectInfo((*aIt)));
230 QString anInfoStr = anInfo.join(";\t");
231 qDebug(QString("onFeatureRedisplayMsg: %1, %2").arg(aObjects.size()).arg(anInfoStr).toStdString().c_str());
234 XGUI_Workshop* aWorkshop = workshop();
235 XGUI_Displayer* aDisplayer = aWorkshop->displayer();
236 for (aIt = aObjects.begin(); aIt != aObjects.end(); ++aIt) {
237 ObjectPtr aObj = (*aIt);
239 // Hide the object if it is invalid or concealed one
240 bool aHide = !aObj->data() || !aObj->data()->isValid() ||
241 aObj->isDisabled() || (!aObj->isDisplayed());
242 if (!aHide) { // check that this is not hidden result
243 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(aObj);
244 aHide = aRes && aRes->isConcealed();
246 #ifdef DEBUG_RESULT_COMPSOLID
247 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(aObj);
249 ResultCompSolidPtr aCompSolidRes = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(aRes);
250 if (aCompSolidRes.get()) {
251 qDebug(QString("COMPSOLID, numberOfSubs = %1").arg(aCompSolidRes->numberOfSubs()).toStdString().c_str());
253 if (ModelAPI_Tools::compSolidOwner(aRes))
254 qDebug("COMPSOLID sub-object");
258 aDisplayer->erase(aObj, false);
259 #ifdef DEBUG_FEATURE_REDISPLAY
260 // Redisplay the visible object or the object of the current operation
261 bool isVisibleObject = aDisplayer->isVisible(aObj);
263 QString anObjInfo = ModuleBase_Tools::objectInfo((aObj));
264 //qDebug(QString("visible=%1 : erase = %2").arg(isVisibleObject).arg(anObjInfo).toStdString().c_str());
268 // Redisplay the visible object or the object of the current operation
269 bool isVisibleObject = aDisplayer->isVisible(aObj);
270 #ifdef DEBUG_FEATURE_REDISPLAY
271 QString anObjInfo = ModuleBase_Tools::objectInfo((aObj));
272 //qDebug(QString("visible=%1 : display= %2").arg(isVisibleObject).arg(anObjInfo).toStdString().c_str());
273 /*FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
274 if (aFeature.get()) {
275 std::string aKind = aFeature->getKind();
276 if (aKind == "SketchMultiRotation")
281 if (isVisibleObject) { // redisplay visible object
282 //displayObject(aObj); // In order to update presentation
283 // in order to avoid the check whether the object can be redisplayed, the exact method
284 // of redisplay is called. This modification is made in order to have the line is updated
285 // by creation of a horizontal constraint on the line by preselection
286 if (ModelAPI_Tools::hasSubResults(std::dynamic_pointer_cast<ModelAPI_Result>(aObj))) {
287 aDisplayer->erase(aObj, false);
290 aDisplayer->redisplay(aObj, false);
291 // Deactivate object of current operation from selection
292 aWorkshop->deactivateActiveObject(aObj, false);
294 } else { // display object if the current operation has it
295 if (displayObject(aObj)) {
296 // Deactivate object of current operation from selection
297 aWorkshop->deactivateActiveObject(aObj, false);
302 aDisplayer->updateViewer();
304 //******************************************************
305 void XGUI_WorkshopListener::onFeatureCreatedMsg(const std::shared_ptr<ModelAPI_ObjectUpdatedMessage>& theMsg)
307 std::set<ObjectPtr> aObjects = theMsg->objects();
308 std::set<ObjectPtr>::const_iterator aIt;
309 #ifdef DEBUG_FEATURE_CREATED
311 for (aIt = aObjects.begin(); aIt != aObjects.end(); ++aIt) {
312 anInfo.append(ModuleBase_Tools::objectInfo((*aIt)));
314 QString anInfoStr = anInfo.join(";\t");
315 qDebug(QString("onFeatureCreatedMsg: %1, %2").arg(aObjects.size()).arg(anInfoStr).toStdString().c_str());
318 //bool aHasPart = false;
319 bool isDisplayed = false;
320 for (aIt = aObjects.begin(); aIt != aObjects.end(); ++aIt) {
321 ObjectPtr anObject = *aIt;
323 #ifdef DEBUG_RESULT_COMPSOLID
324 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
326 ResultCompSolidPtr aCompSolidRes = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(aRes);
327 if (aCompSolidRes.get()) {
328 qDebug(QString("COMPSOLID, numberOfSubs = %1").arg(aCompSolidRes->numberOfSubs()).toStdString().c_str());
330 if (ModelAPI_Tools::compSolidOwner(aRes))
331 qDebug("COMPSOLID sub-object");
334 // the validity of the data should be checked here in order to avoid display of the objects,
335 // which were created, then deleted, but flush for the creation event happens after that
336 // we should not display disabled objects
337 bool aHide = !anObject->data()->isValid() ||
338 anObject->isDisabled() ||
339 !anObject->isDisplayed();
341 // setDisplayed has to be called in order to synchronize internal state of the object
342 // with list of displayed objects
343 if (myWorkshop->module()->canDisplayObject(anObject)) {
344 anObject->setDisplayed(true);
345 isDisplayed = displayObject(*aIt);
347 anObject->setDisplayed(false);
350 //if (myObjectBrowser)
351 // myObjectBrowser->processEvent(theMsg);
353 workshop()->displayer()->updateViewer();
354 //if (aHasPart) { // TODO: Avoid activate last part on loading of document
355 // activateLastPart();
359 void XGUI_WorkshopListener::onNestedStateChanged(const std::string& theFeatureId, const bool theState)
361 XGUI_Workshop* aWorkshop = workshop();
363 //one button is used for all features, which can have nested actions, so it is obtained from
364 // the action manager
365 QAction* anAcceptAllAction = aWorkshop->actionsMgr()->operationStateAction(XGUI_ActionsMgr::AcceptAll, NULL);
366 if (aWorkshop->isSalomeMode()) {
367 XGUI_SalomeConnector* aSalomeConnector = aWorkshop->salomeConnector();
368 XGUI_ActionsMgr* anActionsMgr = aWorkshop->actionsMgr();
369 if (aSalomeConnector->isNestedFeature(anActionsMgr->action(theFeatureId.c_str())))
370 anAcceptAllAction->setEnabled(theState);
372 AppElements_MainMenu* aMenuBar = aWorkshop->mainWindow()->menuObject();
373 if (aMenuBar->feature(theFeatureId.c_str())->button()->additionalButtonWidget())
374 anAcceptAllAction->setEnabled(theState);
378 bool XGUI_WorkshopListener::event(QEvent * theEvent)
380 PostponeMessageQtEvent* aPostponedEv = dynamic_cast<PostponeMessageQtEvent*>(theEvent);
382 std::shared_ptr<Events_Message> aEventPtr = aPostponedEv->postponedMessage();
383 processEvent(aEventPtr);
389 void XGUI_WorkshopListener::addFeature(const std::shared_ptr<Config_FeatureMessage>& theMessage)
393 qDebug() << "XGUI_WorkshopListener::addFeature: NULL message.";
397 ActionInfo aFeatureInfo;
398 aFeatureInfo.initFrom(theMessage);
400 XGUI_Workshop* aWorkshop = workshop();
402 QString aWchName = QString::fromStdString(theMessage->workbenchId());
403 QStringList aNestedFeatures =
404 QString::fromStdString(theMessage->nestedFeatures()).split(" ", QString::SkipEmptyParts);
405 QString aDocKind = QString::fromStdString(theMessage->documentKind());
406 QList<QAction*> aNestedActList;
407 bool isColumnButton = !aNestedFeatures.isEmpty();
408 if (isColumnButton) {
409 QString aNestedActions = QString::fromStdString(theMessage->actionsWhenNested());
410 XGUI_OperationMgr* anOperationMgr = aWorkshop->operationMgr();
411 XGUI_ActionsMgr* anActionsMgr = aWorkshop->actionsMgr();
412 if (aNestedActions.contains(FEATURE_WHEN_NESTED_ACCEPT)) {
413 QAction* anAction = anActionsMgr->operationStateAction(XGUI_ActionsMgr::AcceptAll, NULL);
414 connect(anAction, SIGNAL(triggered()), anOperationMgr, SLOT(commitAllOperations()));
415 aNestedActList << anAction;
417 if (aNestedActions.contains(FEATURE_WHEN_NESTED_ABORT)) {
418 QAction* anAction = anActionsMgr->operationStateAction(XGUI_ActionsMgr::AbortAll, NULL);
419 connect(anAction, SIGNAL(triggered()), anOperationMgr, SLOT(abortAllOperations()));
420 aNestedActList << anAction;
424 if (aWorkshop->isSalomeMode()) {
425 XGUI_SalomeConnector* aSalomeConnector = aWorkshop->salomeConnector();
427 if (isColumnButton) {
428 aAction = aSalomeConnector->addNestedFeature(aWchName, aFeatureInfo, aNestedActList);
430 //Issue #650: in the SALOME mode the tooltip should be same as text
431 aFeatureInfo.toolTip = aFeatureInfo.text;
432 aAction = aSalomeConnector->addFeature(aWchName, aFeatureInfo);
434 aSalomeConnector->setNestedActions(aFeatureInfo.id, aNestedFeatures);
435 aSalomeConnector->setDocumentKind(aFeatureInfo.id, aDocKind);
437 aWorkshop->actionsMgr()->addCommand(aAction);
438 aWorkshop->module()->actionCreated(aAction);
440 //Find or create Workbench
441 AppElements_MainMenu* aMenuBar = aWorkshop->mainWindow()->menuObject();
442 AppElements_Workbench* aPage = aMenuBar->findWorkbench(aWchName);
444 aPage = aWorkshop->addWorkbench(aWchName);
446 //Find or create Group
447 QString aGroupName = QString::fromStdString(theMessage->groupId());
448 AppElements_MenuGroupPanel* aGroup = aPage->findGroup(aGroupName);
450 aGroup = aPage->addGroup(aGroupName);
452 // Check if hotkey sequence is already defined:
453 XGUI_ActionsMgr* anActionsMgr = aWorkshop->actionsMgr();
454 QKeySequence aHotKey = anActionsMgr->registerShortcut(aFeatureInfo.shortcut);
455 if(aHotKey != aFeatureInfo.shortcut) {
456 aFeatureInfo.shortcut = aHotKey;
459 AppElements_Command* aCommand = aGroup->addFeature(aFeatureInfo,
462 // Enrich created button with accept/abort buttons if necessary
463 AppElements_Button* aButton = aCommand->button();
464 if (aButton->isColumnButton()) {
465 aButton->setAdditionalButtons(aNestedActList);
467 aWorkshop->actionsMgr()->addCommand(aCommand);
468 aWorkshop->module()->actionCreated(aCommand);
470 XGUI_OperationMgr* anOperationMgr = workshop()->operationMgr();
471 connect(anOperationMgr, SIGNAL(nestedStateChanged(const std::string&, const bool)),
472 this, SLOT(onNestedStateChanged(const std::string&, const bool)));
476 //**************************************************************
477 bool XGUI_WorkshopListener::displayObject(ObjectPtr theObj)
479 #ifdef DEBUG_RESULT_COMPSOLID
480 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(theObj);
481 if (aRes.get() && (ModelAPI_Tools::hasSubResults(aRes) || ModelAPI_Tools::compSolidOwner(aRes))) {
482 ResultCompSolidPtr aCompSolidRes = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(aRes);
483 if (aCompSolidRes.get()) {
484 qDebug("COMPSOLID: displayObject");
489 XGUI_Workshop* aWorkshop = workshop();
490 // do not display the object if it has sub objects. They should be displayed separately.
491 if (!aWorkshop->module()->canDisplayObject(theObj) ||
492 ModelAPI_Tools::hasSubResults(std::dynamic_pointer_cast<ModelAPI_Result>(theObj)))
495 XGUI_Displayer* aDisplayer = aWorkshop->displayer();
496 ResultBodyPtr aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(theObj);
497 if (aBody.get() != NULL) {
498 int aNb = aDisplayer->objectsCount();
499 aDisplayer->display(theObj, false);
501 myWorkshop->viewer()->fitAll();
503 aDisplayer->display(theObj, false);
508 XGUI_Workshop* XGUI_WorkshopListener::workshop() const
510 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
511 return aConnector->workshop();