1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
3 //#include "XGUI_Constants.h"
4 #include "XGUI_Tools.h"
5 #include "XGUI_Workshop.h"
6 #include "XGUI_SelectionMgr.h"
7 #include "XGUI_Selection.h"
8 #include "XGUI_ObjectsBrowser.h"
9 #include "XGUI_Displayer.h"
10 #include "XGUI_OperationMgr.h"
11 #include "XGUI_SalomeConnector.h"
12 #include "XGUI_ActionsMgr.h"
13 #include "XGUI_ErrorDialog.h"
14 #include "XGUI_ColorDialog.h"
15 #include "XGUI_ViewerProxy.h"
16 #include "XGUI_PropertyPanel.h"
17 #include "XGUI_ContextMenuMgr.h"
18 #include "XGUI_ModuleConnector.h"
19 #include <XGUI_QtEvents.h>
20 #include <XGUI_HistoryMenu.h>
22 #include <AppElements_Workbench.h>
23 #include <AppElements_Viewer.h>
24 #include <AppElements_Command.h>
25 #include <AppElements_MainMenu.h>
26 #include <AppElements_MainWindow.h>
27 #include <AppElements_MenuGroupPanel.h>
28 #include <AppElements_Button.h>
30 #include <ModuleBase_IModule.h>
31 #include <ModuleBase_Preferences.h>
33 #include <ModelAPI_Events.h>
34 #include <ModelAPI_Session.h>
35 #include <ModelAPI_Feature.h>
36 #include <ModelAPI_Data.h>
37 #include <ModelAPI_AttributeDocRef.h>
38 #include <ModelAPI_Object.h>
39 #include <ModelAPI_Validator.h>
40 #include <ModelAPI_ResultGroup.h>
41 #include <ModelAPI_ResultConstruction.h>
42 #include <ModelAPI_ResultBody.h>
43 #include <ModelAPI_AttributeIntArray.h>
44 #include <ModelAPI_ResultParameter.h>
46 //#include <PartSetPlugin_Part.h>
48 #include <Events_Loop.h>
49 #include <Events_Error.h>
50 #include <Events_LongOp.h>
52 #include <ModuleBase_Operation.h>
53 #include <ModuleBase_Operation.h>
54 #include <ModuleBase_OperationDescription.h>
55 #include <ModuleBase_SelectionValidator.h>
56 #include <ModuleBase_WidgetFactory.h>
57 #include <ModuleBase_Tools.h>
58 #include <ModuleBase_IViewer.h>
59 #include <ModuleBase_FilterFactory.h>
60 #include <ModuleBase_PageBase.h>
61 #include <ModuleBase_Tools.h>
63 #include <Config_Common.h>
64 #include <Config_FeatureMessage.h>
65 #include <Config_PointerMessage.h>
66 #include <Config_ModuleReader.h>
67 #include <Config_PropManager.h>
68 #include <Config_SelectionFilterMessage.h>
70 #include <QApplication>
71 #include <QFileDialog>
72 #include <QMessageBox>
73 #include <QMdiSubWindow>
74 #include <QPushButton>
75 #include <QDockWidget>
80 #include <QToolButton>
94 //#define DEBUG_FEATURE_CREATED
95 //#define DEBUG_FEATURE_REDISPLAY
98 XGUI_Workshop::XGUI_Workshop(XGUI_SalomeConnector* theConnector)
100 myCurrentDir(QString()),
102 mySalomeConnector(theConnector),
106 myUpdatePrefs(false),
107 myPartActivating(false),
108 myIsLoadingData(false)
110 myMainWindow = mySalomeConnector ? 0 : new AppElements_MainWindow();
112 myDisplayer = new XGUI_Displayer(this);
114 mySelector = new XGUI_SelectionMgr(this);
115 //connect(mySelector, SIGNAL(selectionChanged()), this, SLOT(updateModuleCommands()));
117 myOperationMgr = new XGUI_OperationMgr(this);
118 myActionsMgr = new XGUI_ActionsMgr(this);
119 myErrorDlg = new XGUI_ErrorDialog(myMainWindow);
120 myContextMenuMgr = new XGUI_ContextMenuMgr(this);
121 connect(myContextMenuMgr, SIGNAL(actionTriggered(const QString&, bool)), this,
122 SLOT(onContextMenuCommand(const QString&, bool)));
124 myViewerProxy = new XGUI_ViewerProxy(this);
125 connect(myViewerProxy, SIGNAL(selectionChanged()),
126 myActionsMgr, SLOT(updateOnViewSelection()));
128 myModuleConnector = new XGUI_ModuleConnector(this);
130 connect(myOperationMgr, SIGNAL(operationStarted(ModuleBase_Operation*)),
131 SLOT(onOperationStarted(ModuleBase_Operation*)));
132 connect(myOperationMgr, SIGNAL(operationResumed(ModuleBase_Operation*)),
133 SLOT(onOperationResumed(ModuleBase_Operation*)));
134 connect(myOperationMgr, SIGNAL(operationStopped(ModuleBase_Operation*)),
135 SLOT(onOperationStopped(ModuleBase_Operation*)));
136 connect(myOperationMgr, SIGNAL(operationCommitted(ModuleBase_Operation*)),
137 SLOT(onOperationCommitted(ModuleBase_Operation*)));
138 connect(myOperationMgr, SIGNAL(operationAborted(ModuleBase_Operation*)),
139 SLOT(onOperationAborted(ModuleBase_Operation*)));
140 connect(myMainWindow, SIGNAL(exitKeySequence()), SLOT(onExit()));
141 connect(this, SIGNAL(errorOccurred(const QString&)), myErrorDlg, SLOT(addError(const QString&)));
144 //******************************************************
145 XGUI_Workshop::~XGUI_Workshop(void)
150 //******************************************************
151 void XGUI_Workshop::startApplication()
155 Config_PropManager::registerProp("Plugins", "default_path", "Default Path",
156 Config_Prop::Directory, "");
158 //Initialize event listening
159 Events_Loop* aLoop = Events_Loop::loop();
160 aLoop->registerListener(this, Events_Error::errorID()); //!< Listening application errors.
161 aLoop->registerListener(this, Events_Loop::eventByName(Config_FeatureMessage::GUI_EVENT()));
162 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OPERATION_LAUNCHED));
163 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
164 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_CREATED));
165 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
166 aLoop->registerListener(this, Events_LongOp::eventID());
167 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_PLUGIN_LOADED));
168 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_TOSHOW));
169 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_TOHIDE));
170 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_SELFILTER_LOADED));
172 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_UPDATE_VIEWER_BLOCKED));
173 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_UPDATE_VIEWER_UNBLOCKED));
175 registerValidators();
177 // Calling of loadCustomProps before activating module is required
178 // by Config_PropManger to restore user-defined path to plugins
179 ModuleBase_Preferences::loadCustomProps();
182 myMainWindow->show();
183 updateCommandStatus();
188 emit applicationStarted();
191 //******************************************************
192 void XGUI_Workshop::initMenu()
194 myContextMenuMgr->createActions();
196 if (isSalomeMode()) {
197 // Create only Undo, Redo commands
198 QAction* aAction = salomeConnector()->addDesktopCommand("UNDO_CMD", tr("Undo"),
199 tr("Undo last command"),
200 QIcon(":pictures/undo.png"),
201 QKeySequence::Undo, false, "MEN_DESK_EDIT");
202 connect(aAction, SIGNAL(triggered(bool)), this, SLOT(onUndo()));
203 addHistoryMenu(aAction, SIGNAL(updateUndoHistory(const QList<ActionInfo>&)), SLOT(onUndo(int)));
205 aAction = salomeConnector()->addDesktopCommand("REDO_CMD", tr("Redo"), tr("Redo last command"),
206 QIcon(":pictures/redo.png"), QKeySequence::Redo,
207 false, "MEN_DESK_EDIT");
208 connect(aAction, SIGNAL(triggered(bool)), this, SLOT(onRedo()));
209 addHistoryMenu(aAction, SIGNAL(updateRedoHistory(const QList<ActionInfo>&)), SLOT(onRedo(int)));
211 salomeConnector()->addDesktopMenuSeparator("MEN_DESK_EDIT");
212 aAction = salomeConnector()->addDesktopCommand("REBUILD_CMD", tr("Rebuild"), tr("Rebuild data objects"),
213 QIcon(":pictures/rebuild.png"), QKeySequence(),
214 false, "MEN_DESK_EDIT");
215 connect(aAction, SIGNAL(triggered(bool)), this, SLOT(onRebuild()));
216 salomeConnector()->addDesktopMenuSeparator("MEN_DESK_EDIT");
218 aAction = salomeConnector()->addDesktopCommand("SAVEAS_CMD", tr("Export NewGeom..."), tr("Export the current document into a NewGeom file"),
219 QIcon(), QKeySequence(),
220 false, "MEN_DESK_FILE");
221 connect(aAction, SIGNAL(triggered(bool)), this, SLOT(onSaveAs()));
223 aAction = salomeConnector()->addDesktopCommand("OPEN_CMD", tr("Import NewGeom..."), tr("Import a NewGeom file"),
224 QIcon(), QKeySequence(),
225 false, "MEN_DESK_FILE");
226 connect(aAction, SIGNAL(triggered(bool)), this, SLOT(onOpen()));
227 salomeConnector()->addDesktopMenuSeparator("MEN_DESK_FILE");
231 // File commands group
232 AppElements_MenuGroupPanel* aGroup = myMainWindow->menuObject()->generalPage();
234 AppElements_Command* aCommand;
236 aCommand = aGroup->addFeature("SAVE_CMD", tr("Save..."), tr("Save the document"),
237 QIcon(":pictures/save.png"), QKeySequence::Save);
238 aCommand->connectTo(this, SLOT(onSave()));
239 //aCommand->disable();
241 QString aUndoId = "UNDO_CMD";
242 aCommand = aGroup->addFeature(aUndoId, tr("Undo"), tr("Undo last command"),
243 QIcon(":pictures/undo.png"), QKeySequence::Undo);
244 aCommand->connectTo(this, SLOT(onUndo()));
245 AppElements_Button* aUndoButton = qobject_cast<AppElements_Button*>(aGroup->widget(aUndoId));
246 addHistoryMenu(aUndoButton,
247 SIGNAL(updateUndoHistory(const QList<ActionInfo>&)),
250 QString aRedoId = "REDO_CMD";
251 aCommand = aGroup->addFeature(aRedoId, tr("Redo"), tr("Redo last command"),
252 QIcon(":pictures/redo.png"), QKeySequence::Redo);
253 aCommand->connectTo(this, SLOT(onRedo()));
254 AppElements_Button* aRedoButton = qobject_cast<AppElements_Button*>(aGroup->widget(aRedoId));
255 addHistoryMenu(aRedoButton,
256 SIGNAL(updateRedoHistory(const QList<ActionInfo>&)),
259 aCommand = aGroup->addFeature("REBUILD_CMD", tr("Rebuild"), tr("Rebuild data objects"),
260 QIcon(":pictures/rebuild.png"), QKeySequence());
261 aCommand->connectTo(this, SLOT(onRebuild()));
263 aCommand = aGroup->addFeature("SAVEAS_CMD", tr("Save as..."), tr("Save the document into a file"),
264 QIcon(":pictures/save.png"), QKeySequence());
265 aCommand->connectTo(this, SLOT(onSaveAs()));
266 //aCommand->disable();
268 aCommand = aGroup->addFeature("OPEN_CMD", tr("Open..."), tr("Open a new document"),
269 QIcon(":pictures/open.png"), QKeySequence::Open);
270 aCommand->connectTo(this, SLOT(onOpen()));
272 //aCommand = aGroup->addFeature("NEW_CMD", tr("New"), tr("Create a new document"),
273 // QIcon(":pictures/new.png"), QKeySequence::New);
274 //aCommand->connectTo(this, SLOT(onNew()));
276 aCommand = aGroup->addFeature("PREF_CMD", tr("Preferences"), tr("Edit preferences"),
277 QIcon(":pictures/preferences.png"), QKeySequence::Preferences);
278 aCommand->connectTo(this, SLOT(onPreferences()));
280 aCommand = aGroup->addFeature("EXIT_CMD", tr("Exit"), tr("Exit application"),
281 QIcon(":pictures/close.png"), QKeySequence::Close);
282 aCommand->connectTo(this, SLOT(onExit()));
283 //FIXME: SBH's test action. Can be used for some GUI tests.
285 // aCommand = aGroup->addFeature("TEST_CMD", "Test!", "Private debug button",
286 // QIcon(":pictures/close.png"), QKeySequence(), true);
287 // aCommand->connectTo(myMainWindow, SLOT(dockPythonConsole()));
291 //******************************************************
292 AppElements_Workbench* XGUI_Workshop::addWorkbench(const QString& theName)
294 AppElements_MainMenu* aMenuBar = myMainWindow->menuObject();
295 return aMenuBar->addWorkbench(theName);
298 //******************************************************
299 void XGUI_Workshop::processEvent(const std::shared_ptr<Events_Message>& theMessage)
301 if (QApplication::instance()->thread() != QThread::currentThread()) {
303 std::cout << "XGUI_Workshop::processEvent: " << "Working in another thread." << std::endl;
305 SessionPtr aMgr = ModelAPI_Session::get();
306 PostponeMessageQtEvent* aPostponeEvent = new PostponeMessageQtEvent(theMessage);
307 QApplication::postEvent(this, aPostponeEvent);
311 //A message to start feature creation received.
312 if (theMessage->eventID() == Events_Loop::loop()->eventByName(Config_FeatureMessage::GUI_EVENT())) {
313 std::shared_ptr<Config_FeatureMessage> aFeatureMsg =
314 std::dynamic_pointer_cast<Config_FeatureMessage>(theMessage);
315 if (!aFeatureMsg->isInternal()) {
316 addFeature(aFeatureMsg);
319 // Process creation of Part
320 else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_CREATED)) {
321 std::shared_ptr<ModelAPI_ObjectUpdatedMessage> aUpdMsg =
322 std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
323 onFeatureCreatedMsg(aUpdMsg);
325 if (mySalomeConnector)
326 mySalomeConnector->createPreferences();
327 myUpdatePrefs = false;
330 else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_PLUGIN_LOADED)) {
331 myUpdatePrefs = true;
334 else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY)) {
335 std::shared_ptr<ModelAPI_ObjectUpdatedMessage> aUpdMsg =
336 std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
337 onFeatureRedisplayMsg(aUpdMsg);
339 //Update property panel on corresponding message. If there is no current operation (no
340 //property panel), or received message has different feature to the current - do nothing.
341 else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_UPDATED)) {
342 std::shared_ptr<ModelAPI_ObjectUpdatedMessage> anUpdateMsg =
343 std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
344 onFeatureUpdatedMsg(anUpdateMsg);
345 } else if (theMessage->eventID() == Events_LongOp::eventID()) {
346 if (Events_LongOp::isPerformed()) {
347 QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
349 QApplication::restoreOverrideCursor();
351 } else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_TOSHOW)) {
352 std::shared_ptr<ModelAPI_ObjectUpdatedMessage> anUpdateMsg =
353 std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
354 const std::set<ObjectPtr>& aObjList = anUpdateMsg->objects();
355 QObjectPtrList aList;
356 std::set<ObjectPtr>::const_iterator aIt;
357 for (aIt = aObjList.cbegin(); aIt != aObjList.cend(); ++aIt)
359 showObjects(aList, true);
360 } else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_TOHIDE)) {
361 std::shared_ptr<ModelAPI_ObjectUpdatedMessage> anUpdateMsg =
362 std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
363 const std::set<ObjectPtr>& aObjList = anUpdateMsg->objects();
364 QObjectPtrList aList;
365 std::set<ObjectPtr>::const_iterator aIt;
366 for (aIt = aObjList.cbegin(); aIt != aObjList.cend(); ++aIt)
368 showObjects(aList, false);
370 //An operation passed by message. Start it, process and commit.
371 else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OPERATION_LAUNCHED)) {
372 std::shared_ptr<Config_PointerMessage> aPartSetMsg =
373 std::dynamic_pointer_cast<Config_PointerMessage>(theMessage);
374 //myPropertyPanel->cleanContent();
375 ModuleBase_Operation* anOperation = (ModuleBase_Operation*) aPartSetMsg->pointer();
377 if (myOperationMgr->startOperation(anOperation)) {
378 myPropertyPanel->updateContentWidget(anOperation->feature());
379 if (!anOperation->getDescription()->hasXmlRepresentation()) {
380 if (anOperation->commit())
381 updateCommandStatus();
385 else if (theMessage->eventID() == Events_Loop::eventByName(EVENT_SELFILTER_LOADED)) {
386 std::shared_ptr<Config_SelectionFilterMessage> aMsg =
387 std::dynamic_pointer_cast<Config_SelectionFilterMessage>(theMessage);
389 ModuleBase_FilterFactory* aFactory = moduleConnector()->selectionFilters();
390 if (!aMsg->attributeId().empty()) {
391 aFactory->assignFilter(aMsg->selectionFilterId(), aMsg->featureId(), aMsg->attributeId(),
395 } else if (theMessage->eventID() == Events_Loop::eventByName(EVENT_UPDATE_VIEWER_BLOCKED)) {
396 // the viewer's update context will not happens until viewer updated is emitted
397 myDisplayer->enableUpdateViewer(false);
398 } else if (theMessage->eventID() == Events_Loop::eventByName(EVENT_UPDATE_VIEWER_UNBLOCKED)) {
399 // the viewer's update context is unblocked, the viewer's update works
400 myDisplayer->enableUpdateViewer(true);
402 //Show error dialog if error message received.
403 std::shared_ptr<Events_Error> anAppError = std::dynamic_pointer_cast<Events_Error>(theMessage);
405 emit errorOccurred(QString::fromLatin1(anAppError->description()));
409 if (!isSalomeMode()) {
410 SessionPtr aMgr = ModelAPI_Session::get();
411 if (aMgr->isModified() != myMainWindow->isModifiedState())
412 myMainWindow->setModifiedState(aMgr->isModified());
416 //******************************************************
417 QMainWindow* XGUI_Workshop::desktop() const
419 return isSalomeMode() ? salomeConnector()->desktop() : myMainWindow;
422 //******************************************************
423 void XGUI_Workshop::onStartWaiting()
425 if (Events_LongOp::isPerformed()) {
426 QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
430 //******************************************************
431 void XGUI_Workshop::onFeatureUpdatedMsg(const std::shared_ptr<ModelAPI_ObjectUpdatedMessage>& theMsg)
433 std::set<ObjectPtr> aFeatures = theMsg->objects();
434 if (myOperationMgr->hasOperation()) {
435 FeaturePtr aCurrentFeature = myOperationMgr->currentOperation()->feature();
436 std::set<ObjectPtr>::const_iterator aIt;
437 for (aIt = aFeatures.begin(); aIt != aFeatures.end(); ++aIt) {
438 ObjectPtr aNewFeature = (*aIt);
439 if (aNewFeature == aCurrentFeature) {
440 myPropertyPanel->updateContentWidget(aCurrentFeature);
445 myOperationMgr->onValidateOperation();
446 //if (myObjectBrowser)
447 // myObjectBrowser->processEvent(theMsg);
450 //******************************************************
451 void XGUI_Workshop::onFeatureRedisplayMsg(const std::shared_ptr<ModelAPI_ObjectUpdatedMessage>& theMsg)
453 std::set<ObjectPtr> aObjects = theMsg->objects();
454 std::set<ObjectPtr>::const_iterator aIt;
456 #ifdef DEBUG_FEATURE_REDISPLAY
458 for (aIt = aObjects.begin(); aIt != aObjects.end(); ++aIt) {
459 anInfo.append(ModuleBase_Tools::objectInfo((*aIt)));
461 QString anInfoStr = anInfo.join(", ");
462 qDebug(QString("onFeatureRedisplayMsg: %1, %2").arg(aObjects.size()).arg(anInfoStr).toStdString().c_str());
465 for (aIt = aObjects.begin(); aIt != aObjects.end(); ++aIt) {
466 ObjectPtr aObj = (*aIt);
468 // Hide the object if it is invalid or concealed one
469 bool aHide = !aObj->data() || !aObj->data()->isValid();
470 if (!aHide) { // check that this is not hidden result
471 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(aObj);
472 aHide = aRes && aRes->isConcealed();
475 myDisplayer->erase(aObj, false);
477 // Redisplay the visible object or the object of the current operation
478 bool isVisibleObject = myDisplayer->isVisible(aObj);
479 #ifdef DEBUG_FEATURE_REDISPLAY
480 //QString anObjInfo = ModuleBase_Tools::objectInfo((aObj));
481 //qDebug(QString("visible=%1 : display= %2").arg(isVisibleObject).arg(anObjInfo).toStdString().c_str());
484 if (isVisibleObject) { // redisplay visible object
485 //displayObject(aObj); // In order to update presentation
486 // in order to avoid the check whether the object can be redisplayed, the exact method
487 // of redisplay is called. This modification is made in order to have the line is updated
488 // by creation of a horizontal constraint on the line by preselection
489 myDisplayer->redisplay(aObj, false);
490 if (myOperationMgr->hasOperation()) {
491 ModuleBase_Operation* aOperation = myOperationMgr->currentOperation();
492 if (!aOperation->isEditOperation() &&
493 aOperation->hasObject(aObj) && myDisplayer->isActive(aObj))
494 myDisplayer->deactivate(aObj);
496 } else { // display object if the current operation has it
497 ModuleBase_Operation* aOperation = myOperationMgr->currentOperation();
498 if (aOperation && aOperation->hasObject(aObj)) {
499 ModuleBase_Operation* aOperation = myOperationMgr->currentOperation();
500 #ifdef DEBUG_FEATURE_REDISPLAY
501 QString anObjInfo = ModuleBase_Tools::objectInfo((aObj));
502 qDebug(QString(" display object = %1").arg(anObjInfo).toStdString().c_str());
504 if (displayObject(aObj)) {
505 // Deactivate object of current operation from selection
506 if (myDisplayer->isActive(aObj))
507 myDisplayer->deactivate(aObj);
513 myDisplayer->updateViewer();
516 //******************************************************
517 void XGUI_Workshop::onFeatureCreatedMsg(const std::shared_ptr<ModelAPI_ObjectUpdatedMessage>& theMsg)
519 std::set<ObjectPtr> aObjects = theMsg->objects();
520 std::set<ObjectPtr>::const_iterator aIt;
521 #ifdef DEBUG_FEATURE_CREATED
523 for (aIt = aObjects.begin(); aIt != aObjects.end(); ++aIt) {
524 anInfo.append(ModuleBase_Tools::objectInfo((*aIt)));
526 QString anInfoStr = anInfo.join(", ");
527 qDebug(QString("onFeatureCreatedMsg: %1, %2").arg(aObjects.size()).arg(anInfoStr).toStdString().c_str());
530 //bool aHasPart = false;
531 bool isDisplayed = false;
532 for (aIt = aObjects.begin(); aIt != aObjects.end(); ++aIt) {
533 ObjectPtr anObject = *aIt;
534 // the validity of the data should be checked here in order to avoid display of the objects,
535 // which were created, then deleted, but flush for the creation event happens after that
536 if (!anObject->data() || !anObject->data()->isValid())
538 //ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aIt);
541 // If a feature is created from the aplication's python console
542 // it doesn't stored in the operation mgr and doesn't displayed
544 isDisplayed = displayObject(*aIt);
547 //if (myObjectBrowser)
548 // myObjectBrowser->processEvent(theMsg);
550 myDisplayer->updateViewer();
551 //if (aHasPart) { // TODO: Avoid activate last part on loading of document
552 // activateLastPart();
556 //******************************************************
557 void XGUI_Workshop::onOperationStarted(ModuleBase_Operation* theOperation)
559 setNestedFeatures(theOperation);
561 if (theOperation->getDescription()->hasXmlRepresentation()) { //!< No need for property panel
562 connectWithOperation(theOperation);
563 setPropertyPanel(theOperation);
565 updateCommandStatus();
567 myModule->operationStarted(theOperation);
570 //******************************************************
571 void XGUI_Workshop::onOperationResumed(ModuleBase_Operation* theOperation)
573 setNestedFeatures(theOperation);
575 if (theOperation->getDescription()->hasXmlRepresentation()) { //!< No need for property panel
576 // connectWithOperation(theOperation); already connected
577 setPropertyPanel(theOperation);
579 updateCommandStatus();
581 myModule->operationResumed(theOperation);
585 //******************************************************
586 void XGUI_Workshop::onOperationStopped(ModuleBase_Operation* theOperation)
588 ModuleBase_ISelection* aSel = mySelector->selection();
589 QObjectPtrList aObj = aSel->selectedPresentations();
590 //!< No need for property panel
591 updateCommandStatus();
593 myPropertyPanel->cleanContent();
595 // Activate objects created by current operation
596 // in order to clean selection modes
597 // the deactivation should be pefromed in the same place, where the mode is activated,
598 // e.g. activation in the current widget activation, deactivation - in the widget's deactivation
600 //myDisplayer->activateObjects(aModes);
601 myModule->operationStopped(theOperation);
603 // if the operation is nested, do not deactivate objects
604 //if (myOperationMgr->operationsCount() == 0) {
605 // Activate selection mode for all objects
607 // TODO: check on OCC_6.9.0
608 // the module current active modes should not be deactivated in order to save the objects selected
609 // the deactivate object in the mode of selection leads to the object is deselected in the viewer.
610 // But, in OCC_6.8.0 this deselection does not happened automatically. It is necessary to call
611 // ClearOutdatedSelection, but this method has an error in the realization, which should be fixed in
612 // the OCC_6.9.0 release. Moreother, it is possible that ClearOutdatedSelection will be called inside
613 // Deactivate method of AIS_InteractiveContext. In this case, we need not call it.
614 module()->activeSelectionModes(aModes);
615 myDisplayer->activateObjects(aModes);
620 void XGUI_Workshop::onOperationCommitted(ModuleBase_Operation* theOperation)
622 myModule->operationCommitted(theOperation);
625 void XGUI_Workshop::onOperationAborted(ModuleBase_Operation* theOperation)
627 myModule->operationAborted(theOperation);
630 void XGUI_Workshop::setNestedFeatures(ModuleBase_Operation* theOperation)
632 if (this->isSalomeMode())
633 theOperation->setNestedFeatures(mySalomeConnector->nestedActions(theOperation->id()));
635 theOperation->setNestedFeatures(myActionsMgr->nestedCommands(theOperation->id()));
638 void XGUI_Workshop::setPropertyPanel(ModuleBase_Operation* theOperation)
641 QString aXmlRepr = theOperation->getDescription()->xmlRepresentation();
642 ModuleBase_WidgetFactory aFactory = ModuleBase_WidgetFactory(aXmlRepr.toStdString(),
645 myPropertyPanel->cleanContent();
646 aFactory.createWidget(myPropertyPanel->contentWidget());
648 QList<ModuleBase_ModelWidget*> aWidgets = aFactory.getModelWidgets();
649 foreach (ModuleBase_ModelWidget* aWidget, aWidgets) {
650 bool isStoreValue = !theOperation->isEditOperation() &&
651 !aWidget->getDefaultValue().empty() &&
652 !aWidget->isComputedDefault();
653 aWidget->setFeature(theOperation->feature(), isStoreValue);
654 aWidget->enableFocusProcessing();
657 myPropertyPanel->setModelWidgets(aWidgets);
658 theOperation->setPropertyPanel(myPropertyPanel);
660 myModule->propertyPanelDefined(theOperation);
662 myPropertyPanel->setWindowTitle(theOperation->getDescription()->description());
665 bool XGUI_Workshop::event(QEvent * theEvent)
667 PostponeMessageQtEvent* aPostponedEv = dynamic_cast<PostponeMessageQtEvent*>(theEvent);
669 std::shared_ptr<Events_Message> aEventPtr = aPostponedEv->postponedMessage();
670 processEvent(aEventPtr);
679 void XGUI_Workshop::addFeature(const std::shared_ptr<Config_FeatureMessage>& theMessage)
683 qDebug() << "XGUI_Workshop::addFeature: NULL message.";
687 ActionInfo aFeatureInfo;
688 aFeatureInfo.initFrom(theMessage);
690 QString aWchName = QString::fromStdString(theMessage->workbenchId());
691 QStringList aNestedFeatures =
692 QString::fromStdString(theMessage->nestedFeatures()).split(" ", QString::SkipEmptyParts);
693 QString aDocKind = QString::fromStdString(theMessage->documentKind());
694 QList<QAction*> aNestedActList;
695 bool isColumnButton = !aNestedFeatures.isEmpty();
696 if (isColumnButton) {
697 QString aNestedActions = QString::fromStdString(theMessage->actionsWhenNested());
698 if (aNestedActions.contains("accept")) {
699 QAction* anAction = myActionsMgr->operationStateAction(XGUI_ActionsMgr::AcceptAll, NULL);
700 connect(anAction, SIGNAL(triggered()), myOperationMgr, SLOT(commitAllOperations()));
701 aNestedActList << anAction;
703 if (aNestedActions.contains("abort")) {
704 QAction* anAction = myActionsMgr->operationStateAction(XGUI_ActionsMgr::AbortAll, NULL);
705 connect(anAction, SIGNAL(triggered()), myOperationMgr, SLOT(abortAllOperations()));
706 aNestedActList << anAction;
710 if (isSalomeMode()) {
712 if (isColumnButton) {
713 aAction = salomeConnector()->addNestedFeature(aWchName, aFeatureInfo, aNestedActList);
715 aAction = salomeConnector()->addFeature(aWchName, aFeatureInfo);
717 salomeConnector()->setNestedActions(aFeatureInfo.id, aNestedFeatures);
718 salomeConnector()->setDocumentKind(aFeatureInfo.id, aDocKind);
720 myActionsMgr->addCommand(aAction);
721 myModule->actionCreated(aAction);
723 //Find or create Workbench
724 AppElements_MainMenu* aMenuBar = myMainWindow->menuObject();
725 AppElements_Workbench* aPage = aMenuBar->findWorkbench(aWchName);
727 aPage = addWorkbench(aWchName);
729 //Find or create Group
730 QString aGroupName = QString::fromStdString(theMessage->groupId());
731 AppElements_MenuGroupPanel* aGroup = aPage->findGroup(aGroupName);
733 aGroup = aPage->addGroup(aGroupName);
735 // Check if hotkey sequence is already defined:
736 QKeySequence aHotKey = myActionsMgr->registerShortcut(aFeatureInfo.shortcut);
737 if(aHotKey != aFeatureInfo.shortcut) {
738 aFeatureInfo.shortcut = aHotKey;
741 AppElements_Command* aCommand = aGroup->addFeature(aFeatureInfo,
744 // Enrich created button with accept/abort buttons if necessary
745 AppElements_Button* aButton = aCommand->button();
746 if (aButton->isColumnButton()) {
747 aButton->setAdditionalButtons(aNestedActList);
749 myActionsMgr->addCommand(aCommand);
750 myModule->actionCreated(aCommand);
755 * Makes a signal/slot connections between Property Panel
756 * and given operation. The given operation becomes a
757 * current operation and previous operation if exists
759 void XGUI_Workshop::connectWithOperation(ModuleBase_Operation* theOperation)
761 QAction* aCommand = 0;
762 if (isSalomeMode()) {
763 aCommand = salomeConnector()->command(theOperation->getDescription()->operationId());
765 AppElements_MainMenu* aMenu = myMainWindow->menuObject();
766 FeaturePtr aFeature = theOperation->feature();
768 aCommand = aMenu->feature(QString::fromStdString(aFeature->getKind()));
770 //Abort operation on uncheck the command
772 connect(aCommand, SIGNAL(triggered(bool)), theOperation, SLOT(setRunning(bool)));
777 * Saves document with given name.
779 void XGUI_Workshop::saveDocument(const QString& theName, std::list<std::string>& theFileNames)
781 QApplication::restoreOverrideCursor();
782 SessionPtr aMgr = ModelAPI_Session::get();
783 aMgr->save(theName.toLatin1().constData(), theFileNames);
784 QApplication::restoreOverrideCursor();
787 bool XGUI_Workshop::isActiveOperationAborted()
789 return myOperationMgr->abortAllOperations();
792 //******************************************************
793 void XGUI_Workshop::onExit()
795 SessionPtr aMgr = ModelAPI_Session::get();
796 if (aMgr->isModified()) {
797 int anAnswer = QMessageBox::question(
798 myMainWindow, tr("Save current file"), tr("The document is modified, save before exit?"),
799 QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel, QMessageBox::Cancel);
800 if (anAnswer == QMessageBox::Save) {
801 bool saved = onSave();
805 } else if (anAnswer == QMessageBox::Cancel) {
812 //******************************************************
813 void XGUI_Workshop::onNew()
815 QApplication::setOverrideCursor(Qt::WaitCursor);
816 if (objectBrowser() == 0) {
818 mySelector->connectViewers();
820 myViewerProxy->connectToViewer();
822 if (!isSalomeMode()) {
823 myMainWindow->showPythonConsole();
824 QMdiSubWindow* aWnd = myMainWindow->viewer()->createView();
825 aWnd->showMaximized();
826 updateCommandStatus();
828 myContextMenuMgr->connectViewer();
829 QApplication::restoreOverrideCursor();
832 //******************************************************
833 void XGUI_Workshop::onOpen()
835 if(!isActiveOperationAborted())
837 //save current file before close if modified
838 SessionPtr aSession = ModelAPI_Session::get();
839 if (aSession->isModified()) {
840 //TODO(sbh): re-launch the app?
841 int anAnswer = QMessageBox::question(
842 myMainWindow, tr("Save current file"),
843 tr("The document is modified, save before opening another?"),
844 QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel, QMessageBox::Cancel);
845 if (anAnswer == QMessageBox::Save) {
847 } else if (anAnswer == QMessageBox::Cancel) {
850 aSession->closeAll();
854 //show file dialog, check if readable and open
855 myCurrentDir = QFileDialog::getExistingDirectory(mainWindow());
856 if (myCurrentDir.isEmpty())
858 QFileInfo aFileInfo(myCurrentDir);
859 if (!aFileInfo.exists() || !aFileInfo.isReadable()) {
860 QMessageBox::critical(myMainWindow, tr("Warning"), tr("Unable to open the file."));
864 QApplication::setOverrideCursor(Qt::WaitCursor);
865 myIsLoadingData = true;
866 aSession->load(myCurrentDir.toLatin1().constData());
867 myObjectBrowser->rebuildDataTree();
869 updateCommandStatus();
870 myIsLoadingData = false;
871 QApplication::restoreOverrideCursor();
874 //******************************************************
875 bool XGUI_Workshop::onSave()
877 if(!isActiveOperationAborted())
879 if (myCurrentDir.isEmpty()) {
882 std::list<std::string> aFiles;
883 saveDocument(myCurrentDir, aFiles);
884 updateCommandStatus();
886 myMainWindow->setModifiedState(false);
890 //******************************************************
891 bool XGUI_Workshop::onSaveAs()
893 if(!isActiveOperationAborted())
895 QFileDialog dialog(mainWindow());
896 dialog.setWindowTitle(tr("Select directory to save files..."));
897 dialog.setFileMode(QFileDialog::Directory);
898 dialog.setFilter(tr("Folders (*)"));
899 dialog.setOptions(QFileDialog::HideNameFilterDetails | QFileDialog::ShowDirsOnly);
900 dialog.setViewMode(QFileDialog::Detail);
902 if (!dialog.exec()) {
905 QString aTempDir = dialog.selectedFiles().first();
907 if (aDir.exists() && !aDir.entryInfoList(QDir::NoDotAndDotDot | QDir::AllEntries).isEmpty()) {
908 int answer = QMessageBox::question(
910 //: Title of the dialog which asks user if he wants to save study in existing non-empty folder
912 tr("The folder already contains some files, save anyway?"),
913 QMessageBox::Save | QMessageBox::Cancel);
914 if (answer == QMessageBox::Cancel) {
918 myCurrentDir = aTempDir;
919 if (!isSalomeMode()) {
920 myMainWindow->setCurrentDir(myCurrentDir, false);
921 myMainWindow->setModifiedState(false);
926 //******************************************************
927 void XGUI_Workshop::onUndo(int theTimes)
929 objectBrowser()->treeView()->setCurrentIndex(QModelIndex());
930 SessionPtr aMgr = ModelAPI_Session::get();
931 if (aMgr->isOperation())
932 operationMgr()->onAbortOperation();
933 for (int i = 0; i < theTimes; ++i) {
936 updateCommandStatus();
939 //******************************************************
940 void XGUI_Workshop::onRedo(int theTimes)
942 // the viewer update should be blocked in order to avoid the features blinking. For the created
943 // feature a results are created, the flush of the created signal caused the viewer redisplay for
944 // each created result. After a redisplay signal is flushed. So, the viewer update is blocked until
945 // redo of all possible objects happens
946 bool isUpdateEnabled = myDisplayer->enableUpdateViewer(false);
948 objectBrowser()->treeView()->setCurrentIndex(QModelIndex());
949 SessionPtr aMgr = ModelAPI_Session::get();
950 if (aMgr->isOperation())
951 operationMgr()->onAbortOperation();
952 for (int i = 0; i < theTimes; ++i) {
955 updateCommandStatus();
957 // unblock the viewer update functionality and make update on purpose
958 myDisplayer->enableUpdateViewer(isUpdateEnabled);
959 myDisplayer->updateViewer();
962 //******************************************************
963 void XGUI_Workshop::onRebuild()
965 SessionPtr aMgr = ModelAPI_Session::get();
966 bool aWasOperation = aMgr->isOperation(); // keep this value
967 if (!aWasOperation) {
968 aMgr->startOperation("Rebuild");
970 static const Events_ID aRebuildEvent = Events_Loop::loop()->eventByName("Rebuild");
971 Events_Loop::loop()->send(std::shared_ptr<Events_Message>(
972 new Events_Message(aRebuildEvent, this)));
973 if (!aWasOperation) {
974 aMgr->finishOperation();
976 updateCommandStatus();
979 //******************************************************
980 void XGUI_Workshop::onPreferences()
982 ModuleBase_Prefs aModif;
983 ModuleBase_Preferences::editPreferences(aModif);
984 if (aModif.size() > 0) {
986 foreach (ModuleBase_Pref aPref, aModif)
988 aSection = aPref.first;
989 if (aSection == ModuleBase_Preferences::VIEWER_SECTION) {
991 myMainWindow->viewer()->updateFromResources();
992 } else if (aSection == ModuleBase_Preferences::MENU_SECTION) {
994 myMainWindow->menuObject()->updateFromResources();
1000 //******************************************************
1001 ModuleBase_IModule* XGUI_Workshop::loadModule(const QString& theModule)
1003 QString libName = QString::fromStdString(library(theModule.toStdString()));
1004 if (libName.isEmpty()) {
1005 qWarning(qPrintable(tr("Information about module \"%1\" doesn't exist.").arg(theModule)));
1010 CREATE_FUNC crtInst = 0;
1013 HINSTANCE modLib = ::LoadLibrary((LPTSTR) qPrintable(libName));
1017 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
1018 0, ::GetLastError(), 0, (LPTSTR) & lpMsgBuf, 0, 0);
1019 QString aMsg((char*) &lpMsgBuf);
1020 err = QString("Failed to load %1. %2").arg(libName).arg(aMsg);
1021 ::LocalFree(lpMsgBuf);
1023 crtInst = (CREATE_FUNC) ::GetProcAddress(modLib, CREATE_MODULE);
1027 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM
1028 | FORMAT_MESSAGE_IGNORE_INSERTS,
1029 0, ::GetLastError(), 0, (LPTSTR) & lpMsgBuf, 0, 0);
1030 QString aMsg((char*) &lpMsgBuf);
1031 err = QString("Failed to find %1 function. %2").arg( CREATE_MODULE).arg(aMsg);
1032 ::LocalFree(lpMsgBuf);
1036 void* modLib = dlopen( libName.toLatin1(), RTLD_LAZY | RTLD_GLOBAL );
1038 err = QString( "Can not load library %1. %2" ).arg( libName ).arg( dlerror() );
1040 crtInst = (CREATE_FUNC)dlsym( modLib, CREATE_MODULE );
1042 err = QString( "Failed to find function %1. %2" ).arg( CREATE_MODULE ).arg( dlerror() );
1047 ModuleBase_IModule* aModule = crtInst ? crtInst(myModuleConnector) : 0;
1049 if (!err.isEmpty()) {
1051 Events_Error::send(err.toStdString());
1053 qWarning(qPrintable(err));
1059 //******************************************************
1060 bool XGUI_Workshop::activateModule()
1062 Config_ModuleReader aModuleReader;
1063 QString moduleName = QString::fromStdString(aModuleReader.getModuleName());
1064 myModule = loadModule(moduleName);
1068 connect(myDisplayer, SIGNAL(objectDisplayed(ObjectPtr, AISObjectPtr)),
1069 myModule, SLOT(onObjectDisplayed(ObjectPtr, AISObjectPtr)));
1070 connect(myDisplayer, SIGNAL(beforeObjectErase(ObjectPtr, AISObjectPtr)),
1071 myModule, SLOT(onBeforeObjectErase(ObjectPtr, AISObjectPtr)));
1073 myModule->createFeatures();
1074 myActionsMgr->update();
1078 //******************************************************
1079 void XGUI_Workshop::updateCommandStatus()
1081 QList<QAction*> aCommands;
1082 if (isSalomeMode()) { // update commands in SALOME mode
1083 aCommands = salomeConnector()->commandList();
1085 AppElements_MainMenu* aMenuBar = myMainWindow->menuObject();
1086 foreach (AppElements_Command* aCmd, aMenuBar->features())
1087 aCommands.append(aCmd);
1089 SessionPtr aMgr = ModelAPI_Session::get();
1090 if (aMgr->hasModuleDocument()) {
1091 QAction *aUndoCmd, *aRedoCmd;
1092 foreach(QAction* aCmd, aCommands) {
1093 QString aId = aCmd->data().toString();
1094 if (aId == "UNDO_CMD")
1096 else if (aId == "REDO_CMD")
1099 // Enable all commands
1100 aCmd->setEnabled(true);
1103 aUndoCmd->setEnabled(myModule->canUndo());
1104 aRedoCmd->setEnabled(myModule->canRedo());
1107 foreach(QAction* aCmd, aCommands) {
1108 QString aId = aCmd->data().toString();
1109 if (aId == "NEW_CMD")
1110 aCmd->setEnabled(true);
1111 else if (aId == "EXIT_CMD")
1112 aCmd->setEnabled(true);
1114 aCmd->setEnabled(false);
1117 myActionsMgr->update();
1118 emit commandStatusUpdated();
1121 void XGUI_Workshop::updateHistory()
1123 std::list<std::string> aUndoList = ModelAPI_Session::get()->undoList();
1124 QList<ActionInfo> aUndoRes = processHistoryList(aUndoList);
1125 emit updateUndoHistory(aUndoRes);
1127 std::list<std::string> aRedoList = ModelAPI_Session::get()->redoList();
1128 QList<ActionInfo> aRedoRes = processHistoryList(aRedoList);
1129 emit updateRedoHistory(aRedoRes);
1132 //******************************************************
1133 QDockWidget* XGUI_Workshop::createObjectBrowser(QWidget* theParent)
1135 QDockWidget* aObjDock = new QDockWidget(theParent);
1136 aObjDock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea | Qt::BottomDockWidgetArea);
1137 aObjDock->setWindowTitle(tr("Object browser"));
1138 aObjDock->setStyleSheet(
1139 "::title { position: relative; padding-left: 5px; text-align: left center }");
1140 myObjectBrowser = new XGUI_ObjectsBrowser(aObjDock);
1141 myObjectBrowser->setDataModel(myModule->dataModel());
1142 myModule->customizeObjectBrowser(myObjectBrowser);
1143 aObjDock->setWidget(myObjectBrowser);
1145 myContextMenuMgr->connectObjectBrowser();
1149 //******************************************************
1151 * Creates dock widgets, places them in corresponding area
1152 * and tabifies if necessary.
1154 void XGUI_Workshop::createDockWidgets()
1156 QMainWindow* aDesktop = isSalomeMode() ? salomeConnector()->desktop() : myMainWindow;
1157 QDockWidget* aObjDock = createObjectBrowser(aDesktop);
1158 aDesktop->addDockWidget(Qt::LeftDockWidgetArea, aObjDock);
1159 myPropertyPanel = new XGUI_PropertyPanel(aDesktop);
1160 myPropertyPanel->setupActions(myActionsMgr);
1161 myPropertyPanel->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea | Qt::BottomDockWidgetArea);
1162 aDesktop->addDockWidget(Qt::LeftDockWidgetArea, myPropertyPanel);
1163 hidePropertyPanel(); ///<! Invisible by default
1164 hideObjectBrowser();
1165 aDesktop->tabifyDockWidget(aObjDock, myPropertyPanel);
1166 myPropertyPanel->installEventFilter(myOperationMgr);
1168 QAction* aOkAct = myActionsMgr->operationStateAction(XGUI_ActionsMgr::Accept);
1169 connect(aOkAct, SIGNAL(triggered()), myOperationMgr, SLOT(onCommitOperation()));
1170 QAction* aCancelAct = myActionsMgr->operationStateAction(XGUI_ActionsMgr::Abort);
1171 connect(aCancelAct, SIGNAL(triggered()), myOperationMgr, SLOT(onAbortOperation()));
1172 connect(myPropertyPanel, SIGNAL(noMoreWidgets()), myModule, SLOT(onNoMoreWidgets()));
1173 connect(myPropertyPanel, SIGNAL(keyReleased(QKeyEvent*)),
1174 myOperationMgr, SLOT(onKeyReleased(QKeyEvent*)));
1175 connect(myOperationMgr, SIGNAL(validationStateChanged(bool)),
1176 aOkAct, SLOT(setEnabled(bool)));
1177 QAction* aAcceptAllAct = myActionsMgr->operationStateAction(XGUI_ActionsMgr::AcceptAll);
1178 connect(myOperationMgr, SIGNAL(nestedStateChanged(bool)),
1179 aAcceptAllAct, SLOT(setEnabled(bool)));
1183 //******************************************************
1184 void XGUI_Workshop::showPropertyPanel()
1186 QAction* aViewAct = myPropertyPanel->toggleViewAction();
1187 ///<! Restore ability to close panel from the window's menu
1188 aViewAct->setEnabled(true);
1189 myPropertyPanel->show();
1190 myPropertyPanel->raise();
1193 //******************************************************
1194 void XGUI_Workshop::hidePropertyPanel()
1196 QAction* aViewAct = myPropertyPanel->toggleViewAction();
1197 ///<! Do not allow to show empty property panel
1198 aViewAct->setEnabled(false);
1199 myPropertyPanel->hide();
1202 //******************************************************
1203 void XGUI_Workshop::showObjectBrowser()
1205 myObjectBrowser->parentWidget()->show();
1208 //******************************************************
1209 void XGUI_Workshop::hideObjectBrowser()
1211 myObjectBrowser->parentWidget()->hide();
1214 //******************************************************
1215 void XGUI_Workshop::onFeatureTriggered()
1217 QAction* aCmd = dynamic_cast<QAction*>(sender());
1219 QString aId = salomeConnector()->commandId(aCmd);
1221 myModule->launchOperation(aId);
1225 //******************************************************
1226 void XGUI_Workshop::salomeViewerSelectionChanged()
1228 emit salomeViewerSelection();
1231 //**************************************************************
1232 ModuleBase_IViewer* XGUI_Workshop::salomeViewer() const
1234 return mySalomeConnector->viewer();
1237 //**************************************************************
1238 void XGUI_Workshop::onContextMenuCommand(const QString& theId, bool isChecked)
1240 QObjectPtrList aObjects = mySelector->selection()->selectedObjects();
1241 if (theId == "DELETE_CMD")
1243 else if (theId == "COLOR_CMD")
1244 changeColor(aObjects);
1245 else if (theId == "SHOW_CMD")
1246 showObjects(aObjects, true);
1247 else if (theId == "HIDE_CMD")
1248 showObjects(aObjects, false);
1249 else if (theId == "SHOW_ONLY_CMD")
1250 showOnlyObjects(aObjects);
1251 else if (theId == "SHADING_CMD")
1252 setDisplayMode(aObjects, XGUI_Displayer::Shading);
1253 else if (theId == "WIREFRAME_CMD")
1254 setDisplayMode(aObjects, XGUI_Displayer::Wireframe);
1255 else if (theId == "HIDEALL_CMD")
1256 myDisplayer->eraseAll();
1259 //**************************************************************
1260 void XGUI_Workshop::deleteObjects()
1262 ModuleBase_IModule* aModule = module();
1263 // 1. allow the module to delete objects, do nothing if it has succeed
1264 if (aModule->deleteObjects()) {
1265 updateCommandStatus();
1269 if (!isActiveOperationAborted())
1271 QObjectPtrList anObjects = mySelector->selection()->selectedObjects();
1272 bool hasResult = false;
1273 bool hasFeature = false;
1274 bool hasParameter = false;
1275 ModuleBase_Tools::checkObjects(anObjects, hasResult, hasFeature, hasParameter);
1276 if (!(hasFeature || hasParameter))
1279 // 1. start operation
1280 QString aDescription = contextMenuMgr()->action("DELETE_CMD")->text();
1281 aDescription += tr(" %1");
1282 QStringList aObjectNames;
1283 foreach (ObjectPtr aObj, anObjects) {
1284 if (!aObj->data().get())
1286 aObjectNames << QString::fromStdString(aObj->data()->name());
1288 aDescription = aDescription.arg(aObjectNames.join(", "));
1290 SessionPtr aMgr = ModelAPI_Session::get();
1291 aMgr->startOperation(aDescription.toStdString());
1292 // 2. close the documents of the removed parts if the result part is in a list of selected objects
1293 foreach (ObjectPtr aObj, anObjects)
1295 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObj);
1297 DocumentPtr aDoc = aObj->document();
1298 if (aDoc == aMgr->activeDocument()) {
1303 // 3. delete objects
1304 QMainWindow* aDesktop = isSalomeMode() ? salomeConnector()->desktop() : myMainWindow;
1305 std::set<FeaturePtr> anIgnoredFeatures;
1306 if (deleteFeatures(anObjects, anIgnoredFeatures, aDesktop, true)) {
1307 myDisplayer->updateViewer();
1308 aMgr->finishOperation();
1309 updateCommandStatus();
1312 aMgr->abortOperation();
1316 //**************************************************************
1317 bool XGUI_Workshop::deleteFeatures(const QObjectPtrList& theList,
1318 std::set<FeaturePtr> theIgnoredFeatures,
1320 const bool theAskAboutDeleteReferences)
1322 // 1. find all referenced features
1323 std::set<FeaturePtr> aRefFeatures;
1324 foreach (ObjectPtr aObj, theList) {
1325 FeaturePtr aFeature = ModelAPI_Feature::feature(aObj);
1326 if (aFeature.get() != NULL) {
1327 aObj->document()->refsToFeature(aFeature, aRefFeatures, false);
1330 // 2. warn about the references remove, break the delete operation if the user chose it
1331 if (theAskAboutDeleteReferences && !aRefFeatures.empty()) {
1332 QStringList aRefNames;
1333 std::set<FeaturePtr>::const_iterator anIt = aRefFeatures.begin(),
1334 aLast = aRefFeatures.end();
1335 for (; anIt != aLast; anIt++) {
1336 aRefNames.append((*anIt)->name().c_str());
1338 QString aNames = aRefNames.join(", ");
1340 QMessageBox::StandardButton aRes = QMessageBox::warning(
1341 theParent, tr("Delete features"),
1342 QString(tr("Selected features are used in the following features: %1.\
1343 These features will be deleted also. Would you like to continue?")).arg(aNames),
1344 QMessageBox::No | QMessageBox::Yes, QMessageBox::No);
1345 if (aRes != QMessageBox::Yes)
1349 // 3. remove referenced features
1350 std::set<FeaturePtr>::const_iterator anIt = aRefFeatures.begin(),
1351 aLast = aRefFeatures.end();
1352 for (; anIt != aLast; anIt++) {
1353 FeaturePtr aFeature = (*anIt);
1354 DocumentPtr aDoc = aFeature->document();
1355 if (theIgnoredFeatures.find(aFeature) == theIgnoredFeatures.end())
1356 aDoc->removeFeature(aFeature);
1359 // 4. remove the parameter features
1360 foreach (ObjectPtr aObj, theList) {
1361 FeaturePtr aFeature = ModelAPI_Feature::feature(aObj);
1363 DocumentPtr aDoc = aObj->document();
1364 if (theIgnoredFeatures.find(aFeature) == theIgnoredFeatures.end())
1365 aDoc->removeFeature(aFeature);
1371 bool hasResults(QObjectPtrList theObjects, const std::set<std::string>& theTypes)
1373 bool isFoundResultType = false;
1374 foreach(ObjectPtr anObj, theObjects)
1376 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObj);
1377 if (aResult.get() == NULL)
1380 isFoundResultType = theTypes.find(aResult->groupName()) != theTypes.end();
1381 if (isFoundResultType)
1384 return isFoundResultType;
1387 //**************************************************************
1388 bool XGUI_Workshop::canChangeColor() const
1390 QObjectPtrList aObjects = mySelector->selection()->selectedObjects();
1392 std::set<std::string> aTypes;
1393 aTypes.insert(ModelAPI_ResultGroup::group());
1394 aTypes.insert(ModelAPI_ResultConstruction::group());
1395 aTypes.insert(ModelAPI_ResultBody::group());
1396 aTypes.insert(ModelAPI_ResultPart::group());
1398 return hasResults(aObjects, aTypes);
1401 void setColor(ResultPtr theResult, std::vector<int>& theColor)
1403 if (!theResult.get())
1406 AttributeIntArrayPtr aColorAttr = theResult->data()->intArray(ModelAPI_Result::COLOR_ID());
1407 if (aColorAttr.get() != NULL) {
1408 if (!aColorAttr->size()) {
1409 aColorAttr->setSize(3);
1411 aColorAttr->setValue(0, theColor[0]);
1412 aColorAttr->setValue(1, theColor[1]);
1413 aColorAttr->setValue(2, theColor[2]);
1417 //**************************************************************
1418 void XGUI_Workshop::changeColor(const QObjectPtrList& theObjects)
1420 AttributeIntArrayPtr aColorAttr;
1421 // 1. find the current color of the object. This is a color of AIS presentation
1422 // The objects are iterated until a first valid color is found
1423 std::vector<int> aColor;
1424 foreach(ObjectPtr anObject, theObjects) {
1425 if (anObject->groupName() == ModelAPI_ResultPart::group()) {
1426 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(anObject);
1427 DocumentPtr aPartDoc = aPart->partDoc();
1428 // the document should be checked on null, because in opened document if the part
1429 // has not been activated yet, the part document is empty
1430 if (!aPartDoc.get()) {
1431 emit errorOccurred(QString::fromLatin1("Color can not be changed on a part with an empty document"));
1434 if (aPartDoc->size(ModelAPI_ResultBody::group()) > 0) {
1435 ObjectPtr aObject = aPartDoc->object(ModelAPI_ResultBody::group(), 0);
1436 ResultBodyPtr aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aObject);
1438 std::string aSection, aName, aDefault;
1439 aBody->colorConfigInfo(aSection, aName, aDefault);
1440 if (!aSection.empty() && !aName.empty()) {
1441 aColor = Config_PropManager::color(aSection, aName, aDefault);
1448 AISObjectPtr anAISObj = myDisplayer->getAISObject(anObject);
1449 if (anAISObj.get()) {
1451 anAISObj->getColor(aColor[0], aColor[1], aColor[2]);
1454 if (!aColor.empty())
1457 if (aColor.size() != 3)
1460 // 2. show the dialog to change the value
1461 XGUI_ColorDialog* aDlg = new XGUI_ColorDialog(mainWindow());
1462 aDlg->setColor(aColor);
1463 aDlg->move(QCursor::pos());
1464 bool isDone = aDlg->exec() == QDialog::Accepted;
1468 bool isRandomColor = aDlg->isRandomColor();
1470 // 3. abort the previous operation and start a new one
1471 SessionPtr aMgr = ModelAPI_Session::get();
1472 bool aWasOperation = aMgr->isOperation(); // keep this value
1473 if (!aWasOperation) {
1474 QString aDescription = contextMenuMgr()->action("COLOR_CMD")->text();
1475 aMgr->startOperation(aDescription.toStdString());
1478 // 4. set the value to all results
1479 foreach(ObjectPtr anObj, theObjects) {
1480 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObj);
1481 if (aResult.get() != NULL) {
1482 if (aResult->groupName() == ModelAPI_ResultPart::group()) {
1483 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aResult);
1484 DocumentPtr aPartDoc = aPart->partDoc();
1485 // the document should be checked on null, because in opened document if the part
1486 // has not been activated yet, the part document is empty
1487 if (aPartDoc.get()) {
1488 for (int i = 0; i < aPartDoc->size(ModelAPI_ResultBody::group()); i++) {
1489 ObjectPtr aObject = aPartDoc->object(ModelAPI_ResultBody::group(), i);
1490 ResultBodyPtr aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aObject);
1491 std::vector<int> aColorResult = aDlg->getColor();
1492 setColor(aBody, aColorResult);
1497 std::vector<int> aColorResult = aDlg->getColor();
1498 setColor(aResult, aColorResult);
1503 aMgr->finishOperation();
1504 updateCommandStatus();
1507 //**************************************************************
1508 void XGUI_Workshop::showObjects(const QObjectPtrList& theList, bool isVisible)
1510 foreach (ObjectPtr aObj, theList)
1513 displayObject(aObj);
1515 myDisplayer->erase(aObj, false);
1518 myDisplayer->updateViewer();
1521 //**************************************************************
1522 void XGUI_Workshop::showOnlyObjects(const QObjectPtrList& theList)
1524 myDisplayer->showOnly(theList);
1528 //**************************************************************
1529 void XGUI_Workshop::registerValidators() const
1531 SessionPtr aMgr = ModelAPI_Session::get();
1532 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
1535 //**************************************************************
1536 void XGUI_Workshop::displayAllResults()
1538 SessionPtr aMgr = ModelAPI_Session::get();
1539 DocumentPtr aRootDoc = aMgr->moduleDocument();
1540 displayDocumentResults(aRootDoc);
1541 for (int i = 0; i < aRootDoc->size(ModelAPI_ResultPart::group()); i++) {
1542 ObjectPtr aObject = aRootDoc->object(ModelAPI_ResultPart::group(), i);
1543 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObject);
1544 displayDocumentResults(aPart->partDoc());
1546 myDisplayer->updateViewer();
1549 //**************************************************************
1550 void XGUI_Workshop::displayDocumentResults(DocumentPtr theDoc)
1554 displayGroupResults(theDoc, ModelAPI_ResultConstruction::group());
1555 displayGroupResults(theDoc, ModelAPI_ResultBody::group());
1558 //**************************************************************
1559 void XGUI_Workshop::displayGroupResults(DocumentPtr theDoc, std::string theGroup)
1561 for (int i = 0; i < theDoc->size(theGroup); i++)
1562 displayObject(theDoc->object(theGroup, i));
1565 //**************************************************************
1566 void XGUI_Workshop::setDisplayMode(const QObjectPtrList& theList, int theMode)
1568 foreach(ObjectPtr aObj, theList) {
1569 myDisplayer->setDisplayMode(aObj, (XGUI_Displayer::DisplayMode)theMode, false);
1571 if (theList.size() > 0)
1572 myDisplayer->updateViewer();
1575 //**************************************************************
1576 void XGUI_Workshop::closeDocument()
1578 ModuleBase_Operation* anOperation = operationMgr()->currentOperation();
1579 while (anOperation) {
1580 anOperation->abort();
1581 anOperation = operationMgr()->currentOperation();
1583 myDisplayer->closeLocalContexts();
1584 myDisplayer->eraseAll();
1585 objectBrowser()->clearContent();
1587 SessionPtr aMgr = ModelAPI_Session::get();
1589 objectBrowser()->clearContent();
1592 //**************************************************************
1593 bool XGUI_Workshop::displayObject(ObjectPtr theObj)
1595 if (!myModule->canDisplayObject(theObj))
1598 ResultBodyPtr aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(theObj);
1599 if (aBody.get() != NULL) {
1600 int aNb = myDisplayer->objectsCount();
1601 myDisplayer->display(theObj, false);
1604 } else if (!(myIsLoadingData || myPartActivating))
1605 myDisplayer->display(theObj, false);
1610 void XGUI_Workshop::addHistoryMenu(QObject* theObject, const char* theSignal, const char* theSlot)
1612 XGUI_HistoryMenu* aMenu = NULL;
1613 if (isSalomeMode()) {
1614 QAction* anAction = qobject_cast<QAction*>(theObject);
1617 aMenu = new XGUI_HistoryMenu(anAction);
1619 QToolButton* aButton = qobject_cast<QToolButton*>(theObject);
1620 aMenu = new XGUI_HistoryMenu(aButton);
1622 connect(this, theSignal, aMenu, SLOT(setHistory(const QList<ActionInfo>&)));
1623 connect(aMenu, SIGNAL(actionSelected(int)), this, theSlot);
1626 QList<ActionInfo> XGUI_Workshop::processHistoryList(const std::list<std::string>& theList) const
1628 QList<ActionInfo> aResult;
1629 std::list<std::string>::const_iterator it = theList.cbegin();
1630 for (; it != theList.cend(); it++) {
1631 QString anId = QString::fromStdString(*it);
1632 bool isEditing = anId.endsWith(ModuleBase_Operation::EditSuffix());
1634 anId.chop(ModuleBase_Operation::EditSuffix().size());
1637 QAction* aContextMenuAct = myContextMenuMgr->actionByName(anId);
1638 if (aContextMenuAct) {
1639 anInfo.initFrom(aContextMenuAct);
1641 anInfo = myActionsMgr->actionInfoById(anId);
1644 anInfo.text = anInfo.text.prepend("Modify ");