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"
8 #include <QApplication>
10 #include <QMessageBox>
19 XGUI_Workshop::XGUI_Workshop() :
22 myMainWindow = new XGUI_MainWindow();
25 //XGUI_MainMenu* aMenuBar = myMainWindow->menuBar();
26 //aMenuBar->setLogo(QPixmap(":pictures/OCCLogo.png"));
29 //******************************************************
30 XGUI_Workshop::~XGUI_Workshop(void)
34 //******************************************************
35 void XGUI_Workshop::startApplication()
42 //******************************************************
43 void XGUI_Workshop::initMenu()
45 int aPageId = addWorkbench(tr("HOME_MENU_TITLE"));
47 // File commands group
48 int aGroupId = addPageGroup(aPageId);
50 XGUI_Command* aCommand;
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()));
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()));
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);
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);
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()));
75 // Edit commands group
76 aGroupId = addPageGroup(aPageId);
78 aCommand = createMenuCommand(aPageId, aGroupId, UNDO_CMD, tr("UNDO_MENU"), tr("UNDO_MENU_TIP"),
79 QIcon(":pictures/undo.png"), QKeySequence::Undo);
81 aCommand = createMenuCommand(aPageId, aGroupId, REDO_CMD, tr("REDO_MENU"), tr("REDO_MENU_TIP"),
82 QIcon(":pictures/redo.png"), QKeySequence::Redo);
84 aCommand = createMenuCommand(aPageId, aGroupId, CUT_CMD, tr("CUT_MENU"), tr("CUT_MENU_TIP"),
85 QIcon(":pictures/cut.png"), QKeySequence::Cut);
87 aCommand = createMenuCommand(aPageId, aGroupId, COPY_CMD, tr("COPY_MENU"), tr("COPY_MENU_TIP"),
88 QIcon(":pictures/copy.png"), QKeySequence::Copy);
90 aCommand = createMenuCommand(aPageId, aGroupId, PASTE_CMD, tr("PASTE_MENU"), tr("PASTE_MENU_TIP"),
91 QIcon(":pictures/paste.png"), QKeySequence::Paste);
94 aPageId = addWorkbench("Primitives");
95 aGroupId = addPageGroup(aPageId);
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"));
102 aPageId = addWorkbench("Operations");
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)
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);
119 //******************************************************
120 int XGUI_Workshop::addWorkbench(const QString& theName)
122 XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
123 return aMenuBar->addWorkbench(theName);
126 //******************************************************
127 int XGUI_Workshop::addPageGroup(int thePageId)
129 XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
130 return aMenuBar->addGroup(thePageId);
133 //******************************************************
134 void XGUI_Workshop::addCommand(XCommandId theCommandId, int thePageId, int theGroupId, XGUI_Command* theCommand)
136 XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
137 aMenuBar->addCommand(thePageId, theGroupId, theCommand);
138 myCommands[theCommandId] = theCommand;
141 //******************************************************
142 XGUI_Command* XGUI_Workshop::command(XCommandId theId) const
144 if (myCommands.contains(theId))
145 return myCommands[theId];
149 //******************************************************
150 void XGUI_Workshop::onExit()
155 //******************************************************
156 void XGUI_Workshop::onNew()
158 myMainWindow->showObjectBrowser();
161 //******************************************************
162 void XGUI_Workshop::onOpen()
164 QString aFileName = QFileDialog::getOpenFileName(mainWindow());
167 //******************************************************
168 void XGUI_Workshop::onSave()
172 //******************************************************
173 void XGUI_Workshop::onSaveAs()
175 QString aFileName = QFileDialog::getSaveFileName(mainWindow());
178 //******************************************************
179 XGUI_Module* XGUI_Workshop::loadModule(const QString& theModule)
181 QString libName = library( theModule );
182 if ( libName.isEmpty() )
184 qWarning( qPrintable( tr( "Information about module \"%1\" doesn't exist." ).arg( theModule ) ) );
189 CREATE_FUNC crtInst = 0;
192 HINSTANCE modLib = ::LoadLibrary( (LPTSTR) libName.utf16() );
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 );
204 crtInst = (CREATE_FUNC)::GetProcAddress( modLib, CREATE_MODULE );
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 );
216 void* modLib = dlopen( libName.toLatin1(), RTLD_LAZY );
218 err = QString( "Can not load library %1. %2" ).arg( libName ).arg( dlerror() );
221 crtInst = (CREATE_FUNC)dlsym( modLib, GET_MODULE_NAME );
223 err = QString( "Failed to find function %1. %2" ).arg( CREATE_MODULE ).arg( dlerror() );
227 XGUI_Module* aModule = crtInst ? crtInst() : 0;
229 if ( !err.isEmpty() ) {
230 if ( mainWindow() && mainWindow()->isVisible() )
231 QMessageBox::warning( mainWindow(), tr( "Error" ), err );
233 qWarning( qPrintable( err ) );
238 //******************************************************
239 void XGUI_Workshop::loadModules()
241 // Test of modules loading
242 QStringList aModules;
243 aModules << "GeomModule";
245 foreach(QString aName, aModules) {
246 XGUI_Module* aModule = loadModule(aName);
247 buildModuleMenu(aModule->moduleDescription());
251 //******************************************************
252 void XGUI_Workshop::buildModuleMenu(const QString& theXML)