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