Salome HOME
55e16fcf87b21e2ba6806da2ed3e3930df466fe0
[modules/shaper.git] / src / XGUI / XGUI_Workshop.cpp
1 #include "XGUI_Module.h"
2 #include "XGUI_Command.h"
3 #include "XGUI_MainMenu.h"
4 #include "XGUI_MainWindow.h"
5 #include "XGUI_MenuGroupPanel.h"
6 #include "XGUI_Tools.h"
7 #include "XGUI_Workbench.h"
8 #include "XGUI_Workshop.h"
9 #include "XGUI_Viewer.h"
10 #include "XGUI_WidgetFactory.h"
11
12 #include <Event_Loop.h>
13 #include <Config_FeatureMessage.h>
14 #include <Config_WidgetMessage.h>
15
16 #include <QApplication>
17 #include <QFileDialog>
18 #include <QMessageBox>
19 #include <QMdiSubWindow>
20
21 #ifdef _DEBUG
22 #include <QDebug>
23 #endif
24
25 #ifdef WIN32
26 #include <windows.h>
27 #else
28 #include <dlfcn.h>
29 #endif
30
31 XGUI_Workshop::XGUI_Workshop()
32     : QObject()
33 {
34   myMainWindow = new XGUI_MainWindow();
35   myPartSetModule = NULL;
36 }
37
38 //******************************************************
39 XGUI_Workshop::~XGUI_Workshop(void)
40 {
41 }
42
43 //******************************************************
44 void XGUI_Workshop::startApplication()
45 {
46   initMenu();
47   //Initialize event listening
48   Event_Loop* aLoop = Event_Loop::loop();
49   //TODO(sbh): Implement static method to extract event id [SEID]
50   Event_ID aFeatureId = aLoop->eventByName("FeatureEvent");
51   aLoop->registerListener(this, aFeatureId);
52   Event_ID aPartSetId = aLoop->eventByName("PartSetModuleEvent");
53   aLoop->registerListener(this, aPartSetId);
54   activateModule();
55   myMainWindow->show();
56   QMdiSubWindow* aWnd = myMainWindow->viewer()->createView();
57   aWnd->showMaximized();
58   myMainWindow->showPythonConsole();
59 }
60
61 //******************************************************
62 void XGUI_Workshop::initMenu()
63 {
64   XGUI_Workbench* aPage = myMainWindow->menuObject()->generalPage();
65
66   // File commands group
67   XGUI_MenuGroupPanel* aGroup = aPage->addGroup("Default");
68
69   XGUI_Command* aCommand;
70
71   aCommand = aGroup->addFeature("SAVE_CMD", tr("SAVE_MENU"), tr("SAVE_MENU_TIP"),
72                                 QIcon(":pictures/save.png"), QKeySequence::Save);
73   aCommand->connectTo(this, SLOT(onSave()));
74   //aCommand->disable();
75
76   aCommand = aGroup->addFeature("UNDO_CMD", tr("UNDO_MENU"), tr("UNDO_MENU_TIP"),
77                                 QIcon(":pictures/undo.png"), QKeySequence::Undo);
78
79   aCommand = aGroup->addFeature("REDO_CMD", tr("REDO_MENU"), tr("REDO_MENU_TIP"),
80                                 QIcon(":pictures/redo.png"), QKeySequence::Redo);
81
82   aCommand = aGroup->addFeature("REBUILD_CMD", tr("REBUILD_MENU"), tr("REBUILD_MENU_TIP"),
83                                 QIcon(":pictures/rebuild.png"));
84
85   aCommand = aGroup->addFeature("SAVEAS_CMD", tr("SAVEAS_MENU"), tr("SAVEAS_MENU_TIP"),
86                                 QIcon(":pictures/save.png"));
87   aCommand->connectTo(this, SLOT(onSaveAs()));
88   //aCommand->disable();
89
90   aCommand = aGroup->addFeature("OPEN_CMD", tr("OPEN_MENU"), tr("OPEN_MENU_TIP"),
91                                 QIcon(":pictures/open.png"), QKeySequence::Open);
92   aCommand->connectTo(this, SLOT(onOpen()));
93
94   aCommand = aGroup->addFeature("NEW_CMD", tr("NEW_MENU"), tr("NEW_MENU_TIP"),
95                                 QIcon(":pictures/new.png"), QKeySequence::New);
96   aCommand->connectTo(this, SLOT(onNew()));
97
98   aCommand = aGroup->addFeature("EXIT_CMD", tr("EXIT_MENU"), tr("EXIT_MENU_TIP"),
99                                 QIcon(":pictures/close.png"), QKeySequence::Close);
100   aCommand->connectTo(this, SLOT(onExit()));
101
102 }
103
104 //******************************************************
105 XGUI_Workbench* XGUI_Workshop::addWorkbench(const QString& theName)
106 {
107   XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
108   return aMenuBar->addWorkbench(theName);
109 }
110
111 //******************************************************
112 void XGUI_Workshop::processEvent(const Event_Message* theMessage)
113 {
114   const Config_FeatureMessage* aFeatureMsg =
115       dynamic_cast<const Config_FeatureMessage*>(theMessage);
116   if (aFeatureMsg) {
117     addFeature(aFeatureMsg);
118     return;
119   }
120   const Config_WidgetMessage* aPartSetMsg = dynamic_cast<const Config_WidgetMessage*>(theMessage);
121   if (aPartSetMsg) {
122     fillPropertyPanel(aPartSetMsg);
123     return;
124   }
125
126 #ifdef _DEBUG
127   qDebug() << "XGUI_Workshop::ProcessEvent: "
128   << "Catch message, but it can not be processed.";
129 #endif
130
131 }
132
133 /*
134  *
135  */
136 void XGUI_Workshop::addFeature(const Config_FeatureMessage* theMessage)
137 {
138   if (!theMessage) {
139 #ifdef _DEBUG
140     qDebug() << "XGUI_Workshop::addFeature: NULL message.";
141 #endif
142     return;
143   }
144   //Find or create Workbench
145   XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
146   QString aWchName = QString::fromStdString(theMessage->workbenchId());
147   XGUI_Workbench* aPage = aMenuBar->findWorkbench(aWchName);
148   if (!aPage) {
149     aPage = addWorkbench(aWchName);
150   }
151   //Find or create Group
152   QString aGroupName = QString::fromStdString(theMessage->groupId());
153   XGUI_MenuGroupPanel* aGroup = aPage->findGroup(aGroupName);
154   if (!aGroup) {
155     aGroup = aPage->addGroup(aGroupName);
156   }
157   //Create feature...
158   QString aFeatureId = QString::fromStdString(theMessage->id());
159   XGUI_Command* aCommand = aGroup->addFeature(QString::fromStdString(theMessage->id()),
160                                               QString::fromStdString(theMessage->text()),
161                                               QString::fromStdString(theMessage->tooltip()),
162                                               QIcon(theMessage->icon().c_str())
163                                               //TODO(sbh): QKeySequence
164                                                   );
165   myPartSetModule->featureCreated(aCommand);
166 }
167
168 /*
169  *
170  */
171 void XGUI_Workshop::fillPropertyPanel(const Config_WidgetMessage* theMessage)
172 {
173   QWidget* aPropWidget = myMainWindow->findChild<QWidget*>("PropertyPanelWidget");
174   if (!aPropWidget) {
175     #ifdef _DEBUG
176     qDebug() << "XGUI_Workshop::fillPropertyPanel: " << "Unable to find property panel";
177     #endif
178     return;
179   }
180   qDeleteAll(aPropWidget->children());
181   std::string aXml = theMessage->xmlRepresentation();
182   XGUI_WidgetFactory aFactory = XGUI_WidgetFactory(aXml);
183   aFactory.fillWidget(aPropWidget);
184 }
185
186 //******************************************************
187 void XGUI_Workshop::onExit()
188 {
189   qApp->exit();
190 }
191
192 //******************************************************
193 void XGUI_Workshop::onNew()
194 {
195   myMainWindow->showObjectBrowser();
196 }
197
198 //******************************************************
199 void XGUI_Workshop::onOpen()
200 {
201   QString aFileName = QFileDialog::getOpenFileName(mainWindow());
202 }
203
204 //******************************************************
205 void XGUI_Workshop::onSave()
206 {
207 }
208
209 //******************************************************
210 void XGUI_Workshop::onSaveAs()
211 {
212   QString aFileName = QFileDialog::getSaveFileName(mainWindow());
213 }
214
215 //******************************************************
216 XGUI_Module* XGUI_Workshop::loadModule(const QString& theModule)
217 {
218   QString libName = library(theModule);
219   if (libName.isEmpty()) {
220     qWarning(
221     qPrintable( tr( "Information about module \"%1\" doesn't exist." ).arg( theModule ) ));
222     return 0;
223   }
224
225   QString err;
226   CREATE_FUNC crtInst = 0;
227
228 #ifdef WIN32
229
230   HINSTANCE modLib = ::LoadLibrary((LPTSTR) qPrintable(libName));
231   if (!modLib) {
232     LPVOID lpMsgBuf;
233     ::FormatMessage(
234         FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
235         0, ::GetLastError(), 0, (LPTSTR) & lpMsgBuf, 0, 0);
236     QString aMsg((char*) &lpMsgBuf);
237     err = QString("Failed to load  %1. %2").arg(libName).arg(aMsg);
238     ::LocalFree(lpMsgBuf);
239   } else {
240     crtInst = (CREATE_FUNC) ::GetProcAddress(modLib, CREATE_MODULE);
241     if (!crtInst) {
242       LPVOID lpMsgBuf;
243       ::FormatMessage(
244           FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM
245               | FORMAT_MESSAGE_IGNORE_INSERTS,
246           0, ::GetLastError(), 0, (LPTSTR) & lpMsgBuf, 0, 0);
247       QString aMsg((char*) &lpMsgBuf);
248       err = QString("Failed to find  %1 function. %2").arg( CREATE_MODULE).arg(aMsg);
249       ::LocalFree(lpMsgBuf);
250     }
251   }
252 #else
253   void* modLib = dlopen( libName.toLatin1(), RTLD_LAZY );
254   if ( !modLib )
255   err = QString( "Can not load library %1. %2" ).arg( libName ).arg( dlerror() );
256   else
257   {
258     crtInst = (CREATE_FUNC)dlsym( modLib, GET_MODULE_NAME );
259     if ( !crtInst )
260     err = QString( "Failed to find function %1. %2" ).arg( CREATE_MODULE ).arg( dlerror() );
261   }
262 #endif
263
264   XGUI_Module* aModule = crtInst ? crtInst(this) : 0;
265
266   if (!err.isEmpty()) {
267     if (mainWindow() && mainWindow()->isVisible())
268       QMessageBox::warning(mainWindow(), tr("Error"), err);
269     else
270       qWarning( qPrintable( err ));
271   }
272   return aModule;
273 }
274
275 //******************************************************
276 bool XGUI_Workshop::activateModule()
277 {
278   myPartSetModule = loadModule("PartSet");
279   if (!myPartSetModule)
280     return false;
281   myPartSetModule->createFeatures();
282   return true;
283 }