1 #include "XGUI_Workshop.h"
2 #include "XGUI_MainWindow.h"
3 #include "XGUI_MainMenu.h"
4 #include "XGUI_Command.h"
5 #include "XGUI_Tools.h"
7 #include <Config_Message.h>
8 #include <Event_Loop.hxx>
10 #include <QApplication>
11 #include <QFileDialog>
12 #include <QMessageBox>
27 XGUI_Workshop::XGUI_Workshop() :
30 myMainWindow = new XGUI_MainWindow();
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("Feature");
45 aLoop->RegisterListener(this, aFeatureId);
50 //******************************************************
51 void XGUI_Workshop::initMenu()
53 IWorkbench* aPage = addWorkbench(tr("GEN_MENU_TITLE"));
55 // File commands group
56 IMenuGroup* aGroup = aPage->addGroup();
58 IFeatureMenu* aCommand;
60 aCommand = aGroup->addFeature("SAVE_CMD", tr("SAVE_MENU"), tr("SAVE_MENU_TIP"),
61 QIcon(":pictures/save.png"), QKeySequence::Save);
62 aCommand->connectTo(this, SLOT(onSave()));
63 //aCommand->disable();
65 aCommand = aGroup->addFeature("UNDO_CMD", tr("UNDO_MENU"), tr("UNDO_MENU_TIP"),
66 QIcon(":pictures/undo.png"), QKeySequence::Undo);
68 aCommand = aGroup->addFeature("REDO_CMD", tr("REDO_MENU"), tr("REDO_MENU_TIP"),
69 QIcon(":pictures/redo.png"), QKeySequence::Redo);
71 aCommand = aGroup->addFeature("REBUILD_CMD", tr("REBUILD_MENU"), tr("REBUILD_MENU_TIP"),
72 QIcon(":pictures/rebuild.png"));
74 aCommand = aGroup->addFeature("SAVEAS_CMD", tr("SAVEAS_MENU"), tr("SAVEAS_MENU_TIP"),
75 QIcon(":pictures/save.png"));
76 aCommand->connectTo(this, SLOT(onSaveAs()));
77 //aCommand->disable();
79 aCommand = aGroup->addFeature("OPEN_CMD", tr("OPEN_MENU"), tr("OPEN_MENU_TIP"),
80 QIcon(":pictures/open.png"), QKeySequence::Open);
81 aCommand->connectTo(this, SLOT(onOpen()));
84 aCommand = aGroup->addFeature("NEW_CMD", tr("NEW_MENU"), tr("NEW_MENU_TIP"),
85 QIcon(":pictures/new.png"), QKeySequence::New);
86 aCommand->connectTo(this, SLOT(onNew()));
88 aCommand = aGroup->addFeature("EXIT_CMD", tr("EXIT_MENU"), tr("EXIT_MENU_TIP"),
89 QIcon(":pictures/close.png"), QKeySequence::Close);
90 aCommand->connectTo(this, SLOT(onExit()));
94 //******************************************************
95 IWorkbench* XGUI_Workshop::addWorkbench(const QString& theName)
97 XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
98 return aMenuBar->addWorkbench(theName);
101 //******************************************************
102 void XGUI_Workshop::ProcessEvent(const Event_Message* theMessage)
104 const Config_FeatureMessage* aMsg =
105 dynamic_cast<const Config_FeatureMessage*>( theMessage );
111 qDebug() << "XGUI_Workshop::ProcessEvent: "
112 << "Got message, but it's not a Config_FeatureMessage";
117 void XGUI_Workshop::addFeature(const Config_FeatureMessage* theMessage)
119 XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
120 IWorkbench* aPage = aMenuBar->findWorkbench(tr( "GEN_MENU_TITLE" ) + "_Workbench");
123 qDebug() << "XGUI_Workshop::ProcessEvent: "
124 << "Creating a workbench " << tr( "GEN_MENU_TITLE" );
126 aPage = addWorkbench(tr( "GEN_MENU_TITLE" ));
128 QString aGroupName = QString::fromStdString(theMessage->m_group);
129 IMenuGroup* aGroup = aPage->findGroup(aGroupName);
131 aGroup = aPage->addGroup(aGroupName);
133 IFeatureMenu* aCommand;
134 aCommand = aGroup->addFeature(
135 QString::fromStdString(theMessage->m_id),
136 QString::fromStdString(theMessage->m_text),
137 QString::fromStdString(theMessage->m_tooltip),
138 QIcon(theMessage->m_icon.c_str())
139 //TODO(sbh): QKeySequence
143 //******************************************************
144 void XGUI_Workshop::onExit()
149 //******************************************************
150 void XGUI_Workshop::onNew()
152 myMainWindow->showObjectBrowser();
155 //******************************************************
156 void XGUI_Workshop::onOpen()
158 QString aFileName = QFileDialog::getOpenFileName(mainWindow());
161 //******************************************************
162 void XGUI_Workshop::onSave()
166 //******************************************************
167 void XGUI_Workshop::onSaveAs()
169 QString aFileName = QFileDialog::getSaveFileName(mainWindow());
172 //******************************************************
173 IModule* XGUI_Workshop::loadModule(const QString& theModule)
175 QString libName = library( theModule );
176 if ( libName.isEmpty() )
178 qWarning( qPrintable( tr( "Information about module \"%1\" doesn't exist." ).arg( theModule ) ) );
183 CREATE_FUNC crtInst = 0;
187 HINSTANCE modLib = ::LoadLibrary( (LPTSTR) qPrintable(libName) );
191 ::FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
192 FORMAT_MESSAGE_IGNORE_INSERTS, 0, ::GetLastError(), 0, (LPTSTR)&lpMsgBuf, 0, 0 );
193 QString aMsg((char*)&lpMsgBuf);
194 err = QString( "Failed to load %1. %2" ).arg( libName ).arg( aMsg );
195 ::LocalFree( lpMsgBuf );
199 crtInst = (CREATE_FUNC)::GetProcAddress( modLib, CREATE_MODULE );
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 find %1 function. %2" ).arg( CREATE_MODULE ).arg(aMsg );
207 ::LocalFree( lpMsgBuf );
211 void* modLib = dlopen( libName.toLatin1(), RTLD_LAZY );
213 err = QString( "Can not load library %1. %2" ).arg( libName ).arg( dlerror() );
216 crtInst = (CREATE_FUNC)dlsym( modLib, GET_MODULE_NAME );
218 err = QString( "Failed to find function %1. %2" ).arg( CREATE_MODULE ).arg( dlerror() );
222 IModule* aModule = crtInst ? crtInst(this) : 0;
224 if ( !err.isEmpty() ) {
225 if ( mainWindow() && mainWindow()->isVisible() )
226 QMessageBox::warning( mainWindow(), tr( "Error" ), err );
228 qWarning( qPrintable( err ) );
233 //******************************************************
234 bool XGUI_Workshop::activateModule()
236 // Test of modules loading
237 IModule* aModule = loadModule("GeomModule");
240 aModule->createFeatures();