Salome HOME
Merge branch 'master' of newgeom:newgeom.git
[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 <QApplication>
8 #include <QFileDialog>
9 #include <QMessageBox>
10
11 #ifdef WIN32
12 #include <windows.h>
13 #else
14 #include <dlfcn.h>
15 #endif
16
17
18 XGUI_Workshop::XGUI_Workshop() :
19     QObject()
20 {
21     myMainWindow = new XGUI_MainWindow();
22 }
23
24 //******************************************************
25 XGUI_Workshop::~XGUI_Workshop(void)
26 {
27 }
28
29 //******************************************************
30 void XGUI_Workshop::startApplication()
31 {
32     initMenu();
33     activateModule();
34     myMainWindow->show();
35 }
36
37 //******************************************************
38 void XGUI_Workshop::initMenu()
39 {
40     IWorkbench* aPage = addWorkbench(tr("GEN_MENU_TITLE"));
41
42     // File commands group
43     IMenuGroup* aGroup = aPage->addGroup();
44
45     IFeatureMenu* aCommand;
46
47     aCommand = aGroup->addFeature("SAVE_CMD", tr("SAVE_MENU"), tr("SAVE_MENU_TIP"),
48                                   QIcon(":pictures/save.png"), QKeySequence::Save);
49     aCommand->connectTo(this, SLOT(onSave()));
50     //aCommand->disable();
51
52     aCommand = aGroup->addFeature("UNDO_CMD", tr("UNDO_MENU"), tr("UNDO_MENU_TIP"),
53                                   QIcon(":pictures/undo.png"), QKeySequence::Undo);
54
55     aCommand = aGroup->addFeature("REDO_CMD", tr("REDO_MENU"), tr("REDO_MENU_TIP"),
56                                  QIcon(":pictures/redo.png"), QKeySequence::Redo);
57
58     aCommand = aGroup->addFeature("REBUILD_CMD", tr("REBUILD_MENU"), tr("REBUILD_MENU_TIP"),
59                                  QIcon(":pictures/rebuild.png"));
60
61     aCommand = aGroup->addFeature("SAVEAS_CMD", tr("SAVEAS_MENU"), tr("SAVEAS_MENU_TIP"),
62                                   QIcon(":pictures/save.png"));
63     aCommand->connectTo(this, SLOT(onSaveAs()));
64     //aCommand->disable();
65
66     aCommand = aGroup->addFeature("OPEN_CMD", tr("OPEN_MENU"), tr("OPEN_MENU_TIP"),
67                                   QIcon(":pictures/open.png"), QKeySequence::Open);
68     aCommand->connectTo(this, SLOT(onOpen()));
69
70
71     aCommand = aGroup->addFeature("NEW_CMD", tr("NEW_MENU"), tr("NEW_MENU_TIP"),
72                                   QIcon(":pictures/new.png"), QKeySequence::New);
73     aCommand->connectTo(this, SLOT(onNew()));
74
75     aCommand = aGroup->addFeature("EXIT_CMD", tr("EXIT_MENU"), tr("EXIT_MENU_TIP"),
76                                   QIcon(":pictures/close.png"), QKeySequence::Close);
77     aCommand->connectTo(this, SLOT(onExit()));
78
79 }
80
81 //******************************************************
82 IWorkbench* XGUI_Workshop::addWorkbench(const QString& theName)
83 {
84     XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
85     return aMenuBar->addWorkbench(theName);
86 }
87
88 //******************************************************
89 void XGUI_Workshop::onExit()
90 {
91     qApp->exit();
92 }
93
94 //******************************************************
95 void XGUI_Workshop::onNew()
96 {
97     myMainWindow->showObjectBrowser();
98 }
99
100 //******************************************************
101 void XGUI_Workshop::onOpen()
102 {
103     QString aFileName = QFileDialog::getOpenFileName(mainWindow());
104 }
105
106 //******************************************************
107 void XGUI_Workshop::onSave()
108 {
109 }
110
111 //******************************************************
112 void XGUI_Workshop::onSaveAs()
113 {
114     QString aFileName = QFileDialog::getSaveFileName(mainWindow());
115 }
116
117 //******************************************************
118 IModule* XGUI_Workshop::loadModule(const QString& theModule)
119 {
120   QString libName = library( theModule );
121   if ( libName.isEmpty() )
122   {
123     qWarning( qPrintable( tr( "Information about module \"%1\" doesn't exist." ).arg( theModule ) ) );
124     return 0;
125   }
126
127   QString err;
128   CREATE_FUNC crtInst = 0;
129
130 #ifdef WIN32
131
132   HINSTANCE modLib = ::LoadLibrary( (LPTSTR) qPrintable(libName) ); 
133   if ( !modLib )
134   {
135     LPVOID lpMsgBuf;
136     ::FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
137                      FORMAT_MESSAGE_IGNORE_INSERTS, 0, ::GetLastError(), 0, (LPTSTR)&lpMsgBuf, 0, 0 );
138     QString aMsg((char*)&lpMsgBuf);
139     err = QString( "Failed to load  %1. %2" ).arg( libName ).arg( aMsg );
140     ::LocalFree( lpMsgBuf );
141   }
142   else
143   {
144     crtInst = (CREATE_FUNC)::GetProcAddress( modLib, CREATE_MODULE );
145     if ( !crtInst )
146     {
147       LPVOID lpMsgBuf;
148       ::FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
149                        FORMAT_MESSAGE_IGNORE_INSERTS, 0, ::GetLastError(), 0, (LPTSTR)&lpMsgBuf, 0, 0 );
150     QString aMsg((char*)&lpMsgBuf);
151     err = QString( "Failed to find  %1 function. %2" ).arg( CREATE_MODULE ).arg(aMsg );
152     ::LocalFree( lpMsgBuf );
153     }
154   }
155 #else
156   void* modLib = dlopen( libName.toLatin1(), RTLD_LAZY );
157   if ( !modLib )
158     err = QString( "Can not load library %1. %2" ).arg( libName ).arg( dlerror() );
159   else
160   {
161     crtInst = (CREATE_FUNC)dlsym( modLib, GET_MODULE_NAME );
162     if ( !crtInst )
163       err = QString( "Failed to find function %1. %2" ).arg( CREATE_MODULE ).arg( dlerror() );
164   }
165 #endif
166
167   IModule* aModule = crtInst ? crtInst(this) : 0;
168
169   if ( !err.isEmpty() ) {
170     if ( mainWindow() && mainWindow()->isVisible() )
171         QMessageBox::warning( mainWindow(), tr( "Error" ), err );
172     else
173       qWarning( qPrintable( err ) ); 
174   }
175   return aModule;
176 }
177
178 //******************************************************
179 bool XGUI_Workshop::activateModule()
180 {
181     // Test of modules loading
182     IModule* aModule = loadModule("GeomModule");
183     if (!aModule)
184         return false;
185     aModule->createFeatures();
186     return true;
187 }
188