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_ResultCompSolid.h>
42 #include <ModelAPI_Tools.h>
44 #include <ModuleBase_Events.h>
45 #include <ModuleBase_IModule.h>
46 #include <ModuleBase_IViewer.h>
47 #include <ModuleBase_IWorkshop.h>
48 #include <ModuleBase_Operation.h>
49 #include <ModuleBase_OperationDescription.h>
50 #include <ModuleBase_OperationFeature.h>
51 #include <ModuleBase_Tools.h>
52 #include <ModuleBase_WidgetSelector.h>
54 #include "XGUI_ActionsMgr.h"
55 #include "XGUI_Displayer.h"
56 #include "XGUI_ErrorMgr.h"
57 #include "XGUI_FacesPanel.h"
58 #include "XGUI_OperationMgr.h"
59 #include "XGUI_ModuleConnector.h"
60 #include "XGUI_PropertyPanel.h"
62 #include "XGUI_QtEvents.h"
63 #include "XGUI_SalomeConnector.h"
64 #include "XGUI_SelectionMgr.h"
65 #include "XGUI_Workshop.h"
68 #include <QApplication>
69 #include <QMainWindow>
77 //#define DEBUG_FEATURE_CREATED
78 //#define DEBUG_FEATURE_REDISPLAY
79 //#define DEBUG_FEATURE_UPDATED
80 //#define DEBUG_RESULT_COMPSOLID
82 #ifdef DEBUG_FEATURE_REDISPLAY
83 const std::string DebugFeatureKind = "";//"Extrusion";
86 XGUI_WorkshopListener::XGUI_WorkshopListener(ModuleBase_IWorkshop* theWorkshop)
87 : myWorkshop(theWorkshop),
90 XGUI_OperationMgr* anOperationMgr = workshop()->operationMgr();
93 //******************************************************
94 XGUI_WorkshopListener::~XGUI_WorkshopListener(void)
98 //******************************************************
99 void XGUI_WorkshopListener::initializeEventListening()
101 //Initialize event listening
102 Events_Loop* aLoop = Events_Loop::loop();
103 aLoop->registerListener(this, Events_InfoMessage::errorID()); //!< Listening application errors.
104 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
105 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_CREATED));
106 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
107 aLoop->registerListener(this, Events_LongOp::eventID());
108 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_PLUGIN_LOADED));
110 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_UPDATE_VIEWER_BLOCKED));
111 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_UPDATE_VIEWER_UNBLOCKED));
112 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_EMPTY_AIS_PRESENTATION));
113 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_UPDATE_BY_WIDGET_SELECTION));
115 aLoop->registerListener(this, Events_Loop::eventByName("FinishOperation"));
116 aLoop->registerListener(this, Events_Loop::eventByName("AbortOperation"));
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);
189 //Show error dialog if error message received.
190 std::shared_ptr<Events_InfoMessage> anIngfoMsg =
191 std::dynamic_pointer_cast<Events_InfoMessage>(theMessage);
193 emit errorOccurred(anIngfoMsg);
198 SessionPtr aMgr = ModelAPI_Session::get();
199 AppElements_MainWindow* aMainWindow = workshop()->mainWindow();
200 if (aMgr->isModified() != aMainWindow->isModifiedState())
201 aMainWindow->setModifiedState(aMgr->isModified());
205 //******************************************************
206 void XGUI_WorkshopListener::onFeatureUpdatedMsg(
207 const std::shared_ptr<ModelAPI_ObjectUpdatedMessage>& theMsg)
209 #ifdef DEBUG_FEATURE_UPDATED
210 std::set<ObjectPtr> anObjects = theMsg->objects();
211 std::set<ObjectPtr>::const_iterator aIt;
213 for (aIt = anObjects.begin(); aIt != anObjects.end(); ++aIt) {
214 anInfo.append(ModuleBase_Tools::objectInfo((*aIt)));
216 QString anInfoStr = anInfo.join(";\t");
217 qDebug(QString("onFeatureUpdatedMsg: %1, %2")
218 .arg(anObjects.size()).arg(anInfoStr).toStdString().c_str());
220 std::set<ObjectPtr> aFeatures = theMsg->objects();
221 XGUI_OperationMgr* anOperationMgr = workshop()->operationMgr();
222 if (anOperationMgr->hasOperation()) {
223 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
224 (anOperationMgr->currentOperation());
226 FeaturePtr aCurrentFeature = aFOperation->feature();
227 std::set<ObjectPtr>::const_iterator aIt;
228 for (aIt = aFeatures.begin(); aIt != aFeatures.end(); ++aIt) {
229 ObjectPtr aNewFeature = (*aIt);
230 if (aNewFeature == aCurrentFeature) {
231 workshop()->propertyPanel()->updateContentWidget(aCurrentFeature);
237 //anOperationMgr->onValidateOperation();
239 //if (myObjectBrowser)
240 // myObjectBrowser->processEvent(theMsg);
243 //******************************************************
244 void XGUI_WorkshopListener::
245 onFeatureRedisplayMsg(const std::shared_ptr<ModelAPI_ObjectUpdatedMessage>& theMsg)
247 std::set<ObjectPtr> anObjects = theMsg->objects();
248 std::set<ObjectPtr>::const_iterator aIt;
250 #ifdef DEBUG_FEATURE_REDISPLAY
252 for (aIt = anObjects.begin(); aIt != anObjects.end(); ++aIt) {
253 anInfo.append(ModuleBase_Tools::objectInfo((*aIt)));
255 QString anInfoStr = anInfo.join(";\t");
256 qDebug(QString("onFeatureRedisplayMsg: %1, %2")
257 .arg(anObjects.size()).arg(anInfoStr).toStdString().c_str());
260 XGUI_Workshop* aWorkshop = workshop();
261 XGUI_Displayer* aDisplayer = aWorkshop->displayer();
262 bool aRedisplayed = false;
263 //std::list<ObjectPtr> aHiddenObjects;
264 for (aIt = anObjects.begin(); aIt != anObjects.end(); ++aIt) {
265 ObjectPtr aObj = (*aIt);
267 // Hide the object if it is invalid or concealed one
268 bool aHide = !aObj->data() || !aObj->data()->isValid() ||
269 aObj->isDisabled() || (!aObj->isDisplayed());
270 if (!aHide) { // check that this is not hidden result
271 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(aObj);
272 aHide = aRes && aRes->isConcealed();
274 // Hide the presentation with an empty shape. But isDisplayed state of the object should not
275 // be changed to the object becomes visible when the shape becomes not empty
276 if (!aHide && aRes.get())
277 aHide = !aRes->shape().get() || aRes->shape()->isNull();
280 #ifdef DEBUG_RESULT_COMPSOLID
281 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(aObj);
283 ResultCompSolidPtr aCompSolidRes = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(aRes);
284 if (aCompSolidRes.get()) {
285 qDebug(QString("COMPSOLID, numberOfSubs = %1")
286 .arg(aCompSolidRes->numberOfSubs()).toStdString().c_str());
288 if (ModelAPI_Tools::compSolidOwner(aRes))
289 qDebug("COMPSOLID sub-object");
292 #ifdef DEBUG_FEATURE_REDISPLAY
293 QString anObjInfo = ModuleBase_Tools::objectInfo((aObj));
294 FeaturePtr aFeature = ModelAPI_Feature::feature(aObj);
295 if (aFeature.get()) {
296 std::string aKind = aFeature->getKind();
297 if (aKind == DebugFeatureKind || DebugFeatureKind.empty()) {
298 qDebug(QString("visible=%1, hide=%2 : display= %2").arg(aDisplayer->isVisible(aObj))
299 .arg(aHide).arg(anObjInfo).toStdString().c_str());
304 //we should provide objects which are hidden in the viewer, e.g. sketch always should
305 // visualizes all sub-features, if some features are to be hidden, sould be proposed may
306 // be to removed #1223
307 // aHiddenObjects.push_back(aObj);
308 aRedisplayed = aDisplayer->erase(aObj, false) || aRedisplayed;
309 #ifdef DEBUG_FEATURE_REDISPLAY
310 // Redisplay the visible object or the object of the current operation
311 bool isVisibleObject = aDisplayer->isVisible(aObj);
313 QString anObjInfo = ModuleBase_Tools::objectInfo((aObj));
317 // Redisplay the visible object or the object of the current operation
318 bool isVisibleObject = aDisplayer->isVisible(aObj);
319 #ifdef DEBUG_FEATURE_REDISPLAY
320 QString anObjInfo = ModuleBase_Tools::objectInfo((aObj));
323 if (isVisibleObject) { // redisplay visible object
324 //displayObject(aObj); // In order to update presentation
325 // in order to avoid the check whether the object can be redisplayed, the exact method
326 // of redisplay is called. This modification is made in order to have the line is updated
327 // by creation of a horizontal constraint on the line by preselection
328 if (ModelAPI_Tools::hasSubResults(std::dynamic_pointer_cast<ModelAPI_Result>(aObj))) {
329 aRedisplayed = aDisplayer->erase(aObj, false) || aRedisplayed;
332 aRedisplayed = aDisplayer->redisplay(aObj, false) || aRedisplayed;
333 // Deactivate object of current operation from selection
334 aWorkshop->deactivateActiveObject(aObj, false);
336 } else { // display object if the current operation has it
337 if (displayObject(aObj)) {
339 // Deactivate object of current operation from selection
340 aWorkshop->deactivateActiveObject(aObj, false);
345 // this processing should be moved in another place in order to do not cause problems in
346 // flush messages chain
347 //if (aHiddenObjects.size() > 0)
348 // myWorkshop->module()->processHiddenObject(aHiddenObjects);
350 bool isCustomized = customizeCurrentObject(anObjects, aRedisplayed);
351 if (aRedisplayed || isCustomized) {
352 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_EMPTY_AIS_PRESENTATION));
354 aDisplayer->updateViewer();
358 //******************************************************
359 void XGUI_WorkshopListener::
360 onFeatureCreatedMsg(const std::shared_ptr<ModelAPI_ObjectUpdatedMessage>& theMsg)
362 std::set<ObjectPtr> anObjects = theMsg->objects();
363 std::set<ObjectPtr>::const_iterator aIt;
364 #ifdef DEBUG_FEATURE_CREATED
366 for (aIt = anObjects.begin(); aIt != anObjects.end(); ++aIt) {
367 anInfo.append(ModuleBase_Tools::objectInfo((*aIt)));
369 QString anInfoStr = anInfo.join(";\t");
370 qDebug(QString("onFeatureCreatedMsg: %1, %2")
371 .arg(anObjects.size()).arg(anInfoStr).toStdString().c_str());
374 //bool aHasPart = false;
375 bool aDisplayed = false;
376 for (aIt = anObjects.begin(); aIt != anObjects.end(); ++aIt) {
377 ObjectPtr anObject = *aIt;
379 #ifdef DEBUG_RESULT_COMPSOLID
380 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
382 ResultCompSolidPtr aCompSolidRes = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(aRes);
383 if (aCompSolidRes.get()) {
384 qDebug(QString("COMPSOLID, numberOfSubs = %1")
385 .arg(aCompSolidRes->numberOfSubs()).toStdString().c_str());
387 if (ModelAPI_Tools::compSolidOwner(aRes))
388 qDebug("COMPSOLID sub-object");
391 // the validity of the data should be checked here in order to avoid display of the objects,
392 // which were created, then deleted, but flush for the creation event happens after that
393 // we should not display disabled objects
394 bool aHide = !anObject->data()->isValid() ||
395 anObject->isDisabled() ||
396 !anObject->isDisplayed();
397 if (!aHide) { // check that this is not hidden result
398 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
399 bool isConcealed = aRes && aRes->isConcealed();
400 aHide = aRes && aRes->isConcealed();
401 // Hide the presentation with an empty shape. But isDisplayed state of the object should not
402 // be changed to the object becomes visible when the shape becomes not empty
403 if (!aHide && aRes.get())
404 aHide = !aRes->shape().get() || aRes->shape()->isNull();
407 // setDisplayed has to be called in order to synchronize internal state of the object
408 // with list of displayed objects
409 if (myWorkshop->module()->canDisplayObject(anObject)) {
410 anObject->setDisplayed(true);
411 aDisplayed = displayObject(anObject);
413 anObject->setDisplayed(false);
417 bool isCustomized = customizeCurrentObject(anObjects, aDisplayed);
419 //if (myObjectBrowser)
420 // myObjectBrowser->processEvent(theMsg);
422 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_EMPTY_AIS_PRESENTATION));
423 workshop()->displayer()->updateViewer();
425 //if (aHasPart) { // TODO: Avoid activate last part on loading of document
426 // activateLastPart();
430 //******************************************************
431 void XGUI_WorkshopListener::onFeatureEmptyPresentationMsg(
432 const std::shared_ptr<ModelAPI_ObjectUpdatedMessage>& theMsg)
434 std::set<ObjectPtr> anObjects = theMsg->objects();
435 std::set<ObjectPtr>::const_iterator aIt;
436 #ifdef DEBUG_FEATURE_CREATED
438 for (aIt = anObjects.begin(); aIt != anObjects.end(); ++aIt) {
439 anInfo.append(ModuleBase_Tools::objectInfo((*aIt)));
441 QString anInfoStr = anInfo.join(";\t");
442 qDebug(QString("onFeatureEmptyPresentationMsg: %1, %2")
443 .arg(anObjects.size()).arg(anInfoStr).toStdString().c_str());
446 XGUI_Workshop* aWorkshop = workshop();
447 XGUI_Displayer* aDisplayer = aWorkshop->displayer();
449 bool aRedisplayed = false;
450 for (aIt = anObjects.begin(); aIt != anObjects.end(); ++aIt) {
451 ObjectPtr anObject = *aIt;
452 aRedisplayed = aDisplayer->erase(anObject, false) || aRedisplayed;
456 aDisplayer->updateViewer();
459 bool XGUI_WorkshopListener::event(QEvent * theEvent)
461 PostponeMessageQtEvent* aPostponedEv = dynamic_cast<PostponeMessageQtEvent*>(theEvent);
463 std::shared_ptr<Events_Message> aEventPtr = aPostponedEv->postponedMessage();
464 processEvent(aEventPtr);
470 //**************************************************************
471 bool XGUI_WorkshopListener::displayObject(ObjectPtr theObj)
473 #ifdef DEBUG_RESULT_COMPSOLID
474 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(theObj);
475 if (aRes.get() && (ModelAPI_Tools::hasSubResults(aRes) || ModelAPI_Tools::compSolidOwner(aRes))) {
476 ResultCompSolidPtr aCompSolidRes = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(aRes);
477 if (aCompSolidRes.get()) {
478 qDebug("COMPSOLID: displayObject");
483 bool aDisplayed = false;
484 XGUI_Workshop* aWorkshop = workshop();
485 // do not display the object if it has sub objects. They should be displayed separately.
486 if (!aWorkshop->module()->canDisplayObject(theObj) ||
487 ModelAPI_Tools::hasSubResults(std::dynamic_pointer_cast<ModelAPI_Result>(theObj)))
490 XGUI_Displayer* aDisplayer = aWorkshop->displayer();
491 int aNb = aDisplayer->objectsCount();
492 return aDisplayer->display(theObj, false);
495 bool XGUI_WorkshopListener::customizeCurrentObject(const std::set<ObjectPtr>& theObjects,
496 bool theForceRedisplay)
498 XGUI_OperationMgr* anOperationMgr = workshop()->operationMgr();
499 FeaturePtr aCurrentFeature;
500 if (anOperationMgr->hasOperation()) {
501 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
502 (anOperationMgr->currentOperation());
504 aCurrentFeature = aFOperation->feature();
508 bool aCustomized = false;
509 if (aCurrentFeature.get()) {
510 // the customize presentation should be redisplayed if force redislayed is true or
511 // if a list of message objects contains the operation feature for case when
512 // the feature is hidden, but arguments of the feature are modified
513 // e.g. extrusion is hidden(h=0) but sketch is chosen
514 if (theForceRedisplay || theObjects.find(aCurrentFeature) != theObjects.end()) {
515 aCustomized = myWorkshop->module()->customizeObject(aCurrentFeature,
516 ModuleBase_IModule::CustomizeArguments, false) || aCustomized;
517 aCustomized = myWorkshop->module()->customizeObject(aCurrentFeature,
518 ModuleBase_IModule::CustomizeResults, false) || aCustomized;
519 aCustomized = myWorkshop->module()->customizeObject(aCurrentFeature,
520 ModuleBase_IModule::CustomizeHighlightedObjects, false) || aCustomized;
526 XGUI_Workshop* XGUI_WorkshopListener::workshop() const
528 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
529 return aConnector->workshop();