Salome HOME
Connect operations to SALOME (Issue #31)
[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 "XGUI_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
18 #include <ModelAPI_PluginManager.h>
19 #include <ModelAPI_Feature.h>
20 #include <ModelAPI_Data.h>
21 #include <ModelAPI_AttributeDocRef.h>
22
23 #include <Events_Loop.h>
24 #include <ModuleBase_PropPanelOperation.h>
25 #include <ModuleBase_Operation.h>
26 #include <Config_FeatureMessage.h>
27 #include <Config_PointerMessage.h>
28
29 #include <QApplication>
30 #include <QFileDialog>
31 #include <QMessageBox>
32 #include <QMdiSubWindow>
33 #include <QPushButton>
34 #include <QDockWidget>
35 #include <QLayout>
36
37 #ifdef _DEBUG
38 #include <QDebug>
39 #endif
40
41 #ifdef WIN32
42 #include <windows.h>
43 #else
44 #include <dlfcn.h>
45 #endif
46
47 XGUI_Workshop::XGUI_Workshop(XGUI_SalomeConnector* theConnector)
48   : QObject(), 
49   myPartSetModule(NULL),
50   mySalomeConnector(theConnector),
51   myPropertyPanelDock(0),
52   myObjectBrowser(0)
53 {
54   myMainWindow = mySalomeConnector? 0 : new XGUI_MainWindow();
55   mySelector = new XGUI_SelectionMgr(this);
56   myDisplayer = myMainWindow? new XGUI_Displayer(myMainWindow->viewer()) : 0;
57   myOperationMgr = new XGUI_OperationMgr(this);
58   connect(myOperationMgr, SIGNAL(operationStarted()),  this, SLOT(onOperationStarted()));
59   connect(myOperationMgr, SIGNAL(operationStopped(ModuleBase_Operation*)),
60           this, SLOT(onOperationStopped(ModuleBase_Operation*)));
61 }
62
63 //******************************************************
64 XGUI_Workshop::~XGUI_Workshop(void)
65 {
66 }
67
68 //******************************************************
69 void XGUI_Workshop::startApplication()
70 {
71   initMenu();
72   //Initialize event listening
73   Events_Loop* aLoop = Events_Loop::loop();
74   //TODO(sbh): Implement static method to extract event id [SEID]
75   Events_ID aFeatureId = aLoop->eventByName("FeatureEvent");
76   aLoop->registerListener(this, aFeatureId);
77   Events_ID aPartSetId = aLoop->eventByName("PartSetModuleEvent");
78   aLoop->registerListener(this, aPartSetId);
79   activateModule();
80   if (myMainWindow) {
81     myMainWindow->show();
82     updateCommandStatus();
83   }
84   onNew();
85   // Testing of document creation
86   //boost::shared_ptr<ModelAPI_PluginManager> aMgr = ModelAPI_PluginManager::get();
87   //boost::shared_ptr<ModelAPI_Feature> aPoint1 = aMgr->rootDocument()->addFeature("Point");
88   //boost::shared_ptr<ModelAPI_Feature> aPart = aMgr->rootDocument()->addFeature("Part");
89   //aPart->execute();
90   //aMgr->setCurrentDocument(aPart->data()->docRef("PartDocument")->value());
91   //boost::shared_ptr<ModelAPI_Feature> aPoint2 = aMgr->rootDocument()->addFeature("Point");
92   //aPoint2 = aMgr->rootDocument()->addFeature("Point");
93
94   //aPart = aMgr->rootDocument()->addFeature("Part");
95   //aPart->execute();
96 }
97
98 //******************************************************
99 void XGUI_Workshop::initMenu()
100 {
101   if (isSalomeMode()) {
102     // Create only Undo, Redo commands
103     salomeConnector()->addEditCommand("UNDO_CMD", 
104                                       tr("Undo"), tr("Undo last command"),
105                                       QIcon(":pictures/undo.png"), 
106                                       false, this, SLOT(onUndo()),
107                                       QKeySequence::Undo);
108     salomeConnector()->addEditCommand("REDO_CMD", 
109                                       tr("Redo"), tr("Redo last command"),
110                                       QIcon(":pictures/redo.png"), 
111                                       false, this, SLOT(onRedo()),
112                                       QKeySequence::Redo);
113     salomeConnector()->addEditMenuSeparator();
114     return;
115   }
116   XGUI_Workbench* aPage = myMainWindow->menuObject()->generalPage();
117
118   // File commands group
119   XGUI_MenuGroupPanel* aGroup = aPage->addGroup("Default");
120
121   XGUI_Command* aCommand;
122
123   aCommand = aGroup->addFeature("SAVE_CMD", tr("Save..."), tr("Save the document"),
124                                 QIcon(":pictures/save.png"), QKeySequence::Save);
125   aCommand->connectTo(this, SLOT(onSave()));
126   //aCommand->disable();
127
128   aCommand = aGroup->addFeature("UNDO_CMD", tr("Undo"), tr("Undo last command"),
129                                 QIcon(":pictures/undo.png"), QKeySequence::Undo);
130   aCommand->connectTo(this, SLOT(onUndo()));
131
132   aCommand = aGroup->addFeature("REDO_CMD", tr("Redo"), tr("Redo last command"),
133                                 QIcon(":pictures/redo.png"), QKeySequence::Redo);
134   aCommand->connectTo(this, SLOT(onRedo()));
135
136   aCommand = aGroup->addFeature("REBUILD_CMD", tr("Rebuild"), tr("Rebuild data objects"),
137                                 QIcon(":pictures/rebuild.png"));
138
139   aCommand = aGroup->addFeature("SAVEAS_CMD", tr("Save as..."), tr("Save the document into a file"),
140                                 QIcon(":pictures/save.png"));
141   aCommand->connectTo(this, SLOT(onSaveAs()));
142   //aCommand->disable();
143
144   aCommand = aGroup->addFeature("OPEN_CMD", tr("Open..."), tr("Open a new document"),
145                                 QIcon(":pictures/open.png"), QKeySequence::Open);
146   aCommand->connectTo(this, SLOT(onOpen()));
147
148   //aCommand = aGroup->addFeature("NEW_CMD", tr("New"), tr("Create a new document"),
149   //                              QIcon(":pictures/new.png"), QKeySequence::New);
150   //aCommand->connectTo(this, SLOT(onNew()));
151
152   aCommand = aGroup->addFeature("EXIT_CMD", tr("Exit"), tr("Exit application"),
153                                 QIcon(":pictures/close.png"), QKeySequence::Close);
154   aCommand->connectTo(this, SLOT(onExit()));
155
156 }
157
158 //******************************************************
159 XGUI_Workbench* XGUI_Workshop::addWorkbench(const QString& theName)
160 {
161   XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
162   return aMenuBar->addWorkbench(theName);
163 }
164
165 //******************************************************
166 void XGUI_Workshop::processEvent(const Events_Message* theMessage)
167 {
168   static Events_ID aFeatureId = Events_Loop::loop()->eventByName("FeatureEvent");
169   if (theMessage->eventID() == aFeatureId) {
170     const Config_FeatureMessage* aFeatureMsg =
171         dynamic_cast<const Config_FeatureMessage*>(theMessage);
172     addFeature(aFeatureMsg);
173     return;
174   }
175   const Config_PointerMessage* aPartSetMsg =
176       dynamic_cast<const Config_PointerMessage*>(theMessage);
177   if (aPartSetMsg) {
178     ModuleBase_PropPanelOperation* anOperation =
179         (ModuleBase_PropPanelOperation*)(aPartSetMsg->pointer());
180
181     if (myOperationMgr->startOperation(anOperation)) {
182       if (anOperation->isPerformedImmediately()) {
183         anOperation->commit();
184         updateCommandStatus();
185       }
186     }
187     return;
188   }
189
190 #ifdef _DEBUG
191   qDebug() << "XGUI_Workshop::ProcessEvent: "
192   << "Catch message, but it can not be processed.";
193 #endif
194
195 }
196
197 //******************************************************
198 void XGUI_Workshop::onOperationStarted()
199 {
200   ModuleBase_PropPanelOperation* aOperation =
201         (ModuleBase_PropPanelOperation*)(myOperationMgr->currentOperation());
202
203   if(!aOperation->xmlRepresentation().isEmpty()) { //!< No need for property panel
204     connectWithOperation(aOperation);
205     QWidget* aPropWidget = myPropertyPanelDock->findChild<QWidget*>(XGUI::PROP_PANEL_WDG);
206     qDeleteAll(aPropWidget->children());
207
208     showPropertyPanel();
209
210     XGUI_WidgetFactory aFactory = XGUI_WidgetFactory(aOperation);
211     aFactory.createWidget(aPropWidget);
212     setPropertyPannelTitle(aOperation->description());
213   }
214 }
215
216 //******************************************************
217 void XGUI_Workshop::onOperationStopped(ModuleBase_Operation* theOperation)
218 {
219   hidePropertyPanel();
220   updateCommandStatus();
221
222   if (myMainWindow) {
223     XGUI_MainMenu* aMenu = myMainWindow->menuObject();
224     aMenu->restoreCommandState();
225   }
226 }
227
228 /*
229  *
230  */
231 void XGUI_Workshop::addFeature(const Config_FeatureMessage* theMessage)
232 {
233   if (!theMessage) {
234 #ifdef _DEBUG
235     qDebug() << "XGUI_Workshop::addFeature: NULL message.";
236 #endif
237     return;
238   }
239   //Find or create Workbench
240   QString aWchName = QString::fromStdString(theMessage->workbenchId());
241   if (isSalomeMode()) {
242     salomeConnector()->addFeature(aWchName,
243                                   QString::fromStdString(theMessage->id()),
244                                   QString::fromStdString(theMessage->text()),
245                                   QString::fromStdString(theMessage->tooltip()),
246                                   QIcon(theMessage->icon().c_str()),
247                                   false, this, 
248                                   SLOT(onFeatureTriggered()), QKeySequence());
249   } else {
250     XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
251     XGUI_Workbench* aPage = aMenuBar->findWorkbench(aWchName);
252     if (!aPage) {
253       aPage = addWorkbench(aWchName);
254     }
255     //Find or create Group
256     QString aGroupName = QString::fromStdString(theMessage->groupId());
257     XGUI_MenuGroupPanel* aGroup = aPage->findGroup(aGroupName);
258     if (!aGroup) {
259       aGroup = aPage->addGroup(aGroupName);
260     }
261     bool isUsePropPanel = theMessage->isUseInput();
262     //Create feature...
263     XGUI_Command* aCommand = aGroup->addFeature(QString::fromStdString(theMessage->id()),
264                                                 QString::fromStdString(theMessage->text()),
265                                                 QString::fromStdString(theMessage->tooltip()),
266                                                 QIcon(theMessage->icon().c_str()),
267                                                 QKeySequence(), isUsePropPanel);
268     
269     connect(aCommand,                   SIGNAL(toggled(bool)),
270             myMainWindow->menuObject(), SLOT(onFeatureChecked(bool)));
271     myPartSetModule->featureCreated(aCommand);
272   }
273 }
274
275 /*
276  * Makes a signal/slot connections between Property Panel
277  * and given operation. The given operation becomes a
278  * current operation and previous operation if exists
279  */
280 void XGUI_Workshop::connectWithOperation(ModuleBase_Operation* theOperation)
281 {
282   QPushButton* aOkBtn = myPropertyPanelDock->findChild<QPushButton*>(XGUI::PROP_PANEL_OK);
283   connect(aOkBtn, SIGNAL(clicked()), theOperation, SLOT(commit()));
284   QPushButton* aCancelBtn = myPropertyPanelDock->findChild<QPushButton*>(XGUI::PROP_PANEL_CANCEL);
285   connect(aCancelBtn, SIGNAL(clicked()), theOperation, SLOT(abort()));
286
287   QAction* aCommand = 0;
288   if (isSalomeMode()) {
289     aCommand = salomeConnector()->command(theOperation->operationId());
290   } else {
291     XGUI_MainMenu* aMenu = myMainWindow->menuObject();
292     aCommand = aMenu->feature(theOperation->operationId());
293   }
294   //Abort operation on uncheck the command
295   connect(aCommand, SIGNAL(toggled(bool)), theOperation, SLOT(setRunning(bool)));
296 }
297
298 //******************************************************
299 void XGUI_Workshop::onExit()
300 {
301   qApp->exit();
302 }
303
304 //******************************************************
305 void XGUI_Workshop::onNew()
306 {
307   QApplication::setOverrideCursor(Qt::WaitCursor);
308   if (objectBrowser() == 0) {
309     createDockWidgets();
310     mySelector->connectObjectBrowser(objectBrowser());
311   }
312   showObjectBrowser();
313   if (!isSalomeMode()) {
314     myMainWindow->showPythonConsole();
315     QMdiSubWindow* aWnd = myMainWindow->viewer()->createView();
316     aWnd->showMaximized();
317     updateCommandStatus();
318   }
319   QApplication::restoreOverrideCursor();
320 }
321
322 //******************************************************
323 void XGUI_Workshop::onOpen()
324 {
325   //QString aFileName = QFileDialog::getOpenFileName(mainWindow());
326   updateCommandStatus();
327 }
328
329 //******************************************************
330 void XGUI_Workshop::onSave()
331 {
332   updateCommandStatus();
333 }
334
335 //******************************************************
336 void XGUI_Workshop::onSaveAs()
337 {
338   //QString aFileName = QFileDialog::getSaveFileName(mainWindow());
339   updateCommandStatus();
340 }
341
342 //******************************************************
343 void XGUI_Workshop::onUndo()
344 {
345   objectBrowser()->setCurrentIndex(QModelIndex());
346   boost::shared_ptr<ModelAPI_PluginManager> aMgr = ModelAPI_PluginManager::get();
347   boost::shared_ptr<ModelAPI_Document> aDoc = aMgr->rootDocument();
348   aDoc->undo();
349   updateCommandStatus();
350 }
351
352 //******************************************************
353 void XGUI_Workshop::onRedo()
354 {
355   objectBrowser()->setCurrentIndex(QModelIndex());
356   boost::shared_ptr<ModelAPI_PluginManager> aMgr = ModelAPI_PluginManager::get();
357   boost::shared_ptr<ModelAPI_Document> aDoc = aMgr->rootDocument();
358   aDoc->redo();
359   updateCommandStatus();
360 }
361
362 //******************************************************
363 XGUI_Module* XGUI_Workshop::loadModule(const QString& theModule)
364 {
365   QString libName = library(theModule);
366   if (libName.isEmpty()) {
367     qWarning(
368     qPrintable( tr( "Information about module \"%1\" doesn't exist." ).arg( theModule ) ));
369     return 0;
370   }
371
372   QString err;
373   CREATE_FUNC crtInst = 0;
374
375 #ifdef WIN32
376
377   HINSTANCE modLib = ::LoadLibrary((LPTSTR) qPrintable(libName));
378   if (!modLib) {
379     LPVOID lpMsgBuf;
380     ::FormatMessage(
381         FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
382         0, ::GetLastError(), 0, (LPTSTR) & lpMsgBuf, 0, 0);
383     QString aMsg((char*) &lpMsgBuf);
384     err = QString("Failed to load  %1. %2").arg(libName).arg(aMsg);
385     ::LocalFree(lpMsgBuf);
386   } else {
387     crtInst = (CREATE_FUNC) ::GetProcAddress(modLib, CREATE_MODULE);
388     if (!crtInst) {
389       LPVOID lpMsgBuf;
390       ::FormatMessage(
391           FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM
392               | FORMAT_MESSAGE_IGNORE_INSERTS,
393           0, ::GetLastError(), 0, (LPTSTR) & lpMsgBuf, 0, 0);
394       QString aMsg((char*) &lpMsgBuf);
395       err = QString("Failed to find  %1 function. %2").arg( CREATE_MODULE).arg(aMsg);
396       ::LocalFree(lpMsgBuf);
397     }
398   }
399 #else
400   void* modLib = dlopen( libName.toLatin1(), RTLD_LAZY );
401   if ( !modLib )
402   err = QString( "Can not load library %1. %2" ).arg( libName ).arg( dlerror() );
403   else
404   {
405     crtInst = (CREATE_FUNC)dlsym( modLib, CREATE_MODULE );
406     if ( !crtInst )
407     err = QString( "Failed to find function %1. %2" ).arg( CREATE_MODULE ).arg( dlerror() );
408   }
409 #endif
410
411   XGUI_Module* aModule = crtInst ? crtInst(this) : 0;
412
413   if (!err.isEmpty()) {
414     if (mainWindow() && mainWindow()->isVisible())
415       QMessageBox::warning(mainWindow(), tr("Error"), err);
416     else
417       qWarning( qPrintable( err ));
418   }
419   return aModule;
420 }
421
422 //******************************************************
423 bool XGUI_Workshop::activateModule()
424 {
425   myPartSetModule = loadModule("PartSet");
426   if (!myPartSetModule)
427     return false;
428   myPartSetModule->createFeatures();
429   return true;
430 }
431
432 //******************************************************
433 void XGUI_Workshop::updateCommandStatus()
434 {
435   if (isSalomeMode()) // TODO: update commands in SALOME
436     return;
437   XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
438
439   QList<XGUI_Command*> aCommands = aMenuBar->features();
440   QList<XGUI_Command*>::const_iterator aIt;
441
442   boost::shared_ptr<ModelAPI_PluginManager> aMgr = ModelAPI_PluginManager::get();
443   if (aMgr->hasRootDocument()) {
444     XGUI_Command* aUndoCmd;
445     XGUI_Command* aRedoCmd;
446     for (aIt = aCommands.constBegin(); aIt != aCommands.constEnd(); ++aIt) {
447       if ((*aIt)->id() == "UNDO_CMD")
448         aUndoCmd = (*aIt);
449       else if ((*aIt)->id() == "REDO_CMD")
450         aRedoCmd = (*aIt);
451       else // Enable all commands
452         (*aIt)->enable();
453     }
454     boost::shared_ptr<ModelAPI_Document> aDoc = aMgr->rootDocument();
455     aUndoCmd->setEnabled(aDoc->canUndo());
456     aRedoCmd->setEnabled(aDoc->canRedo());
457   } else {
458     for (aIt = aCommands.constBegin(); aIt != aCommands.constEnd(); ++aIt) {
459       if ((*aIt)->id() == "NEW_CMD")
460         (*aIt)->enable();
461       else if ((*aIt)->id() == "EXIT_CMD")
462         (*aIt)->enable();
463       else 
464         (*aIt)->disable();
465     }
466   }
467 }
468
469 //******************************************************
470 QDockWidget* XGUI_Workshop::createObjectBrowser(QWidget* theParent)
471 {
472   QDockWidget* aObjDock = new QDockWidget(theParent);
473   aObjDock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
474   aObjDock->setWindowTitle(tr("Object browser"));
475   myObjectBrowser = new XGUI_ObjectsBrowser(aObjDock);
476   aObjDock->setWidget(myObjectBrowser);
477   return aObjDock;
478 }
479
480 //******************************************************
481 QDockWidget* XGUI_Workshop::createPropertyPanel(QWidget* theParent)
482 {
483   QDockWidget* aPropPanel = new QDockWidget(theParent);
484   aPropPanel->setWindowTitle(tr("Property Panel"));
485   QAction* aViewAct = aPropPanel->toggleViewAction();
486   aPropPanel->setObjectName(XGUI::PROP_PANEL);
487
488   QWidget* aContent = new QWidget(aPropPanel);
489   QVBoxLayout* aMainLay = new QVBoxLayout(aContent);
490   aMainLay->setContentsMargins(3, 3, 3, 3);
491   aPropPanel->setWidget(aContent);
492
493   QFrame* aFrm = new QFrame(aContent);
494   aFrm->setFrameStyle(QFrame::Sunken);
495   aFrm->setFrameShape(QFrame::Panel);
496   QHBoxLayout* aBtnLay = new QHBoxLayout(aFrm);
497   aBtnLay->setContentsMargins(0, 0, 0, 0);
498   aMainLay->addWidget(aFrm);
499
500   QPushButton* aBtn = new QPushButton(QIcon(":pictures/button_help.png"), "", aFrm);
501   aBtn->setFlat(true);
502   aBtnLay->addWidget(aBtn);
503   aBtnLay->addStretch(1);
504   aBtn = new QPushButton(QIcon(":pictures/button_ok.png"), "", aFrm);
505   aBtn->setObjectName(XGUI::PROP_PANEL_OK);
506   aBtn->setFlat(true);
507   aBtnLay->addWidget(aBtn);
508   aBtn = new QPushButton(QIcon(":pictures/button_cancel.png"), "", aFrm);
509   aBtn->setObjectName(XGUI::PROP_PANEL_CANCEL);
510   aBtn->setFlat(true);
511   aBtnLay->addWidget(aBtn);
512
513   QWidget* aCustomWidget = new QWidget(aContent);
514   aCustomWidget->setObjectName(XGUI::PROP_PANEL_WDG);
515   aMainLay->addWidget(aCustomWidget);
516   aMainLay->addStretch(1);
517
518   return aPropPanel;
519 }
520
521 //******************************************************
522 void XGUI_Workshop::setPropertyPannelTitle(const QString& theTitle)
523 {
524   myPropertyPanelDock->setWindowTitle(theTitle);
525 }
526
527 //******************************************************
528 /*
529  * Creates dock widgets, places them in corresponding area
530  * and tabifies if necessary.
531  */
532 void XGUI_Workshop::createDockWidgets()
533 {
534   QMainWindow* aDesktop = isSalomeMode()? salomeConnector()->desktop() :
535                                           myMainWindow;
536   QDockWidget* aObjDock = createObjectBrowser(aDesktop);
537   aDesktop->addDockWidget(Qt::LeftDockWidgetArea, aObjDock);
538   myPropertyPanelDock = createPropertyPanel(aDesktop);
539   aDesktop->addDockWidget(Qt::LeftDockWidgetArea, myPropertyPanelDock);
540   hidePropertyPanel(); //<! Invisible by default
541   hideObjectBrowser();
542   aDesktop->tabifyDockWidget(aObjDock, myPropertyPanelDock);
543 }
544
545 //******************************************************
546 void XGUI_Workshop::showPropertyPanel()
547 {
548   QAction* aViewAct = myPropertyPanelDock->toggleViewAction();
549   //<! Restore ability to close panel from the window's menu
550   aViewAct->setEnabled(true);
551   myPropertyPanelDock->show();
552   myPropertyPanelDock->raise();
553 }
554
555 //******************************************************
556 void XGUI_Workshop::hidePropertyPanel()
557 {
558   QAction* aViewAct = myPropertyPanelDock->toggleViewAction();
559   //<! Do not allow to show empty property panel
560   aViewAct->setEnabled(false);
561   myPropertyPanelDock->hide();
562 }
563
564 //******************************************************
565 void XGUI_Workshop::showObjectBrowser()
566 {
567   myObjectBrowser->parentWidget()->show();
568 }
569
570 //******************************************************
571 void XGUI_Workshop::hideObjectBrowser()
572 {
573   myObjectBrowser->parentWidget()->hide();
574 }
575
576 //******************************************************
577 void XGUI_Workshop::onFeatureTriggered()
578 {
579   QAction* aCmd = dynamic_cast<QAction*>(sender());
580   if (aCmd) {
581     QString aId = salomeConnector()->commandId(aCmd);
582     if (!aId.isNull())
583       myPartSetModule->launchOperation(aId);
584   }
585 }