1 // Copyright (C) 2014-2017 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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include "XGUI_WorkshopListener.h"
24 #include <AppElements_MainWindow.h>
27 #include <Config_FeatureMessage.h>
28 #include <Config_PointerMessage.h>
29 #include <Config_Keywords.h>
31 #include <Events_InfoMessage.h>
32 #include <Events_Loop.h>
33 #include <Events_LongOp.h>
35 #include <ModelAPI_Object.h>
36 #include <ModelAPI_Events.h>
37 #include <ModelAPI_Session.h>
38 #include <ModelAPI_Result.h>
39 #include <ModelAPI_Feature.h>
40 #include <ModelAPI_Data.h>
41 #include <ModelAPI_Tools.h>
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>
53 #include "XGUI_ActionsMgr.h"
54 #include "XGUI_Displayer.h"
55 #include "XGUI_ErrorMgr.h"
56 #include "XGUI_FacesPanel.h"
57 #include "XGUI_OperationMgr.h"
58 #include "XGUI_ModuleConnector.h"
59 #include "XGUI_PropertyPanel.h"
61 #include "XGUI_QtEvents.h"
62 #include "XGUI_SalomeConnector.h"
63 #include "XGUI_SelectionMgr.h"
64 #include "XGUI_Workshop.h"
67 #include <QApplication>
68 #include <QMainWindow>
76 //#define DEBUG_FEATURE_CREATED
77 //#define DEBUG_FEATURE_REDISPLAY
78 //#define DEBUG_FEATURE_UPDATED
79 //#define DEBUG_RESULT_COMPSOLID
81 #ifdef DEBUG_FEATURE_REDISPLAY
82 const std::string DebugFeatureKind = "";//"Extrusion";
85 XGUI_WorkshopListener::XGUI_WorkshopListener(XGUI_Workshop* theWorkshop)
86 : myWorkshop(theWorkshop),
89 XGUI_OperationMgr* anOperationMgr = workshop()->operationMgr();
92 //******************************************************
93 XGUI_WorkshopListener::~XGUI_WorkshopListener(void)
97 //******************************************************
98 void XGUI_WorkshopListener::initializeEventListening()
100 //Initialize event listening
101 Events_Loop* aLoop = Events_Loop::loop();
102 aLoop->registerListener(this, Events_InfoMessage::errorID()); //!< Listening application errors.
103 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
104 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_CREATED));
105 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
106 aLoop->registerListener(this, Events_LongOp::eventID());
107 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_PLUGIN_LOADED));
109 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_UPDATE_VIEWER_BLOCKED));
110 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_UPDATE_VIEWER_UNBLOCKED));
111 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_EMPTY_AIS_PRESENTATION));
112 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_UPDATE_BY_WIDGET_SELECTION));
114 aLoop->registerListener(this, Events_Loop::eventByName("FinishOperation"));
115 aLoop->registerListener(this, Events_Loop::eventByName("AbortOperation"));
116 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_AUTOMATIC_RECOMPUTATION_ENABLE));
117 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_AUTOMATIC_RECOMPUTATION_DISABLE));
120 //******************************************************
121 void XGUI_WorkshopListener::processEvent(const std::shared_ptr<Events_Message>& theMessage)
123 if (QApplication::instance() &&
124 QApplication::instance()->thread() != QThread::currentThread()) {
126 std::cout << "XGUI_Workshop::processEvent: " << "Working in another thread." << std::endl;
128 SessionPtr aMgr = ModelAPI_Session::get();
129 PostponeMessageQtEvent* aPostponeEvent = new PostponeMessageQtEvent(theMessage);
130 QApplication::postEvent(this, aPostponeEvent);
134 // Process creation of Part
135 if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_CREATED)) {
136 std::shared_ptr<ModelAPI_ObjectUpdatedMessage> aUpdMsg =
137 std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
138 onFeatureCreatedMsg(aUpdMsg);
140 XGUI_SalomeConnector* aSalomeConnector = workshop()->salomeConnector();
141 if (aSalomeConnector)
142 aSalomeConnector->createPreferences();
143 myUpdatePrefs = false;
146 else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_PLUGIN_LOADED)) {
147 myUpdatePrefs = true;
150 else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY)) {
151 std::shared_ptr<ModelAPI_ObjectUpdatedMessage> aUpdMsg =
152 std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
153 onFeatureRedisplayMsg(aUpdMsg);
154 } else if (theMessage->eventID() == Events_Loop::eventByName(EVENT_EMPTY_AIS_PRESENTATION)) {
155 std::shared_ptr<ModelAPI_ObjectUpdatedMessage> aUpdMsg =
156 std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
157 onFeatureEmptyPresentationMsg(aUpdMsg);
158 } else if (theMessage->eventID() == Events_Loop::eventByName(EVENT_UPDATE_BY_WIDGET_SELECTION)) {
159 ModuleBase_ModelWidget* aWidget = workshop()->propertyPanel()->activeWidget();
161 ModuleBase_WidgetSelector* aWidgetSelector =
162 dynamic_cast<ModuleBase_WidgetSelector*>(aWidget);
164 workshop()->selector()->setSelected(aWidgetSelector->getAttributeSelection());
166 } else if (theMessage->eventID() == Events_Loop::eventByName("FinishOperation")/* ||
167 theMessage->eventID() == Events_Loop::eventByName("AbortOperation")*/)
168 workshop()->facesPanel()->reset(false); // do not flush redisplay, it is flushed after event
170 //Update property panel on corresponding message. If there is no current operation (no
171 //property panel), or received message has different feature to the current - do nothing.
172 else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_UPDATED)) {
173 std::shared_ptr<ModelAPI_ObjectUpdatedMessage> anUpdateMsg =
174 std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
175 onFeatureUpdatedMsg(anUpdateMsg);
176 } else if (theMessage->eventID() == Events_LongOp::eventID()) {
177 if (Events_LongOp::isPerformed()) {
178 QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
180 QApplication::restoreOverrideCursor();
182 } else if (theMessage->eventID() == Events_Loop::eventByName(EVENT_UPDATE_VIEWER_BLOCKED)) {
183 // the viewer's update context will not happens until viewer updated is emitted
184 workshop()->displayer()->enableUpdateViewer(false);
185 } else if (theMessage->eventID() == Events_Loop::eventByName(EVENT_UPDATE_VIEWER_UNBLOCKED)) {
186 // the viewer's update context is unblocked, the viewer's update works
187 XGUI_Displayer* aDisplayer = workshop()->displayer();
188 aDisplayer->enableUpdateViewer(true);
189 } else if ((theMessage->eventID() ==
190 Events_Loop::eventByName(EVENT_AUTOMATIC_RECOMPUTATION_ENABLE)) ||
191 (theMessage->eventID() ==
192 Events_Loop::eventByName(EVENT_AUTOMATIC_RECOMPUTATION_DISABLE))) {
193 myWorkshop->updateAutoComputeState();
195 //Show error dialog if error message received.
196 std::shared_ptr<Events_InfoMessage> anIngfoMsg =
197 std::dynamic_pointer_cast<Events_InfoMessage>(theMessage);
199 emit errorOccurred(anIngfoMsg);
204 SessionPtr aMgr = ModelAPI_Session::get();
205 AppElements_MainWindow* aMainWindow = workshop()->mainWindow();
206 if (aMgr->isModified() != aMainWindow->isModifiedState())
207 aMainWindow->setModifiedState(aMgr->isModified());
211 //******************************************************
212 void XGUI_WorkshopListener::onFeatureUpdatedMsg(
213 const std::shared_ptr<ModelAPI_ObjectUpdatedMessage>& theMsg)
215 #ifdef DEBUG_FEATURE_UPDATED
216 std::set<ObjectPtr> anObjects = theMsg->objects();
217 std::set<ObjectPtr>::const_iterator aIt;
219 for (aIt = anObjects.begin(); aIt != anObjects.end(); ++aIt) {
220 anInfo.append(ModuleBase_Tools::objectInfo((*aIt)));
222 QString anInfoStr = anInfo.join(";\t");
223 qDebug(QString("onFeatureUpdatedMsg: %1, %2")
224 .arg(anObjects.size()).arg(anInfoStr).toStdString().c_str());
226 std::set<ObjectPtr> aFeatures = theMsg->objects();
227 XGUI_OperationMgr* anOperationMgr = workshop()->operationMgr();
228 if (anOperationMgr->hasOperation()) {
229 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
230 (anOperationMgr->currentOperation());
232 FeaturePtr aCurrentFeature = aFOperation->feature();
233 std::set<ObjectPtr>::const_iterator aIt;
234 for (aIt = aFeatures.begin(); aIt != aFeatures.end(); ++aIt) {
235 ObjectPtr aNewFeature = (*aIt);
236 if (aNewFeature == aCurrentFeature) {
237 workshop()->propertyPanel()->updateContentWidget(aCurrentFeature);
243 //anOperationMgr->onValidateOperation();
245 //if (myObjectBrowser)
246 // myObjectBrowser->processEvent(theMsg);
249 //******************************************************
250 void XGUI_WorkshopListener::
251 onFeatureRedisplayMsg(const std::shared_ptr<ModelAPI_ObjectUpdatedMessage>& theMsg)
253 std::set<ObjectPtr> anObjects = theMsg->objects();
254 std::set<ObjectPtr>::const_iterator aIt;
256 #ifdef DEBUG_FEATURE_REDISPLAY
258 for (aIt = anObjects.begin(); aIt != anObjects.end(); ++aIt) {
259 anInfo.append(ModuleBase_Tools::objectInfo((*aIt)));
261 QString anInfoStr = anInfo.join(";\t");
262 qDebug(QString("onFeatureRedisplayMsg: %1, %2")
263 .arg(anObjects.size()).arg(anInfoStr).toStdString().c_str());
266 XGUI_Workshop* aWorkshop = workshop();
267 XGUI_Displayer* aDisplayer = aWorkshop->displayer();
268 bool aRedisplayed = false;
269 //std::list<ObjectPtr> aHiddenObjects;
270 for (aIt = anObjects.begin(); aIt != anObjects.end(); ++aIt) {
271 ObjectPtr aObj = (*aIt);
273 // Hide the object if it is invalid or concealed one
274 bool aHide = !aObj->data() || !aObj->data()->isValid() ||
275 aObj->isDisabled() || (!aObj->isDisplayed());
276 if (!aHide) { // check that this is not hidden result
277 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(aObj);
278 aHide = aRes && aRes->isConcealed();
280 // Hide the presentation with an empty shape. But isDisplayed state of the object should not
281 // be changed to the object becomes visible when the shape becomes not empty
282 if (!aHide && aRes.get())
283 aHide = !aRes->shape().get() || aRes->shape()->isNull();
286 #ifdef DEBUG_RESULT_COMPSOLID
287 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(aObj);
289 ResultCompSolidPtr aCompSolidRes = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(aRes);
290 if (aCompSolidRes.get()) {
291 qDebug(QString("COMPSOLID, numberOfSubs = %1")
292 .arg(aCompSolidRes->numberOfSubs()).toStdString().c_str());
294 if (ModelAPI_Tools::compSolidOwner(aRes))
295 qDebug("COMPSOLID sub-object");
298 #ifdef DEBUG_FEATURE_REDISPLAY
299 QString anObjInfo = ModuleBase_Tools::objectInfo((aObj));
300 FeaturePtr aFeature = ModelAPI_Feature::feature(aObj);
301 if (aFeature.get()) {
302 std::string aKind = aFeature->getKind();
303 if (aKind == DebugFeatureKind || DebugFeatureKind.empty()) {
304 qDebug(QString("visible=%1, hide=%2 : display= %2").arg(aDisplayer->isVisible(aObj))
305 .arg(aHide).arg(anObjInfo).toStdString().c_str());
310 //we should provide objects which are hidden in the viewer, e.g. sketch always should
311 // visualizes all sub-features, if some features are to be hidden, sould be proposed may
312 // be to removed #1223
313 // aHiddenObjects.push_back(aObj);
314 aRedisplayed = aDisplayer->erase(aObj, false) || aRedisplayed;
315 #ifdef DEBUG_FEATURE_REDISPLAY
316 // Redisplay the visible object or the object of the current operation
317 bool isVisibleObject = aDisplayer->isVisible(aObj);
319 QString anObjInfo = ModuleBase_Tools::objectInfo((aObj));
323 // Redisplay the visible object or the object of the current operation
324 bool isVisibleObject = aDisplayer->isVisible(aObj);
325 #ifdef DEBUG_FEATURE_REDISPLAY
326 QString anObjInfo = ModuleBase_Tools::objectInfo((aObj));
329 if (isVisibleObject) { // redisplay visible object
330 //displayObject(aObj); // In order to update presentation
331 // in order to avoid the check whether the object can be redisplayed, the exact method
332 // of redisplay is called. This modification is made in order to have the line is updated
333 // by creation of a horizontal constraint on the line by preselection
334 if (ModelAPI_Tools::hasSubResults(std::dynamic_pointer_cast<ModelAPI_Result>(aObj))) {
335 aRedisplayed = aDisplayer->erase(aObj, false) || aRedisplayed;
338 aRedisplayed = aDisplayer->redisplay(aObj, false) || aRedisplayed;
339 // Deactivate object of current operation from selection
340 aWorkshop->deactivateActiveObject(aObj, false);
342 } else { // display object if the current operation has it
343 if (displayObject(aObj)) {
345 // Deactivate object of current operation from selection
346 aWorkshop->deactivateActiveObject(aObj, false);
351 // this processing should be moved in another place in order to do not cause problems in
352 // flush messages chain
353 //if (aHiddenObjects.size() > 0)
354 // myWorkshop->module()->processHiddenObject(aHiddenObjects);
356 bool isCustomized = customizeCurrentObject(anObjects, aRedisplayed);
357 if (aRedisplayed || isCustomized) {
358 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_EMPTY_AIS_PRESENTATION));
360 aDisplayer->updateViewer();
364 //******************************************************
365 void XGUI_WorkshopListener::
366 onFeatureCreatedMsg(const std::shared_ptr<ModelAPI_ObjectUpdatedMessage>& theMsg)
368 std::set<ObjectPtr> anObjects = theMsg->objects();
369 std::set<ObjectPtr>::const_iterator aIt;
370 #ifdef DEBUG_FEATURE_CREATED
372 for (aIt = anObjects.begin(); aIt != anObjects.end(); ++aIt) {
373 anInfo.append(ModuleBase_Tools::objectInfo((*aIt)));
375 QString anInfoStr = anInfo.join(";\t");
376 qDebug(QString("onFeatureCreatedMsg: %1, %2")
377 .arg(anObjects.size()).arg(anInfoStr).toStdString().c_str());
380 //bool aHasPart = false;
381 bool aDisplayed = false;
382 for (aIt = anObjects.begin(); aIt != anObjects.end(); ++aIt) {
383 ObjectPtr anObject = *aIt;
385 #ifdef DEBUG_RESULT_COMPSOLID
386 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
388 ResultCompSolidPtr aCompSolidRes = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(aRes);
389 if (aCompSolidRes.get()) {
390 qDebug(QString("COMPSOLID, numberOfSubs = %1")
391 .arg(aCompSolidRes->numberOfSubs()).toStdString().c_str());
393 if (ModelAPI_Tools::compSolidOwner(aRes))
394 qDebug("COMPSOLID sub-object");
397 // the validity of the data should be checked here in order to avoid display of the objects,
398 // which were created, then deleted, but flush for the creation event happens after that
399 // we should not display disabled objects
400 bool aHide = !anObject->data()->isValid() ||
401 anObject->isDisabled() ||
402 !anObject->isDisplayed();
403 if (!aHide) { // check that this is not hidden result
404 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
405 bool isConcealed = aRes && aRes->isConcealed();
406 aHide = aRes && aRes->isConcealed();
407 // Hide the presentation with an empty shape. But isDisplayed state of the object should not
408 // be changed to the object becomes visible when the shape becomes not empty
409 if (!aHide && aRes.get())
410 aHide = !aRes->shape().get() || aRes->shape()->isNull();
413 // setDisplayed has to be called in order to synchronize internal state of the object
414 // with list of displayed objects
415 if (myWorkshop->module()->canDisplayObject(anObject)) {
416 anObject->setDisplayed(true);
417 aDisplayed = displayObject(anObject);
419 anObject->setDisplayed(false);
423 bool isCustomized = customizeCurrentObject(anObjects, aDisplayed);
425 //if (myObjectBrowser)
426 // myObjectBrowser->processEvent(theMsg);
428 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_EMPTY_AIS_PRESENTATION));
429 workshop()->displayer()->updateViewer();
431 //if (aHasPart) { // TODO: Avoid activate last part on loading of document
432 // activateLastPart();
436 //******************************************************
437 void XGUI_WorkshopListener::onFeatureEmptyPresentationMsg(
438 const std::shared_ptr<ModelAPI_ObjectUpdatedMessage>& theMsg)
440 std::set<ObjectPtr> anObjects = theMsg->objects();
441 std::set<ObjectPtr>::const_iterator aIt;
442 #ifdef DEBUG_FEATURE_CREATED
444 for (aIt = anObjects.begin(); aIt != anObjects.end(); ++aIt) {
445 anInfo.append(ModuleBase_Tools::objectInfo((*aIt)));
447 QString anInfoStr = anInfo.join(";\t");
448 qDebug(QString("onFeatureEmptyPresentationMsg: %1, %2")
449 .arg(anObjects.size()).arg(anInfoStr).toStdString().c_str());
452 XGUI_Workshop* aWorkshop = workshop();
453 XGUI_Displayer* aDisplayer = aWorkshop->displayer();
455 bool aRedisplayed = false;
456 for (aIt = anObjects.begin(); aIt != anObjects.end(); ++aIt) {
457 ObjectPtr anObject = *aIt;
458 aRedisplayed = aDisplayer->erase(anObject, false) || aRedisplayed;
462 aDisplayer->updateViewer();
465 bool XGUI_WorkshopListener::event(QEvent * theEvent)
467 PostponeMessageQtEvent* aPostponedEv = dynamic_cast<PostponeMessageQtEvent*>(theEvent);
469 std::shared_ptr<Events_Message> aEventPtr = aPostponedEv->postponedMessage();
470 processEvent(aEventPtr);
476 //**************************************************************
477 bool XGUI_WorkshopListener::displayObject(ObjectPtr theObj)
479 #ifdef DEBUG_RESULT_COMPSOLID
480 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(theObj);
481 if (aRes.get() && (ModelAPI_Tools::hasSubResults(aRes) || ModelAPI_Tools::compSolidOwner(aRes))) {
482 ResultCompSolidPtr aCompSolidRes = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(aRes);
483 if (aCompSolidRes.get()) {
484 qDebug("COMPSOLID: displayObject");
489 bool aDisplayed = false;
490 XGUI_Workshop* aWorkshop = workshop();
491 // do not display the object if it has sub objects. They should be displayed separately.
492 if (!aWorkshop->module()->canDisplayObject(theObj) ||
493 ModelAPI_Tools::hasSubResults(std::dynamic_pointer_cast<ModelAPI_Result>(theObj)))
496 XGUI_Displayer* aDisplayer = aWorkshop->displayer();
497 int aNb = aDisplayer->objectsCount();
498 return aDisplayer->display(theObj, false);
501 bool XGUI_WorkshopListener::customizeCurrentObject(const std::set<ObjectPtr>& theObjects,
502 bool theForceRedisplay)
504 XGUI_OperationMgr* anOperationMgr = workshop()->operationMgr();
505 FeaturePtr aCurrentFeature;
506 if (anOperationMgr->hasOperation()) {
507 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
508 (anOperationMgr->currentOperation());
510 aCurrentFeature = aFOperation->feature();
514 bool aCustomized = false;
515 if (aCurrentFeature.get()) {
516 // the customize presentation should be redisplayed if force redislayed is true or
517 // if a list of message objects contains the operation feature for case when
518 // the feature is hidden, but arguments of the feature are modified
519 // e.g. extrusion is hidden(h=0) but sketch is chosen
520 if (theForceRedisplay || theObjects.find(aCurrentFeature) != theObjects.end()) {
521 aCustomized = myWorkshop->module()->customizeObject(aCurrentFeature,
522 ModuleBase_IModule::CustomizeArguments, false) || aCustomized;
523 aCustomized = myWorkshop->module()->customizeObject(aCurrentFeature,
524 ModuleBase_IModule::CustomizeResults, false) || aCustomized;
525 aCustomized = myWorkshop->module()->customizeObject(aCurrentFeature,
526 ModuleBase_IModule::CustomizeHighlightedObjects, false) || aCustomized;
532 XGUI_Workshop* XGUI_WorkshopListener::workshop() const