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