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