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