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