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),
70 //******************************************************
71 XGUI_WorkshopListener::~XGUI_WorkshopListener(void)
75 //******************************************************
76 void XGUI_WorkshopListener::initializeEventListening()
78 //Initialize event listening
79 Events_Loop* aLoop = Events_Loop::loop();
80 aLoop->registerListener(this, Events_Error::errorID()); //!< Listening application errors.
81 aLoop->registerListener(this, Events_Loop::eventByName(Config_FeatureMessage::GUI_EVENT()));
82 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OPERATION_LAUNCHED));
83 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
84 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_CREATED));
85 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
86 aLoop->registerListener(this, Events_LongOp::eventID());
87 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_PLUGIN_LOADED));
88 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_SELFILTER_LOADED));
90 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_UPDATE_VIEWER_BLOCKED));
91 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_UPDATE_VIEWER_UNBLOCKED));
94 //******************************************************
95 void XGUI_WorkshopListener::processEvent(const std::shared_ptr<Events_Message>& theMessage)
97 if (QApplication::instance()->thread() != QThread::currentThread()) {
99 std::cout << "XGUI_Workshop::processEvent: " << "Working in another thread." << std::endl;
101 SessionPtr aMgr = ModelAPI_Session::get();
102 PostponeMessageQtEvent* aPostponeEvent = new PostponeMessageQtEvent(theMessage);
103 QApplication::postEvent(this, aPostponeEvent);
107 //A message to start feature creation received.
108 if (theMessage->eventID() == Events_Loop::loop()->eventByName(Config_FeatureMessage::GUI_EVENT())) {
109 std::shared_ptr<Config_FeatureMessage> aFeatureMsg =
110 std::dynamic_pointer_cast<Config_FeatureMessage>(theMessage);
111 if (!aFeatureMsg->isInternal()) {
112 addFeature(aFeatureMsg);
115 // Process creation of Part
116 else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_CREATED)) {
117 std::shared_ptr<ModelAPI_ObjectUpdatedMessage> aUpdMsg =
118 std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
119 onFeatureCreatedMsg(aUpdMsg);
121 XGUI_SalomeConnector* aSalomeConnector = workshop()->salomeConnector();
122 if (aSalomeConnector)
123 aSalomeConnector->createPreferences();
124 myUpdatePrefs = false;
127 else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_PLUGIN_LOADED)) {
128 myUpdatePrefs = true;
131 else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY)) {
132 std::shared_ptr<ModelAPI_ObjectUpdatedMessage> aUpdMsg =
133 std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
134 onFeatureRedisplayMsg(aUpdMsg);
136 //Update property panel on corresponding message. If there is no current operation (no
137 //property panel), or received message has different feature to the current - do nothing.
138 else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_UPDATED)) {
139 std::shared_ptr<ModelAPI_ObjectUpdatedMessage> anUpdateMsg =
140 std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
141 onFeatureUpdatedMsg(anUpdateMsg);
142 } else if (theMessage->eventID() == Events_LongOp::eventID()) {
143 if (Events_LongOp::isPerformed()) {
144 QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
146 QApplication::restoreOverrideCursor();
149 //An operation passed by message. Start it, process and commit.
150 else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OPERATION_LAUNCHED)) {
151 std::shared_ptr<Config_PointerMessage> aPartSetMsg =
152 std::dynamic_pointer_cast<Config_PointerMessage>(theMessage);
153 //myPropertyPanel->cleanContent();
154 ModuleBase_Operation* anOperation = (ModuleBase_Operation*) aPartSetMsg->pointer();
155 XGUI_OperationMgr* anOperationMgr = workshop()->operationMgr();
157 if (anOperationMgr->startOperation(anOperation)) {
158 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(anOperation);
160 workshop()->propertyPanel()->updateContentWidget(aFOperation->feature());
161 if (!anOperation->getDescription()->hasXmlRepresentation()) {
162 if (anOperation->commit())
163 workshop()->updateCommandStatus();
167 else if (theMessage->eventID() == Events_Loop::eventByName(EVENT_SELFILTER_LOADED)) {
168 std::shared_ptr<Config_SelectionFilterMessage> aMsg =
169 std::dynamic_pointer_cast<Config_SelectionFilterMessage>(theMessage);
171 ModuleBase_FilterFactory* aFactory = myWorkshop->selectionFilters();
172 if (!aMsg->attributeId().empty()) {
173 aFactory->assignFilter(aMsg->selectionFilterId(), aMsg->featureId(), aMsg->attributeId(),
177 } else if (theMessage->eventID() == Events_Loop::eventByName(EVENT_UPDATE_VIEWER_BLOCKED)) {
178 // the viewer's update context will not happens until viewer updated is emitted
179 workshop()->displayer()->enableUpdateViewer(false);
180 } else if (theMessage->eventID() == Events_Loop::eventByName(EVENT_UPDATE_VIEWER_UNBLOCKED)) {
181 // the viewer's update context is unblocked, the viewer's update works
182 XGUI_Displayer* aDisplayer = workshop()->displayer();
183 aDisplayer->enableUpdateViewer(true);
184 aDisplayer->updateViewer();
186 //Show error dialog if error message received.
187 std::shared_ptr<Events_Error> anAppError = std::dynamic_pointer_cast<Events_Error>(theMessage);
189 emit errorOccurred(QString::fromLatin1(anAppError->description()));
193 if (!workshop()->isSalomeMode()) {
194 SessionPtr aMgr = ModelAPI_Session::get();
195 AppElements_MainWindow* aMainWindow = workshop()->mainWindow();
196 if (aMgr->isModified() != aMainWindow->isModifiedState())
197 aMainWindow->setModifiedState(aMgr->isModified());
201 //******************************************************
202 void XGUI_WorkshopListener::onFeatureUpdatedMsg(
203 const std::shared_ptr<ModelAPI_ObjectUpdatedMessage>& theMsg)
205 std::set<ObjectPtr> aFeatures = theMsg->objects();
206 XGUI_OperationMgr* anOperationMgr = workshop()->operationMgr();
207 if (anOperationMgr->hasOperation()) {
208 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
209 (anOperationMgr->currentOperation());
211 FeaturePtr aCurrentFeature = aFOperation->feature();
212 std::set<ObjectPtr>::const_iterator aIt;
213 for (aIt = aFeatures.begin(); aIt != aFeatures.end(); ++aIt) {
214 ObjectPtr aNewFeature = (*aIt);
215 if (aNewFeature == aCurrentFeature) {
216 workshop()->propertyPanel()->updateContentWidget(aCurrentFeature);
222 anOperationMgr->onValidateOperation();
223 //if (myObjectBrowser)
224 // myObjectBrowser->processEvent(theMsg);
227 //******************************************************
228 void XGUI_WorkshopListener::onFeatureRedisplayMsg(const std::shared_ptr<ModelAPI_ObjectUpdatedMessage>& theMsg)
230 std::set<ObjectPtr> aObjects = theMsg->objects();
231 std::set<ObjectPtr>::const_iterator aIt;
233 #ifdef DEBUG_FEATURE_REDISPLAY
235 for (aIt = aObjects.begin(); aIt != aObjects.end(); ++aIt) {
236 anInfo.append(ModuleBase_Tools::objectInfo((*aIt)));
238 QString anInfoStr = anInfo.join(";\t");
239 qDebug(QString("onFeatureRedisplayMsg: %1, %2").arg(aObjects.size()).arg(anInfoStr).toStdString().c_str());
242 XGUI_Workshop* aWorkshop = workshop();
243 XGUI_Displayer* aDisplayer = aWorkshop->displayer();
244 for (aIt = aObjects.begin(); aIt != aObjects.end(); ++aIt) {
245 ObjectPtr aObj = (*aIt);
247 // Hide the object if it is invalid or concealed one
248 bool aHide = !aObj->data() || !aObj->data()->isValid() ||
249 aObj->isDisabled() || (!aObj->isDisplayed());
250 if (!aHide) { // check that this is not hidden result
251 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(aObj);
252 aHide = aRes && aRes->isConcealed();
254 #ifdef DEBUG_RESULT_COMPSOLID
255 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(aObj);
257 ResultCompSolidPtr aCompSolidRes = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(aRes);
258 if (aCompSolidRes.get()) {
259 qDebug(QString("COMPSOLID, numberOfSubs = %1").arg(aCompSolidRes->numberOfSubs()).toStdString().c_str());
261 if (ModelAPI_Tools::compSolidOwner(aRes))
262 qDebug("COMPSOLID sub-object");
266 aDisplayer->erase(aObj, false);
267 #ifdef DEBUG_FEATURE_REDISPLAY
268 // Redisplay the visible object or the object of the current operation
269 bool isVisibleObject = aDisplayer->isVisible(aObj);
271 QString anObjInfo = ModuleBase_Tools::objectInfo((aObj));
272 //qDebug(QString("visible=%1 : erase = %2").arg(isVisibleObject).arg(anObjInfo).toStdString().c_str());
276 // Redisplay the visible object or the object of the current operation
277 bool isVisibleObject = aDisplayer->isVisible(aObj);
278 #ifdef DEBUG_FEATURE_REDISPLAY
279 QString anObjInfo = ModuleBase_Tools::objectInfo((aObj));
280 //qDebug(QString("visible=%1 : display= %2").arg(isVisibleObject).arg(anObjInfo).toStdString().c_str());
281 /*FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
282 if (aFeature.get()) {
283 std::string aKind = aFeature->getKind();
284 if (aKind == "SketchMultiRotation")
289 if (isVisibleObject) { // redisplay visible object
290 //displayObject(aObj); // In order to update presentation
291 // in order to avoid the check whether the object can be redisplayed, the exact method
292 // of redisplay is called. This modification is made in order to have the line is updated
293 // by creation of a horizontal constraint on the line by preselection
294 if (ModelAPI_Tools::hasSubResults(std::dynamic_pointer_cast<ModelAPI_Result>(aObj))) {
295 aDisplayer->erase(aObj, false);
298 aDisplayer->redisplay(aObj, false);
299 // Deactivate object of current operation from selection
300 aWorkshop->deactivateActiveObject(aObj, false);
302 } else { // display object if the current operation has it
303 if (displayObject(aObj)) {
304 // Deactivate object of current operation from selection
305 aWorkshop->deactivateActiveObject(aObj, false);
310 aDisplayer->updateViewer();
312 //******************************************************
313 void XGUI_WorkshopListener::onFeatureCreatedMsg(const std::shared_ptr<ModelAPI_ObjectUpdatedMessage>& theMsg)
315 std::set<ObjectPtr> aObjects = theMsg->objects();
316 std::set<ObjectPtr>::const_iterator aIt;
317 #ifdef DEBUG_FEATURE_CREATED
319 for (aIt = aObjects.begin(); aIt != aObjects.end(); ++aIt) {
320 anInfo.append(ModuleBase_Tools::objectInfo((*aIt)));
322 QString anInfoStr = anInfo.join(";\t");
323 qDebug(QString("onFeatureCreatedMsg: %1, %2").arg(aObjects.size()).arg(anInfoStr).toStdString().c_str());
326 //bool aHasPart = false;
327 bool isDisplayed = false;
328 for (aIt = aObjects.begin(); aIt != aObjects.end(); ++aIt) {
329 ObjectPtr anObject = *aIt;
331 #ifdef DEBUG_RESULT_COMPSOLID
332 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
334 ResultCompSolidPtr aCompSolidRes = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(aRes);
335 if (aCompSolidRes.get()) {
336 qDebug(QString("COMPSOLID, numberOfSubs = %1").arg(aCompSolidRes->numberOfSubs()).toStdString().c_str());
338 if (ModelAPI_Tools::compSolidOwner(aRes))
339 qDebug("COMPSOLID sub-object");
342 // the validity of the data should be checked here in order to avoid display of the objects,
343 // which were created, then deleted, but flush for the creation event happens after that
344 // we should not display disabled objects
345 bool aHide = !anObject->data()->isValid() ||
346 anObject->isDisabled() ||
347 !anObject->isDisplayed();
349 // setDisplayed has to be called in order to synchronize internal state of the object
350 // with list of displayed objects
351 if (myWorkshop->module()->canDisplayObject(anObject)) {
352 anObject->setDisplayed(true);
353 isDisplayed = displayObject(*aIt);
355 anObject->setDisplayed(false);
358 //if (myObjectBrowser)
359 // myObjectBrowser->processEvent(theMsg);
361 workshop()->displayer()->updateViewer();
362 //if (aHasPart) { // TODO: Avoid activate last part on loading of document
363 // activateLastPart();
367 void XGUI_WorkshopListener::onNestedStateChanged(const std::string& theFeatureId, const bool theState)
369 XGUI_Workshop* aWorkshop = workshop();
371 //one button is used for all features, which can have nested actions, so it is obtained from
372 // the action manager
373 QAction* anAcceptAllAction = aWorkshop->actionsMgr()->operationStateAction(XGUI_ActionsMgr::AcceptAll, NULL);
374 if (aWorkshop->isSalomeMode()) {
375 XGUI_SalomeConnector* aSalomeConnector = aWorkshop->salomeConnector();
376 XGUI_ActionsMgr* anActionsMgr = aWorkshop->actionsMgr();
377 if (aSalomeConnector->isNestedFeature(anActionsMgr->action(theFeatureId.c_str())))
378 anAcceptAllAction->setEnabled(theState);
380 AppElements_MainMenu* aMenuBar = aWorkshop->mainWindow()->menuObject();
381 if (aMenuBar->feature(theFeatureId.c_str())->button()->additionalButtonWidget())
382 anAcceptAllAction->setEnabled(theState);
386 bool XGUI_WorkshopListener::event(QEvent * theEvent)
388 PostponeMessageQtEvent* aPostponedEv = dynamic_cast<PostponeMessageQtEvent*>(theEvent);
390 std::shared_ptr<Events_Message> aEventPtr = aPostponedEv->postponedMessage();
391 processEvent(aEventPtr);
397 void XGUI_WorkshopListener::addFeature(const std::shared_ptr<Config_FeatureMessage>& theMessage)
401 qDebug() << "XGUI_WorkshopListener::addFeature: NULL message.";
405 ActionInfo aFeatureInfo;
406 aFeatureInfo.initFrom(theMessage);
408 XGUI_Workshop* aWorkshop = workshop();
410 QString aWchName = QString::fromStdString(theMessage->workbenchId());
411 QStringList aNestedFeatures =
412 QString::fromStdString(theMessage->nestedFeatures()).split(" ", QString::SkipEmptyParts);
413 QString aDocKind = QString::fromStdString(theMessage->documentKind());
414 QList<QAction*> aNestedActList;
415 bool isColumnButton = !aNestedFeatures.isEmpty();
416 if (isColumnButton) {
417 QString aNestedActions = QString::fromStdString(theMessage->actionsWhenNested());
418 XGUI_OperationMgr* anOperationMgr = aWorkshop->operationMgr();
419 XGUI_ActionsMgr* anActionsMgr = aWorkshop->actionsMgr();
420 if (aNestedActions.contains(FEATURE_WHEN_NESTED_ACCEPT)) {
421 QAction* anAction = anActionsMgr->operationStateAction(XGUI_ActionsMgr::AcceptAll, NULL);
422 connect(anAction, SIGNAL(triggered()), anOperationMgr, SLOT(commitAllOperations()));
423 aNestedActList << anAction;
425 if (aNestedActions.contains(FEATURE_WHEN_NESTED_ABORT)) {
426 QAction* anAction = anActionsMgr->operationStateAction(XGUI_ActionsMgr::AbortAll, NULL);
427 connect(anAction, SIGNAL(triggered()), anOperationMgr, SLOT(abortAllOperations()));
428 aNestedActList << anAction;
432 if (aWorkshop->isSalomeMode()) {
433 XGUI_SalomeConnector* aSalomeConnector = aWorkshop->salomeConnector();
435 if (isColumnButton) {
436 aAction = aSalomeConnector->addNestedFeature(aWchName, aFeatureInfo, aNestedActList);
438 //Issue #650: in the SALOME mode the tooltip should be same as text
439 aFeatureInfo.toolTip = aFeatureInfo.text;
440 aAction = aSalomeConnector->addFeature(aWchName, aFeatureInfo);
442 aSalomeConnector->setNestedActions(aFeatureInfo.id, aNestedFeatures);
443 aSalomeConnector->setDocumentKind(aFeatureInfo.id, aDocKind);
445 aWorkshop->actionsMgr()->addCommand(aAction);
446 aWorkshop->module()->actionCreated(aAction);
448 //Find or create Workbench
449 AppElements_MainMenu* aMenuBar = aWorkshop->mainWindow()->menuObject();
450 AppElements_Workbench* aPage = aMenuBar->findWorkbench(aWchName);
452 aPage = aWorkshop->addWorkbench(aWchName);
454 //Find or create Group
455 QString aGroupName = QString::fromStdString(theMessage->groupId());
456 AppElements_MenuGroupPanel* aGroup = aPage->findGroup(aGroupName);
458 aGroup = aPage->addGroup(aGroupName);
460 // Check if hotkey sequence is already defined:
461 XGUI_ActionsMgr* anActionsMgr = aWorkshop->actionsMgr();
462 QKeySequence aHotKey = anActionsMgr->registerShortcut(aFeatureInfo.shortcut);
463 if(aHotKey != aFeatureInfo.shortcut) {
464 aFeatureInfo.shortcut = aHotKey;
467 AppElements_Command* aCommand = aGroup->addFeature(aFeatureInfo,
470 // Enrich created button with accept/abort buttons if necessary
471 AppElements_Button* aButton = aCommand->button();
472 if (aButton->isColumnButton()) {
473 aButton->setAdditionalButtons(aNestedActList);
475 aWorkshop->actionsMgr()->addCommand(aCommand);
476 aWorkshop->module()->actionCreated(aCommand);
478 XGUI_OperationMgr* anOperationMgr = workshop()->operationMgr();
479 connect(anOperationMgr, SIGNAL(nestedStateChanged(const std::string&, const bool)),
480 this, SLOT(onNestedStateChanged(const std::string&, const bool)));
484 //**************************************************************
485 bool XGUI_WorkshopListener::displayObject(ObjectPtr theObj)
487 #ifdef DEBUG_RESULT_COMPSOLID
488 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(theObj);
489 if (aRes.get() && (ModelAPI_Tools::hasSubResults(aRes) || ModelAPI_Tools::compSolidOwner(aRes))) {
490 ResultCompSolidPtr aCompSolidRes = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(aRes);
491 if (aCompSolidRes.get()) {
492 qDebug("COMPSOLID: displayObject");
497 XGUI_Workshop* aWorkshop = workshop();
498 // do not display the object if it has sub objects. They should be displayed separately.
499 if (!aWorkshop->module()->canDisplayObject(theObj) ||
500 ModelAPI_Tools::hasSubResults(std::dynamic_pointer_cast<ModelAPI_Result>(theObj)))
503 XGUI_Displayer* aDisplayer = aWorkshop->displayer();
504 ResultBodyPtr aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(theObj);
505 if (aBody.get() != NULL) {
506 int aNb = aDisplayer->objectsCount();
507 aDisplayer->display(theObj, false);
509 myWorkshop->viewer()->fitAll();
511 aDisplayer->display(theObj, false);
516 XGUI_Workshop* XGUI_WorkshopListener::workshop() const
518 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
519 return aConnector->workshop();