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