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