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);
225 anOperationMgr->onValidateOperation();
226 //if (myObjectBrowser)
227 // myObjectBrowser->processEvent(theMsg);
230 //******************************************************
231 void XGUI_WorkshopListener::onFeatureRedisplayMsg(const std::shared_ptr<ModelAPI_ObjectUpdatedMessage>& theMsg)
233 std::set<ObjectPtr> aObjects = theMsg->objects();
234 std::set<ObjectPtr>::const_iterator aIt;
236 #ifdef DEBUG_FEATURE_REDISPLAY
238 for (aIt = aObjects.begin(); aIt != aObjects.end(); ++aIt) {
239 anInfo.append(ModuleBase_Tools::objectInfo((*aIt)));
241 QString anInfoStr = anInfo.join(";\t");
242 qDebug(QString("onFeatureRedisplayMsg: %1, %2").arg(aObjects.size()).arg(anInfoStr).toStdString().c_str());
245 XGUI_Workshop* aWorkshop = workshop();
246 XGUI_Displayer* aDisplayer = aWorkshop->displayer();
247 for (aIt = aObjects.begin(); aIt != aObjects.end(); ++aIt) {
248 ObjectPtr aObj = (*aIt);
250 // Hide the object if it is invalid or concealed one
251 bool aHide = !aObj->data() || !aObj->data()->isValid() ||
252 aObj->isDisabled() || (!aObj->isDisplayed());
253 if (!aHide) { // check that this is not hidden result
254 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(aObj);
255 aHide = aRes && aRes->isConcealed();
257 #ifdef DEBUG_RESULT_COMPSOLID
258 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(aObj);
260 ResultCompSolidPtr aCompSolidRes = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(aRes);
261 if (aCompSolidRes.get()) {
262 qDebug(QString("COMPSOLID, numberOfSubs = %1").arg(aCompSolidRes->numberOfSubs()).toStdString().c_str());
264 if (ModelAPI_Tools::compSolidOwner(aRes))
265 qDebug("COMPSOLID sub-object");
269 aDisplayer->erase(aObj, false);
270 #ifdef DEBUG_FEATURE_REDISPLAY
271 // Redisplay the visible object or the object of the current operation
272 bool isVisibleObject = aDisplayer->isVisible(aObj);
274 QString anObjInfo = ModuleBase_Tools::objectInfo((aObj));
275 //qDebug(QString("visible=%1 : erase = %2").arg(isVisibleObject).arg(anObjInfo).toStdString().c_str());
279 // Redisplay the visible object or the object of the current operation
280 bool isVisibleObject = aDisplayer->isVisible(aObj);
281 #ifdef DEBUG_FEATURE_REDISPLAY
282 QString anObjInfo = ModuleBase_Tools::objectInfo((aObj));
283 //qDebug(QString("visible=%1 : display= %2").arg(isVisibleObject).arg(anObjInfo).toStdString().c_str());
284 /*FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
285 if (aFeature.get()) {
286 std::string aKind = aFeature->getKind();
287 if (aKind == "SketchMultiRotation")
292 if (isVisibleObject) { // redisplay visible object
293 //displayObject(aObj); // In order to update presentation
294 // in order to avoid the check whether the object can be redisplayed, the exact method
295 // of redisplay is called. This modification is made in order to have the line is updated
296 // by creation of a horizontal constraint on the line by preselection
297 if (ModelAPI_Tools::hasSubResults(std::dynamic_pointer_cast<ModelAPI_Result>(aObj))) {
298 aDisplayer->erase(aObj, false);
301 aDisplayer->redisplay(aObj, false);
302 // Deactivate object of current operation from selection
303 aWorkshop->deactivateActiveObject(aObj, false);
305 } else { // display object if the current operation has it
306 if (displayObject(aObj)) {
307 // Deactivate object of current operation from selection
308 aWorkshop->deactivateActiveObject(aObj, false);
313 aDisplayer->updateViewer();
315 //******************************************************
316 void XGUI_WorkshopListener::onFeatureCreatedMsg(const std::shared_ptr<ModelAPI_ObjectUpdatedMessage>& theMsg)
318 std::set<ObjectPtr> aObjects = theMsg->objects();
319 std::set<ObjectPtr>::const_iterator aIt;
320 #ifdef DEBUG_FEATURE_CREATED
322 for (aIt = aObjects.begin(); aIt != aObjects.end(); ++aIt) {
323 anInfo.append(ModuleBase_Tools::objectInfo((*aIt)));
325 QString anInfoStr = anInfo.join(";\t");
326 qDebug(QString("onFeatureCreatedMsg: %1, %2").arg(aObjects.size()).arg(anInfoStr).toStdString().c_str());
329 //bool aHasPart = false;
330 bool isDisplayed = false;
331 for (aIt = aObjects.begin(); aIt != aObjects.end(); ++aIt) {
332 ObjectPtr anObject = *aIt;
334 #ifdef DEBUG_RESULT_COMPSOLID
335 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
337 ResultCompSolidPtr aCompSolidRes = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(aRes);
338 if (aCompSolidRes.get()) {
339 qDebug(QString("COMPSOLID, numberOfSubs = %1").arg(aCompSolidRes->numberOfSubs()).toStdString().c_str());
341 if (ModelAPI_Tools::compSolidOwner(aRes))
342 qDebug("COMPSOLID sub-object");
345 // the validity of the data should be checked here in order to avoid display of the objects,
346 // which were created, then deleted, but flush for the creation event happens after that
347 // we should not display disabled objects
348 bool aHide = !anObject->data()->isValid() ||
349 anObject->isDisabled() ||
350 !anObject->isDisplayed();
352 // setDisplayed has to be called in order to synchronize internal state of the object
353 // with list of displayed objects
354 if (myWorkshop->module()->canDisplayObject(anObject)) {
355 anObject->setDisplayed(true);
356 isDisplayed = displayObject(*aIt);
358 anObject->setDisplayed(false);
361 //if (myObjectBrowser)
362 // myObjectBrowser->processEvent(theMsg);
364 workshop()->displayer()->updateViewer();
365 //if (aHasPart) { // TODO: Avoid activate last part on loading of document
366 // activateLastPart();
370 void XGUI_WorkshopListener::onNestedStateChanged(const std::string& theFeatureId, const bool theState)
372 XGUI_Workshop* aWorkshop = workshop();
374 //one button is used for all features, which can have nested actions, so it is obtained from
375 // the action manager
376 QAction* anAcceptAllAction = aWorkshop->actionsMgr()->operationStateAction(XGUI_ActionsMgr::AcceptAll, NULL);
377 if (aWorkshop->isSalomeMode()) {
378 XGUI_SalomeConnector* aSalomeConnector = aWorkshop->salomeConnector();
379 XGUI_ActionsMgr* anActionsMgr = aWorkshop->actionsMgr();
380 if (aSalomeConnector->isNestedFeature(anActionsMgr->action(theFeatureId.c_str())))
381 anAcceptAllAction->setEnabled(theState);
383 AppElements_MainMenu* aMenuBar = aWorkshop->mainWindow()->menuObject();
384 AppElements_Command* aCommand = aMenuBar->feature(theFeatureId.c_str());
385 if (aCommand && aCommand->button()->additionalButtonWidget())
386 anAcceptAllAction->setEnabled(theState);
390 bool XGUI_WorkshopListener::event(QEvent * theEvent)
392 PostponeMessageQtEvent* aPostponedEv = dynamic_cast<PostponeMessageQtEvent*>(theEvent);
394 std::shared_ptr<Events_Message> aEventPtr = aPostponedEv->postponedMessage();
395 processEvent(aEventPtr);
401 void XGUI_WorkshopListener::addFeature(const std::shared_ptr<Config_FeatureMessage>& theMessage)
405 qDebug() << "XGUI_WorkshopListener::addFeature: NULL message.";
409 ActionInfo aFeatureInfo;
410 aFeatureInfo.initFrom(theMessage);
412 XGUI_Workshop* aWorkshop = workshop();
414 QString aWchName = QString::fromStdString(theMessage->workbenchId());
415 QStringList aNestedFeatures =
416 QString::fromStdString(theMessage->nestedFeatures()).split(" ", QString::SkipEmptyParts);
417 QString aDocKind = QString::fromStdString(theMessage->documentKind());
418 QList<QAction*> aNestedActList;
419 bool isColumnButton = !aNestedFeatures.isEmpty();
420 if (isColumnButton) {
421 QString aNestedActions = QString::fromStdString(theMessage->actionsWhenNested());
422 XGUI_OperationMgr* anOperationMgr = aWorkshop->operationMgr();
423 XGUI_ActionsMgr* anActionsMgr = aWorkshop->actionsMgr();
424 if (aNestedActions.contains(FEATURE_WHEN_NESTED_ACCEPT)) {
425 QAction* anAction = anActionsMgr->operationStateAction(XGUI_ActionsMgr::AcceptAll, NULL);
426 connect(anAction, SIGNAL(triggered()), anOperationMgr, SLOT(commitAllOperations()));
427 aNestedActList << anAction;
429 if (aNestedActions.contains(FEATURE_WHEN_NESTED_ABORT)) {
430 QAction* anAction = anActionsMgr->operationStateAction(XGUI_ActionsMgr::AbortAll, NULL);
431 connect(anAction, SIGNAL(triggered()), anOperationMgr, SLOT(abortAllOperations()));
432 aNestedActList << anAction;
436 if (aWorkshop->isSalomeMode()) {
437 XGUI_SalomeConnector* aSalomeConnector = aWorkshop->salomeConnector();
439 if (isColumnButton) {
440 aAction = aSalomeConnector->addNestedFeature(aWchName, aFeatureInfo, aNestedActList);
442 //Issue #650: in the SALOME mode the tooltip should be same as text
443 aFeatureInfo.toolTip = aFeatureInfo.text;
444 aAction = aSalomeConnector->addFeature(aWchName, aFeatureInfo);
446 aSalomeConnector->setNestedActions(aFeatureInfo.id, aNestedFeatures);
447 aSalomeConnector->setDocumentKind(aFeatureInfo.id, aDocKind);
449 aWorkshop->actionsMgr()->addCommand(aAction);
450 aWorkshop->module()->actionCreated(aAction);
452 //Find or create Workbench
453 AppElements_MainMenu* aMenuBar = aWorkshop->mainWindow()->menuObject();
454 AppElements_Workbench* aPage = aMenuBar->findWorkbench(aWchName);
456 aPage = aWorkshop->addWorkbench(aWchName);
458 //Find or create Group
459 QString aGroupName = QString::fromStdString(theMessage->groupId());
460 AppElements_MenuGroupPanel* aGroup = aPage->findGroup(aGroupName);
462 aGroup = aPage->addGroup(aGroupName);
464 // Check if hotkey sequence is already defined:
465 XGUI_ActionsMgr* anActionsMgr = aWorkshop->actionsMgr();
466 QKeySequence aHotKey = anActionsMgr->registerShortcut(aFeatureInfo.shortcut);
467 if(aHotKey != aFeatureInfo.shortcut) {
468 aFeatureInfo.shortcut = aHotKey;
471 AppElements_Command* aCommand = aGroup->addFeature(aFeatureInfo,
474 // Enrich created button with accept/abort buttons if necessary
475 AppElements_Button* aButton = aCommand->button();
476 if (aButton->isColumnButton()) {
477 aButton->setAdditionalButtons(aNestedActList);
479 aWorkshop->actionsMgr()->addCommand(aCommand);
480 aWorkshop->module()->actionCreated(aCommand);
485 //**************************************************************
486 bool XGUI_WorkshopListener::displayObject(ObjectPtr theObj)
488 #ifdef DEBUG_RESULT_COMPSOLID
489 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(theObj);
490 if (aRes.get() && (ModelAPI_Tools::hasSubResults(aRes) || ModelAPI_Tools::compSolidOwner(aRes))) {
491 ResultCompSolidPtr aCompSolidRes = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(aRes);
492 if (aCompSolidRes.get()) {
493 qDebug("COMPSOLID: displayObject");
498 XGUI_Workshop* aWorkshop = workshop();
499 // do not display the object if it has sub objects. They should be displayed separately.
500 if (!aWorkshop->module()->canDisplayObject(theObj) ||
501 ModelAPI_Tools::hasSubResults(std::dynamic_pointer_cast<ModelAPI_Result>(theObj)))
504 XGUI_Displayer* aDisplayer = aWorkshop->displayer();
505 ResultBodyPtr aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(theObj);
506 if (aBody.get() != NULL) {
507 int aNb = aDisplayer->objectsCount();
508 aDisplayer->display(theObj, false);
510 myWorkshop->viewer()->fitAll();
512 aDisplayer->display(theObj, false);
517 XGUI_Workshop* XGUI_WorkshopListener::workshop() const
519 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
520 return aConnector->workshop();