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