]> SALOME platform Git repositories - modules/shaper.git/blob - src/XGUI/XGUI_Workshop.cpp
Salome HOME
Provide pop-up menu in Salome mode.
[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   myCurrentFile(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(operationStopped(ModuleBase_Operation*)),
96           this, SLOT(onOperationStopped(ModuleBase_Operation*)));
97   connect(this, SIGNAL(errorOccurred(const QString&)), myErrorDlg, SLOT(addError(const QString&)));
98 }
99
100 //******************************************************
101 XGUI_Workshop::~XGUI_Workshop(void)
102 {
103 }
104
105 //******************************************************
106 void XGUI_Workshop::startApplication()
107 {
108   initMenu();
109   //Initialize event listening
110   Events_Loop* aLoop = Events_Loop::loop();
111   aLoop->registerListener(this, Events_Error::errorID()); //!< Listening application errors.
112   //TODO(sbh): Implement static method to extract event id [SEID]
113   Events_ID aFeatureId = aLoop->eventByName(EVENT_FEATURE_LOADED);
114   aLoop->registerListener(this, aFeatureId);
115   Events_ID aPartSetId = aLoop->eventByName("PartSetModuleEvent");
116   aLoop->registerListener(this, aPartSetId);
117   Events_ID aFeatureUpdatedId = aLoop->eventByName(EVENT_FEATURE_UPDATED);
118   aLoop->registerListener(this, aFeatureUpdatedId);
119   activateModule();
120   if (myMainWindow) {
121     myMainWindow->show();
122     updateCommandStatus();
123   }
124   onNew();
125 }
126
127 //******************************************************
128 void XGUI_Workshop::initMenu()
129 {
130   myContextMenuMgr->createActions();
131
132   if (isSalomeMode()) {
133     // Create only Undo, Redo commands
134     QAction* aAction = salomeConnector()->addEditCommand("UNDO_CMD", 
135                                       tr("Undo"), tr("Undo last command"),
136                                       QIcon(":pictures/undo.png"), 
137                                       QKeySequence::Undo, false);
138     connect(aAction, SIGNAL(triggered(bool)), this, SLOT(onUndo()));
139     aAction = salomeConnector()->addEditCommand("REDO_CMD", 
140                                       tr("Redo"), tr("Redo last command"),
141                                       QIcon(":pictures/redo.png"), 
142                                       QKeySequence::Redo, false);
143     connect(aAction, SIGNAL(triggered(bool)), this, SLOT(onRedo()));
144     salomeConnector()->addEditMenuSeparator();
145     return;
146   }
147   XGUI_Workbench* aPage = myMainWindow->menuObject()->generalPage();
148
149   // File commands group
150   XGUI_MenuGroupPanel* aGroup = aPage->addGroup("Default");
151
152   XGUI_Command* aCommand;
153
154   aCommand = aGroup->addFeature("SAVE_CMD", tr("Save..."), tr("Save the document"),
155                                 QIcon(":pictures/save.png"), QKeySequence::Save);
156   aCommand->connectTo(this, SLOT(onSave()));
157   //aCommand->disable();
158
159   aCommand = aGroup->addFeature("UNDO_CMD", tr("Undo"), tr("Undo last command"),
160                                 QIcon(":pictures/undo.png"), QKeySequence::Undo);
161   aCommand->connectTo(this, SLOT(onUndo()));
162
163   aCommand = aGroup->addFeature("REDO_CMD", tr("Redo"), tr("Redo last command"),
164                                 QIcon(":pictures/redo.png"), QKeySequence::Redo);
165   aCommand->connectTo(this, SLOT(onRedo()));
166
167   aCommand = aGroup->addFeature("REBUILD_CMD", tr("Rebuild"), tr("Rebuild data objects"),
168                                 QIcon(":pictures/rebuild.png"));
169
170   aCommand = aGroup->addFeature("SAVEAS_CMD", tr("Save as..."), tr("Save the document into a file"),
171                                 QIcon(":pictures/save.png"));
172   aCommand->connectTo(this, SLOT(onSaveAs()));
173   //aCommand->disable();
174
175   aCommand = aGroup->addFeature("OPEN_CMD", tr("Open..."), tr("Open a new document"),
176                                 QIcon(":pictures/open.png"), QKeySequence::Open);
177   aCommand->connectTo(this, SLOT(onOpen()));
178
179   //aCommand = aGroup->addFeature("NEW_CMD", tr("New"), tr("Create a new document"),
180   //                              QIcon(":pictures/new.png"), QKeySequence::New);
181   //aCommand->connectTo(this, SLOT(onNew()));
182
183   aCommand = aGroup->addFeature("EXIT_CMD", tr("Exit"), tr("Exit application"),
184                                 QIcon(":pictures/close.png"), QKeySequence::Close);
185   aCommand->connectTo(this, SLOT(onExit()));
186 }
187
188 //******************************************************
189 XGUI_Workbench* XGUI_Workshop::addWorkbench(const QString& theName)
190 {
191   XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
192   return aMenuBar->addWorkbench(theName);
193 }
194
195 //******************************************************
196 void XGUI_Workshop::processEvent(const Events_Message* theMessage)
197 {
198   //A message to start feature creation received.
199   static Events_ID aFeatureLoadedId = Events_Loop::loop()->eventByName(EVENT_FEATURE_LOADED);
200   if (theMessage->eventID() == aFeatureLoadedId) {
201     const Config_FeatureMessage* aFeatureMsg = dynamic_cast<const Config_FeatureMessage*>(theMessage);
202     addFeature(aFeatureMsg);
203     return;
204   }
205   //Update property panel on corresponding message. If there is no current operation (no
206   //property panel), or received message has different feature to the current - do nothing.
207   static Events_ID aFeatureUpdatedId = Events_Loop::loop()->eventByName(EVENT_FEATURE_UPDATED);
208   if (theMessage->eventID() == aFeatureUpdatedId && myOperationMgr->hasOperation())
209   {
210     const Model_FeatureUpdatedMessage* anUpdateMsg =
211         dynamic_cast<const Model_FeatureUpdatedMessage*>(theMessage);
212     FeaturePtr aNewFeature = anUpdateMsg->feature();
213     FeaturePtr aCurrentFeature = myOperationMgr->currentOperation()->feature();
214     if(aNewFeature == aCurrentFeature) {
215       myPropertyPanel->updateContentWidget(aCurrentFeature);
216     }
217   }
218   //An operation passed by message. Start it, process and commit.
219   const Config_PointerMessage* aPartSetMsg = dynamic_cast<const Config_PointerMessage*>(theMessage);
220   if (aPartSetMsg) {
221     ModuleBase_Operation* anOperation =
222         (ModuleBase_Operation*)(aPartSetMsg->pointer());
223
224     if (myOperationMgr->startOperation(anOperation)) {
225       myPropertyPanel->updateContentWidget(anOperation->feature());
226       if (anOperation->getDescription()->xmlRepresentation().isEmpty()) {
227         anOperation->commit();
228         updateCommandStatus();
229       }
230     }
231     return;
232   }
233   //Show error dialog if error message received.
234   const Events_Error* anAppError = dynamic_cast<const Events_Error*>(theMessage);
235   if (anAppError) {
236     emit errorOccurred(QString::fromLatin1(anAppError->description()));
237     myErrorDlg->show();
238     myErrorDlg->raise();
239     myErrorDlg->activateWindow();
240   }
241
242 }
243
244 //******************************************************
245 void XGUI_Workshop::onOperationStarted()
246 {
247   ModuleBase_Operation* aOperation = myOperationMgr->currentOperation();
248
249   if(!aOperation->getDescription()->xmlRepresentation().isEmpty()) { //!< No need for property panel
250     connectWithOperation(aOperation);
251
252     showPropertyPanel();
253
254     ModuleBase_WidgetFactory aFactory = ModuleBase_WidgetFactory(aOperation);
255     QWidget* aContent = myPropertyPanel->contentWidget();
256     qDeleteAll(aContent->children());
257     aFactory.createWidget(aContent);
258     myPropertyPanel->setModelWidgets(aFactory.getModelWidgets());
259     myPropertyPanel->setWindowTitle(aOperation->getDescription()->description());
260   }
261 }
262
263 //******************************************************
264 void XGUI_Workshop::onOperationStopped(ModuleBase_Operation* theOperation)
265 {
266   ModuleBase_Operation* aOperation = myOperationMgr->currentOperation();
267
268   //!< No need for property panel
269   updateCommandStatus();
270   hidePropertyPanel();
271   if(myOperationMgr->operationsCount() > 1) {
272     myActionsMgr->updateAction(theOperation->getDescription()->operationId());
273     return;
274   }
275   if(!aOperation->getDescription()->xmlRepresentation().isEmpty()) { 
276     myActionsMgr->restoreCommandState();
277   }
278 }
279
280 /*
281  *
282  */
283 void XGUI_Workshop::addFeature(const Config_FeatureMessage* theMessage)
284 {
285   if (!theMessage) {
286 #ifdef _DEBUG
287     qDebug() << "XGUI_Workshop::addFeature: NULL message.";
288 #endif
289     return;
290   }
291   // Remember features icons
292   myIcons[QString::fromStdString(theMessage->id())] = QString::fromStdString(theMessage->icon());
293
294   //Find or create Workbench
295   QString aWchName = QString::fromStdString(theMessage->workbenchId());
296   QString aNestedFeatures = QString::fromStdString(theMessage->nestedFeatures());
297   bool isUsePropPanel = theMessage->isUseInput();
298   if (isSalomeMode()) {
299     QString aId = QString::fromStdString(theMessage->id());
300     QAction* aAction = salomeConnector()->addFeature(aWchName,
301                               aId,
302                               QString::fromStdString(theMessage->text()),
303                               QString::fromStdString(theMessage->tooltip()),
304                               QIcon(theMessage->icon().c_str()),
305                               QKeySequence(), isUsePropPanel);
306     myActionsMgr->addCommand(aAction);
307     salomeConnector()->setNestedActions(aId, aNestedFeatures.split(" "));
308     myPartSetModule->featureCreated(aAction);
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   PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
359   DocumentPtr aDoc = aMgr->rootDocument();
360   aDoc->save(theName.toLatin1().constData());
361   QApplication::restoreOverrideCursor();
362 }
363
364 //******************************************************
365 void XGUI_Workshop::onExit()
366 {
367   PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
368   DocumentPtr 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   PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
407   DocumentPtr 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   PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
472   DocumentPtr 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   PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
484   DocumentPtr 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   QList<QAction*> aCommands;
566   if (isSalomeMode()) { // update commands in SALOME mode
567     aCommands = salomeConnector()->commandList();
568   } else {
569     XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
570     foreach (XGUI_Command* aCmd, aMenuBar->features())
571       aCommands.append(aCmd);
572   }
573   PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
574   if (aMgr->hasRootDocument()) {
575     QAction* aUndoCmd;
576     QAction* aRedoCmd;
577     foreach(QAction* aCmd, aCommands) {
578       QString aId = aCmd->data().toString();
579       if (aId == "UNDO_CMD")
580         aUndoCmd = aCmd;
581       else if (aId == "REDO_CMD")
582         aRedoCmd = aCmd;
583       else // Enable all commands
584         aCmd->setEnabled(true);
585     }
586     DocumentPtr aDoc = aMgr->rootDocument();
587     aUndoCmd->setEnabled(aDoc->canUndo());
588     aRedoCmd->setEnabled(aDoc->canRedo());
589   } else {
590     foreach(QAction* aCmd, aCommands) {
591       QString aId = aCmd->data().toString();
592       if (aId == "NEW_CMD")
593         aCmd->setEnabled(true);
594       else if (aId == "EXIT_CMD")
595         aCmd->setEnabled(true);
596       else 
597         aCmd->setEnabled(false);
598     }
599   }
600 }
601
602 //******************************************************
603 QDockWidget* XGUI_Workshop::createObjectBrowser(QWidget* theParent)
604 {
605   QDockWidget* aObjDock = new QDockWidget(theParent);
606   aObjDock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
607   aObjDock->setWindowTitle(tr("Object browser"));
608   myObjectBrowser = new XGUI_ObjectsBrowser(aObjDock);
609   connect(myObjectBrowser, SIGNAL(activePartChanged(FeaturePtr)), this, SLOT(changeCurrentDocument(FeaturePtr)));
610   aObjDock->setWidget(myObjectBrowser);
611
612   myContextMenuMgr->connectObjectBrowser();
613   return aObjDock;
614 }
615
616 //******************************************************
617 /*
618  * Creates dock widgets, places them in corresponding area
619  * and tabifies if necessary.
620  */
621 void XGUI_Workshop::createDockWidgets()
622 {
623   QMainWindow* aDesktop = isSalomeMode()? salomeConnector()->desktop() :
624                                           myMainWindow;
625   QDockWidget* aObjDock = createObjectBrowser(aDesktop);
626   aDesktop->addDockWidget(Qt::LeftDockWidgetArea, aObjDock);
627   myPropertyPanel = new XGUI_PropertyPanel(aDesktop);
628   aDesktop->addDockWidget(Qt::LeftDockWidgetArea, myPropertyPanel);
629   hidePropertyPanel(); //<! Invisible by default
630   hideObjectBrowser();
631   aDesktop->tabifyDockWidget(aObjDock, myPropertyPanel);
632
633   QPushButton* aOkBtn = myPropertyPanel->findChild<QPushButton*>(XGUI::PROP_PANEL_OK);
634   connect(aOkBtn, SIGNAL(clicked()), myOperationMgr, SLOT(onCommitOperation()));
635   QPushButton* aCancelBtn = myPropertyPanel->findChild<QPushButton*>(XGUI::PROP_PANEL_CANCEL);
636   connect(aCancelBtn, SIGNAL(clicked()), myOperationMgr, SLOT(onAbortOperation()));
637 }
638
639 //******************************************************
640 void XGUI_Workshop::showPropertyPanel()
641 {
642   QAction* aViewAct = myPropertyPanel->toggleViewAction();
643   //<! Restore ability to close panel from the window's menu
644   aViewAct->setEnabled(true);
645   myPropertyPanel->show();
646   myPropertyPanel->raise();
647 }
648
649 //******************************************************
650 void XGUI_Workshop::hidePropertyPanel()
651 {
652   QAction* aViewAct = myPropertyPanel->toggleViewAction();
653   //<! Do not allow to show empty property panel
654   aViewAct->setEnabled(false);
655   myPropertyPanel->hide();
656 }
657
658 //******************************************************
659 void XGUI_Workshop::showObjectBrowser()
660 {
661   myObjectBrowser->parentWidget()->show();
662 }
663
664 //******************************************************
665 void XGUI_Workshop::hideObjectBrowser()
666 {
667   myObjectBrowser->parentWidget()->hide();
668 }
669
670 //******************************************************
671 void XGUI_Workshop::onFeatureTriggered()
672 {
673   QAction* aCmd = dynamic_cast<QAction*>(sender());
674   if (aCmd) {
675     QString aId = salomeConnector()->commandId(aCmd);
676     if (!aId.isNull())
677       myPartSetModule->launchOperation(aId);
678   }
679 }
680
681 //******************************************************
682 void XGUI_Workshop::changeCurrentDocument(FeaturePtr thePart)
683 {
684   PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
685   if (thePart) {
686     boost::shared_ptr<ModelAPI_AttributeDocRef> aDocRef = thePart->data()->docRef("PartDocument");
687     if (aDocRef)
688       aMgr->setCurrentDocument(aDocRef->value());
689   } else {
690     aMgr->setCurrentDocument(aMgr->rootDocument());
691   }
692 }
693
694 //******************************************************
695 void XGUI_Workshop::salomeViewerSelectionChanged()
696 {
697   emit salomeViewerSelection();
698 }
699
700
701 //**************************************************************
702 XGUI_SalomeViewer* XGUI_Workshop::salomeViewer() const 
703
704   return mySalomeConnector->viewer(); 
705 }
706
707 //**************************************************************
708 void XGUI_Workshop::onContextMenuCommand(const QString& theId, bool isChecked)
709 {
710   if (theId == "ACTIVATE_PART_CMD")
711     activatePart(true);
712   else if (theId == "DEACTIVATE_PART_CMD") 
713     activatePart(false);
714
715 }
716
717 //**************************************************************
718 void XGUI_Workshop::activatePart(bool toActivate)
719 {
720   if (toActivate) {
721     QFeatureList aFeatures = mySelector->selectedFeatures();
722     if (aFeatures.size() > 0) {
723       changeCurrentDocument(aFeatures.first());
724       myObjectBrowser->activateCurrentPart(true);
725     }
726   } else {
727     changeCurrentDocument(FeaturePtr());
728     myObjectBrowser->activateCurrentPart(false);
729   }
730 }
731