Salome HOME
Merge branch 'master' of newgeom:newgeom
[modules/shaper.git] / src / XGUI / XGUI_Workshop.cpp
1 #include "XGUI_Module.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 "ModuleBase_WidgetFactory.h"
12 #include "XGUI_SelectionMgr.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
24 #include <Model_Events.h>
25 #include <ModelAPI_PluginManager.h>
26 #include <ModelAPI_Feature.h>
27 #include <ModelAPI_Data.h>
28 #include <ModelAPI_AttributeDocRef.h>
29
30 #include <Events_Loop.h>
31 #include <Events_Error.h>
32 #include <ModuleBase_Operation.h>
33 #include <ModuleBase_Operation.h>
34 #include <ModuleBase_OperationDescription.h>
35 #include <Config_Common.h>
36 #include <Config_FeatureMessage.h>
37 #include <Config_PointerMessage.h>
38 #include <Config_ModuleReader.h>
39
40 #include <QApplication>
41 #include <QFileDialog>
42 #include <QMessageBox>
43 #include <QMdiSubWindow>
44 #include <QPushButton>
45 #include <QDockWidget>
46 #include <QLayout>
47
48 #ifdef _DEBUG
49 #include <QDebug>
50 #endif
51
52 #ifdef WIN32
53 #include <windows.h>
54 #else
55 #include <dlfcn.h>
56 #endif
57
58
59 QMap<QString, QString> XGUI_Workshop::myIcons;
60
61 QString XGUI_Workshop::featureIcon(const std::string& theId)
62 {
63   QString aId(theId.c_str());
64   if (myIcons.contains(aId))
65     return myIcons[aId];
66   return QString();
67 }
68
69 XGUI_Workshop::XGUI_Workshop(XGUI_SalomeConnector* theConnector)
70   : QObject(),
71   myCurrentFile(QString()),
72   myPartSetModule(NULL),
73   mySalomeConnector(theConnector),
74   myPropertyPanel(0),
75   myObjectBrowser(0),
76   myDisplayer(0)
77 {
78   myMainWindow = mySalomeConnector? 0 : new XGUI_MainWindow();
79
80   myDisplayer = new XGUI_Displayer(this);
81
82   mySelector = new XGUI_SelectionMgr(this);
83
84   myOperationMgr = new XGUI_OperationMgr(this);
85   myActionsMgr = new XGUI_ActionsMgr(this);
86   myErrorDlg = new XGUI_ErrorDialog(myMainWindow);
87   myContextMenuMgr = new XGUI_ContextMenuMgr(this);
88
89   myViewerProxy = new XGUI_ViewerProxy(this);
90
91   connect(myOperationMgr, SIGNAL(operationStarted()),  this, SLOT(onOperationStarted()));
92   connect(myOperationMgr, SIGNAL(operationStopped(ModuleBase_Operation*)),
93           this, SLOT(onOperationStopped(ModuleBase_Operation*)));
94   connect(this, SIGNAL(errorOccurred(const QString&)), myErrorDlg, SLOT(addError(const QString&)));
95 }
96
97 //******************************************************
98 XGUI_Workshop::~XGUI_Workshop(void)
99 {
100 }
101
102 //******************************************************
103 void XGUI_Workshop::startApplication()
104 {
105   initMenu();
106   //Initialize event listening
107   Events_Loop* aLoop = Events_Loop::loop();
108   aLoop->registerListener(this, Events_Error::errorID()); //!< Listening application errors.
109   //TODO(sbh): Implement static method to extract event id [SEID]
110   Events_ID aFeatureId = aLoop->eventByName(EVENT_FEATURE_LOADED);
111   aLoop->registerListener(this, aFeatureId);
112   Events_ID aPartSetId = aLoop->eventByName("PartSetModuleEvent");
113   aLoop->registerListener(this, aPartSetId);
114   Events_ID aFeatureUpdatedId = aLoop->eventByName(EVENT_FEATURE_UPDATED);
115   aLoop->registerListener(this, aFeatureUpdatedId);
116   activateModule();
117   if (myMainWindow) {
118     myMainWindow->show();
119     updateCommandStatus();
120   }
121   onNew();
122 }
123
124 //******************************************************
125 void XGUI_Workshop::initMenu()
126 {
127   if (isSalomeMode()) {
128     // Create only Undo, Redo commands
129     salomeConnector()->addEditCommand("UNDO_CMD", 
130                                       tr("Undo"), tr("Undo last command"),
131                                       QIcon(":pictures/undo.png"), 
132                                       false, this, SLOT(onUndo()),
133                                       QKeySequence::Undo);
134     salomeConnector()->addEditCommand("REDO_CMD", 
135                                       tr("Redo"), tr("Redo last command"),
136                                       QIcon(":pictures/redo.png"), 
137                                       false, this, SLOT(onRedo()),
138                                       QKeySequence::Redo);
139     salomeConnector()->addEditMenuSeparator();
140     return;
141   }
142   XGUI_Workbench* aPage = myMainWindow->menuObject()->generalPage();
143
144   // File commands group
145   XGUI_MenuGroupPanel* aGroup = aPage->addGroup("Default");
146
147   XGUI_Command* aCommand;
148
149   aCommand = aGroup->addFeature("SAVE_CMD", tr("Save..."), tr("Save the document"),
150                                 QIcon(":pictures/save.png"), QKeySequence::Save);
151   aCommand->connectTo(this, SLOT(onSave()));
152   //aCommand->disable();
153
154   aCommand = aGroup->addFeature("UNDO_CMD", tr("Undo"), tr("Undo last command"),
155                                 QIcon(":pictures/undo.png"), QKeySequence::Undo);
156   aCommand->connectTo(this, SLOT(onUndo()));
157
158   aCommand = aGroup->addFeature("REDO_CMD", tr("Redo"), tr("Redo last command"),
159                                 QIcon(":pictures/redo.png"), QKeySequence::Redo);
160   aCommand->connectTo(this, SLOT(onRedo()));
161
162   aCommand = aGroup->addFeature("REBUILD_CMD", tr("Rebuild"), tr("Rebuild data objects"),
163                                 QIcon(":pictures/rebuild.png"));
164
165   aCommand = aGroup->addFeature("SAVEAS_CMD", tr("Save as..."), tr("Save the document into a file"),
166                                 QIcon(":pictures/save.png"));
167   aCommand->connectTo(this, SLOT(onSaveAs()));
168   //aCommand->disable();
169
170   aCommand = aGroup->addFeature("OPEN_CMD", tr("Open..."), tr("Open a new document"),
171                                 QIcon(":pictures/open.png"), QKeySequence::Open);
172   aCommand->connectTo(this, SLOT(onOpen()));
173
174   //aCommand = aGroup->addFeature("NEW_CMD", tr("New"), tr("Create a new document"),
175   //                              QIcon(":pictures/new.png"), QKeySequence::New);
176   //aCommand->connectTo(this, SLOT(onNew()));
177
178   aCommand = aGroup->addFeature("EXIT_CMD", tr("Exit"), tr("Exit application"),
179                                 QIcon(":pictures/close.png"), QKeySequence::Close);
180   aCommand->connectTo(this, SLOT(onExit()));
181
182   myContextMenuMgr->createActions();
183 }
184
185 //******************************************************
186 XGUI_Workbench* XGUI_Workshop::addWorkbench(const QString& theName)
187 {
188   XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
189   return aMenuBar->addWorkbench(theName);
190 }
191
192 //******************************************************
193 void XGUI_Workshop::processEvent(const Events_Message* theMessage)
194 {
195   //A message to start feature creation received.
196   static Events_ID aFeatureLoadedId = Events_Loop::loop()->eventByName(EVENT_FEATURE_LOADED);
197   if (theMessage->eventID() == aFeatureLoadedId) {
198     const Config_FeatureMessage* aFeatureMsg = dynamic_cast<const Config_FeatureMessage*>(theMessage);
199     addFeature(aFeatureMsg);
200     return;
201   }
202   //Update property panel on corresponding message. If there is no current operation (no
203   //property panel), or received message has different feature to the current - do nothing.
204   static Events_ID aFeatureUpdatedId = Events_Loop::loop()->eventByName(EVENT_FEATURE_UPDATED);
205   if (theMessage->eventID() == aFeatureUpdatedId && myOperationMgr->hasOperation())
206   {
207     const Model_FeatureUpdatedMessage* anUpdateMsg =
208         dynamic_cast<const Model_FeatureUpdatedMessage*>(theMessage);
209     boost::shared_ptr<ModelAPI_Feature> aNewFeature = anUpdateMsg->feature();
210     boost::shared_ptr<ModelAPI_Feature> aCurrentFeature = myOperationMgr->currentOperation()->feature();
211     if(aNewFeature == aCurrentFeature) {
212       myPropertyPanel->updateContentWidget(aCurrentFeature);
213     }
214   }
215   //An operation passed by message. Start it, process and commit.
216   const Config_PointerMessage* aPartSetMsg = dynamic_cast<const Config_PointerMessage*>(theMessage);
217   if (aPartSetMsg) {
218     ModuleBase_Operation* anOperation =
219         (ModuleBase_Operation*)(aPartSetMsg->pointer());
220
221     if (myOperationMgr->startOperation(anOperation)) {
222       myPropertyPanel->updateContentWidget(anOperation->feature());
223       if (anOperation->getDescription()->xmlRepresentation().isEmpty()) {
224         anOperation->commit();
225         updateCommandStatus();
226       }
227     }
228     return;
229   }
230   //Show error dialog if error message received.
231   const Events_Error* anAppError = dynamic_cast<const Events_Error*>(theMessage);
232   if (anAppError) {
233     emit errorOccurred(QString::fromLatin1(anAppError->description()));
234     myErrorDlg->show();
235     myErrorDlg->raise();
236     myErrorDlg->activateWindow();
237   }
238
239 }
240
241 //******************************************************
242 void XGUI_Workshop::onOperationStarted()
243 {
244   ModuleBase_Operation* aOperation = myOperationMgr->currentOperation();
245
246   if(!aOperation->getDescription()->xmlRepresentation().isEmpty()) { //!< No need for property panel
247     connectWithOperation(aOperation);
248
249     showPropertyPanel();
250
251     ModuleBase_WidgetFactory aFactory = ModuleBase_WidgetFactory(aOperation);
252     QWidget* aContent = myPropertyPanel->contentWidget();
253     qDeleteAll(aContent->children());
254     aFactory.createWidget(aContent);
255     myPropertyPanel->setModelWidgets(aFactory.getModelWidgets());
256     myPropertyPanel->setWindowTitle(aOperation->getDescription()->description());
257   }
258 }
259
260 //******************************************************
261 void XGUI_Workshop::onOperationStopped(ModuleBase_Operation* theOperation)
262 {
263   ModuleBase_Operation* aOperation = myOperationMgr->currentOperation();
264
265   //!< No need for property panel
266   updateCommandStatus();
267   hidePropertyPanel();
268   if(myOperationMgr->operationsCount() > 1) {
269     myActionsMgr->updateAction(theOperation->getDescription()->operationId());
270     return;
271   }
272   if(!aOperation->getDescription()->xmlRepresentation().isEmpty()) { 
273     myActionsMgr->restoreCommandState();
274   }
275 }
276
277 /*
278  *
279  */
280 void XGUI_Workshop::addFeature(const Config_FeatureMessage* theMessage)
281 {
282   if (!theMessage) {
283 #ifdef _DEBUG
284     qDebug() << "XGUI_Workshop::addFeature: NULL message.";
285 #endif
286     return;
287   }
288   // Remember features icons
289   myIcons[QString::fromStdString(theMessage->id())] = QString::fromStdString(theMessage->icon());
290
291   //Find or create Workbench
292   QString aWchName = QString::fromStdString(theMessage->workbenchId());
293   QString aNestedFeatures = QString::fromStdString(theMessage->nestedFeatures());
294   bool isUsePropPanel = theMessage->isUseInput();
295   if (isSalomeMode()) {
296     QString aId = QString::fromStdString(theMessage->id());
297     salomeConnector()->addFeature(aWchName,
298                                   QString::fromStdString(theMessage->id()),
299                                   aId,
300                                   QString::fromStdString(theMessage->tooltip()),
301                                   QIcon(theMessage->icon().c_str()),
302                                   isUsePropPanel, this, 
303                                   SLOT(onFeatureTriggered()), QKeySequence());
304     myActionsMgr->addCommand(aId, salomeConnector()->command(aId));
305     salomeConnector()->setNestedActions(aId, aNestedFeatures.split(" "));
306
307   } else {
308
309     XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
310     XGUI_Workbench* aPage = aMenuBar->findWorkbench(aWchName);
311     if (!aPage) {
312       aPage = addWorkbench(aWchName);
313     }
314     //Find or create Group
315     QString aGroupName = QString::fromStdString(theMessage->groupId());
316     XGUI_MenuGroupPanel* aGroup = aPage->findGroup(aGroupName);
317     if (!aGroup) {
318       aGroup = aPage->addGroup(aGroupName);
319     }
320     //Create feature...
321     XGUI_Command* aCommand = aGroup->addFeature(QString::fromStdString(theMessage->id()),
322                                                 QString::fromStdString(theMessage->text()),
323                                                 QString::fromStdString(theMessage->tooltip()),
324                                                 QIcon(theMessage->icon().c_str()),
325                                                 QKeySequence(), isUsePropPanel);
326     aCommand->setUnblockableCommands(aNestedFeatures.split(" "));
327     myActionsMgr->addCommand(aCommand);
328     myPartSetModule->featureCreated(aCommand);
329   }
330 }
331
332 /*
333  * Makes a signal/slot connections between Property Panel
334  * and given operation. The given operation becomes a
335  * current operation and previous operation if exists
336  */
337 void XGUI_Workshop::connectWithOperation(ModuleBase_Operation* theOperation)
338 {
339   QPushButton* aOkBtn = myPropertyPanel->findChild<QPushButton*>(XGUI::PROP_PANEL_OK);
340   connect(aOkBtn, SIGNAL(clicked()), theOperation, SLOT(commit()));
341   QPushButton* aCancelBtn = myPropertyPanel->findChild<QPushButton*>(XGUI::PROP_PANEL_CANCEL);
342   connect(aCancelBtn, SIGNAL(clicked()), theOperation, SLOT(abort()));
343
344   QAction* aCommand = 0;
345   if (isSalomeMode()) {
346     aCommand = salomeConnector()->command(theOperation->getDescription()->operationId());
347   } else {
348     XGUI_MainMenu* aMenu = myMainWindow->menuObject();
349     aCommand = aMenu->feature(theOperation->getDescription()->operationId());
350   }
351   //Abort operation on uncheck the command
352   connect(aCommand, SIGNAL(triggered(bool)), theOperation, SLOT(setRunning(bool)));
353 }
354
355 /*
356  * Saves document with given name.
357  */
358 void XGUI_Workshop::saveDocument(QString theName)
359 {
360   QApplication::restoreOverrideCursor();
361   boost::shared_ptr<ModelAPI_PluginManager> aMgr = ModelAPI_PluginManager::get();
362   boost::shared_ptr<ModelAPI_Document> aDoc = aMgr->rootDocument();
363   aDoc->save(theName.toLatin1().constData());
364   QApplication::restoreOverrideCursor();
365 }
366
367 //******************************************************
368 void XGUI_Workshop::onExit()
369 {
370   boost::shared_ptr<ModelAPI_PluginManager> aMgr = ModelAPI_PluginManager::get();
371   boost::shared_ptr<ModelAPI_Document> aDoc = aMgr->rootDocument();
372   if(aDoc->isModified()) {
373     int anAnswer = QMessageBox::question(
374         myMainWindow, tr("Save current file"),
375         tr("The document is modified, save before exit?"),
376         QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel, QMessageBox::Cancel);
377     if(anAnswer == QMessageBox::Save) {
378       onSave();
379     } else if (anAnswer == QMessageBox::Cancel) {
380       return;
381     }
382   }
383   qApp->exit();
384 }
385
386 //******************************************************
387 void XGUI_Workshop::onNew()
388 {
389   QApplication::setOverrideCursor(Qt::WaitCursor);
390   if (objectBrowser() == 0) {
391     createDockWidgets();
392     mySelector->connectViewers();
393   }
394   myViewerProxy->connectToViewer();
395   showObjectBrowser();
396   if (!isSalomeMode()) {
397     myMainWindow->showPythonConsole();
398     QMdiSubWindow* aWnd = myMainWindow->viewer()->createView();
399     aWnd->showMaximized();
400     updateCommandStatus();
401   }
402   QApplication::restoreOverrideCursor();
403 }
404
405 //******************************************************
406 void XGUI_Workshop::onOpen()
407 {
408   //save current file before close if modified
409   boost::shared_ptr<ModelAPI_PluginManager> aMgr = ModelAPI_PluginManager::get();
410   boost::shared_ptr<ModelAPI_Document> aDoc = aMgr->rootDocument();
411   if(aDoc->isModified()) {
412     //TODO(sbh): re-launch the app?
413     int anAnswer = QMessageBox::question(
414         myMainWindow, tr("Save current file"),
415         tr("The document is modified, save before opening another?"),
416         QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel, QMessageBox::Cancel);
417     if(anAnswer == QMessageBox::Save) {
418       onSave();
419     } else if (anAnswer == QMessageBox::Cancel) {
420       return;
421     }
422     aDoc->close();
423     myCurrentFile = "";
424   }
425
426   //show file dialog, check if readable and open
427   myCurrentFile = QFileDialog::getExistingDirectory(mainWindow());
428   if(myCurrentFile.isEmpty())
429     return;
430   QFileInfo aFileInfo(myCurrentFile);
431   if(!aFileInfo.exists() || !aFileInfo.isReadable()) {
432     QMessageBox::critical(myMainWindow, tr("Warning"), tr("Unable to open the file."));
433     myCurrentFile = "";
434     return;
435   }
436   QApplication::setOverrideCursor(Qt::WaitCursor);
437   aDoc->load(myCurrentFile.toLatin1().constData());
438   QApplication::restoreOverrideCursor();
439   updateCommandStatus();
440 }
441
442 //******************************************************
443 void XGUI_Workshop::onSave()
444 {
445   if(myCurrentFile.isEmpty()) {
446     onSaveAs();
447     return;
448   }
449   saveDocument(myCurrentFile);
450   updateCommandStatus();
451 }
452
453 //******************************************************
454 void XGUI_Workshop::onSaveAs()
455 {
456   QString aTemp = myCurrentFile;
457   myCurrentFile = QFileDialog::getSaveFileName(mainWindow());
458   if(myCurrentFile.isEmpty()) {
459     myCurrentFile = aTemp;
460     return;
461   }
462   QFileInfo aFileInfo(myCurrentFile);
463   if(aFileInfo.exists() && !aFileInfo.isWritable()) {
464     QMessageBox::critical(myMainWindow, tr("Warning"), tr("Unable to save the file."));
465     return;
466   }
467   onSave();
468 }
469
470 //******************************************************
471 void XGUI_Workshop::onUndo()
472 {
473   objectBrowser()->setCurrentIndex(QModelIndex());
474   boost::shared_ptr<ModelAPI_PluginManager> aMgr = ModelAPI_PluginManager::get();
475   boost::shared_ptr<ModelAPI_Document> aDoc = aMgr->rootDocument();
476   if (!operationMgr()->abortOperation())
477     return;
478   aDoc->undo();
479   updateCommandStatus();
480 }
481
482 //******************************************************
483 void XGUI_Workshop::onRedo()
484 {
485   objectBrowser()->setCurrentIndex(QModelIndex());
486   boost::shared_ptr<ModelAPI_PluginManager> aMgr = ModelAPI_PluginManager::get();
487   boost::shared_ptr<ModelAPI_Document> aDoc = aMgr->rootDocument();
488   aDoc->redo();
489   updateCommandStatus();
490 }
491
492 //******************************************************
493 XGUI_Module* XGUI_Workshop::loadModule(const QString& theModule)
494 {
495   QString libName =
496       QString::fromStdString(library(theModule.toStdString()));
497   if (libName.isEmpty()) {
498     qWarning(
499     qPrintable( tr( "Information about module \"%1\" doesn't exist." ).arg( theModule ) ));
500     return 0;
501   }
502
503   QString err;
504   CREATE_FUNC crtInst = 0;
505
506 #ifdef WIN32
507   HINSTANCE modLib = ::LoadLibrary((LPTSTR) qPrintable(libName));
508   if (!modLib) {
509     LPVOID lpMsgBuf;
510     ::FormatMessage(
511         FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
512         0, ::GetLastError(), 0, (LPTSTR) & lpMsgBuf, 0, 0);
513     QString aMsg((char*) &lpMsgBuf);
514     err = QString("Failed to load  %1. %2").arg(libName).arg(aMsg);
515     ::LocalFree(lpMsgBuf);
516   } else {
517     crtInst = (CREATE_FUNC) ::GetProcAddress(modLib, CREATE_MODULE);
518     if (!crtInst) {
519       LPVOID lpMsgBuf;
520       ::FormatMessage(
521           FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM
522               | FORMAT_MESSAGE_IGNORE_INSERTS,
523           0, ::GetLastError(), 0, (LPTSTR) & lpMsgBuf, 0, 0);
524       QString aMsg((char*) &lpMsgBuf);
525       err = QString("Failed to find  %1 function. %2").arg( CREATE_MODULE).arg(aMsg);
526       ::LocalFree(lpMsgBuf);
527     }
528   }
529 #else
530   void* modLib = dlopen( libName.toLatin1(), RTLD_LAZY );
531   if ( !modLib ) {
532     err = QString( "Can not load library %1. %2" ).arg( libName ).arg( dlerror() );
533   } else {
534     crtInst = (CREATE_FUNC)dlsym( modLib, CREATE_MODULE );
535     if ( !crtInst ) {
536       err = QString( "Failed to find function %1. %2" ).arg( CREATE_MODULE ).arg( dlerror() );
537     }
538   }
539 #endif
540
541   XGUI_Module* aModule = crtInst ? crtInst(this) : 0;
542
543   if (!err.isEmpty()) {
544     if (mainWindow()) {
545       QMessageBox::warning(mainWindow(), tr("Error"), err);
546     } else {
547       qWarning( qPrintable( err ));
548     }
549   }
550   return aModule;
551 }
552
553 //******************************************************
554 bool XGUI_Workshop::activateModule()
555 {
556   Config_ModuleReader aModuleReader;
557   QString moduleName = QString::fromStdString(aModuleReader.getModuleName());
558   myPartSetModule = loadModule(moduleName);
559   if (!myPartSetModule)
560     return false;
561   myPartSetModule->createFeatures();
562   return true;
563 }
564
565 //******************************************************
566 void XGUI_Workshop::updateCommandStatus()
567 {
568   if (isSalomeMode()) // TODO: update commands in SALOME
569     return;
570   XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
571
572   QList<XGUI_Command*> aCommands = aMenuBar->features();
573   QList<XGUI_Command*>::const_iterator aIt;
574
575   boost::shared_ptr<ModelAPI_PluginManager> aMgr = ModelAPI_PluginManager::get();
576   if (aMgr->hasRootDocument()) {
577     XGUI_Command* aUndoCmd;
578     XGUI_Command* aRedoCmd;
579     foreach(XGUI_Command* aCmd, aCommands) {
580       if (aCmd->id() == "UNDO_CMD")
581         aUndoCmd = aCmd;
582       else if (aCmd->id() == "REDO_CMD")
583         aRedoCmd = aCmd;
584       else // Enable all commands
585         aCmd->enable();
586     }
587     boost::shared_ptr<ModelAPI_Document> aDoc = aMgr->rootDocument();
588     aUndoCmd->setEnabled(aDoc->canUndo());
589     aRedoCmd->setEnabled(aDoc->canRedo());
590   } else {
591     foreach(XGUI_Command* aCmd, aCommands) {
592       if (aCmd->id() == "NEW_CMD")
593         aCmd->enable();
594       else if (aCmd->id() == "EXIT_CMD")
595         aCmd->enable();
596       else 
597         aCmd->disable();
598     }
599   }
600 }
601
602 //******************************************************
603 QDockWidget* XGUI_Workshop::createObjectBrowser(QWidget* theParent)
604 {
605   QDockWidget* aObjDock = new QDockWidget(theParent);
606   aObjDock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
607   aObjDock->setWindowTitle(tr("Object browser"));
608   myObjectBrowser = new XGUI_ObjectsBrowser(aObjDock);
609   connect(myObjectBrowser, SIGNAL(activePartChanged(FeaturePtr)), this, SLOT(changeCurrentDocument(FeaturePtr)));
610   aObjDock->setWidget(myObjectBrowser);
611
612   myContextMenuMgr->connectObjectBrowser();
613   return aObjDock;
614 }
615
616 //******************************************************
617 /*
618  * Creates dock widgets, places them in corresponding area
619  * and tabifies if necessary.
620  */
621 void XGUI_Workshop::createDockWidgets()
622 {
623   QMainWindow* aDesktop = isSalomeMode()? salomeConnector()->desktop() :
624                                           myMainWindow;
625   QDockWidget* aObjDock = createObjectBrowser(aDesktop);
626   aDesktop->addDockWidget(Qt::LeftDockWidgetArea, aObjDock);
627   myPropertyPanel = new XGUI_PropertyPanel(aDesktop);
628   aDesktop->addDockWidget(Qt::LeftDockWidgetArea, myPropertyPanel);
629   hidePropertyPanel(); //<! Invisible by default
630   hideObjectBrowser();
631   aDesktop->tabifyDockWidget(aObjDock, myPropertyPanel);
632 }
633
634 //******************************************************
635 void XGUI_Workshop::showPropertyPanel()
636 {
637   QAction* aViewAct = myPropertyPanel->toggleViewAction();
638   //<! Restore ability to close panel from the window's menu
639   aViewAct->setEnabled(true);
640   myPropertyPanel->show();
641   myPropertyPanel->raise();
642 }
643
644 //******************************************************
645 void XGUI_Workshop::hidePropertyPanel()
646 {
647   QAction* aViewAct = myPropertyPanel->toggleViewAction();
648   //<! Do not allow to show empty property panel
649   aViewAct->setEnabled(false);
650   myPropertyPanel->hide();
651 }
652
653 //******************************************************
654 void XGUI_Workshop::showObjectBrowser()
655 {
656   myObjectBrowser->parentWidget()->show();
657 }
658
659 //******************************************************
660 void XGUI_Workshop::hideObjectBrowser()
661 {
662   myObjectBrowser->parentWidget()->hide();
663 }
664
665 //******************************************************
666 void XGUI_Workshop::onFeatureTriggered()
667 {
668   QAction* aCmd = dynamic_cast<QAction*>(sender());
669   if (aCmd) {
670     QString aId = salomeConnector()->commandId(aCmd);
671     if (!aId.isNull())
672       myPartSetModule->launchOperation(aId);
673   }
674 }
675
676 //******************************************************
677 void XGUI_Workshop::changeCurrentDocument(FeaturePtr thePart)
678 {
679   boost::shared_ptr<ModelAPI_PluginManager> aMgr = ModelAPI_PluginManager::get();
680   if (thePart) {
681     boost::shared_ptr<ModelAPI_AttributeDocRef> aDocRef = thePart->data()->docRef("PartDocument");
682     if (aDocRef)
683       aMgr->setCurrentDocument(aDocRef->value());
684   } else {
685     aMgr->setCurrentDocument(aMgr->rootDocument());
686   }
687 }
688
689 //******************************************************
690 void XGUI_Workshop::salomeViewerSelectionChanged()
691 {
692   emit salomeViewerSelection();
693 }
694
695
696 //**************************************************************
697 XGUI_SalomeViewer* XGUI_Workshop::salomeViewer() const 
698
699   return mySalomeConnector->viewer(); 
700 }