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