]> SALOME platform Git repositories - modules/shaper.git/blob - src/XGUI/XGUI_Workshop.cpp
Salome HOME
8d3568d7a9b8ce3025ce697ab79e3e97ea4ecc94
[modules/shaper.git] / src / XGUI / XGUI_Workshop.cpp
1 #include "ModuleBase_IModule.h"
2 #include "XGUI_Constants.h"
3 #include "XGUI_Command.h"
4 #include "XGUI_MainMenu.h"
5 #include "XGUI_MainWindow.h"
6 #include "XGUI_MenuGroupPanel.h"
7 #include "XGUI_Tools.h"
8 #include "XGUI_Workbench.h"
9 #include "XGUI_Workshop.h"
10 #include "XGUI_Viewer.h"
11 #include "XGUI_SelectionMgr.h"
12 #include "XGUI_Selection.h"
13 #include "XGUI_ObjectsBrowser.h"
14 #include "XGUI_Displayer.h"
15 #include "XGUI_OperationMgr.h"
16 #include "XGUI_SalomeConnector.h"
17 #include "XGUI_SalomeViewer.h"
18 #include "XGUI_ActionsMgr.h"
19 #include "XGUI_ErrorDialog.h"
20 #include "XGUI_ViewerProxy.h"
21 #include "XGUI_PropertyPanel.h"
22 #include "XGUI_ContextMenuMgr.h"
23 #include "XGUI_ModuleConnector.h"
24 #include "XGUI_Preferences.h"
25 #include <XGUI_QtEvents.h>
26
27 #include <ModelAPI_Events.h>
28 #include <ModelAPI_Session.h>
29 #include <ModelAPI_Feature.h>
30 #include <ModelAPI_Data.h>
31 #include <ModelAPI_AttributeDocRef.h>
32 #include <ModelAPI_Object.h>
33 #include <ModelAPI_Validator.h>
34 #include <ModelAPI_ResultConstruction.h>
35 #include <ModelAPI_ResultBody.h>
36
37 #include <PartSetPlugin_Part.h>
38
39 #include <Events_Loop.h>
40 #include <Events_Error.h>
41 #include <Events_LongOp.h>
42
43 #include <ModuleBase_Operation.h>
44 #include <ModuleBase_Operation.h>
45 #include <ModuleBase_OperationDescription.h>
46 #include <ModuleBase_SelectionValidator.h>
47 #include "ModuleBase_WidgetFactory.h"
48
49 #include <Config_Common.h>
50 #include <Config_FeatureMessage.h>
51 #include <Config_PointerMessage.h>
52 #include <Config_ModuleReader.h>
53
54 #include <QApplication>
55 #include <QFileDialog>
56 #include <QMessageBox>
57 #include <QMdiSubWindow>
58 #include <QPushButton>
59 #include <QDockWidget>
60 #include <QLayout>
61 #include <QThread>
62 #include <QObject>
63
64 #ifdef _DEBUG
65 #include <QDebug>
66 #include <iostream>
67 #endif
68
69 #ifdef WIN32
70 #include <windows.h>
71 #else
72 #include <dlfcn.h>
73 #endif
74
75 QMap<QString, QString> XGUI_Workshop::myIcons;
76
77 QString XGUI_Workshop::featureIcon(const std::string& theId)
78 {
79   QString aId(theId.c_str());
80   if (myIcons.contains(aId))
81     return myIcons[aId];
82   return QString();
83 }
84
85 XGUI_Workshop::XGUI_Workshop(XGUI_SalomeConnector* theConnector)
86     : QObject(),
87       myCurrentDir(QString()),
88       myModule(NULL),
89       mySalomeConnector(theConnector),
90       myPropertyPanel(0),
91       myObjectBrowser(0),
92       myDisplayer(0),
93       myUpdatePrefs(false),
94       myPartActivating(false)
95 {
96   myMainWindow = mySalomeConnector ? 0 : new XGUI_MainWindow();
97
98   myDisplayer = new XGUI_Displayer(this);
99
100   mySelector = new XGUI_SelectionMgr(this);
101   //connect(mySelector, SIGNAL(selectionChanged()), this, SLOT(updateModuleCommands()));
102
103   myOperationMgr = new XGUI_OperationMgr(this);
104   myActionsMgr = new XGUI_ActionsMgr(this);
105   myErrorDlg = new XGUI_ErrorDialog(myMainWindow);
106   myContextMenuMgr = new XGUI_ContextMenuMgr(this);
107   connect(myContextMenuMgr, SIGNAL(actionTriggered(const QString&, bool)), this,
108           SLOT(onContextMenuCommand(const QString&, bool)));
109
110   myViewerProxy = new XGUI_ViewerProxy(this);
111   connect(myViewerProxy, SIGNAL(selectionChanged()), this, SLOT(updateCommandsOnViewSelection()));
112
113   myModuleConnector = new XGUI_ModuleConnector(this);
114
115   connect(myOperationMgr, SIGNAL(operationStarted()), SLOT(onOperationStarted()));
116   connect(myOperationMgr, SIGNAL(operationResumed()), SLOT(onOperationStarted()));
117   connect(myOperationMgr, SIGNAL(operationStopped(ModuleBase_Operation*)),
118           SLOT(onOperationStopped(ModuleBase_Operation*)));
119   connect(myMainWindow, SIGNAL(exitKeySequence()), SLOT(onExit()));
120   connect(myOperationMgr, SIGNAL(operationStarted()), myActionsMgr, SLOT(update()));
121   connect(myOperationMgr, SIGNAL(operationStopped(ModuleBase_Operation*)), myActionsMgr,
122           SLOT(update()));
123   connect(this, SIGNAL(errorOccurred(const QString&)), myErrorDlg, SLOT(addError(const QString&)));
124 }
125
126 //******************************************************
127 XGUI_Workshop::~XGUI_Workshop(void)
128 {
129 }
130
131 //******************************************************
132 void XGUI_Workshop::startApplication()
133 {
134   initMenu();
135   //Initialize event listening
136   Events_Loop* aLoop = Events_Loop::loop();
137   aLoop->registerListener(this, Events_Error::errorID());  //!< Listening application errors.
138   //TODO(sbh): Implement static method to extract event id [SEID]
139   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_FEATURE_LOADED));
140   // TODO Is it good to use non standard event within workshop?
141   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OPERATION_LAUNCHED));
142   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
143   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_CREATED));
144   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
145   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_DELETED));
146   aLoop->registerListener(this, Events_Loop::eventByName("LongOperation"));
147   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_PLUGIN_LOADED));
148   aLoop->registerListener(this, Events_Loop::eventByName("CurrentDocumentChanged"));
149
150   registerValidators();
151   activateModule();
152   if (myMainWindow) {
153     myMainWindow->show();
154     updateCommandStatus();
155   }
156   XGUI_Preferences::loadCustomProps();
157   onNew();
158 }
159
160 //******************************************************
161 void XGUI_Workshop::initMenu()
162 {
163   myContextMenuMgr->createActions();
164
165   if (isSalomeMode()) {
166     // Create only Undo, Redo commands
167     QAction* aAction = salomeConnector()->addEditCommand("UNDO_CMD", tr("Undo"),
168                                                          tr("Undo last command"),
169                                                          QIcon(":pictures/undo.png"),
170                                                          QKeySequence::Undo, false);
171     connect(aAction, SIGNAL(triggered(bool)), this, SLOT(onUndo()));
172     aAction = salomeConnector()->addEditCommand("REDO_CMD", tr("Redo"), tr("Redo last command"),
173                                                 QIcon(":pictures/redo.png"), QKeySequence::Redo,
174                                                 false);
175     connect(aAction, SIGNAL(triggered(bool)), this, SLOT(onRedo()));
176     salomeConnector()->addEditMenuSeparator();
177     return;
178   }
179   // File commands group
180   XGUI_MenuGroupPanel* aGroup = myMainWindow->menuObject()->generalPage();
181
182   XGUI_Command* aCommand;
183
184   aCommand = aGroup->addFeature("SAVE_CMD", tr("Save..."), tr("Save the document"),
185                                 QIcon(":pictures/save.png"), QKeySequence::Save);
186   aCommand->connectTo(this, SLOT(onSave()));
187   //aCommand->disable();
188
189   aCommand = aGroup->addFeature("UNDO_CMD", tr("Undo"), tr("Undo last command"),
190                                 QIcon(":pictures/undo.png"), QKeySequence::Undo);
191   aCommand->connectTo(this, SLOT(onUndo()));
192
193   aCommand = aGroup->addFeature("REDO_CMD", tr("Redo"), tr("Redo last command"),
194                                 QIcon(":pictures/redo.png"), QKeySequence::Redo);
195   aCommand->connectTo(this, SLOT(onRedo()));
196
197   aCommand = aGroup->addFeature("REBUILD_CMD", tr("Rebuild"), tr("Rebuild data objects"),
198                                 QIcon(":pictures/rebuild.png"));
199   aCommand->connectTo(this, SLOT(onRebuild()));
200
201   aCommand = aGroup->addFeature("SAVEAS_CMD", tr("Save as..."), tr("Save the document into a file"),
202                                 QIcon(":pictures/save.png"));
203   aCommand->connectTo(this, SLOT(onSaveAs()));
204   //aCommand->disable();
205
206   aCommand = aGroup->addFeature("OPEN_CMD", tr("Open..."), tr("Open a new document"),
207                                 QIcon(":pictures/open.png"), QKeySequence::Open);
208   aCommand->connectTo(this, SLOT(onOpen()));
209
210   //aCommand = aGroup->addFeature("NEW_CMD", tr("New"), tr("Create a new document"),
211   //                              QIcon(":pictures/new.png"), QKeySequence::New);
212   //aCommand->connectTo(this, SLOT(onNew()));
213
214   aCommand = aGroup->addFeature("PREF_CMD", tr("Preferences"), tr("Edit preferences"),
215                                 QIcon(":pictures/preferences.png"), QKeySequence::Preferences);
216   aCommand->connectTo(this, SLOT(onPreferences()));
217
218   aCommand = aGroup->addFeature("EXIT_CMD", tr("Exit"), tr("Exit application"),
219                                 QIcon(":pictures/close.png"), QKeySequence::Close);
220   aCommand->connectTo(this, SLOT(onExit()));
221   //FIXME: SBH's test action. Can be used for some GUI tests.
222 //  #ifdef _DEBUG
223 //    aCommand = aGroup->addFeature("TEST_CMD", "Test!", "Private debug button",
224 //                                  QIcon(":pictures/close.png"), QKeySequence(), true);
225 //    aCommand->connectTo(myMainWindow, SLOT(dockPythonConsole()));
226 //  #endif
227 }
228
229 //******************************************************
230 XGUI_Workbench* XGUI_Workshop::addWorkbench(const QString& theName)
231 {
232   XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
233   return aMenuBar->addWorkbench(theName);
234 }
235
236 //******************************************************
237 void XGUI_Workshop::processEvent(const boost::shared_ptr<Events_Message>& theMessage)
238 {
239   if (QApplication::instance()->thread() != QThread::currentThread()) {
240     #ifdef _DEBUG
241     std::cout << "XGUI_Workshop::processEvent: " << "Working in another thread." << std::endl;
242     #endif
243     SessionPtr aMgr = ModelAPI_Session::get();
244     PostponeMessageQtEvent* aPostponeEvent = new PostponeMessageQtEvent(theMessage);
245     QApplication::postEvent(this, aPostponeEvent);
246     return;
247   }
248
249   //A message to start feature creation received.
250   if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_FEATURE_LOADED)) {
251     boost::shared_ptr<Config_FeatureMessage> aFeatureMsg =
252        boost::dynamic_pointer_cast<Config_FeatureMessage>(theMessage);
253     if (!aFeatureMsg->isInternal()) {
254       addFeature(aFeatureMsg);
255     }
256   }
257
258   // Process creation of Part
259   else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_CREATED)) {
260     boost::shared_ptr<ModelAPI_ObjectUpdatedMessage> aUpdMsg =
261         boost::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
262     onFeatureCreatedMsg(aUpdMsg);
263     if (myUpdatePrefs) {
264       if (mySalomeConnector)
265         mySalomeConnector->createPreferences();
266       myUpdatePrefs = false;
267     }
268   }
269   else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_PLUGIN_LOADED)) {
270     myUpdatePrefs = true;
271   }
272
273   // Redisplay feature
274   else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY)) {
275     boost::shared_ptr<ModelAPI_ObjectUpdatedMessage> aUpdMsg =
276         boost::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
277     onFeatureRedisplayMsg(aUpdMsg);
278   }
279
280   //Update property panel on corresponding message. If there is no current operation (no
281   //property panel), or received message has different feature to the current - do nothing.
282   else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_UPDATED)) {
283     boost::shared_ptr<ModelAPI_ObjectUpdatedMessage> anUpdateMsg =
284         boost::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
285     onFeatureUpdatedMsg(anUpdateMsg);
286   }
287
288   else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_DELETED)) {
289     boost::shared_ptr<ModelAPI_ObjectDeletedMessage> aDelMsg =
290         boost::dynamic_pointer_cast<ModelAPI_ObjectDeletedMessage>(theMessage);
291     onObjectDeletedMsg(aDelMsg);
292   }
293
294   else if (theMessage->eventID() == Events_LongOp::eventID()) {
295     if (Events_LongOp::isPerformed())
296       QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
297     else
298       QApplication::restoreOverrideCursor();
299   }
300
301   //An operation passed by message. Start it, process and commit.
302   else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OPERATION_LAUNCHED)) {
303     boost::shared_ptr<Config_PointerMessage> aPartSetMsg =
304         boost::dynamic_pointer_cast<Config_PointerMessage>(theMessage);
305     //myPropertyPanel->cleanContent();
306     ModuleBase_Operation* anOperation = (ModuleBase_Operation*) aPartSetMsg->pointer();
307
308     if (myOperationMgr->startOperation(anOperation)) {
309       myPropertyPanel->updateContentWidget(anOperation->feature());
310       if (!anOperation->getDescription()->hasXmlRepresentation()) {
311         if (anOperation->commit())
312           updateCommandStatus();
313       }
314     }
315   }
316   else if (theMessage->eventID() == Events_Loop::loop()->eventByName("CurrentDocumentChanged")) {
317     // Find and Activate active part
318     if (myPartActivating)
319       return;
320     SessionPtr aMgr = ModelAPI_Session::get();
321     DocumentPtr aActiveDoc = aMgr->activeDocument();
322     DocumentPtr aDoc = aMgr->moduleDocument();
323     if (aActiveDoc == aDoc) {
324       activatePart(ResultPartPtr()); 
325       return;
326     }
327     std::string aGrpName = ModelAPI_ResultPart::group();
328     for (int i = 0; i < aDoc->size(aGrpName); i++) {
329       ResultPartPtr aPart = boost::dynamic_pointer_cast<ModelAPI_ResultPart>(aDoc->object(aGrpName, i));
330       if (aPart->partDoc() == aActiveDoc) {
331         activatePart(aPart); // Activate a part which corresponds to active Doc
332         return;
333       }
334     }
335     // If not found then activate global document
336     activatePart(ResultPartPtr()); 
337
338   } else {
339     //Show error dialog if error message received.
340     boost::shared_ptr<Events_Error> anAppError = boost::dynamic_pointer_cast<Events_Error>(theMessage);
341     if (anAppError) {
342       emit errorOccurred(QString::fromLatin1(anAppError->description()));
343     }
344   }
345   if (!isSalomeMode()) {
346     SessionPtr aMgr = ModelAPI_Session::get();
347     if (aMgr->isModified() != myMainWindow->isModifiedState())
348       myMainWindow->setModifiedState(aMgr->isModified());
349   }
350 }
351
352
353 //******************************************************
354 void XGUI_Workshop::onStartWaiting()
355 {
356   if (Events_LongOp::isPerformed()) {
357     QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
358   }
359 }
360
361 //******************************************************
362 void XGUI_Workshop::onFeatureUpdatedMsg(const boost::shared_ptr<ModelAPI_ObjectUpdatedMessage>& theMsg)
363 {
364   std::set<ObjectPtr> aFeatures = theMsg->objects();
365   if (myOperationMgr->hasOperation()) {
366     FeaturePtr aCurrentFeature = myOperationMgr->currentOperation()->feature();
367     std::set<ObjectPtr>::const_iterator aIt;
368     for (aIt = aFeatures.begin(); aIt != aFeatures.end(); ++aIt) {
369       ObjectPtr aNewFeature = (*aIt);
370       if (aNewFeature == aCurrentFeature) {
371         myPropertyPanel->updateContentWidget(aCurrentFeature);
372         break;
373       }
374     }
375   }
376   myOperationMgr->onValidateOperation();
377   if (myObjectBrowser)
378     myObjectBrowser->processEvent(theMsg);
379 }
380
381 //******************************************************
382 void XGUI_Workshop::onFeatureRedisplayMsg(const boost::shared_ptr<ModelAPI_ObjectUpdatedMessage>& theMsg)
383 {
384   std::set<ObjectPtr> aObjects = theMsg->objects();
385   std::set<ObjectPtr>::const_iterator aIt;
386   for (aIt = aObjects.begin(); aIt != aObjects.end(); ++aIt) {
387     ObjectPtr aObj = (*aIt);
388     if (!aObj->data() || !aObj->data()->isValid() || aObj->document()->isConcealed(aObj))
389       myDisplayer->erase(aObj, false);
390     else {
391       if (myDisplayer->isVisible(aObj))  // TODO VSV: Correction sketch drawing
392         myDisplayer->display(aObj, false);  // In order to update presentation
393       else {
394         if (myOperationMgr->hasOperation()) {
395           ModuleBase_Operation* aOperation = myOperationMgr->currentOperation();
396           if (aOperation->hasObject(aObj)) {  // Display only current operation results
397             myDisplayer->display(aObj, false);
398           }
399         }
400       }
401     }
402   }
403   myDisplayer->updateViewer();
404 }
405
406 //******************************************************
407 void XGUI_Workshop::onFeatureCreatedMsg(const boost::shared_ptr<ModelAPI_ObjectUpdatedMessage>& theMsg)
408 {
409   std::set<ObjectPtr> aObjects = theMsg->objects();
410
411   std::set<ObjectPtr>::const_iterator aIt;
412   bool aHasPart = false;
413   bool isDisplayed = false;
414   for (aIt = aObjects.begin(); aIt != aObjects.end(); ++aIt) {
415     ResultPartPtr aPart = boost::dynamic_pointer_cast<ModelAPI_ResultPart>(*aIt);
416     if (aPart) {
417       aHasPart = true;
418       // If a feature is created from the aplication's python console  
419       // it doesn't stored in the operation mgr and doesn't displayed
420     } else if (myOperationMgr->hasOperation()) {
421       ModuleBase_Operation* aOperation = myOperationMgr->currentOperation();
422       if (!(*aIt)->document()->isConcealed(*aIt) &&
423           aOperation->hasObject(*aIt)) {  // Display only current operation results
424         myDisplayer->display(*aIt, false);
425         isDisplayed = true;
426       }
427     }
428   }
429   if (myObjectBrowser)
430     myObjectBrowser->processEvent(theMsg);
431   if (isDisplayed)
432     myDisplayer->updateViewer();
433   if (aHasPart) {
434     activateLastPart();
435   }
436 }
437
438 //******************************************************
439 void XGUI_Workshop::onObjectDeletedMsg(const boost::shared_ptr<ModelAPI_ObjectDeletedMessage>& theMsg)
440 {
441   if (myObjectBrowser)
442     myObjectBrowser->processEvent(theMsg);
443   //std::set<ObjectPtr> aFeatures = theMsg->objects();
444 }
445
446 //******************************************************
447 void XGUI_Workshop::onOperationStarted()
448 {
449   ModuleBase_Operation* aOperation = myOperationMgr->currentOperation();
450   if (this->isSalomeMode()) 
451     aOperation->setNestedFeatures(mySalomeConnector->nestedActions(aOperation->id()));
452   else 
453     aOperation->setNestedFeatures(myActionsMgr->nestedCommands(aOperation->id()));
454   
455   if (aOperation->getDescription()->hasXmlRepresentation()) {  //!< No need for property panel
456     connectWithOperation(aOperation);
457
458     showPropertyPanel();
459     QString aXmlRepr = aOperation->getDescription()->xmlRepresentation();
460     ModuleBase_WidgetFactory aFactory = ModuleBase_WidgetFactory(aXmlRepr.toStdString(),
461                                                                  myModuleConnector);
462
463     myPropertyPanel->cleanContent();
464     aFactory.createWidget(myPropertyPanel->contentWidget());
465
466     QList<ModuleBase_ModelWidget*> aWidgets = aFactory.getModelWidgets();
467     QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
468     ModuleBase_ModelWidget* aWidget;
469     for (; anIt != aLast; anIt++) {
470       aWidget = *anIt;
471       aWidget->setFeature(aOperation->feature());
472       QObject::connect(aWidget, SIGNAL(valuesChanged()), this, SLOT(onWidgetValuesChanged()));
473       // Init default values
474       if (!aOperation->isEditOperation() && !aWidget->isComputedDefault()) {
475         aWidget->storeValue();
476       }
477     }
478
479     myPropertyPanel->setModelWidgets(aWidgets);
480     myPropertyPanel->onActivateNextWidget(NULL);
481     myPropertyPanel->setWindowTitle(aOperation->getDescription()->description());
482   }
483   updateCommandStatus();
484 }
485
486 //******************************************************
487 void XGUI_Workshop::onOperationStopped(ModuleBase_Operation* theOperation)
488 {
489   //!< No need for property panel
490   updateCommandStatus();
491   hidePropertyPanel();
492   myPropertyPanel->cleanContent();
493 }
494
495 bool XGUI_Workshop::event(QEvent * theEvent)
496 {
497   PostponeMessageQtEvent* aPostponedEv = dynamic_cast<PostponeMessageQtEvent*>(theEvent);
498   if (aPostponedEv) {
499 #ifdef _DEBUG
500     std::cout << "XGUI_Workshop::event " << "Got PostponeMessageQtEvent" << std::endl;
501     bool isMyThread = (QApplication::instance()->thread() == QThread::currentThread());
502     std::cout << "XGUI_Workshop::event " << "I am in the Qt's thread: "
503               << isMyThread << std::endl;
504 #endif
505     boost::shared_ptr<Events_Message> aEventPtr = aPostponedEv->postponedMessage();
506     processEvent(aEventPtr);
507     return true;
508   }
509   return false;
510 }
511
512 /*
513  *
514  */
515 void XGUI_Workshop::addFeature(const boost::shared_ptr<Config_FeatureMessage>& theMessage)
516 {
517   if (!theMessage) {
518 #ifdef _DEBUG
519     qDebug() << "XGUI_Workshop::addFeature: NULL message.";
520 #endif
521     return;
522   }
523   // Remember features icons
524   myIcons[QString::fromStdString(theMessage->id())] = QString::fromStdString(theMessage->icon());
525
526   //Find or create Workbench
527   QString aWchName = QString::fromStdString(theMessage->workbenchId());
528   QString aNestedFeatures = QString::fromStdString(theMessage->nestedFeatures());
529   bool isUsePropPanel = theMessage->isUseInput();
530   QString aFeatureId = QString::fromStdString(theMessage->id());
531   if (isSalomeMode()) {
532     QAction* aAction = salomeConnector()->addFeature(aWchName, aFeatureId,
533                                                      QString::fromStdString(theMessage->text()),
534                                                      QString::fromStdString(theMessage->tooltip()),
535                                                      QIcon(theMessage->icon().c_str()),
536                                                      QKeySequence(), isUsePropPanel);
537     salomeConnector()->setNestedActions(aFeatureId, aNestedFeatures.split(" ", QString::SkipEmptyParts));
538     myActionsMgr->addCommand(aAction);
539     myModule->featureCreated(aAction);
540   } else {
541
542     XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
543     XGUI_Workbench* aPage = aMenuBar->findWorkbench(aWchName);
544     if (!aPage) {
545       aPage = addWorkbench(aWchName);
546     }
547     //Find or create Group
548     QString aGroupName = QString::fromStdString(theMessage->groupId());
549     XGUI_MenuGroupPanel* aGroup = aPage->findGroup(aGroupName);
550     if (!aGroup) {
551       aGroup = aPage->addGroup(aGroupName);
552     }
553     // Check if hotkey sequence is already defined:
554     QKeySequence aHotKey = myActionsMgr->registerShortcut(
555         QString::fromStdString(theMessage->keysequence()));
556     // Create feature...
557     XGUI_Command* aCommand = aGroup->addFeature(aFeatureId,
558                                                 QString::fromStdString(theMessage->text()),
559                                                 QString::fromStdString(theMessage->tooltip()),
560                                                 QIcon(theMessage->icon().c_str()), aHotKey,
561                                                 isUsePropPanel);
562     aCommand->setNestedCommands(aNestedFeatures.split(" ", QString::SkipEmptyParts));
563     myActionsMgr->addCommand(aCommand);
564     myModule->featureCreated(aCommand);
565   }
566 }
567
568 /*
569  * Makes a signal/slot connections between Property Panel
570  * and given operation. The given operation becomes a
571  * current operation and previous operation if exists
572  */
573 void XGUI_Workshop::connectWithOperation(ModuleBase_Operation* theOperation)
574 {
575   QAction* aCommand = 0;
576   if (isSalomeMode()) {
577     aCommand = salomeConnector()->command(theOperation->getDescription()->operationId());
578   } else {
579     XGUI_MainMenu* aMenu = myMainWindow->menuObject();
580     FeaturePtr aFeature = theOperation->feature();
581     if(aFeature)
582       aCommand = aMenu->feature(QString::fromStdString(aFeature->getKind()));
583   }
584   //Abort operation on uncheck the command
585   if (aCommand) {
586     connect(aCommand, SIGNAL(triggered(bool)), theOperation, SLOT(setRunning(bool)));
587   }
588 }
589
590 /*
591  * Saves document with given name.
592  */
593 void XGUI_Workshop::saveDocument(const QString& theName, std::list<std::string>& theFileNames)
594 {
595   QApplication::restoreOverrideCursor();
596   SessionPtr aMgr = ModelAPI_Session::get();
597   aMgr->save(theName.toLatin1().constData(), theFileNames);
598   QApplication::restoreOverrideCursor();
599 }
600
601 bool XGUI_Workshop::isActiveOperationAborted()
602 {
603   return myOperationMgr->abortAllOperations();
604 }
605
606 //******************************************************
607 void XGUI_Workshop::onExit()
608 {
609   SessionPtr aMgr = ModelAPI_Session::get();
610   if (aMgr->isModified()) {
611     int anAnswer = QMessageBox::question(
612         myMainWindow, tr("Save current file"), tr("The document is modified, save before exit?"),
613         QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel, QMessageBox::Cancel);
614     if (anAnswer == QMessageBox::Save) {
615       bool saved = onSave();
616       if (!saved) {
617         return;
618       }
619     } else if (anAnswer == QMessageBox::Cancel) {
620       return;
621     }
622   }
623   qApp->exit();
624 }
625
626 //******************************************************
627 void XGUI_Workshop::onNew()
628 {
629   QApplication::setOverrideCursor(Qt::WaitCursor);
630   if (objectBrowser() == 0) {
631     createDockWidgets();
632     mySelector->connectViewers();
633   }
634   myViewerProxy->connectToViewer();
635   showObjectBrowser();
636   if (!isSalomeMode()) {
637     myMainWindow->showPythonConsole();
638     QMdiSubWindow* aWnd = myMainWindow->viewer()->createView();
639     aWnd->showMaximized();
640     updateCommandStatus();
641   }
642   myContextMenuMgr->connectViewer();
643   QApplication::restoreOverrideCursor();
644 }
645
646 //******************************************************
647 void XGUI_Workshop::onOpen()
648 {
649   if(!isActiveOperationAborted())
650     return;
651   //save current file before close if modified
652   SessionPtr aSession = ModelAPI_Session::get();
653   if (aSession->isModified()) {
654     //TODO(sbh): re-launch the app?
655     int anAnswer = QMessageBox::question(
656         myMainWindow, tr("Save current file"),
657         tr("The document is modified, save before opening another?"),
658         QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel, QMessageBox::Cancel);
659     if (anAnswer == QMessageBox::Save) {
660       onSave();
661     } else if (anAnswer == QMessageBox::Cancel) {
662       return;
663     }
664     aSession->moduleDocument()->close();
665     myCurrentDir = "";
666   }
667
668   //show file dialog, check if readable and open
669   myCurrentDir = QFileDialog::getExistingDirectory(mainWindow());
670   if (myCurrentDir.isEmpty())
671     return;
672   QFileInfo aFileInfo(myCurrentDir);
673   if (!aFileInfo.exists() || !aFileInfo.isReadable()) {
674     QMessageBox::critical(myMainWindow, tr("Warning"), tr("Unable to open the file."));
675     myCurrentDir = "";
676     return;
677   }
678   QApplication::setOverrideCursor(Qt::WaitCursor);
679   aSession->load(myCurrentDir.toLatin1().constData());
680   myObjectBrowser->rebuildDataTree();
681   displayAllResults();
682   updateCommandStatus();
683   QApplication::restoreOverrideCursor();
684 }
685
686 //******************************************************
687 bool XGUI_Workshop::onSave()
688 {
689   if(!isActiveOperationAborted())
690     return false;
691   if (myCurrentDir.isEmpty()) {
692     return onSaveAs();
693   }
694   std::list<std::string> aFiles;
695   saveDocument(myCurrentDir, aFiles);
696   updateCommandStatus();
697   myMainWindow->setModifiedState(false);
698   return true;
699 }
700
701 //******************************************************
702 bool XGUI_Workshop::onSaveAs()
703 {
704   if(!isActiveOperationAborted())
705     return false;
706   QFileDialog dialog(mainWindow());
707   dialog.setWindowTitle(tr("Select directory to save files..."));
708   dialog.setFileMode(QFileDialog::Directory);
709   dialog.setFilter(tr("Folders (*)"));
710   dialog.setOptions(QFileDialog::HideNameFilterDetails | QFileDialog::ShowDirsOnly);
711   dialog.setViewMode(QFileDialog::Detail);
712
713   if (!dialog.exec()) {
714     return false;
715   }
716   QString aTempDir = dialog.selectedFiles().first();
717   QDir aDir(aTempDir);
718   if (aDir.exists() && !aDir.entryInfoList(QDir::NoDotAndDotDot | QDir::AllEntries).isEmpty()) {
719     int answer = QMessageBox::question(
720         myMainWindow,
721         //: Title of the dialog which asks user if he wants to save study in existing non-empty folder
722         tr("Save"),
723         tr("The folder already contains some files, save anyway?"),
724         QMessageBox::Save | QMessageBox::Cancel);
725     if (answer == QMessageBox::Cancel) {
726       return false;
727     }
728   }
729   myCurrentDir = aTempDir;
730   if (!isSalomeMode()) {
731     myMainWindow->setCurrentDir(myCurrentDir, false);
732     myMainWindow->setModifiedState(false);
733   }
734   return onSave();
735 }
736
737 //******************************************************
738 void XGUI_Workshop::onUndo()
739 {
740   objectBrowser()->treeView()->setCurrentIndex(QModelIndex());
741   SessionPtr aMgr = ModelAPI_Session::get();
742   if (aMgr->isOperation())
743     operationMgr()->onAbortOperation();
744   aMgr->undo();
745   updateCommandStatus();
746 }
747
748 //******************************************************
749 void XGUI_Workshop::onRedo()
750 {
751   objectBrowser()->treeView()->setCurrentIndex(QModelIndex());
752   SessionPtr aMgr = ModelAPI_Session::get();
753   if (aMgr->isOperation())
754     operationMgr()->onAbortOperation();
755   aMgr->redo();
756   updateCommandStatus();
757 }
758
759 //******************************************************
760 void XGUI_Workshop::onRebuild()
761 {
762   static const Events_ID aRebuildEvent = Events_Loop::loop()->eventByName("Rebuild");
763   Events_Loop::loop()->send(boost::shared_ptr<Events_Message>(
764     new Events_Message(aRebuildEvent, this)));
765 }
766
767 //******************************************************
768 void XGUI_Workshop::onPreferences()
769 {
770   XGUI_Prefs aModif;
771   XGUI_Preferences::editPreferences(aModif);
772   if (aModif.size() > 0) {
773     QString aSection;
774     foreach (XGUI_Pref aPref, aModif)
775     {
776       aSection = aPref.first;
777       if (aSection == XGUI_Preferences::VIEWER_SECTION) {
778         if (!isSalomeMode())
779           myMainWindow->viewer()->updateFromResources();
780       } else if (aSection == XGUI_Preferences::MENU_SECTION) {
781         if (!isSalomeMode())
782           myMainWindow->menuObject()->updateFromResources();
783       }
784     }
785   }
786 }
787
788 //******************************************************
789 ModuleBase_IModule* XGUI_Workshop::loadModule(const QString& theModule)
790 {
791   QString libName = QString::fromStdString(library(theModule.toStdString()));
792   if (libName.isEmpty()) {
793     qWarning(qPrintable(tr("Information about module \"%1\" doesn't exist.").arg(theModule)));
794     return 0;
795   }
796
797   QString err;
798   CREATE_FUNC crtInst = 0;
799
800 #ifdef WIN32
801   HINSTANCE modLib = ::LoadLibrary((LPTSTR) qPrintable(libName));
802   if (!modLib) {
803     LPVOID lpMsgBuf;
804     ::FormatMessage(
805         FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
806         0, ::GetLastError(), 0, (LPTSTR) & lpMsgBuf, 0, 0);
807     QString aMsg((char*) &lpMsgBuf);
808     err = QString("Failed to load  %1. %2").arg(libName).arg(aMsg);
809     ::LocalFree(lpMsgBuf);
810   } else {
811     crtInst = (CREATE_FUNC) ::GetProcAddress(modLib, CREATE_MODULE);
812     if (!crtInst) {
813       LPVOID lpMsgBuf;
814       ::FormatMessage(
815           FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM
816               | FORMAT_MESSAGE_IGNORE_INSERTS,
817           0, ::GetLastError(), 0, (LPTSTR) & lpMsgBuf, 0, 0);
818       QString aMsg((char*) &lpMsgBuf);
819       err = QString("Failed to find  %1 function. %2").arg( CREATE_MODULE).arg(aMsg);
820       ::LocalFree(lpMsgBuf);
821     }
822   }
823 #else
824   void* modLib = dlopen( libName.toLatin1(), RTLD_LAZY | RTLD_GLOBAL );
825   if ( !modLib ) {
826     err = QString( "Can not load library %1. %2" ).arg( libName ).arg( dlerror() );
827   } else {
828     crtInst = (CREATE_FUNC)dlsym( modLib, CREATE_MODULE );
829     if ( !crtInst ) {
830       err = QString( "Failed to find function %1. %2" ).arg( CREATE_MODULE ).arg( dlerror() );
831     }
832   }
833 #endif
834
835   ModuleBase_IModule* aModule = crtInst ? crtInst(this) : 0;
836
837   if (!err.isEmpty()) {
838     if (mainWindow()) {
839       QMessageBox::warning(mainWindow(), tr("Error"), err);
840     } else {
841       qWarning(qPrintable(err));
842     }
843   }
844   return aModule;
845 }
846
847 //******************************************************
848 bool XGUI_Workshop::activateModule()
849 {
850   Config_ModuleReader aModuleReader;
851   QString moduleName = QString::fromStdString(aModuleReader.getModuleName());
852   myModule = loadModule(moduleName);
853   if (!myModule)
854     return false;
855   myModule->createFeatures();
856   myActionsMgr->update();
857   return true;
858 }
859
860 //******************************************************
861 void XGUI_Workshop::updateCommandStatus()
862 {
863   QList<QAction*> aCommands;
864   if (isSalomeMode()) {  // update commands in SALOME mode
865     aCommands = salomeConnector()->commandList();
866   } else {
867     XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
868     foreach (XGUI_Command* aCmd, aMenuBar->features())
869       aCommands.append(aCmd);
870   }
871   SessionPtr aMgr = ModelAPI_Session::get();
872   if (aMgr->hasModuleDocument()) {
873     QAction* aUndoCmd;
874     QAction* aRedoCmd;
875     foreach(QAction* aCmd, aCommands) {
876       QString aId = aCmd->data().toString();
877       if (aId == "UNDO_CMD")
878         aUndoCmd = aCmd;
879       else if (aId == "REDO_CMD")
880         aRedoCmd = aCmd;
881       else
882         // Enable all commands
883         aCmd->setEnabled(true);
884     }
885     aUndoCmd->setEnabled(aMgr->canUndo());
886     aRedoCmd->setEnabled(aMgr->canRedo());
887   } else {
888     foreach(QAction* aCmd, aCommands) {
889       QString aId = aCmd->data().toString();
890       if (aId == "NEW_CMD")
891         aCmd->setEnabled(true);
892       else if (aId == "EXIT_CMD")
893         aCmd->setEnabled(true);
894       else
895         aCmd->setEnabled(false);
896     }
897   }
898   myActionsMgr->update();
899 }
900
901 //******************************************************
902 QList<QAction*> XGUI_Workshop::getModuleCommands() const
903 {
904   QList<QAction*> aCommands;
905   if (isSalomeMode()) {  // update commands in SALOME mode
906     aCommands = salomeConnector()->commandList();
907   } else {
908     XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
909     foreach(XGUI_Command* aCmd, aMenuBar->features())
910     {
911       aCommands.append(aCmd);
912     }
913   }
914   return aCommands;
915 }
916
917 //******************************************************
918 QDockWidget* XGUI_Workshop::createObjectBrowser(QWidget* theParent)
919 {
920   QDockWidget* aObjDock = new QDockWidget(theParent);
921   aObjDock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea | Qt::BottomDockWidgetArea);
922   aObjDock->setWindowTitle(tr("Object browser"));
923   aObjDock->setStyleSheet(
924       "::title { position: relative; padding-left: 5px; text-align: left center }");
925   myObjectBrowser = new XGUI_ObjectsBrowser(aObjDock);
926   connect(myObjectBrowser, SIGNAL(activePartChanged(ObjectPtr)), this,
927           SLOT(changeCurrentDocument(ObjectPtr)));
928   aObjDock->setWidget(myObjectBrowser);
929
930   myContextMenuMgr->connectObjectBrowser();
931   return aObjDock;
932 }
933
934 //******************************************************
935 /*
936  * Creates dock widgets, places them in corresponding area
937  * and tabifies if necessary.
938  */
939 void XGUI_Workshop::createDockWidgets()
940 {
941   QMainWindow* aDesktop = isSalomeMode() ? salomeConnector()->desktop() : myMainWindow;
942   QDockWidget* aObjDock = createObjectBrowser(aDesktop);
943   aDesktop->addDockWidget(Qt::LeftDockWidgetArea, aObjDock);
944   myPropertyPanel = new XGUI_PropertyPanel(aDesktop);
945   myPropertyPanel->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea | Qt::BottomDockWidgetArea);
946   aDesktop->addDockWidget(Qt::LeftDockWidgetArea, myPropertyPanel);
947   hidePropertyPanel();  //<! Invisible by default
948   hideObjectBrowser();
949   aDesktop->tabifyDockWidget(aObjDock, myPropertyPanel);
950
951   QPushButton* aOkBtn = myPropertyPanel->findChild<QPushButton*>(XGUI::PROP_PANEL_OK);
952   connect(aOkBtn, SIGNAL(clicked()), myOperationMgr, SLOT(onCommitOperation()));
953   QPushButton* aCancelBtn = myPropertyPanel->findChild<QPushButton*>(XGUI::PROP_PANEL_CANCEL);
954   connect(aCancelBtn, SIGNAL(clicked()), myOperationMgr, SLOT(onAbortOperation()));
955   connect(myPropertyPanel, SIGNAL(keyReleased(QKeyEvent*)), myOperationMgr,
956           SLOT(onKeyReleased(QKeyEvent*)));
957   connect(myPropertyPanel, SIGNAL(widgetActivated(ModuleBase_ModelWidget*)), myOperationMgr,
958           SLOT(onWidgetActivated(ModuleBase_ModelWidget*)));
959   connect(myOperationMgr, SIGNAL(activateNextWidget(ModuleBase_ModelWidget*)), myPropertyPanel,
960           SLOT(onActivateNextWidget(ModuleBase_ModelWidget*)));
961   connect(myOperationMgr, SIGNAL(operationValidated(bool)), myPropertyPanel,
962           SLOT(setAcceptEnabled(bool)));
963
964 }
965
966 //******************************************************
967 void XGUI_Workshop::showPropertyPanel()
968 {
969   QAction* aViewAct = myPropertyPanel->toggleViewAction();
970   //<! Restore ability to close panel from the window's menu
971   aViewAct->setEnabled(true);
972   myPropertyPanel->show();
973   myPropertyPanel->raise();
974 }
975
976 //******************************************************
977 void XGUI_Workshop::hidePropertyPanel()
978 {
979   QAction* aViewAct = myPropertyPanel->toggleViewAction();
980   //<! Do not allow to show empty property panel
981   aViewAct->setEnabled(false);
982   myPropertyPanel->hide();
983 }
984
985 //******************************************************
986 void XGUI_Workshop::showObjectBrowser()
987 {
988   myObjectBrowser->parentWidget()->show();
989 }
990
991 //******************************************************
992 void XGUI_Workshop::hideObjectBrowser()
993 {
994   myObjectBrowser->parentWidget()->hide();
995 }
996
997 //******************************************************
998 void XGUI_Workshop::onFeatureTriggered()
999 {
1000   QAction* aCmd = dynamic_cast<QAction*>(sender());
1001   if (aCmd) {
1002     QString aId = salomeConnector()->commandId(aCmd);
1003     if (!aId.isNull())
1004       myModule->launchOperation(aId);
1005   }
1006 }
1007
1008 //******************************************************
1009 void XGUI_Workshop::changeCurrentDocument(ObjectPtr theObj)
1010 {
1011   SessionPtr aMgr = ModelAPI_Session::get();
1012   if (theObj) {
1013     ResultPartPtr aPart = boost::dynamic_pointer_cast<ModelAPI_ResultPart>(theObj);
1014     if (aPart) {
1015       DocumentPtr aPartDoc = aPart->partDoc();
1016       if (aPartDoc) {
1017         aMgr->setActiveDocument(aPartDoc);
1018         return;
1019       }
1020     }
1021   }
1022   aMgr->setActiveDocument(aMgr->moduleDocument());
1023 }
1024
1025 //******************************************************
1026 void XGUI_Workshop::salomeViewerSelectionChanged()
1027 {
1028   emit salomeViewerSelection();
1029 }
1030
1031 //**************************************************************
1032 XGUI_SalomeViewer* XGUI_Workshop::salomeViewer() const
1033 {
1034   return mySalomeConnector->viewer();
1035 }
1036
1037 //**************************************************************
1038 void XGUI_Workshop::onContextMenuCommand(const QString& theId, bool isChecked)
1039 {
1040   QList<ObjectPtr> aObjects = mySelector->selection()->selectedObjects();
1041   if ((theId == "ACTIVATE_PART_CMD") && (aObjects.size() > 0)) {
1042     ResultPartPtr aPart = boost::dynamic_pointer_cast<ModelAPI_ResultPart>(aObjects.first());
1043     activatePart(aPart);
1044   } else if (theId == "DEACTIVATE_PART_CMD")
1045     activatePart(ResultPartPtr());
1046   else if (theId == "DELETE_CMD")
1047     deleteObjects(aObjects);
1048   else if (theId == "SHOW_CMD")
1049     showObjects(aObjects, true);
1050   else if (theId == "HIDE_CMD")
1051     showObjects(aObjects, false);
1052   else if (theId == "SHOW_ONLY_CMD")
1053     showOnlyObjects(aObjects);
1054 }
1055
1056 //**************************************************************
1057 void XGUI_Workshop::onWidgetValuesChanged()
1058 {
1059   ModuleBase_Operation* anOperation = myOperationMgr->currentOperation();
1060   FeaturePtr aFeature = anOperation->feature();
1061
1062   ModuleBase_ModelWidget* aSenderWidget = dynamic_cast<ModuleBase_ModelWidget*>(sender());
1063   //if (aCustom)
1064   //  aCustom->storeValue(aFeature);
1065
1066   const QList<ModuleBase_ModelWidget*>& aWidgets = myPropertyPanel->modelWidgets();
1067   QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
1068   for (; anIt != aLast; anIt++) {
1069     ModuleBase_ModelWidget* aCustom = *anIt;
1070     if (aCustom && (/*!aCustom->isInitialized(aFeature) ||*/aCustom == aSenderWidget)) {
1071       //aCustom->storeValue(aFeature);
1072       aCustom->storeValue();
1073     }
1074   }
1075 }
1076
1077 //**************************************************************
1078 void XGUI_Workshop::activatePart(ResultPartPtr theFeature)
1079 {
1080   if (!myPartActivating) {
1081     myPartActivating = true;
1082     if (theFeature)
1083       theFeature->activate();
1084     changeCurrentDocument(theFeature);
1085     myObjectBrowser->activatePart(theFeature);
1086     myPartActivating = false;
1087   }
1088 }
1089
1090 //**************************************************************
1091 void XGUI_Workshop::activateLastPart()
1092 {
1093   SessionPtr aMgr = ModelAPI_Session::get();
1094   DocumentPtr aDoc = aMgr->moduleDocument();
1095   std::string aGrpName = ModelAPI_ResultPart::group();
1096   ObjectPtr aLastPart = aDoc->object(aGrpName, aDoc->size(aGrpName) - 1);
1097   ResultPartPtr aPart = boost::dynamic_pointer_cast<ModelAPI_ResultPart>(aLastPart);
1098   if (aPart) {
1099     activatePart(aPart);
1100   }
1101 }
1102
1103 //**************************************************************
1104 void XGUI_Workshop::deleteObjects(const QList<ObjectPtr>& theList)
1105 {
1106   QMainWindow* aDesktop = isSalomeMode() ? salomeConnector()->desktop() : myMainWindow;
1107   QMessageBox::StandardButton aRes = QMessageBox::warning(
1108       aDesktop, tr("Delete features"), tr("Seleted features will be deleted. Continue?"),
1109       QMessageBox::No | QMessageBox::Yes, QMessageBox::No);
1110   // ToDo: definbe deleting method
1111   if (aRes == QMessageBox::Yes) {
1112     SessionPtr aMgr = ModelAPI_Session::get();
1113     aMgr->startOperation();
1114     foreach (ObjectPtr aObj, theList)
1115     {
1116       ResultPartPtr aPart = boost::dynamic_pointer_cast<ModelAPI_ResultPart>(aObj);
1117       if (aPart) {
1118         DocumentPtr aDoc = aPart->document();
1119         if (aDoc == aMgr->activeDocument()) {
1120           aDoc->close();
1121         }
1122         //aMgr->moduleDocument()->removeFeature(aPart->owner());
1123       } else {
1124         FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
1125         if (aFeature)
1126           aObj->document()->removeFeature(aFeature);
1127       }
1128     }
1129     myDisplayer->updateViewer();
1130     aMgr->finishOperation();
1131   }
1132 }
1133
1134 //**************************************************************
1135 void XGUI_Workshop::showObjects(const QList<ObjectPtr>& theList, bool isVisible)
1136 {
1137   foreach (ObjectPtr aObj, theList)
1138   {
1139     ResultPtr aRes = boost::dynamic_pointer_cast<ModelAPI_Result>(aObj);
1140     if (aRes) {
1141       if (isVisible) {
1142         myDisplayer->display(aRes, false);
1143       } else {
1144         myDisplayer->erase(aRes, false);
1145       }
1146     }
1147   }
1148   myDisplayer->updateViewer();
1149 }
1150
1151 //**************************************************************
1152 void XGUI_Workshop::showOnlyObjects(const QList<ObjectPtr>& theList)
1153 {
1154   myDisplayer->eraseAll(false);
1155   showObjects(theList, true);
1156 }
1157
1158
1159 //**************************************************************
1160 void XGUI_Workshop::updateCommandsOnViewSelection()
1161 {
1162   XGUI_Selection* aSelection = mySelector->selection();
1163   if (aSelection->getSelected().size() == 0)
1164     return;
1165
1166   // Restrict validators to manage only nested (child) features
1167   // of the current feature i.e. if current feature is Sketch -
1168   // Sketch Features & Constraints can be validated.
1169   QStringList aNestedIds;
1170   if(myOperationMgr->hasOperation()) {
1171     FeaturePtr aFeature = myOperationMgr->currentOperation()->feature();
1172     if(aFeature) {
1173       aNestedIds << myActionsMgr->nestedCommands(QString::fromStdString(aFeature->getKind()));
1174     }
1175   }
1176   SessionPtr aMgr = ModelAPI_Session::get();
1177   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
1178   QList<QAction*> aActions = getModuleCommands();
1179   foreach(QAction* aAction, aActions) {
1180     QString aId = aAction->data().toString();
1181     if(!aNestedIds.contains(aId))
1182       continue;
1183     std::list<ModelAPI_Validator*> aValidators;
1184     std::list<std::list<std::string> > anArguments;
1185     aFactory->validators(aId.toStdString(), aValidators, anArguments);
1186     std::list<ModelAPI_Validator*>::iterator aValidator = aValidators.begin();
1187     for (; aValidator != aValidators.end(); aValidator++) {
1188       if (*aValidator) {
1189         const ModuleBase_SelectionValidator* aSelValidator =
1190             dynamic_cast<const ModuleBase_SelectionValidator*>(*aValidator);
1191         if (aSelValidator) {
1192           aAction->setEnabled(aSelValidator->isValid(aSelection));
1193         }
1194       }
1195     }
1196   }
1197 }
1198
1199 //**************************************************************
1200 void XGUI_Workshop::registerValidators() const
1201 {
1202   SessionPtr aMgr = ModelAPI_Session::get();
1203   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
1204 }
1205
1206 //**************************************************************
1207 void XGUI_Workshop::displayAllResults()
1208 {
1209   SessionPtr aMgr = ModelAPI_Session::get();
1210   DocumentPtr aRootDoc = aMgr->moduleDocument();
1211   displayDocumentResults(aRootDoc);
1212   for (int i = 0; i < aRootDoc->size(ModelAPI_ResultPart::group()); i++) {
1213     ObjectPtr aObject = aRootDoc->object(ModelAPI_ResultPart::group(), i);
1214     ResultPartPtr aPart = boost::dynamic_pointer_cast<ModelAPI_ResultPart>(aObject);
1215     displayDocumentResults(aPart->partDoc());
1216   }
1217   myDisplayer->updateViewer();
1218 }
1219
1220 //**************************************************************
1221 void XGUI_Workshop::displayDocumentResults(DocumentPtr theDoc)
1222 {
1223   if (!theDoc)
1224     return;
1225   displayGroupResults(theDoc, ModelAPI_ResultConstruction::group());
1226   displayGroupResults(theDoc, ModelAPI_ResultBody::group());
1227 }
1228
1229 //**************************************************************
1230 void XGUI_Workshop::displayGroupResults(DocumentPtr theDoc, std::string theGroup)
1231 {
1232   for (int i = 0; i < theDoc->size(theGroup); i++)
1233     myDisplayer->display(theDoc->object(theGroup, i), false);
1234 }