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"
7 #include <QApplication>
18 XGUI_Workshop::XGUI_Workshop() :
21 myMainWindow = new XGUI_MainWindow();
24 //******************************************************
25 XGUI_Workshop::~XGUI_Workshop(void)
29 //******************************************************
30 void XGUI_Workshop::startApplication()
37 //******************************************************
38 void XGUI_Workshop::initMenu()
40 int aPageId = addWorkbench(tr("HOME_MENU_TITLE"));
42 // File commands group
43 int aGroupId = addGroup(aPageId);
45 XGUI_Command* aCommand;
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()));
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()));
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);
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);
66 // Edit commands group
67 //aGroupId = addGroup(aPageId);
69 aCommand = createMenuCommand(aPageId, aGroupId, UNDO_CMD, tr("UNDO_MENU"), tr("UNDO_MENU_TIP"),
70 QIcon(":pictures/undo.png"), QKeySequence::Undo);
72 aCommand = createMenuCommand(aPageId, aGroupId, REDO_CMD, tr("REDO_MENU"), tr("REDO_MENU_TIP"),
73 QIcon(":pictures/redo.png"), QKeySequence::Redo);
75 //aCommand = createMenuCommand(aPageId, aGroupId, CUT_CMD, tr("CUT_MENU"), tr("CUT_MENU_TIP"),
76 // QIcon(":pictures/cut.png"), QKeySequence::Cut);
78 //aCommand = createMenuCommand(aPageId, aGroupId, COPY_CMD, tr("COPY_MENU"), tr("COPY_MENU_TIP"),
79 // QIcon(":pictures/copy.png"), QKeySequence::Copy);
81 //aCommand = createMenuCommand(aPageId, aGroupId, PASTE_CMD, tr("PASTE_MENU"), tr("PASTE_MENU_TIP"),
82 // QIcon(":pictures/paste.png"), QKeySequence::Paste);
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()));
89 //aPageId = addWorkbench("Primitives");
90 //aGroupId = addGroup(aPageId);
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"));
97 //aPageId = addWorkbench("Operations");
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)
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);
114 //******************************************************
115 int XGUI_Workshop::addWorkbench(const QString& theName)
117 XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
118 return aMenuBar->addWorkbench(theName);
121 //******************************************************
122 int XGUI_Workshop::addGroup(int thePageId)
124 XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
125 return aMenuBar->addGroup(thePageId);
128 //******************************************************
129 void XGUI_Workshop::addCommand(XCommandId theCommandId, int thePageId, int theGroupId, XGUI_Command* theCommand)
131 XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
132 aMenuBar->addCommand(thePageId, theGroupId, theCommand);
133 myCommands[theCommandId] = theCommand;
136 //******************************************************
137 XGUI_Command* XGUI_Workshop::command(XCommandId theId) const
139 if (myCommands.contains(theId))
140 return myCommands[theId];
144 //******************************************************
145 void XGUI_Workshop::onExit()
150 //******************************************************
151 void XGUI_Workshop::onNew()
153 myMainWindow->showObjectBrowser();
156 //******************************************************
157 void XGUI_Workshop::onOpen()
159 QString aFileName = QFileDialog::getOpenFileName(mainWindow());
162 //******************************************************
163 void XGUI_Workshop::onSave()
167 //******************************************************
168 void XGUI_Workshop::onSaveAs()
170 QString aFileName = QFileDialog::getSaveFileName(mainWindow());
173 //******************************************************
174 IModule* XGUI_Workshop::loadModule(const QString& theModule)
176 QString libName = library( theModule );
177 if ( libName.isEmpty() )
179 qWarning( qPrintable( tr( "Information about module \"%1\" doesn't exist." ).arg( theModule ) ) );
184 CREATE_FUNC crtInst = 0;
188 HINSTANCE modLib = ::LoadLibrary( (LPTSTR) qPrintable(libName) );
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 );
200 crtInst = (CREATE_FUNC)::GetProcAddress( modLib, CREATE_MODULE );
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 );
212 void* modLib = dlopen( libName.toLatin1(), RTLD_LAZY );
214 err = QString( "Can not load library %1. %2" ).arg( libName ).arg( dlerror() );
217 crtInst = (CREATE_FUNC)dlsym( modLib, GET_MODULE_NAME );
219 err = QString( "Failed to find function %1. %2" ).arg( CREATE_MODULE ).arg( dlerror() );
223 IModule* aModule = crtInst ? crtInst(this) : 0;
225 if ( !err.isEmpty() ) {
226 if ( mainWindow() && mainWindow()->isVisible() )
227 QMessageBox::warning( mainWindow(), tr( "Error" ), err );
229 qWarning( qPrintable( err ) );
234 //******************************************************
235 bool XGUI_Workshop::activateModule()
237 // Test of modules loading
238 IModule* aModule = loadModule("GeomModule");
241 aModule->createFeatures();
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)
251 return (int) createMenuCommand(thePageId, theGroupId, LAST_CMD, theTitle, theTip, theIcon, theKeys);