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