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"
10 #include "XGUI_WidgetFactory.h"
12 #include <Event_Loop.h>
13 #include <Config_FeatureMessage.h>
14 #include <Config_WidgetMessage.h>
16 #include <QApplication>
17 #include <QFileDialog>
18 #include <QMessageBox>
19 #include <QMdiSubWindow>
31 XGUI_Workshop::XGUI_Workshop()
34 myMainWindow = new XGUI_MainWindow();
35 myPartSetModule = NULL;
38 //******************************************************
39 XGUI_Workshop::~XGUI_Workshop(void)
43 //******************************************************
44 void XGUI_Workshop::startApplication()
47 //Initialize event listening
48 Event_Loop* aLoop = Event_Loop::loop();
49 //TODO(sbh): Implement static method to extract event id [SEID]
50 Event_ID aFeatureId = aLoop->eventByName("FeatureEvent");
51 aLoop->registerListener(this, aFeatureId);
52 Event_ID aPartSetId = aLoop->eventByName("PartSetModuleEvent");
53 aLoop->registerListener(this, aPartSetId);
56 QMdiSubWindow* aWnd = myMainWindow->viewer()->createView();
57 aWnd->showMaximized();
58 myMainWindow->showPythonConsole();
61 //******************************************************
62 void XGUI_Workshop::initMenu()
64 XGUI_Workbench* aPage = myMainWindow->menuObject()->generalPage();
66 // File commands group
67 XGUI_MenuGroupPanel* aGroup = aPage->addGroup("Default");
69 XGUI_Command* aCommand;
71 aCommand = aGroup->addFeature("SAVE_CMD", tr("Save..."), tr("Save the document"),
72 QIcon(":pictures/save.png"), QKeySequence::Save);
73 aCommand->connectTo(this, SLOT(onSave()));
74 //aCommand->disable();
76 aCommand = aGroup->addFeature("UNDO_CMD", tr("Undo"), tr("Undo last command"),
77 QIcon(":pictures/undo.png"), QKeySequence::Undo);
79 aCommand = aGroup->addFeature("REDO_CMD", tr("Redo"), tr("Redo last command"),
80 QIcon(":pictures/redo.png"), QKeySequence::Redo);
82 aCommand = aGroup->addFeature("REBUILD_CMD", tr("Rebuild"), tr("Rebuild data objects"),
83 QIcon(":pictures/rebuild.png"));
85 aCommand = aGroup->addFeature("SAVEAS_CMD", tr("Save as..."), tr("Save the document into a file"),
86 QIcon(":pictures/save.png"));
87 aCommand->connectTo(this, SLOT(onSaveAs()));
88 //aCommand->disable();
90 aCommand = aGroup->addFeature("OPEN_CMD", tr("Open..."), tr("Open a new document"),
91 QIcon(":pictures/open.png"), QKeySequence::Open);
92 aCommand->connectTo(this, SLOT(onOpen()));
94 aCommand = aGroup->addFeature("NEW_CMD", tr("New"), tr("Create a new document"),
95 QIcon(":pictures/new.png"), QKeySequence::New);
96 aCommand->connectTo(this, SLOT(onNew()));
98 aCommand = aGroup->addFeature("EXIT_CMD", tr("Exit"), tr("Exit application"),
99 QIcon(":pictures/close.png"), QKeySequence::Close);
100 aCommand->connectTo(this, SLOT(onExit()));
104 //******************************************************
105 XGUI_Workbench* XGUI_Workshop::addWorkbench(const QString& theName)
107 XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
108 return aMenuBar->addWorkbench(theName);
111 //******************************************************
112 void XGUI_Workshop::processEvent(const Event_Message* theMessage)
114 const Config_FeatureMessage* aFeatureMsg =
115 dynamic_cast<const Config_FeatureMessage*>(theMessage);
117 addFeature(aFeatureMsg);
120 const Config_WidgetMessage* aPartSetMsg = dynamic_cast<const Config_WidgetMessage*>(theMessage);
122 fillPropertyPanel(aPartSetMsg);
127 qDebug() << "XGUI_Workshop::ProcessEvent: "
128 << "Catch message, but it can not be processed.";
136 void XGUI_Workshop::addFeature(const Config_FeatureMessage* theMessage)
140 qDebug() << "XGUI_Workshop::addFeature: NULL message.";
144 //Find or create Workbench
145 XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
146 QString aWchName = QString::fromStdString(theMessage->workbenchId());
147 XGUI_Workbench* aPage = aMenuBar->findWorkbench(aWchName);
149 aPage = addWorkbench(aWchName);
151 //Find or create Group
152 QString aGroupName = QString::fromStdString(theMessage->groupId());
153 XGUI_MenuGroupPanel* aGroup = aPage->findGroup(aGroupName);
155 aGroup = aPage->addGroup(aGroupName);
158 QString aFeatureId = QString::fromStdString(theMessage->id());
159 XGUI_Command* aCommand = aGroup->addFeature(QString::fromStdString(theMessage->id()),
160 QString::fromStdString(theMessage->text()),
161 QString::fromStdString(theMessage->tooltip()),
162 QIcon(theMessage->icon().c_str())
163 //TODO(sbh): QKeySequence
165 myPartSetModule->featureCreated(aCommand);
171 void XGUI_Workshop::fillPropertyPanel(const Config_WidgetMessage* theMessage)
173 QWidget* aPropWidget = myMainWindow->findChild<QWidget*>("PropertyPanelWidget");
176 qDebug() << "XGUI_Workshop::fillPropertyPanel: " << "Unable to find property panel";
180 qDeleteAll(aPropWidget->children());
181 std::string aXml = theMessage->xmlRepresentation();
182 XGUI_WidgetFactory aFactory = XGUI_WidgetFactory(aXml);
183 aFactory.fillWidget(aPropWidget);
186 //******************************************************
187 void XGUI_Workshop::onExit()
192 //******************************************************
193 void XGUI_Workshop::onNew()
195 myMainWindow->showObjectBrowser();
198 //******************************************************
199 void XGUI_Workshop::onOpen()
201 QString aFileName = QFileDialog::getOpenFileName(mainWindow());
204 //******************************************************
205 void XGUI_Workshop::onSave()
209 //******************************************************
210 void XGUI_Workshop::onSaveAs()
212 QString aFileName = QFileDialog::getSaveFileName(mainWindow());
215 //******************************************************
216 XGUI_Module* XGUI_Workshop::loadModule(const QString& theModule)
218 QString libName = library(theModule);
219 if (libName.isEmpty()) {
221 qPrintable( tr( "Information about module \"%1\" doesn't exist." ).arg( theModule ) ));
226 CREATE_FUNC crtInst = 0;
230 HINSTANCE modLib = ::LoadLibrary((LPTSTR) qPrintable(libName));
234 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
235 0, ::GetLastError(), 0, (LPTSTR) & lpMsgBuf, 0, 0);
236 QString aMsg((char*) &lpMsgBuf);
237 err = QString("Failed to load %1. %2").arg(libName).arg(aMsg);
238 ::LocalFree(lpMsgBuf);
240 crtInst = (CREATE_FUNC) ::GetProcAddress(modLib, CREATE_MODULE);
244 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM
245 | FORMAT_MESSAGE_IGNORE_INSERTS,
246 0, ::GetLastError(), 0, (LPTSTR) & lpMsgBuf, 0, 0);
247 QString aMsg((char*) &lpMsgBuf);
248 err = QString("Failed to find %1 function. %2").arg( CREATE_MODULE).arg(aMsg);
249 ::LocalFree(lpMsgBuf);
253 void* modLib = dlopen( libName.toLatin1(), RTLD_LAZY );
255 err = QString( "Can not load library %1. %2" ).arg( libName ).arg( dlerror() );
258 crtInst = (CREATE_FUNC)dlsym( modLib, GET_MODULE_NAME );
260 err = QString( "Failed to find function %1. %2" ).arg( CREATE_MODULE ).arg( dlerror() );
264 XGUI_Module* aModule = crtInst ? crtInst(this) : 0;
266 if (!err.isEmpty()) {
267 if (mainWindow() && mainWindow()->isVisible())
268 QMessageBox::warning(mainWindow(), tr("Error"), err);
270 qWarning( qPrintable( err ));
275 //******************************************************
276 bool XGUI_Workshop::activateModule()
278 myPartSetModule = loadModule("PartSet");
279 if (!myPartSetModule)
281 myPartSetModule->createFeatures();