1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
3 #include "XGUI_WorkshopListener.h"
4 #include "XGUI_Workshop.h"
5 #include "XGUI_Displayer.h"
6 #include "XGUI_OperationMgr.h"
7 #include "XGUI_SalomeConnector.h"
8 #include "XGUI_ActionsMgr.h"
9 #include "XGUI_PropertyPanel.h"
10 #include "XGUI_ModuleConnector.h"
11 #include "XGUI_QtEvents.h"
13 #include <AppElements_Workbench.h>
14 #include <AppElements_Command.h>
15 #include <AppElements_MainMenu.h>
16 #include <AppElements_MainWindow.h>
17 #include <AppElements_MenuGroupPanel.h>
18 #include <AppElements_Button.h>
20 #include <ModuleBase_IModule.h>
22 #include <ModelAPI_Object.h>
23 #include <ModelAPI_Events.h>
24 #include <ModelAPI_Session.h>
25 #include <ModelAPI_Result.h>
26 #include <ModelAPI_Feature.h>
27 #include <ModelAPI_Data.h>
28 #include <ModelAPI_ResultBody.h>
30 #include <Events_Loop.h>
31 #include <Events_Error.h>
32 #include <Events_LongOp.h>
34 #include <ModuleBase_IWorkshop.h>
36 #include <ModuleBase_Operation.h>
37 #include <ModuleBase_OperationDescription.h>
38 #include <ModuleBase_Tools.h>
39 #include <ModuleBase_IViewer.h>
40 #include <ModuleBase_FilterFactory.h>
42 #include <Config_FeatureMessage.h>
43 #include <Config_PointerMessage.h>
44 #include <Config_SelectionFilterMessage.h>
46 #include <QApplication>
47 #include <QMainWindow>
56 //#define DEBUG_FEATURE_CREATED
57 //#define DEBUG_FEATURE_REDISPLAY
59 XGUI_WorkshopListener::XGUI_WorkshopListener(ModuleBase_IWorkshop* theWorkshop)
60 : myWorkshop(theWorkshop),
65 //******************************************************
66 XGUI_WorkshopListener::~XGUI_WorkshopListener(void)
70 //******************************************************
71 void XGUI_WorkshopListener::initializeEventListening()
73 //Initialize event listening
74 Events_Loop* aLoop = Events_Loop::loop();
75 aLoop->registerListener(this, Events_Error::errorID()); //!< Listening application errors.
76 aLoop->registerListener(this, Events_Loop::eventByName(Config_FeatureMessage::GUI_EVENT()));
77 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OPERATION_LAUNCHED));
78 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
79 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_CREATED));
80 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
81 aLoop->registerListener(this, Events_LongOp::eventID());
82 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_PLUGIN_LOADED));
83 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_SELFILTER_LOADED));
85 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_UPDATE_VIEWER_BLOCKED));
86 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_UPDATE_VIEWER_UNBLOCKED));
89 //******************************************************
90 void XGUI_WorkshopListener::processEvent(const std::shared_ptr<Events_Message>& theMessage)
92 if (QApplication::instance()->thread() != QThread::currentThread()) {
94 std::cout << "XGUI_Workshop::processEvent: " << "Working in another thread." << std::endl;
96 SessionPtr aMgr = ModelAPI_Session::get();
97 PostponeMessageQtEvent* aPostponeEvent = new PostponeMessageQtEvent(theMessage);
98 QApplication::postEvent(this, aPostponeEvent);
102 //A message to start feature creation received.
103 if (theMessage->eventID() == Events_Loop::loop()->eventByName(Config_FeatureMessage::GUI_EVENT())) {
104 std::shared_ptr<Config_FeatureMessage> aFeatureMsg =
105 std::dynamic_pointer_cast<Config_FeatureMessage>(theMessage);
106 if (!aFeatureMsg->isInternal()) {
107 addFeature(aFeatureMsg);
110 // Process creation of Part
111 else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_CREATED)) {
112 std::shared_ptr<ModelAPI_ObjectUpdatedMessage> aUpdMsg =
113 std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
114 onFeatureCreatedMsg(aUpdMsg);
116 XGUI_SalomeConnector* aSalomeConnector = workshop()->salomeConnector();
117 if (aSalomeConnector)
118 aSalomeConnector->createPreferences();
119 myUpdatePrefs = false;
122 else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_PLUGIN_LOADED)) {
123 myUpdatePrefs = true;
126 else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY)) {
127 std::shared_ptr<ModelAPI_ObjectUpdatedMessage> aUpdMsg =
128 std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
129 onFeatureRedisplayMsg(aUpdMsg);
131 //Update property panel on corresponding message. If there is no current operation (no
132 //property panel), or received message has different feature to the current - do nothing.
133 else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_UPDATED)) {
134 std::shared_ptr<ModelAPI_ObjectUpdatedMessage> anUpdateMsg =
135 std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
136 onFeatureUpdatedMsg(anUpdateMsg);
137 } else if (theMessage->eventID() == Events_LongOp::eventID()) {
138 if (Events_LongOp::isPerformed()) {
139 QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
141 QApplication::restoreOverrideCursor();
144 //An operation passed by message. Start it, process and commit.
145 else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OPERATION_LAUNCHED)) {
146 std::shared_ptr<Config_PointerMessage> aPartSetMsg =
147 std::dynamic_pointer_cast<Config_PointerMessage>(theMessage);
148 //myPropertyPanel->cleanContent();
149 ModuleBase_Operation* anOperation = (ModuleBase_Operation*) aPartSetMsg->pointer();
150 XGUI_OperationMgr* anOperationMgr = workshop()->operationMgr();
152 if (anOperationMgr->startOperation(anOperation)) {
153 workshop()->propertyPanel()->updateContentWidget(anOperation->feature());
154 if (!anOperation->getDescription()->hasXmlRepresentation()) {
155 if (anOperation->commit())
156 workshop()->updateCommandStatus();
160 else if (theMessage->eventID() == Events_Loop::eventByName(EVENT_SELFILTER_LOADED)) {
161 std::shared_ptr<Config_SelectionFilterMessage> aMsg =
162 std::dynamic_pointer_cast<Config_SelectionFilterMessage>(theMessage);
164 ModuleBase_FilterFactory* aFactory = myWorkshop->selectionFilters();
165 if (!aMsg->attributeId().empty()) {
166 aFactory->assignFilter(aMsg->selectionFilterId(), aMsg->featureId(), aMsg->attributeId(),
170 } else if (theMessage->eventID() == Events_Loop::eventByName(EVENT_UPDATE_VIEWER_BLOCKED)) {
171 // the viewer's update context will not happens until viewer updated is emitted
172 workshop()->displayer()->enableUpdateViewer(false);
173 } else if (theMessage->eventID() == Events_Loop::eventByName(EVENT_UPDATE_VIEWER_UNBLOCKED)) {
174 // the viewer's update context is unblocked, the viewer's update works
175 XGUI_Displayer* aDisplayer = workshop()->displayer();
176 aDisplayer->enableUpdateViewer(true);
177 aDisplayer->updateViewer();
179 //Show error dialog if error message received.
180 std::shared_ptr<Events_Error> anAppError = std::dynamic_pointer_cast<Events_Error>(theMessage);
182 emit errorOccurred(QString::fromLatin1(anAppError->description()));
186 if (!workshop()->isSalomeMode()) {
187 SessionPtr aMgr = ModelAPI_Session::get();
188 AppElements_MainWindow* aMainWindow = workshop()->mainWindow();
189 if (aMgr->isModified() != aMainWindow->isModifiedState())
190 aMainWindow->setModifiedState(aMgr->isModified());
194 //******************************************************
195 void XGUI_WorkshopListener::onFeatureUpdatedMsg(const std::shared_ptr<ModelAPI_ObjectUpdatedMessage>& theMsg)
197 std::set<ObjectPtr> aFeatures = theMsg->objects();
198 XGUI_OperationMgr* anOperationMgr = workshop()->operationMgr();
199 if (anOperationMgr->hasOperation()) {
200 FeaturePtr aCurrentFeature = anOperationMgr->currentOperation()->feature();
201 std::set<ObjectPtr>::const_iterator aIt;
202 for (aIt = aFeatures.begin(); aIt != aFeatures.end(); ++aIt) {
203 ObjectPtr aNewFeature = (*aIt);
204 if (aNewFeature == aCurrentFeature) {
205 workshop()->propertyPanel()->updateContentWidget(aCurrentFeature);
210 anOperationMgr->onValidateOperation();
211 //if (myObjectBrowser)
212 // myObjectBrowser->processEvent(theMsg);
215 //******************************************************
216 void XGUI_WorkshopListener::onFeatureRedisplayMsg(const std::shared_ptr<ModelAPI_ObjectUpdatedMessage>& theMsg)
218 std::set<ObjectPtr> aObjects = theMsg->objects();
219 std::set<ObjectPtr>::const_iterator aIt;
221 #ifdef DEBUG_FEATURE_REDISPLAY
223 for (aIt = aObjects.begin(); aIt != aObjects.end(); ++aIt) {
224 anInfo.append(ModuleBase_Tools::objectInfo((*aIt)));
226 QString anInfoStr = anInfo.join(", ");
227 qDebug(QString("onFeatureRedisplayMsg: %1, %2").arg(aObjects.size()).arg(anInfoStr).toStdString().c_str());
230 XGUI_Workshop* aWorkshop = workshop();
231 XGUI_Displayer* aDisplayer = aWorkshop->displayer();
232 for (aIt = aObjects.begin(); aIt != aObjects.end(); ++aIt) {
233 ObjectPtr aObj = (*aIt);
235 // Hide the object if it is invalid or concealed one
236 bool aHide = !aObj->data() || !aObj->data()->isValid() ||
237 aObj->isDisabled() || (!aObj->isDisplayed());
238 if (!aHide) { // check that this is not hidden result
239 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(aObj);
240 aHide = aRes && aRes->isConcealed();
243 aDisplayer->erase(aObj, false);
245 // Redisplay the visible object or the object of the current operation
246 bool isVisibleObject = aDisplayer->isVisible(aObj);
247 #ifdef DEBUG_FEATURE_REDISPLAY
248 QString anObjInfo = ModuleBase_Tools::objectInfo((aObj));
249 qDebug(QString("visible=%1 : display= %2").arg(isVisibleObject).arg(anObjInfo).toStdString().c_str());
251 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
252 if (aFeature.get()) {
253 std::string aKind = aFeature->getKind();
254 if (aKind == "SketchMultiRotation")
259 if (isVisibleObject) { // redisplay visible object
260 //displayObject(aObj); // In order to update presentation
261 // in order to avoid the check whether the object can be redisplayed, the exact method
262 // of redisplay is called. This modification is made in order to have the line is updated
263 // by creation of a horizontal constraint on the line by preselection
264 aDisplayer->redisplay(aObj, false);
265 // Deactivate object of current operation from selection
266 aWorkshop->deactivateActiveObject(aObj, false);
267 } else { // display object if the current operation has it
268 if (displayObject(aObj)) {
269 // Deactivate object of current operation from selection
270 aWorkshop->deactivateActiveObject(aObj, false);
275 aDisplayer->updateViewer();
277 //******************************************************
278 void XGUI_WorkshopListener::onFeatureCreatedMsg(const std::shared_ptr<ModelAPI_ObjectUpdatedMessage>& theMsg)
280 std::set<ObjectPtr> aObjects = theMsg->objects();
281 std::set<ObjectPtr>::const_iterator aIt;
282 #ifdef DEBUG_FEATURE_CREATED
284 for (aIt = aObjects.begin(); aIt != aObjects.end(); ++aIt) {
285 anInfo.append(ModuleBase_Tools::objectInfo((*aIt)));
287 QString anInfoStr = anInfo.join(", ");
288 qDebug(QString("onFeatureCreatedMsg: %1, %2").arg(aObjects.size()).arg(anInfoStr).toStdString().c_str());
291 //bool aHasPart = false;
292 bool isDisplayed = false;
293 for (aIt = aObjects.begin(); aIt != aObjects.end(); ++aIt) {
294 ObjectPtr anObject = *aIt;
295 // the validity of the data should be checked here in order to avoid display of the objects,
296 // which were created, then deleted, but flush for the creation event happens after that
297 // we should not display disabled objects
298 bool aHide = !anObject->data()->isValid() ||
299 anObject->isDisabled() ||
300 !anObject->isDisplayed();
302 // setDisplayed has to be called in order to synchronize internal state of the object
303 // with list of displayed objects
304 if (myWorkshop->module()->canDisplayObject(anObject)) {
305 anObject->setDisplayed(true);
306 isDisplayed = displayObject(*aIt);
308 anObject->setDisplayed(false);
311 //if (myObjectBrowser)
312 // myObjectBrowser->processEvent(theMsg);
314 workshop()->displayer()->updateViewer();
315 //if (aHasPart) { // TODO: Avoid activate last part on loading of document
316 // activateLastPart();
320 bool XGUI_WorkshopListener::event(QEvent * theEvent)
322 PostponeMessageQtEvent* aPostponedEv = dynamic_cast<PostponeMessageQtEvent*>(theEvent);
324 std::shared_ptr<Events_Message> aEventPtr = aPostponedEv->postponedMessage();
325 processEvent(aEventPtr);
331 void XGUI_WorkshopListener::addFeature(const std::shared_ptr<Config_FeatureMessage>& theMessage)
335 qDebug() << "XGUI_WorkshopListener::addFeature: NULL message.";
339 ActionInfo aFeatureInfo;
340 aFeatureInfo.initFrom(theMessage);
342 XGUI_Workshop* aWorkshop = workshop();
344 QString aWchName = QString::fromStdString(theMessage->workbenchId());
345 QStringList aNestedFeatures =
346 QString::fromStdString(theMessage->nestedFeatures()).split(" ", QString::SkipEmptyParts);
347 QString aDocKind = QString::fromStdString(theMessage->documentKind());
348 QList<QAction*> aNestedActList;
349 bool isColumnButton = !aNestedFeatures.isEmpty();
350 if (isColumnButton) {
351 QString aNestedActions = QString::fromStdString(theMessage->actionsWhenNested());
352 XGUI_OperationMgr* anOperationMgr = aWorkshop->operationMgr();
353 XGUI_ActionsMgr* anActionsMgr = aWorkshop->actionsMgr();
354 if (aNestedActions.contains("accept")) {
355 QAction* anAction = anActionsMgr->operationStateAction(XGUI_ActionsMgr::AcceptAll, NULL);
356 connect(anAction, SIGNAL(triggered()), anOperationMgr, SLOT(commitAllOperations()));
357 aNestedActList << anAction;
359 if (aNestedActions.contains("abort")) {
360 QAction* anAction = anActionsMgr->operationStateAction(XGUI_ActionsMgr::AbortAll, NULL);
361 connect(anAction, SIGNAL(triggered()), anOperationMgr, SLOT(abortAllOperations()));
362 aNestedActList << anAction;
366 if (aWorkshop->isSalomeMode()) {
367 XGUI_SalomeConnector* aSalomeConnector = aWorkshop->salomeConnector();
369 if (isColumnButton) {
370 aAction = aSalomeConnector->addNestedFeature(aWchName, aFeatureInfo, aNestedActList);
372 aAction = aSalomeConnector->addFeature(aWchName, aFeatureInfo);
374 aSalomeConnector->setNestedActions(aFeatureInfo.id, aNestedFeatures);
375 aSalomeConnector->setDocumentKind(aFeatureInfo.id, aDocKind);
377 aWorkshop->actionsMgr()->addCommand(aAction);
378 aWorkshop->module()->actionCreated(aAction);
380 //Find or create Workbench
381 AppElements_MainMenu* aMenuBar = aWorkshop->mainWindow()->menuObject();
382 AppElements_Workbench* aPage = aMenuBar->findWorkbench(aWchName);
384 aPage = aWorkshop->addWorkbench(aWchName);
386 //Find or create Group
387 QString aGroupName = QString::fromStdString(theMessage->groupId());
388 AppElements_MenuGroupPanel* aGroup = aPage->findGroup(aGroupName);
390 aGroup = aPage->addGroup(aGroupName);
392 // Check if hotkey sequence is already defined:
393 XGUI_ActionsMgr* anActionsMgr = aWorkshop->actionsMgr();
394 QKeySequence aHotKey = anActionsMgr->registerShortcut(aFeatureInfo.shortcut);
395 if(aHotKey != aFeatureInfo.shortcut) {
396 aFeatureInfo.shortcut = aHotKey;
399 AppElements_Command* aCommand = aGroup->addFeature(aFeatureInfo,
402 // Enrich created button with accept/abort buttons if necessary
403 AppElements_Button* aButton = aCommand->button();
404 if (aButton->isColumnButton()) {
405 aButton->setAdditionalButtons(aNestedActList);
407 aWorkshop->actionsMgr()->addCommand(aCommand);
408 aWorkshop->module()->actionCreated(aCommand);
413 //**************************************************************
414 bool XGUI_WorkshopListener::displayObject(ObjectPtr theObj)
416 XGUI_Workshop* aWorkshop = workshop();
417 if (!aWorkshop->module()->canDisplayObject(theObj))
420 XGUI_Displayer* aDisplayer = aWorkshop->displayer();
421 ResultBodyPtr aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(theObj);
422 if (aBody.get() != NULL) {
423 int aNb = aDisplayer->objectsCount();
424 aDisplayer->display(theObj, false);
426 myWorkshop->viewer()->fitAll();
428 aDisplayer->display(theObj, false);
433 XGUI_Workshop* XGUI_WorkshopListener::workshop() const
435 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
436 return aConnector->workshop();