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