]> SALOME platform Git repositories - modules/shaper.git/blob - src/XGUI/XGUI_Workshop.cpp
Salome HOME
Merge branch 'master' of newgeom:newgeom
[modules/shaper.git] / src / XGUI / XGUI_Workshop.cpp
1 #include "XGUI_Module.h"
2 #include "XGUI_Constants.h"
3 #include "XGUI_Command.h"
4 #include "XGUI_MainMenu.h"
5 #include "XGUI_MainWindow.h"
6 #include "XGUI_MenuGroupPanel.h"
7 #include "XGUI_Tools.h"
8 #include "XGUI_Workbench.h"
9 #include "XGUI_Workshop.h"
10 #include "XGUI_Viewer.h"
11 #include "ModuleBase_WidgetFactory.h"
12 #include "XGUI_SelectionMgr.h"
13 #include "XGUI_ObjectsBrowser.h"
14 #include "XGUI_Displayer.h"
15 #include "XGUI_OperationMgr.h"
16 #include "XGUI_SalomeConnector.h"
17 #include "XGUI_SalomeViewer.h"
18 #include "XGUI_ActionsMgr.h"
19 #include "XGUI_ErrorDialog.h"
20 #include "XGUI_ViewerProxy.h"
21 #include "XGUI_PropertyPanel.h"
22 #include "XGUI_ContextMenuMgr.h"
23
24 #include <Model_Events.h>
25 #include <ModelAPI_PluginManager.h>
26 #include <ModelAPI_Feature.h>
27 #include <ModelAPI_Data.h>
28 #include <ModelAPI_AttributeDocRef.h>
29 #include <ModelAPI_Object.h>
30
31 #include <Events_Loop.h>
32 #include <Events_Error.h>
33 #include <ModuleBase_Operation.h>
34 #include <ModuleBase_Operation.h>
35 #include <ModuleBase_OperationDescription.h>
36 #include <Config_Common.h>
37 #include <Config_FeatureMessage.h>
38 #include <Config_PointerMessage.h>
39 #include <Config_ModuleReader.h>
40
41 #include <QApplication>
42 #include <QFileDialog>
43 #include <QMessageBox>
44 #include <QMdiSubWindow>
45 #include <QPushButton>
46 #include <QDockWidget>
47 #include <QLayout>
48
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   if (isSalomeMode()) {
131     // Create only Undo, Redo commands
132     salomeConnector()->addEditCommand("UNDO_CMD", 
133                                       tr("Undo"), tr("Undo last command"),
134                                       QIcon(":pictures/undo.png"), 
135                                       false, this, SLOT(onUndo()),
136                                       QKeySequence::Undo);
137     salomeConnector()->addEditCommand("REDO_CMD", 
138                                       tr("Redo"), tr("Redo last command"),
139                                       QIcon(":pictures/redo.png"), 
140                                       false, this, SLOT(onRedo()),
141                                       QKeySequence::Redo);
142     salomeConnector()->addEditMenuSeparator();
143     return;
144   }
145   XGUI_Workbench* aPage = myMainWindow->menuObject()->generalPage();
146
147   // File commands group
148   XGUI_MenuGroupPanel* aGroup = aPage->addGroup("Default");
149
150   XGUI_Command* aCommand;
151
152   aCommand = aGroup->addFeature("SAVE_CMD", tr("Save..."), tr("Save the document"),
153                                 QIcon(":pictures/save.png"), QKeySequence::Save);
154   aCommand->connectTo(this, SLOT(onSave()));
155   //aCommand->disable();
156
157   aCommand = aGroup->addFeature("UNDO_CMD", tr("Undo"), tr("Undo last command"),
158                                 QIcon(":pictures/undo.png"), QKeySequence::Undo);
159   aCommand->connectTo(this, SLOT(onUndo()));
160
161   aCommand = aGroup->addFeature("REDO_CMD", tr("Redo"), tr("Redo last command"),
162                                 QIcon(":pictures/redo.png"), QKeySequence::Redo);
163   aCommand->connectTo(this, SLOT(onRedo()));
164
165   aCommand = aGroup->addFeature("REBUILD_CMD", tr("Rebuild"), tr("Rebuild data objects"),
166                                 QIcon(":pictures/rebuild.png"));
167
168   aCommand = aGroup->addFeature("SAVEAS_CMD", tr("Save as..."), tr("Save the document into a file"),
169                                 QIcon(":pictures/save.png"));
170   aCommand->connectTo(this, SLOT(onSaveAs()));
171   //aCommand->disable();
172
173   aCommand = aGroup->addFeature("OPEN_CMD", tr("Open..."), tr("Open a new document"),
174                                 QIcon(":pictures/open.png"), QKeySequence::Open);
175   aCommand->connectTo(this, SLOT(onOpen()));
176
177   //aCommand = aGroup->addFeature("NEW_CMD", tr("New"), tr("Create a new document"),
178   //                              QIcon(":pictures/new.png"), QKeySequence::New);
179   //aCommand->connectTo(this, SLOT(onNew()));
180
181   aCommand = aGroup->addFeature("EXIT_CMD", tr("Exit"), tr("Exit application"),
182                                 QIcon(":pictures/close.png"), QKeySequence::Close);
183   aCommand->connectTo(this, SLOT(onExit()));
184
185   myContextMenuMgr->createActions();
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     salomeConnector()->addFeature(aWchName,
301                                   QString::fromStdString(theMessage->id()),
302                                   aId,
303                                   QString::fromStdString(theMessage->tooltip()),
304                                   QIcon(theMessage->icon().c_str()),
305                                   isUsePropPanel, this, 
306                                   SLOT(onFeatureTriggered()), QKeySequence());
307     myActionsMgr->addCommand(aId, salomeConnector()->command(aId));
308     salomeConnector()->setNestedActions(aId, aNestedFeatures.split(" "));
309
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     myCurrentFile = "";
422   }
423
424   //show file dialog, check if readable and open
425   myCurrentFile = QFileDialog::getExistingDirectory(mainWindow());
426   if(myCurrentFile.isEmpty())
427     return;
428   QFileInfo aFileInfo(myCurrentFile);
429   if(!aFileInfo.exists() || !aFileInfo.isReadable()) {
430     QMessageBox::critical(myMainWindow, tr("Warning"), tr("Unable to open the file."));
431     myCurrentFile = "";
432     return;
433   }
434   QApplication::setOverrideCursor(Qt::WaitCursor);
435   aDoc->load(myCurrentFile.toLatin1().constData());
436   QApplication::restoreOverrideCursor();
437   updateCommandStatus();
438 }
439
440 //******************************************************
441 void XGUI_Workshop::onSave()
442 {
443   if(myCurrentFile.isEmpty()) {
444     onSaveAs();
445     return;
446   }
447   saveDocument(myCurrentFile);
448   updateCommandStatus();
449 }
450
451 //******************************************************
452 void XGUI_Workshop::onSaveAs()
453 {
454   QString aTemp = myCurrentFile;
455   myCurrentFile = QFileDialog::getSaveFileName(mainWindow());
456   if(myCurrentFile.isEmpty()) {
457     myCurrentFile = aTemp;
458     return;
459   }
460   QFileInfo aFileInfo(myCurrentFile);
461   if(aFileInfo.exists() && !aFileInfo.isWritable()) {
462     QMessageBox::critical(myMainWindow, tr("Warning"), tr("Unable to save the file."));
463     return;
464   }
465   onSave();
466 }
467
468 //******************************************************
469 void XGUI_Workshop::onUndo()
470 {
471   objectBrowser()->treeView()->setCurrentIndex(QModelIndex());
472   PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
473   DocumentPtr aDoc = aMgr->rootDocument();
474   if (aDoc->isOperation())
475     operationMgr()->abortOperation();
476   aDoc->undo();
477   updateCommandStatus();
478 }
479
480 //******************************************************
481 void XGUI_Workshop::onRedo()
482 {
483   objectBrowser()->treeView()->setCurrentIndex(QModelIndex());
484   PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
485   DocumentPtr aDoc = aMgr->rootDocument();
486   aDoc->redo();
487   updateCommandStatus();
488 }
489
490 //******************************************************
491 XGUI_Module* XGUI_Workshop::loadModule(const QString& theModule)
492 {
493   QString libName =
494       QString::fromStdString(library(theModule.toStdString()));
495   if (libName.isEmpty()) {
496     qWarning(
497     qPrintable( tr( "Information about module \"%1\" doesn't exist." ).arg( theModule ) ));
498     return 0;
499   }
500
501   QString err;
502   CREATE_FUNC crtInst = 0;
503
504 #ifdef WIN32
505   HINSTANCE modLib = ::LoadLibrary((LPTSTR) qPrintable(libName));
506   if (!modLib) {
507     LPVOID lpMsgBuf;
508     ::FormatMessage(
509         FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
510         0, ::GetLastError(), 0, (LPTSTR) & lpMsgBuf, 0, 0);
511     QString aMsg((char*) &lpMsgBuf);
512     err = QString("Failed to load  %1. %2").arg(libName).arg(aMsg);
513     ::LocalFree(lpMsgBuf);
514   } else {
515     crtInst = (CREATE_FUNC) ::GetProcAddress(modLib, CREATE_MODULE);
516     if (!crtInst) {
517       LPVOID lpMsgBuf;
518       ::FormatMessage(
519           FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM
520               | FORMAT_MESSAGE_IGNORE_INSERTS,
521           0, ::GetLastError(), 0, (LPTSTR) & lpMsgBuf, 0, 0);
522       QString aMsg((char*) &lpMsgBuf);
523       err = QString("Failed to find  %1 function. %2").arg( CREATE_MODULE).arg(aMsg);
524       ::LocalFree(lpMsgBuf);
525     }
526   }
527 #else
528   void* modLib = dlopen( libName.toLatin1(), RTLD_LAZY );
529   if ( !modLib ) {
530     err = QString( "Can not load library %1. %2" ).arg( libName ).arg( dlerror() );
531   } else {
532     crtInst = (CREATE_FUNC)dlsym( modLib, CREATE_MODULE );
533     if ( !crtInst ) {
534       err = QString( "Failed to find function %1. %2" ).arg( CREATE_MODULE ).arg( dlerror() );
535     }
536   }
537 #endif
538
539   XGUI_Module* aModule = crtInst ? crtInst(this) : 0;
540
541   if (!err.isEmpty()) {
542     if (mainWindow()) {
543       QMessageBox::warning(mainWindow(), tr("Error"), err);
544     } else {
545       qWarning( qPrintable( err ));
546     }
547   }
548   return aModule;
549 }
550
551 //******************************************************
552 bool XGUI_Workshop::activateModule()
553 {
554   Config_ModuleReader aModuleReader;
555   QString moduleName = QString::fromStdString(aModuleReader.getModuleName());
556   myPartSetModule = loadModule(moduleName);
557   if (!myPartSetModule)
558     return false;
559   myPartSetModule->createFeatures();
560   return true;
561 }
562
563 //******************************************************
564 void XGUI_Workshop::updateCommandStatus()
565 {
566   if (isSalomeMode()) // TODO: update commands in SALOME
567     return;
568   XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
569
570   QList<XGUI_Command*> aCommands = aMenuBar->features();
571   QList<XGUI_Command*>::const_iterator aIt;
572
573   PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
574   if (aMgr->hasRootDocument()) {
575     XGUI_Command* aUndoCmd;
576     XGUI_Command* aRedoCmd;
577     foreach(XGUI_Command* aCmd, aCommands) {
578       if (aCmd->id() == "UNDO_CMD")
579         aUndoCmd = aCmd;
580       else if (aCmd->id() == "REDO_CMD")
581         aRedoCmd = aCmd;
582       else // Enable all commands
583         aCmd->enable();
584     }
585     DocumentPtr aDoc = aMgr->rootDocument();
586     aUndoCmd->setEnabled(aDoc->canUndo());
587     aRedoCmd->setEnabled(aDoc->canRedo());
588   } else {
589     foreach(XGUI_Command* aCmd, aCommands) {
590       if (aCmd->id() == "NEW_CMD")
591         aCmd->enable();
592       else if (aCmd->id() == "EXIT_CMD")
593         aCmd->enable();
594       else 
595         aCmd->disable();
596     }
597   }
598 }
599
600 //******************************************************
601 QDockWidget* XGUI_Workshop::createObjectBrowser(QWidget* theParent)
602 {
603   QDockWidget* aObjDock = new QDockWidget(theParent);
604   aObjDock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
605   aObjDock->setWindowTitle(tr("Object browser"));
606   myObjectBrowser = new XGUI_ObjectsBrowser(aObjDock);
607   connect(myObjectBrowser, SIGNAL(activePartChanged(FeaturePtr)), this, SLOT(changeCurrentDocument(FeaturePtr)));
608   aObjDock->setWidget(myObjectBrowser);
609
610   myContextMenuMgr->connectObjectBrowser();
611   return aObjDock;
612 }
613
614 //******************************************************
615 /*
616  * Creates dock widgets, places them in corresponding area
617  * and tabifies if necessary.
618  */
619 void XGUI_Workshop::createDockWidgets()
620 {
621   QMainWindow* aDesktop = isSalomeMode()? salomeConnector()->desktop() :
622                                           myMainWindow;
623   QDockWidget* aObjDock = createObjectBrowser(aDesktop);
624   aDesktop->addDockWidget(Qt::LeftDockWidgetArea, aObjDock);
625   myPropertyPanel = new XGUI_PropertyPanel(aDesktop);
626   aDesktop->addDockWidget(Qt::LeftDockWidgetArea, myPropertyPanel);
627   hidePropertyPanel(); //<! Invisible by default
628   hideObjectBrowser();
629   aDesktop->tabifyDockWidget(aObjDock, myPropertyPanel);
630
631   QPushButton* aOkBtn = myPropertyPanel->findChild<QPushButton*>(XGUI::PROP_PANEL_OK);
632   connect(aOkBtn, SIGNAL(clicked()), myOperationMgr, SLOT(onCommitOperation()));
633   QPushButton* aCancelBtn = myPropertyPanel->findChild<QPushButton*>(XGUI::PROP_PANEL_CANCEL);
634   connect(aCancelBtn, SIGNAL(clicked()), myOperationMgr, SLOT(onAbortOperation()));
635 }
636
637 //******************************************************
638 void XGUI_Workshop::showPropertyPanel()
639 {
640   QAction* aViewAct = myPropertyPanel->toggleViewAction();
641   //<! Restore ability to close panel from the window's menu
642   aViewAct->setEnabled(true);
643   myPropertyPanel->show();
644   myPropertyPanel->raise();
645 }
646
647 //******************************************************
648 void XGUI_Workshop::hidePropertyPanel()
649 {
650   QAction* aViewAct = myPropertyPanel->toggleViewAction();
651   //<! Do not allow to show empty property panel
652   aViewAct->setEnabled(false);
653   myPropertyPanel->hide();
654 }
655
656 //******************************************************
657 void XGUI_Workshop::showObjectBrowser()
658 {
659   myObjectBrowser->parentWidget()->show();
660 }
661
662 //******************************************************
663 void XGUI_Workshop::hideObjectBrowser()
664 {
665   myObjectBrowser->parentWidget()->hide();
666 }
667
668 //******************************************************
669 void XGUI_Workshop::onFeatureTriggered()
670 {
671   QAction* aCmd = dynamic_cast<QAction*>(sender());
672   if (aCmd) {
673     QString aId = salomeConnector()->commandId(aCmd);
674     if (!aId.isNull())
675       myPartSetModule->launchOperation(aId);
676   }
677 }
678
679 //******************************************************
680 void XGUI_Workshop::changeCurrentDocument(FeaturePtr thePart)
681 {
682   PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
683   if (thePart) {
684     boost::shared_ptr<ModelAPI_AttributeDocRef> aDocRef = thePart->data()->docRef("PartDocument");
685     if (aDocRef)
686       aMgr->setCurrentDocument(aDocRef->value());
687   } else {
688     aMgr->setCurrentDocument(aMgr->rootDocument());
689   }
690 }
691
692 //******************************************************
693 void XGUI_Workshop::salomeViewerSelectionChanged()
694 {
695   emit salomeViewerSelection();
696 }
697
698
699 //**************************************************************
700 XGUI_SalomeViewer* XGUI_Workshop::salomeViewer() const 
701
702   return mySalomeConnector->viewer(); 
703 }
704
705 //**************************************************************
706 void XGUI_Workshop::onContextMenuCommand(const QString& theId, bool isChecked)
707 {
708   if (theId == "ACTIVATE_PART_CMD")
709     activatePart(true);
710   else if (theId == "DEACTIVATE_PART_CMD") 
711     activatePart(false);
712
713 }
714
715 //**************************************************************
716 void XGUI_Workshop::activatePart(bool toActivate)
717 {
718   if (toActivate) {
719     QFeatureList aFeatures = mySelector->selectedFeatures();
720     if (aFeatures.size() > 0) {
721       changeCurrentDocument(aFeatures.first());
722       myObjectBrowser->activateCurrentPart(true);
723     }
724   } else {
725     changeCurrentDocument(FeaturePtr());
726     myObjectBrowser->activateCurrentPart(false);
727   }
728 }
729