1 // Copyright (C) 2014-2019 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "XGUI_WorkshopListener.h"
23 #include <AppElements_MainWindow.h>
26 #include <Config_FeatureMessage.h>
27 #include <Config_PointerMessage.h>
28 #include <Config_Keywords.h>
30 #include <Events_InfoMessage.h>
31 #include <Events_Loop.h>
32 #include <Events_LongOp.h>
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>
42 #include <ModuleBase_Events.h>
43 #include <ModuleBase_IModule.h>
44 #include <ModuleBase_IViewer.h>
45 #include <ModuleBase_IWorkshop.h>
46 #include <ModuleBase_Operation.h>
47 #include <ModuleBase_OperationDescription.h>
48 #include <ModuleBase_OperationFeature.h>
49 #include <ModuleBase_Tools.h>
50 #include <ModuleBase_WidgetSelector.h>
52 #include "XGUI_ActionsMgr.h"
53 #include "XGUI_Displayer.h"
54 #include "XGUI_ErrorMgr.h"
55 #include "XGUI_FacesPanel.h"
56 #include "XGUI_OperationMgr.h"
57 #include "XGUI_ModuleConnector.h"
58 #include "XGUI_PropertyPanel.h"
60 #include "XGUI_QtEvents.h"
61 #include "XGUI_SalomeConnector.h"
62 #include "XGUI_SelectionMgr.h"
63 #include "XGUI_Workshop.h"
66 #include <QApplication>
67 #include <QMainWindow>
75 //#define DEBUG_FEATURE_CREATED
76 //#define DEBUG_FEATURE_REDISPLAY
77 //#define DEBUG_FEATURE_UPDATED
78 //#define DEBUG_RESULT_COMPSOLID
80 #ifdef DEBUG_FEATURE_REDISPLAY
81 const std::string DebugFeatureKind = "";//"Extrusion";
84 XGUI_WorkshopListener::XGUI_WorkshopListener(XGUI_Workshop* theWorkshop)
85 : myWorkshop(theWorkshop),
88 XGUI_OperationMgr* anOperationMgr = workshop()->operationMgr();
91 //******************************************************
92 XGUI_WorkshopListener::~XGUI_WorkshopListener(void)
96 //******************************************************
97 void XGUI_WorkshopListener::initializeEventListening()
99 //Initialize event listening
100 Events_Loop* aLoop = Events_Loop::loop();
101 aLoop->registerListener(this, Events_InfoMessage::errorID()); //!< Listening application errors.
102 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
103 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_CREATED));
104 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
105 aLoop->registerListener(this, Events_LongOp::eventID());
106 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_PLUGIN_LOADED));
108 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_UPDATE_VIEWER_BLOCKED));
109 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_UPDATE_VIEWER_UNBLOCKED));
110 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_EMPTY_AIS_PRESENTATION));
111 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_UPDATE_BY_WIDGET_SELECTION));
113 aLoop->registerListener(this, Events_Loop::eventByName("FinishOperation"));
114 aLoop->registerListener(this, Events_Loop::eventByName("AbortOperation"));
115 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_AUTOMATIC_RECOMPUTATION_ENABLE));
116 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_AUTOMATIC_RECOMPUTATION_DISABLE));
119 //******************************************************
120 void XGUI_WorkshopListener::processEvent(const std::shared_ptr<Events_Message>& theMessage)
122 if (QApplication::instance() &&
123 QApplication::instance()->thread() != QThread::currentThread()) {
125 std::cout << "XGUI_Workshop::processEvent: " << "Working in another thread." << std::endl;
127 SessionPtr aMgr = ModelAPI_Session::get();
128 PostponeMessageQtEvent* aPostponeEvent = new PostponeMessageQtEvent(theMessage);
129 QApplication::postEvent(this, aPostponeEvent);
133 // Process creation of Part
134 if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_CREATED)) {
135 std::shared_ptr<ModelAPI_ObjectUpdatedMessage> aUpdMsg =
136 std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
137 onFeatureCreatedMsg(aUpdMsg);
139 XGUI_SalomeConnector* aSalomeConnector = workshop()->salomeConnector();
140 if (aSalomeConnector)
141 aSalomeConnector->createPreferences();
142 myUpdatePrefs = false;
145 else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_PLUGIN_LOADED)) {
146 myUpdatePrefs = true;
149 else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY)) {
150 std::shared_ptr<ModelAPI_ObjectUpdatedMessage> aUpdMsg =
151 std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
152 onFeatureRedisplayMsg(aUpdMsg);
153 } else if (theMessage->eventID() == Events_Loop::eventByName(EVENT_EMPTY_AIS_PRESENTATION)) {
154 std::shared_ptr<ModelAPI_ObjectUpdatedMessage> aUpdMsg =
155 std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
156 onFeatureEmptyPresentationMsg(aUpdMsg);
157 } else if (theMessage->eventID() == Events_Loop::eventByName(EVENT_UPDATE_BY_WIDGET_SELECTION)) {
158 ModuleBase_ModelWidget* aWidget = workshop()->propertyPanel()->activeWidget();
160 ModuleBase_WidgetSelector* aWidgetSelector =
161 dynamic_cast<ModuleBase_WidgetSelector*>(aWidget);
163 workshop()->selector()->setSelected(aWidgetSelector->getAttributeSelection());
165 } else if (theMessage->eventID() == Events_Loop::eventByName("FinishOperation")/* ||
166 theMessage->eventID() == Events_Loop::eventByName("AbortOperation")*/)
167 workshop()->facesPanel()->reset(false); // do not flush redisplay, it is flushed after event
169 //Update property panel on corresponding message. If there is no current operation (no
170 //property panel), or received message has different feature to the current - do nothing.
171 else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_UPDATED)) {
172 std::shared_ptr<ModelAPI_ObjectUpdatedMessage> anUpdateMsg =
173 std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
174 onFeatureUpdatedMsg(anUpdateMsg);
175 } else if (theMessage->eventID() == Events_LongOp::eventID()) {
176 if (Events_LongOp::isPerformed()) {
177 QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
179 QApplication::restoreOverrideCursor();
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 } else if ((theMessage->eventID() ==
189 Events_Loop::eventByName(EVENT_AUTOMATIC_RECOMPUTATION_ENABLE)) ||
190 (theMessage->eventID() ==
191 Events_Loop::eventByName(EVENT_AUTOMATIC_RECOMPUTATION_DISABLE))) {
192 myWorkshop->updateAutoComputeState();
194 //Show error dialog if error message received.
195 std::shared_ptr<Events_InfoMessage> anIngfoMsg =
196 std::dynamic_pointer_cast<Events_InfoMessage>(theMessage);
198 emit errorOccurred(anIngfoMsg);
203 SessionPtr aMgr = ModelAPI_Session::get();
204 AppElements_MainWindow* aMainWindow = workshop()->mainWindow();
205 if (aMgr->isModified() != aMainWindow->isModifiedState())
206 aMainWindow->setModifiedState(aMgr->isModified());
210 //******************************************************
211 void XGUI_WorkshopListener::onFeatureUpdatedMsg(
212 const std::shared_ptr<ModelAPI_ObjectUpdatedMessage>& theMsg)
214 #ifdef DEBUG_FEATURE_UPDATED
215 std::set<ObjectPtr> anObjects = theMsg->objects();
216 std::set<ObjectPtr>::const_iterator aIt;
218 for (aIt = anObjects.begin(); aIt != anObjects.end(); ++aIt) {
219 anInfo.append(ModuleBase_Tools::objectInfo((*aIt)));
221 QString anInfoStr = anInfo.join(";\t");
222 qDebug(QString("onFeatureUpdatedMsg: %1, %2")
223 .arg(anObjects.size()).arg(anInfoStr).toStdString().c_str());
225 std::set<ObjectPtr> aFeatures = theMsg->objects();
226 XGUI_OperationMgr* anOperationMgr = workshop()->operationMgr();
227 if (anOperationMgr->hasOperation()) {
228 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
229 (anOperationMgr->currentOperation());
231 FeaturePtr aCurrentFeature = aFOperation->feature();
232 std::set<ObjectPtr>::const_iterator aIt;
233 for (aIt = aFeatures.begin(); aIt != aFeatures.end(); ++aIt) {
234 ObjectPtr aNewFeature = (*aIt);
235 if (aNewFeature == aCurrentFeature) {
236 workshop()->propertyPanel()->updateContentWidget(aCurrentFeature);
242 //anOperationMgr->onValidateOperation();
244 //if (myObjectBrowser)
245 // myObjectBrowser->processEvent(theMsg);
248 //******************************************************
249 void XGUI_WorkshopListener::
250 onFeatureRedisplayMsg(const std::shared_ptr<ModelAPI_ObjectUpdatedMessage>& theMsg)
252 std::set<ObjectPtr> anObjects = theMsg->objects();
253 std::set<ObjectPtr>::const_iterator aIt;
255 #ifdef DEBUG_FEATURE_REDISPLAY
257 for (aIt = anObjects.begin(); aIt != anObjects.end(); ++aIt) {
258 anInfo.append(ModuleBase_Tools::objectInfo((*aIt)));
260 QString anInfoStr = anInfo.join(";\t");
261 qDebug(QString("onFeatureRedisplayMsg: %1, %2")
262 .arg(anObjects.size()).arg(anInfoStr).toStdString().c_str());
265 XGUI_Workshop* aWorkshop = workshop();
266 XGUI_Displayer* aDisplayer = aWorkshop->displayer();
267 bool aRedisplayed = false;
268 //std::list<ObjectPtr> aHiddenObjects;
269 for (aIt = anObjects.begin(); aIt != anObjects.end(); ++aIt) {
270 ObjectPtr aObj = (*aIt);
272 // Hide the object if it is invalid or concealed one
273 bool aHide = !aObj->data() || !aObj->data()->isValid() ||
274 aObj->isDisabled() || (!aObj->isDisplayed());
275 if (!aHide) { // check that this is not hidden result
276 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(aObj);
277 aHide = aRes && aRes->isConcealed();
279 // Hide the presentation with an empty shape. But isDisplayed state of the object should not
280 // be changed to the object becomes visible when the shape becomes not empty
281 if (!aHide && aRes.get())
282 aHide = !aRes->shape().get() || aRes->shape()->isNull();
285 #ifdef DEBUG_RESULT_COMPSOLID
286 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(aObj);
288 ResultCompSolidPtr aCompSolidRes = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(aRes);
289 if (aCompSolidRes.get()) {
290 qDebug(QString("COMPSOLID, numberOfSubs = %1")
291 .arg(aCompSolidRes->numberOfSubs()).toStdString().c_str());
293 if (ModelAPI_Tools::compSolidOwner(aRes))
294 qDebug("COMPSOLID sub-object");
297 #ifdef DEBUG_FEATURE_REDISPLAY
298 QString anObjInfo = ModuleBase_Tools::objectInfo((aObj));
299 FeaturePtr aFeature = ModelAPI_Feature::feature(aObj);
300 if (aFeature.get()) {
301 std::string aKind = aFeature->getKind();
302 if (aKind == DebugFeatureKind || DebugFeatureKind.empty()) {
303 qDebug(QString("visible=%1, hide=%2 : display= %2").arg(aDisplayer->isVisible(aObj))
304 .arg(aHide).arg(anObjInfo).toStdString().c_str());
309 //we should provide objects which are hidden in the viewer, e.g. sketch always should
310 // visualizes all sub-features, if some features are to be hidden, sould be proposed may
311 // be to removed #1223
312 // aHiddenObjects.push_back(aObj);
313 aRedisplayed = aDisplayer->erase(aObj, false) || aRedisplayed;
314 #ifdef DEBUG_FEATURE_REDISPLAY
315 // Redisplay the visible object or the object of the current operation
316 bool isVisibleObject = aDisplayer->isVisible(aObj);
318 QString anObjInfo = ModuleBase_Tools::objectInfo((aObj));
322 // Redisplay the visible object or the object of the current operation
323 bool isVisibleObject = aDisplayer->isVisible(aObj);
324 #ifdef DEBUG_FEATURE_REDISPLAY
325 QString anObjInfo = ModuleBase_Tools::objectInfo((aObj));
328 if (isVisibleObject) { // redisplay visible object
329 //displayObject(aObj); // In order to update presentation
330 // in order to avoid the check whether the object can be redisplayed, the exact method
331 // of redisplay is called. This modification is made in order to have the line is updated
332 // by creation of a horizontal constraint on the line by preselection
333 if (ModelAPI_Tools::hasSubResults(std::dynamic_pointer_cast<ModelAPI_Result>(aObj))) {
334 aRedisplayed = aDisplayer->erase(aObj, false) || aRedisplayed;
337 aRedisplayed = aDisplayer->redisplay(aObj, false) || aRedisplayed;
338 // Deactivate object of current operation from selection
339 aWorkshop->deactivateActiveObject(aObj, false);
341 } else { // display object if the current operation has it
342 if (displayObject(aObj)) {
344 // Deactivate object of current operation from selection
345 aWorkshop->deactivateActiveObject(aObj, false);
350 // this processing should be moved in another place in order to do not cause problems in
351 // flush messages chain
352 //if (aHiddenObjects.size() > 0)
353 // myWorkshop->module()->processHiddenObject(aHiddenObjects);
355 bool isCustomized = customizeCurrentObject(anObjects, aRedisplayed);
356 if (aRedisplayed || isCustomized) {
357 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_EMPTY_AIS_PRESENTATION));
359 aDisplayer->updateViewer();
363 //******************************************************
364 void XGUI_WorkshopListener::
365 onFeatureCreatedMsg(const std::shared_ptr<ModelAPI_ObjectUpdatedMessage>& theMsg)
367 std::set<ObjectPtr> anObjects = theMsg->objects();
368 std::set<ObjectPtr>::const_iterator aIt;
369 #ifdef DEBUG_FEATURE_CREATED
371 for (aIt = anObjects.begin(); aIt != anObjects.end(); ++aIt) {
372 anInfo.append(ModuleBase_Tools::objectInfo((*aIt)));
374 QString anInfoStr = anInfo.join(";\t");
375 qDebug(QString("onFeatureCreatedMsg: %1, %2")
376 .arg(anObjects.size()).arg(anInfoStr).toStdString().c_str());
379 //bool aHasPart = false;
380 bool aDisplayed = false;
381 for (aIt = anObjects.begin(); aIt != anObjects.end(); ++aIt) {
382 ObjectPtr anObject = *aIt;
384 #ifdef DEBUG_RESULT_COMPSOLID
385 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
387 ResultCompSolidPtr aCompSolidRes = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(aRes);
388 if (aCompSolidRes.get()) {
389 qDebug(QString("COMPSOLID, numberOfSubs = %1")
390 .arg(aCompSolidRes->numberOfSubs()).toStdString().c_str());
392 if (ModelAPI_Tools::compSolidOwner(aRes))
393 qDebug("COMPSOLID sub-object");
396 // the validity of the data should be checked here in order to avoid display of the objects,
397 // which were created, then deleted, but flush for the creation event happens after that
398 // we should not display disabled objects
399 bool aHide = !anObject->data()->isValid() ||
400 anObject->isDisabled() ||
401 !anObject->isDisplayed();
402 if (!aHide) { // check that this is not hidden result
403 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
404 bool isConcealed = aRes && aRes->isConcealed();
405 aHide = aRes && aRes->isConcealed();
406 // Hide the presentation with an empty shape. But isDisplayed state of the object should not
407 // be changed to the object becomes visible when the shape becomes not empty
408 if (!aHide && aRes.get())
409 aHide = !aRes->shape().get() || aRes->shape()->isNull();
412 // setDisplayed has to be called in order to synchronize internal state of the object
413 // with list of displayed objects
414 if (myWorkshop->module()->canDisplayObject(anObject)) {
415 anObject->setDisplayed(true);
416 aDisplayed = displayObject(anObject);
418 anObject->setDisplayed(false);
422 bool isCustomized = customizeCurrentObject(anObjects, aDisplayed);
424 //if (myObjectBrowser)
425 // myObjectBrowser->processEvent(theMsg);
427 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_EMPTY_AIS_PRESENTATION));
428 workshop()->displayer()->updateViewer();
430 //if (aHasPart) { // TODO: Avoid activate last part on loading of document
431 // activateLastPart();
435 //******************************************************
436 void XGUI_WorkshopListener::onFeatureEmptyPresentationMsg(
437 const std::shared_ptr<ModelAPI_ObjectUpdatedMessage>& theMsg)
439 std::set<ObjectPtr> anObjects = theMsg->objects();
440 std::set<ObjectPtr>::const_iterator aIt;
441 #ifdef DEBUG_FEATURE_CREATED
443 for (aIt = anObjects.begin(); aIt != anObjects.end(); ++aIt) {
444 anInfo.append(ModuleBase_Tools::objectInfo((*aIt)));
446 QString anInfoStr = anInfo.join(";\t");
447 qDebug(QString("onFeatureEmptyPresentationMsg: %1, %2")
448 .arg(anObjects.size()).arg(anInfoStr).toStdString().c_str());
451 XGUI_Workshop* aWorkshop = workshop();
452 XGUI_Displayer* aDisplayer = aWorkshop->displayer();
454 bool aRedisplayed = false;
455 for (aIt = anObjects.begin(); aIt != anObjects.end(); ++aIt) {
456 ObjectPtr anObject = *aIt;
457 aRedisplayed = aDisplayer->erase(anObject, false) || aRedisplayed;
461 aDisplayer->updateViewer();
464 bool XGUI_WorkshopListener::event(QEvent * theEvent)
466 PostponeMessageQtEvent* aPostponedEv = dynamic_cast<PostponeMessageQtEvent*>(theEvent);
468 std::shared_ptr<Events_Message> aEventPtr = aPostponedEv->postponedMessage();
469 processEvent(aEventPtr);
475 //**************************************************************
476 bool XGUI_WorkshopListener::displayObject(ObjectPtr theObj)
478 #ifdef DEBUG_RESULT_COMPSOLID
479 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(theObj);
480 if (aRes.get() && (ModelAPI_Tools::hasSubResults(aRes) || ModelAPI_Tools::compSolidOwner(aRes))) {
481 ResultCompSolidPtr aCompSolidRes = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(aRes);
482 if (aCompSolidRes.get()) {
483 qDebug("COMPSOLID: displayObject");
488 bool aDisplayed = false;
489 XGUI_Workshop* aWorkshop = workshop();
490 // do not display the object if it has sub objects. They should be displayed separately.
491 if (!aWorkshop->module()->canDisplayObject(theObj) ||
492 ModelAPI_Tools::hasSubResults(std::dynamic_pointer_cast<ModelAPI_Result>(theObj)))
495 XGUI_Displayer* aDisplayer = aWorkshop->displayer();
496 int aNb = aDisplayer->objectsCount();
497 return aDisplayer->display(theObj, false);
500 bool XGUI_WorkshopListener::customizeCurrentObject(const std::set<ObjectPtr>& theObjects,
501 bool theForceRedisplay)
503 XGUI_OperationMgr* anOperationMgr = workshop()->operationMgr();
504 FeaturePtr aCurrentFeature;
505 if (anOperationMgr->hasOperation()) {
506 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
507 (anOperationMgr->currentOperation());
509 aCurrentFeature = aFOperation->feature();
513 bool aCustomized = false;
514 if (aCurrentFeature.get()) {
515 // the customize presentation should be redisplayed if force redislayed is true or
516 // if a list of message objects contains the operation feature for case when
517 // the feature is hidden, but arguments of the feature are modified
518 // e.g. extrusion is hidden(h=0) but sketch is chosen
519 if (theForceRedisplay || theObjects.find(aCurrentFeature) != theObjects.end()) {
520 aCustomized = myWorkshop->module()->customizeObject(aCurrentFeature,
521 ModuleBase_IModule::CustomizeArguments, false) || aCustomized;
522 aCustomized = myWorkshop->module()->customizeObject(aCurrentFeature,
523 ModuleBase_IModule::CustomizeResults, false) || aCustomized;
524 aCustomized = myWorkshop->module()->customizeObject(aCurrentFeature,
525 ModuleBase_IModule::CustomizeHighlightedObjects, false) || aCustomized;
531 XGUI_Workshop* XGUI_WorkshopListener::workshop() const