1 #include "XGUI_Module.h"
2 #include "XGUI_Constants.h"
3 #include "XGUI_Command.h"
4 #include "XGUI_MainMenu.h"
5 #include "XGUI_MainWindow.h"
6 #include "XGUI_MenuGroupPanel.h"
7 #include "XGUI_Tools.h"
8 #include "XGUI_Workbench.h"
9 #include "XGUI_Workshop.h"
10 #include "XGUI_Viewer.h"
11 #include "XGUI_WidgetFactory.h"
13 #include <ModelAPI_PluginManager.h>
14 #include <ModelAPI_Feature.h>
15 #include <ModelAPI_Object.h>
16 #include <ModelAPI_AttributeDocRef.h>
18 #include <Event_Loop.h>
19 #include <ModuleBase_Operation.h>
20 #include <Config_FeatureMessage.h>
21 #include <Config_PointerMessage.h>
23 #include <QApplication>
24 #include <QFileDialog>
25 #include <QMessageBox>
26 #include <QMdiSubWindow>
27 #include <QPushButton>
28 #include <QDockWidget>
40 XGUI_Workshop::XGUI_Workshop()
42 myCurrentOperation(NULL),
45 myMainWindow = new XGUI_MainWindow();
48 //******************************************************
49 XGUI_Workshop::~XGUI_Workshop(void)
53 //******************************************************
54 void XGUI_Workshop::startApplication()
57 //Initialize event listening
58 Event_Loop* aLoop = Event_Loop::loop();
59 //TODO(sbh): Implement static method to extract event id [SEID]
60 Event_ID aFeatureId = aLoop->eventByName("FeatureEvent");
61 aLoop->registerListener(this, aFeatureId);
62 Event_ID aPartSetId = aLoop->eventByName("PartSetModuleEvent");
63 aLoop->registerListener(this, aPartSetId);
66 QMdiSubWindow* aWnd = myMainWindow->viewer()->createView();
67 aWnd->showMaximized();
68 myMainWindow->showPythonConsole();
70 // Testing of document creation
71 std::shared_ptr<ModelAPI_PluginManager> aMgr = ModelAPI_PluginManager::get();
72 std::shared_ptr<ModelAPI_Feature> aPoint1 = aMgr->rootDocument()->addFeature("Point");
73 std::shared_ptr<ModelAPI_Feature> aPart = aMgr->rootDocument()->addFeature("Part");
75 aMgr->setCurrentDocument(aPart->data()->docRef("PartDocument")->value());
76 std::shared_ptr<ModelAPI_Feature> aPoint2 = aMgr->rootDocument()->addFeature("Point");
77 aPoint2 = aMgr->rootDocument()->addFeature("Point");
79 aPart = aMgr->rootDocument()->addFeature("Part");
83 //******************************************************
84 void XGUI_Workshop::initMenu()
86 XGUI_Workbench* aPage = myMainWindow->menuObject()->generalPage();
88 // File commands group
89 XGUI_MenuGroupPanel* aGroup = aPage->addGroup("Default");
91 XGUI_Command* aCommand;
93 aCommand = aGroup->addFeature("SAVE_CMD", tr("Save..."), tr("Save the document"),
94 QIcon(":pictures/save.png"), QKeySequence::Save);
95 aCommand->connectTo(this, SLOT(onSave()));
96 //aCommand->disable();
98 aCommand = aGroup->addFeature("UNDO_CMD", tr("Undo"), tr("Undo last command"),
99 QIcon(":pictures/undo.png"), QKeySequence::Undo);
101 aCommand = aGroup->addFeature("REDO_CMD", tr("Redo"), tr("Redo last command"),
102 QIcon(":pictures/redo.png"), QKeySequence::Redo);
104 aCommand = aGroup->addFeature("REBUILD_CMD", tr("Rebuild"), tr("Rebuild data objects"),
105 QIcon(":pictures/rebuild.png"));
107 aCommand = aGroup->addFeature("SAVEAS_CMD", tr("Save as..."), tr("Save the document into a file"),
108 QIcon(":pictures/save.png"));
109 aCommand->connectTo(this, SLOT(onSaveAs()));
110 //aCommand->disable();
112 aCommand = aGroup->addFeature("OPEN_CMD", tr("Open..."), tr("Open a new document"),
113 QIcon(":pictures/open.png"), QKeySequence::Open);
114 aCommand->connectTo(this, SLOT(onOpen()));
116 aCommand = aGroup->addFeature("NEW_CMD", tr("New"), tr("Create a new document"),
117 QIcon(":pictures/new.png"), QKeySequence::New);
118 aCommand->connectTo(this, SLOT(onNew()));
120 aCommand = aGroup->addFeature("EXIT_CMD", tr("Exit"), tr("Exit application"),
121 QIcon(":pictures/close.png"), QKeySequence::Close);
122 aCommand->connectTo(this, SLOT(onExit()));
126 //******************************************************
127 XGUI_Workbench* XGUI_Workshop::addWorkbench(const QString& theName)
129 XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
130 return aMenuBar->addWorkbench(theName);
133 //******************************************************
134 void XGUI_Workshop::processEvent(const Event_Message* theMessage)
136 const Config_FeatureMessage* aFeatureMsg =
137 dynamic_cast<const Config_FeatureMessage*>(theMessage);
139 addFeature(aFeatureMsg);
142 const Config_PointerMessage* aPartSetMsg =
143 dynamic_cast<const Config_PointerMessage*>(theMessage);
145 ModuleBase_Operation* aOperation = (ModuleBase_Operation*)(aPartSetMsg->pointer());
146 setCurrentOperation(aOperation);
147 if(aOperation->xmlRepresentation().isEmpty()) { //!< No need for property panel
148 myCurrentOperation->start();
149 myCurrentOperation->commit();
151 fillPropertyPanel(aOperation);
157 qDebug() << "XGUI_Workshop::ProcessEvent: "
158 << "Catch message, but it can not be processed.";
166 void XGUI_Workshop::addFeature(const Config_FeatureMessage* theMessage)
170 qDebug() << "XGUI_Workshop::addFeature: NULL message.";
174 //Find or create Workbench
175 XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
176 QString aWchName = QString::fromStdString(theMessage->workbenchId());
177 XGUI_Workbench* aPage = aMenuBar->findWorkbench(aWchName);
179 aPage = addWorkbench(aWchName);
181 //Find or create Group
182 QString aGroupName = QString::fromStdString(theMessage->groupId());
183 XGUI_MenuGroupPanel* aGroup = aPage->findGroup(aGroupName);
185 aGroup = aPage->addGroup(aGroupName);
188 QString aFeatureId = QString::fromStdString(theMessage->id());
189 XGUI_Command* aCommand = aGroup->addFeature(QString::fromStdString(theMessage->id()),
190 QString::fromStdString(theMessage->text()),
191 QString::fromStdString(theMessage->tooltip()),
192 QIcon(theMessage->icon().c_str())
193 //TODO(sbh): QKeySequence
195 myPartSetModule->featureCreated(aCommand);
201 void XGUI_Workshop::fillPropertyPanel(ModuleBase_Operation* theOperation)
203 connectToPropertyPanel(theOperation);
204 QWidget* aPropWidget = myMainWindow->findChild<QWidget*>(XGUI::PROP_PANEL_WDG);
205 qDeleteAll(aPropWidget->children());
206 theOperation->start();
207 XGUI_WidgetFactory aFactory = XGUI_WidgetFactory(theOperation);
208 aFactory.fillWidget(aPropWidget);
211 void XGUI_Workshop::setCurrentOperation(ModuleBase_Operation* theOperation)
213 //FIXME: Ask user about aborting of current operation?
214 if (myCurrentOperation) {
215 //TODO get isOperation from document
216 if (myCurrentOperation->isRunning())
217 myCurrentOperation->abort();
219 myCurrentOperation->deleteLater();
221 myCurrentOperation = theOperation;
225 * Makes a signal/slot connections between Property Panel
226 * and given operation. The given operation becomes a
227 * current operation and previous operation if exists
229 void XGUI_Workshop::connectToPropertyPanel(ModuleBase_Operation* theOperation)
231 QDockWidget* aPanel = myMainWindow->findChild<QDockWidget*>(XGUI::PROP_PANEL);
232 QPushButton* aOkBtn = aPanel->findChild<QPushButton*>(XGUI::PROP_PANEL_OK);
233 connect(aOkBtn, SIGNAL(clicked()), theOperation, SLOT(commit()));
234 QPushButton* aCancelBtn = aPanel->findChild<QPushButton*>(XGUI::PROP_PANEL_CANCEL);
235 connect(aCancelBtn, SIGNAL(clicked()), theOperation, SLOT(abort()));
237 connect(theOperation, SIGNAL(started()), myMainWindow, SLOT(showPropertyPanel()));
238 connect(theOperation, SIGNAL(stopped()), myMainWindow, SLOT(hidePropertyPanel()));
241 //******************************************************
242 void XGUI_Workshop::onExit()
247 //******************************************************
248 void XGUI_Workshop::onNew()
250 myMainWindow->showObjectBrowser();
253 //******************************************************
254 void XGUI_Workshop::onOpen()
256 QString aFileName = QFileDialog::getOpenFileName(mainWindow());
259 //******************************************************
260 void XGUI_Workshop::onSave()
264 //******************************************************
265 void XGUI_Workshop::onSaveAs()
267 QString aFileName = QFileDialog::getSaveFileName(mainWindow());
270 //******************************************************
271 XGUI_Module* XGUI_Workshop::loadModule(const QString& theModule)
273 QString libName = library(theModule);
274 if (libName.isEmpty()) {
276 qPrintable( tr( "Information about module \"%1\" doesn't exist." ).arg( theModule ) ));
281 CREATE_FUNC crtInst = 0;
285 HINSTANCE modLib = ::LoadLibrary((LPTSTR) qPrintable(libName));
289 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
290 0, ::GetLastError(), 0, (LPTSTR) & lpMsgBuf, 0, 0);
291 QString aMsg((char*) &lpMsgBuf);
292 err = QString("Failed to load %1. %2").arg(libName).arg(aMsg);
293 ::LocalFree(lpMsgBuf);
295 crtInst = (CREATE_FUNC) ::GetProcAddress(modLib, CREATE_MODULE);
299 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM
300 | FORMAT_MESSAGE_IGNORE_INSERTS,
301 0, ::GetLastError(), 0, (LPTSTR) & lpMsgBuf, 0, 0);
302 QString aMsg((char*) &lpMsgBuf);
303 err = QString("Failed to find %1 function. %2").arg( CREATE_MODULE).arg(aMsg);
304 ::LocalFree(lpMsgBuf);
308 void* modLib = dlopen( libName.toLatin1(), RTLD_LAZY );
310 err = QString( "Can not load library %1. %2" ).arg( libName ).arg( dlerror() );
313 crtInst = (CREATE_FUNC)dlsym( modLib, GET_MODULE_NAME );
315 err = QString( "Failed to find function %1. %2" ).arg( CREATE_MODULE ).arg( dlerror() );
319 XGUI_Module* aModule = crtInst ? crtInst(this) : 0;
321 if (!err.isEmpty()) {
322 if (mainWindow() && mainWindow()->isVisible())
323 QMessageBox::warning(mainWindow(), tr("Error"), err);
325 qWarning( qPrintable( err ));
330 //******************************************************
331 bool XGUI_Workshop::activateModule()
333 myPartSetModule = loadModule("PartSet");
334 if (!myPartSetModule)
336 myPartSetModule->createFeatures();