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>
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 Event_ID aFeatureId = aLoop->EventByName("menu_item");
50 aLoop->RegisterListener(this, aFeatureId);
51 Event_ID aPartSetId = aLoop->EventByName("partset_module");
52 aLoop->RegisterListener(this, aPartSetId);
55 QMdiSubWindow* aWnd = myMainWindow->viewer()->createView();
56 aWnd->showMaximized();
59 //******************************************************
60 void XGUI_Workshop::initMenu()
62 XGUI_Workbench* aPage = addWorkbench(tr("GEN_MENU_TITLE"));
64 // File commands group
65 XGUI_MenuGroupPanel* aGroup = aPage->addGroup("Default");
67 XGUI_Command* aCommand;
69 aCommand = aGroup->addFeature("SAVE_CMD", tr("SAVE_MENU"), tr("SAVE_MENU_TIP"),
70 QIcon(":pictures/save.png"), QKeySequence::Save);
71 aCommand->connectTo(this, SLOT(onSave()));
72 //aCommand->disable();
74 aCommand = aGroup->addFeature("UNDO_CMD", tr("UNDO_MENU"), tr("UNDO_MENU_TIP"),
75 QIcon(":pictures/undo.png"), QKeySequence::Undo);
77 aCommand = aGroup->addFeature("REDO_CMD", tr("REDO_MENU"), tr("REDO_MENU_TIP"),
78 QIcon(":pictures/redo.png"), QKeySequence::Redo);
80 aCommand = aGroup->addFeature("REBUILD_CMD", tr("REBUILD_MENU"), tr("REBUILD_MENU_TIP"),
81 QIcon(":pictures/rebuild.png"));
83 aCommand = aGroup->addFeature("SAVEAS_CMD", tr("SAVEAS_MENU"), tr("SAVEAS_MENU_TIP"),
84 QIcon(":pictures/save.png"));
85 aCommand->connectTo(this, SLOT(onSaveAs()));
86 //aCommand->disable();
88 aCommand = aGroup->addFeature("OPEN_CMD", tr("OPEN_MENU"), tr("OPEN_MENU_TIP"),
89 QIcon(":pictures/open.png"), QKeySequence::Open);
90 aCommand->connectTo(this, SLOT(onOpen()));
93 aCommand = aGroup->addFeature("NEW_CMD", tr("NEW_MENU"), tr("NEW_MENU_TIP"),
94 QIcon(":pictures/new.png"), QKeySequence::New);
95 aCommand->connectTo(this, SLOT(onNew()));
97 aCommand = aGroup->addFeature("EXIT_CMD", tr("EXIT_MENU"), tr("EXIT_MENU_TIP"),
98 QIcon(":pictures/close.png"), QKeySequence::Close);
99 aCommand->connectTo(this, SLOT(onExit()));
103 //******************************************************
104 XGUI_Workbench* XGUI_Workshop::addWorkbench(const QString& theName)
106 XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
107 return aMenuBar->addWorkbench(theName);
110 //******************************************************
111 void XGUI_Workshop::ProcessEvent(const Event_Message* theMessage)
113 const Config_FeatureMessage* aFeatureMsg =
114 dynamic_cast<const Config_FeatureMessage*>( theMessage );
116 addFeature(aFeatureMsg);
120 qDebug() << "XGUI_Workshop::ProcessEvent: "
121 << "Catch message, but it can not be processed.";
129 void XGUI_Workshop::addFeature(const Config_FeatureMessage* theMessage)
133 qDebug() << "XGUI_Workshop::addFeature: NULL message.";
137 //Find or create Workbench
138 XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
139 QString aWchName = QString::fromStdString(theMessage->workbenchId());
140 XGUI_Workbench* aPage = aMenuBar->findWorkbench(aWchName);
142 aPage = addWorkbench(aWchName);
144 //Find or create Group
145 QString aGroupName = QString::fromStdString(theMessage->groupId());
146 XGUI_MenuGroupPanel* aGroup = aPage->findGroup(aGroupName);
148 aGroup = aPage->addGroup(aGroupName);
151 QString aFeatureId = QString::fromStdString(theMessage->id());
152 XGUI_Command* aCommand = aGroup->addFeature(
153 QString::fromStdString(theMessage->id()),
154 QString::fromStdString(theMessage->text()),
155 QString::fromStdString(theMessage->tooltip()),
156 QIcon(theMessage->icon().c_str())
157 //TODO(sbh): QKeySequence
159 myPartSetModule->featureCreated(aCommand);
162 //******************************************************
163 void XGUI_Workshop::onExit()
168 //******************************************************
169 void XGUI_Workshop::onNew()
171 myMainWindow->showObjectBrowser();
174 //******************************************************
175 void XGUI_Workshop::onOpen()
177 QString aFileName = QFileDialog::getOpenFileName(mainWindow());
180 //******************************************************
181 void XGUI_Workshop::onSave()
185 //******************************************************
186 void XGUI_Workshop::onSaveAs()
188 QString aFileName = QFileDialog::getSaveFileName(mainWindow());
191 //******************************************************
192 XGUI_Module* XGUI_Workshop::loadModule(const QString& theModule)
194 QString libName = library( theModule );
195 if ( libName.isEmpty() )
197 qWarning( qPrintable( tr( "Information about module \"%1\" doesn't exist." ).arg( theModule ) ) );
202 CREATE_FUNC crtInst = 0;
206 HINSTANCE modLib = ::LoadLibrary( (LPTSTR) qPrintable(libName) );
210 ::FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
211 FORMAT_MESSAGE_IGNORE_INSERTS, 0, ::GetLastError(), 0, (LPTSTR)&lpMsgBuf, 0, 0 );
212 QString aMsg((char*)&lpMsgBuf);
213 err = QString( "Failed to load %1. %2" ).arg( libName ).arg( aMsg );
214 ::LocalFree( lpMsgBuf );
218 crtInst = (CREATE_FUNC)::GetProcAddress( modLib, CREATE_MODULE );
222 ::FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
223 FORMAT_MESSAGE_IGNORE_INSERTS, 0, ::GetLastError(), 0, (LPTSTR)&lpMsgBuf, 0, 0 );
224 QString aMsg((char*)&lpMsgBuf);
225 err = QString( "Failed to find %1 function. %2" ).arg( CREATE_MODULE ).arg(aMsg );
226 ::LocalFree( lpMsgBuf );
230 void* modLib = dlopen( libName.toLatin1(), RTLD_LAZY );
232 err = QString( "Can not load library %1. %2" ).arg( libName ).arg( dlerror() );
235 crtInst = (CREATE_FUNC)dlsym( modLib, GET_MODULE_NAME );
237 err = QString( "Failed to find function %1. %2" ).arg( CREATE_MODULE ).arg( dlerror() );
241 XGUI_Module* aModule = crtInst ? crtInst(this) : 0;
243 if ( !err.isEmpty() ) {
244 if ( mainWindow() && mainWindow()->isVisible() )
245 QMessageBox::warning( mainWindow(), tr( "Error" ), err );
247 qWarning( qPrintable( err ) );
252 //******************************************************
253 bool XGUI_Workshop::activateModule()
255 myPartSetModule = loadModule("PartSet");
256 if (!myPartSetModule)
258 myPartSetModule->createFeatures();