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