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