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