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