]> SALOME platform Git repositories - modules/shaper.git/blob - src/XGUI/XGUI_Workshop.cpp
Salome HOME
Merge remote-tracking branch 'remotes/origin/master' into SketchSolver
[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       }
237     }
238     if (aHasPart) {
239       //The created part will be created in Object Browser later and we have to activate it
240       // only when it is created everywere
241       QTimer::singleShot(50, this, SLOT(activateLastPart()));
242     }
243   }
244
245   //Update property panel on corresponding message. If there is no current operation (no
246   //property panel), or received message has different feature to the current - do nothing.
247   static Events_ID aFeatureUpdatedId = Events_Loop::loop()->eventByName(EVENT_FEATURE_UPDATED);
248   if (theMessage->eventID() == aFeatureUpdatedId && myOperationMgr->hasOperation())
249   {
250     const Model_FeatureUpdatedMessage* anUpdateMsg =
251         dynamic_cast<const Model_FeatureUpdatedMessage*>(theMessage);
252     std::set<FeaturePtr> aFeatures = anUpdateMsg->features();
253
254     FeaturePtr aCurrentFeature = myOperationMgr->currentOperation()->feature();
255     std::set<FeaturePtr>::const_iterator aIt;
256     for (aIt = aFeatures.begin(); aIt != aFeatures.end(); ++aIt) {
257       FeaturePtr aNewFeature = (*aIt);
258       if(aNewFeature == aCurrentFeature) {
259         myPropertyPanel->updateContentWidget(aCurrentFeature);
260         break;
261       } 
262     }
263   }
264   //An operation passed by message. Start it, process and commit.
265   const Config_PointerMessage* aPartSetMsg = dynamic_cast<const Config_PointerMessage*>(theMessage);
266   if (aPartSetMsg) {
267     ModuleBase_Operation* anOperation =
268         (ModuleBase_Operation*)(aPartSetMsg->pointer());
269
270     if (myOperationMgr->startOperation(anOperation)) {
271       myPropertyPanel->updateContentWidget(anOperation->feature());
272       if (anOperation->getDescription()->xmlRepresentation().isEmpty()) {
273         anOperation->commit();
274         updateCommandStatus();
275       }
276     }
277     return;
278   }
279   //Show error dialog if error message received.
280   const Events_Error* anAppError = dynamic_cast<const Events_Error*>(theMessage);
281   if (anAppError) {
282     emit errorOccurred(QString::fromLatin1(anAppError->description()));
283   }
284
285 }
286
287 //******************************************************
288 void XGUI_Workshop::onOperationStarted()
289 {
290   ModuleBase_Operation* aOperation = myOperationMgr->currentOperation();
291
292   if(!aOperation->getDescription()->xmlRepresentation().isEmpty()) { //!< No need for property panel
293     connectWithOperation(aOperation);
294
295     showPropertyPanel();
296
297     ModuleBase_WidgetFactory aFactory = ModuleBase_WidgetFactory(aOperation, myModuleConnector);
298     QWidget* aContent = myPropertyPanel->contentWidget();
299     qDeleteAll(aContent->children());
300     aFactory.createWidget(aContent);
301     myPropertyPanel->setModelWidgets(aFactory.getModelWidgets());
302     myPropertyPanel->setWindowTitle(aOperation->getDescription()->description());
303   }
304   updateCommandStatus();
305 }
306
307 //******************************************************
308 void XGUI_Workshop::onOperationStopped(ModuleBase_Operation* theOperation)
309 {
310   //!< No need for property panel
311   updateCommandStatus();
312   hidePropertyPanel();
313 }
314
315 /*
316  *
317  */
318 void XGUI_Workshop::addFeature(const Config_FeatureMessage* theMessage)
319 {
320   if (!theMessage) {
321 #ifdef _DEBUG
322     qDebug() << "XGUI_Workshop::addFeature: NULL message.";
323 #endif
324     return;
325   }
326   // Remember features icons
327   myIcons[QString::fromStdString(theMessage->id())] = QString::fromStdString(theMessage->icon());
328
329   //Find or create Workbench
330   QString aWchName = QString::fromStdString(theMessage->workbenchId());
331   QString aNestedFeatures = QString::fromStdString(theMessage->nestedFeatures());
332   bool isUsePropPanel = theMessage->isUseInput();
333   QString aId = QString::fromStdString(theMessage->id());
334   if (isSalomeMode()) {
335     QAction* aAction = salomeConnector()->addFeature(aWchName,
336                               aId,
337                               QString::fromStdString(theMessage->text()),
338                               QString::fromStdString(theMessage->tooltip()),
339                               QIcon(theMessage->icon().c_str()),
340                               QKeySequence(), isUsePropPanel);
341     salomeConnector()->setNestedActions(aId, aNestedFeatures.split(" "));
342     myActionsMgr->addCommand(aAction);
343     myPartSetModule->featureCreated(aAction);
344   } else {
345
346     XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
347     XGUI_Workbench* aPage = aMenuBar->findWorkbench(aWchName);
348     if (!aPage) {
349       aPage = addWorkbench(aWchName);
350     }
351     //Find or create Group
352     QString aGroupName = QString::fromStdString(theMessage->groupId());
353     XGUI_MenuGroupPanel* aGroup = aPage->findGroup(aGroupName);
354     if (!aGroup) {
355       aGroup = aPage->addGroup(aGroupName);
356     }
357     //Create feature...
358     XGUI_Command* aCommand = aGroup->addFeature(aId,
359                                                 QString::fromStdString(theMessage->text()),
360                                                 QString::fromStdString(theMessage->tooltip()),
361                                                 QIcon(theMessage->icon().c_str()),
362                                                 QKeySequence(), isUsePropPanel);
363     aCommand->setNestedCommands(aNestedFeatures.split(" ", QString::SkipEmptyParts));
364     myActionsMgr->addCommand(aCommand);
365     myPartSetModule->featureCreated(aCommand);
366   }
367 }
368
369 /*
370  * Makes a signal/slot connections between Property Panel
371  * and given operation. The given operation becomes a
372  * current operation and previous operation if exists
373  */
374 void XGUI_Workshop::connectWithOperation(ModuleBase_Operation* theOperation)
375 {
376   QAction* aCommand = 0;
377   if (isSalomeMode()) {
378     aCommand = salomeConnector()->command(theOperation->getDescription()->operationId());
379   } else {
380     XGUI_MainMenu* aMenu = myMainWindow->menuObject();
381     aCommand = aMenu->feature(theOperation->getDescription()->operationId());
382   }
383   //Abort operation on uncheck the command
384   connect(aCommand, SIGNAL(triggered(bool)), theOperation, SLOT(setRunning(bool)));
385 }
386
387 /*
388  * Saves document with given name.
389  */
390 void XGUI_Workshop::saveDocument(QString theName)
391 {
392   QApplication::restoreOverrideCursor();
393   PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
394   DocumentPtr aDoc = aMgr->rootDocument();
395   aDoc->save(theName.toLatin1().constData());
396   QApplication::restoreOverrideCursor();
397 }
398
399 //******************************************************
400 void XGUI_Workshop::onExit()
401 {
402   PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
403   DocumentPtr aDoc = aMgr->rootDocument();
404   if(aDoc->isModified()) {
405     int anAnswer = QMessageBox::question(
406         myMainWindow, tr("Save current file"),
407         tr("The document is modified, save before exit?"),
408         QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel, QMessageBox::Cancel);
409     if(anAnswer == QMessageBox::Save) {
410       bool saved = onSave();
411       if(!saved) {
412         return;
413       }
414     } else if (anAnswer == QMessageBox::Cancel) {
415       return;
416     }
417   }
418   qApp->exit();
419 }
420
421 //******************************************************
422 void XGUI_Workshop::onNew()
423 {
424   QApplication::setOverrideCursor(Qt::WaitCursor);
425   if (objectBrowser() == 0) {
426     createDockWidgets();
427     mySelector->connectViewers();
428   }
429   myViewerProxy->connectToViewer();
430   showObjectBrowser();
431   if (!isSalomeMode()) {
432     myMainWindow->showPythonConsole();
433     QMdiSubWindow* aWnd = myMainWindow->viewer()->createView();
434     aWnd->showMaximized();
435     updateCommandStatus();
436   }
437   QApplication::restoreOverrideCursor();
438 }
439
440 //******************************************************
441 void XGUI_Workshop::onOpen()
442 {
443   //save current file before close if modified
444   PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
445   DocumentPtr aDoc = aMgr->rootDocument();
446   if(aDoc->isModified()) {
447     //TODO(sbh): re-launch the app?
448     int anAnswer = QMessageBox::question(
449         myMainWindow, tr("Save current file"),
450         tr("The document is modified, save before opening another?"),
451         QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel, QMessageBox::Cancel);
452     if(anAnswer == QMessageBox::Save) {
453       onSave();
454     } else if (anAnswer == QMessageBox::Cancel) {
455       return;
456     }
457     aDoc->close();
458     myCurrentDir = "";
459   }
460
461   //show file dialog, check if readable and open
462   myCurrentDir = QFileDialog::getExistingDirectory(mainWindow());
463   if(myCurrentDir.isEmpty())
464     return;
465   QFileInfo aFileInfo(myCurrentDir);
466   if(!aFileInfo.exists() || !aFileInfo.isReadable()) {
467     QMessageBox::critical(myMainWindow, tr("Warning"), tr("Unable to open the file."));
468     myCurrentDir = "";
469     return;
470   }
471   QApplication::setOverrideCursor(Qt::WaitCursor);
472   aDoc->load(myCurrentDir.toLatin1().constData());
473   QApplication::restoreOverrideCursor();
474   updateCommandStatus();
475 }
476
477 //******************************************************
478 bool XGUI_Workshop::onSave()
479 {
480   if(myCurrentDir.isEmpty()) {
481     return onSaveAs();
482   }
483   saveDocument(myCurrentDir);
484   updateCommandStatus();
485   return true;
486 }
487
488 //******************************************************
489 bool XGUI_Workshop::onSaveAs()
490 {
491   QFileDialog dialog(mainWindow());
492   dialog.setWindowTitle(tr("Select directory to save files..."));
493   dialog.setFileMode(QFileDialog::Directory);
494   dialog.setFilter(tr("Folders (*)"));
495   dialog.setOptions(QFileDialog::HideNameFilterDetails | QFileDialog::ShowDirsOnly);
496   dialog.setViewMode(QFileDialog::Detail);
497
498   if(!dialog.exec()) {
499     return false;
500   }
501   QString aTempDir = dialog.selectedFiles().first();
502   QDir aDir(aTempDir);
503   if(aDir.exists() && !aDir.entryInfoList(QDir::NoDotAndDotDot|QDir::AllEntries).isEmpty()) {
504     int answer = QMessageBox::question(myMainWindow,
505                                        //: Title of the dialog which asks user if he wants to save study in existing non-empty folder
506                                        tr("Save"),
507                                        tr("The folder already contains some files, save anyway?"),
508                                        QMessageBox::Save|QMessageBox::Cancel);
509     if(answer == QMessageBox::Cancel) {
510       return false;
511     }
512   }
513   myCurrentDir = aTempDir;
514   return onSave();
515 }
516
517 //******************************************************
518 void XGUI_Workshop::onUndo()
519 {
520   objectBrowser()->treeView()->setCurrentIndex(QModelIndex());
521   PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
522   DocumentPtr aDoc = aMgr->rootDocument();
523   if (aDoc->isOperation())
524     operationMgr()->abortOperation();
525   aDoc->undo();
526   updateCommandStatus();
527 }
528
529 //******************************************************
530 void XGUI_Workshop::onRedo()
531 {
532   objectBrowser()->treeView()->setCurrentIndex(QModelIndex());
533   PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
534   DocumentPtr aDoc = aMgr->rootDocument();
535   if (aDoc->isOperation())
536     operationMgr()->abortOperation();
537   aDoc->redo();
538   updateCommandStatus();
539 }
540
541 //******************************************************
542 XGUI_Module* XGUI_Workshop::loadModule(const QString& theModule)
543 {
544   QString libName =
545       QString::fromStdString(library(theModule.toStdString()));
546   if (libName.isEmpty()) {
547     qWarning(
548     qPrintable( tr( "Information about module \"%1\" doesn't exist." ).arg( theModule ) ));
549     return 0;
550   }
551
552   QString err;
553   CREATE_FUNC crtInst = 0;
554
555 #ifdef WIN32
556   HINSTANCE modLib = ::LoadLibrary((LPTSTR) qPrintable(libName));
557   if (!modLib) {
558     LPVOID lpMsgBuf;
559     ::FormatMessage(
560         FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
561         0, ::GetLastError(), 0, (LPTSTR) & lpMsgBuf, 0, 0);
562     QString aMsg((char*) &lpMsgBuf);
563     err = QString("Failed to load  %1. %2").arg(libName).arg(aMsg);
564     ::LocalFree(lpMsgBuf);
565   } else {
566     crtInst = (CREATE_FUNC) ::GetProcAddress(modLib, CREATE_MODULE);
567     if (!crtInst) {
568       LPVOID lpMsgBuf;
569       ::FormatMessage(
570           FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM
571               | FORMAT_MESSAGE_IGNORE_INSERTS,
572           0, ::GetLastError(), 0, (LPTSTR) & lpMsgBuf, 0, 0);
573       QString aMsg((char*) &lpMsgBuf);
574       err = QString("Failed to find  %1 function. %2").arg( CREATE_MODULE).arg(aMsg);
575       ::LocalFree(lpMsgBuf);
576     }
577   }
578 #else
579   void* modLib = dlopen( libName.toLatin1(), RTLD_LAZY | RTLD_GLOBAL );
580   if ( !modLib ) {
581     err = QString( "Can not load library %1. %2" ).arg( libName ).arg( dlerror() );
582   } else {
583     crtInst = (CREATE_FUNC)dlsym( modLib, CREATE_MODULE );
584     if ( !crtInst ) {
585       err = QString( "Failed to find function %1. %2" ).arg( CREATE_MODULE ).arg( dlerror() );
586     }
587   }
588 #endif
589
590   XGUI_Module* aModule = crtInst ? crtInst(this) : 0;
591
592   if (!err.isEmpty()) {
593     if (mainWindow()) {
594       QMessageBox::warning(mainWindow(), tr("Error"), err);
595     } else {
596       qWarning( qPrintable( err ));
597     }
598   }
599   return aModule;
600 }
601
602 //******************************************************
603 bool XGUI_Workshop::activateModule()
604 {
605   Config_ModuleReader aModuleReader;
606   QString moduleName = QString::fromStdString(aModuleReader.getModuleName());
607   myPartSetModule = loadModule(moduleName);
608   if (!myPartSetModule)
609     return false;
610   myPartSetModule->createFeatures();
611   myActionsMgr->update();
612   return true;
613 }
614
615 //******************************************************
616 void XGUI_Workshop::updateCommandStatus()
617 {
618   QList<QAction*> aCommands;
619   if (isSalomeMode()) { // update commands in SALOME mode
620     aCommands = salomeConnector()->commandList();
621   } else {
622     XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
623     foreach (XGUI_Command* aCmd, aMenuBar->features())
624       aCommands.append(aCmd);
625   }
626   PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
627   if (aMgr->hasRootDocument()) {
628     QAction* aUndoCmd;
629     QAction* aRedoCmd;
630     foreach(QAction* aCmd, aCommands) {
631       QString aId = aCmd->data().toString();
632       if (aId == "UNDO_CMD")
633         aUndoCmd = aCmd;
634       else if (aId == "REDO_CMD")
635         aRedoCmd = aCmd;
636       else // Enable all commands
637         aCmd->setEnabled(true);
638     }
639     DocumentPtr aDoc = aMgr->rootDocument();
640     aUndoCmd->setEnabled(aDoc->canUndo());
641     aRedoCmd->setEnabled(aDoc->canRedo());
642   } else {
643     foreach(QAction* aCmd, aCommands) {
644       QString aId = aCmd->data().toString();
645       if (aId == "NEW_CMD")
646         aCmd->setEnabled(true);
647       else if (aId == "EXIT_CMD")
648         aCmd->setEnabled(true);
649       else 
650         aCmd->setEnabled(false);
651     }
652   }
653   myActionsMgr->update();
654 }
655
656 //******************************************************
657 QDockWidget* XGUI_Workshop::createObjectBrowser(QWidget* theParent)
658 {
659   QDockWidget* aObjDock = new QDockWidget(theParent);
660   aObjDock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
661   aObjDock->setWindowTitle(tr("Object browser"));
662   aObjDock->setStyleSheet("::title { position: relative; padding-left: 5px; text-align: left center }");
663   myObjectBrowser = new XGUI_ObjectsBrowser(aObjDock);
664   connect(myObjectBrowser, SIGNAL(activePartChanged(FeaturePtr)), this, SLOT(changeCurrentDocument(FeaturePtr)));
665   aObjDock->setWidget(myObjectBrowser);
666
667   myContextMenuMgr->connectObjectBrowser();
668   return aObjDock;
669 }
670
671 //******************************************************
672 /*
673  * Creates dock widgets, places them in corresponding area
674  * and tabifies if necessary.
675  */
676 void XGUI_Workshop::createDockWidgets()
677 {
678   QMainWindow* aDesktop = isSalomeMode()? salomeConnector()->desktop() :
679                                           myMainWindow;
680   QDockWidget* aObjDock = createObjectBrowser(aDesktop);
681   aDesktop->addDockWidget(Qt::LeftDockWidgetArea, aObjDock);
682   myPropertyPanel = new XGUI_PropertyPanel(aDesktop);
683   aDesktop->addDockWidget(Qt::LeftDockWidgetArea, myPropertyPanel);
684   hidePropertyPanel(); //<! Invisible by default
685   hideObjectBrowser();
686   aDesktop->tabifyDockWidget(aObjDock, myPropertyPanel);
687
688   QPushButton* aOkBtn = myPropertyPanel->findChild<QPushButton*>(XGUI::PROP_PANEL_OK);
689   connect(aOkBtn, SIGNAL(clicked()), myOperationMgr, SLOT(onCommitOperation()));
690   QPushButton* aCancelBtn = myPropertyPanel->findChild<QPushButton*>(XGUI::PROP_PANEL_CANCEL);
691   connect(aCancelBtn, SIGNAL(clicked()), myOperationMgr, SLOT(onAbortOperation()));
692
693   connect(myPropertyPanel, SIGNAL(keyReleased(const std::string&, QKeyEvent*)),
694           myOperationMgr, SLOT(onKeyReleased(const std::string&, QKeyEvent*)));
695 }
696
697 //******************************************************
698 void XGUI_Workshop::showPropertyPanel()
699 {
700   QAction* aViewAct = myPropertyPanel->toggleViewAction();
701   //<! Restore ability to close panel from the window's menu
702   aViewAct->setEnabled(true);
703   myPropertyPanel->show();
704   myPropertyPanel->raise();
705 }
706
707 //******************************************************
708 void XGUI_Workshop::hidePropertyPanel()
709 {
710   QAction* aViewAct = myPropertyPanel->toggleViewAction();
711   //<! Do not allow to show empty property panel
712   aViewAct->setEnabled(false);
713   myPropertyPanel->hide();
714 }
715
716 //******************************************************
717 void XGUI_Workshop::showObjectBrowser()
718 {
719   myObjectBrowser->parentWidget()->show();
720 }
721
722 //******************************************************
723 void XGUI_Workshop::hideObjectBrowser()
724 {
725   myObjectBrowser->parentWidget()->hide();
726 }
727
728 //******************************************************
729 void XGUI_Workshop::onFeatureTriggered()
730 {
731   QAction* aCmd = dynamic_cast<QAction*>(sender());
732   if (aCmd) {
733     QString aId = salomeConnector()->commandId(aCmd);
734     if (!aId.isNull())
735       myPartSetModule->launchOperation(aId);
736   }
737 }
738
739 //******************************************************
740 void XGUI_Workshop::changeCurrentDocument(FeaturePtr thePart)
741 {
742   PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
743   if (thePart) {
744     DocumentPtr aFeaDoc;
745     if (!XGUI_Tools::isModelObject(thePart)) {
746       aFeaDoc = thePart->data()->docRef("PartDocument")->value();
747     } else {
748       ObjectPtr aObject = boost::dynamic_pointer_cast<ModelAPI_Object>(thePart);
749       aFeaDoc = aObject->featureRef()->data()->docRef("PartDocument")->value();
750     }
751     if (aFeaDoc)
752       aMgr->setCurrentDocument(aFeaDoc);
753   } else {
754     aMgr->setCurrentDocument(aMgr->rootDocument());
755   }
756 }
757
758 //******************************************************
759 void XGUI_Workshop::salomeViewerSelectionChanged()
760 {
761   emit salomeViewerSelection();
762 }
763
764
765 //**************************************************************
766 XGUI_SalomeViewer* XGUI_Workshop::salomeViewer() const 
767
768   return mySalomeConnector->viewer(); 
769 }
770
771 //**************************************************************
772 void XGUI_Workshop::onContextMenuCommand(const QString& theId, bool isChecked)
773 {
774   QFeatureList aFeatures = mySelector->selectedFeatures();
775   if ((theId == "ACTIVATE_PART_CMD") && (aFeatures.size() > 0))
776     activatePart(aFeatures.first());
777   else if (theId == "DEACTIVATE_PART_CMD") 
778     activatePart(FeaturePtr());
779   else if (theId == "DELETE_CMD")
780     deleteFeatures(aFeatures);
781   else if (theId == "SHOW_CMD")
782     showFeatures(aFeatures, true);
783   else if (theId == "HIDE_CMD")
784     showFeatures(aFeatures, false);
785 }
786
787 //**************************************************************
788 void XGUI_Workshop::activatePart(FeaturePtr theFeature)
789 {
790   changeCurrentDocument(theFeature);
791   myObjectBrowser->activatePart(theFeature);
792 }
793
794 //**************************************************************
795 void XGUI_Workshop::activateLastPart()
796 {
797   PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
798   DocumentPtr aDoc = aMgr->rootDocument();
799   FeaturePtr aLastPart = aDoc->feature(PARTS_GROUP, aDoc->size(PARTS_GROUP) - 1, true);
800   activatePart(aLastPart);
801 }
802
803 //**************************************************************
804 void XGUI_Workshop::deleteFeatures(QFeatureList theList)
805 {
806   QMainWindow* aDesktop = isSalomeMode()? salomeConnector()->desktop() : myMainWindow;
807   QMessageBox::StandardButton aRes = QMessageBox::warning(aDesktop, tr("Delete features"), 
808                                                           tr("Seleted features will be deleted. Continue?"), 
809                                                           QMessageBox::No | QMessageBox::Yes, QMessageBox::No);
810   if (aRes == QMessageBox::Yes) {
811     PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
812     aMgr->rootDocument()->startOperation();
813     foreach (FeaturePtr aFeature, theList) {
814       if (aFeature->getKind() == PARTSET_PART_KIND) {
815         DocumentPtr aDoc;
816         if (!XGUI_Tools::isModelObject(aFeature)) {
817           aDoc = aFeature->data()->docRef("PartDocument")->value();
818         } else {
819           ObjectPtr aObject = boost::dynamic_pointer_cast<ModelAPI_Object>(aFeature);
820           aDoc = aObject->featureRef()->data()->docRef("PartDocument")->value();
821           aFeature = aObject->featureRef();
822         }
823         if (aDoc == aMgr->currentDocument()) {
824           aDoc->close();
825         }
826       } else {
827         if (XGUI_Tools::isModelObject(aFeature)) {
828           ObjectPtr aObject = boost::dynamic_pointer_cast<ModelAPI_Object>(aFeature);
829           aFeature = aObject->featureRef();
830         }
831       }
832       aFeature->document()->removeFeature(aFeature);
833     }
834     aMgr->rootDocument()->finishOperation();
835   }
836 }
837
838 //**************************************************************
839 void XGUI_Workshop::showFeatures(QFeatureList theList, bool isVisible)
840 {
841 //  foreach (FeaturePtr aFeature, theList) {
842 //  }
843 }