1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
3 //#include "XGUI_Constants.h"
4 #include "XGUI_Workshop.h"
6 #include "XGUI_ActionsMgr.h"
7 #include "XGUI_ColorDialog.h"
8 #include "XGUI_ContextMenuMgr.h"
9 #include "XGUI_Displayer.h"
10 #include "XGUI_ErrorDialog.h"
11 #include "XGUI_ErrorMgr.h"
12 #include "XGUI_ModuleConnector.h"
13 #include "XGUI_ObjectsBrowser.h"
14 #include "XGUI_OperationMgr.h"
15 #include "XGUI_PropertyPanel.h"
16 #include "XGUI_SalomeConnector.h"
17 #include "XGUI_Selection.h"
18 #include "XGUI_SelectionMgr.h"
19 #include "XGUI_Tools.h"
20 #include "XGUI_ViewerProxy.h"
21 #include "XGUI_WorkshopListener.h"
22 #include <XGUI_CustomPrs.h>
23 #include <XGUI_HistoryMenu.h>
24 #include <XGUI_QtEvents.h>
26 #include <AppElements_Button.h>
27 #include <AppElements_Command.h>
28 #include <AppElements_MainMenu.h>
29 #include <AppElements_MainWindow.h>
30 #include <AppElements_MenuGroupPanel.h>
31 #include <AppElements_Viewer.h>
32 #include <AppElements_Workbench.h>
34 #include <ModelAPI_AttributeDocRef.h>
35 #include <ModelAPI_AttributeIntArray.h>
36 #include <ModelAPI_Data.h>
37 #include <ModelAPI_Events.h>
38 #include <ModelAPI_Feature.h>
39 #include <ModelAPI_Object.h>
40 #include <ModelAPI_ResultBody.h>
41 #include <ModelAPI_ResultConstruction.h>
42 #include <ModelAPI_ResultGroup.h>
43 #include <ModelAPI_ResultParameter.h>
44 #include <ModelAPI_Session.h>
45 #include <ModelAPI_Validator.h>
46 #include <ModelAPI_ResultCompSolid.h>
47 #include <ModelAPI_Tools.h>
49 //#include <PartSetPlugin_Part.h>
51 #include <Events_Loop.h>
52 #include <Events_Error.h>
53 #include <Events_LongOp.h>
55 #include <ModuleBase_FilterFactory.h>
56 #include <ModuleBase_IModule.h>
57 #include <ModuleBase_IViewer.h>
58 #include <ModuleBase_Operation.h>
59 #include <ModuleBase_OperationDescription.h>
60 #include <ModuleBase_PageBase.h>
61 #include <ModuleBase_Preferences.h>
62 #include <ModuleBase_SelectionValidator.h>
63 #include <ModuleBase_Tools.h>
64 #include <ModuleBase_WidgetFactory.h>
66 #include <Config_Common.h>
67 #include <Config_FeatureMessage.h>
68 #include <Config_ModuleReader.h>
69 #include <Config_PointerMessage.h>
70 #include <Config_PropManager.h>
71 #include <Config_SelectionFilterMessage.h>
73 #include <SUIT_ResourceMgr.h>
75 #include <QApplication>
76 #include <QFileDialog>
77 #include <QMessageBox>
78 #include <QMdiSubWindow>
79 #include <QPushButton>
80 #include <QDockWidget>
85 #include <QToolButton>
87 #include <QDesktopWidget>
102 //#define DEBUG_DELETE
104 XGUI_Workshop::XGUI_Workshop(XGUI_SalomeConnector* theConnector)
106 myCurrentDir(QString()),
108 mySalomeConnector(theConnector),
113 myMainWindow = mySalomeConnector ? 0 : new AppElements_MainWindow();
116 SUIT_ResourceMgr* aResMgr = ModuleBase_Preferences::resourceMgr();
117 bool aCloc = aResMgr->booleanValue("language", "locale", true);
119 QLocale::setDefault( QLocale::c() );
121 QLocale::setDefault( QLocale::system() );
124 myDisplayer = new XGUI_Displayer(this);
126 mySelector = new XGUI_SelectionMgr(this);
127 //connect(mySelector, SIGNAL(selectionChanged()), this, SLOT(updateModuleCommands()));
129 myOperationMgr = new XGUI_OperationMgr(this, 0);
130 myActionsMgr = new XGUI_ActionsMgr(this);
131 myErrorDlg = new XGUI_ErrorDialog(QApplication::desktop());
132 myErrorMgr = new XGUI_ErrorMgr(this);
133 myContextMenuMgr = new XGUI_ContextMenuMgr(this);
134 connect(myContextMenuMgr, SIGNAL(actionTriggered(const QString&, bool)), this,
135 SLOT(onContextMenuCommand(const QString&, bool)));
137 myViewerProxy = new XGUI_ViewerProxy(this);
138 connect(myViewerProxy, SIGNAL(selectionChanged()),
139 myActionsMgr, SLOT(updateOnViewSelection()));
141 myModuleConnector = new XGUI_ModuleConnector(this);
143 ModuleBase_IWorkshop* aWorkshop = moduleConnector();
144 myOperationMgr->setWorkshop(aWorkshop);
146 myEventsListener = new XGUI_WorkshopListener(aWorkshop);
148 connect(myOperationMgr, SIGNAL(operationStarted(ModuleBase_Operation*)),
149 SLOT(onOperationStarted(ModuleBase_Operation*)));
150 connect(myOperationMgr, SIGNAL(operationResumed(ModuleBase_Operation*)),
151 SLOT(onOperationResumed(ModuleBase_Operation*)));
152 connect(myOperationMgr, SIGNAL(operationStopped(ModuleBase_Operation*)),
153 SLOT(onOperationStopped(ModuleBase_Operation*)));
154 connect(myOperationMgr, SIGNAL(operationCommitted(ModuleBase_Operation*)),
155 SLOT(onOperationCommitted(ModuleBase_Operation*)));
156 connect(myOperationMgr, SIGNAL(operationAborted(ModuleBase_Operation*)),
157 SLOT(onOperationAborted(ModuleBase_Operation*)));
158 connect(myOperationMgr, SIGNAL(validationStateChanged(bool)),
159 myErrorMgr, SLOT(onValidationStateChanged()));
162 connect(myMainWindow, SIGNAL(exitKeySequence()), SLOT(onExit()));
163 connect(this, SIGNAL(errorOccurred(const QString&)), myErrorDlg, SLOT(addError(const QString&)));
164 connect(myEventsListener, SIGNAL(errorOccurred(const QString&)),
165 myErrorDlg, SLOT(addError(const QString&)));
167 //Config_PropManager::registerProp("Visualization", "object_default_color", "Object color",
168 // Config_Prop::Color, "225,225,225");
170 Config_PropManager::registerProp("Visualization", "result_body_color", "Body color",
171 Config_Prop::Color, ModelAPI_ResultBody::DEFAULT_COLOR());
172 Config_PropManager::registerProp("Visualization", "result_group_color", "Group color",
173 Config_Prop::Color, ModelAPI_ResultGroup::DEFAULT_COLOR());
174 Config_PropManager::registerProp("Visualization", "result_construction_color", "Construction color",
175 Config_Prop::Color, ModelAPI_ResultConstruction::DEFAULT_COLOR());
176 Config_PropManager::registerProp("Visualization", "result_part_color", "Part color",
177 Config_Prop::Color, ModelAPI_ResultPart::DEFAULT_COLOR());
180 //******************************************************
181 XGUI_Workshop::~XGUI_Workshop(void)
186 //******************************************************
187 void XGUI_Workshop::startApplication()
191 Config_PropManager::registerProp("Plugins", "default_path", "Default Path",
192 Config_Prop::Directory, "");
194 //Initialize event listening
195 myEventsListener->initializeEventListening();
197 registerValidators();
199 // Calling of loadCustomProps before activating module is required
200 // by Config_PropManger to restore user-defined path to plugins
201 ModuleBase_Preferences::loadCustomProps();
204 myMainWindow->show();
205 updateCommandStatus();
210 emit applicationStarted();
213 void XGUI_Workshop::activateModule()
215 myModule->activateSelectionFilters();
217 connect(myDisplayer, SIGNAL(objectDisplayed(ObjectPtr, AISObjectPtr)),
218 myModule, SLOT(onObjectDisplayed(ObjectPtr, AISObjectPtr)));
219 connect(myDisplayer, SIGNAL(beforeObjectErase(ObjectPtr, AISObjectPtr)),
220 myModule, SLOT(onBeforeObjectErase(ObjectPtr, AISObjectPtr)));
222 myActionsMgr->update();
226 void XGUI_Workshop::deactivateModule()
228 myModule->deactivateSelectionFilters();
230 disconnect(myDisplayer, SIGNAL(objectDisplayed(ObjectPtr, AISObjectPtr)),
231 myModule, SLOT(onObjectDisplayed(ObjectPtr, AISObjectPtr)));
232 disconnect(myDisplayer, SIGNAL(beforeObjectErase(ObjectPtr, AISObjectPtr)),
233 myModule, SLOT(onBeforeObjectErase(ObjectPtr, AISObjectPtr)));
236 //******************************************************
237 void XGUI_Workshop::initMenu()
239 myContextMenuMgr->createActions();
241 if (isSalomeMode()) {
242 // Create only Undo, Redo commands
243 QAction* aAction = salomeConnector()->addDesktopCommand("UNDO_CMD", tr("Undo"),
244 tr("Undo last command"),
245 QIcon(":pictures/undo.png"),
246 QKeySequence::Undo, false, "MEN_DESK_EDIT");
247 QString aToolBarTitle = tr( "INF_DESK_TOOLBAR_STANDARD" );
248 salomeConnector()->addActionInToolbar( aAction,aToolBarTitle );
250 connect(aAction, SIGNAL(triggered(bool)), this, SLOT(onUndo()));
251 addHistoryMenu(aAction, SIGNAL(updateUndoHistory(const QList<ActionInfo>&)), SLOT(onUndo(int)));
253 aAction = salomeConnector()->addDesktopCommand("REDO_CMD", tr("Redo"), tr("Redo last command"),
254 QIcon(":pictures/redo.png"), QKeySequence::Redo,
255 false, "MEN_DESK_EDIT");
256 salomeConnector()->addActionInToolbar( aAction, aToolBarTitle );
258 connect(aAction, SIGNAL(triggered(bool)), this, SLOT(onRedo()));
259 addHistoryMenu(aAction, SIGNAL(updateRedoHistory(const QList<ActionInfo>&)), SLOT(onRedo(int)));
261 salomeConnector()->addDesktopMenuSeparator("MEN_DESK_EDIT");
262 aAction = salomeConnector()->addDesktopCommand("REBUILD_CMD", tr("Rebuild"), tr("Rebuild data objects"),
263 QIcon(":pictures/rebuild.png"), QKeySequence(),
264 false, "MEN_DESK_EDIT");
265 salomeConnector()->addActionInToolbar( aAction, aToolBarTitle );
267 connect(aAction, SIGNAL(triggered(bool)), this, SLOT(onRebuild()));
268 salomeConnector()->addDesktopMenuSeparator("MEN_DESK_EDIT");
270 aAction = salomeConnector()->addDesktopCommand("SAVEAS_CMD", tr("Export NewGeom..."), tr("Export the current document into a NewGeom file"),
271 QIcon(), QKeySequence(),
272 false, "MEN_DESK_FILE");
273 connect(aAction, SIGNAL(triggered(bool)), this, SLOT(onSaveAs()));
275 aAction = salomeConnector()->addDesktopCommand("OPEN_CMD", tr("Import NewGeom..."), tr("Import a NewGeom file"),
276 QIcon(), QKeySequence(),
277 false, "MEN_DESK_FILE");
278 connect(aAction, SIGNAL(triggered(bool)), this, SLOT(onOpen()));
279 salomeConnector()->addDesktopMenuSeparator("MEN_DESK_FILE");
283 // File commands group
284 AppElements_MenuGroupPanel* aGroup = myMainWindow->menuObject()->generalPage();
286 AppElements_Command* aCommand;
288 aCommand = aGroup->addFeature("SAVE_CMD", tr("Save"), tr("Save the document"),
289 QIcon(":pictures/save.png"), QKeySequence::Save);
290 aCommand->connectTo(this, SLOT(onSave()));
291 //aCommand->disable();
293 QString aUndoId = "UNDO_CMD";
294 aCommand = aGroup->addFeature(aUndoId, tr("Undo"), tr("Undo last command"),
295 QIcon(":pictures/undo.png"), QKeySequence::Undo);
296 aCommand->connectTo(this, SLOT(onUndo()));
297 AppElements_Button* aUndoButton = qobject_cast<AppElements_Button*>(aGroup->widget(aUndoId));
298 addHistoryMenu(aUndoButton,
299 SIGNAL(updateUndoHistory(const QList<ActionInfo>&)),
302 QString aRedoId = "REDO_CMD";
303 aCommand = aGroup->addFeature(aRedoId, tr("Redo"), tr("Redo last command"),
304 QIcon(":pictures/redo.png"), QKeySequence::Redo);
305 aCommand->connectTo(this, SLOT(onRedo()));
306 AppElements_Button* aRedoButton = qobject_cast<AppElements_Button*>(aGroup->widget(aRedoId));
307 addHistoryMenu(aRedoButton,
308 SIGNAL(updateRedoHistory(const QList<ActionInfo>&)),
311 aCommand = aGroup->addFeature("REBUILD_CMD", tr("Rebuild"), tr("Rebuild data objects"),
312 QIcon(":pictures/rebuild.png"), QKeySequence());
313 aCommand->connectTo(this, SLOT(onRebuild()));
315 aCommand = aGroup->addFeature("SAVEAS_CMD", tr("Save as..."), tr("Save the document into a file"),
316 QIcon(":pictures/save.png"), QKeySequence());
317 aCommand->connectTo(this, SLOT(onSaveAs()));
318 //aCommand->disable();
320 aCommand = aGroup->addFeature("OPEN_CMD", tr("Open..."), tr("Open a new document"),
321 QIcon(":pictures/open.png"), QKeySequence::Open);
322 aCommand->connectTo(this, SLOT(onOpen()));
324 //aCommand = aGroup->addFeature("NEW_CMD", tr("New"), tr("Create a new document"),
325 // QIcon(":pictures/new.png"), QKeySequence::New);
326 //aCommand->connectTo(this, SLOT(onNew()));
328 aCommand = aGroup->addFeature("PREF_CMD", tr("Preferences"), tr("Edit preferences"),
329 QIcon(":pictures/preferences.png"), QKeySequence::Preferences);
330 aCommand->connectTo(this, SLOT(onPreferences()));
332 aCommand = aGroup->addFeature("EXIT_CMD", tr("Exit"), tr("Exit application"),
333 QIcon(":pictures/close.png"), QKeySequence::Close);
334 aCommand->connectTo(this, SLOT(onExit()));
335 //FIXME: SBH's test action. Can be used for some GUI tests.
337 // aCommand = aGroup->addFeature("TEST_CMD", "Test!", "Private debug button",
338 // QIcon(":pictures/close.png"), QKeySequence(), true);
339 // aCommand->connectTo(myMainWindow, SLOT(dockPythonConsole()));
343 //******************************************************
344 AppElements_Workbench* XGUI_Workshop::addWorkbench(const QString& theName)
346 AppElements_MainMenu* aMenuBar = myMainWindow->menuObject();
347 return aMenuBar->addWorkbench(theName);
350 //******************************************************
351 QMainWindow* XGUI_Workshop::desktop() const
353 return isSalomeMode() ? salomeConnector()->desktop() : myMainWindow;
356 //******************************************************
357 void XGUI_Workshop::onStartWaiting()
359 if (Events_LongOp::isPerformed()) {
360 QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
365 //******************************************************
366 void XGUI_Workshop::deactivateActiveObject(const ObjectPtr& theObject, const bool theUpdateViewer)
368 if (!myModule->canActivateSelection(theObject)) {
369 if (myDisplayer->isActive(theObject))
370 myDisplayer->deactivate(theObject, theUpdateViewer);
374 //******************************************************
375 void XGUI_Workshop::onOperationStarted(ModuleBase_Operation* theOperation)
377 setNestedFeatures(theOperation);
379 if (theOperation->getDescription()->hasXmlRepresentation()) { //!< No need for property panel
380 setPropertyPanel(theOperation);
381 // filling the operation values by the current selection
382 // if the operation can be commited after the controls filling, the method perform should
383 // be stopped. Otherwise unnecessary presentations can be shown(e.g. operation prs in sketch)
384 if (!theOperation->isEditOperation()) {
385 theOperation->activateByPreselection();
386 if (operationMgr()->currentOperation() != theOperation)
390 updateCommandStatus();
392 myModule->onOperationStarted(theOperation);
394 // the objects of the current operation should be deactivated
395 QObjectPtrList anObjects;
396 FeaturePtr aFeature = theOperation->feature();
397 anObjects.append(aFeature);
398 std::list<ResultPtr> aResults = aFeature->results();
399 std::list<ResultPtr>::const_iterator aIt;
400 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
401 anObjects.append(*aIt);
403 QObjectPtrList::const_iterator anIt = anObjects.begin(), aLast = anObjects.end();
404 for (; anIt != aLast; anIt++)
405 deactivateActiveObject(*anIt, false);
406 if (anObjects.size() > 0)
407 myDisplayer->updateViewer();
410 //******************************************************
411 void XGUI_Workshop::onOperationResumed(ModuleBase_Operation* theOperation)
413 setNestedFeatures(theOperation);
415 if (theOperation->getDescription()->hasXmlRepresentation()) { //!< No need for property panel
416 setPropertyPanel(theOperation);
418 updateCommandStatus();
420 myModule->onOperationResumed(theOperation);
424 //******************************************************
425 void XGUI_Workshop::onOperationStopped(ModuleBase_Operation* theOperation)
427 ModuleBase_ISelection* aSel = mySelector->selection();
428 QObjectPtrList aObj = aSel->selectedPresentations();
429 //!< No need for property panel
430 updateCommandStatus();
432 myPropertyPanel->cleanContent();
434 myModule->onOperationStopped(theOperation);
436 // the deactivated objects of the current operation should be activated back.
437 // They were deactivated on operation start or an object redisplay
438 QObjectPtrList anObjects;
439 FeaturePtr aFeature = theOperation->feature();
440 if (myDisplayer->isVisible(aFeature) && !myDisplayer->isActive(aFeature))
441 anObjects.append(aFeature);
442 std::list<ResultPtr> aResults = aFeature->results();
443 std::list<ResultPtr>::const_iterator aIt;
444 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
445 ResultPtr anObject = *aIt;
446 if (myDisplayer->isVisible(anObject) && !myDisplayer->isActive(anObject)) {
447 anObjects.append(anObject);
451 module()->activeSelectionModes(aModes);
452 myDisplayer->activateObjects(aModes, anObjects);
456 void XGUI_Workshop::onOperationCommitted(ModuleBase_Operation* theOperation)
458 myModule->onOperationCommitted(theOperation);
461 void XGUI_Workshop::onOperationAborted(ModuleBase_Operation* theOperation)
463 myModule->onOperationAborted(theOperation);
466 void XGUI_Workshop::setNestedFeatures(ModuleBase_Operation* theOperation)
468 if (this->isSalomeMode())
469 theOperation->setNestedFeatures(mySalomeConnector->nestedActions(theOperation->id()));
471 theOperation->setNestedFeatures(myActionsMgr->nestedCommands(theOperation->id()));
474 void XGUI_Workshop::setPropertyPanel(ModuleBase_Operation* theOperation)
477 QString aXmlRepr = theOperation->getDescription()->xmlRepresentation();
478 ModuleBase_WidgetFactory aFactory = ModuleBase_WidgetFactory(aXmlRepr.toStdString(),
481 myPropertyPanel->cleanContent();
482 aFactory.createWidget(myPropertyPanel->contentWidget());
484 QList<ModuleBase_ModelWidget*> aWidgets = aFactory.getModelWidgets();
485 foreach (ModuleBase_ModelWidget* aWidget, aWidgets) {
486 bool isStoreValue = !theOperation->isEditOperation() &&
487 !aWidget->getDefaultValue().empty() &&
488 !aWidget->isComputedDefault();
489 aWidget->setFeature(theOperation->feature(), isStoreValue);
490 aWidget->enableFocusProcessing();
493 myPropertyPanel->setModelWidgets(aWidgets);
494 theOperation->setPropertyPanel(myPropertyPanel);
496 myModule->propertyPanelDefined(theOperation);
498 myPropertyPanel->setWindowTitle(theOperation->getDescription()->description());
500 myErrorMgr->setPropertyPanel(myPropertyPanel);
504 * Saves document with given name.
506 void XGUI_Workshop::saveDocument(const QString& theName, std::list<std::string>& theFileNames)
508 QApplication::restoreOverrideCursor();
509 SessionPtr aMgr = ModelAPI_Session::get();
510 aMgr->save(theName.toLatin1().constData(), theFileNames);
511 QApplication::restoreOverrideCursor();
514 bool XGUI_Workshop::isActiveOperationAborted()
516 return myOperationMgr->abortAllOperations();
519 //******************************************************
520 void XGUI_Workshop::onExit()
522 SessionPtr aMgr = ModelAPI_Session::get();
523 if (aMgr->isModified()) {
524 int anAnswer = QMessageBox::question(
525 myMainWindow, tr("Save current file"), tr("The document is modified, save before exit?"),
526 QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel, QMessageBox::Cancel);
527 if (anAnswer == QMessageBox::Save) {
528 bool saved = onSave();
532 } else if (anAnswer == QMessageBox::Cancel) {
539 //******************************************************
540 void XGUI_Workshop::onNew()
542 QApplication::setOverrideCursor(Qt::WaitCursor);
543 if (objectBrowser() == 0) {
545 mySelector->connectViewers();
547 myViewerProxy->connectToViewer();
549 if (!isSalomeMode()) {
550 myMainWindow->showPythonConsole();
551 QMdiSubWindow* aWnd = myMainWindow->viewer()->createView();
552 aWnd->showMaximized();
553 updateCommandStatus();
555 myContextMenuMgr->connectViewer();
556 QApplication::restoreOverrideCursor();
559 //******************************************************
560 void XGUI_Workshop::onOpen()
562 if(!isActiveOperationAborted())
564 //save current file before close if modified
565 SessionPtr aSession = ModelAPI_Session::get();
566 if (aSession->isModified()) {
567 //TODO(sbh): re-launch the app?
568 int anAnswer = QMessageBox::question(
569 myMainWindow, tr("Save current file"),
570 tr("The document is modified, save before opening another?"),
571 QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel, QMessageBox::Cancel);
572 if (anAnswer == QMessageBox::Save) {
574 } else if (anAnswer == QMessageBox::Cancel) {
580 //show file dialog, check if readable and open
581 myCurrentDir = QFileDialog::getExistingDirectory(mainWindow(), tr("Select directory"));
582 if (myCurrentDir.isEmpty())
584 QFileInfo aFileInfo(myCurrentDir);
585 if (!aFileInfo.exists() || !aFileInfo.isReadable()) {
586 QMessageBox::critical(myMainWindow, tr("Warning"), tr("Unable to open the file."));
590 QApplication::setOverrideCursor(Qt::WaitCursor);
591 aSession->closeAll();
592 aSession->load(myCurrentDir.toLatin1().constData());
593 myObjectBrowser->rebuildDataTree();
594 //displayAllResults();
595 updateCommandStatus();
596 QApplication::restoreOverrideCursor();
599 //******************************************************
600 bool XGUI_Workshop::onSave()
602 if(!isActiveOperationAborted())
604 if (myCurrentDir.isEmpty()) {
607 std::list<std::string> aFiles;
608 saveDocument(myCurrentDir, aFiles);
609 updateCommandStatus();
611 myMainWindow->setModifiedState(false);
615 //******************************************************
616 bool XGUI_Workshop::onSaveAs()
618 if(!isActiveOperationAborted())
620 QFileDialog dialog(mainWindow());
621 dialog.setWindowTitle(tr("Select directory to save files..."));
622 dialog.setFileMode(QFileDialog::Directory);
623 dialog.setFilter(tr("Directories (*)"));
624 dialog.setOptions(QFileDialog::HideNameFilterDetails | QFileDialog::ShowDirsOnly);
625 dialog.setViewMode(QFileDialog::Detail);
627 if (!dialog.exec()) {
631 QString aTempDir = dialog.selectedFiles().first();
633 if (aDir.exists() && !aDir.entryInfoList(QDir::NoDotAndDotDot | QDir::AllEntries).isEmpty()) {
634 int answer = QMessageBox::question(
636 //: Title of the dialog which asks user if he wants to save study in existing non-empty folder
638 tr("The directory already contains some files, save anyway?"),
639 QMessageBox::Save | QMessageBox::Cancel);
640 if (answer == QMessageBox::Cancel) {
644 myCurrentDir = aTempDir;
645 if (!isSalomeMode()) {
646 myMainWindow->setCurrentDir(myCurrentDir, false);
647 myMainWindow->setModifiedState(false);
652 //******************************************************
653 void XGUI_Workshop::onUndo(int theTimes)
655 objectBrowser()->treeView()->setCurrentIndex(QModelIndex());
656 SessionPtr aMgr = ModelAPI_Session::get();
657 if (aMgr->isOperation()) {
658 /// this is important for nested operrations
659 /// when sketch opeation is active, this condition is false and
660 /// the sketch operation is not aborted
661 operationMgr()->onAbortOperation();
663 for (int i = 0; i < theTimes; ++i) {
666 updateCommandStatus();
669 //******************************************************
670 void XGUI_Workshop::onRedo(int theTimes)
672 // the viewer update should be blocked in order to avoid the features blinking. For the created
673 // feature a results are created, the flush of the created signal caused the viewer redisplay for
674 // each created result. After a redisplay signal is flushed. So, the viewer update is blocked until
675 // redo of all possible objects happens
676 bool isUpdateEnabled = myDisplayer->enableUpdateViewer(false);
678 objectBrowser()->treeView()->setCurrentIndex(QModelIndex());
679 SessionPtr aMgr = ModelAPI_Session::get();
680 if (aMgr->isOperation()) {
681 /// this is important for nested operrations
682 /// when sketch opeation is active, this condition is false and
683 /// the sketch operation is not aborted
684 operationMgr()->onAbortOperation();
686 for (int i = 0; i < theTimes; ++i) {
689 updateCommandStatus();
691 // unblock the viewer update functionality and make update on purpose
692 myDisplayer->enableUpdateViewer(isUpdateEnabled);
693 myDisplayer->updateViewer();
696 //******************************************************
697 void XGUI_Workshop::onRebuild()
699 SessionPtr aMgr = ModelAPI_Session::get();
700 bool aWasOperation = aMgr->isOperation(); // keep this value
701 if (!aWasOperation) {
702 aMgr->startOperation("Rebuild");
704 static const Events_ID aRebuildEvent = Events_Loop::loop()->eventByName("Rebuild");
705 Events_Loop::loop()->send(std::shared_ptr<Events_Message>(
706 new Events_Message(aRebuildEvent, this)));
707 if (!aWasOperation) {
708 aMgr->finishOperation();
710 updateCommandStatus();
713 //******************************************************
714 void XGUI_Workshop::onPreferences()
716 ModuleBase_Prefs aModif;
717 ModuleBase_Preferences::editPreferences(aModif);
718 if (aModif.size() > 0) {
720 foreach (ModuleBase_Pref aPref, aModif)
722 aSection = aPref.first;
723 if (aSection == ModuleBase_Preferences::VIEWER_SECTION) {
725 myMainWindow->viewer()->updateFromResources();
726 } else if (aSection == ModuleBase_Preferences::MENU_SECTION) {
728 myMainWindow->menuObject()->updateFromResources();
734 //******************************************************
735 ModuleBase_IModule* XGUI_Workshop::loadModule(const QString& theModule)
737 QString libName = QString::fromStdString(library(theModule.toStdString()));
738 if (libName.isEmpty()) {
739 qWarning(qPrintable(tr("Information about module \"%1\" doesn't exist.").arg(theModule)));
744 CREATE_FUNC crtInst = 0;
747 HINSTANCE modLib = ::LoadLibrary((LPTSTR) qPrintable(libName));
751 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
752 0, ::GetLastError(), 0, (LPTSTR) & lpMsgBuf, 0, 0);
753 QString aMsg((char*) &lpMsgBuf);
754 err = QString("Failed to load %1. %2").arg(libName).arg(aMsg);
755 ::LocalFree(lpMsgBuf);
757 crtInst = (CREATE_FUNC) ::GetProcAddress(modLib, CREATE_MODULE);
761 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM
762 | FORMAT_MESSAGE_IGNORE_INSERTS,
763 0, ::GetLastError(), 0, (LPTSTR) & lpMsgBuf, 0, 0);
764 QString aMsg((char*) &lpMsgBuf);
765 err = QString("Failed to find %1 function. %2").arg( CREATE_MODULE).arg(aMsg);
766 ::LocalFree(lpMsgBuf);
770 void* modLib = dlopen( libName.toLatin1(), RTLD_LAZY | RTLD_GLOBAL );
772 err = QString( "Can not load library %1. %2" ).arg( libName ).arg( dlerror() );
774 crtInst = (CREATE_FUNC)dlsym( modLib, CREATE_MODULE );
776 err = QString( "Failed to find function %1. %2" ).arg( CREATE_MODULE ).arg( dlerror() );
781 ModuleBase_IModule* aModule = crtInst ? crtInst(myModuleConnector) : 0;
783 if (!err.isEmpty()) {
785 Events_Error::send(err.toStdString());
787 qWarning(qPrintable(err));
793 //******************************************************
794 bool XGUI_Workshop::createModule()
796 Config_ModuleReader aModuleReader;
797 QString moduleName = QString::fromStdString(aModuleReader.getModuleName());
798 myModule = loadModule(moduleName);
802 //connect(myDisplayer, SIGNAL(objectDisplayed(ObjectPtr, AISObjectPtr)),
803 // myModule, SLOT(onObjectDisplayed(ObjectPtr, AISObjectPtr)));
804 //connect(myDisplayer, SIGNAL(beforeObjectErase(ObjectPtr, AISObjectPtr)),
805 // myModule, SLOT(onBeforeObjectErase(ObjectPtr, AISObjectPtr)));
807 myModule->createFeatures();
808 //myActionsMgr->update();
812 //******************************************************
813 void XGUI_Workshop::updateCommandStatus()
815 QList<QAction*> aCommands;
816 if (isSalomeMode()) { // update commands in SALOME mode
817 aCommands = salomeConnector()->commandList();
819 AppElements_MainMenu* aMenuBar = myMainWindow->menuObject();
820 foreach (AppElements_Command* aCmd, aMenuBar->features())
821 aCommands.append(aCmd);
823 SessionPtr aMgr = ModelAPI_Session::get();
824 if (aMgr->hasModuleDocument()) {
825 foreach(QAction* aCmd, aCommands) {
826 QString aId = aCmd->data().toString();
827 if (aId == "UNDO_CMD")
828 aCmd->setEnabled(myModule->canUndo());
829 else if (aId == "REDO_CMD")
830 aCmd->setEnabled(myModule->canRedo());
832 // Enable all commands
833 aCmd->setEnabled(true);
837 foreach(QAction* aCmd, aCommands) {
838 QString aId = aCmd->data().toString();
839 if (aId == "NEW_CMD")
840 aCmd->setEnabled(true);
841 else if (aId == "EXIT_CMD")
842 aCmd->setEnabled(true);
844 aCmd->setEnabled(false);
847 myActionsMgr->update();
848 emit commandStatusUpdated();
851 void XGUI_Workshop::updateHistory()
853 std::list<std::string> aUndoList = ModelAPI_Session::get()->undoList();
854 QList<ActionInfo> aUndoRes = processHistoryList(aUndoList);
855 emit updateUndoHistory(aUndoRes);
857 std::list<std::string> aRedoList = ModelAPI_Session::get()->redoList();
858 QList<ActionInfo> aRedoRes = processHistoryList(aRedoList);
859 emit updateRedoHistory(aRedoRes);
862 //******************************************************
863 QDockWidget* XGUI_Workshop::createObjectBrowser(QWidget* theParent)
865 QDockWidget* aObjDock = new QDockWidget(theParent);
866 aObjDock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea | Qt::BottomDockWidgetArea);
867 aObjDock->setWindowTitle(tr("Object browser"));
868 aObjDock->setStyleSheet(
869 "::title { position: relative; padding-left: 5px; text-align: left center }");
870 myObjectBrowser = new XGUI_ObjectsBrowser(aObjDock);
871 #ifdef ModuleDataModel
872 myObjectBrowser->setDataModel(myModule->dataModel());
874 myModule->customizeObjectBrowser(myObjectBrowser);
875 aObjDock->setWidget(myObjectBrowser);
877 myContextMenuMgr->connectObjectBrowser();
881 //******************************************************
883 * Creates dock widgets, places them in corresponding area
884 * and tabifies if necessary.
886 void XGUI_Workshop::createDockWidgets()
888 QMainWindow* aDesktop = isSalomeMode() ? salomeConnector()->desktop() : myMainWindow;
889 QDockWidget* aObjDock = createObjectBrowser(aDesktop);
890 aDesktop->addDockWidget(Qt::LeftDockWidgetArea, aObjDock);
891 myPropertyPanel = new XGUI_PropertyPanel(aDesktop);
892 myPropertyPanel->setupActions(myActionsMgr);
893 myPropertyPanel->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea | Qt::BottomDockWidgetArea);
894 aDesktop->addDockWidget(Qt::LeftDockWidgetArea, myPropertyPanel);
895 hidePropertyPanel(); ///<! Invisible by default
897 aDesktop->tabifyDockWidget(aObjDock, myPropertyPanel);
898 myPropertyPanel->installEventFilter(myOperationMgr);
900 QAction* aOkAct = myActionsMgr->operationStateAction(XGUI_ActionsMgr::Accept);
901 connect(aOkAct, SIGNAL(triggered()), myOperationMgr, SLOT(onCommitOperation()));
902 QAction* aCancelAct = myActionsMgr->operationStateAction(XGUI_ActionsMgr::Abort);
903 connect(aCancelAct, SIGNAL(triggered()), myOperationMgr, SLOT(onAbortOperation()));
904 connect(myPropertyPanel, SIGNAL(noMoreWidgets()), myModule, SLOT(onNoMoreWidgets()));
905 connect(myPropertyPanel, SIGNAL(keyReleased(QKeyEvent*)),
906 myOperationMgr, SLOT(onKeyReleased(QKeyEvent*)));
907 connect(myOperationMgr, SIGNAL(validationStateChanged(bool)),
908 aOkAct, SLOT(setEnabled(bool)));
909 QAction* aAcceptAllAct = myActionsMgr->operationStateAction(XGUI_ActionsMgr::AcceptAll);
910 connect(myOperationMgr, SIGNAL(nestedStateChanged(bool)),
911 aAcceptAllAct, SLOT(setEnabled(bool)));
915 //******************************************************
916 void XGUI_Workshop::showPropertyPanel()
918 QAction* aViewAct = myPropertyPanel->toggleViewAction();
919 ///<! Restore ability to close panel from the window's menu
920 aViewAct->setEnabled(true);
921 myPropertyPanel->show();
922 myPropertyPanel->raise();
925 //******************************************************
926 void XGUI_Workshop::hidePropertyPanel()
928 QAction* aViewAct = myPropertyPanel->toggleViewAction();
929 ///<! Do not allow to show empty property panel
930 aViewAct->setEnabled(false);
931 myPropertyPanel->hide();
934 //******************************************************
935 void XGUI_Workshop::showObjectBrowser()
937 myObjectBrowser->parentWidget()->show();
940 //******************************************************
941 void XGUI_Workshop::hideObjectBrowser()
943 myObjectBrowser->parentWidget()->hide();
946 //******************************************************
947 void XGUI_Workshop::salomeViewerSelectionChanged()
949 emit salomeViewerSelection();
952 //**************************************************************
953 ModuleBase_IViewer* XGUI_Workshop::salomeViewer() const
955 return mySalomeConnector->viewer();
958 //**************************************************************
959 void XGUI_Workshop::onContextMenuCommand(const QString& theId, bool isChecked)
961 QObjectPtrList aObjects = mySelector->selection()->selectedObjects();
962 if (theId == "DELETE_CMD")
964 else if (theId == "MOVE_CMD")
966 else if (theId == "COLOR_CMD")
967 changeColor(aObjects);
968 else if (theId == "SHOW_CMD")
969 showObjects(aObjects, true);
970 else if (theId == "HIDE_CMD")
971 showObjects(aObjects, false);
972 else if (theId == "SHOW_ONLY_CMD")
973 showOnlyObjects(aObjects);
974 else if (theId == "SHADING_CMD")
975 setDisplayMode(aObjects, XGUI_Displayer::Shading);
976 else if (theId == "WIREFRAME_CMD")
977 setDisplayMode(aObjects, XGUI_Displayer::Wireframe);
978 else if (theId == "HIDEALL_CMD") {
979 QObjectPtrList aList = myDisplayer->displayedObjects();
980 foreach (ObjectPtr aObj, aList)
981 aObj->setDisplayed(false);
982 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
986 //**************************************************************
987 void XGUI_Workshop::deleteObjects()
989 ModuleBase_IModule* aModule = module();
990 // 1. allow the module to delete objects, do nothing if it has succeed
991 if (aModule->deleteObjects()) {
992 updateCommandStatus();
996 if (!isActiveOperationAborted())
998 QObjectPtrList anObjects = mySelector->selection()->selectedObjects();
999 // It is necessary to clear selection in order to avoid selection changed event during
1000 // deleteion and negative consequences connected with processing of already deleted items
1001 mySelector->clearSelection();
1002 // check whether the object can be deleted. There should not be parts which are not loaded
1003 if (!XGUI_Tools::canRemoveOrRename(myMainWindow, anObjects))
1006 bool hasResult = false;
1007 bool hasFeature = false;
1008 bool hasParameter = false;
1009 bool hasSubFeature = false;
1010 ModuleBase_Tools::checkObjects(anObjects, hasResult, hasFeature, hasParameter, hasSubFeature);
1011 if (!(hasFeature || hasParameter))
1014 // 1. start operation
1015 QString aDescription = contextMenuMgr()->action("DELETE_CMD")->text();
1016 aDescription += tr(" %1");
1017 QStringList aObjectNames;
1018 foreach (ObjectPtr aObj, anObjects) {
1019 if (!aObj->data()->isValid())
1021 aObjectNames << QString::fromStdString(aObj->data()->name());
1023 aDescription = aDescription.arg(aObjectNames.join(", "));
1025 SessionPtr aMgr = ModelAPI_Session::get();
1026 aMgr->startOperation(aDescription.toStdString());
1027 // 2. close the documents of the removed parts if the result part is in a list of selected objects
1028 // this is performed in the RemoveFeature of Part object.
1029 /*foreach (ObjectPtr aObj, anObjects)
1031 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObj);
1033 DocumentPtr aDoc = aObj->document();
1034 if (aDoc == aMgr->activeDocument()) {
1039 // 3. delete objects
1040 QMainWindow* aDesktop = isSalomeMode() ? salomeConnector()->desktop() : myMainWindow;
1041 std::set<FeaturePtr> anIgnoredFeatures;
1042 if (deleteFeatures(anObjects, anIgnoredFeatures, aDesktop, true)) {
1043 myDisplayer->updateViewer();
1044 aMgr->finishOperation();
1045 updateCommandStatus();
1048 aMgr->abortOperation();
1052 //**************************************************************
1053 void XGUI_Workshop::moveObjects()
1055 if (!isActiveOperationAborted())
1058 SessionPtr aMgr = ModelAPI_Session::get();
1060 QString anActionId = "MOVE_CMD";
1061 QString aDescription = contextMenuMgr()->action(anActionId)->text();
1062 aMgr->startOperation(aDescription.toStdString());
1064 QObjectPtrList anObjects = mySelector->selection()->selectedObjects();
1065 DocumentPtr anActiveDocument = aMgr->activeDocument();
1067 FeaturePtr aCurrentFeature = anActiveDocument->currentFeature(true);
1068 foreach (ObjectPtr aObject, anObjects) {
1069 if (!myModule->canApplyAction(aObject, anActionId))
1072 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObject);
1073 if (aFeature.get()) {
1074 anActiveDocument->moveFeature(aFeature, aCurrentFeature);
1075 aCurrentFeature = anActiveDocument->currentFeature(true);
1078 aMgr->finishOperation();
1081 //**************************************************************
1082 bool XGUI_Workshop::deleteFeatures(const QObjectPtrList& theList,
1083 const std::set<FeaturePtr>& theIgnoredFeatures,
1085 const bool theAskAboutDeleteReferences)
1089 QObjectPtrList::const_iterator aDIt = theList.begin(), aDLast = theList.end();
1090 for (; aDIt != aDLast; ++aDIt) {
1091 aDInfo.append(ModuleBase_Tools::objectInfo((*aDIt)));
1093 QString anInfoStr = aDInfo.join(", ");
1094 qDebug(QString("deleteFeatures: %1, %2").arg(theList.size()).arg(anInfoStr).toStdString().c_str());
1097 // 1. find all referenced features
1098 std::set<FeaturePtr> aDirectRefFeatures, aIndirectRefFeatures;
1099 foreach (ObjectPtr aDeletedObj, theList) {
1100 XGUI_Tools::refsToFeatureInAllDocuments(aDeletedObj, aDeletedObj, aDirectRefFeatures, aIndirectRefFeatures);
1101 std::set<FeaturePtr> aDifference;
1102 std::set_difference(aIndirectRefFeatures.begin(), aIndirectRefFeatures.end(),
1103 aDirectRefFeatures.begin(), aDirectRefFeatures.end(),
1104 std::inserter(aDifference, aDifference.begin()));
1105 aIndirectRefFeatures = aDifference;
1107 // 2. warn about the references remove, break the delete operation if the user chose it
1108 if (theAskAboutDeleteReferences && !aDirectRefFeatures.empty()) {
1109 QStringList aDirectRefNames;
1110 foreach(const FeaturePtr& aFeature, aDirectRefFeatures)
1111 aDirectRefNames.append(aFeature->name().c_str());
1112 QString aDirectNames = aDirectRefNames.join(", ");
1114 QStringList aIndirectRefNames;
1115 foreach(const FeaturePtr& aFeature, aIndirectRefFeatures)
1116 aIndirectRefNames.append(aFeature->name().c_str());
1117 QString aIndirectNames = aIndirectRefNames.join(", ");
1119 QMessageBox::StandardButton aRes = QMessageBox::warning(
1120 theParent, tr("Delete features"),
1121 QString(tr("Selected features are used in the following features: %1.\
1122 These features will be deleted.\n%2Would you like to continue?")).arg(aDirectNames)
1123 .arg(aIndirectNames.isEmpty() ? QString() : QString("Also these features will be deleted: %1.\n").arg(aIndirectNames)),
1124 QMessageBox::No | QMessageBox::Yes, QMessageBox::No);
1125 if (aRes != QMessageBox::Yes)
1129 // 3. remove referenced features
1130 std::set<FeaturePtr> aFeaturesToDelete = aDirectRefFeatures;
1131 aFeaturesToDelete.insert(aIndirectRefFeatures.begin(), aIndirectRefFeatures.end());
1132 std::set<FeaturePtr>::const_iterator anIt = aFeaturesToDelete.begin(),
1133 aLast = aFeaturesToDelete.end();
1137 for (; anIt != aLast; anIt++) {
1138 FeaturePtr aFeature = (*anIt);
1139 DocumentPtr aDoc = aFeature->document();
1140 if (theIgnoredFeatures.find(aFeature) == theIgnoredFeatures.end()) {
1141 aDoc->removeFeature(aFeature);
1143 anInfo.append(ModuleBase_Tools::objectInfo(aFeature).toStdString().c_str());
1148 qDebug(QString("remove references:%1").arg(anInfo.join("; ")).toStdString().c_str());
1152 QString anActionId = "DELETE_CMD";
1153 QString anId = QString::fromStdString(anActionId.toStdString().c_str());
1154 QStringList anObjectGroups = contextMenuMgr()->actionObjectGroups(anId);
1155 // 4. remove the parameter features
1156 foreach (ObjectPtr aObj, theList) {
1157 // features and parameters can be removed here,
1158 // the results are removed only by a corresponded feature remove
1159 std::string aGroupName = aObj->groupName();
1160 if (!anObjectGroups.contains(aGroupName.c_str()))
1163 if (!myModule->canApplyAction(aObj, anActionId))
1166 FeaturePtr aFeature = ModelAPI_Feature::feature(aObj);
1168 /*// TODO: to learn the workshop to delegate the Part object deletion to the PartSet module
1169 // part features are removed in the PartSet module. This condition should be moved there
1170 if (aFeature->getKind() == "Part")
1173 DocumentPtr aDoc = aObj->document();
1174 if (theIgnoredFeatures.find(aFeature) == theIgnoredFeatures.end()) {
1176 QString anInfoStr = ModuleBase_Tools::objectInfo(aFeature);
1177 anInfo.append(anInfoStr);
1178 qDebug(QString("remove feature :%1").arg(anInfoStr).toStdString().c_str());
1180 aDoc->removeFeature(aFeature);
1185 qDebug(QString("remove features:%1").arg(anInfo.join("; ")).toStdString().c_str());
1190 bool hasResults(QObjectPtrList theObjects, const std::set<std::string>& theTypes)
1192 bool isFoundResultType = false;
1193 foreach(ObjectPtr anObj, theObjects)
1195 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObj);
1196 if (aResult.get() == NULL)
1199 isFoundResultType = theTypes.find(aResult->groupName()) != theTypes.end();
1200 if (isFoundResultType)
1203 return isFoundResultType;
1206 //**************************************************************
1207 // Returns the list of features placed between theObject and the current feature
1208 // in the same document. Excludes theObject, includes the current feature.
1209 std::list<FeaturePtr> toCurrentFeatures(const ObjectPtr& theObject)
1211 std::list<FeaturePtr> aResult;
1212 DocumentPtr aDocument = theObject->document();
1213 std::list<FeaturePtr> anAllFeatures = aDocument->allFeatures();
1214 // find the object iterator
1215 std::list<FeaturePtr>::iterator aObjectIt = std::find(anAllFeatures.begin(), anAllFeatures.end(), theObject);
1216 if (aObjectIt == anAllFeatures.end())
1218 // find the current feature iterator
1219 std::list<FeaturePtr>::iterator aCurrentIt = std::find(anAllFeatures.begin(), anAllFeatures.end(), aDocument->currentFeature(true));
1220 if (aCurrentIt == anAllFeatures.end())
1222 // check the right order
1223 if (std::distance(aObjectIt, anAllFeatures.end()) <= std::distance(aCurrentIt, anAllFeatures.end()))
1225 // exclude the object
1226 std::advance(aObjectIt, 1);
1227 // include the current feature
1228 std::advance(aCurrentIt, 1);
1229 return std::list<FeaturePtr>(aObjectIt, aCurrentIt);
1232 bool XGUI_Workshop::canMoveFeature()
1234 QString anActionId = "MOVE_CMD";
1236 QObjectPtrList aObjects = mySelector->selection()->selectedObjects();
1237 QObjectPtrList aValidatedObjects;
1238 foreach (ObjectPtr aObject, aObjects) {
1239 if (myModule->canApplyAction(aObject, anActionId))
1240 aValidatedObjects.append(aObject);
1242 if (aValidatedObjects.size() != aObjects.size())
1243 aObjects = aValidatedObjects;
1245 bool aCanMove = !aObjects.empty();
1247 QObjectPtrList::const_iterator anIt = aObjects.begin(), aLast = aObjects.end();
1248 for (; anIt != aLast && aCanMove; anIt++) {
1249 ObjectPtr aObject = *anIt;
1250 // 1. Get features placed between selected and current in the document
1251 std::list<FeaturePtr> aFeaturesBetween = toCurrentFeatures(aObject);
1252 // if aFeaturesBetween is empty it means wrong order or aObject is the current feature
1253 if (aFeaturesBetween.empty())
1256 std::set<FeaturePtr> aPlacedFeatures(aFeaturesBetween.begin(), aFeaturesBetween.end());
1257 // 2. Get all reference features to the selected object in the document
1258 std::set<FeaturePtr> aRefFeatures;
1259 XGUI_Tools::refsToFeatureInFeatureDocument(aObject, aRefFeatures);
1261 if (aRefFeatures.empty())
1264 // 3. Find any placed features in all reference features
1265 std::set<FeaturePtr> aIntersectionFeatures;
1266 std::set_intersection(aRefFeatures.begin(), aRefFeatures.end(),
1267 aPlacedFeatures.begin(), aPlacedFeatures.end(),
1268 std::inserter(aIntersectionFeatures, aIntersectionFeatures.begin()));
1269 // 4. Return false if any reference feature is placed before curent feature
1270 if (!aIntersectionFeatures.empty())
1278 //**************************************************************
1279 bool XGUI_Workshop::canBeShaded(const ObjectPtr& theObject) const
1281 bool aCanBeShaded = myDisplayer->canBeShaded(theObject);
1282 if (!aCanBeShaded) {
1283 ResultCompSolidPtr aCompsolidResult =
1284 std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(theObject);
1285 if (aCompsolidResult.get() != NULL) { // change colors for all sub-solids
1286 for(int i = 0; i < aCompsolidResult->numberOfSubs() && !aCanBeShaded; i++)
1287 aCanBeShaded = myDisplayer->canBeShaded(aCompsolidResult->subResult(i));
1290 return aCanBeShaded;
1293 //**************************************************************
1294 bool XGUI_Workshop::canChangeColor() const
1296 QObjectPtrList aObjects = mySelector->selection()->selectedObjects();
1298 std::set<std::string> aTypes;
1299 aTypes.insert(ModelAPI_ResultGroup::group());
1300 aTypes.insert(ModelAPI_ResultConstruction::group());
1301 aTypes.insert(ModelAPI_ResultBody::group());
1302 aTypes.insert(ModelAPI_ResultPart::group());
1304 return hasResults(aObjects, aTypes);
1307 void setColor(ResultPtr theResult, std::vector<int>& theColor)
1309 if (!theResult.get())
1312 AttributeIntArrayPtr aColorAttr = theResult->data()->intArray(ModelAPI_Result::COLOR_ID());
1313 if (aColorAttr.get() != NULL) {
1314 if (!aColorAttr->size()) {
1315 aColorAttr->setSize(3);
1317 aColorAttr->setValue(0, theColor[0]);
1318 aColorAttr->setValue(1, theColor[1]);
1319 aColorAttr->setValue(2, theColor[2]);
1323 //**************************************************************
1324 void XGUI_Workshop::changeColor(const QObjectPtrList& theObjects)
1326 AttributeIntArrayPtr aColorAttr;
1327 // 1. find the current color of the object. This is a color of AIS presentation
1328 // The objects are iterated until a first valid color is found
1329 std::vector<int> aColor;
1330 foreach(ObjectPtr anObject, theObjects) {
1331 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
1332 if (aResult.get()) {
1333 XGUI_CustomPrs::getResultColor(aResult, aColor);
1336 // TODO: remove the obtaining a color from the AIS object
1337 // this does not happen never because:
1338 // 1. The color can be changed only on results
1339 // 2. The result can be not visualized in the viewer(e.g. Origin Construction)
1340 AISObjectPtr anAISObj = myDisplayer->getAISObject(anObject);
1341 if (anAISObj.get()) {
1343 anAISObj->getColor(aColor[0], aColor[1], aColor[2]);
1346 if (!aColor.empty())
1349 if (aColor.size() != 3)
1352 // 2. show the dialog to change the value
1353 XGUI_ColorDialog* aDlg = new XGUI_ColorDialog(mainWindow());
1354 aDlg->setColor(aColor);
1355 aDlg->move(QCursor::pos());
1356 bool isDone = aDlg->exec() == QDialog::Accepted;
1360 bool isRandomColor = aDlg->isRandomColor();
1362 // 3. abort the previous operation and start a new one
1363 SessionPtr aMgr = ModelAPI_Session::get();
1364 bool aWasOperation = aMgr->isOperation(); // keep this value
1365 if (!aWasOperation) {
1366 QString aDescription = contextMenuMgr()->action("COLOR_CMD")->text();
1367 aMgr->startOperation(aDescription.toStdString());
1370 // 4. set the value to all results
1371 std::vector<int> aColorResult = aDlg->getColor();
1372 foreach(ObjectPtr anObj, theObjects) {
1373 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObj);
1374 if (aResult.get() != NULL) {
1375 ResultCompSolidPtr aCompsolidResult = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(aResult);
1376 if (aCompsolidResult.get() != NULL) { // change colors for all sub-solids
1377 for(int i = 0; i < aCompsolidResult->numberOfSubs(); i++)
1378 setColor(aCompsolidResult->subResult(i), aColorResult);
1380 setColor(aResult, aColorResult);
1384 aMgr->finishOperation();
1385 updateCommandStatus();
1388 //**************************************************************
1389 #define SET_DISPLAY_GROUP(aGroupName, aDisplay) \
1390 for (int i = 0; i < aDoc->size(aGroupName); i++) { \
1391 aDoc->object(aGroupName, i)->setDisplayed(aDisplay); \
1393 void XGUI_Workshop::showObjects(const QObjectPtrList& theList, bool isVisible)
1395 foreach (ObjectPtr aObj, theList) {
1397 ResultPartPtr aPartRes = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObj);
1399 DocumentPtr aDoc = aPartRes->partDoc();
1400 SET_DISPLAY_GROUP(ModelAPI_ResultBody::group(), isVisible)
1401 SET_DISPLAY_GROUP(ModelAPI_ResultConstruction::group(), isVisible)
1402 SET_DISPLAY_GROUP(ModelAPI_ResultGroup::group(), isVisible)
1405 aObj->setDisplayed(isVisible);
1408 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
1411 //**************************************************************
1412 void XGUI_Workshop::showOnlyObjects(const QObjectPtrList& theList)
1414 // Hide all displayed objects
1415 QObjectPtrList aList = myDisplayer->displayedObjects();
1416 foreach (ObjectPtr aObj, aList)
1417 aObj->setDisplayed(false);
1419 // Show only objects from the list
1420 foreach (ObjectPtr aObj, theList) {
1422 ResultPartPtr aPartRes = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObj);
1424 DocumentPtr aDoc = aPartRes->partDoc();
1425 SET_DISPLAY_GROUP(ModelAPI_ResultBody::group(), true)
1426 SET_DISPLAY_GROUP(ModelAPI_ResultConstruction::group(), true)
1427 SET_DISPLAY_GROUP(ModelAPI_ResultGroup::group(), true)
1430 aObj->setDisplayed(true);
1433 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
1438 //**************************************************************
1439 void XGUI_Workshop::registerValidators() const
1441 SessionPtr aMgr = ModelAPI_Session::get();
1442 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
1445 //**************************************************************
1446 /*void XGUI_Workshop::displayAllResults()
1448 SessionPtr aMgr = ModelAPI_Session::get();
1449 DocumentPtr aRootDoc = aMgr->moduleDocument();
1450 displayDocumentResults(aRootDoc);
1451 for (int i = 0; i < aRootDoc->size(ModelAPI_ResultPart::group()); i++) {
1452 ObjectPtr aObject = aRootDoc->object(ModelAPI_ResultPart::group(), i);
1453 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObject);
1454 displayDocumentResults(aPart->partDoc());
1456 myDisplayer->updateViewer();
1459 //**************************************************************
1460 void XGUI_Workshop::displayDocumentResults(DocumentPtr theDoc)
1464 displayGroupResults(theDoc, ModelAPI_ResultConstruction::group());
1465 displayGroupResults(theDoc, ModelAPI_ResultBody::group());
1468 //**************************************************************
1469 void XGUI_Workshop::displayGroupResults(DocumentPtr theDoc, std::string theGroup)
1471 for (int i = 0; i < theDoc->size(theGroup); i++)
1472 theDoc->object(theGroup, i)->setDisplayed(true);
1473 //displayObject(theDoc->object(theGroup, i));
1474 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
1477 //**************************************************************
1478 void XGUI_Workshop::setDisplayMode(const QObjectPtrList& theList, int theMode)
1480 foreach(ObjectPtr aObj, theList) {
1481 myDisplayer->setDisplayMode(aObj, (XGUI_Displayer::DisplayMode)theMode, false);
1483 ResultCompSolidPtr aCompsolidResult = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(aObj);
1484 if (aCompsolidResult.get() != NULL) { // change colors for all sub-solids
1485 for(int i = 0; i < aCompsolidResult->numberOfSubs(); i++) {
1486 myDisplayer->setDisplayMode(aCompsolidResult->subResult(i),
1487 (XGUI_Displayer::DisplayMode)theMode, false);
1491 if (theList.size() > 0)
1492 myDisplayer->updateViewer();
1495 //**************************************************************
1496 void XGUI_Workshop::closeDocument()
1498 ModuleBase_Operation* anOperation = operationMgr()->currentOperation();
1499 while (anOperation) {
1500 anOperation->abort();
1501 anOperation = operationMgr()->currentOperation();
1503 myDisplayer->closeLocalContexts();
1504 myDisplayer->eraseAll();
1505 objectBrowser()->clearContent();
1507 SessionPtr aMgr = ModelAPI_Session::get();
1511 void XGUI_Workshop::addHistoryMenu(QObject* theObject, const char* theSignal, const char* theSlot)
1513 XGUI_HistoryMenu* aMenu = NULL;
1514 if (isSalomeMode()) {
1515 QAction* anAction = qobject_cast<QAction*>(theObject);
1518 aMenu = new XGUI_HistoryMenu(anAction);
1520 QToolButton* aButton = qobject_cast<QToolButton*>(theObject);
1521 aMenu = new XGUI_HistoryMenu(aButton);
1523 connect(this, theSignal, aMenu, SLOT(setHistory(const QList<ActionInfo>&)));
1524 connect(aMenu, SIGNAL(actionSelected(int)), this, theSlot);
1527 QList<ActionInfo> XGUI_Workshop::processHistoryList(const std::list<std::string>& theList) const
1529 QList<ActionInfo> aResult;
1530 std::list<std::string>::const_iterator it = theList.cbegin();
1531 for (; it != theList.cend(); it++) {
1532 QString anId = QString::fromStdString(*it);
1533 bool isEditing = anId.endsWith(ModuleBase_Operation::EditSuffix());
1535 anId.chop(ModuleBase_Operation::EditSuffix().size());
1538 QAction* aContextMenuAct = myContextMenuMgr->actionByName(anId);
1539 if (aContextMenuAct) {
1540 anInfo.initFrom(aContextMenuAct);
1542 anInfo = myActionsMgr->actionInfoById(anId);
1545 anInfo.text = anInfo.text.prepend("Modify ");