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.hxx>
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("menu_item");
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();
57 //******************************************************
58 void XGUI_Workshop::initMenu()
60 XGUI_Workbench* aPage = addWorkbench(tr("GEN_MENU_TITLE"));
62 // File commands group
63 XGUI_MenuGroupPanel* aGroup = aPage->addGroup("Default");
65 XGUI_Command* aCommand;
67 aCommand = aGroup->addFeature("SAVE_CMD", tr("SAVE_MENU"), tr("SAVE_MENU_TIP"),
68 QIcon(":pictures/save.png"), QKeySequence::Save);
69 aCommand->connectTo(this, SLOT(onSave()));
70 //aCommand->disable();
72 aCommand = aGroup->addFeature("UNDO_CMD", tr("UNDO_MENU"), tr("UNDO_MENU_TIP"),
73 QIcon(":pictures/undo.png"), QKeySequence::Undo);
75 aCommand = aGroup->addFeature("REDO_CMD", tr("REDO_MENU"), tr("REDO_MENU_TIP"),
76 QIcon(":pictures/redo.png"), QKeySequence::Redo);
78 aCommand = aGroup->addFeature("REBUILD_CMD", tr("REBUILD_MENU"), tr("REBUILD_MENU_TIP"),
79 QIcon(":pictures/rebuild.png"));
81 aCommand = aGroup->addFeature("SAVEAS_CMD", tr("SAVEAS_MENU"), tr("SAVEAS_MENU_TIP"),
82 QIcon(":pictures/save.png"));
83 aCommand->connectTo(this, SLOT(onSaveAs()));
84 //aCommand->disable();
86 aCommand = aGroup->addFeature("OPEN_CMD", tr("OPEN_MENU"), tr("OPEN_MENU_TIP"),
87 QIcon(":pictures/open.png"), QKeySequence::Open);
88 aCommand->connectTo(this, SLOT(onOpen()));
90 aCommand = aGroup->addFeature("NEW_CMD", tr("NEW_MENU"), tr("NEW_MENU_TIP"),
91 QIcon(":pictures/new.png"), QKeySequence::New);
92 aCommand->connectTo(this, SLOT(onNew()));
94 aCommand = aGroup->addFeature("EXIT_CMD", tr("EXIT_MENU"), tr("EXIT_MENU_TIP"),
95 QIcon(":pictures/close.png"), QKeySequence::Close);
96 aCommand->connectTo(this, SLOT(onExit()));
100 //******************************************************
101 XGUI_Workbench* XGUI_Workshop::addWorkbench(const QString& theName)
103 XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
104 return aMenuBar->addWorkbench(theName);
107 //******************************************************
108 void XGUI_Workshop::ProcessEvent(const Event_Message* theMessage)
110 const Config_FeatureMessage* aFeatureMsg = dynamic_cast<const Config_FeatureMessage*>(theMessage);
112 addFeature(aFeatureMsg);
116 qDebug() << "XGUI_Workshop::ProcessEvent: "
117 << "Catch message, but it can not be processed.";
125 void XGUI_Workshop::addFeature(const Config_FeatureMessage* theMessage)
129 qDebug() << "XGUI_Workshop::addFeature: NULL message.";
133 //Find or create Workbench
134 XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
135 QString aWchName = QString::fromStdString(theMessage->workbenchId());
136 XGUI_Workbench* aPage = aMenuBar->findWorkbench(aWchName);
138 aPage = addWorkbench(aWchName);
140 //Find or create Group
141 QString aGroupName = QString::fromStdString(theMessage->groupId());
142 XGUI_MenuGroupPanel* aGroup = aPage->findGroup(aGroupName);
144 aGroup = aPage->addGroup(aGroupName);
147 QString aFeatureId = QString::fromStdString(theMessage->id());
148 XGUI_Command* aCommand = aGroup->addFeature(QString::fromStdString(theMessage->id()),
149 QString::fromStdString(theMessage->text()),
150 QString::fromStdString(theMessage->tooltip()),
151 QIcon(theMessage->icon().c_str())
152 //TODO(sbh): QKeySequence
154 myPartSetModule->featureCreated(aCommand);
157 //******************************************************
158 void XGUI_Workshop::onExit()
163 //******************************************************
164 void XGUI_Workshop::onNew()
166 myMainWindow->showObjectBrowser();
169 //******************************************************
170 void XGUI_Workshop::onOpen()
172 QString aFileName = QFileDialog::getOpenFileName(mainWindow());
175 //******************************************************
176 void XGUI_Workshop::onSave()
180 //******************************************************
181 void XGUI_Workshop::onSaveAs()
183 QString aFileName = QFileDialog::getSaveFileName(mainWindow());
186 //******************************************************
187 XGUI_Module* XGUI_Workshop::loadModule(const QString& theModule)
189 QString libName = library(theModule);
190 if (libName.isEmpty()) {
192 qPrintable( tr( "Information about module \"%1\" doesn't exist." ).arg( theModule ) ));
197 CREATE_FUNC crtInst = 0;
201 HINSTANCE modLib = ::LoadLibrary((LPTSTR) qPrintable(libName));
205 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
206 0, ::GetLastError(), 0, (LPTSTR) & lpMsgBuf, 0, 0);
207 QString aMsg((char*) &lpMsgBuf);
208 err = QString("Failed to load %1. %2").arg(libName).arg(aMsg);
209 ::LocalFree(lpMsgBuf);
211 crtInst = (CREATE_FUNC) ::GetProcAddress(modLib, CREATE_MODULE);
215 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM
216 | FORMAT_MESSAGE_IGNORE_INSERTS,
217 0, ::GetLastError(), 0, (LPTSTR) & lpMsgBuf, 0, 0);
218 QString aMsg((char*) &lpMsgBuf);
219 err = QString("Failed to find %1 function. %2").arg( CREATE_MODULE).arg(aMsg);
220 ::LocalFree(lpMsgBuf);
224 void* modLib = dlopen( libName.toLatin1(), RTLD_LAZY );
226 err = QString( "Can not load library %1. %2" ).arg( libName ).arg( dlerror() );
229 crtInst = (CREATE_FUNC)dlsym( modLib, GET_MODULE_NAME );
231 err = QString( "Failed to find function %1. %2" ).arg( CREATE_MODULE ).arg( dlerror() );
235 XGUI_Module* aModule = crtInst ? crtInst(this) : 0;
237 if (!err.isEmpty()) {
238 if (mainWindow() && mainWindow()->isVisible())
239 QMessageBox::warning(mainWindow(), tr("Error"), err);
241 qWarning( qPrintable( err ));
246 //******************************************************
247 bool XGUI_Workshop::activateModule()
249 myPartSetModule = loadModule("PartSet");
250 if (!myPartSetModule)
252 myPartSetModule->createFeatures();