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