]> SALOME platform Git repositories - modules/shaper.git/blob - src/XGUI/XGUI_Workshop.cpp
Salome HOME
According to "operation-widget_factory-property"
[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 #include "XGUI_ModuleConnector.h"
24
25 #include <Model_Events.h>
26 #include <ModelAPI_PluginManager.h>
27 #include <ModelAPI_Feature.h>
28 #include <ModelAPI_Data.h>
29 #include <ModelAPI_AttributeDocRef.h>
30 #include <ModelAPI_Object.h>
31
32 #include <PartSetPlugin_Part.h>
33
34 #include <Events_Loop.h>
35 #include <Events_Error.h>
36 #include <ModuleBase_Operation.h>
37 #include <ModuleBase_Operation.h>
38 #include <ModuleBase_OperationDescription.h>
39 #include <Config_Common.h>
40 #include <Config_FeatureMessage.h>
41 #include <Config_PointerMessage.h>
42 #include <Config_ModuleReader.h>
43
44 #include <QApplication>
45 #include <QFileDialog>
46 #include <QMessageBox>
47 #include <QMdiSubWindow>
48 #include <QPushButton>
49 #include <QDockWidget>
50 #include <QLayout>
51 #include <QTimer>
52
53 #ifdef _DEBUG
54 #include <QDebug>
55 #endif
56
57 #ifdef WIN32
58 #include <windows.h>
59 #else
60 #include <dlfcn.h>
61 #endif
62
63
64 QMap<QString, QString> XGUI_Workshop::myIcons;
65
66 QString XGUI_Workshop::featureIcon(const std::string& theId)
67 {
68   QString aId(theId.c_str());
69   if (myIcons.contains(aId))
70     return myIcons[aId];
71   return QString();
72 }
73
74 XGUI_Workshop::XGUI_Workshop(XGUI_SalomeConnector* theConnector)
75   : QObject(),
76   myCurrentDir(QString()),
77   myPartSetModule(NULL),
78   mySalomeConnector(theConnector),
79   myPropertyPanel(0),
80   myObjectBrowser(0),
81   myDisplayer(0)
82 {
83   myMainWindow = mySalomeConnector? 0 : new XGUI_MainWindow();
84
85   myDisplayer = new XGUI_Displayer(this);
86
87   mySelector = new XGUI_SelectionMgr(this);
88
89   myOperationMgr = new XGUI_OperationMgr(this);
90   myActionsMgr = new XGUI_ActionsMgr(this);
91   myErrorDlg = new XGUI_ErrorDialog(myMainWindow);
92   myContextMenuMgr = new XGUI_ContextMenuMgr(this);
93   connect(myContextMenuMgr, SIGNAL(actionTriggered(const QString&, bool)), 
94           this, SLOT(onContextMenuCommand(const QString&, bool)));
95
96   myViewerProxy = new XGUI_ViewerProxy(this);
97   
98   myModuleConnector = new XGUI_ModuleConnector(this);
99
100   connect(myOperationMgr, SIGNAL(operationStarted()), SLOT(onOperationStarted()));
101   connect(myOperationMgr, SIGNAL(operationResumed()), SLOT(onOperationStarted()));
102   connect(myOperationMgr, SIGNAL(operationStopped(ModuleBase_Operation*)), SLOT(onOperationStopped(ModuleBase_Operation*)));
103   connect(myMainWindow, SIGNAL(exitKeySequence()), SLOT(onExit()));
104   connect(myOperationMgr, SIGNAL(operationStarted()), myActionsMgr, SLOT(update()));
105   connect(myOperationMgr, SIGNAL(operationStopped(ModuleBase_Operation*)), myActionsMgr, SLOT(update()));
106   connect(this, SIGNAL(errorOccurred(const QString&)), myErrorDlg, SLOT(addError(const QString&)));
107 }
108
109 //******************************************************
110 XGUI_Workshop::~XGUI_Workshop(void)
111 {
112 }
113
114 //******************************************************
115 void XGUI_Workshop::startApplication()
116 {
117   initMenu();
118   //Initialize event listening
119   Events_Loop* aLoop = Events_Loop::loop();
120   aLoop->registerListener(this, Events_Error::errorID()); //!< Listening application errors.
121   //TODO(sbh): Implement static method to extract event id [SEID]
122   Events_ID aFeatureId = aLoop->eventByName(EVENT_FEATURE_LOADED);
123   aLoop->registerListener(this, aFeatureId);
124   Events_ID aPartSetId = aLoop->eventByName("PartSetModuleEvent");
125   aLoop->registerListener(this, aPartSetId);
126   Events_ID aFeatureUpdatedId = aLoop->eventByName(EVENT_FEATURE_UPDATED);
127   aLoop->registerListener(this, aFeatureUpdatedId);
128   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_FEATURE_CREATED));
129
130   activateModule();
131   if (myMainWindow) {
132     myMainWindow->show();
133     updateCommandStatus();
134   }
135   onNew();
136 }
137
138 //******************************************************
139 void XGUI_Workshop::initMenu()
140 {
141   myContextMenuMgr->createActions();
142
143   if (isSalomeMode()) {
144     // Create only Undo, Redo commands
145     QAction* aAction = salomeConnector()->addEditCommand("UNDO_CMD", 
146                                       tr("Undo"), tr("Undo last command"),
147                                       QIcon(":pictures/undo.png"), 
148                                       QKeySequence::Undo, false);
149     connect(aAction, SIGNAL(triggered(bool)), this, SLOT(onUndo()));
150     aAction = salomeConnector()->addEditCommand("REDO_CMD", 
151                                       tr("Redo"), tr("Redo last command"),
152                                       QIcon(":pictures/redo.png"), 
153                                       QKeySequence::Redo, false);
154     connect(aAction, SIGNAL(triggered(bool)), this, SLOT(onRedo()));
155     salomeConnector()->addEditMenuSeparator();
156     return;
157   }
158   XGUI_Workbench* aPage = myMainWindow->menuObject()->generalPage();
159
160   // File commands group
161   XGUI_MenuGroupPanel* aGroup = aPage->addGroup("Default");
162
163   XGUI_Command* aCommand;
164
165   aCommand = aGroup->addFeature("SAVE_CMD", tr("Save..."), tr("Save the document"),
166                                 QIcon(":pictures/save.png"), QKeySequence::Save);
167   aCommand->connectTo(this, SLOT(onSave()));
168   //aCommand->disable();
169
170   aCommand = aGroup->addFeature("UNDO_CMD", tr("Undo"), tr("Undo last command"),
171                                 QIcon(":pictures/undo.png"), QKeySequence::Undo);
172   aCommand->connectTo(this, SLOT(onUndo()));
173
174   aCommand = aGroup->addFeature("REDO_CMD", tr("Redo"), tr("Redo last command"),
175                                 QIcon(":pictures/redo.png"), QKeySequence::Redo);
176   aCommand->connectTo(this, SLOT(onRedo()));
177
178   aCommand = aGroup->addFeature("REBUILD_CMD", tr("Rebuild"), tr("Rebuild data objects"),
179                                 QIcon(":pictures/rebuild.png"));
180
181   aCommand = aGroup->addFeature("SAVEAS_CMD", tr("Save as..."), tr("Save the document into a file"),
182                                 QIcon(":pictures/save.png"));
183   aCommand->connectTo(this, SLOT(onSaveAs()));
184   //aCommand->disable();
185
186   aCommand = aGroup->addFeature("OPEN_CMD", tr("Open..."), tr("Open a new document"),
187                                 QIcon(":pictures/open.png"), QKeySequence::Open);
188   aCommand->connectTo(this, SLOT(onOpen()));
189
190   //aCommand = aGroup->addFeature("NEW_CMD", tr("New"), tr("Create a new document"),
191   //                              QIcon(":pictures/new.png"), QKeySequence::New);
192   //aCommand->connectTo(this, SLOT(onNew()));
193
194   aCommand = aGroup->addFeature("EXIT_CMD", tr("Exit"), tr("Exit application"),
195                                 QIcon(":pictures/close.png"), QKeySequence::Close);
196   aCommand->connectTo(this, SLOT(onExit()));
197   //FIXME: SBH's test action. Can be used for some GUI tests.
198   //#ifdef _DEBUG
199   //  aCommand = aGroup->addFeature("TEST_CMD", "Test!", "Private debug button",
200   //                                QIcon(":pictures/close.png"));
201   //  aCommand->connectTo(myActionsMgr, SLOT(update()));
202   //#endif
203 }
204
205 //******************************************************
206 XGUI_Workbench* XGUI_Workshop::addWorkbench(const QString& theName)
207 {
208   XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
209   return aMenuBar->addWorkbench(theName);
210 }
211
212 //******************************************************
213 void XGUI_Workshop::processEvent(const Events_Message* theMessage)
214 {
215   //A message to start feature creation received.
216   static Events_ID aFeatureLoadedId = Events_Loop::loop()->eventByName(EVENT_FEATURE_LOADED);
217   if (theMessage->eventID() == aFeatureLoadedId) {
218     const Config_FeatureMessage* aFeatureMsg = dynamic_cast<const Config_FeatureMessage*>(theMessage);
219     if(!aFeatureMsg->isInternal()) {
220       addFeature(aFeatureMsg);
221     }
222     return;
223   }
224   // Process creation of Part
225   if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_FEATURE_CREATED)) {
226     const Model_FeatureUpdatedMessage* aUpdMsg = dynamic_cast<const Model_FeatureUpdatedMessage*>(theMessage);
227     std::set<FeaturePtr> aFeatures = aUpdMsg->features();
228
229     std::set<FeaturePtr>::const_iterator aIt;
230     bool aHasPart = false;
231     for (aIt = aFeatures.begin(); aIt != aFeatures.end(); ++aIt) {
232       FeaturePtr aFeature = (*aIt);
233       if (aFeature->getKind() == PARTSET_PART_KIND) {
234         aHasPart = true;
235         //break;
236       } else {
237         myDisplayer->display(aFeature, false);
238       }
239     }
240     myDisplayer->updateViewer();
241     if (aHasPart) {
242       //The created part will be created in Object Browser later and we have to activate it
243       // only when it is created everywere
244       QTimer::singleShot(50, this, SLOT(activateLastPart()));
245     }
246   }
247
248   //Update property panel on corresponding message. If there is no current operation (no
249   //property panel), or received message has different feature to the current - do nothing.
250   static Events_ID aFeatureUpdatedId = Events_Loop::loop()->eventByName(EVENT_FEATURE_UPDATED);
251   if (theMessage->eventID() == aFeatureUpdatedId) {
252     const Model_FeatureUpdatedMessage* anUpdateMsg =
253         dynamic_cast<const Model_FeatureUpdatedMessage*>(theMessage);
254
255     std::set<FeaturePtr> aFeatures = anUpdateMsg->features();
256     if (myOperationMgr->hasOperation())
257     {
258       FeaturePtr aCurrentFeature = myOperationMgr->currentOperation()->feature();
259       std::set<FeaturePtr>::const_iterator aIt;
260       for (aIt = aFeatures.begin(); aIt != aFeatures.end(); ++aIt) {
261         FeaturePtr aNewFeature = (*aIt);
262         if(aNewFeature == aCurrentFeature) {
263           myPropertyPanel->updateContentWidget(aCurrentFeature);
264           break;
265         } 
266       }
267     }
268     // Redisplay feature if it is modified
269     std::set<FeaturePtr>::const_iterator aIt;
270     for (aIt = aFeatures.begin(); aIt != aFeatures.end(); ++aIt) {
271       FeaturePtr aFeature = (*aIt);
272       if (aFeature->getKind() != PARTSET_PART_KIND) {
273         if (myDisplayer->isVisible(aFeature))
274           myDisplayer->redisplay(aFeature, false);
275         else 
276           myDisplayer->display(aFeature, false);
277       }
278     }
279     myDisplayer->updateViewer();
280   }
281   //An operation passed by message. Start it, process and commit.
282   const Config_PointerMessage* aPartSetMsg = dynamic_cast<const Config_PointerMessage*>(theMessage);
283   if (aPartSetMsg) {
284     // TODO: Clear previous content
285     //myPropertyPanel->cleanContent();
286     ModuleBase_Operation* anOperation =
287         (ModuleBase_Operation*)(aPartSetMsg->pointer());
288
289     if (myOperationMgr->startOperation(anOperation)) {
290       myPropertyPanel->updateContentWidget(anOperation->feature());
291       if (!anOperation->getDescription()->hasXmlRepresentation()) {
292         anOperation->commit();
293         updateCommandStatus();
294       }
295     }
296     return;
297   }
298   //Show error dialog if error message received.
299   const Events_Error* anAppError = dynamic_cast<const Events_Error*>(theMessage);
300   if (anAppError) {
301     emit errorOccurred(QString::fromLatin1(anAppError->description()));
302   }
303
304 }
305
306 //******************************************************
307 void XGUI_Workshop::onOperationStarted()
308 {
309   ModuleBase_Operation* aOperation = myOperationMgr->currentOperation();
310
311   if(aOperation->getDescription()->hasXmlRepresentation()) { //!< No need for property panel
312     connectWithOperation(aOperation);
313
314     showPropertyPanel();
315     // TODO: Clear previous content
316     /*
317     QString aXmlRepr = aOperation->getDescription()->xmlRepresentation();
318     ModuleBase_WidgetFactory aFactory = ModuleBase_WidgetFactory(aXmlRepr.toStdString(), myModuleConnector);
319     QWidget* aContent = myPropertyPanel->contentWidget();
320     qDeleteAll(aContent->children());
321     aFactory.createWidget(aContent);
322     myPropertyPanel->setModelWidgets(aFactory.getModelWidgets());
323     */
324     myPropertyPanel->setModelWidgets(aOperation->getDescription()->modelWidgets());
325     myPropertyPanel->setWindowTitle(aOperation->getDescription()->description());
326   }
327   updateCommandStatus();
328 }
329
330 //******************************************************
331 void XGUI_Workshop::onOperationStopped(ModuleBase_Operation* theOperation)
332 {
333   //!< No need for property panel
334   updateCommandStatus();
335   hidePropertyPanel();
336   //myPropertyPanel->cleanContent();
337 }
338
339 /*
340  *
341  */
342 void XGUI_Workshop::addFeature(const Config_FeatureMessage* theMessage)
343 {
344   if (!theMessage) {
345 #ifdef _DEBUG
346     qDebug() << "XGUI_Workshop::addFeature: NULL message.";
347 #endif
348     return;
349   }
350   // Remember features icons
351   myIcons[QString::fromStdString(theMessage->id())] = QString::fromStdString(theMessage->icon());
352
353   //Find or create Workbench
354   QString aWchName = QString::fromStdString(theMessage->workbenchId());
355   QString aNestedFeatures = QString::fromStdString(theMessage->nestedFeatures());
356   bool isUsePropPanel = theMessage->isUseInput();
357   QString aId = QString::fromStdString(theMessage->id());
358   if (isSalomeMode()) {
359     QAction* aAction = salomeConnector()->addFeature(aWchName,
360                               aId,
361                               QString::fromStdString(theMessage->text()),
362                               QString::fromStdString(theMessage->tooltip()),
363                               QIcon(theMessage->icon().c_str()),
364                               QKeySequence(), isUsePropPanel);
365     salomeConnector()->setNestedActions(aId, aNestedFeatures.split(" "));
366     myActionsMgr->addCommand(aAction);
367     myPartSetModule->featureCreated(aAction);
368   } else {
369
370     XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
371     XGUI_Workbench* aPage = aMenuBar->findWorkbench(aWchName);
372     if (!aPage) {
373       aPage = addWorkbench(aWchName);
374     }
375     //Find or create Group
376     QString aGroupName = QString::fromStdString(theMessage->groupId());
377     XGUI_MenuGroupPanel* aGroup = aPage->findGroup(aGroupName);
378     if (!aGroup) {
379       aGroup = aPage->addGroup(aGroupName);
380     }
381     //Create feature...
382     XGUI_Command* aCommand = aGroup->addFeature(aId,
383                                                 QString::fromStdString(theMessage->text()),
384                                                 QString::fromStdString(theMessage->tooltip()),
385                                                 QIcon(theMessage->icon().c_str()),
386                                                 QKeySequence(), isUsePropPanel);
387     aCommand->setNestedCommands(aNestedFeatures.split(" ", QString::SkipEmptyParts));
388     myActionsMgr->addCommand(aCommand);
389     myPartSetModule->featureCreated(aCommand);
390   }
391 }
392
393 /*
394  * Makes a signal/slot connections between Property Panel
395  * and given operation. The given operation becomes a
396  * current operation and previous operation if exists
397  */
398 void XGUI_Workshop::connectWithOperation(ModuleBase_Operation* theOperation)
399 {
400   QAction* aCommand = 0;
401   if (isSalomeMode()) {
402     aCommand = salomeConnector()->command(theOperation->getDescription()->operationId());
403   } else {
404     XGUI_MainMenu* aMenu = myMainWindow->menuObject();
405     aCommand = aMenu->feature(theOperation->getDescription()->operationId());
406   }
407   //Abort operation on uncheck the command
408   connect(aCommand, SIGNAL(triggered(bool)), theOperation, SLOT(setRunning(bool)));
409 }
410
411 /*
412  * Saves document with given name.
413  */
414 void XGUI_Workshop::saveDocument(QString theName)
415 {
416   QApplication::restoreOverrideCursor();
417   PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
418   DocumentPtr aDoc = aMgr->rootDocument();
419   aDoc->save(theName.toLatin1().constData());
420   QApplication::restoreOverrideCursor();
421 }
422
423 //******************************************************
424 void XGUI_Workshop::onExit()
425 {
426   PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
427   DocumentPtr aDoc = aMgr->rootDocument();
428   if(aDoc->isModified()) {
429     int anAnswer = QMessageBox::question(
430         myMainWindow, tr("Save current file"),
431         tr("The document is modified, save before exit?"),
432         QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel, QMessageBox::Cancel);
433     if(anAnswer == QMessageBox::Save) {
434       bool saved = onSave();
435       if(!saved) {
436         return;
437       }
438     } else if (anAnswer == QMessageBox::Cancel) {
439       return;
440     }
441   }
442   qApp->exit();
443 }
444
445 //******************************************************
446 void XGUI_Workshop::onNew()
447 {
448   QApplication::setOverrideCursor(Qt::WaitCursor);
449   if (objectBrowser() == 0) {
450     createDockWidgets();
451     mySelector->connectViewers();
452   }
453   myViewerProxy->connectToViewer();
454   showObjectBrowser();
455   if (!isSalomeMode()) {
456     myMainWindow->showPythonConsole();
457     QMdiSubWindow* aWnd = myMainWindow->viewer()->createView();
458     aWnd->showMaximized();
459     updateCommandStatus();
460   }
461   myContextMenuMgr->connectViewer();
462   QApplication::restoreOverrideCursor();
463 }
464
465 //******************************************************
466 void XGUI_Workshop::onOpen()
467 {
468   //save current file before close if modified
469   PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
470   DocumentPtr aDoc = aMgr->rootDocument();
471   if(aDoc->isModified()) {
472     //TODO(sbh): re-launch the app?
473     int anAnswer = QMessageBox::question(
474         myMainWindow, tr("Save current file"),
475         tr("The document is modified, save before opening another?"),
476         QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel, QMessageBox::Cancel);
477     if(anAnswer == QMessageBox::Save) {
478       onSave();
479     } else if (anAnswer == QMessageBox::Cancel) {
480       return;
481     }
482     aDoc->close();
483     myCurrentDir = "";
484   }
485
486   //show file dialog, check if readable and open
487   myCurrentDir = QFileDialog::getExistingDirectory(mainWindow());
488   if(myCurrentDir.isEmpty())
489     return;
490   QFileInfo aFileInfo(myCurrentDir);
491   if(!aFileInfo.exists() || !aFileInfo.isReadable()) {
492     QMessageBox::critical(myMainWindow, tr("Warning"), tr("Unable to open the file."));
493     myCurrentDir = "";
494     return;
495   }
496   QApplication::setOverrideCursor(Qt::WaitCursor);
497   aDoc->load(myCurrentDir.toLatin1().constData());
498   updateCommandStatus();
499   myObjectBrowser->rebuildDataTree();
500   QApplication::restoreOverrideCursor();
501 }
502
503 //******************************************************
504 bool XGUI_Workshop::onSave()
505 {
506   if(myCurrentDir.isEmpty()) {
507     return onSaveAs();
508   }
509   saveDocument(myCurrentDir);
510   updateCommandStatus();
511   return true;
512 }
513
514 //******************************************************
515 bool XGUI_Workshop::onSaveAs()
516 {
517   QFileDialog dialog(mainWindow());
518   dialog.setWindowTitle(tr("Select directory to save files..."));
519   dialog.setFileMode(QFileDialog::Directory);
520   dialog.setFilter(tr("Folders (*)"));
521   dialog.setOptions(QFileDialog::HideNameFilterDetails | QFileDialog::ShowDirsOnly);
522   dialog.setViewMode(QFileDialog::Detail);
523
524   if(!dialog.exec()) {
525     return false;
526   }
527   QString aTempDir = dialog.selectedFiles().first();
528   QDir aDir(aTempDir);
529   if(aDir.exists() && !aDir.entryInfoList(QDir::NoDotAndDotDot|QDir::AllEntries).isEmpty()) {
530     int answer = QMessageBox::question(myMainWindow,
531                                        //: Title of the dialog which asks user if he wants to save study in existing non-empty folder
532                                        tr("Save"),
533                                        tr("The folder already contains some files, save anyway?"),
534                                        QMessageBox::Save|QMessageBox::Cancel);
535     if(answer == QMessageBox::Cancel) {
536       return false;
537     }
538   }
539   myCurrentDir = aTempDir;
540   return onSave();
541 }
542
543 //******************************************************
544 void XGUI_Workshop::onUndo()
545 {
546   objectBrowser()->treeView()->setCurrentIndex(QModelIndex());
547   PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
548   DocumentPtr aDoc = aMgr->rootDocument();
549   if (aDoc->isOperation())
550     operationMgr()->abortOperation();
551   aDoc->undo();
552   updateCommandStatus();
553 }
554
555 //******************************************************
556 void XGUI_Workshop::onRedo()
557 {
558   objectBrowser()->treeView()->setCurrentIndex(QModelIndex());
559   PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
560   DocumentPtr aDoc = aMgr->rootDocument();
561   if (aDoc->isOperation())
562     operationMgr()->abortOperation();
563   aDoc->redo();
564   updateCommandStatus();
565 }
566
567 //******************************************************
568 XGUI_Module* XGUI_Workshop::loadModule(const QString& theModule)
569 {
570   QString libName =
571       QString::fromStdString(library(theModule.toStdString()));
572   if (libName.isEmpty()) {
573     qWarning(
574     qPrintable( tr( "Information about module \"%1\" doesn't exist." ).arg( theModule ) ));
575     return 0;
576   }
577
578   QString err;
579   CREATE_FUNC crtInst = 0;
580
581 #ifdef WIN32
582   HINSTANCE modLib = ::LoadLibrary((LPTSTR) qPrintable(libName));
583   if (!modLib) {
584     LPVOID lpMsgBuf;
585     ::FormatMessage(
586         FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
587         0, ::GetLastError(), 0, (LPTSTR) & lpMsgBuf, 0, 0);
588     QString aMsg((char*) &lpMsgBuf);
589     err = QString("Failed to load  %1. %2").arg(libName).arg(aMsg);
590     ::LocalFree(lpMsgBuf);
591   } else {
592     crtInst = (CREATE_FUNC) ::GetProcAddress(modLib, CREATE_MODULE);
593     if (!crtInst) {
594       LPVOID lpMsgBuf;
595       ::FormatMessage(
596           FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM
597               | FORMAT_MESSAGE_IGNORE_INSERTS,
598           0, ::GetLastError(), 0, (LPTSTR) & lpMsgBuf, 0, 0);
599       QString aMsg((char*) &lpMsgBuf);
600       err = QString("Failed to find  %1 function. %2").arg( CREATE_MODULE).arg(aMsg);
601       ::LocalFree(lpMsgBuf);
602     }
603   }
604 #else
605   void* modLib = dlopen( libName.toLatin1(), RTLD_LAZY | RTLD_GLOBAL );
606   if ( !modLib ) {
607     err = QString( "Can not load library %1. %2" ).arg( libName ).arg( dlerror() );
608   } else {
609     crtInst = (CREATE_FUNC)dlsym( modLib, CREATE_MODULE );
610     if ( !crtInst ) {
611       err = QString( "Failed to find function %1. %2" ).arg( CREATE_MODULE ).arg( dlerror() );
612     }
613   }
614 #endif
615
616   XGUI_Module* aModule = crtInst ? crtInst(this) : 0;
617
618   if (!err.isEmpty()) {
619     if (mainWindow()) {
620       QMessageBox::warning(mainWindow(), tr("Error"), err);
621     } else {
622       qWarning( qPrintable( err ));
623     }
624   }
625   return aModule;
626 }
627
628 //******************************************************
629 bool XGUI_Workshop::activateModule()
630 {
631   Config_ModuleReader aModuleReader;
632   QString moduleName = QString::fromStdString(aModuleReader.getModuleName());
633   myPartSetModule = loadModule(moduleName);
634   if (!myPartSetModule)
635     return false;
636   myPartSetModule->createFeatures();
637   myActionsMgr->update();
638   return true;
639 }
640
641 //******************************************************
642 void XGUI_Workshop::updateCommandStatus()
643 {
644   QList<QAction*> aCommands;
645   if (isSalomeMode()) { // update commands in SALOME mode
646     aCommands = salomeConnector()->commandList();
647   } else {
648     XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
649     foreach (XGUI_Command* aCmd, aMenuBar->features())
650       aCommands.append(aCmd);
651   }
652   PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
653   if (aMgr->hasRootDocument()) {
654     QAction* aUndoCmd;
655     QAction* aRedoCmd;
656     foreach(QAction* aCmd, aCommands) {
657       QString aId = aCmd->data().toString();
658       if (aId == "UNDO_CMD")
659         aUndoCmd = aCmd;
660       else if (aId == "REDO_CMD")
661         aRedoCmd = aCmd;
662       else // Enable all commands
663         aCmd->setEnabled(true);
664     }
665     DocumentPtr aDoc = aMgr->rootDocument();
666     aUndoCmd->setEnabled(aDoc->canUndo());
667     aRedoCmd->setEnabled(aDoc->canRedo());
668   } else {
669     foreach(QAction* aCmd, aCommands) {
670       QString aId = aCmd->data().toString();
671       if (aId == "NEW_CMD")
672         aCmd->setEnabled(true);
673       else if (aId == "EXIT_CMD")
674         aCmd->setEnabled(true);
675       else 
676         aCmd->setEnabled(false);
677     }
678   }
679   myActionsMgr->update();
680 }
681
682 //******************************************************
683 QDockWidget* XGUI_Workshop::createObjectBrowser(QWidget* theParent)
684 {
685   QDockWidget* aObjDock = new QDockWidget(theParent);
686   aObjDock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
687   aObjDock->setWindowTitle(tr("Object browser"));
688   aObjDock->setStyleSheet("::title { position: relative; padding-left: 5px; text-align: left center }");
689   myObjectBrowser = new XGUI_ObjectsBrowser(aObjDock);
690   connect(myObjectBrowser, SIGNAL(activePartChanged(FeaturePtr)), this, SLOT(changeCurrentDocument(FeaturePtr)));
691   aObjDock->setWidget(myObjectBrowser);
692
693   myContextMenuMgr->connectObjectBrowser();
694   return aObjDock;
695 }
696
697 //******************************************************
698 /*
699  * Creates dock widgets, places them in corresponding area
700  * and tabifies if necessary.
701  */
702 void XGUI_Workshop::createDockWidgets()
703 {
704   QMainWindow* aDesktop = isSalomeMode()? salomeConnector()->desktop() :
705                                           myMainWindow;
706   QDockWidget* aObjDock = createObjectBrowser(aDesktop);
707   aDesktop->addDockWidget(Qt::LeftDockWidgetArea, aObjDock);
708   myPropertyPanel = new XGUI_PropertyPanel(aDesktop);
709   aDesktop->addDockWidget(Qt::LeftDockWidgetArea, myPropertyPanel);
710   hidePropertyPanel(); //<! Invisible by default
711   hideObjectBrowser();
712   aDesktop->tabifyDockWidget(aObjDock, myPropertyPanel);
713
714   QPushButton* aOkBtn = myPropertyPanel->findChild<QPushButton*>(XGUI::PROP_PANEL_OK);
715   connect(aOkBtn, SIGNAL(clicked()), myOperationMgr, SLOT(onCommitOperation()));
716   QPushButton* aCancelBtn = myPropertyPanel->findChild<QPushButton*>(XGUI::PROP_PANEL_CANCEL);
717   connect(aCancelBtn, SIGNAL(clicked()), myOperationMgr, SLOT(onAbortOperation()));
718
719   connect(myPropertyPanel, SIGNAL(keyReleased(const std::string&, QKeyEvent*)),
720           myOperationMgr, SLOT(onKeyReleased(const std::string&, QKeyEvent*)));
721 }
722
723 //******************************************************
724 void XGUI_Workshop::showPropertyPanel()
725 {
726   QAction* aViewAct = myPropertyPanel->toggleViewAction();
727   //<! Restore ability to close panel from the window's menu
728   aViewAct->setEnabled(true);
729   myPropertyPanel->show();
730   myPropertyPanel->raise();
731 }
732
733 //******************************************************
734 void XGUI_Workshop::hidePropertyPanel()
735 {
736   QAction* aViewAct = myPropertyPanel->toggleViewAction();
737   //<! Do not allow to show empty property panel
738   aViewAct->setEnabled(false);
739   myPropertyPanel->hide();
740 }
741
742 //******************************************************
743 void XGUI_Workshop::showObjectBrowser()
744 {
745   myObjectBrowser->parentWidget()->show();
746 }
747
748 //******************************************************
749 void XGUI_Workshop::hideObjectBrowser()
750 {
751   myObjectBrowser->parentWidget()->hide();
752 }
753
754 //******************************************************
755 void XGUI_Workshop::onFeatureTriggered()
756 {
757   QAction* aCmd = dynamic_cast<QAction*>(sender());
758   if (aCmd) {
759     QString aId = salomeConnector()->commandId(aCmd);
760     if (!aId.isNull())
761       myPartSetModule->launchOperation(aId);
762   }
763 }
764
765 //******************************************************
766 void XGUI_Workshop::changeCurrentDocument(FeaturePtr thePart)
767 {
768   PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
769   if (thePart) {
770     DocumentPtr aFeaDoc;
771     if (!XGUI_Tools::isModelObject(thePart)) {
772       aFeaDoc = thePart->data()->docRef("PartDocument")->value();
773     } else {
774       ObjectPtr aObject = boost::dynamic_pointer_cast<ModelAPI_Object>(thePart);
775       aFeaDoc = aObject->featureRef()->data()->docRef("PartDocument")->value();
776     }
777     if (aFeaDoc)
778       aMgr->setCurrentDocument(aFeaDoc);
779   } else {
780     aMgr->setCurrentDocument(aMgr->rootDocument());
781   }
782 }
783
784 //******************************************************
785 void XGUI_Workshop::salomeViewerSelectionChanged()
786 {
787   emit salomeViewerSelection();
788 }
789
790
791 //**************************************************************
792 XGUI_SalomeViewer* XGUI_Workshop::salomeViewer() const 
793
794   return mySalomeConnector->viewer(); 
795 }
796
797 //**************************************************************
798 void XGUI_Workshop::onContextMenuCommand(const QString& theId, bool isChecked)
799 {
800   QFeatureList aFeatures = mySelector->selectedFeatures();
801   if ((theId == "ACTIVATE_PART_CMD") && (aFeatures.size() > 0))
802     activatePart(aFeatures.first());
803   else if (theId == "DEACTIVATE_PART_CMD") 
804     activatePart(FeaturePtr());
805   else if (theId == "DELETE_CMD")
806     deleteFeatures(aFeatures);
807   else if (theId == "SHOW_CMD")
808     showFeatures(aFeatures, true);
809   else if (theId == "HIDE_CMD")
810     showFeatures(aFeatures, false);
811 }
812
813 //**************************************************************
814 void XGUI_Workshop::activatePart(FeaturePtr theFeature)
815 {
816   changeCurrentDocument(theFeature);
817   myObjectBrowser->activatePart(theFeature);
818 }
819
820 //**************************************************************
821 void XGUI_Workshop::activateLastPart()
822 {
823   PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
824   DocumentPtr aDoc = aMgr->rootDocument();
825   FeaturePtr aLastPart = aDoc->feature(PARTS_GROUP, aDoc->size(PARTS_GROUP) - 1, true);
826   activatePart(aLastPart);
827 }
828
829 //**************************************************************
830 void XGUI_Workshop::deleteFeatures(QFeatureList theList)
831 {
832   QMainWindow* aDesktop = isSalomeMode()? salomeConnector()->desktop() : myMainWindow;
833   QMessageBox::StandardButton aRes = QMessageBox::warning(aDesktop, tr("Delete features"), 
834                                                           tr("Seleted features will be deleted. Continue?"), 
835                                                           QMessageBox::No | QMessageBox::Yes, QMessageBox::No);
836   if (aRes == QMessageBox::Yes) {
837     PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
838     aMgr->rootDocument()->startOperation();
839     foreach (FeaturePtr aFeature, theList) {
840       if (aFeature->getKind() == PARTSET_PART_KIND) {
841         DocumentPtr aDoc;
842         if (!XGUI_Tools::isModelObject(aFeature)) {
843           aDoc = aFeature->data()->docRef("PartDocument")->value();
844         } else {
845           ObjectPtr aObject = boost::dynamic_pointer_cast<ModelAPI_Object>(aFeature);
846           aDoc = aObject->featureRef()->data()->docRef("PartDocument")->value();
847           aFeature = aObject->featureRef();
848         }
849         if (aDoc == aMgr->currentDocument()) {
850           aDoc->close();
851         }
852       } else {
853         if (XGUI_Tools::isModelObject(aFeature)) {
854           ObjectPtr aObject = boost::dynamic_pointer_cast<ModelAPI_Object>(aFeature);
855           aFeature = aObject->featureRef();
856         }
857       }
858       if (myDisplayer->isVisible(aFeature))
859         myDisplayer->erase(aFeature, false);
860       aFeature->document()->removeFeature(aFeature);
861     }
862     myDisplayer->updateViewer();
863     aMgr->rootDocument()->finishOperation();
864   }
865 }
866
867 //**************************************************************
868 void XGUI_Workshop::showFeatures(QFeatureList theList, bool isVisible)
869 {
870   if (isVisible) {
871     foreach (FeaturePtr aFeature, theList) {
872       myDisplayer->display(aFeature, false);
873     }
874   } else {
875     foreach (FeaturePtr aFeature, theList) {
876       myDisplayer->erase(aFeature, false);
877     }
878   }
879   myDisplayer->updateViewer();
880 }