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