Salome HOME
Removing using of TS file
[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.h>
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("RegisterFeature");
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   myMainWindow->showPythonConsole();
56 }
57
58 //******************************************************
59 void XGUI_Workshop::initMenu()
60 {
61   XGUI_Workbench* aPage = myMainWindow->menuObject()->generalPage();
62
63   // File commands group
64   XGUI_MenuGroupPanel* aGroup = aPage->addGroup("Default");
65
66   XGUI_Command* aCommand;
67
68   aCommand = aGroup->addFeature("SAVE_CMD", tr("Save..."), tr("Save the document"),
69                                 QIcon(":pictures/save.png"), QKeySequence::Save);
70   aCommand->connectTo(this, SLOT(onSave()));
71   //aCommand->disable();
72
73   aCommand = aGroup->addFeature("UNDO_CMD", tr("Undo"), tr("Undo last command"),
74                                 QIcon(":pictures/undo.png"), QKeySequence::Undo);
75
76   aCommand = aGroup->addFeature("REDO_CMD", tr("Redo"), tr("Redo last command"),
77                                 QIcon(":pictures/redo.png"), QKeySequence::Redo);
78
79   aCommand = aGroup->addFeature("REBUILD_CMD", tr("Rebuild"), tr("Rebuild data objects"),
80                                 QIcon(":pictures/rebuild.png"));
81
82   aCommand = aGroup->addFeature("SAVEAS_CMD", tr("Save as..."), tr("Save the document into a file"),
83                                 QIcon(":pictures/save.png"));
84   aCommand->connectTo(this, SLOT(onSaveAs()));
85   //aCommand->disable();
86
87   aCommand = aGroup->addFeature("OPEN_CMD", tr("Open..."), tr("Open a new document"),
88                                 QIcon(":pictures/open.png"), QKeySequence::Open);
89   aCommand->connectTo(this, SLOT(onOpen()));
90
91   aCommand = aGroup->addFeature("NEW_CMD", tr("New"), tr("Create a new document"),
92                                 QIcon(":pictures/new.png"), QKeySequence::New);
93   aCommand->connectTo(this, SLOT(onNew()));
94
95   aCommand = aGroup->addFeature("EXIT_CMD", tr("Exit"), tr("Exit application"),
96                                 QIcon(":pictures/close.png"), QKeySequence::Close);
97   aCommand->connectTo(this, SLOT(onExit()));
98
99 }
100
101 //******************************************************
102 XGUI_Workbench* XGUI_Workshop::addWorkbench(const QString& theName)
103 {
104   XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
105   return aMenuBar->addWorkbench(theName);
106 }
107
108 //******************************************************
109 void XGUI_Workshop::processEvent(const Event_Message* theMessage)
110 {
111   const Config_FeatureMessage* aFeatureMsg = dynamic_cast<const Config_FeatureMessage*>(theMessage);
112   if (aFeatureMsg) {
113     addFeature(aFeatureMsg);
114     return;
115   }
116 #ifdef _DEBUG
117   qDebug() << "XGUI_Workshop::ProcessEvent: "
118   << "Catch message, but it can not be processed.";
119 #endif
120
121 }
122
123 /*
124  *
125  */
126 void XGUI_Workshop::addFeature(const Config_FeatureMessage* theMessage)
127 {
128   if (!theMessage) {
129 #ifdef _DEBUG
130     qDebug() << "XGUI_Workshop::addFeature: NULL message.";
131 #endif
132     return;
133   }
134   //Find or create Workbench
135   XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
136   QString aWchName = QString::fromStdString(theMessage->workbenchId());
137   XGUI_Workbench* aPage = aMenuBar->findWorkbench(aWchName);
138   if (!aPage) {
139     aPage = addWorkbench(aWchName);
140   }
141   //Find or create Group
142   QString aGroupName = QString::fromStdString(theMessage->groupId());
143   XGUI_MenuGroupPanel* aGroup = aPage->findGroup(aGroupName);
144   if (!aGroup) {
145     aGroup = aPage->addGroup(aGroupName);
146   }
147   //Create feature...
148   QString aFeatureId = QString::fromStdString(theMessage->id());
149   XGUI_Command* aCommand = aGroup->addFeature(QString::fromStdString(theMessage->id()),
150                                               QString::fromStdString(theMessage->text()),
151                                               QString::fromStdString(theMessage->tooltip()),
152                                               QIcon(theMessage->icon().c_str())
153                                               //TODO(sbh): QKeySequence
154                                                   );
155   myPartSetModule->featureCreated(aCommand);
156 }
157
158 //******************************************************
159 void XGUI_Workshop::onExit()
160 {
161   qApp->exit();
162 }
163
164 //******************************************************
165 void XGUI_Workshop::onNew()
166 {
167   myMainWindow->showObjectBrowser();
168 }
169
170 //******************************************************
171 void XGUI_Workshop::onOpen()
172 {
173   QString aFileName = QFileDialog::getOpenFileName(mainWindow());
174 }
175
176 //******************************************************
177 void XGUI_Workshop::onSave()
178 {
179 }
180
181 //******************************************************
182 void XGUI_Workshop::onSaveAs()
183 {
184   QString aFileName = QFileDialog::getSaveFileName(mainWindow());
185 }
186
187 //******************************************************
188 XGUI_Module* XGUI_Workshop::loadModule(const QString& theModule)
189 {
190   QString libName = library(theModule);
191   if (libName.isEmpty()) {
192     qWarning(
193     qPrintable( tr( "Information about module \"%1\" doesn't exist." ).arg( theModule ) ));
194     return 0;
195   }
196
197   QString err;
198   CREATE_FUNC crtInst = 0;
199
200 #ifdef WIN32
201
202   HINSTANCE modLib = ::LoadLibrary((LPTSTR) qPrintable(libName));
203   if (!modLib) {
204     LPVOID lpMsgBuf;
205     ::FormatMessage(
206         FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
207         0, ::GetLastError(), 0, (LPTSTR) & lpMsgBuf, 0, 0);
208     QString aMsg((char*) &lpMsgBuf);
209     err = QString("Failed to load  %1. %2").arg(libName).arg(aMsg);
210     ::LocalFree(lpMsgBuf);
211   } else {
212     crtInst = (CREATE_FUNC) ::GetProcAddress(modLib, CREATE_MODULE);
213     if (!crtInst) {
214       LPVOID lpMsgBuf;
215       ::FormatMessage(
216           FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM
217               | FORMAT_MESSAGE_IGNORE_INSERTS,
218           0, ::GetLastError(), 0, (LPTSTR) & lpMsgBuf, 0, 0);
219       QString aMsg((char*) &lpMsgBuf);
220       err = QString("Failed to find  %1 function. %2").arg( CREATE_MODULE).arg(aMsg);
221       ::LocalFree(lpMsgBuf);
222     }
223   }
224 #else
225   void* modLib = dlopen( libName.toLatin1(), RTLD_LAZY );
226   if ( !modLib )
227   err = QString( "Can not load library %1. %2" ).arg( libName ).arg( dlerror() );
228   else
229   {
230     crtInst = (CREATE_FUNC)dlsym( modLib, GET_MODULE_NAME );
231     if ( !crtInst )
232     err = QString( "Failed to find function %1. %2" ).arg( CREATE_MODULE ).arg( dlerror() );
233   }
234 #endif
235
236   XGUI_Module* aModule = crtInst ? crtInst(this) : 0;
237
238   if (!err.isEmpty()) {
239     if (mainWindow() && mainWindow()->isVisible())
240       QMessageBox::warning(mainWindow(), tr("Error"), err);
241     else
242       qWarning( qPrintable( err ));
243   }
244   return aModule;
245 }
246
247 //******************************************************
248 bool XGUI_Workshop::activateModule()
249 {
250   myPartSetModule = loadModule("PartSet");
251   if (!myPartSetModule)
252     return false;
253   myPartSetModule->createFeatures();
254   return true;
255 }
256