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"
15 #include <AppElements_MainWindow.h>
18 #include <ModuleBase_IModule.h>
19 #include <ModuleBase_Events.h>
21 #include <ModelAPI_Object.h>
22 #include <ModelAPI_Events.h>
23 #include <ModelAPI_Session.h>
24 #include <ModelAPI_Result.h>
25 #include <ModelAPI_Feature.h>
26 #include <ModelAPI_Data.h>
27 #include <ModelAPI_ResultBody.h>
28 #include <ModelAPI_ResultGroup.h>
29 #include <ModelAPI_ResultCompSolid.h>
30 #include <ModelAPI_Tools.h>
32 #include <Events_Loop.h>
33 #include <Events_LongOp.h>
35 #include <ModuleBase_IWorkshop.h>
37 #include <ModuleBase_Operation.h>
38 #include <ModuleBase_OperationDescription.h>
39 #include <ModuleBase_OperationFeature.h>
40 #include <ModuleBase_Tools.h>
41 #include <ModuleBase_IViewer.h>
42 #include <ModuleBase_WidgetSelector.h>
44 #include <Config_FeatureMessage.h>
45 #include <Config_PointerMessage.h>
46 #include <Config_Keywords.h>
47 #include <Events_InfoMessage.h>
49 #include <QApplication>
50 #include <QMainWindow>
59 //#define DEBUG_FEATURE_CREATED
60 //#define DEBUG_FEATURE_REDISPLAY
61 //#define DEBUG_FEATURE_UPDATED
62 //#define DEBUG_RESULT_COMPSOLID
64 #ifdef DEBUG_FEATURE_REDISPLAY
65 const std::string DebugFeatureKind = "";//"Extrusion";
68 XGUI_WorkshopListener::XGUI_WorkshopListener(ModuleBase_IWorkshop* theWorkshop)
69 : myWorkshop(theWorkshop),
72 XGUI_OperationMgr* anOperationMgr = workshop()->operationMgr();
75 //******************************************************
76 XGUI_WorkshopListener::~XGUI_WorkshopListener(void)
80 //******************************************************
81 void XGUI_WorkshopListener::initializeEventListening()
83 //Initialize event listening
84 Events_Loop* aLoop = Events_Loop::loop();
85 aLoop->registerListener(this, Events_InfoMessage::errorID()); //!< Listening application errors.
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));
92 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_UPDATE_VIEWER_BLOCKED));
93 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_UPDATE_VIEWER_UNBLOCKED));
94 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_EMPTY_AIS_PRESENTATION));
95 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_UPDATE_BY_WIDGET_SELECTION));
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 // Process creation of Part
112 if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_CREATED)) {
113 std::shared_ptr<ModelAPI_ObjectUpdatedMessage> aUpdMsg =
114 std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
115 onFeatureCreatedMsg(aUpdMsg);
117 XGUI_SalomeConnector* aSalomeConnector = workshop()->salomeConnector();
118 if (aSalomeConnector)
119 aSalomeConnector->createPreferences();
120 myUpdatePrefs = false;
123 else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_PLUGIN_LOADED)) {
124 myUpdatePrefs = true;
127 else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY)) {
128 std::shared_ptr<ModelAPI_ObjectUpdatedMessage> aUpdMsg =
129 std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
130 onFeatureRedisplayMsg(aUpdMsg);
131 } else if (theMessage->eventID() == Events_Loop::eventByName(EVENT_EMPTY_AIS_PRESENTATION)) {
132 std::shared_ptr<ModelAPI_ObjectUpdatedMessage> aUpdMsg =
133 std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
134 onFeatureEmptyPresentationMsg(aUpdMsg);
135 } else if (theMessage->eventID() == Events_Loop::eventByName(EVENT_UPDATE_BY_WIDGET_SELECTION)) {
136 ModuleBase_ModelWidget* aWidget = workshop()->propertyPanel()->activeWidget();
138 ModuleBase_WidgetSelector* aWidgetSelector =
139 dynamic_cast<ModuleBase_WidgetSelector*>(aWidget);
141 myWorkshop->setSelected(aWidgetSelector->getAttributeSelection());
145 //Update property panel on corresponding message. If there is no current operation (no
146 //property panel), or received message has different feature to the current - do nothing.
147 else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_UPDATED)) {
148 std::shared_ptr<ModelAPI_ObjectUpdatedMessage> anUpdateMsg =
149 std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
150 onFeatureUpdatedMsg(anUpdateMsg);
151 } else if (theMessage->eventID() == Events_LongOp::eventID()) {
152 if (Events_LongOp::isPerformed()) {
153 QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
155 QApplication::restoreOverrideCursor();
157 } else if (theMessage->eventID() == Events_Loop::eventByName(EVENT_UPDATE_VIEWER_BLOCKED)) {
158 // the viewer's update context will not happens until viewer updated is emitted
159 workshop()->displayer()->enableUpdateViewer(false);
160 } else if (theMessage->eventID() == Events_Loop::eventByName(EVENT_UPDATE_VIEWER_UNBLOCKED)) {
161 // the viewer's update context is unblocked, the viewer's update works
162 XGUI_Displayer* aDisplayer = workshop()->displayer();
163 aDisplayer->enableUpdateViewer(true);
165 //Show error dialog if error message received.
166 std::shared_ptr<Events_InfoMessage> anIngfoMsg =
167 std::dynamic_pointer_cast<Events_InfoMessage>(theMessage);
169 emit errorOccurred(anIngfoMsg);
174 SessionPtr aMgr = ModelAPI_Session::get();
175 AppElements_MainWindow* aMainWindow = workshop()->mainWindow();
176 if (aMgr->isModified() != aMainWindow->isModifiedState())
177 aMainWindow->setModifiedState(aMgr->isModified());
181 //******************************************************
182 void XGUI_WorkshopListener::onFeatureUpdatedMsg(
183 const std::shared_ptr<ModelAPI_ObjectUpdatedMessage>& theMsg)
185 #ifdef DEBUG_FEATURE_UPDATED
186 std::set<ObjectPtr> anObjects = theMsg->objects();
187 std::set<ObjectPtr>::const_iterator aIt;
189 for (aIt = anObjects.begin(); aIt != anObjects.end(); ++aIt) {
190 anInfo.append(ModuleBase_Tools::objectInfo((*aIt)));
192 QString anInfoStr = anInfo.join(";\t");
193 qDebug(QString("onFeatureUpdatedMsg: %1, %2")
194 .arg(anObjects.size()).arg(anInfoStr).toStdString().c_str());
196 std::set<ObjectPtr> aFeatures = theMsg->objects();
197 XGUI_OperationMgr* anOperationMgr = workshop()->operationMgr();
198 if (anOperationMgr->hasOperation()) {
199 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
200 (anOperationMgr->currentOperation());
202 FeaturePtr aCurrentFeature = aFOperation->feature();
203 std::set<ObjectPtr>::const_iterator aIt;
204 for (aIt = aFeatures.begin(); aIt != aFeatures.end(); ++aIt) {
205 ObjectPtr aNewFeature = (*aIt);
206 if (aNewFeature == aCurrentFeature) {
207 workshop()->propertyPanel()->updateContentWidget(aCurrentFeature);
213 //anOperationMgr->onValidateOperation();
215 //if (myObjectBrowser)
216 // myObjectBrowser->processEvent(theMsg);
219 //******************************************************
220 void XGUI_WorkshopListener::
221 onFeatureRedisplayMsg(const std::shared_ptr<ModelAPI_ObjectUpdatedMessage>& theMsg)
223 std::set<ObjectPtr> anObjects = theMsg->objects();
224 std::set<ObjectPtr>::const_iterator aIt;
226 #ifdef DEBUG_FEATURE_REDISPLAY
228 for (aIt = anObjects.begin(); aIt != anObjects.end(); ++aIt) {
229 anInfo.append(ModuleBase_Tools::objectInfo((*aIt)));
231 QString anInfoStr = anInfo.join(";\t");
232 qDebug(QString("onFeatureRedisplayMsg: %1, %2")
233 .arg(anObjects.size()).arg(anInfoStr).toStdString().c_str());
236 XGUI_Workshop* aWorkshop = workshop();
237 XGUI_Displayer* aDisplayer = aWorkshop->displayer();
238 //bool aFirstVisualizedBody = false;
239 bool aDoFitAll = false;
240 int aNbOfShownObjects = workshop()->displayer()->objectsCount();
241 bool aRedisplayed = false;
242 //std::list<ObjectPtr> aHiddenObjects;
243 for (aIt = anObjects.begin(); aIt != anObjects.end(); ++aIt) {
244 ObjectPtr aObj = (*aIt);
246 // Hide the object if it is invalid or concealed one
247 bool aHide = !aObj->data() || !aObj->data()->isValid() ||
248 aObj->isDisabled() || (!aObj->isDisplayed());
249 if (!aHide) { // check that this is not hidden result
250 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(aObj);
251 aHide = aRes && aRes->isConcealed();
253 // Hide the presentation with an empty shape. But isDisplayed state of the object should not
254 // be changed to the object becomes visible when the shape becomes not empty
255 if (!aHide && aRes.get())
256 aHide = !aRes->shape().get() || aRes->shape()->isNull();
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")
265 .arg(aCompSolidRes->numberOfSubs()).toStdString().c_str());
267 if (ModelAPI_Tools::compSolidOwner(aRes))
268 qDebug("COMPSOLID sub-object");
271 #ifdef DEBUG_FEATURE_REDISPLAY
272 QString anObjInfo = ModuleBase_Tools::objectInfo((aObj));
273 FeaturePtr aFeature = ModelAPI_Feature::feature(aObj);
274 if (aFeature.get()) {
275 std::string aKind = aFeature->getKind();
276 if (aKind == DebugFeatureKind || DebugFeatureKind.empty()) {
277 qDebug(QString("visible=%1, hide=%2 : display= %2").arg(aDisplayer->isVisible(aObj))
278 .arg(aHide).arg(anObjInfo).toStdString().c_str());
283 //we should provide objects which are hidden in the viewer, e.g. sketch always should
284 // visualizes all sub-features, if some features are to be hidden, sould be proposed may
285 // be to removed #1223
286 // aHiddenObjects.push_back(aObj);
287 aRedisplayed = aDisplayer->erase(aObj, false) || aRedisplayed;
288 #ifdef DEBUG_FEATURE_REDISPLAY
289 // Redisplay the visible object or the object of the current operation
290 bool isVisibleObject = aDisplayer->isVisible(aObj);
292 QString anObjInfo = ModuleBase_Tools::objectInfo((aObj));
296 // Redisplay the visible object or the object of the current operation
297 bool isVisibleObject = aDisplayer->isVisible(aObj);
298 #ifdef DEBUG_FEATURE_REDISPLAY
299 QString anObjInfo = ModuleBase_Tools::objectInfo((aObj));
302 if (isVisibleObject) { // redisplay visible object
303 //displayObject(aObj); // In order to update presentation
304 // in order to avoid the check whether the object can be redisplayed, the exact method
305 // of redisplay is called. This modification is made in order to have the line is updated
306 // by creation of a horizontal constraint on the line by preselection
307 if (ModelAPI_Tools::hasSubResults(std::dynamic_pointer_cast<ModelAPI_Result>(aObj))) {
308 aRedisplayed = aDisplayer->erase(aObj, false) || aRedisplayed;
311 aRedisplayed = aDisplayer->redisplay(aObj, false) || aRedisplayed;
312 // Deactivate object of current operation from selection
313 aWorkshop->deactivateActiveObject(aObj, false);
315 } else { // display object if the current operation has it
316 if (displayObject(aObj)) {
317 aDoFitAll = aDoFitAll || neededFitAll(aObj, aNbOfShownObjects);
320 // Deactivate object of current operation from selection
321 aWorkshop->deactivateActiveObject(aObj, false);
326 // this processing should be moved in another place in order to do not cause problems in
327 // flush messages chain
328 //if (aHiddenObjects.size() > 0)
329 // myWorkshop->module()->processHiddenObject(aHiddenObjects);
331 bool isCustomized = customizeCurrentObject(anObjects, aRedisplayed);
332 if (aRedisplayed || isCustomized) {
333 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_EMPTY_AIS_PRESENTATION));
335 //VSV FitAll updated viewer by itself
337 myWorkshop->viewer()->fitAll();
339 aDisplayer->updateViewer();
343 //******************************************************
344 void XGUI_WorkshopListener::
345 onFeatureCreatedMsg(const std::shared_ptr<ModelAPI_ObjectUpdatedMessage>& theMsg)
347 std::set<ObjectPtr> anObjects = theMsg->objects();
348 std::set<ObjectPtr>::const_iterator aIt;
349 #ifdef DEBUG_FEATURE_CREATED
351 for (aIt = anObjects.begin(); aIt != anObjects.end(); ++aIt) {
352 anInfo.append(ModuleBase_Tools::objectInfo((*aIt)));
354 QString anInfoStr = anInfo.join(";\t");
355 qDebug(QString("onFeatureCreatedMsg: %1, %2")
356 .arg(anObjects.size()).arg(anInfoStr).toStdString().c_str());
359 bool aDoFitAll = false;
360 int aNbOfShownObjects = workshop()->displayer()->objectsCount();
362 //bool aHasPart = false;
363 bool aDisplayed = false;
364 for (aIt = anObjects.begin(); aIt != anObjects.end(); ++aIt) {
365 ObjectPtr anObject = *aIt;
367 #ifdef DEBUG_RESULT_COMPSOLID
368 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
370 ResultCompSolidPtr aCompSolidRes = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(aRes);
371 if (aCompSolidRes.get()) {
372 qDebug(QString("COMPSOLID, numberOfSubs = %1")
373 .arg(aCompSolidRes->numberOfSubs()).toStdString().c_str());
375 if (ModelAPI_Tools::compSolidOwner(aRes))
376 qDebug("COMPSOLID sub-object");
379 // the validity of the data should be checked here in order to avoid display of the objects,
380 // which were created, then deleted, but flush for the creation event happens after that
381 // we should not display disabled objects
382 bool aHide = !anObject->data()->isValid() ||
383 anObject->isDisabled() ||
384 !anObject->isDisplayed();
385 if (!aHide) { // check that this is not hidden result
386 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
387 bool isConcealed = aRes && aRes->isConcealed();
388 aHide = aRes && aRes->isConcealed();
389 // Hide the presentation with an empty shape. But isDisplayed state of the object should not
390 // be changed to the object becomes visible when the shape becomes not empty
391 if (!aHide && aRes.get())
392 aHide = !aRes->shape().get() || aRes->shape()->isNull();
395 // setDisplayed has to be called in order to synchronize internal state of the object
396 // with list of displayed objects
397 if (myWorkshop->module()->canDisplayObject(anObject)) {
398 anObject->setDisplayed(true);
399 aDisplayed = displayObject(anObject);
401 aDoFitAll = aDoFitAll || neededFitAll(anObject, aNbOfShownObjects);
403 anObject->setDisplayed(false);
407 bool isCustomized = customizeCurrentObject(anObjects, aDisplayed);
409 //if (myObjectBrowser)
410 // myObjectBrowser->processEvent(theMsg);
412 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_EMPTY_AIS_PRESENTATION));
413 //VSV FitAll updated viewer by itself
415 myWorkshop->viewer()->fitAll();
417 workshop()->displayer()->updateViewer();
419 //if (aHasPart) { // TODO: Avoid activate last part on loading of document
420 // activateLastPart();
424 //******************************************************
425 void XGUI_WorkshopListener::onFeatureEmptyPresentationMsg(
426 const std::shared_ptr<ModelAPI_ObjectUpdatedMessage>& theMsg)
428 std::set<ObjectPtr> anObjects = theMsg->objects();
429 std::set<ObjectPtr>::const_iterator aIt;
430 #ifdef DEBUG_FEATURE_CREATED
432 for (aIt = anObjects.begin(); aIt != anObjects.end(); ++aIt) {
433 anInfo.append(ModuleBase_Tools::objectInfo((*aIt)));
435 QString anInfoStr = anInfo.join(";\t");
436 qDebug(QString("onFeatureEmptyPresentationMsg: %1, %2")
437 .arg(anObjects.size()).arg(anInfoStr).toStdString().c_str());
440 XGUI_Workshop* aWorkshop = workshop();
441 XGUI_Displayer* aDisplayer = aWorkshop->displayer();
443 bool aRedisplayed = false;
444 for (aIt = anObjects.begin(); aIt != anObjects.end(); ++aIt) {
445 ObjectPtr anObject = *aIt;
446 aRedisplayed = aDisplayer->erase(anObject, false) || aRedisplayed;
450 aDisplayer->updateViewer();
453 bool XGUI_WorkshopListener::event(QEvent * theEvent)
455 PostponeMessageQtEvent* aPostponedEv = dynamic_cast<PostponeMessageQtEvent*>(theEvent);
457 std::shared_ptr<Events_Message> aEventPtr = aPostponedEv->postponedMessage();
458 processEvent(aEventPtr);
464 //**************************************************************
465 bool XGUI_WorkshopListener::displayObject(ObjectPtr theObj)
467 #ifdef DEBUG_RESULT_COMPSOLID
468 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(theObj);
469 if (aRes.get() && (ModelAPI_Tools::hasSubResults(aRes) || ModelAPI_Tools::compSolidOwner(aRes))) {
470 ResultCompSolidPtr aCompSolidRes = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(aRes);
471 if (aCompSolidRes.get()) {
472 qDebug("COMPSOLID: displayObject");
477 bool aDisplayed = false;
478 XGUI_Workshop* aWorkshop = workshop();
479 // do not display the object if it has sub objects. They should be displayed separately.
480 if (!aWorkshop->module()->canDisplayObject(theObj) ||
481 ModelAPI_Tools::hasSubResults(std::dynamic_pointer_cast<ModelAPI_Result>(theObj)))
484 XGUI_Displayer* aDisplayer = aWorkshop->displayer();
485 int aNb = aDisplayer->objectsCount();
486 return aDisplayer->display(theObj, false);
489 //**************************************************************
490 bool XGUI_WorkshopListener::neededFitAll(ObjectPtr theObj, const int theNbOfShownObjects)
492 bool aFirstVisualizedBody = false;
494 if (theNbOfShownObjects == 0) {
495 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObj);
497 std::string aResultGroupName = aResult->groupName();
498 if (aResultGroupName == ModelAPI_ResultBody::group() ||
499 aResultGroupName == ModelAPI_ResultGroup::group()) {
500 std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aResult);
501 aFirstVisualizedBody = aShapePtr.get() != NULL;
505 return aFirstVisualizedBody;
508 bool XGUI_WorkshopListener::customizeCurrentObject(const std::set<ObjectPtr>& theObjects,
509 bool theForceRedisplay)
511 XGUI_OperationMgr* anOperationMgr = workshop()->operationMgr();
512 FeaturePtr aCurrentFeature;
513 if (anOperationMgr->hasOperation()) {
514 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
515 (anOperationMgr->currentOperation());
517 aCurrentFeature = aFOperation->feature();
521 bool aCustomized = false;
522 if (aCurrentFeature.get()) {
523 // the customize presentation should be redisplayed if force redislayed is true or
524 // if a list of message objects contains the operation feature for case when
525 // the feature is hidden, but arguments of the feature are modified
526 // e.g. extrusion is hidden(h=0) but sketch is chosen
527 if (theForceRedisplay || theObjects.find(aCurrentFeature) != theObjects.end()) {
528 aCustomized = myWorkshop->module()->customizeObject(aCurrentFeature,
529 ModuleBase_IModule::CustomizeArguments, false) || aCustomized;
530 aCustomized = myWorkshop->module()->customizeObject(aCurrentFeature,
531 ModuleBase_IModule::CustomizeResults, false) || aCustomized;
532 aCustomized = myWorkshop->module()->customizeObject(aCurrentFeature,
533 ModuleBase_IModule::CustomizeHighlightedObjects, false) || aCustomized;
539 XGUI_Workshop* XGUI_WorkshopListener::workshop() const
541 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
542 return aConnector->workshop();