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_OperationFeature.h>
41 #include <ModuleBase_Tools.h>
42 #include <ModuleBase_IViewer.h>
43 #include <ModuleBase_FilterFactory.h>
45 #include <Config_FeatureMessage.h>
46 #include <Config_PointerMessage.h>
47 #include <Config_SelectionFilterMessage.h>
48 #include <Config_Keywords.h>
50 #include <QApplication>
51 #include <QMainWindow>
60 //#define DEBUG_FEATURE_CREATED
61 //#define DEBUG_FEATURE_REDISPLAY
62 //#define DEBUG_RESULT_COMPSOLID
64 XGUI_WorkshopListener::XGUI_WorkshopListener(ModuleBase_IWorkshop* theWorkshop)
65 : myWorkshop(theWorkshop),
68 XGUI_OperationMgr* anOperationMgr = workshop()->operationMgr();
69 connect(anOperationMgr, SIGNAL(nestedStateChanged(const std::string&, const bool)),
70 this, SLOT(onNestedStateChanged(const std::string&, const bool)));
73 //******************************************************
74 XGUI_WorkshopListener::~XGUI_WorkshopListener(void)
78 //******************************************************
79 void XGUI_WorkshopListener::initializeEventListening()
81 //Initialize event listening
82 Events_Loop* aLoop = Events_Loop::loop();
83 aLoop->registerListener(this, Events_Error::errorID()); //!< Listening application errors.
84 aLoop->registerListener(this, Events_Loop::eventByName(Config_FeatureMessage::GUI_EVENT()));
85 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OPERATION_LAUNCHED));
86 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
87 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_CREATED));
88 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
89 aLoop->registerListener(this, Events_LongOp::eventID());
90 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_PLUGIN_LOADED));
91 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_SELFILTER_LOADED));
93 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_UPDATE_VIEWER_BLOCKED));
94 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_UPDATE_VIEWER_UNBLOCKED));
97 //******************************************************
98 void XGUI_WorkshopListener::processEvent(const std::shared_ptr<Events_Message>& theMessage)
100 if (QApplication::instance()->thread() != QThread::currentThread()) {
102 std::cout << "XGUI_Workshop::processEvent: " << "Working in another thread." << std::endl;
104 SessionPtr aMgr = ModelAPI_Session::get();
105 PostponeMessageQtEvent* aPostponeEvent = new PostponeMessageQtEvent(theMessage);
106 QApplication::postEvent(this, aPostponeEvent);
110 //A message to start feature creation received.
111 if (theMessage->eventID() == Events_Loop::loop()->eventByName(Config_FeatureMessage::GUI_EVENT())) {
112 std::shared_ptr<Config_FeatureMessage> aFeatureMsg =
113 std::dynamic_pointer_cast<Config_FeatureMessage>(theMessage);
114 if (!aFeatureMsg->isInternal()) {
115 addFeature(aFeatureMsg);
118 // Process creation of Part
119 else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_CREATED)) {
120 std::shared_ptr<ModelAPI_ObjectUpdatedMessage> aUpdMsg =
121 std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
122 onFeatureCreatedMsg(aUpdMsg);
124 XGUI_SalomeConnector* aSalomeConnector = workshop()->salomeConnector();
125 if (aSalomeConnector)
126 aSalomeConnector->createPreferences();
127 myUpdatePrefs = false;
130 else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_PLUGIN_LOADED)) {
131 myUpdatePrefs = true;
134 else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY)) {
135 std::shared_ptr<ModelAPI_ObjectUpdatedMessage> aUpdMsg =
136 std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
137 onFeatureRedisplayMsg(aUpdMsg);
139 //Update property panel on corresponding message. If there is no current operation (no
140 //property panel), or received message has different feature to the current - do nothing.
141 else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_UPDATED)) {
142 std::shared_ptr<ModelAPI_ObjectUpdatedMessage> anUpdateMsg =
143 std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
144 onFeatureUpdatedMsg(anUpdateMsg);
145 } else if (theMessage->eventID() == Events_LongOp::eventID()) {
146 if (Events_LongOp::isPerformed()) {
147 QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
149 QApplication::restoreOverrideCursor();
152 //An operation passed by message. Start it, process and commit.
153 else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OPERATION_LAUNCHED)) {
154 std::shared_ptr<Config_PointerMessage> aPartSetMsg =
155 std::dynamic_pointer_cast<Config_PointerMessage>(theMessage);
156 //myPropertyPanel->cleanContent();
157 ModuleBase_Operation* anOperation = (ModuleBase_Operation*) aPartSetMsg->pointer();
158 XGUI_OperationMgr* anOperationMgr = workshop()->operationMgr();
160 if (anOperationMgr->startOperation(anOperation)) {
161 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(anOperation);
163 workshop()->propertyPanel()->updateContentWidget(aFOperation->feature());
164 if (!anOperation->getDescription()->hasXmlRepresentation()) {
165 if (anOperation->commit())
166 workshop()->updateCommandStatus();
170 else if (theMessage->eventID() == Events_Loop::eventByName(EVENT_SELFILTER_LOADED)) {
171 std::shared_ptr<Config_SelectionFilterMessage> aMsg =
172 std::dynamic_pointer_cast<Config_SelectionFilterMessage>(theMessage);
174 ModuleBase_FilterFactory* aFactory = myWorkshop->selectionFilters();
175 if (!aMsg->attributeId().empty()) {
176 aFactory->assignFilter(aMsg->selectionFilterId(), aMsg->featureId(), aMsg->attributeId(),
180 } else if (theMessage->eventID() == Events_Loop::eventByName(EVENT_UPDATE_VIEWER_BLOCKED)) {
181 // the viewer's update context will not happens until viewer updated is emitted
182 workshop()->displayer()->enableUpdateViewer(false);
183 } else if (theMessage->eventID() == Events_Loop::eventByName(EVENT_UPDATE_VIEWER_UNBLOCKED)) {
184 // the viewer's update context is unblocked, the viewer's update works
185 XGUI_Displayer* aDisplayer = workshop()->displayer();
186 aDisplayer->enableUpdateViewer(true);
187 aDisplayer->updateViewer();
189 //Show error dialog if error message received.
190 std::shared_ptr<Events_Error> anAppError = std::dynamic_pointer_cast<Events_Error>(theMessage);
192 emit errorOccurred(QString::fromLatin1(anAppError->description()));
196 if (!workshop()->isSalomeMode()) {
197 SessionPtr aMgr = ModelAPI_Session::get();
198 AppElements_MainWindow* aMainWindow = workshop()->mainWindow();
199 if (aMgr->isModified() != aMainWindow->isModifiedState())
200 aMainWindow->setModifiedState(aMgr->isModified());
204 //******************************************************
205 void XGUI_WorkshopListener::onFeatureUpdatedMsg(
206 const std::shared_ptr<ModelAPI_ObjectUpdatedMessage>& theMsg)
208 std::set<ObjectPtr> aFeatures = theMsg->objects();
209 XGUI_OperationMgr* anOperationMgr = workshop()->operationMgr();
210 if (anOperationMgr->hasOperation()) {
211 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
212 (anOperationMgr->currentOperation());
214 FeaturePtr aCurrentFeature = aFOperation->feature();
215 std::set<ObjectPtr>::const_iterator aIt;
216 for (aIt = aFeatures.begin(); aIt != aFeatures.end(); ++aIt) {
217 ObjectPtr aNewFeature = (*aIt);
218 if (aNewFeature == aCurrentFeature) {
219 workshop()->propertyPanel()->updateContentWidget(aCurrentFeature);
223 myWorkshop->module()->customizeObject(aCurrentFeature);
226 anOperationMgr->onValidateOperation();
228 //if (myObjectBrowser)
229 // myObjectBrowser->processEvent(theMsg);
232 //******************************************************
233 void XGUI_WorkshopListener::onFeatureRedisplayMsg(const std::shared_ptr<ModelAPI_ObjectUpdatedMessage>& theMsg)
235 std::set<ObjectPtr> aObjects = theMsg->objects();
236 std::set<ObjectPtr>::const_iterator aIt;
238 #ifdef DEBUG_FEATURE_REDISPLAY
240 for (aIt = aObjects.begin(); aIt != aObjects.end(); ++aIt) {
241 anInfo.append(ModuleBase_Tools::objectInfo((*aIt)));
243 QString anInfoStr = anInfo.join(";\t");
244 qDebug(QString("onFeatureRedisplayMsg: %1, %2").arg(aObjects.size()).arg(anInfoStr).toStdString().c_str());
247 XGUI_Workshop* aWorkshop = workshop();
248 XGUI_Displayer* aDisplayer = aWorkshop->displayer();
249 for (aIt = aObjects.begin(); aIt != aObjects.end(); ++aIt) {
250 ObjectPtr aObj = (*aIt);
252 // Hide the object if it is invalid or concealed one
253 bool aHide = !aObj->data() || !aObj->data()->isValid() ||
254 aObj->isDisabled() || (!aObj->isDisplayed());
255 if (!aHide) { // check that this is not hidden result
256 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(aObj);
257 aHide = aRes && aRes->isConcealed();
259 #ifdef DEBUG_RESULT_COMPSOLID
260 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(aObj);
262 ResultCompSolidPtr aCompSolidRes = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(aRes);
263 if (aCompSolidRes.get()) {
264 qDebug(QString("COMPSOLID, numberOfSubs = %1").arg(aCompSolidRes->numberOfSubs()).toStdString().c_str());
266 if (ModelAPI_Tools::compSolidOwner(aRes))
267 qDebug("COMPSOLID sub-object");
271 aDisplayer->erase(aObj, false);
272 #ifdef DEBUG_FEATURE_REDISPLAY
273 // Redisplay the visible object or the object of the current operation
274 bool isVisibleObject = aDisplayer->isVisible(aObj);
276 QString anObjInfo = ModuleBase_Tools::objectInfo((aObj));
277 //qDebug(QString("visible=%1 : erase = %2").arg(isVisibleObject).arg(anObjInfo).toStdString().c_str());
281 // Redisplay the visible object or the object of the current operation
282 bool isVisibleObject = aDisplayer->isVisible(aObj);
283 #ifdef DEBUG_FEATURE_REDISPLAY
284 QString anObjInfo = ModuleBase_Tools::objectInfo((aObj));
285 //qDebug(QString("visible=%1 : display= %2").arg(isVisibleObject).arg(anObjInfo).toStdString().c_str());
286 /*FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
287 if (aFeature.get()) {
288 std::string aKind = aFeature->getKind();
289 if (aKind == "SketchMultiRotation")
294 if (isVisibleObject) { // redisplay visible object
295 //displayObject(aObj); // In order to update presentation
296 // in order to avoid the check whether the object can be redisplayed, the exact method
297 // of redisplay is called. This modification is made in order to have the line is updated
298 // by creation of a horizontal constraint on the line by preselection
299 if (ModelAPI_Tools::hasSubResults(std::dynamic_pointer_cast<ModelAPI_Result>(aObj))) {
300 aDisplayer->erase(aObj, false);
303 aDisplayer->redisplay(aObj, false);
304 // Deactivate object of current operation from selection
305 aWorkshop->deactivateActiveObject(aObj, false);
307 } else { // display object if the current operation has it
308 if (displayObject(aObj)) {
309 // Deactivate object of current operation from selection
310 aWorkshop->deactivateActiveObject(aObj, false);
315 aDisplayer->updateViewer();
317 //******************************************************
318 void XGUI_WorkshopListener::onFeatureCreatedMsg(const std::shared_ptr<ModelAPI_ObjectUpdatedMessage>& theMsg)
320 std::set<ObjectPtr> aObjects = theMsg->objects();
321 std::set<ObjectPtr>::const_iterator aIt;
322 #ifdef DEBUG_FEATURE_CREATED
324 for (aIt = aObjects.begin(); aIt != aObjects.end(); ++aIt) {
325 anInfo.append(ModuleBase_Tools::objectInfo((*aIt)));
327 QString anInfoStr = anInfo.join(";\t");
328 qDebug(QString("onFeatureCreatedMsg: %1, %2").arg(aObjects.size()).arg(anInfoStr).toStdString().c_str());
331 //bool aHasPart = false;
332 bool isDisplayed = false;
333 for (aIt = aObjects.begin(); aIt != aObjects.end(); ++aIt) {
334 ObjectPtr anObject = *aIt;
336 #ifdef DEBUG_RESULT_COMPSOLID
337 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
339 ResultCompSolidPtr aCompSolidRes = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(aRes);
340 if (aCompSolidRes.get()) {
341 qDebug(QString("COMPSOLID, numberOfSubs = %1").arg(aCompSolidRes->numberOfSubs()).toStdString().c_str());
343 if (ModelAPI_Tools::compSolidOwner(aRes))
344 qDebug("COMPSOLID sub-object");
347 // the validity of the data should be checked here in order to avoid display of the objects,
348 // which were created, then deleted, but flush for the creation event happens after that
349 // we should not display disabled objects
350 bool aHide = !anObject->data()->isValid() ||
351 anObject->isDisabled() ||
352 !anObject->isDisplayed();
354 // setDisplayed has to be called in order to synchronize internal state of the object
355 // with list of displayed objects
356 if (myWorkshop->module()->canDisplayObject(anObject)) {
357 anObject->setDisplayed(true);
358 isDisplayed = displayObject(*aIt);
360 anObject->setDisplayed(false);
363 //if (myObjectBrowser)
364 // myObjectBrowser->processEvent(theMsg);
366 workshop()->displayer()->updateViewer();
367 //if (aHasPart) { // TODO: Avoid activate last part on loading of document
368 // activateLastPart();
372 void XGUI_WorkshopListener::onNestedStateChanged(const std::string& theFeatureId, const bool theState)
374 XGUI_Workshop* aWorkshop = workshop();
376 //one button is used for all features, which can have nested actions, so it is obtained from
377 // the action manager
378 QAction* anAcceptAllAction = aWorkshop->actionsMgr()->operationStateAction(XGUI_ActionsMgr::AcceptAll, NULL);
379 if (aWorkshop->isSalomeMode()) {
380 XGUI_SalomeConnector* aSalomeConnector = aWorkshop->salomeConnector();
381 XGUI_ActionsMgr* anActionsMgr = aWorkshop->actionsMgr();
382 if (aSalomeConnector->isNestedFeature(anActionsMgr->action(theFeatureId.c_str())))
383 anAcceptAllAction->setEnabled(theState);
385 AppElements_MainMenu* aMenuBar = aWorkshop->mainWindow()->menuObject();
386 AppElements_Command* aCommand = aMenuBar->feature(theFeatureId.c_str());
387 if (aCommand && aCommand->button()->additionalButtonWidget())
388 anAcceptAllAction->setEnabled(theState);
392 bool XGUI_WorkshopListener::event(QEvent * theEvent)
394 PostponeMessageQtEvent* aPostponedEv = dynamic_cast<PostponeMessageQtEvent*>(theEvent);
396 std::shared_ptr<Events_Message> aEventPtr = aPostponedEv->postponedMessage();
397 processEvent(aEventPtr);
403 void XGUI_WorkshopListener::addFeature(const std::shared_ptr<Config_FeatureMessage>& theMessage)
407 qDebug() << "XGUI_WorkshopListener::addFeature: NULL message.";
411 ActionInfo aFeatureInfo;
412 aFeatureInfo.initFrom(theMessage);
414 XGUI_Workshop* aWorkshop = workshop();
416 QString aWchName = QString::fromStdString(theMessage->workbenchId());
417 QStringList aNestedFeatures =
418 QString::fromStdString(theMessage->nestedFeatures()).split(" ", QString::SkipEmptyParts);
419 QString aDocKind = QString::fromStdString(theMessage->documentKind());
420 QList<QAction*> aNestedActList;
421 bool isColumnButton = !aNestedFeatures.isEmpty();
422 if (isColumnButton) {
423 QString aNestedActions = QString::fromStdString(theMessage->actionsWhenNested());
424 XGUI_OperationMgr* anOperationMgr = aWorkshop->operationMgr();
425 XGUI_ActionsMgr* anActionsMgr = aWorkshop->actionsMgr();
426 if (aNestedActions.contains(FEATURE_WHEN_NESTED_ACCEPT)) {
427 QAction* anAction = anActionsMgr->operationStateAction(XGUI_ActionsMgr::AcceptAll, NULL);
428 connect(anAction, SIGNAL(triggered()), anOperationMgr, SLOT(commitAllOperations()));
429 aNestedActList << anAction;
431 if (aNestedActions.contains(FEATURE_WHEN_NESTED_ABORT)) {
432 QAction* anAction = anActionsMgr->operationStateAction(XGUI_ActionsMgr::AbortAll, NULL);
433 connect(anAction, SIGNAL(triggered()), anOperationMgr, SLOT(abortAllOperations()));
434 aNestedActList << anAction;
438 if (aWorkshop->isSalomeMode()) {
439 XGUI_SalomeConnector* aSalomeConnector = aWorkshop->salomeConnector();
441 if (isColumnButton) {
442 aAction = aSalomeConnector->addNestedFeature(aWchName, aFeatureInfo, aNestedActList);
444 //Issue #650: in the SALOME mode the tooltip should be same as text
445 aFeatureInfo.toolTip = aFeatureInfo.text;
446 aAction = aSalomeConnector->addFeature(aWchName, aFeatureInfo);
448 aSalomeConnector->setNestedActions(aFeatureInfo.id, aNestedFeatures);
449 aSalomeConnector->setDocumentKind(aFeatureInfo.id, aDocKind);
451 aWorkshop->actionsMgr()->addCommand(aAction);
452 aWorkshop->module()->actionCreated(aAction);
454 //Find or create Workbench
455 AppElements_MainMenu* aMenuBar = aWorkshop->mainWindow()->menuObject();
456 AppElements_Workbench* aPage = aMenuBar->findWorkbench(aWchName);
458 aPage = aWorkshop->addWorkbench(aWchName);
460 //Find or create Group
461 QString aGroupName = QString::fromStdString(theMessage->groupId());
462 AppElements_MenuGroupPanel* aGroup = aPage->findGroup(aGroupName);
464 aGroup = aPage->addGroup(aGroupName);
466 // Check if hotkey sequence is already defined:
467 XGUI_ActionsMgr* anActionsMgr = aWorkshop->actionsMgr();
468 QKeySequence aHotKey = anActionsMgr->registerShortcut(aFeatureInfo.shortcut);
469 if(aHotKey != aFeatureInfo.shortcut) {
470 aFeatureInfo.shortcut = aHotKey;
473 AppElements_Command* aCommand = aGroup->addFeature(aFeatureInfo,
476 // Enrich created button with accept/abort buttons if necessary
477 AppElements_Button* aButton = aCommand->button();
478 if (aButton->isColumnButton()) {
479 aButton->setAdditionalButtons(aNestedActList);
481 aWorkshop->actionsMgr()->addCommand(aCommand);
482 aWorkshop->module()->actionCreated(aCommand);
487 //**************************************************************
488 bool XGUI_WorkshopListener::displayObject(ObjectPtr theObj)
490 #ifdef DEBUG_RESULT_COMPSOLID
491 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(theObj);
492 if (aRes.get() && (ModelAPI_Tools::hasSubResults(aRes) || ModelAPI_Tools::compSolidOwner(aRes))) {
493 ResultCompSolidPtr aCompSolidRes = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(aRes);
494 if (aCompSolidRes.get()) {
495 qDebug("COMPSOLID: displayObject");
500 XGUI_Workshop* aWorkshop = workshop();
501 // do not display the object if it has sub objects. They should be displayed separately.
502 if (!aWorkshop->module()->canDisplayObject(theObj) ||
503 ModelAPI_Tools::hasSubResults(std::dynamic_pointer_cast<ModelAPI_Result>(theObj)))
506 XGUI_Displayer* aDisplayer = aWorkshop->displayer();
507 ResultBodyPtr aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(theObj);
508 if (aBody.get() != NULL) {
509 int aNb = aDisplayer->objectsCount();
510 aDisplayer->display(theObj, false);
512 myWorkshop->viewer()->fitAll();
514 aDisplayer->display(theObj, false);
519 XGUI_Workshop* XGUI_WorkshopListener::workshop() const
521 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
522 return aConnector->workshop();