Salome HOME
a3fac8efc54a4c71c8aefc520bfe60b3d418b259
[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(aMgr->activeDocument());
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->validateCurrentOperation();
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<ModelAPI_Document> aDoc = aPostponedEv->resultDoc();
503     if (aDoc) {
504 #ifdef _DEBUG
505       std::cout << "XGUI_Workshop::event " << "Passed boost ptr is ok, doc id: " << aDoc->id()
506           << std::endl;
507 #endif
508     }
509     //TODO(mpv): After modifications in the XGUI_QtEvents.* this code should be like...
510     //boost::shared_ptr<Events_Message> aEventPtr = aPostponedEv->postponedMessage();
511     //processEvent(aEventPtr);
512     return true;
513   }
514   return false;
515 }
516
517 /*
518  *
519  */
520 void XGUI_Workshop::addFeature(const boost::shared_ptr<Config_FeatureMessage>& theMessage)
521 {
522   if (!theMessage) {
523 #ifdef _DEBUG
524     qDebug() << "XGUI_Workshop::addFeature: NULL message.";
525 #endif
526     return;
527   }
528   // Remember features icons
529   myIcons[QString::fromStdString(theMessage->id())] = QString::fromStdString(theMessage->icon());
530
531   //Find or create Workbench
532   QString aWchName = QString::fromStdString(theMessage->workbenchId());
533   QString aNestedFeatures = QString::fromStdString(theMessage->nestedFeatures());
534   bool isUsePropPanel = theMessage->isUseInput();
535   QString aFeatureId = QString::fromStdString(theMessage->id());
536   if (isSalomeMode()) {
537     QAction* aAction = salomeConnector()->addFeature(aWchName, aFeatureId,
538                                                      QString::fromStdString(theMessage->text()),
539                                                      QString::fromStdString(theMessage->tooltip()),
540                                                      QIcon(theMessage->icon().c_str()),
541                                                      QKeySequence(), isUsePropPanel);
542     salomeConnector()->setNestedActions(aFeatureId, aNestedFeatures.split(" ", QString::SkipEmptyParts));
543     myActionsMgr->addCommand(aAction);
544     myModule->featureCreated(aAction);
545   } else {
546
547     XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
548     XGUI_Workbench* aPage = aMenuBar->findWorkbench(aWchName);
549     if (!aPage) {
550       aPage = addWorkbench(aWchName);
551     }
552     //Find or create Group
553     QString aGroupName = QString::fromStdString(theMessage->groupId());
554     XGUI_MenuGroupPanel* aGroup = aPage->findGroup(aGroupName);
555     if (!aGroup) {
556       aGroup = aPage->addGroup(aGroupName);
557     }
558     // Check if hotkey sequence is already defined:
559     QKeySequence aHotKey = myActionsMgr->registerShortcut(
560         QString::fromStdString(theMessage->keysequence()));
561     // Create feature...
562     XGUI_Command* aCommand = aGroup->addFeature(aFeatureId,
563                                                 QString::fromStdString(theMessage->text()),
564                                                 QString::fromStdString(theMessage->tooltip()),
565                                                 QIcon(theMessage->icon().c_str()), aHotKey,
566                                                 isUsePropPanel);
567     aCommand->setNestedCommands(aNestedFeatures.split(" ", QString::SkipEmptyParts));
568     myActionsMgr->addCommand(aCommand);
569     myModule->featureCreated(aCommand);
570   }
571 }
572
573 /*
574  * Makes a signal/slot connections between Property Panel
575  * and given operation. The given operation becomes a
576  * current operation and previous operation if exists
577  */
578 void XGUI_Workshop::connectWithOperation(ModuleBase_Operation* theOperation)
579 {
580   QAction* aCommand = 0;
581   if (isSalomeMode()) {
582     aCommand = salomeConnector()->command(theOperation->getDescription()->operationId());
583   } else {
584     XGUI_MainMenu* aMenu = myMainWindow->menuObject();
585     aCommand = aMenu->feature(theOperation->getDescription()->operationId());
586   }
587   //Abort operation on uncheck the command
588   if (aCommand)
589     connect(aCommand, SIGNAL(triggered(bool)), theOperation, SLOT(setRunning(bool)));
590 }
591
592 /*
593  * Saves document with given name.
594  */
595 void XGUI_Workshop::saveDocument(const QString& theName, std::list<std::string>& theFileNames)
596 {
597   QApplication::restoreOverrideCursor();
598   SessionPtr aMgr = ModelAPI_Session::get();
599   aMgr->save(theName.toLatin1().constData(), theFileNames);
600   QApplication::restoreOverrideCursor();
601 }
602
603 bool XGUI_Workshop::isActiveOperationAborted()
604 {
605   if(!myOperationMgr->hasOperation())
606     return true;
607   return myOperationMgr->abortOperation();
608 }
609
610 //******************************************************
611 void XGUI_Workshop::onExit()
612 {
613   SessionPtr aMgr = ModelAPI_Session::get();
614   if (aMgr->isModified()) {
615     int anAnswer = QMessageBox::question(
616         myMainWindow, tr("Save current file"), tr("The document is modified, save before exit?"),
617         QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel, QMessageBox::Cancel);
618     if (anAnswer == QMessageBox::Save) {
619       bool saved = onSave();
620       if (!saved) {
621         return;
622       }
623     } else if (anAnswer == QMessageBox::Cancel) {
624       return;
625     }
626   }
627   qApp->exit();
628 }
629
630 //******************************************************
631 void XGUI_Workshop::onNew()
632 {
633   QApplication::setOverrideCursor(Qt::WaitCursor);
634   if (objectBrowser() == 0) {
635     createDockWidgets();
636     mySelector->connectViewers();
637   }
638   myViewerProxy->connectToViewer();
639   showObjectBrowser();
640   if (!isSalomeMode()) {
641     myMainWindow->showPythonConsole();
642     QMdiSubWindow* aWnd = myMainWindow->viewer()->createView();
643     aWnd->showMaximized();
644     updateCommandStatus();
645   }
646   myContextMenuMgr->connectViewer();
647   QApplication::restoreOverrideCursor();
648 }
649
650 //******************************************************
651 void XGUI_Workshop::onOpen()
652 {
653   if(!isActiveOperationAborted())
654     return;
655   //save current file before close if modified
656   SessionPtr aSession = ModelAPI_Session::get();
657   if (aSession->isModified()) {
658     //TODO(sbh): re-launch the app?
659     int anAnswer = QMessageBox::question(
660         myMainWindow, tr("Save current file"),
661         tr("The document is modified, save before opening another?"),
662         QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel, QMessageBox::Cancel);
663     if (anAnswer == QMessageBox::Save) {
664       onSave();
665     } else if (anAnswer == QMessageBox::Cancel) {
666       return;
667     }
668     aSession->moduleDocument()->close();
669     myCurrentDir = "";
670   }
671
672   //show file dialog, check if readable and open
673   myCurrentDir = QFileDialog::getExistingDirectory(mainWindow());
674   if (myCurrentDir.isEmpty())
675     return;
676   QFileInfo aFileInfo(myCurrentDir);
677   if (!aFileInfo.exists() || !aFileInfo.isReadable()) {
678     QMessageBox::critical(myMainWindow, tr("Warning"), tr("Unable to open the file."));
679     myCurrentDir = "";
680     return;
681   }
682   QApplication::setOverrideCursor(Qt::WaitCursor);
683   aSession->load(myCurrentDir.toLatin1().constData());
684   myObjectBrowser->rebuildDataTree();
685   displayAllResults();
686   updateCommandStatus();
687   QApplication::restoreOverrideCursor();
688 }
689
690 //******************************************************
691 bool XGUI_Workshop::onSave()
692 {
693   if(!isActiveOperationAborted())
694     return false;
695   if (myCurrentDir.isEmpty()) {
696     return onSaveAs();
697   }
698   std::list<std::string> aFiles;
699   saveDocument(myCurrentDir, aFiles);
700   updateCommandStatus();
701   myMainWindow->setModifiedState(false);
702   return true;
703 }
704
705 //******************************************************
706 bool XGUI_Workshop::onSaveAs()
707 {
708   if(!isActiveOperationAborted())
709     return false;
710   QFileDialog dialog(mainWindow());
711   dialog.setWindowTitle(tr("Select directory to save files..."));
712   dialog.setFileMode(QFileDialog::Directory);
713   dialog.setFilter(tr("Folders (*)"));
714   dialog.setOptions(QFileDialog::HideNameFilterDetails | QFileDialog::ShowDirsOnly);
715   dialog.setViewMode(QFileDialog::Detail);
716
717   if (!dialog.exec()) {
718     return false;
719   }
720   QString aTempDir = dialog.selectedFiles().first();
721   QDir aDir(aTempDir);
722   if (aDir.exists() && !aDir.entryInfoList(QDir::NoDotAndDotDot | QDir::AllEntries).isEmpty()) {
723     int answer = QMessageBox::question(
724         myMainWindow,
725         //: Title of the dialog which asks user if he wants to save study in existing non-empty folder
726         tr("Save"),
727         tr("The folder already contains some files, save anyway?"),
728         QMessageBox::Save | QMessageBox::Cancel);
729     if (answer == QMessageBox::Cancel) {
730       return false;
731     }
732   }
733   myCurrentDir = aTempDir;
734   if (!isSalomeMode()) {
735     myMainWindow->setCurrentDir(myCurrentDir, false);
736     myMainWindow->setModifiedState(false);
737   }
738   return onSave();
739 }
740
741 //******************************************************
742 void XGUI_Workshop::onUndo()
743 {
744   objectBrowser()->treeView()->setCurrentIndex(QModelIndex());
745   SessionPtr aMgr = ModelAPI_Session::get();
746   if (aMgr->isOperation())
747     operationMgr()->abortOperation();
748   aMgr->undo();
749   updateCommandStatus();
750 }
751
752 //******************************************************
753 void XGUI_Workshop::onRedo()
754 {
755   objectBrowser()->treeView()->setCurrentIndex(QModelIndex());
756   SessionPtr aMgr = ModelAPI_Session::get();
757   if (aMgr->isOperation())
758     operationMgr()->abortOperation();
759   aMgr->redo();
760   updateCommandStatus();
761 }
762
763 //******************************************************
764 void XGUI_Workshop::onPreferences()
765 {
766   XGUI_Prefs aModif;
767   XGUI_Preferences::editPreferences(aModif);
768   if (aModif.size() > 0) {
769     QString aSection;
770     foreach (XGUI_Pref aPref, aModif)
771     {
772       aSection = aPref.first;
773       if (aSection == XGUI_Preferences::VIEWER_SECTION) {
774         if (!isSalomeMode())
775           myMainWindow->viewer()->updateFromResources();
776       } else if (aSection == XGUI_Preferences::MENU_SECTION) {
777         if (!isSalomeMode())
778           myMainWindow->menuObject()->updateFromResources();
779       }
780     }
781   }
782 }
783
784 //******************************************************
785 ModuleBase_IModule* XGUI_Workshop::loadModule(const QString& theModule)
786 {
787   QString libName = QString::fromStdString(library(theModule.toStdString()));
788   if (libName.isEmpty()) {
789     qWarning(qPrintable(tr("Information about module \"%1\" doesn't exist.").arg(theModule)));
790     return 0;
791   }
792
793   QString err;
794   CREATE_FUNC crtInst = 0;
795
796 #ifdef WIN32
797   HINSTANCE modLib = ::LoadLibrary((LPTSTR) qPrintable(libName));
798   if (!modLib) {
799     LPVOID lpMsgBuf;
800     ::FormatMessage(
801         FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
802         0, ::GetLastError(), 0, (LPTSTR) & lpMsgBuf, 0, 0);
803     QString aMsg((char*) &lpMsgBuf);
804     err = QString("Failed to load  %1. %2").arg(libName).arg(aMsg);
805     ::LocalFree(lpMsgBuf);
806   } else {
807     crtInst = (CREATE_FUNC) ::GetProcAddress(modLib, CREATE_MODULE);
808     if (!crtInst) {
809       LPVOID lpMsgBuf;
810       ::FormatMessage(
811           FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM
812               | FORMAT_MESSAGE_IGNORE_INSERTS,
813           0, ::GetLastError(), 0, (LPTSTR) & lpMsgBuf, 0, 0);
814       QString aMsg((char*) &lpMsgBuf);
815       err = QString("Failed to find  %1 function. %2").arg( CREATE_MODULE).arg(aMsg);
816       ::LocalFree(lpMsgBuf);
817     }
818   }
819 #else
820   void* modLib = dlopen( libName.toLatin1(), RTLD_LAZY | RTLD_GLOBAL );
821   if ( !modLib ) {
822     err = QString( "Can not load library %1. %2" ).arg( libName ).arg( dlerror() );
823   } else {
824     crtInst = (CREATE_FUNC)dlsym( modLib, CREATE_MODULE );
825     if ( !crtInst ) {
826       err = QString( "Failed to find function %1. %2" ).arg( CREATE_MODULE ).arg( dlerror() );
827     }
828   }
829 #endif
830
831   ModuleBase_IModule* aModule = crtInst ? crtInst(this) : 0;
832
833   if (!err.isEmpty()) {
834     if (mainWindow()) {
835       QMessageBox::warning(mainWindow(), tr("Error"), err);
836     } else {
837       qWarning(qPrintable(err));
838     }
839   }
840   return aModule;
841 }
842
843 //******************************************************
844 bool XGUI_Workshop::activateModule()
845 {
846   Config_ModuleReader aModuleReader;
847   QString moduleName = QString::fromStdString(aModuleReader.getModuleName());
848   myModule = loadModule(moduleName);
849   if (!myModule)
850     return false;
851   myModule->createFeatures();
852   myActionsMgr->update();
853   return true;
854 }
855
856 //******************************************************
857 void XGUI_Workshop::updateCommandStatus()
858 {
859   QList<QAction*> aCommands;
860   if (isSalomeMode()) {  // update commands in SALOME mode
861     aCommands = salomeConnector()->commandList();
862   } else {
863     XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
864     foreach (XGUI_Command* aCmd, aMenuBar->features())
865       aCommands.append(aCmd);
866   }
867   SessionPtr aMgr = ModelAPI_Session::get();
868   if (aMgr->hasModuleDocument()) {
869     QAction* aUndoCmd;
870     QAction* aRedoCmd;
871     foreach(QAction* aCmd, aCommands)
872     {
873       QString aId = aCmd->data().toString();
874       if (aId == "UNDO_CMD")
875         aUndoCmd = aCmd;
876       else if (aId == "REDO_CMD")
877         aRedoCmd = aCmd;
878       else
879         // Enable all commands
880         aCmd->setEnabled(true);
881     }
882     aUndoCmd->setEnabled(aMgr->canUndo());
883     aRedoCmd->setEnabled(aMgr->canRedo());
884   } else {
885     foreach(QAction* aCmd, aCommands)
886     {
887       QString aId = aCmd->data().toString();
888       if (aId == "NEW_CMD")
889         aCmd->setEnabled(true);
890       else if (aId == "EXIT_CMD")
891         aCmd->setEnabled(true);
892       else
893         aCmd->setEnabled(false);
894     }
895   }
896   myActionsMgr->update();
897 }
898
899 //******************************************************
900 QList<QAction*> XGUI_Workshop::getModuleCommands() const
901 {
902   QList<QAction*> aCommands;
903   if (isSalomeMode()) {  // update commands in SALOME mode
904     aCommands = salomeConnector()->commandList();
905   } else {
906     XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
907     foreach(XGUI_Command* aCmd, aMenuBar->features())
908     {
909       aCommands.append(aCmd);
910     }
911   }
912   return aCommands;
913 }
914
915 //******************************************************
916 QDockWidget* XGUI_Workshop::createObjectBrowser(QWidget* theParent)
917 {
918   QDockWidget* aObjDock = new QDockWidget(theParent);
919   aObjDock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea | Qt::BottomDockWidgetArea);
920   aObjDock->setWindowTitle(tr("Object browser"));
921   aObjDock->setStyleSheet(
922       "::title { position: relative; padding-left: 5px; text-align: left center }");
923   myObjectBrowser = new XGUI_ObjectsBrowser(aObjDock);
924   connect(myObjectBrowser, SIGNAL(activePartChanged(ObjectPtr)), this,
925           SLOT(changeCurrentDocument(ObjectPtr)));
926   aObjDock->setWidget(myObjectBrowser);
927
928   myContextMenuMgr->connectObjectBrowser();
929   return aObjDock;
930 }
931
932 //******************************************************
933 /*
934  * Creates dock widgets, places them in corresponding area
935  * and tabifies if necessary.
936  */
937 void XGUI_Workshop::createDockWidgets()
938 {
939   QMainWindow* aDesktop = isSalomeMode() ? salomeConnector()->desktop() : myMainWindow;
940   QDockWidget* aObjDock = createObjectBrowser(aDesktop);
941   aDesktop->addDockWidget(Qt::LeftDockWidgetArea, aObjDock);
942   myPropertyPanel = new XGUI_PropertyPanel(aDesktop);
943   myPropertyPanel->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea | Qt::BottomDockWidgetArea);
944   aDesktop->addDockWidget(Qt::LeftDockWidgetArea, myPropertyPanel);
945   hidePropertyPanel();  //<! Invisible by default
946   hideObjectBrowser();
947   aDesktop->tabifyDockWidget(aObjDock, myPropertyPanel);
948
949   QPushButton* aOkBtn = myPropertyPanel->findChild<QPushButton*>(XGUI::PROP_PANEL_OK);
950   connect(aOkBtn, SIGNAL(clicked()), myOperationMgr, SLOT(onCommitOperation()));
951   QPushButton* aCancelBtn = myPropertyPanel->findChild<QPushButton*>(XGUI::PROP_PANEL_CANCEL);
952   connect(aCancelBtn, SIGNAL(clicked()), myOperationMgr, SLOT(onAbortOperation()));
953 //TODO(sbh): KeyReleasedProblem
954   connect(myPropertyPanel, SIGNAL(keyReleased(QKeyEvent*)), myOperationMgr,
955           SLOT(onKeyReleased(QKeyEvent*)));
956
957   connect(myPropertyPanel, SIGNAL(widgetActivated(ModuleBase_ModelWidget*)), myOperationMgr,
958           SLOT(onWidgetActivated(ModuleBase_ModelWidget*)));
959   connect(myOperationMgr, SIGNAL(activateNextWidget(ModuleBase_ModelWidget*)), myPropertyPanel,
960           SLOT(onActivateNextWidget(ModuleBase_ModelWidget*)));
961   connect(myOperationMgr, SIGNAL(operationValidated(bool)), myPropertyPanel,
962           SLOT(setAcceptEnabled(bool)));
963
964 }
965
966 //******************************************************
967 void XGUI_Workshop::showPropertyPanel()
968 {
969   QAction* aViewAct = myPropertyPanel->toggleViewAction();
970   //<! Restore ability to close panel from the window's menu
971   aViewAct->setEnabled(true);
972   myPropertyPanel->show();
973   myPropertyPanel->raise();
974 }
975
976 //******************************************************
977 void XGUI_Workshop::hidePropertyPanel()
978 {
979   QAction* aViewAct = myPropertyPanel->toggleViewAction();
980   //<! Do not allow to show empty property panel
981   aViewAct->setEnabled(false);
982   myPropertyPanel->hide();
983 }
984
985 //******************************************************
986 void XGUI_Workshop::showObjectBrowser()
987 {
988   myObjectBrowser->parentWidget()->show();
989 }
990
991 //******************************************************
992 void XGUI_Workshop::hideObjectBrowser()
993 {
994   myObjectBrowser->parentWidget()->hide();
995 }
996
997 //******************************************************
998 void XGUI_Workshop::onFeatureTriggered()
999 {
1000   QAction* aCmd = dynamic_cast<QAction*>(sender());
1001   if (aCmd) {
1002     QString aId = salomeConnector()->commandId(aCmd);
1003     if (!aId.isNull())
1004       myModule->launchOperation(aId);
1005   }
1006 }
1007
1008 //******************************************************
1009 void XGUI_Workshop::changeCurrentDocument(ObjectPtr theObj)
1010 {
1011   SessionPtr aMgr = ModelAPI_Session::get();
1012   if (theObj) {
1013     ResultPartPtr aPart = boost::dynamic_pointer_cast<ModelAPI_ResultPart>(theObj);
1014     if (aPart) {
1015       DocumentPtr aPartDoc = aPart->partDoc();
1016       if (aPartDoc) {
1017         aMgr->setActiveDocument(aPartDoc);
1018         return;
1019       }
1020     }
1021   }
1022   aMgr->setActiveDocument(aMgr->moduleDocument());
1023 }
1024
1025 //******************************************************
1026 void XGUI_Workshop::salomeViewerSelectionChanged()
1027 {
1028   emit salomeViewerSelection();
1029 }
1030
1031 //**************************************************************
1032 XGUI_SalomeViewer* XGUI_Workshop::salomeViewer() const
1033 {
1034   return mySalomeConnector->viewer();
1035 }
1036
1037 //**************************************************************
1038 void XGUI_Workshop::onContextMenuCommand(const QString& theId, bool isChecked)
1039 {
1040   QList<ObjectPtr> aObjects = mySelector->selection()->selectedObjects();
1041   if ((theId == "ACTIVATE_PART_CMD") && (aObjects.size() > 0)) {
1042     ResultPartPtr aPart = boost::dynamic_pointer_cast<ModelAPI_ResultPart>(aObjects.first());
1043     activatePart(aPart);
1044   } else if (theId == "DEACTIVATE_PART_CMD")
1045     activatePart(ResultPartPtr());
1046   else if (theId == "DELETE_CMD")
1047     deleteObjects(aObjects);
1048   else if (theId == "SHOW_CMD")
1049     showObjects(aObjects, true);
1050   else if (theId == "HIDE_CMD")
1051     showObjects(aObjects, false);
1052   else if (theId == "SHOW_ONLY_CMD")
1053     showOnlyObjects(aObjects);
1054 }
1055
1056 //**************************************************************
1057 void XGUI_Workshop::onWidgetValuesChanged()
1058 {
1059   ModuleBase_Operation* anOperation = myOperationMgr->currentOperation();
1060   FeaturePtr aFeature = anOperation->feature();
1061
1062   ModuleBase_ModelWidget* aSenderWidget = dynamic_cast<ModuleBase_ModelWidget*>(sender());
1063   //if (aCustom)
1064   //  aCustom->storeValue(aFeature);
1065
1066   const QList<ModuleBase_ModelWidget*>& aWidgets = myPropertyPanel->modelWidgets();
1067   QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
1068   for (; anIt != aLast; anIt++) {
1069     ModuleBase_ModelWidget* aCustom = *anIt;
1070     if (aCustom && (/*!aCustom->isInitialized(aFeature) ||*/aCustom == aSenderWidget)) {
1071       //aCustom->storeValue(aFeature);
1072       aCustom->storeValue();
1073     }
1074   }
1075 }
1076
1077 //**************************************************************
1078 void XGUI_Workshop::activatePart(ResultPartPtr theFeature)
1079 {
1080   if (!myPartActivating) {
1081     myPartActivating = true;
1082     if (theFeature)
1083       theFeature->activate();
1084     changeCurrentDocument(theFeature);
1085     myObjectBrowser->activatePart(theFeature);
1086     myPartActivating = false;
1087   }
1088 }
1089
1090 //**************************************************************
1091 void XGUI_Workshop::activateLastPart()
1092 {
1093   SessionPtr aMgr = ModelAPI_Session::get();
1094   DocumentPtr aDoc = aMgr->moduleDocument();
1095   std::string aGrpName = ModelAPI_ResultPart::group();
1096   ObjectPtr aLastPart = aDoc->object(aGrpName, aDoc->size(aGrpName) - 1);
1097   ResultPartPtr aPart = boost::dynamic_pointer_cast<ModelAPI_ResultPart>(aLastPart);
1098   if (aPart) {
1099     activatePart(aPart);
1100   }
1101 }
1102
1103 //**************************************************************
1104 void XGUI_Workshop::deleteObjects(const QList<ObjectPtr>& theList)
1105 {
1106   QMainWindow* aDesktop = isSalomeMode() ? salomeConnector()->desktop() : myMainWindow;
1107   QMessageBox::StandardButton aRes = QMessageBox::warning(
1108       aDesktop, tr("Delete features"), tr("Seleted features will be deleted. Continue?"),
1109       QMessageBox::No | QMessageBox::Yes, QMessageBox::No);
1110   // ToDo: definbe deleting method
1111   if (aRes == QMessageBox::Yes) {
1112     SessionPtr aMgr = ModelAPI_Session::get();
1113     aMgr->startOperation();
1114     foreach (ObjectPtr aObj, theList)
1115     {
1116       ResultPartPtr aPart = boost::dynamic_pointer_cast<ModelAPI_ResultPart>(aObj);
1117       if (aPart) {
1118         DocumentPtr aDoc = aPart->document();
1119         if (aDoc == aMgr->activeDocument()) {
1120           aDoc->close();
1121         }
1122         //aMgr->moduleDocument()->removeFeature(aPart->owner());
1123       } else {
1124         FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
1125         if (aFeature)
1126           aObj->document()->removeFeature(aFeature);
1127       }
1128     }
1129     myDisplayer->updateViewer();
1130     aMgr->finishOperation();
1131   }
1132 }
1133
1134 //**************************************************************
1135 void XGUI_Workshop::showObjects(const QList<ObjectPtr>& theList, bool isVisible)
1136 {
1137   foreach (ObjectPtr aObj, theList)
1138   {
1139     ResultPtr aRes = boost::dynamic_pointer_cast<ModelAPI_Result>(aObj);
1140     if (aRes) {
1141       if (isVisible) {
1142         myDisplayer->display(aRes, false);
1143       } else {
1144         myDisplayer->erase(aRes, false);
1145       }
1146     }
1147   }
1148   myDisplayer->updateViewer();
1149 }
1150
1151 //**************************************************************
1152 void XGUI_Workshop::showOnlyObjects(const QList<ObjectPtr>& theList)
1153 {
1154   myDisplayer->eraseAll(false);
1155   showObjects(theList, true);
1156 }
1157
1158
1159 //**************************************************************
1160 void XGUI_Workshop::updateCommandsOnViewSelection()
1161 {
1162   SessionPtr aMgr = ModelAPI_Session::get();
1163   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
1164   XGUI_Selection* aSelection = mySelector->selection();
1165   if (aSelection->getSelected().size() == 0)
1166     return;
1167
1168   QList<QAction*> aActions = getModuleCommands();
1169   foreach(QAction* aAction, aActions)
1170   {
1171     QString aId = aAction->data().toString();
1172     std::list<ModelAPI_Validator*> aValidators;
1173     std::list<std::list<std::string> > anArguments;
1174     aFactory->validators(aId.toStdString(), aValidators, anArguments);
1175     std::list<ModelAPI_Validator*>::iterator aValidator = aValidators.begin();
1176     for (; aValidator != aValidators.end(); aValidator++) {
1177       if (*aValidator) {
1178         const ModuleBase_SelectionValidator* aSelValidator =
1179             dynamic_cast<const ModuleBase_SelectionValidator*>(*aValidator);
1180         if (aSelValidator) {
1181           aAction->setEnabled(aSelValidator->isValid(aSelection));
1182         }
1183       }
1184     }
1185   }
1186 }
1187
1188 //**************************************************************
1189 void XGUI_Workshop::registerValidators() const
1190 {
1191   SessionPtr aMgr = ModelAPI_Session::get();
1192   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
1193 }
1194
1195 //**************************************************************
1196 void XGUI_Workshop::displayAllResults()
1197 {
1198   SessionPtr aMgr = ModelAPI_Session::get();
1199   DocumentPtr aRootDoc = aMgr->moduleDocument();
1200   displayDocumentResults(aRootDoc);
1201   for (int i = 0; i < aRootDoc->size(ModelAPI_ResultPart::group()); i++) {
1202     ObjectPtr aObject = aRootDoc->object(ModelAPI_ResultPart::group(), i);
1203     ResultPartPtr aPart = boost::dynamic_pointer_cast<ModelAPI_ResultPart>(aObject);
1204     displayDocumentResults(aPart->partDoc());
1205   }
1206   myDisplayer->updateViewer();
1207 }
1208
1209 //**************************************************************
1210 void XGUI_Workshop::displayDocumentResults(DocumentPtr theDoc)
1211 {
1212   if (!theDoc)
1213     return;
1214   displayGroupResults(theDoc, ModelAPI_ResultConstruction::group());
1215   displayGroupResults(theDoc, ModelAPI_ResultBody::group());
1216 }
1217
1218 //**************************************************************
1219 void XGUI_Workshop::displayGroupResults(DocumentPtr theDoc, std::string theGroup)
1220 {
1221   for (int i = 0; i < theDoc->size(theGroup); i++)
1222     myDisplayer->display(theDoc->object(theGroup, i), false);
1223 }