]> SALOME platform Git repositories - modules/shaper.git/blob - src/XGUI/XGUI_Workshop.cpp
Salome HOME
refs #30 - Sketch base GUI: create, draw lines
[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(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   if (isSalomeMode()) {
132     // Create only Undo, Redo commands
133     salomeConnector()->addEditCommand("UNDO_CMD", 
134                                       tr("Undo"), tr("Undo last command"),
135                                       QIcon(":pictures/undo.png"), 
136                                       false, this, SLOT(onUndo()),
137                                       QKeySequence::Undo);
138     salomeConnector()->addEditCommand("REDO_CMD", 
139                                       tr("Redo"), tr("Redo last command"),
140                                       QIcon(":pictures/redo.png"), 
141                                       false, this, SLOT(onRedo()),
142                                       QKeySequence::Redo);
143     salomeConnector()->addEditMenuSeparator();
144     return;
145   }
146   XGUI_Workbench* aPage = myMainWindow->menuObject()->generalPage();
147
148   // File commands group
149   XGUI_MenuGroupPanel* aGroup = aPage->addGroup("Default");
150
151   XGUI_Command* aCommand;
152
153   aCommand = aGroup->addFeature("SAVE_CMD", tr("Save..."), tr("Save the document"),
154                                 QIcon(":pictures/save.png"), QKeySequence::Save);
155   aCommand->connectTo(this, SLOT(onSave()));
156   //aCommand->disable();
157
158   aCommand = aGroup->addFeature("UNDO_CMD", tr("Undo"), tr("Undo last command"),
159                                 QIcon(":pictures/undo.png"), QKeySequence::Undo);
160   aCommand->connectTo(this, SLOT(onUndo()));
161
162   aCommand = aGroup->addFeature("REDO_CMD", tr("Redo"), tr("Redo last command"),
163                                 QIcon(":pictures/redo.png"), QKeySequence::Redo);
164   aCommand->connectTo(this, SLOT(onRedo()));
165
166   aCommand = aGroup->addFeature("REBUILD_CMD", tr("Rebuild"), tr("Rebuild data objects"),
167                                 QIcon(":pictures/rebuild.png"));
168
169   aCommand = aGroup->addFeature("SAVEAS_CMD", tr("Save as..."), tr("Save the document into a file"),
170                                 QIcon(":pictures/save.png"));
171   aCommand->connectTo(this, SLOT(onSaveAs()));
172   //aCommand->disable();
173
174   aCommand = aGroup->addFeature("OPEN_CMD", tr("Open..."), tr("Open a new document"),
175                                 QIcon(":pictures/open.png"), QKeySequence::Open);
176   aCommand->connectTo(this, SLOT(onOpen()));
177
178   //aCommand = aGroup->addFeature("NEW_CMD", tr("New"), tr("Create a new document"),
179   //                              QIcon(":pictures/new.png"), QKeySequence::New);
180   //aCommand->connectTo(this, SLOT(onNew()));
181
182   aCommand = aGroup->addFeature("EXIT_CMD", tr("Exit"), tr("Exit application"),
183                                 QIcon(":pictures/close.png"), QKeySequence::Close);
184   aCommand->connectTo(this, SLOT(onExit()));
185
186   myContextMenuMgr->createActions();
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     salomeConnector()->addFeature(aWchName,
302                                   QString::fromStdString(theMessage->id()),
303                                   aId,
304                                   QString::fromStdString(theMessage->tooltip()),
305                                   QIcon(theMessage->icon().c_str()),
306                                   isUsePropPanel, this, 
307                                   SLOT(onFeatureTriggered()), QKeySequence());
308     myActionsMgr->addCommand(aId, salomeConnector()->command(aId));
309     salomeConnector()->setNestedActions(aId, aNestedFeatures.split(" "));
310
311   } else {
312
313     XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
314     XGUI_Workbench* aPage = aMenuBar->findWorkbench(aWchName);
315     if (!aPage) {
316       aPage = addWorkbench(aWchName);
317     }
318     //Find or create Group
319     QString aGroupName = QString::fromStdString(theMessage->groupId());
320     XGUI_MenuGroupPanel* aGroup = aPage->findGroup(aGroupName);
321     if (!aGroup) {
322       aGroup = aPage->addGroup(aGroupName);
323     }
324     //Create feature...
325     XGUI_Command* aCommand = aGroup->addFeature(QString::fromStdString(theMessage->id()),
326                                                 QString::fromStdString(theMessage->text()),
327                                                 QString::fromStdString(theMessage->tooltip()),
328                                                 QIcon(theMessage->icon().c_str()),
329                                                 QKeySequence(), isUsePropPanel);
330     aCommand->setUnblockableCommands(aNestedFeatures.split(" "));
331     myActionsMgr->addCommand(aCommand);
332     myPartSetModule->featureCreated(aCommand);
333   }
334 }
335
336 /*
337  * Makes a signal/slot connections between Property Panel
338  * and given operation. The given operation becomes a
339  * current operation and previous operation if exists
340  */
341 void XGUI_Workshop::connectWithOperation(ModuleBase_Operation* theOperation)
342 {
343   QAction* aCommand = 0;
344   if (isSalomeMode()) {
345     aCommand = salomeConnector()->command(theOperation->getDescription()->operationId());
346   } else {
347     XGUI_MainMenu* aMenu = myMainWindow->menuObject();
348     aCommand = aMenu->feature(theOperation->getDescription()->operationId());
349   }
350   //Abort operation on uncheck the command
351   connect(aCommand, SIGNAL(triggered(bool)), theOperation, SLOT(setRunning(bool)));
352 }
353
354 /*
355  * Saves document with given name.
356  */
357 void XGUI_Workshop::saveDocument(QString theName)
358 {
359   QApplication::restoreOverrideCursor();
360   PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
361   DocumentPtr aDoc = aMgr->rootDocument();
362   aDoc->save(theName.toLatin1().constData());
363   QApplication::restoreOverrideCursor();
364 }
365
366 //******************************************************
367 void XGUI_Workshop::onExit()
368 {
369   PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
370   DocumentPtr aDoc = aMgr->rootDocument();
371   if(aDoc->isModified()) {
372     int anAnswer = QMessageBox::question(
373         myMainWindow, tr("Save current file"),
374         tr("The document is modified, save before exit?"),
375         QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel, QMessageBox::Cancel);
376     if(anAnswer == QMessageBox::Save) {
377       onSave();
378     } else if (anAnswer == QMessageBox::Cancel) {
379       return;
380     }
381   }
382   qApp->exit();
383 }
384
385 //******************************************************
386 void XGUI_Workshop::onNew()
387 {
388   QApplication::setOverrideCursor(Qt::WaitCursor);
389   if (objectBrowser() == 0) {
390     createDockWidgets();
391     mySelector->connectViewers();
392   }
393   myViewerProxy->connectToViewer();
394   showObjectBrowser();
395   if (!isSalomeMode()) {
396     myMainWindow->showPythonConsole();
397     QMdiSubWindow* aWnd = myMainWindow->viewer()->createView();
398     aWnd->showMaximized();
399     updateCommandStatus();
400   }
401   QApplication::restoreOverrideCursor();
402 }
403
404 //******************************************************
405 void XGUI_Workshop::onOpen()
406 {
407   //save current file before close if modified
408   PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
409   DocumentPtr aDoc = aMgr->rootDocument();
410   if(aDoc->isModified()) {
411     //TODO(sbh): re-launch the app?
412     int anAnswer = QMessageBox::question(
413         myMainWindow, tr("Save current file"),
414         tr("The document is modified, save before opening another?"),
415         QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel, QMessageBox::Cancel);
416     if(anAnswer == QMessageBox::Save) {
417       onSave();
418     } else if (anAnswer == QMessageBox::Cancel) {
419       return;
420     }
421     aDoc->close();
422     myCurrentFile = "";
423   }
424
425   //show file dialog, check if readable and open
426   myCurrentFile = QFileDialog::getExistingDirectory(mainWindow());
427   if(myCurrentFile.isEmpty())
428     return;
429   QFileInfo aFileInfo(myCurrentFile);
430   if(!aFileInfo.exists() || !aFileInfo.isReadable()) {
431     QMessageBox::critical(myMainWindow, tr("Warning"), tr("Unable to open the file."));
432     myCurrentFile = "";
433     return;
434   }
435   QApplication::setOverrideCursor(Qt::WaitCursor);
436   aDoc->load(myCurrentFile.toLatin1().constData());
437   QApplication::restoreOverrideCursor();
438   updateCommandStatus();
439 }
440
441 //******************************************************
442 void XGUI_Workshop::onSave()
443 {
444   if(myCurrentFile.isEmpty()) {
445     onSaveAs();
446     return;
447   }
448   saveDocument(myCurrentFile);
449   updateCommandStatus();
450 }
451
452 //******************************************************
453 void XGUI_Workshop::onSaveAs()
454 {
455   QString aTemp = myCurrentFile;
456   myCurrentFile = QFileDialog::getSaveFileName(mainWindow());
457   if(myCurrentFile.isEmpty()) {
458     myCurrentFile = aTemp;
459     return;
460   }
461   QFileInfo aFileInfo(myCurrentFile);
462   if(aFileInfo.exists() && !aFileInfo.isWritable()) {
463     QMessageBox::critical(myMainWindow, tr("Warning"), tr("Unable to save the file."));
464     return;
465   }
466   onSave();
467 }
468
469 //******************************************************
470 void XGUI_Workshop::onUndo()
471 {
472   objectBrowser()->treeView()->setCurrentIndex(QModelIndex());
473   PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
474   DocumentPtr aDoc = aMgr->rootDocument();
475   if (aDoc->isOperation())
476     operationMgr()->abortOperation();
477   aDoc->undo();
478   updateCommandStatus();
479 }
480
481 //******************************************************
482 void XGUI_Workshop::onRedo()
483 {
484   objectBrowser()->treeView()->setCurrentIndex(QModelIndex());
485   PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
486   DocumentPtr aDoc = aMgr->rootDocument();
487   aDoc->redo();
488   updateCommandStatus();
489 }
490
491 //******************************************************
492 XGUI_Module* XGUI_Workshop::loadModule(const QString& theModule)
493 {
494   QString libName =
495       QString::fromStdString(library(theModule.toStdString()));
496   if (libName.isEmpty()) {
497     qWarning(
498     qPrintable( tr( "Information about module \"%1\" doesn't exist." ).arg( theModule ) ));
499     return 0;
500   }
501
502   QString err;
503   CREATE_FUNC crtInst = 0;
504
505 #ifdef WIN32
506   HINSTANCE modLib = ::LoadLibrary((LPTSTR) qPrintable(libName));
507   if (!modLib) {
508     LPVOID lpMsgBuf;
509     ::FormatMessage(
510         FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
511         0, ::GetLastError(), 0, (LPTSTR) & lpMsgBuf, 0, 0);
512     QString aMsg((char*) &lpMsgBuf);
513     err = QString("Failed to load  %1. %2").arg(libName).arg(aMsg);
514     ::LocalFree(lpMsgBuf);
515   } else {
516     crtInst = (CREATE_FUNC) ::GetProcAddress(modLib, CREATE_MODULE);
517     if (!crtInst) {
518       LPVOID lpMsgBuf;
519       ::FormatMessage(
520           FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM
521               | FORMAT_MESSAGE_IGNORE_INSERTS,
522           0, ::GetLastError(), 0, (LPTSTR) & lpMsgBuf, 0, 0);
523       QString aMsg((char*) &lpMsgBuf);
524       err = QString("Failed to find  %1 function. %2").arg( CREATE_MODULE).arg(aMsg);
525       ::LocalFree(lpMsgBuf);
526     }
527   }
528 #else
529   void* modLib = dlopen( libName.toLatin1(), RTLD_LAZY );
530   if ( !modLib ) {
531     err = QString( "Can not load library %1. %2" ).arg( libName ).arg( dlerror() );
532   } else {
533     crtInst = (CREATE_FUNC)dlsym( modLib, CREATE_MODULE );
534     if ( !crtInst ) {
535       err = QString( "Failed to find function %1. %2" ).arg( CREATE_MODULE ).arg( dlerror() );
536     }
537   }
538 #endif
539
540   XGUI_Module* aModule = crtInst ? crtInst(this) : 0;
541
542   if (!err.isEmpty()) {
543     if (mainWindow()) {
544       QMessageBox::warning(mainWindow(), tr("Error"), err);
545     } else {
546       qWarning( qPrintable( err ));
547     }
548   }
549   return aModule;
550 }
551
552 //******************************************************
553 bool XGUI_Workshop::activateModule()
554 {
555   Config_ModuleReader aModuleReader;
556   QString moduleName = QString::fromStdString(aModuleReader.getModuleName());
557   myPartSetModule = loadModule(moduleName);
558   if (!myPartSetModule)
559     return false;
560   myPartSetModule->createFeatures();
561   return true;
562 }
563
564 //******************************************************
565 void XGUI_Workshop::updateCommandStatus()
566 {
567   if (isSalomeMode()) // TODO: update commands in SALOME
568     return;
569   XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
570
571   QList<XGUI_Command*> aCommands = aMenuBar->features();
572   QList<XGUI_Command*>::const_iterator aIt;
573
574   PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
575   if (aMgr->hasRootDocument()) {
576     XGUI_Command* aUndoCmd;
577     XGUI_Command* aRedoCmd;
578     foreach(XGUI_Command* aCmd, aCommands) {
579       if (aCmd->id() == "UNDO_CMD")
580         aUndoCmd = aCmd;
581       else if (aCmd->id() == "REDO_CMD")
582         aRedoCmd = aCmd;
583       else // Enable all commands
584         aCmd->enable();
585     }
586     DocumentPtr aDoc = aMgr->rootDocument();
587     aUndoCmd->setEnabled(aDoc->canUndo());
588     aRedoCmd->setEnabled(aDoc->canRedo());
589   } else {
590     foreach(XGUI_Command* aCmd, aCommands) {
591       if (aCmd->id() == "NEW_CMD")
592         aCmd->enable();
593       else if (aCmd->id() == "EXIT_CMD")
594         aCmd->enable();
595       else 
596         aCmd->disable();
597     }
598   }
599 }
600
601 //******************************************************
602 QDockWidget* XGUI_Workshop::createObjectBrowser(QWidget* theParent)
603 {
604   QDockWidget* aObjDock = new QDockWidget(theParent);
605   aObjDock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
606   aObjDock->setWindowTitle(tr("Object browser"));
607   myObjectBrowser = new XGUI_ObjectsBrowser(aObjDock);
608   connect(myObjectBrowser, SIGNAL(activePartChanged(FeaturePtr)), this, SLOT(changeCurrentDocument(FeaturePtr)));
609   aObjDock->setWidget(myObjectBrowser);
610
611   myContextMenuMgr->connectObjectBrowser();
612   return aObjDock;
613 }
614
615 //******************************************************
616 /*
617  * Creates dock widgets, places them in corresponding area
618  * and tabifies if necessary.
619  */
620 void XGUI_Workshop::createDockWidgets()
621 {
622   QMainWindow* aDesktop = isSalomeMode()? salomeConnector()->desktop() :
623                                           myMainWindow;
624   QDockWidget* aObjDock = createObjectBrowser(aDesktop);
625   aDesktop->addDockWidget(Qt::LeftDockWidgetArea, aObjDock);
626   myPropertyPanel = new XGUI_PropertyPanel(aDesktop);
627   aDesktop->addDockWidget(Qt::LeftDockWidgetArea, myPropertyPanel);
628   hidePropertyPanel(); //<! Invisible by default
629   hideObjectBrowser();
630   aDesktop->tabifyDockWidget(aObjDock, myPropertyPanel);
631
632   QPushButton* aOkBtn = myPropertyPanel->findChild<QPushButton*>(XGUI::PROP_PANEL_OK);
633   connect(aOkBtn, SIGNAL(clicked()), myOperationMgr, SLOT(onCommitOperation()));
634   QPushButton* aCancelBtn = myPropertyPanel->findChild<QPushButton*>(XGUI::PROP_PANEL_CANCEL);
635   connect(aCancelBtn, SIGNAL(clicked()), myOperationMgr, SLOT(onAbortOperation()));
636 }
637
638 //******************************************************
639 void XGUI_Workshop::showPropertyPanel()
640 {
641   QAction* aViewAct = myPropertyPanel->toggleViewAction();
642   //<! Restore ability to close panel from the window's menu
643   aViewAct->setEnabled(true);
644   myPropertyPanel->show();
645   myPropertyPanel->raise();
646 }
647
648 //******************************************************
649 void XGUI_Workshop::hidePropertyPanel()
650 {
651   QAction* aViewAct = myPropertyPanel->toggleViewAction();
652   //<! Do not allow to show empty property panel
653   aViewAct->setEnabled(false);
654   myPropertyPanel->hide();
655 }
656
657 //******************************************************
658 void XGUI_Workshop::showObjectBrowser()
659 {
660   myObjectBrowser->parentWidget()->show();
661 }
662
663 //******************************************************
664 void XGUI_Workshop::hideObjectBrowser()
665 {
666   myObjectBrowser->parentWidget()->hide();
667 }
668
669 //******************************************************
670 void XGUI_Workshop::onFeatureTriggered()
671 {
672   QAction* aCmd = dynamic_cast<QAction*>(sender());
673   if (aCmd) {
674     QString aId = salomeConnector()->commandId(aCmd);
675     if (!aId.isNull())
676       myPartSetModule->launchOperation(aId);
677   }
678 }
679
680 //******************************************************
681 void XGUI_Workshop::changeCurrentDocument(FeaturePtr thePart)
682 {
683   PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
684   if (thePart) {
685     boost::shared_ptr<ModelAPI_AttributeDocRef> aDocRef = thePart->data()->docRef("PartDocument");
686     if (aDocRef)
687       aMgr->setCurrentDocument(aDocRef->value());
688   } else {
689     aMgr->setCurrentDocument(aMgr->rootDocument());
690   }
691 }
692
693 //******************************************************
694 void XGUI_Workshop::salomeViewerSelectionChanged()
695 {
696   emit salomeViewerSelection();
697 }
698
699
700 //**************************************************************
701 XGUI_SalomeViewer* XGUI_Workshop::salomeViewer() const 
702
703   return mySalomeConnector->viewer(); 
704 }
705
706 //**************************************************************
707 void XGUI_Workshop::onContextMenuCommand(const QString& theId, bool isChecked)
708 {
709   if (theId == "ACTIVATE_PART_CMD")
710     activatePart(true);
711   else if (theId == "DEACTIVATE_PART_CMD") 
712     activatePart(false);
713
714 }
715
716 //**************************************************************
717 void XGUI_Workshop::activatePart(bool toActivate)
718 {
719   if (toActivate) {
720     QFeatureList aFeatures = mySelector->selectedFeatures();
721     if (aFeatures.size() > 0) {
722       changeCurrentDocument(aFeatures.first());
723       myObjectBrowser->activateCurrentPart(true);
724     }
725   } else {
726     changeCurrentDocument(FeaturePtr());
727     myObjectBrowser->activateCurrentPart(false);
728   }
729 }
730