Salome HOME
Testing GUI version
[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     int aPageId = addWorkbench(tr("HOME_MENU_TITLE"));
41
42     // File commands group
43     int aGroupId = addGroup(aPageId);
44
45     XGUI_Command* aCommand;
46
47     //aCommand = createMenuCommand(aPageId, aGroupId, NEW_CMD, tr("NEW_MENU"), tr("NEW_MENU_TIP"),
48     //                             QIcon(":pictures/new.png"), QKeySequence::New);
49     //connect(aCommand, SIGNAL(triggered()), this, SLOT(onNew()));
50
51     //aCommand = createMenuCommand(aPageId, aGroupId, OPEN_CMD, tr("OPEN_MENU"), tr("OPEN_MENU_TIP"),
52     //                             QIcon(":pictures/open.png"), QKeySequence::Open);
53     //connect(aCommand, SIGNAL(triggered()), this, SLOT(onOpen()));
54
55     aCommand = createMenuCommand(aPageId, aGroupId, SAVE_CMD, tr("SAVE_MENU"), tr("SAVE_MENU_TIP"),
56                                  QIcon(":pictures/save.png"), QKeySequence::Save);
57     connect(aCommand, SIGNAL(triggered()), this, SLOT(onSave()));
58     aCommand->setEnabled(false);
59
60     aCommand = createMenuCommand(aPageId, aGroupId, SAVEAS_CMD, tr("SAVEAS_MENU"), tr("SAVEAS_MENU_TIP"),
61                                  QIcon(":pictures/save.png"));
62     connect(aCommand, SIGNAL(triggered()), this, SLOT(onSaveAs()));
63     aCommand->setEnabled(false);
64
65
66     // Edit commands group
67     //aGroupId = addGroup(aPageId);
68
69     aCommand = createMenuCommand(aPageId, aGroupId, UNDO_CMD, tr("UNDO_MENU"), tr("UNDO_MENU_TIP"),
70                                  QIcon(":pictures/undo.png"), QKeySequence::Undo);
71
72     aCommand = createMenuCommand(aPageId, aGroupId, REDO_CMD, tr("REDO_MENU"), tr("REDO_MENU_TIP"),
73                                  QIcon(":pictures/redo.png"), QKeySequence::Redo);
74
75     //aCommand = createMenuCommand(aPageId, aGroupId, CUT_CMD, tr("CUT_MENU"), tr("CUT_MENU_TIP"),
76     //                             QIcon(":pictures/cut.png"), QKeySequence::Cut);
77
78     //aCommand = createMenuCommand(aPageId, aGroupId, COPY_CMD, tr("COPY_MENU"), tr("COPY_MENU_TIP"),
79     //                             QIcon(":pictures/copy.png"), QKeySequence::Copy);
80
81     //aCommand = createMenuCommand(aPageId, aGroupId, PASTE_CMD, tr("PASTE_MENU"), tr("PASTE_MENU_TIP"),
82     //                             QIcon(":pictures/paste.png"), QKeySequence::Paste);
83
84     aCommand = createMenuCommand(aPageId, aGroupId, EXIT_CMD, tr("EXIT_MENU"), tr("EXIT_MENU_TIP"),
85                                  QIcon(":pictures/close.png"), QKeySequence::Close);
86     connect(aCommand, SIGNAL(triggered()), this, SLOT(onExit()));
87
88     // Tests
89     //aPageId = addWorkbench("Primitives");
90     //aGroupId = addGroup(aPageId);
91     //
92     //aCommand = createMenuCommand(aPageId, aGroupId, LAST_CMD, "Box", "Create Box", QIcon(":pictures/box.png"));
93     //aCommand = createMenuCommand(aPageId, aGroupId, LAST_CMD, "Cylinder", "Create Cylinder", QIcon(":pictures/cylinder.png"));
94     //aCommand = createMenuCommand(aPageId, aGroupId, LAST_CMD, "Disk", "Create Disk", QIcon(":pictures/disk.png"));
95     //aCommand = createMenuCommand(aPageId, aGroupId, LAST_CMD, "Torus", "Create Torus", QIcon(":pictures/torus.png"));
96
97     //aPageId = addWorkbench("Operations");
98
99 }
100
101 //******************************************************
102 XGUI_Command* XGUI_Workshop::createMenuCommand(int thePageId, int theGroupId, XCommandId theCmdId, 
103                                                const QString& theTitle, const QString& theTip, 
104                                                const QIcon& theIcon, const QKeySequence& theKeys)
105 {
106     XGUI_Command* aCommand = new XGUI_Command(theIcon, theTitle, this);
107     aCommand->setToolTip(theTip);
108     if (!theKeys.isEmpty())
109         aCommand->setShortcut(theKeys);
110     addCommand(theCmdId, thePageId, theGroupId, aCommand);
111     return aCommand;
112 }
113
114 //******************************************************
115 int XGUI_Workshop::addWorkbench(const QString& theName)
116 {
117     XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
118     return aMenuBar->addWorkbench(theName);
119 }
120
121 //******************************************************
122 int XGUI_Workshop::addGroup(int thePageId)
123 {
124     XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
125     return aMenuBar->addGroup(thePageId);
126 }
127
128 //******************************************************
129 void XGUI_Workshop::addCommand(XCommandId theCommandId, int thePageId, int theGroupId, XGUI_Command* theCommand)
130 {
131     XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
132     aMenuBar->addCommand(thePageId, theGroupId, theCommand);
133     myCommands[theCommandId] = theCommand;
134 }
135
136 //******************************************************
137 XGUI_Command* XGUI_Workshop::command(XCommandId theId) const
138 {
139     if (myCommands.contains(theId))
140         return myCommands[theId];
141     return 0;
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 IModule* 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   IModule* 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     IModule* aModule = loadModule("GeomModule");
239     if (!aModule)
240         return false;
241     aModule->createFeatures();
242     return true;
243 }
244
245 //******************************************************
246 int XGUI_Workshop::addFeature(int thePageId, int theGroupId, 
247                            const QString& theTitle, const QString& theTip, 
248                            const QIcon& theIcon, 
249                            const QKeySequence& theKeys)
250 {
251     return (int) createMenuCommand(thePageId, theGroupId, LAST_CMD, theTitle, theTip, theIcon, theKeys);
252 }