Salome HOME
58d5a87c1f5fdb0a9c14a76095575fd0e6f2a00c
[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   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_Operation* aOperation = (ModuleBase_Operation*)(aPartSetMsg->pointer());
151     setCurrentOperation(aOperation);
152     if(aOperation->xmlRepresentation().isEmpty()) { //!< No need for property panel
153       myCurrentOperation->start();
154       myCurrentOperation->commit();
155       updateCommandStatus();
156     } else {
157       fillPropertyPanel(aOperation);
158     }
159     return;
160   }
161
162 #ifdef _DEBUG
163   qDebug() << "XGUI_Workshop::ProcessEvent: "
164   << "Catch message, but it can not be processed.";
165 #endif
166
167 }
168
169 /*
170  *
171  */
172 void XGUI_Workshop::addFeature(const Config_FeatureMessage* theMessage)
173 {
174   if (!theMessage) {
175 #ifdef _DEBUG
176     qDebug() << "XGUI_Workshop::addFeature: NULL message.";
177 #endif
178     return;
179   }
180   //Find or create Workbench
181   XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
182   QString aWchName = QString::fromStdString(theMessage->workbenchId());
183   XGUI_Workbench* aPage = aMenuBar->findWorkbench(aWchName);
184   if (!aPage) {
185     aPage = addWorkbench(aWchName);
186   }
187   //Find or create Group
188   QString aGroupName = QString::fromStdString(theMessage->groupId());
189   XGUI_MenuGroupPanel* aGroup = aPage->findGroup(aGroupName);
190   if (!aGroup) {
191     aGroup = aPage->addGroup(aGroupName);
192   }
193   //Create feature...
194   QString aFeatureId = QString::fromStdString(theMessage->id());
195   XGUI_Command* aCommand = aGroup->addFeature(QString::fromStdString(theMessage->id()),
196                                               QString::fromStdString(theMessage->text()),
197                                               QString::fromStdString(theMessage->tooltip()),
198                                               QIcon(theMessage->icon().c_str())
199                                               //TODO(sbh): QKeySequence
200                                                   );
201   myPartSetModule->featureCreated(aCommand);
202 }
203
204 /*
205  *
206  */
207 void XGUI_Workshop::fillPropertyPanel(ModuleBase_Operation* theOperation)
208 {
209   connectToPropertyPanel(theOperation);
210   QWidget* aPropWidget = myMainWindow->findChild<QWidget*>(XGUI::PROP_PANEL_WDG);
211   qDeleteAll(aPropWidget->children());
212   theOperation->start();
213   XGUI_WidgetFactory aFactory = XGUI_WidgetFactory(theOperation);
214   aFactory.createWidget(aPropWidget);
215 }
216
217 void XGUI_Workshop::setCurrentOperation(ModuleBase_Operation* theOperation)
218 {
219   //FIXME: Ask user about aborting of current operation?
220   if (myCurrentOperation) {
221     //TODO get isOperation from document
222     if (myCurrentOperation->isRunning())
223       myCurrentOperation->abort();
224
225     myCurrentOperation->deleteLater();
226   }
227   myCurrentOperation = theOperation;
228 }
229
230 /*
231  * Makes a signal/slot connections between Property Panel
232  * and given operation. The given operation becomes a
233  * current operation and previous operation if exists
234  */
235 void XGUI_Workshop::connectToPropertyPanel(ModuleBase_Operation* theOperation)
236 {
237   QDockWidget* aPanel = myMainWindow->findChild<QDockWidget*>(XGUI::PROP_PANEL);
238   QPushButton* aOkBtn = aPanel->findChild<QPushButton*>(XGUI::PROP_PANEL_OK);
239   connect(aOkBtn, SIGNAL(clicked()), theOperation, SLOT(commit()));
240   QPushButton* aCancelBtn = aPanel->findChild<QPushButton*>(XGUI::PROP_PANEL_CANCEL);
241   connect(aCancelBtn, SIGNAL(clicked()), theOperation, SLOT(abort()));
242
243   connect(theOperation, SIGNAL(started()), myMainWindow, SLOT(showPropertyPanel()));
244   connect(theOperation, SIGNAL(stopped()), myMainWindow, SLOT(hidePropertyPanel()));
245   connect(theOperation, SIGNAL(stopped()), this, SLOT(updateCommandStatus()));
246 }
247
248 //******************************************************
249 void XGUI_Workshop::onExit()
250 {
251   qApp->exit();
252 }
253
254 //******************************************************
255 void XGUI_Workshop::onNew()
256 {
257   QApplication::setOverrideCursor(Qt::WaitCursor);
258   if (myMainWindow->objectBrowser() == 0) {
259     myMainWindow->createDockWidgets();
260     mySelector->connectObjectBrowser(myMainWindow->objectBrowser());
261   }
262   myMainWindow->showObjectBrowser();
263   myMainWindow->showPythonConsole();
264   QMdiSubWindow* aWnd = myMainWindow->viewer()->createView();
265   aWnd->showMaximized();
266   updateCommandStatus();
267   QApplication::restoreOverrideCursor();
268 }
269
270 //******************************************************
271 void XGUI_Workshop::onOpen()
272 {
273   //QString aFileName = QFileDialog::getOpenFileName(mainWindow());
274   updateCommandStatus();
275 }
276
277 //******************************************************
278 void XGUI_Workshop::onSave()
279 {
280   updateCommandStatus();
281 }
282
283 //******************************************************
284 void XGUI_Workshop::onSaveAs()
285 {
286   //QString aFileName = QFileDialog::getSaveFileName(mainWindow());
287   updateCommandStatus();
288 }
289
290 //******************************************************
291 void XGUI_Workshop::onUndo()
292 {
293   myMainWindow->objectBrowser()->setCurrentIndex(QModelIndex());
294   std::shared_ptr<ModelAPI_PluginManager> aMgr = ModelAPI_PluginManager::get();
295   std::shared_ptr<ModelAPI_Document> aDoc = aMgr->rootDocument();
296   aDoc->undo();
297   updateCommandStatus();
298 }
299
300 //******************************************************
301 void XGUI_Workshop::onRedo()
302 {
303   myMainWindow->objectBrowser()->setCurrentIndex(QModelIndex());
304   std::shared_ptr<ModelAPI_PluginManager> aMgr = ModelAPI_PluginManager::get();
305   std::shared_ptr<ModelAPI_Document> aDoc = aMgr->rootDocument();
306   aDoc->redo();
307   updateCommandStatus();
308 }
309
310
311 //******************************************************
312 XGUI_Module* XGUI_Workshop::loadModule(const QString& theModule)
313 {
314   QString libName = library(theModule);
315   if (libName.isEmpty()) {
316     qWarning(
317     qPrintable( tr( "Information about module \"%1\" doesn't exist." ).arg( theModule ) ));
318     return 0;
319   }
320
321   QString err;
322   CREATE_FUNC crtInst = 0;
323
324 #ifdef WIN32
325
326   HINSTANCE modLib = ::LoadLibrary((LPTSTR) qPrintable(libName));
327   if (!modLib) {
328     LPVOID lpMsgBuf;
329     ::FormatMessage(
330         FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
331         0, ::GetLastError(), 0, (LPTSTR) & lpMsgBuf, 0, 0);
332     QString aMsg((char*) &lpMsgBuf);
333     err = QString("Failed to load  %1. %2").arg(libName).arg(aMsg);
334     ::LocalFree(lpMsgBuf);
335   } else {
336     crtInst = (CREATE_FUNC) ::GetProcAddress(modLib, CREATE_MODULE);
337     if (!crtInst) {
338       LPVOID lpMsgBuf;
339       ::FormatMessage(
340           FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM
341               | FORMAT_MESSAGE_IGNORE_INSERTS,
342           0, ::GetLastError(), 0, (LPTSTR) & lpMsgBuf, 0, 0);
343       QString aMsg((char*) &lpMsgBuf);
344       err = QString("Failed to find  %1 function. %2").arg( CREATE_MODULE).arg(aMsg);
345       ::LocalFree(lpMsgBuf);
346     }
347   }
348 #else
349   void* modLib = dlopen( libName.toLatin1(), RTLD_LAZY );
350   if ( !modLib )
351   err = QString( "Can not load library %1. %2" ).arg( libName ).arg( dlerror() );
352   else
353   {
354     crtInst = (CREATE_FUNC)dlsym( modLib, CREATE_MODULE );
355     if ( !crtInst )
356     err = QString( "Failed to find function %1. %2" ).arg( CREATE_MODULE ).arg( dlerror() );
357   }
358 #endif
359
360   XGUI_Module* aModule = crtInst ? crtInst(this) : 0;
361
362   if (!err.isEmpty()) {
363     if (mainWindow() && mainWindow()->isVisible())
364       QMessageBox::warning(mainWindow(), tr("Error"), err);
365     else
366       qWarning( qPrintable( err ));
367   }
368   return aModule;
369 }
370
371 //******************************************************
372 bool XGUI_Workshop::activateModule()
373 {
374   myPartSetModule = loadModule("PartSet");
375   if (!myPartSetModule)
376     return false;
377   myPartSetModule->createFeatures();
378   return true;
379 }
380
381 //******************************************************
382 void XGUI_Workshop::updateCommandStatus()
383 {
384   XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
385
386   QList<XGUI_Command*> aCommands = aMenuBar->features();
387   QList<XGUI_Command*>::const_iterator aIt;
388
389   std::shared_ptr<ModelAPI_PluginManager> aMgr = ModelAPI_PluginManager::get();
390   if (aMgr->hasRootDocument()) {
391     XGUI_Command* aUndoCmd;
392     XGUI_Command* aRedoCmd;
393     for (aIt = aCommands.constBegin(); aIt != aCommands.constEnd(); ++aIt) {
394       if ((*aIt)->id() == "UNDO_CMD")
395         aUndoCmd = (*aIt);
396       else if ((*aIt)->id() == "REDO_CMD")
397         aRedoCmd = (*aIt);
398       else // Enable all commands
399         (*aIt)->enable();
400     }
401     std::shared_ptr<ModelAPI_Document> aDoc = aMgr->rootDocument();
402     aUndoCmd->setEnabled(aDoc->canUndo());
403     aRedoCmd->setEnabled(aDoc->canRedo());
404   } else {
405     for (aIt = aCommands.constBegin(); aIt != aCommands.constEnd(); ++aIt) {
406       if ((*aIt)->id() == "NEW_CMD")
407         (*aIt)->enable();
408       else if ((*aIt)->id() == "EXIT_CMD")
409         (*aIt)->enable();
410       else 
411         (*aIt)->disable();
412     }
413   }
414 }