Salome HOME
368d7dc0ef0b0ec8553aa1a6d92c340d08e6d583
[modules/shaper.git] / src / XGUI / XGUI_WorkshopListener.cpp
1 // Copyright (C) 2014-2023  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "XGUI_WorkshopListener.h"
21
22 #ifndef HAVE_SALOME
23 #include <AppElements_MainWindow.h>
24 #endif
25
26 #include <Config_FeatureMessage.h>
27 #include <Config_PointerMessage.h>
28 #include <Config_Keywords.h>
29
30 #include <Events_InfoMessage.h>
31 #include <Events_Loop.h>
32 #include <Events_LongOp.h>
33
34 #include <ModelAPI_Object.h>
35 #include <ModelAPI_Events.h>
36 #include <ModelAPI_Session.h>
37 #include <ModelAPI_Result.h>
38 #include <ModelAPI_Feature.h>
39 #include <ModelAPI_Data.h>
40 #include <ModelAPI_Tools.h>
41 #include <ModelAPI_ResultField.h>
42
43 #include <ModuleBase_Events.h>
44 #include <ModuleBase_IModule.h>
45 #include <ModuleBase_IViewer.h>
46 #include <ModuleBase_IWorkshop.h>
47 #include <ModuleBase_Operation.h>
48 #include <ModuleBase_OperationDescription.h>
49 #include <ModuleBase_OperationFeature.h>
50 #include <ModuleBase_Tools.h>
51 #include <ModuleBase_WidgetSelector.h>
52
53 #include <SUIT_Application.h>
54 #include <SUIT_Session.h>
55
56 #include "XGUI_ActionsMgr.h"
57 #include "XGUI_Displayer.h"
58 #include "XGUI_ErrorMgr.h"
59 #include "XGUI_FacesPanel.h"
60 #include "XGUI_OperationMgr.h"
61 #include "XGUI_ModuleConnector.h"
62 #include "XGUI_PropertyPanel.h"
63 #include "XGUI_QtEvents.h"
64 #include "XGUI_SalomeConnector.h"
65 #include "XGUI_SelectionMgr.h"
66 #include "XGUI_Workshop.h"
67
68 #include <QAction>
69 #include <QApplication>
70 #include <QMainWindow>
71 #include <QThread>
72
73 #ifdef _DEBUG
74 #include <QDebug>
75 #include <iostream>
76 #endif
77
78 #ifdef WIN32
79 #pragma warning(disable : 4189) // for declaration of unused variables (MAYBE_UNUSED)
80 #endif
81
82 //#define DEBUG_FEATURE_CREATED
83 //#define DEBUG_FEATURE_REDISPLAY
84 //#define DEBUG_FEATURE_UPDATED
85 //#define DEBUG_RESULT_COMPSOLID
86
87 #ifdef DEBUG_FEATURE_REDISPLAY
88 const std::string DebugFeatureKind = "";//"Extrusion";
89 #endif
90
91 XGUI_WorkshopListener::XGUI_WorkshopListener(XGUI_Workshop* theWorkshop)
92   : myWorkshop(theWorkshop),
93     myUpdatePrefs(false)
94 {
95 }
96
97 //******************************************************
98 XGUI_WorkshopListener::~XGUI_WorkshopListener(void)
99 {
100 }
101
102 //******************************************************
103 void XGUI_WorkshopListener::initializeEventListening()
104 {
105   //Initialize event listening
106   Events_Loop* aLoop = Events_Loop::loop();
107   aLoop->registerListener(this, Events_InfoMessage::errorID());  //!< Listening application errors.
108   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
109   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_CREATED));
110   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
111   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_VISUAL_ATTRIBUTES));
112   aLoop->registerListener(this, Events_LongOp::eventID());
113   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_PLUGIN_LOADED));
114
115   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_UPDATE_VIEWER_BLOCKED));
116   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_UPDATE_VIEWER_UNBLOCKED));
117   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_EMPTY_AIS_PRESENTATION));
118   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_UPDATE_BY_WIDGET_SELECTION));
119
120   aLoop->registerListener(this, Events_Loop::eventByName("FinishOperation"));
121   aLoop->registerListener(this, Events_Loop::eventByName("AbortOperation"));
122   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_AUTOMATIC_RECOMPUTATION_ENABLE));
123   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_AUTOMATIC_RECOMPUTATION_DISABLE));
124   aLoop->registerListener(this, ModelAPI_ObjectRenamedMessage::eventId());
125 }
126
127 //******************************************************
128 void XGUI_WorkshopListener::processEvent(const std::shared_ptr<Events_Message>& theMessage)
129 {
130   if (QApplication::instance() &&
131       QApplication::instance()->thread() != QThread::currentThread()) {
132     #ifdef _DEBUG
133     std::cout << "XGUI_Workshop::processEvent: " << "Working in another thread." << std::endl;
134     #endif
135     SessionPtr aMgr = ModelAPI_Session::get();
136     PostponeMessageQtEvent* aPostponeEvent = new PostponeMessageQtEvent(theMessage);
137     QApplication::postEvent(this, aPostponeEvent);
138     return;
139   }
140
141   // Process creation of Part
142   if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_CREATED)) {
143     std::shared_ptr<ModelAPI_ObjectUpdatedMessage> aUpdMsg =
144         std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
145     onFeatureCreatedMsg(aUpdMsg);
146     if (myUpdatePrefs) {
147       XGUI_SalomeConnector* aSalomeConnector = workshop()->salomeConnector();
148       if (aSalomeConnector)
149         aSalomeConnector->createPreferences();
150       myUpdatePrefs = false;
151     }
152   }
153   else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_PLUGIN_LOADED)) {
154     myUpdatePrefs = true;
155   }
156   // Redisplay feature
157   else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY)) {
158     std::shared_ptr<ModelAPI_ObjectUpdatedMessage> aUpdMsg =
159       std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
160     onFeatureRedisplayMsg(aUpdMsg);
161   }
162   else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_VISUAL_ATTRIBUTES)) {
163     std::shared_ptr<ModelAPI_ObjectUpdatedMessage> aUpdMsg =
164       std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
165     std::set<ObjectPtr> aObjList = aUpdMsg->objects();
166     std::set<ObjectPtr>::const_iterator aIt;
167     std::list<ResultPtr>::const_iterator aResIt;
168     XGUI_Displayer* aDisplayer = workshop()->displayer();
169     AISObjectPtr aAIS;
170     for (aIt = aObjList.begin(); aIt != aObjList.end(); ++aIt) {
171       FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*aIt);
172       if (aFeature) {
173         aAIS = aDisplayer->getAISObject(aFeature);
174         if (aAIS.get())
175           workshop()->module()->customizePresentation(aFeature, aAIS);
176
177         std::list<ResultPtr> aResults = aFeature->results();
178         for (aResIt = aResults.begin(); aResIt != aResults.end(); ++aResIt) {
179           aAIS = aDisplayer->getAISObject(*aResIt);
180           if (aAIS.get())
181             workshop()->module()->customizePresentation(*aResIt, aAIS);
182         }
183       }
184     }
185   } else if (theMessage->eventID() == Events_Loop::eventByName(EVENT_EMPTY_AIS_PRESENTATION)) {
186     std::shared_ptr<ModelAPI_ObjectUpdatedMessage> aUpdMsg =
187         std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
188     onFeatureEmptyPresentationMsg(aUpdMsg);
189   } else if (theMessage->eventID() == Events_Loop::eventByName(EVENT_UPDATE_BY_WIDGET_SELECTION)) {
190     ModuleBase_ModelWidget* aWidget = workshop()->propertyPanel()->activeWidget();
191     if (aWidget) {
192       ModuleBase_WidgetSelector* aWidgetSelector =
193         dynamic_cast<ModuleBase_WidgetSelector*>(aWidget);
194       if (aWidgetSelector)
195         workshop()->selector()->setSelected(aWidgetSelector->getAttributeSelection());
196     }
197   }
198   else if (theMessage->eventID() == Events_Loop::eventByName("FinishOperation")/* ||
199           theMessage->eventID() == Events_Loop::eventByName("AbortOperation")*/)
200   {
201     XGUI_FacesPanel* aFacesPanel = workshop()->facesPanel();
202     if (aFacesPanel)
203       aFacesPanel->reset(false); // do not flush redisplay, it is flushed after event
204   }
205   //Update property panel on corresponding message. If there is no current operation (no
206   //property panel), or received message has different feature to the current - do nothing.
207   else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_UPDATED)) {
208     std::shared_ptr<ModelAPI_ObjectUpdatedMessage> anUpdateMsg =
209         std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
210     onFeatureUpdatedMsg(anUpdateMsg);
211   } else if (theMessage->eventID() == Events_LongOp::eventID()) {
212     if (Events_LongOp::isPerformed()) {
213       QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
214     } else {
215       QApplication::restoreOverrideCursor();
216     }
217   } else if (theMessage->eventID() == Events_Loop::eventByName(EVENT_UPDATE_VIEWER_BLOCKED)) {
218     // the viewer's update context will not happens until viewer updated is emitted
219       workshop()->displayer()->enableUpdateViewer(false);
220   } else if (theMessage->eventID() == Events_Loop::eventByName(EVENT_UPDATE_VIEWER_UNBLOCKED)) {
221     // the viewer's update context is unblocked, the viewer's update works
222     XGUI_Displayer* aDisplayer = workshop()->displayer();
223     aDisplayer->enableUpdateViewer(true);
224   }
225   else if ((theMessage->eventID() ==
226     Events_Loop::eventByName(EVENT_AUTOMATIC_RECOMPUTATION_ENABLE)) ||
227     (theMessage->eventID() ==
228       Events_Loop::eventByName(EVENT_AUTOMATIC_RECOMPUTATION_DISABLE))) {
229     myWorkshop->updateAutoComputeState();
230   }
231   else if (theMessage->eventID() == ModelAPI_ObjectRenamedMessage::eventId()) {
232     myWorkshop->updateGroupsText();
233     myWorkshop->displayer()->updateViewer();
234   } else {
235     //Show error dialog if error message received.
236     std::shared_ptr<Events_InfoMessage> anIngfoMsg =
237       std::dynamic_pointer_cast<Events_InfoMessage>(theMessage);
238     if (anIngfoMsg) {
239       emit errorOccurred(anIngfoMsg);
240     }
241     return;
242   }
243 #ifndef HAVE_SALOME
244     SessionPtr aMgr = ModelAPI_Session::get();
245     AppElements_MainWindow* aMainWindow = workshop()->mainWindow();
246     if (aMgr->isModified() != aMainWindow->isModifiedState())
247       aMainWindow->setModifiedState(aMgr->isModified());
248 #endif
249 }
250
251 //******************************************************
252 void XGUI_WorkshopListener::onFeatureUpdatedMsg(
253                                      const std::shared_ptr<ModelAPI_ObjectUpdatedMessage>& theMsg)
254 {
255 #ifdef DEBUG_FEATURE_UPDATED
256   std::set<ObjectPtr> anObjects = theMsg->objects();
257   std::set<ObjectPtr>::const_iterator aIt;
258   QStringList anInfo;
259   for (aIt = anObjects.begin(); aIt != anObjects.end(); ++aIt) {
260     anInfo.append(ModuleBase_Tools::objectInfo((*aIt)));
261   }
262   QString anInfoStr = anInfo.join(";\t");
263   qDebug(QString("onFeatureUpdatedMsg: %1, %2")
264     .arg(anObjects.size()).arg(anInfoStr).toStdString().c_str());
265 #endif
266   std::set<ObjectPtr> aFeatures = theMsg->objects();
267   XGUI_OperationMgr* anOperationMgr = workshop()->operationMgr();
268   if (anOperationMgr->hasOperation()) {
269     ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
270                                                       (anOperationMgr->currentOperation());
271     if (aFOperation) {
272       FeaturePtr aCurrentFeature = aFOperation->feature();
273       std::set<ObjectPtr>::const_iterator aIt;
274       for (aIt = aFeatures.begin(); aIt != aFeatures.end(); ++aIt) {
275         ObjectPtr aNewFeature = (*aIt);
276         if (aNewFeature == aCurrentFeature) {
277           workshop()->propertyPanel()->updateContentWidget(aCurrentFeature);
278           break;
279         }
280       }
281     }
282   }
283   //anOperationMgr->onValidateOperation();
284
285   //if (myObjectBrowser)
286   //  myObjectBrowser->processEvent(theMsg);
287 }
288
289 //******************************************************
290 void XGUI_WorkshopListener::
291   onFeatureRedisplayMsg(const std::shared_ptr<ModelAPI_ObjectUpdatedMessage>& theMsg)
292 {
293   std::set<ObjectPtr> anObjects = theMsg->objects();
294   std::set<ObjectPtr>::const_iterator aIt;
295
296 #ifdef DEBUG_FEATURE_REDISPLAY
297   QStringList anInfo;
298   for (aIt = anObjects.begin(); aIt != anObjects.end(); ++aIt) {
299     anInfo.append(ModuleBase_Tools::objectInfo((*aIt)));
300   }
301   QString anInfoStr = anInfo.join(";\t");
302   qDebug(QString("onFeatureRedisplayMsg: %1, %2")
303     .arg(anObjects.size()).arg(anInfoStr).toStdString().c_str());
304 #endif
305
306   XGUI_Workshop* aWorkshop = workshop();
307   XGUI_Displayer* aDisplayer = aWorkshop->displayer();
308   bool aRedisplayed = false;
309   //std::list<ObjectPtr> aHiddenObjects;
310   for (aIt = anObjects.begin(); aIt != anObjects.end(); ++aIt) {
311     ObjectPtr aObj = (*aIt);
312
313     // Hide the object if it is invalid or concealed one
314     bool aHide = !aObj->data() || !aObj->data()->isValid() ||
315       aObj->isDisabled() || (!aObj->isDisplayed());
316
317     if (!aHide) { // check that this is not hidden result
318       ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(aObj);
319       aHide = aRes && aRes->isConcealed();
320
321       // Hide the presentation with an empty shape. But isDisplayed state of the object should not
322       // be changed to the object becomes visible when the shape becomes not empty
323       if (!aHide && aRes.get())
324         aHide = !aRes->shape().get() || aRes->shape()->isNull();
325     }
326
327 #ifdef DEBUG_RESULT_COMPSOLID
328     ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(aObj);
329     if (aRes.get()) {
330       ResultCompSolidPtr aCompSolidRes = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(aRes);
331       if (aCompSolidRes.get()) {
332           qDebug(QString("COMPSOLID, numberOfSubs = %1")
333             .arg(aCompSolidRes->numberOfSubs()).toStdString().c_str());
334       }
335       if (ModelAPI_Tools::compSolidOwner(aRes))
336         qDebug("COMPSOLID sub-object");
337     }
338 #endif
339     #ifdef DEBUG_FEATURE_REDISPLAY
340       QString anObjInfo = ModuleBase_Tools::objectInfo((aObj));
341       FeaturePtr aFeature = ModelAPI_Feature::feature(aObj);
342       if (aFeature.get()) {
343         std::string aKind = aFeature->getKind();
344         if (aKind == DebugFeatureKind || DebugFeatureKind.empty()) {
345           qDebug(QString("visible=%1, hide=%2 : display= %2").arg(aDisplayer->isVisible(aObj))
346                                             .arg(aHide).arg(anObjInfo).toStdString().c_str());
347         }
348       }
349     #endif
350     if (aHide) {
351       //we should provide objects which are hidden in the viewer, e.g. sketch always should
352       // visualizes all sub-features, if some features are to be hidden, sould be proposed may
353       // be to removed #1223
354       // aHiddenObjects.push_back(aObj);
355       aRedisplayed = aDisplayer->erase(aObj, false) || aRedisplayed;
356       #ifdef DEBUG_FEATURE_REDISPLAY
357         // Redisplay the visible object or the object of the current operation
358         bool isVisibleObject = aDisplayer->isVisible(aObj);
359
360         QString anObjInfo = ModuleBase_Tools::objectInfo((aObj));
361       #endif
362     }
363     else {
364       // Redisplay the visible object or the object of the current operation
365       bool isVisibleObject = aDisplayer->isVisible(aObj);
366       #ifdef DEBUG_FEATURE_REDISPLAY
367         QString anObjInfo = ModuleBase_Tools::objectInfo((aObj));
368       #endif
369
370       if (isVisibleObject)  { // redisplay visible object
371         //displayObject(aObj);  // In order to update presentation
372         // in order to avoid the check whether the object can be redisplayed, the exact method
373         // of redisplay is called. This modification is made in order to have the line is updated
374         // by creation of a horizontal constraint on the line by preselection
375         if (ModelAPI_Tools::hasSubResults(std::dynamic_pointer_cast<ModelAPI_Result>(aObj))) {
376           aRedisplayed = aDisplayer->erase(aObj, false) || aRedisplayed;
377         }
378         else {
379           aRedisplayed = aDisplayer->redisplay(aObj, false) || aRedisplayed;
380           // Deactivate object of current operation from selection
381           aWorkshop->deactivateActiveObject(aObj, false);
382         }
383       } else { // display object if the current operation has it
384         if (displayObject(aObj)) {
385           aRedisplayed = true;
386           // Deactivate object of current operation from selection
387           aWorkshop->deactivateActiveObject(aObj, false);
388         }
389       }
390     }
391   }
392
393   // this processing should be moved in another place in order to do not cause problems in
394   // flush messages chain
395   //if (aHiddenObjects.size() > 0)
396   //  myWorkshop->module()->processHiddenObject(aHiddenObjects);
397
398   bool isCustomized = customizeFeature(anObjects, aRedisplayed);
399   if (aRedisplayed || isCustomized) {
400     Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_EMPTY_AIS_PRESENTATION));
401
402     // Do not update viewer here because it can be called in a loop
403     // In this case Update has to be called after redisplay event
404     //aDisplayer->updateViewer();
405   }
406 }
407
408 //******************************************************
409 void XGUI_WorkshopListener::
410   onFeatureCreatedMsg(const std::shared_ptr<ModelAPI_ObjectUpdatedMessage>& theMsg)
411 {
412   bool isLoadedScript = false;
413   SUIT_Session* aSession = SUIT_Session::session();
414   if (aSession)
415   {
416     QVariant aVar = aSession->activeApplication()->property("IsLoadedScript");
417     isLoadedScript = !aVar.isNull() && aVar.toBool();
418   }
419
420   std::set<ObjectPtr> anObjects = theMsg->objects();
421   std::set<ObjectPtr>::const_iterator aIt;
422 #ifdef DEBUG_FEATURE_CREATED
423   QStringList anInfo;
424   for (aIt = anObjects.begin(); aIt != anObjects.end(); ++aIt) {
425     anInfo.append(ModuleBase_Tools::objectInfo((*aIt)));
426   }
427   QString anInfoStr = anInfo.join(";\t");
428   qDebug(QString("onFeatureCreatedMsg: %1, %2")
429     .arg(anObjects.size()).arg(anInfoStr).toStdString().c_str());
430 #endif
431
432   //bool aHasPart = false;
433   bool aDisplayed = false;
434   for (aIt = anObjects.begin(); aIt != anObjects.end(); ++aIt) {
435     ObjectPtr anObject = *aIt;
436
437 #ifdef DEBUG_RESULT_COMPSOLID
438     ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
439     if (aRes.get()) {
440       ResultCompSolidPtr aCompSolidRes =
441         std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(aRes);
442       if (aCompSolidRes.get()) {
443         qDebug(QString("COMPSOLID, numberOfSubs = %1")
444           .arg(aCompSolidRes->numberOfSubs()).toStdString().c_str());
445       }
446       if (ModelAPI_Tools::compSolidOwner(aRes))
447         qDebug("COMPSOLID sub-object");
448     }
449 #endif
450
451     ResultBodyPtr aRes = std::dynamic_pointer_cast<ModelAPI_ResultBody>(anObject);
452     if (aRes.get() && aRes->numberOfSubs() > 0)
453       for (int anIndex = 0; anIndex < aRes->numberOfSubs(); ++anIndex)
454         setDisplayed(aRes->subResult(anIndex), isLoadedScript, aDisplayed);
455     else
456       setDisplayed(anObject, isLoadedScript, aDisplayed);
457   }
458
459   MAYBE_UNUSED bool isCustomized = customizeFeature(anObjects, aDisplayed);
460
461   //if (myObjectBrowser)
462   //  myObjectBrowser->processEvent(theMsg);
463   if (aDisplayed) {
464     Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_EMPTY_AIS_PRESENTATION));
465     workshop()->displayer()->updateViewer();
466   }
467   //if (aHasPart) { // TODO: Avoid activate last part on loading of document
468   //  activateLastPart();
469   //}
470 }
471
472 //******************************************************
473 void XGUI_WorkshopListener::onFeatureEmptyPresentationMsg(
474                                       const std::shared_ptr<ModelAPI_ObjectUpdatedMessage>& theMsg)
475 {
476   std::set<ObjectPtr> anObjects = theMsg->objects();
477   std::set<ObjectPtr>::const_iterator aIt;
478 #ifdef DEBUG_FEATURE_CREATED
479   QStringList anInfo;
480   for (aIt = anObjects.begin(); aIt != anObjects.end(); ++aIt) {
481     anInfo.append(ModuleBase_Tools::objectInfo((*aIt)));
482   }
483   QString anInfoStr = anInfo.join(";\t");
484   qDebug(QString("onFeatureEmptyPresentationMsg: %1, %2")
485     .arg(anObjects.size()).arg(anInfoStr).toStdString().c_str());
486 #endif
487
488   XGUI_Workshop* aWorkshop = workshop();
489   XGUI_Displayer* aDisplayer = aWorkshop->displayer();
490
491   bool aRedisplayed = false;
492   for (aIt = anObjects.begin(); aIt != anObjects.end(); ++aIt) {
493     ObjectPtr anObject = *aIt;
494     aRedisplayed = aDisplayer->erase(anObject, false) || aRedisplayed;
495   }
496
497   if (aRedisplayed)
498     aDisplayer->updateViewer();
499 }
500
501 bool XGUI_WorkshopListener::event(QEvent * theEvent)
502 {
503   PostponeMessageQtEvent* aPostponedEv = dynamic_cast<PostponeMessageQtEvent*>(theEvent);
504   if (aPostponedEv) {
505     std::shared_ptr<Events_Message> aEventPtr = aPostponedEv->postponedMessage();
506     processEvent(aEventPtr);
507     return true;
508   }
509   return false;
510 }
511
512 //**************************************************************
513 bool XGUI_WorkshopListener::displayObject(ObjectPtr theObj)
514 {
515 #ifdef DEBUG_RESULT_COMPSOLID
516   ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(theObj);
517   if (aRes.get() && (ModelAPI_Tools::hasSubResults(aRes) || ModelAPI_Tools::compSolidOwner(aRes))) {
518     ResultCompSolidPtr aCompSolidRes = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(aRes);
519     if (aCompSolidRes.get()) {
520       qDebug("COMPSOLID: displayObject");
521     }
522   }
523 #endif
524
525   bool aDisplayed = false;
526   XGUI_Workshop* aWorkshop = workshop();
527   // do not display the object if it has sub objects. They should be displayed separately.
528   if (!aWorkshop->module()->canDisplayObject(theObj) ||
529       ModelAPI_Tools::hasSubResults(std::dynamic_pointer_cast<ModelAPI_Result>(theObj)))
530     return aDisplayed;
531
532   XGUI_Displayer* aDisplayer = aWorkshop->displayer();
533   return aDisplayer->display(theObj, false);
534 }
535
536 bool XGUI_WorkshopListener::customizeFeature(const std::set<ObjectPtr>& theObjects,
537                                                    bool theForceRedisplay)
538 {
539   XGUI_OperationMgr* anOperationMgr = workshop()->operationMgr();
540   FeaturePtr aCurrentFeature;
541   if (anOperationMgr->hasOperation()) {
542     ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
543                                                       (anOperationMgr->currentOperation());
544     if (aFOperation) {
545       aCurrentFeature = aFOperation->feature();
546     }
547   }
548
549   bool aCustomized = false;
550   if (aCurrentFeature.get()) {
551     // the customize presentation should be redisplayed if force redislayed is true or
552     // if a list of message objects contains the operation feature for case when
553     // the feature is hidden, but arguments of the feature are modified
554     // e.g. extrusion is hidden(h=0) but sketch is chosen
555     if (theForceRedisplay || theObjects.find(aCurrentFeature) != theObjects.end()) {
556       aCustomized = myWorkshop->module()->customizeFeature(aCurrentFeature,
557                                  ModuleBase_IModule::CustomizeArguments, false) || aCustomized;
558       aCustomized = myWorkshop->module()->customizeFeature(aCurrentFeature,
559                                    ModuleBase_IModule::CustomizeResults, false) || aCustomized;
560       aCustomized = myWorkshop->module()->customizeFeature(aCurrentFeature,
561                         ModuleBase_IModule::CustomizeHighlightedObjects, false) || aCustomized;
562     }
563   }
564   return aCustomized;
565 }
566
567 XGUI_Workshop* XGUI_WorkshopListener::workshop() const
568 {
569   return myWorkshop;
570 }
571
572
573 void XGUI_WorkshopListener::setDisplayed(
574   ObjectPtr theObject, const bool theIsLoadedScript, bool& theDisplayed)
575 {
576   if (theIsLoadedScript) {
577     theObject->setDisplayed(theDisplayed);
578     return;
579   }
580   // the validity of the data should be checked here in order to avoid display of the objects,
581   // which were created, then deleted, but flush for the creation event happens after that
582   // we should not display disabled objects
583   bool aHide = !theObject->data()->isValid() ||
584                 theObject->isDisabled() ||
585                !theObject->isDisplayed();
586   if (!aHide) { // check that this is not hidden result
587     ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
588     aHide = aRes && aRes->isConcealed();
589     // Hide the presentation with an empty shape. But isDisplayed state of the object should not
590     // be changed to the object becomes visible when the shape becomes not empty
591     if (!aHide && aRes.get())
592       aHide = !aRes->shape().get() || aRes->shape()->isNull();
593   }
594   if (!aHide) {
595     // setDisplayed has to be called in order to synchronize internal state of the object
596     // with list of displayed objects
597     if (myWorkshop->module()->canDisplayObject(theObject)) {
598       theObject->setDisplayed(true);
599       theDisplayed = displayObject(theObject);
600     }
601     else
602       theObject->setDisplayed(false);
603   }
604 }