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