Salome HOME
"GUI" stubs for testing of CMake building procedures, preliminary examples of GUI
[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 #include "XGUI_Module.h"
7
8 #include <QApplication>
9 #include <QFileDialog>
10 #include <QMessageBox>
11
12 #ifdef WIN32
13 #include <windows.h>
14 #else
15 #include <dlfcn.h>
16 #endif
17
18
19 XGUI_Workshop::XGUI_Workshop() :
20     QObject()
21 {
22     myMainWindow = new XGUI_MainWindow();
23
24     // Set Logo
25     //XGUI_MainMenu* aMenuBar = myMainWindow->menuBar();
26     //aMenuBar->setLogo(QPixmap(":pictures/OCCLogo.png"));
27 }
28
29 //******************************************************
30 XGUI_Workshop::~XGUI_Workshop(void)
31 {
32 }
33
34 //******************************************************
35 void XGUI_Workshop::startApplication()
36 {
37     initMenu();
38     //loadModules();
39     myMainWindow->show();
40 }
41
42 //******************************************************
43 void XGUI_Workshop::initMenu()
44 {
45     int aPageId = addWorkbench(tr("HOME_MENU_TITLE"));
46
47     // File commands group
48     int aGroupId = addPageGroup(aPageId);
49
50     XGUI_Command* aCommand;
51
52     aCommand = createMenuCommand(aPageId, aGroupId, NEW_CMD, tr("NEW_MENU"), tr("NEW_MENU_TIP"),
53                                  QIcon(":pictures/new.png"), QKeySequence::New);
54     connect(aCommand, SIGNAL(triggered()), this, SLOT(onNew()));
55
56     aCommand = createMenuCommand(aPageId, aGroupId, OPEN_CMD, tr("OPEN_MENU"), tr("OPEN_MENU_TIP"),
57                                  QIcon(":pictures/open.png"), QKeySequence::Open);
58     connect(aCommand, SIGNAL(triggered()), this, SLOT(onOpen()));
59
60     aCommand = createMenuCommand(aPageId, aGroupId, SAVE_CMD, tr("SAVE_MENU"), tr("SAVE_MENU_TIP"),
61                                  QIcon(":pictures/save.png"), QKeySequence::Save);
62     connect(aCommand, SIGNAL(triggered()), this, SLOT(onSave()));
63     aCommand->setEnabled(false);
64
65     aCommand = createMenuCommand(aPageId, aGroupId, SAVEAS_CMD, tr("SAVEAS_MENU"), tr("SAVEAS_MENU_TIP"),
66                                  QIcon(":pictures/save.png"));
67     connect(aCommand, SIGNAL(triggered()), this, SLOT(onSaveAs()));
68     aCommand->setEnabled(false);
69
70     aCommand = createMenuCommand(aPageId, aGroupId, EXIT_CMD, tr("EXIT_MENU"), tr("EXIT_MENU_TIP"),
71                                  QIcon(":pictures/close.png"), QKeySequence::Close);
72     connect(aCommand, SIGNAL(triggered()), this, SLOT(onExit()));
73
74
75     // Edit commands group
76     aGroupId = addPageGroup(aPageId);
77
78     aCommand = createMenuCommand(aPageId, aGroupId, UNDO_CMD, tr("UNDO_MENU"), tr("UNDO_MENU_TIP"),
79                                  QIcon(":pictures/undo.png"), QKeySequence::Undo);
80
81     aCommand = createMenuCommand(aPageId, aGroupId, REDO_CMD, tr("REDO_MENU"), tr("REDO_MENU_TIP"),
82                                  QIcon(":pictures/redo.png"), QKeySequence::Redo);
83
84     aCommand = createMenuCommand(aPageId, aGroupId, CUT_CMD, tr("CUT_MENU"), tr("CUT_MENU_TIP"),
85                                  QIcon(":pictures/cut.png"), QKeySequence::Cut);
86
87     aCommand = createMenuCommand(aPageId, aGroupId, COPY_CMD, tr("COPY_MENU"), tr("COPY_MENU_TIP"),
88                                  QIcon(":pictures/copy.png"), QKeySequence::Copy);
89
90     aCommand = createMenuCommand(aPageId, aGroupId, PASTE_CMD, tr("PASTE_MENU"), tr("PASTE_MENU_TIP"),
91                                  QIcon(":pictures/paste.png"), QKeySequence::Paste);
92
93     // Tests
94     aPageId = addWorkbench("Primitives");
95     aGroupId = addPageGroup(aPageId);
96     
97     aCommand = createMenuCommand(aPageId, aGroupId, LAST_CMD, "Box", "Create Box", QIcon(":pictures/box.png"));
98     aCommand = createMenuCommand(aPageId, aGroupId, LAST_CMD, "Cylinder", "Create Cylinder", QIcon(":pictures/cylinder.png"));
99     aCommand = createMenuCommand(aPageId, aGroupId, LAST_CMD, "Disk", "Create Disk", QIcon(":pictures/disk.png"));
100     aCommand = createMenuCommand(aPageId, aGroupId, LAST_CMD, "Torus", "Create Torus", QIcon(":pictures/torus.png"));
101
102     aPageId = addWorkbench("Operations");
103
104 }
105
106 //******************************************************
107 XGUI_Command* XGUI_Workshop::createMenuCommand(int thePageId, int theGroupId, XCommandId theCmdId, 
108                                                const QString& theTitle, const QString& theTip, 
109                                                const QIcon& theIcon, const QKeySequence& theKeys)
110 {
111     XGUI_Command* aCommand = new XGUI_Command(theIcon, theTitle, this);
112     aCommand->setToolTip(theTip);
113     if (!theKeys.isEmpty())
114         aCommand->setShortcut(theKeys);
115     addCommand(theCmdId, thePageId, theGroupId, aCommand);
116     return aCommand;
117 }
118
119 //******************************************************
120 int XGUI_Workshop::addWorkbench(const QString& theName)
121 {
122     XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
123     return aMenuBar->addWorkbench(theName);
124 }
125
126 //******************************************************
127 int XGUI_Workshop::addPageGroup(int thePageId)
128 {
129     XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
130     return aMenuBar->addGroup(thePageId);
131 }
132
133 //******************************************************
134 void XGUI_Workshop::addCommand(XCommandId theCommandId, int thePageId, int theGroupId, XGUI_Command* theCommand)
135 {
136     XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
137     aMenuBar->addCommand(thePageId, theGroupId, theCommand);
138     myCommands[theCommandId] = theCommand;
139 }
140
141 //******************************************************
142 XGUI_Command* XGUI_Workshop::command(XCommandId theId) const
143 {
144     if (myCommands.contains(theId))
145         return myCommands[theId];
146     return 0;
147 }
148
149 //******************************************************
150 void XGUI_Workshop::onExit()
151 {
152     qApp->exit();
153 }
154
155 //******************************************************
156 void XGUI_Workshop::onNew()
157 {
158     myMainWindow->showObjectBrowser();
159 }
160
161 //******************************************************
162 void XGUI_Workshop::onOpen()
163 {
164     QString aFileName = QFileDialog::getOpenFileName(mainWindow());
165 }
166
167 //******************************************************
168 void XGUI_Workshop::onSave()
169 {
170 }
171
172 //******************************************************
173 void XGUI_Workshop::onSaveAs()
174 {
175     QString aFileName = QFileDialog::getSaveFileName(mainWindow());
176 }
177
178 //******************************************************
179 XGUI_Module* XGUI_Workshop::loadModule(const QString& theModule)
180 {
181   QString libName = library( theModule );
182   if ( libName.isEmpty() )
183   {
184     qWarning( qPrintable( tr( "Information about module \"%1\" doesn't exist." ).arg( theModule ) ) );
185     return 0;
186   }
187
188   QString err;
189   CREATE_FUNC crtInst = 0;
190
191 #ifdef WIN32
192   HINSTANCE modLib = ::LoadLibrary( (LPTSTR) libName.utf16() ); 
193   if ( !modLib )
194   {
195     LPVOID lpMsgBuf;
196     ::FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
197                      FORMAT_MESSAGE_IGNORE_INSERTS, 0, ::GetLastError(), 0, (LPTSTR)&lpMsgBuf, 0, 0 );
198     QString aMsg((char*)&lpMsgBuf);
199     err = QString( "Failed to load  %1. %2" ).arg( libName ).arg( aMsg );
200     ::LocalFree( lpMsgBuf );
201   }
202   else
203   {
204     crtInst = (CREATE_FUNC)::GetProcAddress( modLib, CREATE_MODULE );
205     if ( !crtInst )
206     {
207       LPVOID lpMsgBuf;
208       ::FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
209                        FORMAT_MESSAGE_IGNORE_INSERTS, 0, ::GetLastError(), 0, (LPTSTR)&lpMsgBuf, 0, 0 );
210     QString aMsg((char*)&lpMsgBuf);
211     err = QString( "Failed to find  %1 function. %2" ).arg( CREATE_MODULE ).arg(aMsg );
212     ::LocalFree( lpMsgBuf );
213     }
214   }
215 #else
216   void* modLib = dlopen( libName.toLatin1(), RTLD_LAZY );
217   if ( !modLib )
218     err = QString( "Can not load library %1. %2" ).arg( libName ).arg( dlerror() );
219   else
220   {
221     crtInst = (CREATE_FUNC)dlsym( modLib, GET_MODULE_NAME );
222     if ( !crtInst )
223       err = QString( "Failed to find function %1. %2" ).arg( CREATE_MODULE ).arg( dlerror() );
224   }
225 #endif
226
227   XGUI_Module* aModule = crtInst ? crtInst() : 0;
228
229   if ( !err.isEmpty() ) {
230     if ( mainWindow() && mainWindow()->isVisible() )
231         QMessageBox::warning( mainWindow(), tr( "Error" ), err );
232     else
233       qWarning( qPrintable( err ) ); 
234   }
235   return aModule;
236 }
237
238 //******************************************************
239 void XGUI_Workshop::loadModules()
240 {
241     // Test of modules loading
242     QStringList aModules;
243     aModules << "GeomModule";
244
245     foreach(QString aName, aModules) {
246         XGUI_Module* aModule = loadModule(aName);
247         buildModuleMenu(aModule->moduleDescription());
248     }
249 }
250
251 //******************************************************
252 void XGUI_Workshop::buildModuleMenu(const QString& theXML)
253 {
254
255 }