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_FEATURE_UPDATED
63 //#define DEBUG_RESULT_COMPSOLID
65 XGUI_WorkshopListener::XGUI_WorkshopListener(ModuleBase_IWorkshop* theWorkshop)
66 : myWorkshop(theWorkshop),
69 XGUI_OperationMgr* anOperationMgr = workshop()->operationMgr();
70 connect(anOperationMgr, SIGNAL(nestedStateChanged(const std::string&, const bool)),
71 this, SLOT(onNestedStateChanged(const std::string&, const bool)));
74 //******************************************************
75 XGUI_WorkshopListener::~XGUI_WorkshopListener(void)
79 //******************************************************
80 void XGUI_WorkshopListener::initializeEventListening()
82 //Initialize event listening
83 Events_Loop* aLoop = Events_Loop::loop();
84 aLoop->registerListener(this, Events_Error::errorID()); //!< Listening application errors.
85 aLoop->registerListener(this, Events_Loop::eventByName(Config_FeatureMessage::GUI_EVENT()));
86 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OPERATION_LAUNCHED));
87 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
88 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_CREATED));
89 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
90 aLoop->registerListener(this, Events_LongOp::eventID());
91 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_PLUGIN_LOADED));
92 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_SELFILTER_LOADED));
94 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_UPDATE_VIEWER_BLOCKED));
95 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_UPDATE_VIEWER_UNBLOCKED));
98 //******************************************************
99 void XGUI_WorkshopListener::processEvent(const std::shared_ptr<Events_Message>& theMessage)
101 if (QApplication::instance()->thread() != QThread::currentThread()) {
103 std::cout << "XGUI_Workshop::processEvent: " << "Working in another thread." << std::endl;
105 SessionPtr aMgr = ModelAPI_Session::get();
106 PostponeMessageQtEvent* aPostponeEvent = new PostponeMessageQtEvent(theMessage);
107 QApplication::postEvent(this, aPostponeEvent);
111 //A message to start feature creation received.
112 if (theMessage->eventID() == Events_Loop::loop()->eventByName(Config_FeatureMessage::GUI_EVENT())) {
113 std::shared_ptr<Config_FeatureMessage> aFeatureMsg =
114 std::dynamic_pointer_cast<Config_FeatureMessage>(theMessage);
115 if (!aFeatureMsg->isInternal()) {
116 addFeature(aFeatureMsg);
119 // Process creation of Part
120 else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_CREATED)) {
121 std::shared_ptr<ModelAPI_ObjectUpdatedMessage> aUpdMsg =
122 std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
123 onFeatureCreatedMsg(aUpdMsg);
125 XGUI_SalomeConnector* aSalomeConnector = workshop()->salomeConnector();
126 if (aSalomeConnector)
127 aSalomeConnector->createPreferences();
128 myUpdatePrefs = false;
131 else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_PLUGIN_LOADED)) {
132 myUpdatePrefs = true;
135 else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY)) {
136 std::shared_ptr<ModelAPI_ObjectUpdatedMessage> aUpdMsg =
137 std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
138 onFeatureRedisplayMsg(aUpdMsg);
140 //Update property panel on corresponding message. If there is no current operation (no
141 //property panel), or received message has different feature to the current - do nothing.
142 else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_UPDATED)) {
143 std::shared_ptr<ModelAPI_ObjectUpdatedMessage> anUpdateMsg =
144 std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
145 onFeatureUpdatedMsg(anUpdateMsg);
146 } else if (theMessage->eventID() == Events_LongOp::eventID()) {
147 if (Events_LongOp::isPerformed()) {
148 QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
150 QApplication::restoreOverrideCursor();
153 //An operation passed by message. Start it, process and commit.
154 else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OPERATION_LAUNCHED)) {
155 std::shared_ptr<Config_PointerMessage> aPartSetMsg =
156 std::dynamic_pointer_cast<Config_PointerMessage>(theMessage);
157 //myPropertyPanel->cleanContent();
158 ModuleBase_Operation* anOperation = (ModuleBase_Operation*) aPartSetMsg->pointer();
159 XGUI_OperationMgr* anOperationMgr = workshop()->operationMgr();
161 if (anOperationMgr->startOperation(anOperation)) {
162 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(anOperation);
164 workshop()->propertyPanel()->updateContentWidget(aFOperation->feature());
165 if (!anOperation->getDescription()->hasXmlRepresentation()) {
166 if (anOperation->commit())
167 workshop()->updateCommandStatus();
171 else if (theMessage->eventID() == Events_Loop::eventByName(EVENT_SELFILTER_LOADED)) {
172 std::shared_ptr<Config_SelectionFilterMessage> aMsg =
173 std::dynamic_pointer_cast<Config_SelectionFilterMessage>(theMessage);
175 ModuleBase_FilterFactory* aFactory = myWorkshop->selectionFilters();
176 if (!aMsg->attributeId().empty()) {
177 aFactory->assignFilter(aMsg->selectionFilterId(), aMsg->featureId(), aMsg->attributeId(),
181 } else if (theMessage->eventID() == Events_Loop::eventByName(EVENT_UPDATE_VIEWER_BLOCKED)) {
182 // the viewer's update context will not happens until viewer updated is emitted
183 workshop()->displayer()->enableUpdateViewer(false);
184 } else if (theMessage->eventID() == Events_Loop::eventByName(EVENT_UPDATE_VIEWER_UNBLOCKED)) {
185 // the viewer's update context is unblocked, the viewer's update works
186 XGUI_Displayer* aDisplayer = workshop()->displayer();
187 aDisplayer->enableUpdateViewer(true);
188 aDisplayer->updateViewer();
190 //Show error dialog if error message received.
191 std::shared_ptr<Events_Error> anAppError = std::dynamic_pointer_cast<Events_Error>(theMessage);
193 emit errorOccurred(QString::fromLatin1(anAppError->description()));
197 if (!workshop()->isSalomeMode()) {
198 SessionPtr aMgr = ModelAPI_Session::get();
199 AppElements_MainWindow* aMainWindow = workshop()->mainWindow();
200 if (aMgr->isModified() != aMainWindow->isModifiedState())
201 aMainWindow->setModifiedState(aMgr->isModified());
205 //******************************************************
206 void XGUI_WorkshopListener::onFeatureUpdatedMsg(
207 const std::shared_ptr<ModelAPI_ObjectUpdatedMessage>& theMsg)
209 #ifdef DEBUG_FEATURE_UPDATED
210 std::set<ObjectPtr> aObjects = theMsg->objects();
211 std::set<ObjectPtr>::const_iterator aIt;
213 for (aIt = aObjects.begin(); aIt != aObjects.end(); ++aIt) {
214 anInfo.append(ModuleBase_Tools::objectInfo((*aIt)));
216 QString anInfoStr = anInfo.join(";\t");
217 qDebug(QString("onFeatureUpdatedMsg: %1, %2").arg(aObjects.size()).arg(anInfoStr).toStdString().c_str());
219 bool isModified = false;
220 std::set<ObjectPtr> aFeatures = theMsg->objects();
221 XGUI_OperationMgr* anOperationMgr = workshop()->operationMgr();
222 if (anOperationMgr->hasOperation()) {
223 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
224 (anOperationMgr->currentOperation());
226 FeaturePtr aCurrentFeature = aFOperation->feature();
227 std::set<ObjectPtr>::const_iterator aIt;
228 for (aIt = aFeatures.begin(); aIt != aFeatures.end(); ++aIt) {
229 ObjectPtr aNewFeature = (*aIt);
230 if (aNewFeature == aCurrentFeature) {
231 workshop()->propertyPanel()->updateContentWidget(aCurrentFeature);
235 isModified = myWorkshop->module()->customizeObject(aCurrentFeature, false);
238 anOperationMgr->onValidateOperation();
240 //if (myObjectBrowser)
241 // myObjectBrowser->processEvent(theMsg);
243 workshop()->displayer()->updateViewer();
246 //******************************************************
247 void XGUI_WorkshopListener::onFeatureRedisplayMsg(const std::shared_ptr<ModelAPI_ObjectUpdatedMessage>& theMsg)
249 std::set<ObjectPtr> aObjects = theMsg->objects();
250 std::set<ObjectPtr>::const_iterator aIt;
252 #ifdef DEBUG_FEATURE_REDISPLAY
254 for (aIt = aObjects.begin(); aIt != aObjects.end(); ++aIt) {
255 anInfo.append(ModuleBase_Tools::objectInfo((*aIt)));
257 QString anInfoStr = anInfo.join(";\t");
258 qDebug(QString("onFeatureRedisplayMsg: %1, %2").arg(aObjects.size()).arg(anInfoStr).toStdString().c_str());
261 XGUI_Workshop* aWorkshop = workshop();
262 XGUI_Displayer* aDisplayer = aWorkshop->displayer();
263 for (aIt = aObjects.begin(); aIt != aObjects.end(); ++aIt) {
264 ObjectPtr aObj = (*aIt);
266 // Hide the object if it is invalid or concealed one
267 bool aHide = !aObj->data() || !aObj->data()->isValid() ||
268 aObj->isDisabled() || (!aObj->isDisplayed());
269 if (!aHide) { // check that this is not hidden result
270 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(aObj);
271 aHide = aRes && aRes->isConcealed();
273 #ifdef DEBUG_RESULT_COMPSOLID
274 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(aObj);
276 ResultCompSolidPtr aCompSolidRes = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(aRes);
277 if (aCompSolidRes.get()) {
278 qDebug(QString("COMPSOLID, numberOfSubs = %1").arg(aCompSolidRes->numberOfSubs()).toStdString().c_str());
280 if (ModelAPI_Tools::compSolidOwner(aRes))
281 qDebug("COMPSOLID sub-object");
285 aDisplayer->erase(aObj, false);
286 #ifdef DEBUG_FEATURE_REDISPLAY
287 // Redisplay the visible object or the object of the current operation
288 bool isVisibleObject = aDisplayer->isVisible(aObj);
290 QString anObjInfo = ModuleBase_Tools::objectInfo((aObj));
291 //qDebug(QString("visible=%1 : erase = %2").arg(isVisibleObject).arg(anObjInfo).toStdString().c_str());
295 // Redisplay the visible object or the object of the current operation
296 bool isVisibleObject = aDisplayer->isVisible(aObj);
297 #ifdef DEBUG_FEATURE_REDISPLAY
298 QString anObjInfo = ModuleBase_Tools::objectInfo((aObj));
299 //qDebug(QString("visible=%1 : display= %2").arg(isVisibleObject).arg(anObjInfo).toStdString().c_str());
300 /*FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
301 if (aFeature.get()) {
302 std::string aKind = aFeature->getKind();
303 if (aKind == "SketchMultiRotation")
308 if (isVisibleObject) { // redisplay visible object
309 //displayObject(aObj); // In order to update presentation
310 // in order to avoid the check whether the object can be redisplayed, the exact method
311 // of redisplay is called. This modification is made in order to have the line is updated
312 // by creation of a horizontal constraint on the line by preselection
313 if (ModelAPI_Tools::hasSubResults(std::dynamic_pointer_cast<ModelAPI_Result>(aObj))) {
314 aDisplayer->erase(aObj, false);
317 aDisplayer->redisplay(aObj, false);
318 // Deactivate object of current operation from selection
319 aWorkshop->deactivateActiveObject(aObj, false);
321 } else { // display object if the current operation has it
322 if (displayObject(aObj)) {
323 // Deactivate object of current operation from selection
324 aWorkshop->deactivateActiveObject(aObj, false);
329 aDisplayer->updateViewer();
331 //******************************************************
332 void XGUI_WorkshopListener::onFeatureCreatedMsg(const std::shared_ptr<ModelAPI_ObjectUpdatedMessage>& theMsg)
334 std::set<ObjectPtr> aObjects = theMsg->objects();
335 std::set<ObjectPtr>::const_iterator aIt;
336 #ifdef DEBUG_FEATURE_CREATED
338 for (aIt = aObjects.begin(); aIt != aObjects.end(); ++aIt) {
339 anInfo.append(ModuleBase_Tools::objectInfo((*aIt)));
341 QString anInfoStr = anInfo.join(";\t");
342 qDebug(QString("onFeatureCreatedMsg: %1, %2").arg(aObjects.size()).arg(anInfoStr).toStdString().c_str());
345 //bool aHasPart = false;
346 bool isDisplayed = false;
347 for (aIt = aObjects.begin(); aIt != aObjects.end(); ++aIt) {
348 ObjectPtr anObject = *aIt;
350 #ifdef DEBUG_RESULT_COMPSOLID
351 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
353 ResultCompSolidPtr aCompSolidRes = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(aRes);
354 if (aCompSolidRes.get()) {
355 qDebug(QString("COMPSOLID, numberOfSubs = %1").arg(aCompSolidRes->numberOfSubs()).toStdString().c_str());
357 if (ModelAPI_Tools::compSolidOwner(aRes))
358 qDebug("COMPSOLID sub-object");
361 // the validity of the data should be checked here in order to avoid display of the objects,
362 // which were created, then deleted, but flush for the creation event happens after that
363 // we should not display disabled objects
364 bool aHide = !anObject->data()->isValid() ||
365 anObject->isDisabled() ||
366 !anObject->isDisplayed();
368 // setDisplayed has to be called in order to synchronize internal state of the object
369 // with list of displayed objects
370 if (myWorkshop->module()->canDisplayObject(anObject)) {
371 anObject->setDisplayed(true);
372 isDisplayed = displayObject(*aIt);
374 anObject->setDisplayed(false);
377 //if (myObjectBrowser)
378 // myObjectBrowser->processEvent(theMsg);
380 workshop()->displayer()->updateViewer();
381 //if (aHasPart) { // TODO: Avoid activate last part on loading of document
382 // activateLastPart();
386 void XGUI_WorkshopListener::onNestedStateChanged(const std::string& theFeatureId, const bool theState)
388 XGUI_Workshop* aWorkshop = workshop();
390 //one button is used for all features, which can have nested actions, so it is obtained from
391 // the action manager
392 QAction* anAcceptAllAction = aWorkshop->actionsMgr()->operationStateAction(XGUI_ActionsMgr::AcceptAll, NULL);
393 if (aWorkshop->isSalomeMode()) {
394 XGUI_SalomeConnector* aSalomeConnector = aWorkshop->salomeConnector();
395 XGUI_ActionsMgr* anActionsMgr = aWorkshop->actionsMgr();
396 if (aSalomeConnector->isNestedFeature(anActionsMgr->action(theFeatureId.c_str())))
397 anAcceptAllAction->setEnabled(theState);
399 AppElements_MainMenu* aMenuBar = aWorkshop->mainWindow()->menuObject();
400 AppElements_Command* aCommand = aMenuBar->feature(theFeatureId.c_str());
401 if (aCommand && aCommand->button()->additionalButtonWidget())
402 anAcceptAllAction->setEnabled(theState);
406 bool XGUI_WorkshopListener::event(QEvent * theEvent)
408 PostponeMessageQtEvent* aPostponedEv = dynamic_cast<PostponeMessageQtEvent*>(theEvent);
410 std::shared_ptr<Events_Message> aEventPtr = aPostponedEv->postponedMessage();
411 processEvent(aEventPtr);
417 void XGUI_WorkshopListener::addFeature(const std::shared_ptr<Config_FeatureMessage>& theMessage)
421 qDebug() << "XGUI_WorkshopListener::addFeature: NULL message.";
425 ActionInfo aFeatureInfo;
426 aFeatureInfo.initFrom(theMessage);
428 XGUI_Workshop* aWorkshop = workshop();
430 QString aWchName = QString::fromStdString(theMessage->workbenchId());
431 QStringList aNestedFeatures =
432 QString::fromStdString(theMessage->nestedFeatures()).split(" ", QString::SkipEmptyParts);
433 QString aDocKind = QString::fromStdString(theMessage->documentKind());
434 QList<QAction*> aNestedActList;
435 bool isColumnButton = !aNestedFeatures.isEmpty();
436 if (isColumnButton) {
437 QString aNestedActions = QString::fromStdString(theMessage->actionsWhenNested());
438 XGUI_OperationMgr* anOperationMgr = aWorkshop->operationMgr();
439 XGUI_ActionsMgr* anActionsMgr = aWorkshop->actionsMgr();
440 if (aNestedActions.contains(FEATURE_WHEN_NESTED_ACCEPT)) {
441 QAction* anAction = anActionsMgr->operationStateAction(XGUI_ActionsMgr::AcceptAll, NULL);
442 connect(anAction, SIGNAL(triggered()), anOperationMgr, SLOT(commitAllOperations()));
443 aNestedActList << anAction;
445 if (aNestedActions.contains(FEATURE_WHEN_NESTED_ABORT)) {
446 QAction* anAction = anActionsMgr->operationStateAction(XGUI_ActionsMgr::AbortAll, NULL);
447 connect(anAction, SIGNAL(triggered()), anOperationMgr, SLOT(abortAllOperations()));
448 aNestedActList << anAction;
452 if (aWorkshop->isSalomeMode()) {
453 XGUI_SalomeConnector* aSalomeConnector = aWorkshop->salomeConnector();
455 if (isColumnButton) {
456 aAction = aSalomeConnector->addNestedFeature(aWchName, aFeatureInfo, aNestedActList);
458 //Issue #650: in the SALOME mode the tooltip should be same as text
459 aFeatureInfo.toolTip = aFeatureInfo.text;
460 aAction = aSalomeConnector->addFeature(aWchName, aFeatureInfo);
462 aSalomeConnector->setNestedActions(aFeatureInfo.id, aNestedFeatures);
463 aSalomeConnector->setDocumentKind(aFeatureInfo.id, aDocKind);
465 aWorkshop->actionsMgr()->addCommand(aAction);
466 aWorkshop->module()->actionCreated(aAction);
468 //Find or create Workbench
469 AppElements_MainMenu* aMenuBar = aWorkshop->mainWindow()->menuObject();
470 AppElements_Workbench* aPage = aMenuBar->findWorkbench(aWchName);
472 aPage = aWorkshop->addWorkbench(aWchName);
474 //Find or create Group
475 QString aGroupName = QString::fromStdString(theMessage->groupId());
476 AppElements_MenuGroupPanel* aGroup = aPage->findGroup(aGroupName);
478 aGroup = aPage->addGroup(aGroupName);
480 // Check if hotkey sequence is already defined:
481 XGUI_ActionsMgr* anActionsMgr = aWorkshop->actionsMgr();
482 QKeySequence aHotKey = anActionsMgr->registerShortcut(aFeatureInfo.shortcut);
483 if(aHotKey != aFeatureInfo.shortcut) {
484 aFeatureInfo.shortcut = aHotKey;
487 AppElements_Command* aCommand = aGroup->addFeature(aFeatureInfo,
490 // Enrich created button with accept/abort buttons if necessary
491 AppElements_Button* aButton = aCommand->button();
492 if (aButton->isColumnButton()) {
493 aButton->setAdditionalButtons(aNestedActList);
495 aWorkshop->actionsMgr()->addCommand(aCommand);
496 aWorkshop->module()->actionCreated(aCommand);
501 //**************************************************************
502 bool XGUI_WorkshopListener::displayObject(ObjectPtr theObj)
504 #ifdef DEBUG_RESULT_COMPSOLID
505 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(theObj);
506 if (aRes.get() && (ModelAPI_Tools::hasSubResults(aRes) || ModelAPI_Tools::compSolidOwner(aRes))) {
507 ResultCompSolidPtr aCompSolidRes = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(aRes);
508 if (aCompSolidRes.get()) {
509 qDebug("COMPSOLID: displayObject");
514 XGUI_Workshop* aWorkshop = workshop();
515 // do not display the object if it has sub objects. They should be displayed separately.
516 if (!aWorkshop->module()->canDisplayObject(theObj) ||
517 ModelAPI_Tools::hasSubResults(std::dynamic_pointer_cast<ModelAPI_Result>(theObj)))
520 XGUI_Displayer* aDisplayer = aWorkshop->displayer();
521 ResultBodyPtr aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(theObj);
522 if (aBody.get() != NULL) {
523 int aNb = aDisplayer->objectsCount();
524 aDisplayer->display(theObj, false);
526 myWorkshop->viewer()->fitAll();
528 aDisplayer->display(theObj, false);
533 XGUI_Workshop* XGUI_WorkshopListener::workshop() const
535 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
536 return aConnector->workshop();