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 <ModelAPI_PluginManager.h>
12 #include <ModelAPI_Feature.h>
14 #include <Config_FeatureMessage.h>
15 #include <Event_Loop.h>
17 #include <QApplication>
18 #include <QFileDialog>
19 #include <QMessageBox>
20 #include <QMdiSubWindow>
32 XGUI_Workshop::XGUI_Workshop()
35 myMainWindow = new XGUI_MainWindow();
36 myPartSetModule = NULL;
39 //******************************************************
40 XGUI_Workshop::~XGUI_Workshop(void)
44 //******************************************************
45 void XGUI_Workshop::startApplication()
48 //Initialize event listening
49 Event_Loop* aLoop = Event_Loop::loop();
50 Event_ID aFeatureId = aLoop->eventByName("RegisterFeature");
51 aLoop->registerListener(this, aFeatureId);
52 Event_ID aPartSetId = aLoop->eventByName("partset_module");
53 aLoop->registerListener(this, aPartSetId);
56 QMdiSubWindow* aWnd = myMainWindow->viewer()->createView();
57 aWnd->showMaximized();
58 myMainWindow->showPythonConsole();
60 std::shared_ptr<ModelAPI_PluginManager> aMgr = ModelAPI_PluginManager::get();
61 std::shared_ptr<ModelAPI_Feature> myRoot = aMgr->createFeature("Point");
64 //******************************************************
65 void XGUI_Workshop::initMenu()
67 XGUI_Workbench* aPage = myMainWindow->menuObject()->generalPage();
69 // File commands group
70 XGUI_MenuGroupPanel* aGroup = aPage->addGroup("Default");
72 XGUI_Command* aCommand;
74 aCommand = aGroup->addFeature("SAVE_CMD", tr("Save..."), tr("Save the document"),
75 QIcon(":pictures/save.png"), QKeySequence::Save);
76 aCommand->connectTo(this, SLOT(onSave()));
77 //aCommand->disable();
79 aCommand = aGroup->addFeature("UNDO_CMD", tr("Undo"), tr("Undo last command"),
80 QIcon(":pictures/undo.png"), QKeySequence::Undo);
82 aCommand = aGroup->addFeature("REDO_CMD", tr("Redo"), tr("Redo last command"),
83 QIcon(":pictures/redo.png"), QKeySequence::Redo);
85 aCommand = aGroup->addFeature("REBUILD_CMD", tr("Rebuild"), tr("Rebuild data objects"),
86 QIcon(":pictures/rebuild.png"));
88 aCommand = aGroup->addFeature("SAVEAS_CMD", tr("Save as..."), tr("Save the document into a file"),
89 QIcon(":pictures/save.png"));
90 aCommand->connectTo(this, SLOT(onSaveAs()));
91 //aCommand->disable();
93 aCommand = aGroup->addFeature("OPEN_CMD", tr("Open..."), tr("Open a new document"),
94 QIcon(":pictures/open.png"), QKeySequence::Open);
95 aCommand->connectTo(this, SLOT(onOpen()));
97 aCommand = aGroup->addFeature("NEW_CMD", tr("New"), tr("Create a new document"),
98 QIcon(":pictures/new.png"), QKeySequence::New);
99 aCommand->connectTo(this, SLOT(onNew()));
101 aCommand = aGroup->addFeature("EXIT_CMD", tr("Exit"), tr("Exit application"),
102 QIcon(":pictures/close.png"), QKeySequence::Close);
103 aCommand->connectTo(this, SLOT(onExit()));
107 //******************************************************
108 XGUI_Workbench* XGUI_Workshop::addWorkbench(const QString& theName)
110 XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
111 return aMenuBar->addWorkbench(theName);
114 //******************************************************
115 void XGUI_Workshop::processEvent(const Event_Message* theMessage)
117 const Config_FeatureMessage* aFeatureMsg = dynamic_cast<const Config_FeatureMessage*>(theMessage);
119 addFeature(aFeatureMsg);
123 qDebug() << "XGUI_Workshop::ProcessEvent: "
124 << "Catch message, but it can not be processed.";
132 void XGUI_Workshop::addFeature(const Config_FeatureMessage* theMessage)
136 qDebug() << "XGUI_Workshop::addFeature: NULL message.";
140 //Find or create Workbench
141 XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
142 QString aWchName = QString::fromStdString(theMessage->workbenchId());
143 XGUI_Workbench* aPage = aMenuBar->findWorkbench(aWchName);
145 aPage = addWorkbench(aWchName);
147 //Find or create Group
148 QString aGroupName = QString::fromStdString(theMessage->groupId());
149 XGUI_MenuGroupPanel* aGroup = aPage->findGroup(aGroupName);
151 aGroup = aPage->addGroup(aGroupName);
154 QString aFeatureId = QString::fromStdString(theMessage->id());
155 XGUI_Command* aCommand = aGroup->addFeature(QString::fromStdString(theMessage->id()),
156 QString::fromStdString(theMessage->text()),
157 QString::fromStdString(theMessage->tooltip()),
158 QIcon(theMessage->icon().c_str())
159 //TODO(sbh): QKeySequence
161 myPartSetModule->featureCreated(aCommand);
164 //******************************************************
165 void XGUI_Workshop::onExit()
170 //******************************************************
171 void XGUI_Workshop::onNew()
173 myMainWindow->showObjectBrowser();
176 //******************************************************
177 void XGUI_Workshop::onOpen()
179 QString aFileName = QFileDialog::getOpenFileName(mainWindow());
182 //******************************************************
183 void XGUI_Workshop::onSave()
187 //******************************************************
188 void XGUI_Workshop::onSaveAs()
190 QString aFileName = QFileDialog::getSaveFileName(mainWindow());
193 //******************************************************
194 XGUI_Module* XGUI_Workshop::loadModule(const QString& theModule)
196 QString libName = library(theModule);
197 if (libName.isEmpty()) {
199 qPrintable( tr( "Information about module \"%1\" doesn't exist." ).arg( theModule ) ));
204 CREATE_FUNC crtInst = 0;
208 HINSTANCE modLib = ::LoadLibrary((LPTSTR) qPrintable(libName));
212 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
213 0, ::GetLastError(), 0, (LPTSTR) & lpMsgBuf, 0, 0);
214 QString aMsg((char*) &lpMsgBuf);
215 err = QString("Failed to load %1. %2").arg(libName).arg(aMsg);
216 ::LocalFree(lpMsgBuf);
218 crtInst = (CREATE_FUNC) ::GetProcAddress(modLib, CREATE_MODULE);
222 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM
223 | FORMAT_MESSAGE_IGNORE_INSERTS,
224 0, ::GetLastError(), 0, (LPTSTR) & lpMsgBuf, 0, 0);
225 QString aMsg((char*) &lpMsgBuf);
226 err = QString("Failed to find %1 function. %2").arg( CREATE_MODULE).arg(aMsg);
227 ::LocalFree(lpMsgBuf);
231 void* modLib = dlopen( libName.toLatin1(), RTLD_LAZY );
233 err = QString( "Can not load library %1. %2" ).arg( libName ).arg( dlerror() );
236 crtInst = (CREATE_FUNC)dlsym( modLib, GET_MODULE_NAME );
238 err = QString( "Failed to find function %1. %2" ).arg( CREATE_MODULE ).arg( dlerror() );
242 XGUI_Module* aModule = crtInst ? crtInst(this) : 0;
244 if (!err.isEmpty()) {
245 if (mainWindow() && mainWindow()->isVisible())
246 QMessageBox::warning(mainWindow(), tr("Error"), err);
248 qWarning( qPrintable( err ));
253 //******************************************************
254 bool XGUI_Workshop::activateModule()
256 myPartSetModule = loadModule("PartSet");
257 if (!myPartSetModule)
259 myPartSetModule->createFeatures();