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