Salome HOME
b7cd63a16e03c65de8fcc7faa5e32e2f589e9780
[modules/shaper.git] / src / XGUI / XGUI_Workshop.cpp
1 #include "XGUI_Workshop.h"
2 #include "XGUI_MainWindow.h"
3 #include "XGUI_MainMenu.h"
4 #include "XGUI_Command.h"
5 #include "XGUI_Tools.h"
6
7 #include <Config_Message.h>
8 #include <Event_Loop.hxx>
9
10 #include <QApplication>
11 #include <QFileDialog>
12 #include <QMessageBox>
13 #ifdef _DEBUG
14 #include <QDebug>
15 #endif
16
17
18 #include <cstdio>
19
20 #ifdef WIN32
21 #include <windows.h>
22 #else
23 #include <dlfcn.h>
24 #endif
25
26
27 XGUI_Workshop::XGUI_Workshop() :
28     QObject()
29 {
30     myMainWindow = new XGUI_MainWindow();
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("Feature");
45     aLoop->RegisterListener(this, aFeatureId);
46     activateModule();
47     myMainWindow->show();
48 }
49
50 //******************************************************
51 void XGUI_Workshop::initMenu()
52 {
53     IWorkbench* aPage = addWorkbench(tr("GEN_MENU_TITLE"));
54
55     // File commands group
56     IMenuGroup* aGroup = aPage->addGroup();
57
58     IFeatureMenu* aCommand;
59
60     aCommand = aGroup->addFeature("SAVE_CMD", tr("SAVE_MENU"), tr("SAVE_MENU_TIP"),
61                                   QIcon(":pictures/save.png"), QKeySequence::Save);
62     aCommand->connectTo(this, SLOT(onSave()));
63     //aCommand->disable();
64
65     aCommand = aGroup->addFeature("UNDO_CMD", tr("UNDO_MENU"), tr("UNDO_MENU_TIP"),
66                                   QIcon(":pictures/undo.png"), QKeySequence::Undo);
67
68     aCommand = aGroup->addFeature("REDO_CMD", tr("REDO_MENU"), tr("REDO_MENU_TIP"),
69                                  QIcon(":pictures/redo.png"), QKeySequence::Redo);
70
71     aCommand = aGroup->addFeature("REBUILD_CMD", tr("REBUILD_MENU"), tr("REBUILD_MENU_TIP"),
72                                  QIcon(":pictures/rebuild.png"));
73
74     aCommand = aGroup->addFeature("SAVEAS_CMD", tr("SAVEAS_MENU"), tr("SAVEAS_MENU_TIP"),
75                                   QIcon(":pictures/save.png"));
76     aCommand->connectTo(this, SLOT(onSaveAs()));
77     //aCommand->disable();
78
79     aCommand = aGroup->addFeature("OPEN_CMD", tr("OPEN_MENU"), tr("OPEN_MENU_TIP"),
80                                   QIcon(":pictures/open.png"), QKeySequence::Open);
81     aCommand->connectTo(this, SLOT(onOpen()));
82
83
84     aCommand = aGroup->addFeature("NEW_CMD", tr("NEW_MENU"), tr("NEW_MENU_TIP"),
85                                   QIcon(":pictures/new.png"), QKeySequence::New);
86     aCommand->connectTo(this, SLOT(onNew()));
87
88     aCommand = aGroup->addFeature("EXIT_CMD", tr("EXIT_MENU"), tr("EXIT_MENU_TIP"),
89                                   QIcon(":pictures/close.png"), QKeySequence::Close);
90     aCommand->connectTo(this, SLOT(onExit()));
91
92 }
93
94 //******************************************************
95 IWorkbench* XGUI_Workshop::addWorkbench(const QString& theName)
96 {
97     XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
98     return aMenuBar->addWorkbench(theName);
99 }
100
101 //******************************************************
102 void XGUI_Workshop::ProcessEvent(const Event_Message* theMessage)
103 {
104   const Config_FeatureMessage* aMsg =
105       dynamic_cast<const Config_FeatureMessage*>( theMessage );
106   if(aMsg) {
107     addFeature(aMsg);
108     return;
109   }
110   #ifdef _DEBUG
111   qDebug() << "XGUI_Workshop::ProcessEvent: "
112       << "Got message, but it's not a Config_FeatureMessage";
113   #endif
114
115 }
116
117 void XGUI_Workshop::addFeature(const Config_FeatureMessage* theMessage)
118 {
119   XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
120   IWorkbench* aPage = aMenuBar->findWorkbench(tr( "GEN_MENU_TITLE" ) + "_Workbench");
121   if(!aPage) {
122     #ifdef _DEBUG
123     qDebug() << "XGUI_Workshop::ProcessEvent: "
124              << "Creating a workbench " << tr( "GEN_MENU_TITLE" );
125     #endif
126     aPage = addWorkbench(tr( "GEN_MENU_TITLE" ));
127   }
128   QString aGroupName = QString::fromStdString(theMessage->m_group);
129   IMenuGroup* aGroup = aPage->findGroup(aGroupName);
130   if(!aGroup) {
131     aGroup = aPage->addGroup(aGroupName);
132   }
133   IFeatureMenu* aCommand;
134   aCommand = aGroup->addFeature(
135     QString::fromStdString(theMessage->m_id),
136     QString::fromStdString(theMessage->m_text),
137     QString::fromStdString(theMessage->m_tooltip),
138     QIcon(theMessage->m_icon.c_str())
139     //TODO(sbh): QKeySequence
140   );
141 }
142
143 //******************************************************
144 void XGUI_Workshop::onExit()
145 {
146     qApp->exit();
147 }
148
149 //******************************************************
150 void XGUI_Workshop::onNew()
151 {
152     myMainWindow->showObjectBrowser();
153 }
154
155 //******************************************************
156 void XGUI_Workshop::onOpen()
157 {
158     QString aFileName = QFileDialog::getOpenFileName(mainWindow());
159 }
160
161 //******************************************************
162 void XGUI_Workshop::onSave()
163 {
164 }
165
166 //******************************************************
167 void XGUI_Workshop::onSaveAs()
168 {
169     QString aFileName = QFileDialog::getSaveFileName(mainWindow());
170 }
171
172 //******************************************************
173 IModule* XGUI_Workshop::loadModule(const QString& theModule)
174 {
175   QString libName = library( theModule );
176   if ( libName.isEmpty() )
177   {
178     qWarning( qPrintable( tr( "Information about module \"%1\" doesn't exist." ).arg( theModule ) ) );
179     return 0;
180   }
181
182   QString err;
183   CREATE_FUNC crtInst = 0;
184
185 #ifdef WIN32
186
187   HINSTANCE modLib = ::LoadLibrary( (LPTSTR) qPrintable(libName) ); 
188   if ( !modLib )
189   {
190     LPVOID lpMsgBuf;
191     ::FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
192                      FORMAT_MESSAGE_IGNORE_INSERTS, 0, ::GetLastError(), 0, (LPTSTR)&lpMsgBuf, 0, 0 );
193     QString aMsg((char*)&lpMsgBuf);
194     err = QString( "Failed to load  %1. %2" ).arg( libName ).arg( aMsg );
195     ::LocalFree( lpMsgBuf );
196   }
197   else
198   {
199     crtInst = (CREATE_FUNC)::GetProcAddress( modLib, CREATE_MODULE );
200     if ( !crtInst )
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 find  %1 function. %2" ).arg( CREATE_MODULE ).arg(aMsg );
207     ::LocalFree( lpMsgBuf );
208     }
209   }
210 #else
211   void* modLib = dlopen( libName.toLatin1(), RTLD_LAZY );
212   if ( !modLib )
213     err = QString( "Can not load library %1. %2" ).arg( libName ).arg( dlerror() );
214   else
215   {
216     crtInst = (CREATE_FUNC)dlsym( modLib, GET_MODULE_NAME );
217     if ( !crtInst )
218       err = QString( "Failed to find function %1. %2" ).arg( CREATE_MODULE ).arg( dlerror() );
219   }
220 #endif
221
222   IModule* aModule = crtInst ? crtInst(this) : 0;
223
224   if ( !err.isEmpty() ) {
225     if ( mainWindow() && mainWindow()->isVisible() )
226         QMessageBox::warning( mainWindow(), tr( "Error" ), err );
227     else
228       qWarning( qPrintable( err ) ); 
229   }
230   return aModule;
231 }
232
233 //******************************************************
234 bool XGUI_Workshop::activateModule()
235 {
236     // Test of modules loading
237     IModule* aModule = loadModule("GeomModule");
238     if (!aModule)
239         return false;
240     aModule->createFeatures();
241     return true;
242 }
243