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 <ModelAPI_PluginManager.h>
13 #include <ModelAPI_Feature.h>
15 #include <Event_Loop.h>
16 #include <Config_FeatureMessage.h>
17 #include <Config_WidgetMessage.h>
19 #include <QApplication>
20 #include <QFileDialog>
21 #include <QMessageBox>
22 #include <QMdiSubWindow>
34 XGUI_Workshop::XGUI_Workshop()
37 myMainWindow = new XGUI_MainWindow();
38 myPartSetModule = NULL;
41 //******************************************************
42 XGUI_Workshop::~XGUI_Workshop(void)
46 //******************************************************
47 void XGUI_Workshop::startApplication()
50 //Initialize event listening
51 Event_Loop* aLoop = Event_Loop::loop();
52 //TODO(sbh): Implement static method to extract event id [SEID]
53 Event_ID aFeatureId = aLoop->eventByName("FeatureEvent");
54 aLoop->registerListener(this, aFeatureId);
55 Event_ID aPartSetId = aLoop->eventByName("PartSetModuleEvent");
56 aLoop->registerListener(this, aPartSetId);
59 QMdiSubWindow* aWnd = myMainWindow->viewer()->createView();
60 aWnd->showMaximized();
61 myMainWindow->showPythonConsole();
63 std::shared_ptr<ModelAPI_PluginManager> aMgr = ModelAPI_PluginManager::get();
64 std::shared_ptr<ModelAPI_Feature> myRoot = aMgr->createFeature("Point");
67 //******************************************************
68 void XGUI_Workshop::initMenu()
70 XGUI_Workbench* aPage = myMainWindow->menuObject()->generalPage();
72 // File commands group
73 XGUI_MenuGroupPanel* aGroup = aPage->addGroup("Default");
75 XGUI_Command* aCommand;
77 aCommand = aGroup->addFeature("SAVE_CMD", tr("Save..."), tr("Save the document"),
78 QIcon(":pictures/save.png"), QKeySequence::Save);
79 aCommand->connectTo(this, SLOT(onSave()));
80 //aCommand->disable();
82 aCommand = aGroup->addFeature("UNDO_CMD", tr("Undo"), tr("Undo last command"),
83 QIcon(":pictures/undo.png"), QKeySequence::Undo);
85 aCommand = aGroup->addFeature("REDO_CMD", tr("Redo"), tr("Redo last command"),
86 QIcon(":pictures/redo.png"), QKeySequence::Redo);
88 aCommand = aGroup->addFeature("REBUILD_CMD", tr("Rebuild"), tr("Rebuild data objects"),
89 QIcon(":pictures/rebuild.png"));
91 aCommand = aGroup->addFeature("SAVEAS_CMD", tr("Save as..."), tr("Save the document into a file"),
92 QIcon(":pictures/save.png"));
93 aCommand->connectTo(this, SLOT(onSaveAs()));
94 //aCommand->disable();
96 aCommand = aGroup->addFeature("OPEN_CMD", tr("Open..."), tr("Open a new document"),
97 QIcon(":pictures/open.png"), QKeySequence::Open);
98 aCommand->connectTo(this, SLOT(onOpen()));
100 aCommand = aGroup->addFeature("NEW_CMD", tr("New"), tr("Create a new document"),
101 QIcon(":pictures/new.png"), QKeySequence::New);
102 aCommand->connectTo(this, SLOT(onNew()));
104 aCommand = aGroup->addFeature("EXIT_CMD", tr("Exit"), tr("Exit application"),
105 QIcon(":pictures/close.png"), QKeySequence::Close);
106 aCommand->connectTo(this, SLOT(onExit()));
110 //******************************************************
111 XGUI_Workbench* XGUI_Workshop::addWorkbench(const QString& theName)
113 XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
114 return aMenuBar->addWorkbench(theName);
117 //******************************************************
118 void XGUI_Workshop::processEvent(const Event_Message* theMessage)
120 const Config_FeatureMessage* aFeatureMsg =
121 dynamic_cast<const Config_FeatureMessage*>(theMessage);
123 addFeature(aFeatureMsg);
126 const Config_WidgetMessage* aPartSetMsg = dynamic_cast<const Config_WidgetMessage*>(theMessage);
128 fillPropertyPanel(aPartSetMsg);
133 qDebug() << "XGUI_Workshop::ProcessEvent: "
134 << "Catch message, but it can not be processed.";
142 void XGUI_Workshop::addFeature(const Config_FeatureMessage* theMessage)
146 qDebug() << "XGUI_Workshop::addFeature: NULL message.";
150 //Find or create Workbench
151 XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
152 QString aWchName = QString::fromStdString(theMessage->workbenchId());
153 XGUI_Workbench* aPage = aMenuBar->findWorkbench(aWchName);
155 aPage = addWorkbench(aWchName);
157 //Find or create Group
158 QString aGroupName = QString::fromStdString(theMessage->groupId());
159 XGUI_MenuGroupPanel* aGroup = aPage->findGroup(aGroupName);
161 aGroup = aPage->addGroup(aGroupName);
164 QString aFeatureId = QString::fromStdString(theMessage->id());
165 XGUI_Command* aCommand = aGroup->addFeature(QString::fromStdString(theMessage->id()),
166 QString::fromStdString(theMessage->text()),
167 QString::fromStdString(theMessage->tooltip()),
168 QIcon(theMessage->icon().c_str())
169 //TODO(sbh): QKeySequence
171 myPartSetModule->featureCreated(aCommand);
177 void XGUI_Workshop::fillPropertyPanel(const Config_WidgetMessage* theMessage)
179 QWidget* aPropWidget = myMainWindow->findChild<QWidget*>("PropertyPanelWidget");
182 qDebug() << "XGUI_Workshop::fillPropertyPanel: " << "Unable to find property panel";
186 qDeleteAll(aPropWidget->children());
187 std::string aXml = theMessage->xmlRepresentation();
188 XGUI_WidgetFactory aFactory = XGUI_WidgetFactory(aXml);
189 aFactory.fillWidget(aPropWidget);
192 //******************************************************
193 void XGUI_Workshop::onExit()
198 //******************************************************
199 void XGUI_Workshop::onNew()
201 myMainWindow->showObjectBrowser();
204 //******************************************************
205 void XGUI_Workshop::onOpen()
207 QString aFileName = QFileDialog::getOpenFileName(mainWindow());
210 //******************************************************
211 void XGUI_Workshop::onSave()
215 //******************************************************
216 void XGUI_Workshop::onSaveAs()
218 QString aFileName = QFileDialog::getSaveFileName(mainWindow());
221 //******************************************************
222 XGUI_Module* XGUI_Workshop::loadModule(const QString& theModule)
224 QString libName = library(theModule);
225 if (libName.isEmpty()) {
227 qPrintable( tr( "Information about module \"%1\" doesn't exist." ).arg( theModule ) ));
232 CREATE_FUNC crtInst = 0;
236 HINSTANCE modLib = ::LoadLibrary((LPTSTR) qPrintable(libName));
240 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
241 0, ::GetLastError(), 0, (LPTSTR) & lpMsgBuf, 0, 0);
242 QString aMsg((char*) &lpMsgBuf);
243 err = QString("Failed to load %1. %2").arg(libName).arg(aMsg);
244 ::LocalFree(lpMsgBuf);
246 crtInst = (CREATE_FUNC) ::GetProcAddress(modLib, CREATE_MODULE);
250 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM
251 | FORMAT_MESSAGE_IGNORE_INSERTS,
252 0, ::GetLastError(), 0, (LPTSTR) & lpMsgBuf, 0, 0);
253 QString aMsg((char*) &lpMsgBuf);
254 err = QString("Failed to find %1 function. %2").arg( CREATE_MODULE).arg(aMsg);
255 ::LocalFree(lpMsgBuf);
259 void* modLib = dlopen( libName.toLatin1(), RTLD_LAZY );
261 err = QString( "Can not load library %1. %2" ).arg( libName ).arg( dlerror() );
264 crtInst = (CREATE_FUNC)dlsym( modLib, GET_MODULE_NAME );
266 err = QString( "Failed to find function %1. %2" ).arg( CREATE_MODULE ).arg( dlerror() );
270 XGUI_Module* aModule = crtInst ? crtInst(this) : 0;
272 if (!err.isEmpty()) {
273 if (mainWindow() && mainWindow()->isVisible())
274 QMessageBox::warning(mainWindow(), tr("Error"), err);
276 qWarning( qPrintable( err ));
281 //******************************************************
282 bool XGUI_Workshop::activateModule()
284 myPartSetModule = loadModule("PartSet");
285 if (!myPartSetModule)
287 myPartSetModule->createFeatures();