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