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(ModuleBase_IWorkshop* 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"));
118 //******************************************************
119 void XGUI_WorkshopListener::processEvent(const std::shared_ptr<Events_Message>& theMessage)
121 if (QApplication::instance() &&
122 QApplication::instance()->thread() != QThread::currentThread()) {
124 std::cout << "XGUI_Workshop::processEvent: " << "Working in another thread." << std::endl;
126 SessionPtr aMgr = ModelAPI_Session::get();
127 PostponeMessageQtEvent* aPostponeEvent = new PostponeMessageQtEvent(theMessage);
128 QApplication::postEvent(this, aPostponeEvent);
132 // Process creation of Part
133 if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_CREATED)) {
134 std::shared_ptr<ModelAPI_ObjectUpdatedMessage> aUpdMsg =
135 std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
136 onFeatureCreatedMsg(aUpdMsg);
138 XGUI_SalomeConnector* aSalomeConnector = workshop()->salomeConnector();
139 if (aSalomeConnector)
140 aSalomeConnector->createPreferences();
141 myUpdatePrefs = false;
144 else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_PLUGIN_LOADED)) {
145 myUpdatePrefs = true;
148 else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY)) {
149 std::shared_ptr<ModelAPI_ObjectUpdatedMessage> aUpdMsg =
150 std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
151 onFeatureRedisplayMsg(aUpdMsg);
152 } else if (theMessage->eventID() == Events_Loop::eventByName(EVENT_EMPTY_AIS_PRESENTATION)) {
153 std::shared_ptr<ModelAPI_ObjectUpdatedMessage> aUpdMsg =
154 std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
155 onFeatureEmptyPresentationMsg(aUpdMsg);
156 } else if (theMessage->eventID() == Events_Loop::eventByName(EVENT_UPDATE_BY_WIDGET_SELECTION)) {
157 ModuleBase_ModelWidget* aWidget = workshop()->propertyPanel()->activeWidget();
159 ModuleBase_WidgetSelector* aWidgetSelector =
160 dynamic_cast<ModuleBase_WidgetSelector*>(aWidget);
162 workshop()->selector()->setSelected(aWidgetSelector->getAttributeSelection());
164 } else if (theMessage->eventID() == Events_Loop::eventByName("FinishOperation")/* ||
165 theMessage->eventID() == Events_Loop::eventByName("AbortOperation")*/)
166 workshop()->facesPanel()->reset(false); // do not flush redisplay, it is flushed after event
168 //Update property panel on corresponding message. If there is no current operation (no
169 //property panel), or received message has different feature to the current - do nothing.
170 else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_UPDATED)) {
171 std::shared_ptr<ModelAPI_ObjectUpdatedMessage> anUpdateMsg =
172 std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
173 onFeatureUpdatedMsg(anUpdateMsg);
174 } else if (theMessage->eventID() == Events_LongOp::eventID()) {
175 if (Events_LongOp::isPerformed()) {
176 QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
178 QApplication::restoreOverrideCursor();
180 } else if (theMessage->eventID() == Events_Loop::eventByName(EVENT_UPDATE_VIEWER_BLOCKED)) {
181 // the viewer's update context will not happens until viewer updated is emitted
182 workshop()->displayer()->enableUpdateViewer(false);
183 } else if (theMessage->eventID() == Events_Loop::eventByName(EVENT_UPDATE_VIEWER_UNBLOCKED)) {
184 // the viewer's update context is unblocked, the viewer's update works
185 XGUI_Displayer* aDisplayer = workshop()->displayer();
186 aDisplayer->enableUpdateViewer(true);
188 //Show error dialog if error message received.
189 std::shared_ptr<Events_InfoMessage> anIngfoMsg =
190 std::dynamic_pointer_cast<Events_InfoMessage>(theMessage);
192 emit errorOccurred(anIngfoMsg);
197 SessionPtr aMgr = ModelAPI_Session::get();
198 AppElements_MainWindow* aMainWindow = workshop()->mainWindow();
199 if (aMgr->isModified() != aMainWindow->isModifiedState())
200 aMainWindow->setModifiedState(aMgr->isModified());
204 //******************************************************
205 void XGUI_WorkshopListener::onFeatureUpdatedMsg(
206 const std::shared_ptr<ModelAPI_ObjectUpdatedMessage>& theMsg)
208 #ifdef DEBUG_FEATURE_UPDATED
209 std::set<ObjectPtr> anObjects = theMsg->objects();
210 std::set<ObjectPtr>::const_iterator aIt;
212 for (aIt = anObjects.begin(); aIt != anObjects.end(); ++aIt) {
213 anInfo.append(ModuleBase_Tools::objectInfo((*aIt)));
215 QString anInfoStr = anInfo.join(";\t");
216 qDebug(QString("onFeatureUpdatedMsg: %1, %2")
217 .arg(anObjects.size()).arg(anInfoStr).toStdString().c_str());
219 std::set<ObjectPtr> aFeatures = theMsg->objects();
220 XGUI_OperationMgr* anOperationMgr = workshop()->operationMgr();
221 if (anOperationMgr->hasOperation()) {
222 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
223 (anOperationMgr->currentOperation());
225 FeaturePtr aCurrentFeature = aFOperation->feature();
226 std::set<ObjectPtr>::const_iterator aIt;
227 for (aIt = aFeatures.begin(); aIt != aFeatures.end(); ++aIt) {
228 ObjectPtr aNewFeature = (*aIt);
229 if (aNewFeature == aCurrentFeature) {
230 workshop()->propertyPanel()->updateContentWidget(aCurrentFeature);
236 //anOperationMgr->onValidateOperation();
238 //if (myObjectBrowser)
239 // myObjectBrowser->processEvent(theMsg);
242 //******************************************************
243 void XGUI_WorkshopListener::
244 onFeatureRedisplayMsg(const std::shared_ptr<ModelAPI_ObjectUpdatedMessage>& theMsg)
246 std::set<ObjectPtr> anObjects = theMsg->objects();
247 std::set<ObjectPtr>::const_iterator aIt;
249 #ifdef DEBUG_FEATURE_REDISPLAY
251 for (aIt = anObjects.begin(); aIt != anObjects.end(); ++aIt) {
252 anInfo.append(ModuleBase_Tools::objectInfo((*aIt)));
254 QString anInfoStr = anInfo.join(";\t");
255 qDebug(QString("onFeatureRedisplayMsg: %1, %2")
256 .arg(anObjects.size()).arg(anInfoStr).toStdString().c_str());
259 XGUI_Workshop* aWorkshop = workshop();
260 XGUI_Displayer* aDisplayer = aWorkshop->displayer();
261 bool aRedisplayed = false;
262 //std::list<ObjectPtr> aHiddenObjects;
263 for (aIt = anObjects.begin(); aIt != anObjects.end(); ++aIt) {
264 ObjectPtr aObj = (*aIt);
266 // Hide the object if it is invalid or concealed one
267 bool aHide = !aObj->data() || !aObj->data()->isValid() ||
268 aObj->isDisabled() || (!aObj->isDisplayed());
269 if (!aHide) { // check that this is not hidden result
270 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(aObj);
271 aHide = aRes && aRes->isConcealed();
273 // Hide the presentation with an empty shape. But isDisplayed state of the object should not
274 // be changed to the object becomes visible when the shape becomes not empty
275 if (!aHide && aRes.get())
276 aHide = !aRes->shape().get() || aRes->shape()->isNull();
279 #ifdef DEBUG_RESULT_COMPSOLID
280 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(aObj);
282 ResultCompSolidPtr aCompSolidRes = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(aRes);
283 if (aCompSolidRes.get()) {
284 qDebug(QString("COMPSOLID, numberOfSubs = %1")
285 .arg(aCompSolidRes->numberOfSubs()).toStdString().c_str());
287 if (ModelAPI_Tools::compSolidOwner(aRes))
288 qDebug("COMPSOLID sub-object");
291 #ifdef DEBUG_FEATURE_REDISPLAY
292 QString anObjInfo = ModuleBase_Tools::objectInfo((aObj));
293 FeaturePtr aFeature = ModelAPI_Feature::feature(aObj);
294 if (aFeature.get()) {
295 std::string aKind = aFeature->getKind();
296 if (aKind == DebugFeatureKind || DebugFeatureKind.empty()) {
297 qDebug(QString("visible=%1, hide=%2 : display= %2").arg(aDisplayer->isVisible(aObj))
298 .arg(aHide).arg(anObjInfo).toStdString().c_str());
303 //we should provide objects which are hidden in the viewer, e.g. sketch always should
304 // visualizes all sub-features, if some features are to be hidden, sould be proposed may
305 // be to removed #1223
306 // aHiddenObjects.push_back(aObj);
307 aRedisplayed = aDisplayer->erase(aObj, false) || aRedisplayed;
308 #ifdef DEBUG_FEATURE_REDISPLAY
309 // Redisplay the visible object or the object of the current operation
310 bool isVisibleObject = aDisplayer->isVisible(aObj);
312 QString anObjInfo = ModuleBase_Tools::objectInfo((aObj));
316 // Redisplay the visible object or the object of the current operation
317 bool isVisibleObject = aDisplayer->isVisible(aObj);
318 #ifdef DEBUG_FEATURE_REDISPLAY
319 QString anObjInfo = ModuleBase_Tools::objectInfo((aObj));
322 if (isVisibleObject) { // redisplay visible object
323 //displayObject(aObj); // In order to update presentation
324 // in order to avoid the check whether the object can be redisplayed, the exact method
325 // of redisplay is called. This modification is made in order to have the line is updated
326 // by creation of a horizontal constraint on the line by preselection
327 if (ModelAPI_Tools::hasSubResults(std::dynamic_pointer_cast<ModelAPI_Result>(aObj))) {
328 aRedisplayed = aDisplayer->erase(aObj, false) || aRedisplayed;
331 aRedisplayed = aDisplayer->redisplay(aObj, false) || aRedisplayed;
332 // Deactivate object of current operation from selection
333 aWorkshop->deactivateActiveObject(aObj, false);
335 } else { // display object if the current operation has it
336 if (displayObject(aObj)) {
338 // Deactivate object of current operation from selection
339 aWorkshop->deactivateActiveObject(aObj, false);
344 // this processing should be moved in another place in order to do not cause problems in
345 // flush messages chain
346 //if (aHiddenObjects.size() > 0)
347 // myWorkshop->module()->processHiddenObject(aHiddenObjects);
349 bool isCustomized = customizeCurrentObject(anObjects, aRedisplayed);
350 if (aRedisplayed || isCustomized) {
351 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_EMPTY_AIS_PRESENTATION));
353 aDisplayer->updateViewer();
357 //******************************************************
358 void XGUI_WorkshopListener::
359 onFeatureCreatedMsg(const std::shared_ptr<ModelAPI_ObjectUpdatedMessage>& theMsg)
361 std::set<ObjectPtr> anObjects = theMsg->objects();
362 std::set<ObjectPtr>::const_iterator aIt;
363 #ifdef DEBUG_FEATURE_CREATED
365 for (aIt = anObjects.begin(); aIt != anObjects.end(); ++aIt) {
366 anInfo.append(ModuleBase_Tools::objectInfo((*aIt)));
368 QString anInfoStr = anInfo.join(";\t");
369 qDebug(QString("onFeatureCreatedMsg: %1, %2")
370 .arg(anObjects.size()).arg(anInfoStr).toStdString().c_str());
373 //bool aHasPart = false;
374 bool aDisplayed = false;
375 for (aIt = anObjects.begin(); aIt != anObjects.end(); ++aIt) {
376 ObjectPtr anObject = *aIt;
378 #ifdef DEBUG_RESULT_COMPSOLID
379 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
381 ResultCompSolidPtr aCompSolidRes = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(aRes);
382 if (aCompSolidRes.get()) {
383 qDebug(QString("COMPSOLID, numberOfSubs = %1")
384 .arg(aCompSolidRes->numberOfSubs()).toStdString().c_str());
386 if (ModelAPI_Tools::compSolidOwner(aRes))
387 qDebug("COMPSOLID sub-object");
390 // the validity of the data should be checked here in order to avoid display of the objects,
391 // which were created, then deleted, but flush for the creation event happens after that
392 // we should not display disabled objects
393 bool aHide = !anObject->data()->isValid() ||
394 anObject->isDisabled() ||
395 !anObject->isDisplayed();
396 if (!aHide) { // check that this is not hidden result
397 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
398 bool isConcealed = aRes && aRes->isConcealed();
399 aHide = aRes && aRes->isConcealed();
400 // Hide the presentation with an empty shape. But isDisplayed state of the object should not
401 // be changed to the object becomes visible when the shape becomes not empty
402 if (!aHide && aRes.get())
403 aHide = !aRes->shape().get() || aRes->shape()->isNull();
406 // setDisplayed has to be called in order to synchronize internal state of the object
407 // with list of displayed objects
408 if (myWorkshop->module()->canDisplayObject(anObject)) {
409 anObject->setDisplayed(true);
410 aDisplayed = displayObject(anObject);
412 anObject->setDisplayed(false);
416 bool isCustomized = customizeCurrentObject(anObjects, aDisplayed);
418 //if (myObjectBrowser)
419 // myObjectBrowser->processEvent(theMsg);
421 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_EMPTY_AIS_PRESENTATION));
422 workshop()->displayer()->updateViewer();
424 //if (aHasPart) { // TODO: Avoid activate last part on loading of document
425 // activateLastPart();
429 //******************************************************
430 void XGUI_WorkshopListener::onFeatureEmptyPresentationMsg(
431 const std::shared_ptr<ModelAPI_ObjectUpdatedMessage>& theMsg)
433 std::set<ObjectPtr> anObjects = theMsg->objects();
434 std::set<ObjectPtr>::const_iterator aIt;
435 #ifdef DEBUG_FEATURE_CREATED
437 for (aIt = anObjects.begin(); aIt != anObjects.end(); ++aIt) {
438 anInfo.append(ModuleBase_Tools::objectInfo((*aIt)));
440 QString anInfoStr = anInfo.join(";\t");
441 qDebug(QString("onFeatureEmptyPresentationMsg: %1, %2")
442 .arg(anObjects.size()).arg(anInfoStr).toStdString().c_str());
445 XGUI_Workshop* aWorkshop = workshop();
446 XGUI_Displayer* aDisplayer = aWorkshop->displayer();
448 bool aRedisplayed = false;
449 for (aIt = anObjects.begin(); aIt != anObjects.end(); ++aIt) {
450 ObjectPtr anObject = *aIt;
451 aRedisplayed = aDisplayer->erase(anObject, false) || aRedisplayed;
455 aDisplayer->updateViewer();
458 bool XGUI_WorkshopListener::event(QEvent * theEvent)
460 PostponeMessageQtEvent* aPostponedEv = dynamic_cast<PostponeMessageQtEvent*>(theEvent);
462 std::shared_ptr<Events_Message> aEventPtr = aPostponedEv->postponedMessage();
463 processEvent(aEventPtr);
469 //**************************************************************
470 bool XGUI_WorkshopListener::displayObject(ObjectPtr theObj)
472 #ifdef DEBUG_RESULT_COMPSOLID
473 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(theObj);
474 if (aRes.get() && (ModelAPI_Tools::hasSubResults(aRes) || ModelAPI_Tools::compSolidOwner(aRes))) {
475 ResultCompSolidPtr aCompSolidRes = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(aRes);
476 if (aCompSolidRes.get()) {
477 qDebug("COMPSOLID: displayObject");
482 bool aDisplayed = false;
483 XGUI_Workshop* aWorkshop = workshop();
484 // do not display the object if it has sub objects. They should be displayed separately.
485 if (!aWorkshop->module()->canDisplayObject(theObj) ||
486 ModelAPI_Tools::hasSubResults(std::dynamic_pointer_cast<ModelAPI_Result>(theObj)))
489 XGUI_Displayer* aDisplayer = aWorkshop->displayer();
490 int aNb = aDisplayer->objectsCount();
491 return aDisplayer->display(theObj, false);
494 bool XGUI_WorkshopListener::customizeCurrentObject(const std::set<ObjectPtr>& theObjects,
495 bool theForceRedisplay)
497 XGUI_OperationMgr* anOperationMgr = workshop()->operationMgr();
498 FeaturePtr aCurrentFeature;
499 if (anOperationMgr->hasOperation()) {
500 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
501 (anOperationMgr->currentOperation());
503 aCurrentFeature = aFOperation->feature();
507 bool aCustomized = false;
508 if (aCurrentFeature.get()) {
509 // the customize presentation should be redisplayed if force redislayed is true or
510 // if a list of message objects contains the operation feature for case when
511 // the feature is hidden, but arguments of the feature are modified
512 // e.g. extrusion is hidden(h=0) but sketch is chosen
513 if (theForceRedisplay || theObjects.find(aCurrentFeature) != theObjects.end()) {
514 aCustomized = myWorkshop->module()->customizeObject(aCurrentFeature,
515 ModuleBase_IModule::CustomizeArguments, false) || aCustomized;
516 aCustomized = myWorkshop->module()->customizeObject(aCurrentFeature,
517 ModuleBase_IModule::CustomizeResults, false) || aCustomized;
518 aCustomized = myWorkshop->module()->customizeObject(aCurrentFeature,
519 ModuleBase_IModule::CustomizeHighlightedObjects, false) || aCustomized;
525 XGUI_Workshop* XGUI_WorkshopListener::workshop() const
527 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
528 return aConnector->workshop();