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