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