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"
10 #include <Config_Message.h>
11 #include <Event_Loop.hxx>
13 #include <QApplication>
14 #include <QFileDialog>
15 #include <QMessageBox>
26 XGUI_Workshop::XGUI_Workshop() :
29 myMainWindow = new XGUI_MainWindow();
30 myPartSetModule = NULL;
33 //******************************************************
34 XGUI_Workshop::~XGUI_Workshop(void)
38 //******************************************************
39 void XGUI_Workshop::startApplication()
42 //Initialize event listening
43 Event_Loop* aLoop = Event_Loop::Loop();
44 Event_ID aFeatureId = aLoop->EventByName("menu_item");
45 aLoop->RegisterListener(this, aFeatureId);
46 Event_ID aPartSetId = aLoop->EventByName("partset_module");
47 aLoop->RegisterListener(this, aPartSetId);
52 //******************************************************
53 void XGUI_Workshop::initMenu()
55 XGUI_Workbench* aPage = addWorkbench(tr("GEN_MENU_TITLE"));
57 // File commands group
58 XGUI_MenuGroupPanel* aGroup = aPage->addGroup("Default");
60 XGUI_Command* aCommand;
62 aCommand = aGroup->addFeature("SAVE_CMD", tr("SAVE_MENU"), tr("SAVE_MENU_TIP"),
63 QIcon(":pictures/save.png"), QKeySequence::Save);
64 aCommand->connectTo(this, SLOT(onSave()));
65 //aCommand->disable();
67 aCommand = aGroup->addFeature("UNDO_CMD", tr("UNDO_MENU"), tr("UNDO_MENU_TIP"),
68 QIcon(":pictures/undo.png"), QKeySequence::Undo);
70 aCommand = aGroup->addFeature("REDO_CMD", tr("REDO_MENU"), tr("REDO_MENU_TIP"),
71 QIcon(":pictures/redo.png"), QKeySequence::Redo);
73 aCommand = aGroup->addFeature("REBUILD_CMD", tr("REBUILD_MENU"), tr("REBUILD_MENU_TIP"),
74 QIcon(":pictures/rebuild.png"));
76 aCommand = aGroup->addFeature("SAVEAS_CMD", tr("SAVEAS_MENU"), tr("SAVEAS_MENU_TIP"),
77 QIcon(":pictures/save.png"));
78 aCommand->connectTo(this, SLOT(onSaveAs()));
79 //aCommand->disable();
81 aCommand = aGroup->addFeature("OPEN_CMD", tr("OPEN_MENU"), tr("OPEN_MENU_TIP"),
82 QIcon(":pictures/open.png"), QKeySequence::Open);
83 aCommand->connectTo(this, SLOT(onOpen()));
86 aCommand = aGroup->addFeature("NEW_CMD", tr("NEW_MENU"), tr("NEW_MENU_TIP"),
87 QIcon(":pictures/new.png"), QKeySequence::New);
88 aCommand->connectTo(this, SLOT(onNew()));
90 aCommand = aGroup->addFeature("EXIT_CMD", tr("EXIT_MENU"), tr("EXIT_MENU_TIP"),
91 QIcon(":pictures/close.png"), QKeySequence::Close);
92 aCommand->connectTo(this, SLOT(onExit()));
96 //******************************************************
97 XGUI_Workbench* XGUI_Workshop::addWorkbench(const QString& theName)
99 XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
100 return aMenuBar->addWorkbench(theName);
103 //******************************************************
104 void XGUI_Workshop::ProcessEvent(const Event_Message* theMessage)
106 const Config_FeatureMessage* aFeatureMsg =
107 dynamic_cast<const Config_FeatureMessage*>( theMessage );
109 addFeature(aFeatureMsg);
113 qDebug() << "XGUI_Workshop::ProcessEvent: "
114 << "Catch message, but it can not be processed.";
122 void XGUI_Workshop::addFeature(const Config_FeatureMessage* theMessage)
126 qDebug() << "XGUI_Workshop::addFeature: NULL message.";
130 //Find or create Workbench
131 XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
132 QString aWchName = QString::fromStdString(theMessage->workbenchId());
133 XGUI_Workbench* aPage = aMenuBar->findWorkbench(aWchName);
135 aPage = addWorkbench(aWchName);
137 //Find or create Group
138 QString aGroupName = QString::fromStdString(theMessage->groupId());
139 XGUI_MenuGroupPanel* aGroup = aPage->findGroup(aGroupName);
141 aGroup = aPage->addGroup(aGroupName);
144 QString aFeatureId = QString::fromStdString(theMessage->id());
145 XGUI_Command* aCommand = aGroup->addFeature(
146 QString::fromStdString(theMessage->id()),
147 QString::fromStdString(theMessage->text()),
148 QString::fromStdString(theMessage->tooltip()),
149 QIcon(theMessage->icon().c_str())
150 //TODO(sbh): QKeySequence
152 myPartSetModule->featureCreated(aCommand);
155 //******************************************************
156 void XGUI_Workshop::onExit()
161 //******************************************************
162 void XGUI_Workshop::onNew()
164 myMainWindow->showObjectBrowser();
167 //******************************************************
168 void XGUI_Workshop::onOpen()
170 QString aFileName = QFileDialog::getOpenFileName(mainWindow());
173 //******************************************************
174 void XGUI_Workshop::onSave()
178 //******************************************************
179 void XGUI_Workshop::onSaveAs()
181 QString aFileName = QFileDialog::getSaveFileName(mainWindow());
184 //******************************************************
185 XGUI_Module* XGUI_Workshop::loadModule(const QString& theModule)
187 QString libName = library( theModule );
188 if ( libName.isEmpty() )
190 qWarning( qPrintable( tr( "Information about module \"%1\" doesn't exist." ).arg( theModule ) ) );
195 CREATE_FUNC crtInst = 0;
199 HINSTANCE modLib = ::LoadLibrary( (LPTSTR) qPrintable(libName) );
203 ::FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
204 FORMAT_MESSAGE_IGNORE_INSERTS, 0, ::GetLastError(), 0, (LPTSTR)&lpMsgBuf, 0, 0 );
205 QString aMsg((char*)&lpMsgBuf);
206 err = QString( "Failed to load %1. %2" ).arg( libName ).arg( aMsg );
207 ::LocalFree( lpMsgBuf );
211 crtInst = (CREATE_FUNC)::GetProcAddress( modLib, CREATE_MODULE );
215 ::FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
216 FORMAT_MESSAGE_IGNORE_INSERTS, 0, ::GetLastError(), 0, (LPTSTR)&lpMsgBuf, 0, 0 );
217 QString aMsg((char*)&lpMsgBuf);
218 err = QString( "Failed to find %1 function. %2" ).arg( CREATE_MODULE ).arg(aMsg );
219 ::LocalFree( lpMsgBuf );
223 void* modLib = dlopen( libName.toLatin1(), RTLD_LAZY );
225 err = QString( "Can not load library %1. %2" ).arg( libName ).arg( dlerror() );
228 crtInst = (CREATE_FUNC)dlsym( modLib, GET_MODULE_NAME );
230 err = QString( "Failed to find function %1. %2" ).arg( CREATE_MODULE ).arg( dlerror() );
234 XGUI_Module* aModule = crtInst ? crtInst(this) : 0;
236 if ( !err.isEmpty() ) {
237 if ( mainWindow() && mainWindow()->isVisible() )
238 QMessageBox::warning( mainWindow(), tr( "Error" ), err );
240 qWarning( qPrintable( err ) );
245 //******************************************************
246 bool XGUI_Workshop::activateModule()
248 myPartSetModule = loadModule("PartSet");
249 if (!myPartSetModule)
251 myPartSetModule->createFeatures();