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_ErrorMgr.h"
7 #include "XGUI_OperationMgr.h"
8 #include "XGUI_SalomeConnector.h"
9 #include "XGUI_ActionsMgr.h"
10 #include "XGUI_PropertyPanel.h"
11 #include "XGUI_ModuleConnector.h"
12 #include "XGUI_QtEvents.h"
13 #include "XGUI_SelectionMgr.h"
16 #include <AppElements_MainWindow.h>
19 #include <ModuleBase_IModule.h>
20 #include <ModuleBase_Events.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_ResultGroup.h>
30 #include <ModelAPI_ResultCompSolid.h>
31 #include <ModelAPI_Tools.h>
33 #include <Events_Loop.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_WidgetSelector.h>
45 #include <Config_FeatureMessage.h>
46 #include <Config_PointerMessage.h>
47 #include <Config_Keywords.h>
48 #include <Events_InfoMessage.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 #ifdef DEBUG_FEATURE_REDISPLAY
66 const std::string DebugFeatureKind = "";//"Extrusion";
69 XGUI_WorkshopListener::XGUI_WorkshopListener(ModuleBase_IWorkshop* theWorkshop)
70 : myWorkshop(theWorkshop),
73 XGUI_OperationMgr* anOperationMgr = workshop()->operationMgr();
76 //******************************************************
77 XGUI_WorkshopListener::~XGUI_WorkshopListener(void)
81 //******************************************************
82 void XGUI_WorkshopListener::initializeEventListening()
84 //Initialize event listening
85 Events_Loop* aLoop = Events_Loop::loop();
86 aLoop->registerListener(this, Events_InfoMessage::errorID()); //!< Listening application errors.
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));
93 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_UPDATE_VIEWER_BLOCKED));
94 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_UPDATE_VIEWER_UNBLOCKED));
95 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_EMPTY_AIS_PRESENTATION));
96 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_UPDATE_BY_WIDGET_SELECTION));
99 //******************************************************
100 void XGUI_WorkshopListener::processEvent(const std::shared_ptr<Events_Message>& theMessage)
102 if (QApplication::instance()->thread() != QThread::currentThread()) {
104 std::cout << "XGUI_Workshop::processEvent: " << "Working in another thread." << std::endl;
106 SessionPtr aMgr = ModelAPI_Session::get();
107 PostponeMessageQtEvent* aPostponeEvent = new PostponeMessageQtEvent(theMessage);
108 QApplication::postEvent(this, aPostponeEvent);
112 // Process creation of Part
113 if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_CREATED)) {
114 std::shared_ptr<ModelAPI_ObjectUpdatedMessage> aUpdMsg =
115 std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
116 onFeatureCreatedMsg(aUpdMsg);
118 XGUI_SalomeConnector* aSalomeConnector = workshop()->salomeConnector();
119 if (aSalomeConnector)
120 aSalomeConnector->createPreferences();
121 myUpdatePrefs = false;
124 else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_PLUGIN_LOADED)) {
125 myUpdatePrefs = true;
128 else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY)) {
129 std::shared_ptr<ModelAPI_ObjectUpdatedMessage> aUpdMsg =
130 std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
131 onFeatureRedisplayMsg(aUpdMsg);
132 } else if (theMessage->eventID() == Events_Loop::eventByName(EVENT_EMPTY_AIS_PRESENTATION)) {
133 std::shared_ptr<ModelAPI_ObjectUpdatedMessage> aUpdMsg =
134 std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
135 onFeatureEmptyPresentationMsg(aUpdMsg);
136 } else if (theMessage->eventID() == Events_Loop::eventByName(EVENT_UPDATE_BY_WIDGET_SELECTION)) {
137 ModuleBase_ModelWidget* aWidget = workshop()->propertyPanel()->activeWidget();
139 ModuleBase_WidgetSelector* aWidgetSelector =
140 dynamic_cast<ModuleBase_WidgetSelector*>(aWidget);
142 workshop()->selector()->setSelected(aWidgetSelector->getAttributeSelection());
146 //Update property panel on corresponding message. If there is no current operation (no
147 //property panel), or received message has different feature to the current - do nothing.
148 else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_UPDATED)) {
149 std::shared_ptr<ModelAPI_ObjectUpdatedMessage> anUpdateMsg =
150 std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
151 onFeatureUpdatedMsg(anUpdateMsg);
152 } else if (theMessage->eventID() == Events_LongOp::eventID()) {
153 if (Events_LongOp::isPerformed()) {
154 QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
156 QApplication::restoreOverrideCursor();
158 } else if (theMessage->eventID() == Events_Loop::eventByName(EVENT_UPDATE_VIEWER_BLOCKED)) {
159 // the viewer's update context will not happens until viewer updated is emitted
160 workshop()->displayer()->enableUpdateViewer(false);
161 } else if (theMessage->eventID() == Events_Loop::eventByName(EVENT_UPDATE_VIEWER_UNBLOCKED)) {
162 // the viewer's update context is unblocked, the viewer's update works
163 XGUI_Displayer* aDisplayer = workshop()->displayer();
164 aDisplayer->enableUpdateViewer(true);
166 //Show error dialog if error message received.
167 std::shared_ptr<Events_InfoMessage> anIngfoMsg =
168 std::dynamic_pointer_cast<Events_InfoMessage>(theMessage);
170 emit errorOccurred(anIngfoMsg);
175 SessionPtr aMgr = ModelAPI_Session::get();
176 AppElements_MainWindow* aMainWindow = workshop()->mainWindow();
177 if (aMgr->isModified() != aMainWindow->isModifiedState())
178 aMainWindow->setModifiedState(aMgr->isModified());
182 //******************************************************
183 void XGUI_WorkshopListener::onFeatureUpdatedMsg(
184 const std::shared_ptr<ModelAPI_ObjectUpdatedMessage>& theMsg)
186 #ifdef DEBUG_FEATURE_UPDATED
187 std::set<ObjectPtr> anObjects = theMsg->objects();
188 std::set<ObjectPtr>::const_iterator aIt;
190 for (aIt = anObjects.begin(); aIt != anObjects.end(); ++aIt) {
191 anInfo.append(ModuleBase_Tools::objectInfo((*aIt)));
193 QString anInfoStr = anInfo.join(";\t");
194 qDebug(QString("onFeatureUpdatedMsg: %1, %2")
195 .arg(anObjects.size()).arg(anInfoStr).toStdString().c_str());
197 std::set<ObjectPtr> aFeatures = theMsg->objects();
198 XGUI_OperationMgr* anOperationMgr = workshop()->operationMgr();
199 if (anOperationMgr->hasOperation()) {
200 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
201 (anOperationMgr->currentOperation());
203 FeaturePtr aCurrentFeature = aFOperation->feature();
204 std::set<ObjectPtr>::const_iterator aIt;
205 for (aIt = aFeatures.begin(); aIt != aFeatures.end(); ++aIt) {
206 ObjectPtr aNewFeature = (*aIt);
207 if (aNewFeature == aCurrentFeature) {
208 workshop()->propertyPanel()->updateContentWidget(aCurrentFeature);
214 //anOperationMgr->onValidateOperation();
216 //if (myObjectBrowser)
217 // myObjectBrowser->processEvent(theMsg);
220 //******************************************************
221 void XGUI_WorkshopListener::
222 onFeatureRedisplayMsg(const std::shared_ptr<ModelAPI_ObjectUpdatedMessage>& theMsg)
224 std::set<ObjectPtr> anObjects = theMsg->objects();
225 std::set<ObjectPtr>::const_iterator aIt;
227 #ifdef DEBUG_FEATURE_REDISPLAY
229 for (aIt = anObjects.begin(); aIt != anObjects.end(); ++aIt) {
230 anInfo.append(ModuleBase_Tools::objectInfo((*aIt)));
232 QString anInfoStr = anInfo.join(";\t");
233 qDebug(QString("onFeatureRedisplayMsg: %1, %2")
234 .arg(anObjects.size()).arg(anInfoStr).toStdString().c_str());
237 XGUI_Workshop* aWorkshop = workshop();
238 XGUI_Displayer* aDisplayer = aWorkshop->displayer();
239 //bool aFirstVisualizedBody = false;
240 bool aDoFitAll = false;
241 int aNbOfShownObjects = workshop()->displayer()->objectsCount();
242 bool aRedisplayed = false;
243 //std::list<ObjectPtr> aHiddenObjects;
244 for (aIt = anObjects.begin(); aIt != anObjects.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 // Hide the presentation with an empty shape. But isDisplayed state of the object should not
255 // be changed to the object becomes visible when the shape becomes not empty
256 if (!aHide && aRes.get())
257 aHide = !aRes->shape().get() || aRes->shape()->isNull();
260 #ifdef DEBUG_RESULT_COMPSOLID
261 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(aObj);
263 ResultCompSolidPtr aCompSolidRes = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(aRes);
264 if (aCompSolidRes.get()) {
265 qDebug(QString("COMPSOLID, numberOfSubs = %1")
266 .arg(aCompSolidRes->numberOfSubs()).toStdString().c_str());
268 if (ModelAPI_Tools::compSolidOwner(aRes))
269 qDebug("COMPSOLID sub-object");
272 #ifdef DEBUG_FEATURE_REDISPLAY
273 QString anObjInfo = ModuleBase_Tools::objectInfo((aObj));
274 FeaturePtr aFeature = ModelAPI_Feature::feature(aObj);
275 if (aFeature.get()) {
276 std::string aKind = aFeature->getKind();
277 if (aKind == DebugFeatureKind || DebugFeatureKind.empty()) {
278 qDebug(QString("visible=%1, hide=%2 : display= %2").arg(aDisplayer->isVisible(aObj))
279 .arg(aHide).arg(anObjInfo).toStdString().c_str());
284 //we should provide objects which are hidden in the viewer, e.g. sketch always should
285 // visualizes all sub-features, if some features are to be hidden, sould be proposed may
286 // be to removed #1223
287 // aHiddenObjects.push_back(aObj);
288 aRedisplayed = aDisplayer->erase(aObj, false) || aRedisplayed;
289 #ifdef DEBUG_FEATURE_REDISPLAY
290 // Redisplay the visible object or the object of the current operation
291 bool isVisibleObject = aDisplayer->isVisible(aObj);
293 QString anObjInfo = ModuleBase_Tools::objectInfo((aObj));
297 // Redisplay the visible object or the object of the current operation
298 bool isVisibleObject = aDisplayer->isVisible(aObj);
299 #ifdef DEBUG_FEATURE_REDISPLAY
300 QString anObjInfo = ModuleBase_Tools::objectInfo((aObj));
303 if (isVisibleObject) { // redisplay visible object
304 //displayObject(aObj); // In order to update presentation
305 // in order to avoid the check whether the object can be redisplayed, the exact method
306 // of redisplay is called. This modification is made in order to have the line is updated
307 // by creation of a horizontal constraint on the line by preselection
308 if (ModelAPI_Tools::hasSubResults(std::dynamic_pointer_cast<ModelAPI_Result>(aObj))) {
309 aRedisplayed = aDisplayer->erase(aObj, false) || aRedisplayed;
312 aRedisplayed = aDisplayer->redisplay(aObj, false) || aRedisplayed;
313 // Deactivate object of current operation from selection
314 aWorkshop->deactivateActiveObject(aObj, false);
316 } else { // display object if the current operation has it
317 if (displayObject(aObj)) {
318 aDoFitAll = aDoFitAll || neededFitAll(aObj, aNbOfShownObjects);
321 // Deactivate object of current operation from selection
322 aWorkshop->deactivateActiveObject(aObj, false);
327 // this processing should be moved in another place in order to do not cause problems in
328 // flush messages chain
329 //if (aHiddenObjects.size() > 0)
330 // myWorkshop->module()->processHiddenObject(aHiddenObjects);
332 bool isCustomized = customizeCurrentObject(anObjects, aRedisplayed);
333 if (aRedisplayed || isCustomized) {
334 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_EMPTY_AIS_PRESENTATION));
336 //VSV FitAll updated viewer by itself
338 myWorkshop->viewer()->fitAll();
340 aDisplayer->updateViewer();
344 //******************************************************
345 void XGUI_WorkshopListener::
346 onFeatureCreatedMsg(const std::shared_ptr<ModelAPI_ObjectUpdatedMessage>& theMsg)
348 std::set<ObjectPtr> anObjects = theMsg->objects();
349 std::set<ObjectPtr>::const_iterator aIt;
350 #ifdef DEBUG_FEATURE_CREATED
352 for (aIt = anObjects.begin(); aIt != anObjects.end(); ++aIt) {
353 anInfo.append(ModuleBase_Tools::objectInfo((*aIt)));
355 QString anInfoStr = anInfo.join(";\t");
356 qDebug(QString("onFeatureCreatedMsg: %1, %2")
357 .arg(anObjects.size()).arg(anInfoStr).toStdString().c_str());
360 bool aDoFitAll = false;
361 int aNbOfShownObjects = workshop()->displayer()->objectsCount();
363 //bool aHasPart = false;
364 bool aDisplayed = false;
365 for (aIt = anObjects.begin(); aIt != anObjects.end(); ++aIt) {
366 ObjectPtr anObject = *aIt;
368 #ifdef DEBUG_RESULT_COMPSOLID
369 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
371 ResultCompSolidPtr aCompSolidRes = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(aRes);
372 if (aCompSolidRes.get()) {
373 qDebug(QString("COMPSOLID, numberOfSubs = %1")
374 .arg(aCompSolidRes->numberOfSubs()).toStdString().c_str());
376 if (ModelAPI_Tools::compSolidOwner(aRes))
377 qDebug("COMPSOLID sub-object");
380 // the validity of the data should be checked here in order to avoid display of the objects,
381 // which were created, then deleted, but flush for the creation event happens after that
382 // we should not display disabled objects
383 bool aHide = !anObject->data()->isValid() ||
384 anObject->isDisabled() ||
385 !anObject->isDisplayed();
386 if (!aHide) { // check that this is not hidden result
387 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
388 bool isConcealed = aRes && aRes->isConcealed();
389 aHide = aRes && aRes->isConcealed();
390 // Hide the presentation with an empty shape. But isDisplayed state of the object should not
391 // be changed to the object becomes visible when the shape becomes not empty
392 if (!aHide && aRes.get())
393 aHide = !aRes->shape().get() || aRes->shape()->isNull();
396 // setDisplayed has to be called in order to synchronize internal state of the object
397 // with list of displayed objects
398 if (myWorkshop->module()->canDisplayObject(anObject)) {
399 anObject->setDisplayed(true);
400 aDisplayed = displayObject(anObject);
402 aDoFitAll = aDoFitAll || neededFitAll(anObject, aNbOfShownObjects);
404 anObject->setDisplayed(false);
408 bool isCustomized = customizeCurrentObject(anObjects, aDisplayed);
410 //if (myObjectBrowser)
411 // myObjectBrowser->processEvent(theMsg);
413 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_EMPTY_AIS_PRESENTATION));
414 //VSV FitAll updated viewer by itself
416 myWorkshop->viewer()->fitAll();
418 workshop()->displayer()->updateViewer();
420 //if (aHasPart) { // TODO: Avoid activate last part on loading of document
421 // activateLastPart();
425 //******************************************************
426 void XGUI_WorkshopListener::onFeatureEmptyPresentationMsg(
427 const std::shared_ptr<ModelAPI_ObjectUpdatedMessage>& theMsg)
429 std::set<ObjectPtr> anObjects = theMsg->objects();
430 std::set<ObjectPtr>::const_iterator aIt;
431 #ifdef DEBUG_FEATURE_CREATED
433 for (aIt = anObjects.begin(); aIt != anObjects.end(); ++aIt) {
434 anInfo.append(ModuleBase_Tools::objectInfo((*aIt)));
436 QString anInfoStr = anInfo.join(";\t");
437 qDebug(QString("onFeatureEmptyPresentationMsg: %1, %2")
438 .arg(anObjects.size()).arg(anInfoStr).toStdString().c_str());
441 XGUI_Workshop* aWorkshop = workshop();
442 XGUI_Displayer* aDisplayer = aWorkshop->displayer();
444 bool aRedisplayed = false;
445 for (aIt = anObjects.begin(); aIt != anObjects.end(); ++aIt) {
446 ObjectPtr anObject = *aIt;
447 aRedisplayed = aDisplayer->erase(anObject, false) || aRedisplayed;
451 aDisplayer->updateViewer();
454 bool XGUI_WorkshopListener::event(QEvent * theEvent)
456 PostponeMessageQtEvent* aPostponedEv = dynamic_cast<PostponeMessageQtEvent*>(theEvent);
458 std::shared_ptr<Events_Message> aEventPtr = aPostponedEv->postponedMessage();
459 processEvent(aEventPtr);
465 //**************************************************************
466 bool XGUI_WorkshopListener::displayObject(ObjectPtr theObj)
468 #ifdef DEBUG_RESULT_COMPSOLID
469 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(theObj);
470 if (aRes.get() && (ModelAPI_Tools::hasSubResults(aRes) || ModelAPI_Tools::compSolidOwner(aRes))) {
471 ResultCompSolidPtr aCompSolidRes = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(aRes);
472 if (aCompSolidRes.get()) {
473 qDebug("COMPSOLID: displayObject");
478 bool aDisplayed = false;
479 XGUI_Workshop* aWorkshop = workshop();
480 // do not display the object if it has sub objects. They should be displayed separately.
481 if (!aWorkshop->module()->canDisplayObject(theObj) ||
482 ModelAPI_Tools::hasSubResults(std::dynamic_pointer_cast<ModelAPI_Result>(theObj)))
485 XGUI_Displayer* aDisplayer = aWorkshop->displayer();
486 int aNb = aDisplayer->objectsCount();
487 return aDisplayer->display(theObj, false);
490 //**************************************************************
491 bool XGUI_WorkshopListener::neededFitAll(ObjectPtr theObj, const int theNbOfShownObjects)
493 bool aFirstVisualizedBody = false;
495 if (theNbOfShownObjects == 0) {
496 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObj);
498 std::string aResultGroupName = aResult->groupName();
499 if (aResultGroupName == ModelAPI_ResultBody::group() ||
500 aResultGroupName == ModelAPI_ResultGroup::group()) {
501 std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aResult);
502 aFirstVisualizedBody = aShapePtr.get() != NULL;
506 return aFirstVisualizedBody;
509 bool XGUI_WorkshopListener::customizeCurrentObject(const std::set<ObjectPtr>& theObjects,
510 bool theForceRedisplay)
512 XGUI_OperationMgr* anOperationMgr = workshop()->operationMgr();
513 FeaturePtr aCurrentFeature;
514 if (anOperationMgr->hasOperation()) {
515 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
516 (anOperationMgr->currentOperation());
518 aCurrentFeature = aFOperation->feature();
522 bool aCustomized = false;
523 if (aCurrentFeature.get()) {
524 // the customize presentation should be redisplayed if force redislayed is true or
525 // if a list of message objects contains the operation feature for case when
526 // the feature is hidden, but arguments of the feature are modified
527 // e.g. extrusion is hidden(h=0) but sketch is chosen
528 if (theForceRedisplay || theObjects.find(aCurrentFeature) != theObjects.end()) {
529 aCustomized = myWorkshop->module()->customizeObject(aCurrentFeature,
530 ModuleBase_IModule::CustomizeArguments, false) || aCustomized;
531 aCustomized = myWorkshop->module()->customizeObject(aCurrentFeature,
532 ModuleBase_IModule::CustomizeResults, false) || aCustomized;
533 aCustomized = myWorkshop->module()->customizeObject(aCurrentFeature,
534 ModuleBase_IModule::CustomizeHighlightedObjects, false) || aCustomized;
540 XGUI_Workshop* XGUI_WorkshopListener::workshop() const
542 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
543 return aConnector->workshop();