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