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>
41 #include <ModelAPI_ResultField.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());
277 if (!aHide) { // check that this is not hidden result
278 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(aObj);
279 aHide = aRes && aRes->isConcealed();
281 // Hide the presentation with an empty shape. But isDisplayed state of the object should not
282 // be changed to the object becomes visible when the shape becomes not empty
283 if (!aHide && aRes.get())
284 aHide = !aRes->shape().get() || aRes->shape()->isNull();
287 #ifdef DEBUG_RESULT_COMPSOLID
288 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(aObj);
290 ResultCompSolidPtr aCompSolidRes = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(aRes);
291 if (aCompSolidRes.get()) {
292 qDebug(QString("COMPSOLID, numberOfSubs = %1")
293 .arg(aCompSolidRes->numberOfSubs()).toStdString().c_str());
295 if (ModelAPI_Tools::compSolidOwner(aRes))
296 qDebug("COMPSOLID sub-object");
299 #ifdef DEBUG_FEATURE_REDISPLAY
300 QString anObjInfo = ModuleBase_Tools::objectInfo((aObj));
301 FeaturePtr aFeature = ModelAPI_Feature::feature(aObj);
302 if (aFeature.get()) {
303 std::string aKind = aFeature->getKind();
304 if (aKind == DebugFeatureKind || DebugFeatureKind.empty()) {
305 qDebug(QString("visible=%1, hide=%2 : display= %2").arg(aDisplayer->isVisible(aObj))
306 .arg(aHide).arg(anObjInfo).toStdString().c_str());
311 //we should provide objects which are hidden in the viewer, e.g. sketch always should
312 // visualizes all sub-features, if some features are to be hidden, sould be proposed may
313 // be to removed #1223
314 // aHiddenObjects.push_back(aObj);
315 aRedisplayed = aDisplayer->erase(aObj, false) || aRedisplayed;
316 #ifdef DEBUG_FEATURE_REDISPLAY
317 // Redisplay the visible object or the object of the current operation
318 bool isVisibleObject = aDisplayer->isVisible(aObj);
320 QString anObjInfo = ModuleBase_Tools::objectInfo((aObj));
324 // Redisplay the visible object or the object of the current operation
325 bool isVisibleObject = aDisplayer->isVisible(aObj);
326 #ifdef DEBUG_FEATURE_REDISPLAY
327 QString anObjInfo = ModuleBase_Tools::objectInfo((aObj));
330 if (isVisibleObject) { // redisplay visible object
331 //displayObject(aObj); // In order to update presentation
332 // in order to avoid the check whether the object can be redisplayed, the exact method
333 // of redisplay is called. This modification is made in order to have the line is updated
334 // by creation of a horizontal constraint on the line by preselection
335 if (ModelAPI_Tools::hasSubResults(std::dynamic_pointer_cast<ModelAPI_Result>(aObj))) {
336 aRedisplayed = aDisplayer->erase(aObj, false) || aRedisplayed;
339 aRedisplayed = aDisplayer->redisplay(aObj, false) || aRedisplayed;
340 // Deactivate object of current operation from selection
341 aWorkshop->deactivateActiveObject(aObj, false);
343 } else { // display object if the current operation has it
344 if (displayObject(aObj)) {
346 // Deactivate object of current operation from selection
347 aWorkshop->deactivateActiveObject(aObj, false);
352 // this processing should be moved in another place in order to do not cause problems in
353 // flush messages chain
354 //if (aHiddenObjects.size() > 0)
355 // myWorkshop->module()->processHiddenObject(aHiddenObjects);
357 bool isCustomized = customizeCurrentObject(anObjects, aRedisplayed);
358 if (aRedisplayed || isCustomized) {
359 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_EMPTY_AIS_PRESENTATION));
361 aDisplayer->updateViewer();
365 //******************************************************
366 void XGUI_WorkshopListener::
367 onFeatureCreatedMsg(const std::shared_ptr<ModelAPI_ObjectUpdatedMessage>& theMsg)
369 std::set<ObjectPtr> anObjects = theMsg->objects();
370 std::set<ObjectPtr>::const_iterator aIt;
371 #ifdef DEBUG_FEATURE_CREATED
373 for (aIt = anObjects.begin(); aIt != anObjects.end(); ++aIt) {
374 anInfo.append(ModuleBase_Tools::objectInfo((*aIt)));
376 QString anInfoStr = anInfo.join(";\t");
377 qDebug(QString("onFeatureCreatedMsg: %1, %2")
378 .arg(anObjects.size()).arg(anInfoStr).toStdString().c_str());
381 //bool aHasPart = false;
382 bool aDisplayed = false;
383 for (aIt = anObjects.begin(); aIt != anObjects.end(); ++aIt) {
384 ObjectPtr anObject = *aIt;
386 #ifdef DEBUG_RESULT_COMPSOLID
387 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
389 ResultCompSolidPtr aCompSolidRes = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(aRes);
390 if (aCompSolidRes.get()) {
391 qDebug(QString("COMPSOLID, numberOfSubs = %1")
392 .arg(aCompSolidRes->numberOfSubs()).toStdString().c_str());
394 if (ModelAPI_Tools::compSolidOwner(aRes))
395 qDebug("COMPSOLID sub-object");
398 // the validity of the data should be checked here in order to avoid display of the objects,
399 // which were created, then deleted, but flush for the creation event happens after that
400 // we should not display disabled objects
401 bool aHide = !anObject->data()->isValid() ||
402 anObject->isDisabled() ||
403 !anObject->isDisplayed();
404 if (!aHide) { // check that this is not hidden result
405 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
406 bool isConcealed = aRes && aRes->isConcealed();
407 aHide = aRes && aRes->isConcealed();
408 // Hide the presentation with an empty shape. But isDisplayed state of the object should not
409 // be changed to the object becomes visible when the shape becomes not empty
410 if (!aHide && aRes.get())
411 aHide = !aRes->shape().get() || aRes->shape()->isNull();
414 // setDisplayed has to be called in order to synchronize internal state of the object
415 // with list of displayed objects
416 if (myWorkshop->module()->canDisplayObject(anObject)) {
417 anObject->setDisplayed(true);
418 aDisplayed = displayObject(anObject);
420 anObject->setDisplayed(false);
424 bool isCustomized = customizeCurrentObject(anObjects, aDisplayed);
426 //if (myObjectBrowser)
427 // myObjectBrowser->processEvent(theMsg);
429 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_EMPTY_AIS_PRESENTATION));
430 workshop()->displayer()->updateViewer();
432 //if (aHasPart) { // TODO: Avoid activate last part on loading of document
433 // activateLastPart();
437 //******************************************************
438 void XGUI_WorkshopListener::onFeatureEmptyPresentationMsg(
439 const std::shared_ptr<ModelAPI_ObjectUpdatedMessage>& theMsg)
441 std::set<ObjectPtr> anObjects = theMsg->objects();
442 std::set<ObjectPtr>::const_iterator aIt;
443 #ifdef DEBUG_FEATURE_CREATED
445 for (aIt = anObjects.begin(); aIt != anObjects.end(); ++aIt) {
446 anInfo.append(ModuleBase_Tools::objectInfo((*aIt)));
448 QString anInfoStr = anInfo.join(";\t");
449 qDebug(QString("onFeatureEmptyPresentationMsg: %1, %2")
450 .arg(anObjects.size()).arg(anInfoStr).toStdString().c_str());
453 XGUI_Workshop* aWorkshop = workshop();
454 XGUI_Displayer* aDisplayer = aWorkshop->displayer();
456 bool aRedisplayed = false;
457 for (aIt = anObjects.begin(); aIt != anObjects.end(); ++aIt) {
458 ObjectPtr anObject = *aIt;
459 aRedisplayed = aDisplayer->erase(anObject, false) || aRedisplayed;
463 aDisplayer->updateViewer();
466 bool XGUI_WorkshopListener::event(QEvent * theEvent)
468 PostponeMessageQtEvent* aPostponedEv = dynamic_cast<PostponeMessageQtEvent*>(theEvent);
470 std::shared_ptr<Events_Message> aEventPtr = aPostponedEv->postponedMessage();
471 processEvent(aEventPtr);
477 //**************************************************************
478 bool XGUI_WorkshopListener::displayObject(ObjectPtr theObj)
480 #ifdef DEBUG_RESULT_COMPSOLID
481 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(theObj);
482 if (aRes.get() && (ModelAPI_Tools::hasSubResults(aRes) || ModelAPI_Tools::compSolidOwner(aRes))) {
483 ResultCompSolidPtr aCompSolidRes = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(aRes);
484 if (aCompSolidRes.get()) {
485 qDebug("COMPSOLID: displayObject");
490 bool aDisplayed = false;
491 XGUI_Workshop* aWorkshop = workshop();
492 // do not display the object if it has sub objects. They should be displayed separately.
493 if (!aWorkshop->module()->canDisplayObject(theObj) ||
494 ModelAPI_Tools::hasSubResults(std::dynamic_pointer_cast<ModelAPI_Result>(theObj)))
497 XGUI_Displayer* aDisplayer = aWorkshop->displayer();
498 int aNb = aDisplayer->objectsCount();
499 return aDisplayer->display(theObj, false);
502 bool XGUI_WorkshopListener::customizeCurrentObject(const std::set<ObjectPtr>& theObjects,
503 bool theForceRedisplay)
505 XGUI_OperationMgr* anOperationMgr = workshop()->operationMgr();
506 FeaturePtr aCurrentFeature;
507 if (anOperationMgr->hasOperation()) {
508 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
509 (anOperationMgr->currentOperation());
511 aCurrentFeature = aFOperation->feature();
515 bool aCustomized = false;
516 if (aCurrentFeature.get()) {
517 // the customize presentation should be redisplayed if force redislayed is true or
518 // if a list of message objects contains the operation feature for case when
519 // the feature is hidden, but arguments of the feature are modified
520 // e.g. extrusion is hidden(h=0) but sketch is chosen
521 if (theForceRedisplay || theObjects.find(aCurrentFeature) != theObjects.end()) {
522 aCustomized = myWorkshop->module()->customizeObject(aCurrentFeature,
523 ModuleBase_IModule::CustomizeArguments, false) || aCustomized;
524 aCustomized = myWorkshop->module()->customizeObject(aCurrentFeature,
525 ModuleBase_IModule::CustomizeResults, false) || aCustomized;
526 aCustomized = myWorkshop->module()->customizeObject(aCurrentFeature,
527 ModuleBase_IModule::CustomizeHighlightedObjects, false) || aCustomized;
533 XGUI_Workshop* XGUI_WorkshopListener::workshop() const