1 #include "XGUI_Module.h"
2 #include "XGUI_Command.h"
3 #include "XGUI_MainMenu.h"
4 #include "XGUI_MainWindow.h"
5 #include "XGUI_MenuGroupPanel.h"
6 #include "XGUI_Tools.h"
7 #include "XGUI_Workbench.h"
8 #include "XGUI_Workshop.h"
9 #include "XGUI_Viewer.h"
11 #include <Config_FeatureMessage.h>
12 #include <Event_Loop.h>
14 #include <QApplication>
15 #include <QFileDialog>
16 #include <QMessageBox>
17 #include <QMdiSubWindow>
29 XGUI_Workshop::XGUI_Workshop()
32 myMainWindow = new XGUI_MainWindow();
33 myPartSetModule = NULL;
36 //******************************************************
37 XGUI_Workshop::~XGUI_Workshop(void)
41 //******************************************************
42 void XGUI_Workshop::startApplication()
45 //Initialize event listening
46 Event_Loop* aLoop = Event_Loop::loop();
47 Event_ID aFeatureId = aLoop->eventByName("RegisterFeature");
48 aLoop->registerListener(this, aFeatureId);
49 Event_ID aPartSetId = aLoop->eventByName("partset_module");
50 aLoop->registerListener(this, aPartSetId);
53 QMdiSubWindow* aWnd = myMainWindow->viewer()->createView();
54 aWnd->showMaximized();
55 myMainWindow->showPythonConsole();
58 //******************************************************
59 void XGUI_Workshop::initMenu()
61 XGUI_Workbench* aPage = myMainWindow->menuObject()->generalPage();
63 // File commands group
64 XGUI_MenuGroupPanel* aGroup = aPage->addGroup("Default");
66 XGUI_Command* aCommand;
68 aCommand = aGroup->addFeature("SAVE_CMD", tr("SAVE_MENU"), tr("SAVE_MENU_TIP"),
69 QIcon(":pictures/save.png"), QKeySequence::Save);
70 aCommand->connectTo(this, SLOT(onSave()));
71 //aCommand->disable();
73 aCommand = aGroup->addFeature("UNDO_CMD", tr("UNDO_MENU"), tr("UNDO_MENU_TIP"),
74 QIcon(":pictures/undo.png"), QKeySequence::Undo);
76 aCommand = aGroup->addFeature("REDO_CMD", tr("REDO_MENU"), tr("REDO_MENU_TIP"),
77 QIcon(":pictures/redo.png"), QKeySequence::Redo);
79 aCommand = aGroup->addFeature("REBUILD_CMD", tr("REBUILD_MENU"), tr("REBUILD_MENU_TIP"),
80 QIcon(":pictures/rebuild.png"));
82 aCommand = aGroup->addFeature("SAVEAS_CMD", tr("SAVEAS_MENU"), tr("SAVEAS_MENU_TIP"),
83 QIcon(":pictures/save.png"));
84 aCommand->connectTo(this, SLOT(onSaveAs()));
85 //aCommand->disable();
87 aCommand = aGroup->addFeature("OPEN_CMD", tr("OPEN_MENU"), tr("OPEN_MENU_TIP"),
88 QIcon(":pictures/open.png"), QKeySequence::Open);
89 aCommand->connectTo(this, SLOT(onOpen()));
91 aCommand = aGroup->addFeature("NEW_CMD", tr("NEW_MENU"), tr("NEW_MENU_TIP"),
92 QIcon(":pictures/new.png"), QKeySequence::New);
93 aCommand->connectTo(this, SLOT(onNew()));
95 aCommand = aGroup->addFeature("EXIT_CMD", tr("EXIT_MENU"), tr("EXIT_MENU_TIP"),
96 QIcon(":pictures/close.png"), QKeySequence::Close);
97 aCommand->connectTo(this, SLOT(onExit()));
101 //******************************************************
102 XGUI_Workbench* XGUI_Workshop::addWorkbench(const QString& theName)
104 XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
105 return aMenuBar->addWorkbench(theName);
108 //******************************************************
109 void XGUI_Workshop::processEvent(const Event_Message* theMessage)
111 const Config_FeatureMessage* aFeatureMsg = dynamic_cast<const Config_FeatureMessage*>(theMessage);
113 addFeature(aFeatureMsg);
117 qDebug() << "XGUI_Workshop::ProcessEvent: "
118 << "Catch message, but it can not be processed.";
126 void XGUI_Workshop::addFeature(const Config_FeatureMessage* theMessage)
130 qDebug() << "XGUI_Workshop::addFeature: NULL message.";
134 //Find or create Workbench
135 XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
136 QString aWchName = QString::fromStdString(theMessage->workbenchId());
137 XGUI_Workbench* aPage = aMenuBar->findWorkbench(aWchName);
139 aPage = addWorkbench(aWchName);
141 //Find or create Group
142 QString aGroupName = QString::fromStdString(theMessage->groupId());
143 XGUI_MenuGroupPanel* aGroup = aPage->findGroup(aGroupName);
145 aGroup = aPage->addGroup(aGroupName);
148 QString aFeatureId = QString::fromStdString(theMessage->id());
149 XGUI_Command* aCommand = aGroup->addFeature(QString::fromStdString(theMessage->id()),
150 QString::fromStdString(theMessage->text()),
151 QString::fromStdString(theMessage->tooltip()),
152 QIcon(theMessage->icon().c_str())
153 //TODO(sbh): QKeySequence
155 myPartSetModule->featureCreated(aCommand);
158 //******************************************************
159 void XGUI_Workshop::onExit()
164 //******************************************************
165 void XGUI_Workshop::onNew()
167 myMainWindow->showObjectBrowser();
170 //******************************************************
171 void XGUI_Workshop::onOpen()
173 QString aFileName = QFileDialog::getOpenFileName(mainWindow());
176 //******************************************************
177 void XGUI_Workshop::onSave()
181 //******************************************************
182 void XGUI_Workshop::onSaveAs()
184 QString aFileName = QFileDialog::getSaveFileName(mainWindow());
187 //******************************************************
188 XGUI_Module* XGUI_Workshop::loadModule(const QString& theModule)
190 QString libName = library(theModule);
191 if (libName.isEmpty()) {
193 qPrintable( tr( "Information about module \"%1\" doesn't exist." ).arg( theModule ) ));
198 CREATE_FUNC crtInst = 0;
202 HINSTANCE modLib = ::LoadLibrary((LPTSTR) qPrintable(libName));
206 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
207 0, ::GetLastError(), 0, (LPTSTR) & lpMsgBuf, 0, 0);
208 QString aMsg((char*) &lpMsgBuf);
209 err = QString("Failed to load %1. %2").arg(libName).arg(aMsg);
210 ::LocalFree(lpMsgBuf);
212 crtInst = (CREATE_FUNC) ::GetProcAddress(modLib, CREATE_MODULE);
216 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM
217 | FORMAT_MESSAGE_IGNORE_INSERTS,
218 0, ::GetLastError(), 0, (LPTSTR) & lpMsgBuf, 0, 0);
219 QString aMsg((char*) &lpMsgBuf);
220 err = QString("Failed to find %1 function. %2").arg( CREATE_MODULE).arg(aMsg);
221 ::LocalFree(lpMsgBuf);
225 void* modLib = dlopen( libName.toLatin1(), RTLD_LAZY );
227 err = QString( "Can not load library %1. %2" ).arg( libName ).arg( dlerror() );
230 crtInst = (CREATE_FUNC)dlsym( modLib, GET_MODULE_NAME );
232 err = QString( "Failed to find function %1. %2" ).arg( CREATE_MODULE ).arg( dlerror() );
236 XGUI_Module* aModule = crtInst ? crtInst(this) : 0;
238 if (!err.isEmpty()) {
239 if (mainWindow() && mainWindow()->isVisible())
240 QMessageBox::warning(mainWindow(), tr("Error"), err);
242 qWarning( qPrintable( err ));
247 //******************************************************
248 bool XGUI_Workshop::activateModule()
250 myPartSetModule = loadModule("PartSet");
251 if (!myPartSetModule)
253 myPartSetModule->createFeatures();