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