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