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