Salome HOME
eb4552d96161ab6d21ae48bc83026b93c19dc00d
[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
17 #include <ModelAPI_PluginManager.h>
18 #include <ModelAPI_Feature.h>
19 #include <ModelAPI_Data.h>
20 #include <ModelAPI_AttributeDocRef.h>
21
22 #include <Events_Loop.h>
23 #include <ModuleBase_PropPanelOperation.h>
24 #include <ModuleBase_Operation.h>
25 #include <Config_FeatureMessage.h>
26 #include <Config_PointerMessage.h>
27
28 #include <QApplication>
29 #include <QFileDialog>
30 #include <QMessageBox>
31 #include <QMdiSubWindow>
32 #include <QPushButton>
33 #include <QDockWidget>
34
35 #ifdef _DEBUG
36 #include <QDebug>
37 #endif
38
39 #ifdef WIN32
40 #include <windows.h>
41 #else
42 #include <dlfcn.h>
43 #endif
44
45 XGUI_Workshop::XGUI_Workshop()
46   : QObject(), 
47   myPartSetModule(NULL)
48 {
49   myMainWindow = new XGUI_MainWindow();
50   mySelector = new XGUI_SelectionMgr(this);
51   myDisplayer = new XGUI_Displayer(myMainWindow->viewer());
52   myOperationMgr = new XGUI_OperationMgr(this);
53   connect(myOperationMgr, SIGNAL(beforeOperationStart()), this, SLOT(onBeforeOperationStart()));
54   connect(myOperationMgr, SIGNAL(afterOperationStart()),  this, SLOT(onAfterOperationStart()));
55 }
56
57 //******************************************************
58 XGUI_Workshop::~XGUI_Workshop(void)
59 {
60 }
61
62 //******************************************************
63 void XGUI_Workshop::startApplication()
64 {
65   initMenu();
66   //Initialize event listening
67   Events_Loop* aLoop = Events_Loop::loop();
68   //TODO(sbh): Implement static method to extract event id [SEID]
69   Events_ID aFeatureId = aLoop->eventByName("FeatureEvent");
70   aLoop->registerListener(this, aFeatureId);
71   Events_ID aPartSetId = aLoop->eventByName("PartSetModuleEvent");
72   aLoop->registerListener(this, aPartSetId);
73   activateModule();
74   myMainWindow->show();
75
76   updateCommandStatus();
77   onNew();
78   // Testing of document creation
79   //boost::shared_ptr<ModelAPI_PluginManager> aMgr = ModelAPI_PluginManager::get();
80   //boost::shared_ptr<ModelAPI_Feature> aPoint1 = aMgr->rootDocument()->addFeature("Point");
81   //boost::shared_ptr<ModelAPI_Feature> aPart = aMgr->rootDocument()->addFeature("Part");
82   //aPart->execute();
83   //aMgr->setCurrentDocument(aPart->data()->docRef("PartDocument")->value());
84   //boost::shared_ptr<ModelAPI_Feature> aPoint2 = aMgr->rootDocument()->addFeature("Point");
85   //aPoint2 = aMgr->rootDocument()->addFeature("Point");
86
87   //aPart = aMgr->rootDocument()->addFeature("Part");
88   //aPart->execute();
89 }
90
91 //******************************************************
92 void XGUI_Workshop::initMenu()
93 {
94   XGUI_Workbench* aPage = myMainWindow->menuObject()->generalPage();
95
96   // File commands group
97   XGUI_MenuGroupPanel* aGroup = aPage->addGroup("Default");
98
99   XGUI_Command* aCommand;
100
101   aCommand = aGroup->addFeature("SAVE_CMD", tr("Save..."), tr("Save the document"),
102                                 QIcon(":pictures/save.png"), QKeySequence::Save);
103   aCommand->connectTo(this, SLOT(onSave()));
104   //aCommand->disable();
105
106   aCommand = aGroup->addFeature("UNDO_CMD", tr("Undo"), tr("Undo last command"),
107                                 QIcon(":pictures/undo.png"), QKeySequence::Undo);
108   aCommand->connectTo(this, SLOT(onUndo()));
109
110   aCommand = aGroup->addFeature("REDO_CMD", tr("Redo"), tr("Redo last command"),
111                                 QIcon(":pictures/redo.png"), QKeySequence::Redo);
112   aCommand->connectTo(this, SLOT(onRedo()));
113
114   aCommand = aGroup->addFeature("REBUILD_CMD", tr("Rebuild"), tr("Rebuild data objects"),
115                                 QIcon(":pictures/rebuild.png"));
116
117   aCommand = aGroup->addFeature("SAVEAS_CMD", tr("Save as..."), tr("Save the document into a file"),
118                                 QIcon(":pictures/save.png"));
119   aCommand->connectTo(this, SLOT(onSaveAs()));
120   //aCommand->disable();
121
122   aCommand = aGroup->addFeature("OPEN_CMD", tr("Open..."), tr("Open a new document"),
123                                 QIcon(":pictures/open.png"), QKeySequence::Open);
124   aCommand->connectTo(this, SLOT(onOpen()));
125
126   //aCommand = aGroup->addFeature("NEW_CMD", tr("New"), tr("Create a new document"),
127   //                              QIcon(":pictures/new.png"), QKeySequence::New);
128   //aCommand->connectTo(this, SLOT(onNew()));
129
130   aCommand = aGroup->addFeature("EXIT_CMD", tr("Exit"), tr("Exit application"),
131                                 QIcon(":pictures/close.png"), QKeySequence::Close);
132   aCommand->connectTo(this, SLOT(onExit()));
133
134 }
135
136 //******************************************************
137 XGUI_Workbench* XGUI_Workshop::addWorkbench(const QString& theName)
138 {
139   XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
140   return aMenuBar->addWorkbench(theName);
141 }
142
143 //******************************************************
144 void XGUI_Workshop::processEvent(const Events_Message* theMessage)
145 {
146   static Events_ID aFeatureId = Events_Loop::loop()->eventByName("FeatureEvent");
147   if (theMessage->eventID() == aFeatureId) {
148     const Config_FeatureMessage* aFeatureMsg =
149         dynamic_cast<const Config_FeatureMessage*>(theMessage);
150     addFeature(aFeatureMsg);
151     return;
152   }
153   const Config_PointerMessage* aPartSetMsg =
154       dynamic_cast<const Config_PointerMessage*>(theMessage);
155   if (aPartSetMsg) {
156     ModuleBase_PropPanelOperation* anOperation =
157         (ModuleBase_PropPanelOperation*)(aPartSetMsg->pointer());
158
159     if (myOperationMgr->startOperation(anOperation))
160     {
161       if (anOperation->isPerformedImmediately())
162       {
163         myOperationMgr->commitCurrentOperation();
164         updateCommandStatus();
165       }
166       /*if(anOperation->xmlRepresentation().isEmpty()) { //!< No need for property panel
167         //connectToPropertyPanel(anOperation);
168
169         anOperation->start();
170
171         if (anOperation->isPerformedImmediately()) {
172           anOperation->commit();
173           updateCommandStatus();
174         }
175       } else {
176         connectToPropertyPanel(anOperation);
177         QWidget* aPropWidget = myMainWindow->findChild<QWidget*>(XGUI::PROP_PANEL_WDG);
178         qDeleteAll(aPropWidget->children());
179
180         anOperation->start();
181         
182         XGUI_WidgetFactory aFactory = XGUI_WidgetFactory(anOperation);
183         aFactory.createWidget(aPropWidget);
184         myMainWindow->setPropertyPannelTitle(anOperation->description());
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 void XGUI_Workshop::onBeforeOperationStart()
198 {
199   ModuleBase_PropPanelOperation* aOperation =
200         (ModuleBase_PropPanelOperation*)(myOperationMgr->currentOperation());
201
202   if(aOperation->xmlRepresentation().isEmpty()) { //!< No need for property panel
203     //myPartSetModule->connectToPropertyPanel(aOperation);
204   } else {
205     connectWithOperation(aOperation);
206     QWidget* aPropWidget = myMainWindow->findChild<QWidget*>(XGUI::PROP_PANEL_WDG);
207     qDeleteAll(aPropWidget->children());
208   }
209 }
210
211 void XGUI_Workshop::onAfterOperationStart()
212 {
213   ModuleBase_PropPanelOperation* aOperation =
214         (ModuleBase_PropPanelOperation*)(myOperationMgr->currentOperation());
215
216   if(aOperation->xmlRepresentation().isEmpty()) { //!< No need for property panel
217   } else {
218     XGUI_WidgetFactory aFactory = XGUI_WidgetFactory(aOperation);
219     QWidget* aPropWidget = myMainWindow->findChild<QWidget*>(XGUI::PROP_PANEL_WDG);
220     aFactory.createWidget(aPropWidget);
221     myMainWindow->setPropertyPannelTitle(aOperation->description());
222   }
223 }
224
225 /*
226  *
227  */
228 void XGUI_Workshop::addFeature(const Config_FeatureMessage* theMessage)
229 {
230   if (!theMessage) {
231 #ifdef _DEBUG
232     qDebug() << "XGUI_Workshop::addFeature: NULL message.";
233 #endif
234     return;
235   }
236   //Find or create Workbench
237   XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
238   QString aWchName = QString::fromStdString(theMessage->workbenchId());
239   XGUI_Workbench* aPage = aMenuBar->findWorkbench(aWchName);
240   if (!aPage) {
241     aPage = addWorkbench(aWchName);
242   }
243   //Find or create Group
244   QString aGroupName = QString::fromStdString(theMessage->groupId());
245   XGUI_MenuGroupPanel* aGroup = aPage->findGroup(aGroupName);
246   if (!aGroup) {
247     aGroup = aPage->addGroup(aGroupName);
248   }
249   bool isUsePropPanel = theMessage->isUseInput();
250   //Create feature...
251   XGUI_Command* aCommand = aGroup->addFeature(QString::fromStdString(theMessage->id()),
252                                               QString::fromStdString(theMessage->text()),
253                                               QString::fromStdString(theMessage->tooltip()),
254                                               QIcon(theMessage->icon().c_str()),
255                                               QKeySequence(), isUsePropPanel);
256   
257   connect(aCommand,                   SIGNAL(toggled(bool)),
258           myMainWindow->menuObject(), SLOT(onFeatureChecked(bool)));
259   myPartSetModule->featureCreated(aCommand);
260 }
261
262 /*
263  *
264  */
265 /*void XGUI_Workshop::fillPropertyPanel(ModuleBase_PropPanelOperation* theOperation)
266 {
267   connectWithOperation(theOperation);
268   QWidget* aPropWidget = myMainWindow->findChild<QWidget*>(XGUI::PROP_PANEL_WDG);
269   qDeleteAll(aPropWidget->children());
270   theOperation->start();
271   XGUI_WidgetFactory aFactory = XGUI_WidgetFactory(theOperation);
272   aFactory.createWidget(aPropWidget);
273   myMainWindow->setPropertyPannelTitle(theOperation->description());
274 }*/
275
276 /*
277  * Makes a signal/slot connections between Property Panel
278  * and given operation. The given operation becomes a
279  * current operation and previous operation if exists
280  */
281 void XGUI_Workshop::connectWithOperation(ModuleBase_Operation* theOperation)
282 {
283   QDockWidget* aPanel = myMainWindow->findChild<QDockWidget*>(XGUI::PROP_PANEL);
284   QPushButton* aOkBtn = aPanel->findChild<QPushButton*>(XGUI::PROP_PANEL_OK);
285   connect(aOkBtn, SIGNAL(clicked()), theOperation, SLOT(commit()));
286   QPushButton* aCancelBtn = aPanel->findChild<QPushButton*>(XGUI::PROP_PANEL_CANCEL);
287   connect(aCancelBtn, SIGNAL(clicked()), theOperation, SLOT(abort()));
288
289   connect(theOperation, SIGNAL(started()), myMainWindow, SLOT(showPropertyPanel()));
290   connect(theOperation, SIGNAL(stopped()), myMainWindow, SLOT(hidePropertyPanel()));
291   connect(theOperation, SIGNAL(stopped()), this, SLOT(updateCommandStatus()));
292
293   XGUI_MainMenu* aMenu = myMainWindow->menuObject();
294   connect(theOperation, SIGNAL(stopped()), aMenu, SLOT(restoreCommandState()));
295
296   XGUI_Command* aCommand = aMenu->feature(theOperation->operationId());
297   //Abort operation on uncheck the command
298   connect(aCommand, SIGNAL(toggled(bool)), theOperation, SLOT(setRunning(bool)));
299
300 }
301
302 //******************************************************
303 void XGUI_Workshop::onExit()
304 {
305   qApp->exit();
306 }
307
308 //******************************************************
309 void XGUI_Workshop::onNew()
310 {
311   QApplication::setOverrideCursor(Qt::WaitCursor);
312   if (myMainWindow->objectBrowser() == 0) {
313     myMainWindow->createDockWidgets();
314     mySelector->connectObjectBrowser(myMainWindow->objectBrowser());
315   }
316   myMainWindow->showObjectBrowser();
317   myMainWindow->showPythonConsole();
318   QMdiSubWindow* aWnd = myMainWindow->viewer()->createView();
319   aWnd->showMaximized();
320   updateCommandStatus();
321   QApplication::restoreOverrideCursor();
322 }
323
324 //******************************************************
325 void XGUI_Workshop::onOpen()
326 {
327   //QString aFileName = QFileDialog::getOpenFileName(mainWindow());
328   updateCommandStatus();
329 }
330
331 //******************************************************
332 void XGUI_Workshop::onSave()
333 {
334   updateCommandStatus();
335 }
336
337 //******************************************************
338 void XGUI_Workshop::onSaveAs()
339 {
340   //QString aFileName = QFileDialog::getSaveFileName(mainWindow());
341   updateCommandStatus();
342 }
343
344 //******************************************************
345 void XGUI_Workshop::onUndo()
346 {
347   myMainWindow->objectBrowser()->setCurrentIndex(QModelIndex());
348   boost::shared_ptr<ModelAPI_PluginManager> aMgr = ModelAPI_PluginManager::get();
349   boost::shared_ptr<ModelAPI_Document> aDoc = aMgr->rootDocument();
350   aDoc->undo();
351   updateCommandStatus();
352 }
353
354 //******************************************************
355 void XGUI_Workshop::onRedo()
356 {
357   myMainWindow->objectBrowser()->setCurrentIndex(QModelIndex());
358   boost::shared_ptr<ModelAPI_PluginManager> aMgr = ModelAPI_PluginManager::get();
359   boost::shared_ptr<ModelAPI_Document> aDoc = aMgr->rootDocument();
360   aDoc->redo();
361   updateCommandStatus();
362 }
363
364 //******************************************************
365 XGUI_Module* XGUI_Workshop::loadModule(const QString& theModule)
366 {
367   QString libName = library(theModule);
368   if (libName.isEmpty()) {
369     qWarning(
370     qPrintable( tr( "Information about module \"%1\" doesn't exist." ).arg( theModule ) ));
371     return 0;
372   }
373
374   QString err;
375   CREATE_FUNC crtInst = 0;
376
377 #ifdef WIN32
378
379   HINSTANCE modLib = ::LoadLibrary((LPTSTR) qPrintable(libName));
380   if (!modLib) {
381     LPVOID lpMsgBuf;
382     ::FormatMessage(
383         FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
384         0, ::GetLastError(), 0, (LPTSTR) & lpMsgBuf, 0, 0);
385     QString aMsg((char*) &lpMsgBuf);
386     err = QString("Failed to load  %1. %2").arg(libName).arg(aMsg);
387     ::LocalFree(lpMsgBuf);
388   } else {
389     crtInst = (CREATE_FUNC) ::GetProcAddress(modLib, CREATE_MODULE);
390     if (!crtInst) {
391       LPVOID lpMsgBuf;
392       ::FormatMessage(
393           FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM
394               | FORMAT_MESSAGE_IGNORE_INSERTS,
395           0, ::GetLastError(), 0, (LPTSTR) & lpMsgBuf, 0, 0);
396       QString aMsg((char*) &lpMsgBuf);
397       err = QString("Failed to find  %1 function. %2").arg( CREATE_MODULE).arg(aMsg);
398       ::LocalFree(lpMsgBuf);
399     }
400   }
401 #else
402   void* modLib = dlopen( libName.toLatin1(), RTLD_LAZY );
403   if ( !modLib )
404   err = QString( "Can not load library %1. %2" ).arg( libName ).arg( dlerror() );
405   else
406   {
407     crtInst = (CREATE_FUNC)dlsym( modLib, CREATE_MODULE );
408     if ( !crtInst )
409     err = QString( "Failed to find function %1. %2" ).arg( CREATE_MODULE ).arg( dlerror() );
410   }
411 #endif
412
413   XGUI_Module* aModule = crtInst ? crtInst(this) : 0;
414
415   if (!err.isEmpty()) {
416     if (mainWindow() && mainWindow()->isVisible())
417       QMessageBox::warning(mainWindow(), tr("Error"), err);
418     else
419       qWarning( qPrintable( err ));
420   }
421   return aModule;
422 }
423
424 //******************************************************
425 bool XGUI_Workshop::activateModule()
426 {
427   myPartSetModule = loadModule("PartSet");
428   if (!myPartSetModule)
429     return false;
430   myPartSetModule->createFeatures();
431   return true;
432 }
433
434 //******************************************************
435 void XGUI_Workshop::updateCommandStatus()
436 {
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 }