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