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"
22 #include "XGUI_Workshop.h"
23 #include "XGUI_Displayer.h"
24 #include "XGUI_ErrorMgr.h"
25 #include "XGUI_OperationMgr.h"
26 #include "XGUI_SalomeConnector.h"
27 #include "XGUI_ActionsMgr.h"
28 #include "XGUI_PropertyPanel.h"
29 #include "XGUI_ModuleConnector.h"
30 #include "XGUI_QtEvents.h"
31 #include "XGUI_SelectionMgr.h"
34 #include <AppElements_MainWindow.h>
37 #include <ModuleBase_IModule.h>
38 #include <ModuleBase_Events.h>
40 #include <ModelAPI_Object.h>
41 #include <ModelAPI_Events.h>
42 #include <ModelAPI_Session.h>
43 #include <ModelAPI_Result.h>
44 #include <ModelAPI_Feature.h>
45 #include <ModelAPI_Data.h>
46 #include <ModelAPI_ResultCompSolid.h>
47 #include <ModelAPI_Tools.h>
49 #include <Events_Loop.h>
50 #include <Events_LongOp.h>
52 #include <ModuleBase_IWorkshop.h>
54 #include <ModuleBase_Operation.h>
55 #include <ModuleBase_OperationDescription.h>
56 #include <ModuleBase_OperationFeature.h>
57 #include <ModuleBase_Tools.h>
58 #include <ModuleBase_IViewer.h>
59 #include <ModuleBase_WidgetSelector.h>
61 #include <Config_FeatureMessage.h>
62 #include <Config_PointerMessage.h>
63 #include <Config_Keywords.h>
64 #include <Events_InfoMessage.h>
66 #include <QApplication>
67 #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));
115 //******************************************************
116 void XGUI_WorkshopListener::processEvent(const std::shared_ptr<Events_Message>& theMessage)
118 if (QApplication::instance()->thread() != QThread::currentThread()) {
120 std::cout << "XGUI_Workshop::processEvent: " << "Working in another thread." << std::endl;
122 SessionPtr aMgr = ModelAPI_Session::get();
123 PostponeMessageQtEvent* aPostponeEvent = new PostponeMessageQtEvent(theMessage);
124 QApplication::postEvent(this, aPostponeEvent);
128 // Process creation of Part
129 if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_CREATED)) {
130 std::shared_ptr<ModelAPI_ObjectUpdatedMessage> aUpdMsg =
131 std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
132 onFeatureCreatedMsg(aUpdMsg);
134 XGUI_SalomeConnector* aSalomeConnector = workshop()->salomeConnector();
135 if (aSalomeConnector)
136 aSalomeConnector->createPreferences();
137 myUpdatePrefs = false;
140 else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_PLUGIN_LOADED)) {
141 myUpdatePrefs = true;
144 else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY)) {
145 std::shared_ptr<ModelAPI_ObjectUpdatedMessage> aUpdMsg =
146 std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
147 onFeatureRedisplayMsg(aUpdMsg);
148 } else if (theMessage->eventID() == Events_Loop::eventByName(EVENT_EMPTY_AIS_PRESENTATION)) {
149 std::shared_ptr<ModelAPI_ObjectUpdatedMessage> aUpdMsg =
150 std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
151 onFeatureEmptyPresentationMsg(aUpdMsg);
152 } else if (theMessage->eventID() == Events_Loop::eventByName(EVENT_UPDATE_BY_WIDGET_SELECTION)) {
153 ModuleBase_ModelWidget* aWidget = workshop()->propertyPanel()->activeWidget();
155 ModuleBase_WidgetSelector* aWidgetSelector =
156 dynamic_cast<ModuleBase_WidgetSelector*>(aWidget);
158 workshop()->selector()->setSelected(aWidgetSelector->getAttributeSelection());
162 //Update property panel on corresponding message. If there is no current operation (no
163 //property panel), or received message has different feature to the current - do nothing.
164 else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_UPDATED)) {
165 std::shared_ptr<ModelAPI_ObjectUpdatedMessage> anUpdateMsg =
166 std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
167 onFeatureUpdatedMsg(anUpdateMsg);
168 } else if (theMessage->eventID() == Events_LongOp::eventID()) {
169 if (Events_LongOp::isPerformed()) {
170 QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
172 QApplication::restoreOverrideCursor();
174 } else if (theMessage->eventID() == Events_Loop::eventByName(EVENT_UPDATE_VIEWER_BLOCKED)) {
175 // the viewer's update context will not happens until viewer updated is emitted
176 workshop()->displayer()->enableUpdateViewer(false);
177 } else if (theMessage->eventID() == Events_Loop::eventByName(EVENT_UPDATE_VIEWER_UNBLOCKED)) {
178 // the viewer's update context is unblocked, the viewer's update works
179 XGUI_Displayer* aDisplayer = workshop()->displayer();
180 aDisplayer->enableUpdateViewer(true);
182 //Show error dialog if error message received.
183 std::shared_ptr<Events_InfoMessage> anIngfoMsg =
184 std::dynamic_pointer_cast<Events_InfoMessage>(theMessage);
186 emit errorOccurred(anIngfoMsg);
191 SessionPtr aMgr = ModelAPI_Session::get();
192 AppElements_MainWindow* aMainWindow = workshop()->mainWindow();
193 if (aMgr->isModified() != aMainWindow->isModifiedState())
194 aMainWindow->setModifiedState(aMgr->isModified());
198 //******************************************************
199 void XGUI_WorkshopListener::onFeatureUpdatedMsg(
200 const std::shared_ptr<ModelAPI_ObjectUpdatedMessage>& theMsg)
202 #ifdef DEBUG_FEATURE_UPDATED
203 std::set<ObjectPtr> anObjects = theMsg->objects();
204 std::set<ObjectPtr>::const_iterator aIt;
206 for (aIt = anObjects.begin(); aIt != anObjects.end(); ++aIt) {
207 anInfo.append(ModuleBase_Tools::objectInfo((*aIt)));
209 QString anInfoStr = anInfo.join(";\t");
210 qDebug(QString("onFeatureUpdatedMsg: %1, %2")
211 .arg(anObjects.size()).arg(anInfoStr).toStdString().c_str());
213 std::set<ObjectPtr> aFeatures = theMsg->objects();
214 XGUI_OperationMgr* anOperationMgr = workshop()->operationMgr();
215 if (anOperationMgr->hasOperation()) {
216 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
217 (anOperationMgr->currentOperation());
219 FeaturePtr aCurrentFeature = aFOperation->feature();
220 std::set<ObjectPtr>::const_iterator aIt;
221 for (aIt = aFeatures.begin(); aIt != aFeatures.end(); ++aIt) {
222 ObjectPtr aNewFeature = (*aIt);
223 if (aNewFeature == aCurrentFeature) {
224 workshop()->propertyPanel()->updateContentWidget(aCurrentFeature);
230 //anOperationMgr->onValidateOperation();
232 //if (myObjectBrowser)
233 // myObjectBrowser->processEvent(theMsg);
236 //******************************************************
237 void XGUI_WorkshopListener::
238 onFeatureRedisplayMsg(const std::shared_ptr<ModelAPI_ObjectUpdatedMessage>& theMsg)
240 std::set<ObjectPtr> anObjects = theMsg->objects();
241 std::set<ObjectPtr>::const_iterator aIt;
243 #ifdef DEBUG_FEATURE_REDISPLAY
245 for (aIt = anObjects.begin(); aIt != anObjects.end(); ++aIt) {
246 anInfo.append(ModuleBase_Tools::objectInfo((*aIt)));
248 QString anInfoStr = anInfo.join(";\t");
249 qDebug(QString("onFeatureRedisplayMsg: %1, %2")
250 .arg(anObjects.size()).arg(anInfoStr).toStdString().c_str());
253 XGUI_Workshop* aWorkshop = workshop();
254 XGUI_Displayer* aDisplayer = aWorkshop->displayer();
255 bool aRedisplayed = false;
256 //std::list<ObjectPtr> aHiddenObjects;
257 for (aIt = anObjects.begin(); aIt != anObjects.end(); ++aIt) {
258 ObjectPtr aObj = (*aIt);
260 // Hide the object if it is invalid or concealed one
261 bool aHide = !aObj->data() || !aObj->data()->isValid() ||
262 aObj->isDisabled() || (!aObj->isDisplayed());
263 if (!aHide) { // check that this is not hidden result
264 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(aObj);
265 aHide = aRes && aRes->isConcealed();
267 // Hide the presentation with an empty shape. But isDisplayed state of the object should not
268 // be changed to the object becomes visible when the shape becomes not empty
269 if (!aHide && aRes.get())
270 aHide = !aRes->shape().get() || aRes->shape()->isNull();
273 #ifdef DEBUG_RESULT_COMPSOLID
274 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(aObj);
276 ResultCompSolidPtr aCompSolidRes = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(aRes);
277 if (aCompSolidRes.get()) {
278 qDebug(QString("COMPSOLID, numberOfSubs = %1")
279 .arg(aCompSolidRes->numberOfSubs()).toStdString().c_str());
281 if (ModelAPI_Tools::compSolidOwner(aRes))
282 qDebug("COMPSOLID sub-object");
285 #ifdef DEBUG_FEATURE_REDISPLAY
286 QString anObjInfo = ModuleBase_Tools::objectInfo((aObj));
287 FeaturePtr aFeature = ModelAPI_Feature::feature(aObj);
288 if (aFeature.get()) {
289 std::string aKind = aFeature->getKind();
290 if (aKind == DebugFeatureKind || DebugFeatureKind.empty()) {
291 qDebug(QString("visible=%1, hide=%2 : display= %2").arg(aDisplayer->isVisible(aObj))
292 .arg(aHide).arg(anObjInfo).toStdString().c_str());
297 //we should provide objects which are hidden in the viewer, e.g. sketch always should
298 // visualizes all sub-features, if some features are to be hidden, sould be proposed may
299 // be to removed #1223
300 // aHiddenObjects.push_back(aObj);
301 aRedisplayed = aDisplayer->erase(aObj, false) || aRedisplayed;
302 #ifdef DEBUG_FEATURE_REDISPLAY
303 // Redisplay the visible object or the object of the current operation
304 bool isVisibleObject = aDisplayer->isVisible(aObj);
306 QString anObjInfo = ModuleBase_Tools::objectInfo((aObj));
310 // Redisplay the visible object or the object of the current operation
311 bool isVisibleObject = aDisplayer->isVisible(aObj);
312 #ifdef DEBUG_FEATURE_REDISPLAY
313 QString anObjInfo = ModuleBase_Tools::objectInfo((aObj));
316 if (isVisibleObject) { // redisplay visible object
317 //displayObject(aObj); // In order to update presentation
318 // in order to avoid the check whether the object can be redisplayed, the exact method
319 // of redisplay is called. This modification is made in order to have the line is updated
320 // by creation of a horizontal constraint on the line by preselection
321 if (ModelAPI_Tools::hasSubResults(std::dynamic_pointer_cast<ModelAPI_Result>(aObj))) {
322 aRedisplayed = aDisplayer->erase(aObj, false) || aRedisplayed;
325 aRedisplayed = aDisplayer->redisplay(aObj, false) || aRedisplayed;
326 // Deactivate object of current operation from selection
327 aWorkshop->deactivateActiveObject(aObj, false);
329 } else { // display object if the current operation has it
330 if (displayObject(aObj)) {
332 // Deactivate object of current operation from selection
333 aWorkshop->deactivateActiveObject(aObj, false);
338 // this processing should be moved in another place in order to do not cause problems in
339 // flush messages chain
340 //if (aHiddenObjects.size() > 0)
341 // myWorkshop->module()->processHiddenObject(aHiddenObjects);
343 bool isCustomized = customizeCurrentObject(anObjects, aRedisplayed);
344 if (aRedisplayed || isCustomized) {
345 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_EMPTY_AIS_PRESENTATION));
347 aDisplayer->updateViewer();
351 //******************************************************
352 void XGUI_WorkshopListener::
353 onFeatureCreatedMsg(const std::shared_ptr<ModelAPI_ObjectUpdatedMessage>& theMsg)
355 std::set<ObjectPtr> anObjects = theMsg->objects();
356 std::set<ObjectPtr>::const_iterator aIt;
357 #ifdef DEBUG_FEATURE_CREATED
359 for (aIt = anObjects.begin(); aIt != anObjects.end(); ++aIt) {
360 anInfo.append(ModuleBase_Tools::objectInfo((*aIt)));
362 QString anInfoStr = anInfo.join(";\t");
363 qDebug(QString("onFeatureCreatedMsg: %1, %2")
364 .arg(anObjects.size()).arg(anInfoStr).toStdString().c_str());
367 //bool aHasPart = false;
368 bool aDisplayed = false;
369 for (aIt = anObjects.begin(); aIt != anObjects.end(); ++aIt) {
370 ObjectPtr anObject = *aIt;
372 #ifdef DEBUG_RESULT_COMPSOLID
373 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
375 ResultCompSolidPtr aCompSolidRes = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(aRes);
376 if (aCompSolidRes.get()) {
377 qDebug(QString("COMPSOLID, numberOfSubs = %1")
378 .arg(aCompSolidRes->numberOfSubs()).toStdString().c_str());
380 if (ModelAPI_Tools::compSolidOwner(aRes))
381 qDebug("COMPSOLID sub-object");
384 // the validity of the data should be checked here in order to avoid display of the objects,
385 // which were created, then deleted, but flush for the creation event happens after that
386 // we should not display disabled objects
387 bool aHide = !anObject->data()->isValid() ||
388 anObject->isDisabled() ||
389 !anObject->isDisplayed();
390 if (!aHide) { // check that this is not hidden result
391 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
392 bool isConcealed = aRes && aRes->isConcealed();
393 aHide = aRes && aRes->isConcealed();
394 // Hide the presentation with an empty shape. But isDisplayed state of the object should not
395 // be changed to the object becomes visible when the shape becomes not empty
396 if (!aHide && aRes.get())
397 aHide = !aRes->shape().get() || aRes->shape()->isNull();
400 // setDisplayed has to be called in order to synchronize internal state of the object
401 // with list of displayed objects
402 if (myWorkshop->module()->canDisplayObject(anObject)) {
403 anObject->setDisplayed(true);
404 aDisplayed = displayObject(anObject);
406 anObject->setDisplayed(false);
410 bool isCustomized = customizeCurrentObject(anObjects, aDisplayed);
412 //if (myObjectBrowser)
413 // myObjectBrowser->processEvent(theMsg);
415 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_EMPTY_AIS_PRESENTATION));
416 workshop()->displayer()->updateViewer();
418 //if (aHasPart) { // TODO: Avoid activate last part on loading of document
419 // activateLastPart();
423 //******************************************************
424 void XGUI_WorkshopListener::onFeatureEmptyPresentationMsg(
425 const std::shared_ptr<ModelAPI_ObjectUpdatedMessage>& theMsg)
427 std::set<ObjectPtr> anObjects = theMsg->objects();
428 std::set<ObjectPtr>::const_iterator aIt;
429 #ifdef DEBUG_FEATURE_CREATED
431 for (aIt = anObjects.begin(); aIt != anObjects.end(); ++aIt) {
432 anInfo.append(ModuleBase_Tools::objectInfo((*aIt)));
434 QString anInfoStr = anInfo.join(";\t");
435 qDebug(QString("onFeatureEmptyPresentationMsg: %1, %2")
436 .arg(anObjects.size()).arg(anInfoStr).toStdString().c_str());
439 XGUI_Workshop* aWorkshop = workshop();
440 XGUI_Displayer* aDisplayer = aWorkshop->displayer();
442 bool aRedisplayed = false;
443 for (aIt = anObjects.begin(); aIt != anObjects.end(); ++aIt) {
444 ObjectPtr anObject = *aIt;
445 aRedisplayed = aDisplayer->erase(anObject, false) || aRedisplayed;
449 aDisplayer->updateViewer();
452 bool XGUI_WorkshopListener::event(QEvent * theEvent)
454 PostponeMessageQtEvent* aPostponedEv = dynamic_cast<PostponeMessageQtEvent*>(theEvent);
456 std::shared_ptr<Events_Message> aEventPtr = aPostponedEv->postponedMessage();
457 processEvent(aEventPtr);
463 //**************************************************************
464 bool XGUI_WorkshopListener::displayObject(ObjectPtr theObj)
466 #ifdef DEBUG_RESULT_COMPSOLID
467 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(theObj);
468 if (aRes.get() && (ModelAPI_Tools::hasSubResults(aRes) || ModelAPI_Tools::compSolidOwner(aRes))) {
469 ResultCompSolidPtr aCompSolidRes = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(aRes);
470 if (aCompSolidRes.get()) {
471 qDebug("COMPSOLID: displayObject");
476 bool aDisplayed = false;
477 XGUI_Workshop* aWorkshop = workshop();
478 // do not display the object if it has sub objects. They should be displayed separately.
479 if (!aWorkshop->module()->canDisplayObject(theObj) ||
480 ModelAPI_Tools::hasSubResults(std::dynamic_pointer_cast<ModelAPI_Result>(theObj)))
483 XGUI_Displayer* aDisplayer = aWorkshop->displayer();
484 int aNb = aDisplayer->objectsCount();
485 return aDisplayer->display(theObj, false);
488 bool XGUI_WorkshopListener::customizeCurrentObject(const std::set<ObjectPtr>& theObjects,
489 bool theForceRedisplay)
491 XGUI_OperationMgr* anOperationMgr = workshop()->operationMgr();
492 FeaturePtr aCurrentFeature;
493 if (anOperationMgr->hasOperation()) {
494 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
495 (anOperationMgr->currentOperation());
497 aCurrentFeature = aFOperation->feature();
501 bool aCustomized = false;
502 if (aCurrentFeature.get()) {
503 // the customize presentation should be redisplayed if force redislayed is true or
504 // if a list of message objects contains the operation feature for case when
505 // the feature is hidden, but arguments of the feature are modified
506 // e.g. extrusion is hidden(h=0) but sketch is chosen
507 if (theForceRedisplay || theObjects.find(aCurrentFeature) != theObjects.end()) {
508 aCustomized = myWorkshop->module()->customizeObject(aCurrentFeature,
509 ModuleBase_IModule::CustomizeArguments, false) || aCustomized;
510 aCustomized = myWorkshop->module()->customizeObject(aCurrentFeature,
511 ModuleBase_IModule::CustomizeResults, false) || aCustomized;
512 aCustomized = myWorkshop->module()->customizeObject(aCurrentFeature,
513 ModuleBase_IModule::CustomizeHighlightedObjects, false) || aCustomized;
519 XGUI_Workshop* XGUI_WorkshopListener::workshop() const
521 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
522 return aConnector->workshop();