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_SELFILTER_LOADED));
170 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_UPDATE_VIEWER_BLOCKED));
171 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_UPDATE_VIEWER_UNBLOCKED));
173 registerValidators();
175 // Calling of loadCustomProps before activating module is required
176 // by Config_PropManger to restore user-defined path to plugins
177 ModuleBase_Preferences::loadCustomProps();
180 myMainWindow->show();
181 updateCommandStatus();
186 emit applicationStarted();
189 //******************************************************
190 void XGUI_Workshop::initMenu()
192 myContextMenuMgr->createActions();
194 if (isSalomeMode()) {
195 // Create only Undo, Redo commands
196 QAction* aAction = salomeConnector()->addDesktopCommand("UNDO_CMD", tr("Undo"),
197 tr("Undo last command"),
198 QIcon(":pictures/undo.png"),
199 QKeySequence::Undo, false, "MEN_DESK_EDIT");
200 connect(aAction, SIGNAL(triggered(bool)), this, SLOT(onUndo()));
201 addHistoryMenu(aAction, SIGNAL(updateUndoHistory(const QList<ActionInfo>&)), SLOT(onUndo(int)));
203 aAction = salomeConnector()->addDesktopCommand("REDO_CMD", tr("Redo"), tr("Redo last command"),
204 QIcon(":pictures/redo.png"), QKeySequence::Redo,
205 false, "MEN_DESK_EDIT");
206 connect(aAction, SIGNAL(triggered(bool)), this, SLOT(onRedo()));
207 addHistoryMenu(aAction, SIGNAL(updateRedoHistory(const QList<ActionInfo>&)), SLOT(onRedo(int)));
209 salomeConnector()->addDesktopMenuSeparator("MEN_DESK_EDIT");
210 aAction = salomeConnector()->addDesktopCommand("REBUILD_CMD", tr("Rebuild"), tr("Rebuild data objects"),
211 QIcon(":pictures/rebuild.png"), QKeySequence(),
212 false, "MEN_DESK_EDIT");
213 connect(aAction, SIGNAL(triggered(bool)), this, SLOT(onRebuild()));
214 salomeConnector()->addDesktopMenuSeparator("MEN_DESK_EDIT");
216 aAction = salomeConnector()->addDesktopCommand("SAVEAS_CMD", tr("Export NewGeom..."), tr("Export the current document into a NewGeom file"),
217 QIcon(), QKeySequence(),
218 false, "MEN_DESK_FILE");
219 connect(aAction, SIGNAL(triggered(bool)), this, SLOT(onSaveAs()));
221 aAction = salomeConnector()->addDesktopCommand("OPEN_CMD", tr("Import NewGeom..."), tr("Import a NewGeom file"),
222 QIcon(), QKeySequence(),
223 false, "MEN_DESK_FILE");
224 connect(aAction, SIGNAL(triggered(bool)), this, SLOT(onOpen()));
225 salomeConnector()->addDesktopMenuSeparator("MEN_DESK_FILE");
229 // File commands group
230 AppElements_MenuGroupPanel* aGroup = myMainWindow->menuObject()->generalPage();
232 AppElements_Command* aCommand;
234 aCommand = aGroup->addFeature("SAVE_CMD", tr("Save..."), tr("Save the document"),
235 QIcon(":pictures/save.png"), QKeySequence::Save);
236 aCommand->connectTo(this, SLOT(onSave()));
237 //aCommand->disable();
239 QString aUndoId = "UNDO_CMD";
240 aCommand = aGroup->addFeature(aUndoId, tr("Undo"), tr("Undo last command"),
241 QIcon(":pictures/undo.png"), QKeySequence::Undo);
242 aCommand->connectTo(this, SLOT(onUndo()));
243 AppElements_Button* aUndoButton = qobject_cast<AppElements_Button*>(aGroup->widget(aUndoId));
244 addHistoryMenu(aUndoButton,
245 SIGNAL(updateUndoHistory(const QList<ActionInfo>&)),
248 QString aRedoId = "REDO_CMD";
249 aCommand = aGroup->addFeature(aRedoId, tr("Redo"), tr("Redo last command"),
250 QIcon(":pictures/redo.png"), QKeySequence::Redo);
251 aCommand->connectTo(this, SLOT(onRedo()));
252 AppElements_Button* aRedoButton = qobject_cast<AppElements_Button*>(aGroup->widget(aRedoId));
253 addHistoryMenu(aRedoButton,
254 SIGNAL(updateRedoHistory(const QList<ActionInfo>&)),
257 aCommand = aGroup->addFeature("REBUILD_CMD", tr("Rebuild"), tr("Rebuild data objects"),
258 QIcon(":pictures/rebuild.png"), QKeySequence());
259 aCommand->connectTo(this, SLOT(onRebuild()));
261 aCommand = aGroup->addFeature("SAVEAS_CMD", tr("Save as..."), tr("Save the document into a file"),
262 QIcon(":pictures/save.png"), QKeySequence());
263 aCommand->connectTo(this, SLOT(onSaveAs()));
264 //aCommand->disable();
266 aCommand = aGroup->addFeature("OPEN_CMD", tr("Open..."), tr("Open a new document"),
267 QIcon(":pictures/open.png"), QKeySequence::Open);
268 aCommand->connectTo(this, SLOT(onOpen()));
270 //aCommand = aGroup->addFeature("NEW_CMD", tr("New"), tr("Create a new document"),
271 // QIcon(":pictures/new.png"), QKeySequence::New);
272 //aCommand->connectTo(this, SLOT(onNew()));
274 aCommand = aGroup->addFeature("PREF_CMD", tr("Preferences"), tr("Edit preferences"),
275 QIcon(":pictures/preferences.png"), QKeySequence::Preferences);
276 aCommand->connectTo(this, SLOT(onPreferences()));
278 aCommand = aGroup->addFeature("EXIT_CMD", tr("Exit"), tr("Exit application"),
279 QIcon(":pictures/close.png"), QKeySequence::Close);
280 aCommand->connectTo(this, SLOT(onExit()));
281 //FIXME: SBH's test action. Can be used for some GUI tests.
283 // aCommand = aGroup->addFeature("TEST_CMD", "Test!", "Private debug button",
284 // QIcon(":pictures/close.png"), QKeySequence(), true);
285 // aCommand->connectTo(myMainWindow, SLOT(dockPythonConsole()));
289 //******************************************************
290 AppElements_Workbench* XGUI_Workshop::addWorkbench(const QString& theName)
292 AppElements_MainMenu* aMenuBar = myMainWindow->menuObject();
293 return aMenuBar->addWorkbench(theName);
296 //******************************************************
297 void XGUI_Workshop::processEvent(const std::shared_ptr<Events_Message>& theMessage)
299 if (QApplication::instance()->thread() != QThread::currentThread()) {
301 std::cout << "XGUI_Workshop::processEvent: " << "Working in another thread." << std::endl;
303 SessionPtr aMgr = ModelAPI_Session::get();
304 PostponeMessageQtEvent* aPostponeEvent = new PostponeMessageQtEvent(theMessage);
305 QApplication::postEvent(this, aPostponeEvent);
309 //A message to start feature creation received.
310 if (theMessage->eventID() == Events_Loop::loop()->eventByName(Config_FeatureMessage::GUI_EVENT())) {
311 std::shared_ptr<Config_FeatureMessage> aFeatureMsg =
312 std::dynamic_pointer_cast<Config_FeatureMessage>(theMessage);
313 if (!aFeatureMsg->isInternal()) {
314 addFeature(aFeatureMsg);
317 // Process creation of Part
318 else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_CREATED)) {
319 std::shared_ptr<ModelAPI_ObjectUpdatedMessage> aUpdMsg =
320 std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
321 onFeatureCreatedMsg(aUpdMsg);
323 if (mySalomeConnector)
324 mySalomeConnector->createPreferences();
325 myUpdatePrefs = false;
328 else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_PLUGIN_LOADED)) {
329 myUpdatePrefs = true;
332 else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY)) {
333 std::shared_ptr<ModelAPI_ObjectUpdatedMessage> aUpdMsg =
334 std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
335 onFeatureRedisplayMsg(aUpdMsg);
337 //Update property panel on corresponding message. If there is no current operation (no
338 //property panel), or received message has different feature to the current - do nothing.
339 else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_UPDATED)) {
340 std::shared_ptr<ModelAPI_ObjectUpdatedMessage> anUpdateMsg =
341 std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
342 onFeatureUpdatedMsg(anUpdateMsg);
343 } else if (theMessage->eventID() == Events_LongOp::eventID()) {
344 if (Events_LongOp::isPerformed()) {
345 QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
347 QApplication::restoreOverrideCursor();
350 //An operation passed by message. Start it, process and commit.
351 else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OPERATION_LAUNCHED)) {
352 std::shared_ptr<Config_PointerMessage> aPartSetMsg =
353 std::dynamic_pointer_cast<Config_PointerMessage>(theMessage);
354 //myPropertyPanel->cleanContent();
355 ModuleBase_Operation* anOperation = (ModuleBase_Operation*) aPartSetMsg->pointer();
357 if (myOperationMgr->startOperation(anOperation)) {
358 myPropertyPanel->updateContentWidget(anOperation->feature());
359 if (!anOperation->getDescription()->hasXmlRepresentation()) {
360 if (anOperation->commit())
361 updateCommandStatus();
365 else if (theMessage->eventID() == Events_Loop::eventByName(EVENT_SELFILTER_LOADED)) {
366 std::shared_ptr<Config_SelectionFilterMessage> aMsg =
367 std::dynamic_pointer_cast<Config_SelectionFilterMessage>(theMessage);
369 ModuleBase_FilterFactory* aFactory = moduleConnector()->selectionFilters();
370 if (!aMsg->attributeId().empty()) {
371 aFactory->assignFilter(aMsg->selectionFilterId(), aMsg->featureId(), aMsg->attributeId(),
375 } else if (theMessage->eventID() == Events_Loop::eventByName(EVENT_UPDATE_VIEWER_BLOCKED)) {
376 // the viewer's update context will not happens until viewer updated is emitted
377 myDisplayer->enableUpdateViewer(false);
378 } else if (theMessage->eventID() == Events_Loop::eventByName(EVENT_UPDATE_VIEWER_UNBLOCKED)) {
379 // the viewer's update context is unblocked, the viewer's update works
380 myDisplayer->enableUpdateViewer(true);
382 //Show error dialog if error message received.
383 std::shared_ptr<Events_Error> anAppError = std::dynamic_pointer_cast<Events_Error>(theMessage);
385 emit errorOccurred(QString::fromLatin1(anAppError->description()));
389 if (!isSalomeMode()) {
390 SessionPtr aMgr = ModelAPI_Session::get();
391 if (aMgr->isModified() != myMainWindow->isModifiedState())
392 myMainWindow->setModifiedState(aMgr->isModified());
396 //******************************************************
397 QMainWindow* XGUI_Workshop::desktop() const
399 return isSalomeMode() ? salomeConnector()->desktop() : myMainWindow;
402 //******************************************************
403 void XGUI_Workshop::onStartWaiting()
405 if (Events_LongOp::isPerformed()) {
406 QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
410 //******************************************************
411 void XGUI_Workshop::onFeatureUpdatedMsg(const std::shared_ptr<ModelAPI_ObjectUpdatedMessage>& theMsg)
413 std::set<ObjectPtr> aFeatures = theMsg->objects();
414 if (myOperationMgr->hasOperation()) {
415 FeaturePtr aCurrentFeature = myOperationMgr->currentOperation()->feature();
416 std::set<ObjectPtr>::const_iterator aIt;
417 for (aIt = aFeatures.begin(); aIt != aFeatures.end(); ++aIt) {
418 ObjectPtr aNewFeature = (*aIt);
419 if (aNewFeature == aCurrentFeature) {
420 myPropertyPanel->updateContentWidget(aCurrentFeature);
425 myOperationMgr->onValidateOperation();
426 //if (myObjectBrowser)
427 // myObjectBrowser->processEvent(theMsg);
430 //******************************************************
431 void XGUI_Workshop::onFeatureRedisplayMsg(const std::shared_ptr<ModelAPI_ObjectUpdatedMessage>& theMsg)
433 std::set<ObjectPtr> aObjects = theMsg->objects();
434 std::set<ObjectPtr>::const_iterator aIt;
436 #ifdef DEBUG_FEATURE_REDISPLAY
438 for (aIt = aObjects.begin(); aIt != aObjects.end(); ++aIt) {
439 anInfo.append(ModuleBase_Tools::objectInfo((*aIt)));
441 QString anInfoStr = anInfo.join(", ");
442 qDebug(QString("onFeatureRedisplayMsg: %1, %2").arg(aObjects.size()).arg(anInfoStr).toStdString().c_str());
445 for (aIt = aObjects.begin(); aIt != aObjects.end(); ++aIt) {
446 ObjectPtr aObj = (*aIt);
448 // Hide the object if it is invalid or concealed one
449 bool aHide = !aObj->data() || !aObj->data()->isValid() ||
450 aObj->isDisabled() || (!aObj->isDisplayed());
451 if (!aHide) { // check that this is not hidden result
452 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(aObj);
453 aHide = aRes && aRes->isConcealed();
456 myDisplayer->erase(aObj, false);
458 // Redisplay the visible object or the object of the current operation
459 bool isVisibleObject = myDisplayer->isVisible(aObj);
460 #ifdef DEBUG_FEATURE_REDISPLAY
461 //QString anObjInfo = ModuleBase_Tools::objectInfo((aObj));
462 //qDebug(QString("visible=%1 : display= %2").arg(isVisibleObject).arg(anObjInfo).toStdString().c_str());
465 if (isVisibleObject) { // redisplay visible object
466 //displayObject(aObj); // In order to update presentation
467 // in order to avoid the check whether the object can be redisplayed, the exact method
468 // of redisplay is called. This modification is made in order to have the line is updated
469 // by creation of a horizontal constraint on the line by preselection
470 myDisplayer->redisplay(aObj, false);
471 if (myOperationMgr->hasOperation()) {
472 ModuleBase_Operation* aOperation = myOperationMgr->currentOperation();
473 if (!aOperation->isEditOperation() &&
474 aOperation->hasObject(aObj) && myDisplayer->isActive(aObj))
475 myDisplayer->deactivate(aObj);
477 } else { // display object if the current operation has it
478 if (displayObject(aObj)) {
479 ModuleBase_Operation* aOperation = myOperationMgr->currentOperation();
480 if (aOperation && aOperation->hasObject(aObj)) {
481 ModuleBase_Operation* aOperation = myOperationMgr->currentOperation();
482 #ifdef DEBUG_FEATURE_REDISPLAY
483 QString anObjInfo = ModuleBase_Tools::objectInfo((aObj));
484 qDebug(QString(" display object = %1").arg(anObjInfo).toStdString().c_str());
486 // Deactivate object of current operation from selection
487 if (myDisplayer->isActive(aObj))
488 myDisplayer->deactivate(aObj);
494 myDisplayer->updateViewer();
497 //******************************************************
498 void XGUI_Workshop::onFeatureCreatedMsg(const std::shared_ptr<ModelAPI_ObjectUpdatedMessage>& theMsg)
500 std::set<ObjectPtr> aObjects = theMsg->objects();
501 std::set<ObjectPtr>::const_iterator aIt;
502 #ifdef DEBUG_FEATURE_CREATED
504 for (aIt = aObjects.begin(); aIt != aObjects.end(); ++aIt) {
505 anInfo.append(ModuleBase_Tools::objectInfo((*aIt)));
507 QString anInfoStr = anInfo.join(", ");
508 qDebug(QString("onFeatureCreatedMsg: %1, %2").arg(aObjects.size()).arg(anInfoStr).toStdString().c_str());
511 //bool aHasPart = false;
512 bool isDisplayed = false;
513 for (aIt = aObjects.begin(); aIt != aObjects.end(); ++aIt) {
514 ObjectPtr anObject = *aIt;
515 // the validity of the data should be checked here in order to avoid display of the objects,
516 // which were created, then deleted, but flush for the creation event happens after that
517 // we should not display disabled objects
518 if (!anObject->data() || !anObject->data()->isValid() || anObject->isDisabled())
520 //ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aIt);
523 // If a feature is created from the aplication's python console
524 // it doesn't stored in the operation mgr and doesn't displayed
526 isDisplayed = displayObject(*aIt);
529 //if (myObjectBrowser)
530 // myObjectBrowser->processEvent(theMsg);
532 myDisplayer->updateViewer();
533 //if (aHasPart) { // TODO: Avoid activate last part on loading of document
534 // activateLastPart();
538 //******************************************************
539 void XGUI_Workshop::onOperationStarted(ModuleBase_Operation* theOperation)
541 setNestedFeatures(theOperation);
543 if (theOperation->getDescription()->hasXmlRepresentation()) { //!< No need for property panel
544 connectWithOperation(theOperation);
545 setPropertyPanel(theOperation);
547 updateCommandStatus();
549 myModule->operationStarted(theOperation);
552 //******************************************************
553 void XGUI_Workshop::onOperationResumed(ModuleBase_Operation* theOperation)
555 setNestedFeatures(theOperation);
557 if (theOperation->getDescription()->hasXmlRepresentation()) { //!< No need for property panel
558 // connectWithOperation(theOperation); already connected
559 setPropertyPanel(theOperation);
561 updateCommandStatus();
563 myModule->operationResumed(theOperation);
567 //******************************************************
568 void XGUI_Workshop::onOperationStopped(ModuleBase_Operation* theOperation)
570 ModuleBase_ISelection* aSel = mySelector->selection();
571 QObjectPtrList aObj = aSel->selectedPresentations();
572 //!< No need for property panel
573 updateCommandStatus();
575 myPropertyPanel->cleanContent();
577 // Activate objects created by current operation
578 // in order to clean selection modes
579 // the deactivation should be pefromed in the same place, where the mode is activated,
580 // e.g. activation in the current widget activation, deactivation - in the widget's deactivation
582 //myDisplayer->activateObjects(aModes);
583 myModule->operationStopped(theOperation);
585 // if the operation is nested, do not deactivate objects
586 //if (myOperationMgr->operationsCount() == 0) {
587 // Activate selection mode for all objects
589 // TODO: check on OCC_6.9.0
590 // the module current active modes should not be deactivated in order to save the objects selected
591 // the deactivate object in the mode of selection leads to the object is deselected in the viewer.
592 // But, in OCC_6.8.0 this deselection does not happened automatically. It is necessary to call
593 // ClearOutdatedSelection, but this method has an error in the realization, which should be fixed in
594 // the OCC_6.9.0 release. Moreother, it is possible that ClearOutdatedSelection will be called inside
595 // Deactivate method of AIS_InteractiveContext. In this case, we need not call it.
596 module()->activeSelectionModes(aModes);
597 myDisplayer->activateObjects(aModes);
602 void XGUI_Workshop::onOperationCommitted(ModuleBase_Operation* theOperation)
604 myModule->operationCommitted(theOperation);
607 void XGUI_Workshop::onOperationAborted(ModuleBase_Operation* theOperation)
609 myModule->operationAborted(theOperation);
612 void XGUI_Workshop::setNestedFeatures(ModuleBase_Operation* theOperation)
614 if (this->isSalomeMode())
615 theOperation->setNestedFeatures(mySalomeConnector->nestedActions(theOperation->id()));
617 theOperation->setNestedFeatures(myActionsMgr->nestedCommands(theOperation->id()));
620 void XGUI_Workshop::setPropertyPanel(ModuleBase_Operation* theOperation)
623 QString aXmlRepr = theOperation->getDescription()->xmlRepresentation();
624 ModuleBase_WidgetFactory aFactory = ModuleBase_WidgetFactory(aXmlRepr.toStdString(),
627 myPropertyPanel->cleanContent();
628 aFactory.createWidget(myPropertyPanel->contentWidget());
630 QList<ModuleBase_ModelWidget*> aWidgets = aFactory.getModelWidgets();
631 foreach (ModuleBase_ModelWidget* aWidget, aWidgets) {
632 bool isStoreValue = !theOperation->isEditOperation() &&
633 !aWidget->getDefaultValue().empty() &&
634 !aWidget->isComputedDefault();
635 aWidget->setFeature(theOperation->feature(), isStoreValue);
636 aWidget->enableFocusProcessing();
639 myPropertyPanel->setModelWidgets(aWidgets);
640 theOperation->setPropertyPanel(myPropertyPanel);
642 myModule->propertyPanelDefined(theOperation);
644 myPropertyPanel->setWindowTitle(theOperation->getDescription()->description());
647 bool XGUI_Workshop::event(QEvent * theEvent)
649 PostponeMessageQtEvent* aPostponedEv = dynamic_cast<PostponeMessageQtEvent*>(theEvent);
651 std::shared_ptr<Events_Message> aEventPtr = aPostponedEv->postponedMessage();
652 processEvent(aEventPtr);
661 void XGUI_Workshop::addFeature(const std::shared_ptr<Config_FeatureMessage>& theMessage)
665 qDebug() << "XGUI_Workshop::addFeature: NULL message.";
669 ActionInfo aFeatureInfo;
670 aFeatureInfo.initFrom(theMessage);
672 QString aWchName = QString::fromStdString(theMessage->workbenchId());
673 QStringList aNestedFeatures =
674 QString::fromStdString(theMessage->nestedFeatures()).split(" ", QString::SkipEmptyParts);
675 QString aDocKind = QString::fromStdString(theMessage->documentKind());
676 QList<QAction*> aNestedActList;
677 bool isColumnButton = !aNestedFeatures.isEmpty();
678 if (isColumnButton) {
679 QString aNestedActions = QString::fromStdString(theMessage->actionsWhenNested());
680 if (aNestedActions.contains("accept")) {
681 QAction* anAction = myActionsMgr->operationStateAction(XGUI_ActionsMgr::AcceptAll, NULL);
682 connect(anAction, SIGNAL(triggered()), myOperationMgr, SLOT(commitAllOperations()));
683 aNestedActList << anAction;
685 if (aNestedActions.contains("abort")) {
686 QAction* anAction = myActionsMgr->operationStateAction(XGUI_ActionsMgr::AbortAll, NULL);
687 connect(anAction, SIGNAL(triggered()), myOperationMgr, SLOT(abortAllOperations()));
688 aNestedActList << anAction;
692 if (isSalomeMode()) {
694 if (isColumnButton) {
695 aAction = salomeConnector()->addNestedFeature(aWchName, aFeatureInfo, aNestedActList);
697 aAction = salomeConnector()->addFeature(aWchName, aFeatureInfo);
699 salomeConnector()->setNestedActions(aFeatureInfo.id, aNestedFeatures);
700 salomeConnector()->setDocumentKind(aFeatureInfo.id, aDocKind);
702 myActionsMgr->addCommand(aAction);
703 myModule->actionCreated(aAction);
705 //Find or create Workbench
706 AppElements_MainMenu* aMenuBar = myMainWindow->menuObject();
707 AppElements_Workbench* aPage = aMenuBar->findWorkbench(aWchName);
709 aPage = addWorkbench(aWchName);
711 //Find or create Group
712 QString aGroupName = QString::fromStdString(theMessage->groupId());
713 AppElements_MenuGroupPanel* aGroup = aPage->findGroup(aGroupName);
715 aGroup = aPage->addGroup(aGroupName);
717 // Check if hotkey sequence is already defined:
718 QKeySequence aHotKey = myActionsMgr->registerShortcut(aFeatureInfo.shortcut);
719 if(aHotKey != aFeatureInfo.shortcut) {
720 aFeatureInfo.shortcut = aHotKey;
723 AppElements_Command* aCommand = aGroup->addFeature(aFeatureInfo,
726 // Enrich created button with accept/abort buttons if necessary
727 AppElements_Button* aButton = aCommand->button();
728 if (aButton->isColumnButton()) {
729 aButton->setAdditionalButtons(aNestedActList);
731 myActionsMgr->addCommand(aCommand);
732 myModule->actionCreated(aCommand);
737 * Makes a signal/slot connections between Property Panel
738 * and given operation. The given operation becomes a
739 * current operation and previous operation if exists
741 void XGUI_Workshop::connectWithOperation(ModuleBase_Operation* theOperation)
743 QAction* aCommand = 0;
744 if (isSalomeMode()) {
745 aCommand = salomeConnector()->command(theOperation->getDescription()->operationId());
747 AppElements_MainMenu* aMenu = myMainWindow->menuObject();
748 FeaturePtr aFeature = theOperation->feature();
750 aCommand = aMenu->feature(QString::fromStdString(aFeature->getKind()));
752 //Abort operation on uncheck the command
754 connect(aCommand, SIGNAL(triggered(bool)), theOperation, SLOT(setRunning(bool)));
759 * Saves document with given name.
761 void XGUI_Workshop::saveDocument(const QString& theName, std::list<std::string>& theFileNames)
763 QApplication::restoreOverrideCursor();
764 SessionPtr aMgr = ModelAPI_Session::get();
765 aMgr->save(theName.toLatin1().constData(), theFileNames);
766 QApplication::restoreOverrideCursor();
769 bool XGUI_Workshop::isActiveOperationAborted()
771 return myOperationMgr->abortAllOperations();
774 //******************************************************
775 void XGUI_Workshop::onExit()
777 SessionPtr aMgr = ModelAPI_Session::get();
778 if (aMgr->isModified()) {
779 int anAnswer = QMessageBox::question(
780 myMainWindow, tr("Save current file"), tr("The document is modified, save before exit?"),
781 QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel, QMessageBox::Cancel);
782 if (anAnswer == QMessageBox::Save) {
783 bool saved = onSave();
787 } else if (anAnswer == QMessageBox::Cancel) {
794 //******************************************************
795 void XGUI_Workshop::onNew()
797 QApplication::setOverrideCursor(Qt::WaitCursor);
798 if (objectBrowser() == 0) {
800 mySelector->connectViewers();
802 myViewerProxy->connectToViewer();
804 if (!isSalomeMode()) {
805 myMainWindow->showPythonConsole();
806 QMdiSubWindow* aWnd = myMainWindow->viewer()->createView();
807 aWnd->showMaximized();
808 updateCommandStatus();
810 myContextMenuMgr->connectViewer();
811 QApplication::restoreOverrideCursor();
814 //******************************************************
815 void XGUI_Workshop::onOpen()
817 if(!isActiveOperationAborted())
819 //save current file before close if modified
820 SessionPtr aSession = ModelAPI_Session::get();
821 if (aSession->isModified()) {
822 //TODO(sbh): re-launch the app?
823 int anAnswer = QMessageBox::question(
824 myMainWindow, tr("Save current file"),
825 tr("The document is modified, save before opening another?"),
826 QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel, QMessageBox::Cancel);
827 if (anAnswer == QMessageBox::Save) {
829 } else if (anAnswer == QMessageBox::Cancel) {
835 //show file dialog, check if readable and open
836 myCurrentDir = QFileDialog::getExistingDirectory(mainWindow());
837 if (myCurrentDir.isEmpty())
839 QFileInfo aFileInfo(myCurrentDir);
840 if (!aFileInfo.exists() || !aFileInfo.isReadable()) {
841 QMessageBox::critical(myMainWindow, tr("Warning"), tr("Unable to open the file."));
845 QApplication::setOverrideCursor(Qt::WaitCursor);
846 myIsLoadingData = true;
847 aSession->closeAll();
848 aSession->load(myCurrentDir.toLatin1().constData());
849 myObjectBrowser->rebuildDataTree();
851 updateCommandStatus();
852 myIsLoadingData = false;
853 QApplication::restoreOverrideCursor();
856 //******************************************************
857 bool XGUI_Workshop::onSave()
859 if(!isActiveOperationAborted())
861 if (myCurrentDir.isEmpty()) {
864 std::list<std::string> aFiles;
865 saveDocument(myCurrentDir, aFiles);
866 updateCommandStatus();
868 myMainWindow->setModifiedState(false);
872 //******************************************************
873 bool XGUI_Workshop::onSaveAs()
875 if(!isActiveOperationAborted())
877 QFileDialog dialog(mainWindow());
878 dialog.setWindowTitle(tr("Select directory to save files..."));
879 dialog.setFileMode(QFileDialog::Directory);
880 dialog.setFilter(tr("Folders (*)"));
881 dialog.setOptions(QFileDialog::HideNameFilterDetails | QFileDialog::ShowDirsOnly);
882 dialog.setViewMode(QFileDialog::Detail);
884 if (!dialog.exec()) {
887 QString aTempDir = dialog.selectedFiles().first();
889 if (aDir.exists() && !aDir.entryInfoList(QDir::NoDotAndDotDot | QDir::AllEntries).isEmpty()) {
890 int answer = QMessageBox::question(
892 //: Title of the dialog which asks user if he wants to save study in existing non-empty folder
894 tr("The folder already contains some files, save anyway?"),
895 QMessageBox::Save | QMessageBox::Cancel);
896 if (answer == QMessageBox::Cancel) {
900 myCurrentDir = aTempDir;
901 if (!isSalomeMode()) {
902 myMainWindow->setCurrentDir(myCurrentDir, false);
903 myMainWindow->setModifiedState(false);
908 //******************************************************
909 void XGUI_Workshop::onUndo(int theTimes)
911 objectBrowser()->treeView()->setCurrentIndex(QModelIndex());
912 SessionPtr aMgr = ModelAPI_Session::get();
913 if (aMgr->isOperation())
914 operationMgr()->onAbortOperation();
915 for (int i = 0; i < theTimes; ++i) {
918 updateCommandStatus();
921 //******************************************************
922 void XGUI_Workshop::onRedo(int theTimes)
924 // the viewer update should be blocked in order to avoid the features blinking. For the created
925 // feature a results are created, the flush of the created signal caused the viewer redisplay for
926 // each created result. After a redisplay signal is flushed. So, the viewer update is blocked until
927 // redo of all possible objects happens
928 bool isUpdateEnabled = myDisplayer->enableUpdateViewer(false);
930 objectBrowser()->treeView()->setCurrentIndex(QModelIndex());
931 SessionPtr aMgr = ModelAPI_Session::get();
932 if (aMgr->isOperation())
933 operationMgr()->onAbortOperation();
934 for (int i = 0; i < theTimes; ++i) {
937 updateCommandStatus();
939 // unblock the viewer update functionality and make update on purpose
940 myDisplayer->enableUpdateViewer(isUpdateEnabled);
941 myDisplayer->updateViewer();
944 //******************************************************
945 void XGUI_Workshop::onRebuild()
947 SessionPtr aMgr = ModelAPI_Session::get();
948 bool aWasOperation = aMgr->isOperation(); // keep this value
949 if (!aWasOperation) {
950 aMgr->startOperation("Rebuild");
952 static const Events_ID aRebuildEvent = Events_Loop::loop()->eventByName("Rebuild");
953 Events_Loop::loop()->send(std::shared_ptr<Events_Message>(
954 new Events_Message(aRebuildEvent, this)));
955 if (!aWasOperation) {
956 aMgr->finishOperation();
958 updateCommandStatus();
961 //******************************************************
962 void XGUI_Workshop::onPreferences()
964 ModuleBase_Prefs aModif;
965 ModuleBase_Preferences::editPreferences(aModif);
966 if (aModif.size() > 0) {
968 foreach (ModuleBase_Pref aPref, aModif)
970 aSection = aPref.first;
971 if (aSection == ModuleBase_Preferences::VIEWER_SECTION) {
973 myMainWindow->viewer()->updateFromResources();
974 } else if (aSection == ModuleBase_Preferences::MENU_SECTION) {
976 myMainWindow->menuObject()->updateFromResources();
982 //******************************************************
983 ModuleBase_IModule* XGUI_Workshop::loadModule(const QString& theModule)
985 QString libName = QString::fromStdString(library(theModule.toStdString()));
986 if (libName.isEmpty()) {
987 qWarning(qPrintable(tr("Information about module \"%1\" doesn't exist.").arg(theModule)));
992 CREATE_FUNC crtInst = 0;
995 HINSTANCE modLib = ::LoadLibrary((LPTSTR) qPrintable(libName));
999 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
1000 0, ::GetLastError(), 0, (LPTSTR) & lpMsgBuf, 0, 0);
1001 QString aMsg((char*) &lpMsgBuf);
1002 err = QString("Failed to load %1. %2").arg(libName).arg(aMsg);
1003 ::LocalFree(lpMsgBuf);
1005 crtInst = (CREATE_FUNC) ::GetProcAddress(modLib, CREATE_MODULE);
1009 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM
1010 | FORMAT_MESSAGE_IGNORE_INSERTS,
1011 0, ::GetLastError(), 0, (LPTSTR) & lpMsgBuf, 0, 0);
1012 QString aMsg((char*) &lpMsgBuf);
1013 err = QString("Failed to find %1 function. %2").arg( CREATE_MODULE).arg(aMsg);
1014 ::LocalFree(lpMsgBuf);
1018 void* modLib = dlopen( libName.toLatin1(), RTLD_LAZY | RTLD_GLOBAL );
1020 err = QString( "Can not load library %1. %2" ).arg( libName ).arg( dlerror() );
1022 crtInst = (CREATE_FUNC)dlsym( modLib, CREATE_MODULE );
1024 err = QString( "Failed to find function %1. %2" ).arg( CREATE_MODULE ).arg( dlerror() );
1029 ModuleBase_IModule* aModule = crtInst ? crtInst(myModuleConnector) : 0;
1031 if (!err.isEmpty()) {
1033 Events_Error::send(err.toStdString());
1035 qWarning(qPrintable(err));
1041 //******************************************************
1042 bool XGUI_Workshop::activateModule()
1044 Config_ModuleReader aModuleReader;
1045 QString moduleName = QString::fromStdString(aModuleReader.getModuleName());
1046 myModule = loadModule(moduleName);
1050 connect(myDisplayer, SIGNAL(objectDisplayed(ObjectPtr, AISObjectPtr)),
1051 myModule, SLOT(onObjectDisplayed(ObjectPtr, AISObjectPtr)));
1052 connect(myDisplayer, SIGNAL(beforeObjectErase(ObjectPtr, AISObjectPtr)),
1053 myModule, SLOT(onBeforeObjectErase(ObjectPtr, AISObjectPtr)));
1055 myModule->createFeatures();
1056 myActionsMgr->update();
1060 //******************************************************
1061 void XGUI_Workshop::updateCommandStatus()
1063 QList<QAction*> aCommands;
1064 if (isSalomeMode()) { // update commands in SALOME mode
1065 aCommands = salomeConnector()->commandList();
1067 AppElements_MainMenu* aMenuBar = myMainWindow->menuObject();
1068 foreach (AppElements_Command* aCmd, aMenuBar->features())
1069 aCommands.append(aCmd);
1071 SessionPtr aMgr = ModelAPI_Session::get();
1072 if (aMgr->hasModuleDocument()) {
1073 QAction *aUndoCmd, *aRedoCmd;
1074 foreach(QAction* aCmd, aCommands) {
1075 QString aId = aCmd->data().toString();
1076 if (aId == "UNDO_CMD")
1078 else if (aId == "REDO_CMD")
1081 // Enable all commands
1082 aCmd->setEnabled(true);
1085 aUndoCmd->setEnabled(myModule->canUndo());
1086 aRedoCmd->setEnabled(myModule->canRedo());
1089 foreach(QAction* aCmd, aCommands) {
1090 QString aId = aCmd->data().toString();
1091 if (aId == "NEW_CMD")
1092 aCmd->setEnabled(true);
1093 else if (aId == "EXIT_CMD")
1094 aCmd->setEnabled(true);
1096 aCmd->setEnabled(false);
1099 myActionsMgr->update();
1100 emit commandStatusUpdated();
1103 void XGUI_Workshop::updateHistory()
1105 std::list<std::string> aUndoList = ModelAPI_Session::get()->undoList();
1106 QList<ActionInfo> aUndoRes = processHistoryList(aUndoList);
1107 emit updateUndoHistory(aUndoRes);
1109 std::list<std::string> aRedoList = ModelAPI_Session::get()->redoList();
1110 QList<ActionInfo> aRedoRes = processHistoryList(aRedoList);
1111 emit updateRedoHistory(aRedoRes);
1114 //******************************************************
1115 QDockWidget* XGUI_Workshop::createObjectBrowser(QWidget* theParent)
1117 QDockWidget* aObjDock = new QDockWidget(theParent);
1118 aObjDock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea | Qt::BottomDockWidgetArea);
1119 aObjDock->setWindowTitle(tr("Object browser"));
1120 aObjDock->setStyleSheet(
1121 "::title { position: relative; padding-left: 5px; text-align: left center }");
1122 myObjectBrowser = new XGUI_ObjectsBrowser(aObjDock);
1123 myObjectBrowser->setDataModel(myModule->dataModel());
1124 myModule->customizeObjectBrowser(myObjectBrowser);
1125 aObjDock->setWidget(myObjectBrowser);
1127 myContextMenuMgr->connectObjectBrowser();
1131 //******************************************************
1133 * Creates dock widgets, places them in corresponding area
1134 * and tabifies if necessary.
1136 void XGUI_Workshop::createDockWidgets()
1138 QMainWindow* aDesktop = isSalomeMode() ? salomeConnector()->desktop() : myMainWindow;
1139 QDockWidget* aObjDock = createObjectBrowser(aDesktop);
1140 aDesktop->addDockWidget(Qt::LeftDockWidgetArea, aObjDock);
1141 myPropertyPanel = new XGUI_PropertyPanel(aDesktop);
1142 myPropertyPanel->setupActions(myActionsMgr);
1143 myPropertyPanel->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea | Qt::BottomDockWidgetArea);
1144 aDesktop->addDockWidget(Qt::LeftDockWidgetArea, myPropertyPanel);
1145 hidePropertyPanel(); ///<! Invisible by default
1146 hideObjectBrowser();
1147 aDesktop->tabifyDockWidget(aObjDock, myPropertyPanel);
1148 myPropertyPanel->installEventFilter(myOperationMgr);
1150 QAction* aOkAct = myActionsMgr->operationStateAction(XGUI_ActionsMgr::Accept);
1151 connect(aOkAct, SIGNAL(triggered()), myOperationMgr, SLOT(onCommitOperation()));
1152 QAction* aCancelAct = myActionsMgr->operationStateAction(XGUI_ActionsMgr::Abort);
1153 connect(aCancelAct, SIGNAL(triggered()), myOperationMgr, SLOT(onAbortOperation()));
1154 connect(myPropertyPanel, SIGNAL(noMoreWidgets()), myModule, SLOT(onNoMoreWidgets()));
1155 connect(myPropertyPanel, SIGNAL(keyReleased(QKeyEvent*)),
1156 myOperationMgr, SLOT(onKeyReleased(QKeyEvent*)));
1157 connect(myOperationMgr, SIGNAL(validationStateChanged(bool)),
1158 aOkAct, SLOT(setEnabled(bool)));
1159 QAction* aAcceptAllAct = myActionsMgr->operationStateAction(XGUI_ActionsMgr::AcceptAll);
1160 connect(myOperationMgr, SIGNAL(nestedStateChanged(bool)),
1161 aAcceptAllAct, SLOT(setEnabled(bool)));
1165 //******************************************************
1166 void XGUI_Workshop::showPropertyPanel()
1168 QAction* aViewAct = myPropertyPanel->toggleViewAction();
1169 ///<! Restore ability to close panel from the window's menu
1170 aViewAct->setEnabled(true);
1171 myPropertyPanel->show();
1172 myPropertyPanel->raise();
1175 //******************************************************
1176 void XGUI_Workshop::hidePropertyPanel()
1178 QAction* aViewAct = myPropertyPanel->toggleViewAction();
1179 ///<! Do not allow to show empty property panel
1180 aViewAct->setEnabled(false);
1181 myPropertyPanel->hide();
1184 //******************************************************
1185 void XGUI_Workshop::showObjectBrowser()
1187 myObjectBrowser->parentWidget()->show();
1190 //******************************************************
1191 void XGUI_Workshop::hideObjectBrowser()
1193 myObjectBrowser->parentWidget()->hide();
1196 //******************************************************
1197 void XGUI_Workshop::onFeatureTriggered()
1199 QAction* aCmd = dynamic_cast<QAction*>(sender());
1201 QString aId = salomeConnector()->commandId(aCmd);
1203 myModule->launchOperation(aId);
1207 //******************************************************
1208 void XGUI_Workshop::salomeViewerSelectionChanged()
1210 emit salomeViewerSelection();
1213 //**************************************************************
1214 ModuleBase_IViewer* XGUI_Workshop::salomeViewer() const
1216 return mySalomeConnector->viewer();
1219 //**************************************************************
1220 void XGUI_Workshop::onContextMenuCommand(const QString& theId, bool isChecked)
1222 QObjectPtrList aObjects = mySelector->selection()->selectedObjects();
1223 if (theId == "DELETE_CMD")
1225 else if (theId == "COLOR_CMD")
1226 changeColor(aObjects);
1227 else if (theId == "SHOW_CMD")
1228 showObjects(aObjects, true);
1229 else if (theId == "HIDE_CMD")
1230 showObjects(aObjects, false);
1231 else if (theId == "SHOW_ONLY_CMD")
1232 showOnlyObjects(aObjects);
1233 else if (theId == "SHADING_CMD")
1234 setDisplayMode(aObjects, XGUI_Displayer::Shading);
1235 else if (theId == "WIREFRAME_CMD")
1236 setDisplayMode(aObjects, XGUI_Displayer::Wireframe);
1237 else if (theId == "HIDEALL_CMD") {
1238 QObjectPtrList aList = myDisplayer->displayedObjects();
1239 foreach (ObjectPtr aObj, aList)
1240 aObj->setDisplayed(false);
1241 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
1245 //**************************************************************
1246 void XGUI_Workshop::deleteObjects()
1248 ModuleBase_IModule* aModule = module();
1249 // 1. allow the module to delete objects, do nothing if it has succeed
1250 if (aModule->deleteObjects()) {
1251 updateCommandStatus();
1255 if (!isActiveOperationAborted())
1257 QObjectPtrList anObjects = mySelector->selection()->selectedObjects();
1258 bool hasResult = false;
1259 bool hasFeature = false;
1260 bool hasParameter = false;
1261 ModuleBase_Tools::checkObjects(anObjects, hasResult, hasFeature, hasParameter);
1262 if (!(hasFeature || hasParameter))
1265 // 1. start operation
1266 QString aDescription = contextMenuMgr()->action("DELETE_CMD")->text();
1267 aDescription += tr(" %1");
1268 QStringList aObjectNames;
1269 foreach (ObjectPtr aObj, anObjects) {
1270 if (!aObj->data()->isValid())
1272 aObjectNames << QString::fromStdString(aObj->data()->name());
1274 aDescription = aDescription.arg(aObjectNames.join(", "));
1276 SessionPtr aMgr = ModelAPI_Session::get();
1277 aMgr->startOperation(aDescription.toStdString());
1278 // 2. close the documents of the removed parts if the result part is in a list of selected objects
1279 foreach (ObjectPtr aObj, anObjects)
1281 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObj);
1283 DocumentPtr aDoc = aObj->document();
1284 if (aDoc == aMgr->activeDocument()) {
1289 // 3. delete objects
1290 QMainWindow* aDesktop = isSalomeMode() ? salomeConnector()->desktop() : myMainWindow;
1291 std::set<FeaturePtr> anIgnoredFeatures;
1292 if (deleteFeatures(anObjects, anIgnoredFeatures, aDesktop, true)) {
1293 myDisplayer->updateViewer();
1294 aMgr->finishOperation();
1295 updateCommandStatus();
1298 aMgr->abortOperation();
1302 //**************************************************************
1303 bool XGUI_Workshop::deleteFeatures(const QObjectPtrList& theList,
1304 std::set<FeaturePtr> theIgnoredFeatures,
1306 const bool theAskAboutDeleteReferences)
1308 // 1. find all referenced features
1309 std::set<FeaturePtr> aRefFeatures;
1310 foreach (ObjectPtr aObj, theList) {
1311 FeaturePtr aFeature = ModelAPI_Feature::feature(aObj);
1312 if (aFeature.get() != NULL) {
1313 aObj->document()->refsToFeature(aFeature, aRefFeatures, false);
1316 // 2. warn about the references remove, break the delete operation if the user chose it
1317 if (theAskAboutDeleteReferences && !aRefFeatures.empty()) {
1318 QStringList aRefNames;
1319 std::set<FeaturePtr>::const_iterator anIt = aRefFeatures.begin(),
1320 aLast = aRefFeatures.end();
1321 for (; anIt != aLast; anIt++) {
1322 aRefNames.append((*anIt)->name().c_str());
1324 QString aNames = aRefNames.join(", ");
1326 QMessageBox::StandardButton aRes = QMessageBox::warning(
1327 theParent, tr("Delete features"),
1328 QString(tr("Selected features are used in the following features: %1.\
1329 These features will be deleted also. Would you like to continue?")).arg(aNames),
1330 QMessageBox::No | QMessageBox::Yes, QMessageBox::No);
1331 if (aRes != QMessageBox::Yes)
1335 // 3. remove referenced features
1336 std::set<FeaturePtr>::const_iterator anIt = aRefFeatures.begin(),
1337 aLast = aRefFeatures.end();
1338 for (; anIt != aLast; anIt++) {
1339 FeaturePtr aFeature = (*anIt);
1340 DocumentPtr aDoc = aFeature->document();
1341 if (theIgnoredFeatures.find(aFeature) == theIgnoredFeatures.end())
1342 aDoc->removeFeature(aFeature);
1345 // 4. remove the parameter features
1346 foreach (ObjectPtr aObj, theList) {
1347 FeaturePtr aFeature = ModelAPI_Feature::feature(aObj);
1349 DocumentPtr aDoc = aObj->document();
1350 if (theIgnoredFeatures.find(aFeature) == theIgnoredFeatures.end())
1351 aDoc->removeFeature(aFeature);
1357 bool hasResults(QObjectPtrList theObjects, const std::set<std::string>& theTypes)
1359 bool isFoundResultType = false;
1360 foreach(ObjectPtr anObj, theObjects)
1362 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObj);
1363 if (aResult.get() == NULL)
1366 isFoundResultType = theTypes.find(aResult->groupName()) != theTypes.end();
1367 if (isFoundResultType)
1370 return isFoundResultType;
1373 //**************************************************************
1374 bool XGUI_Workshop::canChangeColor() const
1376 QObjectPtrList aObjects = mySelector->selection()->selectedObjects();
1378 std::set<std::string> aTypes;
1379 aTypes.insert(ModelAPI_ResultGroup::group());
1380 aTypes.insert(ModelAPI_ResultConstruction::group());
1381 aTypes.insert(ModelAPI_ResultBody::group());
1382 aTypes.insert(ModelAPI_ResultPart::group());
1384 return hasResults(aObjects, aTypes);
1387 void setColor(ResultPtr theResult, std::vector<int>& theColor)
1389 if (!theResult.get())
1392 AttributeIntArrayPtr aColorAttr = theResult->data()->intArray(ModelAPI_Result::COLOR_ID());
1393 if (aColorAttr.get() != NULL) {
1394 if (!aColorAttr->size()) {
1395 aColorAttr->setSize(3);
1397 aColorAttr->setValue(0, theColor[0]);
1398 aColorAttr->setValue(1, theColor[1]);
1399 aColorAttr->setValue(2, theColor[2]);
1403 //**************************************************************
1404 void XGUI_Workshop::changeColor(const QObjectPtrList& theObjects)
1406 AttributeIntArrayPtr aColorAttr;
1407 // 1. find the current color of the object. This is a color of AIS presentation
1408 // The objects are iterated until a first valid color is found
1409 std::vector<int> aColor;
1410 foreach(ObjectPtr anObject, theObjects) {
1411 if (anObject->groupName() == ModelAPI_ResultPart::group()) {
1412 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(anObject);
1413 DocumentPtr aPartDoc = aPart->partDoc();
1414 // the document should be checked on null, because in opened document if the part
1415 // has not been activated yet, the part document is empty
1416 if (!aPartDoc.get()) {
1417 emit errorOccurred(QString::fromLatin1("Color can not be changed on a part with an empty document"));
1420 if (aPartDoc->size(ModelAPI_ResultBody::group()) > 0) {
1421 ObjectPtr aObject = aPartDoc->object(ModelAPI_ResultBody::group(), 0);
1422 ResultBodyPtr aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aObject);
1424 std::string aSection, aName, aDefault;
1425 aBody->colorConfigInfo(aSection, aName, aDefault);
1426 if (!aSection.empty() && !aName.empty()) {
1427 aColor = Config_PropManager::color(aSection, aName, aDefault);
1434 AISObjectPtr anAISObj = myDisplayer->getAISObject(anObject);
1435 if (anAISObj.get()) {
1437 anAISObj->getColor(aColor[0], aColor[1], aColor[2]);
1440 if (!aColor.empty())
1443 if (aColor.size() != 3)
1446 // 2. show the dialog to change the value
1447 XGUI_ColorDialog* aDlg = new XGUI_ColorDialog(mainWindow());
1448 aDlg->setColor(aColor);
1449 aDlg->move(QCursor::pos());
1450 bool isDone = aDlg->exec() == QDialog::Accepted;
1454 bool isRandomColor = aDlg->isRandomColor();
1456 // 3. abort the previous operation and start a new one
1457 SessionPtr aMgr = ModelAPI_Session::get();
1458 bool aWasOperation = aMgr->isOperation(); // keep this value
1459 if (!aWasOperation) {
1460 QString aDescription = contextMenuMgr()->action("COLOR_CMD")->text();
1461 aMgr->startOperation(aDescription.toStdString());
1464 // 4. set the value to all results
1465 foreach(ObjectPtr anObj, theObjects) {
1466 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObj);
1467 if (aResult.get() != NULL) {
1468 if (aResult->groupName() == ModelAPI_ResultPart::group()) {
1469 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aResult);
1470 DocumentPtr aPartDoc = aPart->partDoc();
1471 // the document should be checked on null, because in opened document if the part
1472 // has not been activated yet, the part document is empty
1473 if (aPartDoc.get()) {
1474 for (int i = 0; i < aPartDoc->size(ModelAPI_ResultBody::group()); i++) {
1475 ObjectPtr aObject = aPartDoc->object(ModelAPI_ResultBody::group(), i);
1476 ResultBodyPtr aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aObject);
1477 std::vector<int> aColorResult = aDlg->getColor();
1478 setColor(aBody, aColorResult);
1483 std::vector<int> aColorResult = aDlg->getColor();
1484 setColor(aResult, aColorResult);
1489 aMgr->finishOperation();
1490 updateCommandStatus();
1493 //**************************************************************
1494 void XGUI_Workshop::showObjects(const QObjectPtrList& theList, bool isVisible)
1496 foreach (ObjectPtr aObj, theList) {
1498 aObj->setDisplayed(true);
1499 //displayObject(aObj);
1501 aObj->setDisplayed(false);
1502 //myDisplayer->erase(aObj, false);
1505 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
1506 //myDisplayer->updateViewer();
1509 //**************************************************************
1510 void XGUI_Workshop::showOnlyObjects(const QObjectPtrList& theList)
1512 QObjectPtrList aList = myDisplayer->displayedObjects();
1513 foreach (ObjectPtr aObj, aList)
1514 aObj->setDisplayed(false);
1515 foreach (ObjectPtr aObj, theList)
1516 aObj->setDisplayed(true);
1517 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
1522 //**************************************************************
1523 void XGUI_Workshop::registerValidators() const
1525 SessionPtr aMgr = ModelAPI_Session::get();
1526 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
1529 //**************************************************************
1530 void XGUI_Workshop::displayAllResults()
1532 SessionPtr aMgr = ModelAPI_Session::get();
1533 DocumentPtr aRootDoc = aMgr->moduleDocument();
1534 displayDocumentResults(aRootDoc);
1535 for (int i = 0; i < aRootDoc->size(ModelAPI_ResultPart::group()); i++) {
1536 ObjectPtr aObject = aRootDoc->object(ModelAPI_ResultPart::group(), i);
1537 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObject);
1538 displayDocumentResults(aPart->partDoc());
1540 myDisplayer->updateViewer();
1543 //**************************************************************
1544 void XGUI_Workshop::displayDocumentResults(DocumentPtr theDoc)
1548 displayGroupResults(theDoc, ModelAPI_ResultConstruction::group());
1549 displayGroupResults(theDoc, ModelAPI_ResultBody::group());
1552 //**************************************************************
1553 void XGUI_Workshop::displayGroupResults(DocumentPtr theDoc, std::string theGroup)
1555 for (int i = 0; i < theDoc->size(theGroup); i++)
1556 theDoc->object(theGroup, i)->setDisplayed(true);
1557 //displayObject(theDoc->object(theGroup, i));
1558 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
1561 //**************************************************************
1562 void XGUI_Workshop::setDisplayMode(const QObjectPtrList& theList, int theMode)
1564 foreach(ObjectPtr aObj, theList) {
1565 myDisplayer->setDisplayMode(aObj, (XGUI_Displayer::DisplayMode)theMode, false);
1567 if (theList.size() > 0)
1568 myDisplayer->updateViewer();
1571 //**************************************************************
1572 void XGUI_Workshop::closeDocument()
1574 ModuleBase_Operation* anOperation = operationMgr()->currentOperation();
1575 while (anOperation) {
1576 anOperation->abort();
1577 anOperation = operationMgr()->currentOperation();
1579 myDisplayer->closeLocalContexts();
1580 myDisplayer->eraseAll();
1581 objectBrowser()->clearContent();
1583 SessionPtr aMgr = ModelAPI_Session::get();
1587 //**************************************************************
1588 bool XGUI_Workshop::displayObject(ObjectPtr theObj)
1590 if (!myModule->canDisplayObject(theObj))
1593 ResultBodyPtr aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(theObj);
1594 if (aBody.get() != NULL) {
1595 int aNb = myDisplayer->objectsCount();
1596 myDisplayer->display(theObj, false);
1599 } else if (!(myIsLoadingData || myPartActivating))
1600 myDisplayer->display(theObj, false);
1605 void XGUI_Workshop::addHistoryMenu(QObject* theObject, const char* theSignal, const char* theSlot)
1607 XGUI_HistoryMenu* aMenu = NULL;
1608 if (isSalomeMode()) {
1609 QAction* anAction = qobject_cast<QAction*>(theObject);
1612 aMenu = new XGUI_HistoryMenu(anAction);
1614 QToolButton* aButton = qobject_cast<QToolButton*>(theObject);
1615 aMenu = new XGUI_HistoryMenu(aButton);
1617 connect(this, theSignal, aMenu, SLOT(setHistory(const QList<ActionInfo>&)));
1618 connect(aMenu, SIGNAL(actionSelected(int)), this, theSlot);
1621 QList<ActionInfo> XGUI_Workshop::processHistoryList(const std::list<std::string>& theList) const
1623 QList<ActionInfo> aResult;
1624 std::list<std::string>::const_iterator it = theList.cbegin();
1625 for (; it != theList.cend(); it++) {
1626 QString anId = QString::fromStdString(*it);
1627 bool isEditing = anId.endsWith(ModuleBase_Operation::EditSuffix());
1629 anId.chop(ModuleBase_Operation::EditSuffix().size());
1632 QAction* aContextMenuAct = myContextMenuMgr->actionByName(anId);
1633 if (aContextMenuAct) {
1634 anInfo.initFrom(aContextMenuAct);
1636 anInfo = myActionsMgr->actionInfoById(anId);
1639 anInfo.text = anInfo.text.prepend("Modify ");