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"
12 #include "XGUI_SelectionMgr.h"
13 #include "XGUI_ObjectsBrowser.h"
15 #include <ModelAPI_PluginManager.h>
16 #include <ModelAPI_Feature.h>
17 #include <ModelAPI_Data.h>
18 #include <ModelAPI_AttributeDocRef.h>
20 #include <Event_Loop.h>
21 #include <ModuleBase_PropPanelOperation.h>
22 #include <Config_FeatureMessage.h>
23 #include <Config_PointerMessage.h>
25 #include <QApplication>
26 #include <QFileDialog>
27 #include <QMessageBox>
28 #include <QMdiSubWindow>
29 #include <QPushButton>
30 #include <QDockWidget>
42 XGUI_Workshop::XGUI_Workshop()
44 myCurrentOperation(NULL),
47 myMainWindow = new XGUI_MainWindow();
48 mySelector = new XGUI_SelectionMgr(this);
51 //******************************************************
52 XGUI_Workshop::~XGUI_Workshop(void)
56 //******************************************************
57 void XGUI_Workshop::startApplication()
60 //Initialize event listening
61 Event_Loop* aLoop = Event_Loop::loop();
62 //TODO(sbh): Implement static method to extract event id [SEID]
63 Event_ID aFeatureId = aLoop->eventByName("FeatureEvent");
64 aLoop->registerListener(this, aFeatureId);
65 Event_ID aPartSetId = aLoop->eventByName("PartSetModuleEvent");
66 aLoop->registerListener(this, aPartSetId);
70 updateCommandStatus();
72 // Testing of document creation
73 //std::shared_ptr<ModelAPI_PluginManager> aMgr = ModelAPI_PluginManager::get();
74 //std::shared_ptr<ModelAPI_Feature> aPoint1 = aMgr->rootDocument()->addFeature("Point");
75 //std::shared_ptr<ModelAPI_Feature> aPart = aMgr->rootDocument()->addFeature("Part");
77 //aMgr->setCurrentDocument(aPart->data()->docRef("PartDocument")->value());
78 //std::shared_ptr<ModelAPI_Feature> aPoint2 = aMgr->rootDocument()->addFeature("Point");
79 //aPoint2 = aMgr->rootDocument()->addFeature("Point");
81 //aPart = aMgr->rootDocument()->addFeature("Part");
85 //******************************************************
86 void XGUI_Workshop::initMenu()
88 XGUI_Workbench* aPage = myMainWindow->menuObject()->generalPage();
90 // File commands group
91 XGUI_MenuGroupPanel* aGroup = aPage->addGroup("Default");
93 XGUI_Command* aCommand;
95 aCommand = aGroup->addFeature("SAVE_CMD", tr("Save..."), tr("Save the document"),
96 QIcon(":pictures/save.png"), QKeySequence::Save);
97 aCommand->connectTo(this, SLOT(onSave()));
98 //aCommand->disable();
100 aCommand = aGroup->addFeature("UNDO_CMD", tr("Undo"), tr("Undo last command"),
101 QIcon(":pictures/undo.png"), QKeySequence::Undo);
102 aCommand->connectTo(this, SLOT(onUndo()));
104 aCommand = aGroup->addFeature("REDO_CMD", tr("Redo"), tr("Redo last command"),
105 QIcon(":pictures/redo.png"), QKeySequence::Redo);
106 aCommand->connectTo(this, SLOT(onRedo()));
108 aCommand = aGroup->addFeature("REBUILD_CMD", tr("Rebuild"), tr("Rebuild data objects"),
109 QIcon(":pictures/rebuild.png"));
111 aCommand = aGroup->addFeature("SAVEAS_CMD", tr("Save as..."), tr("Save the document into a file"),
112 QIcon(":pictures/save.png"));
113 aCommand->connectTo(this, SLOT(onSaveAs()));
114 //aCommand->disable();
116 aCommand = aGroup->addFeature("OPEN_CMD", tr("Open..."), tr("Open a new document"),
117 QIcon(":pictures/open.png"), QKeySequence::Open);
118 aCommand->connectTo(this, SLOT(onOpen()));
120 //aCommand = aGroup->addFeature("NEW_CMD", tr("New"), tr("Create a new document"),
121 // QIcon(":pictures/new.png"), QKeySequence::New);
122 //aCommand->connectTo(this, SLOT(onNew()));
124 aCommand = aGroup->addFeature("EXIT_CMD", tr("Exit"), tr("Exit application"),
125 QIcon(":pictures/close.png"), QKeySequence::Close);
126 aCommand->connectTo(this, SLOT(onExit()));
130 //******************************************************
131 XGUI_Workbench* XGUI_Workshop::addWorkbench(const QString& theName)
133 XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
134 return aMenuBar->addWorkbench(theName);
137 //******************************************************
138 void XGUI_Workshop::processEvent(const Event_Message* theMessage)
140 static Event_ID aFeatureId = Event_Loop::loop()->eventByName("FeatureEvent");
141 if (theMessage->eventID() == aFeatureId) {
142 const Config_FeatureMessage* aFeatureMsg =
143 dynamic_cast<const Config_FeatureMessage*>(theMessage);
144 addFeature(aFeatureMsg);
147 const Config_PointerMessage* aPartSetMsg =
148 dynamic_cast<const Config_PointerMessage*>(theMessage);
150 ModuleBase_PropPanelOperation* aOperation =
151 (ModuleBase_PropPanelOperation*)(aPartSetMsg->pointer());
152 setCurrentOperation(aOperation);
153 if(aOperation->xmlRepresentation().isEmpty()) { //!< No need for property panel
154 myCurrentOperation->start();
155 myCurrentOperation->commit();
156 updateCommandStatus();
158 fillPropertyPanel(aOperation);
164 qDebug() << "XGUI_Workshop::ProcessEvent: "
165 << "Catch message, but it can not be processed.";
173 void XGUI_Workshop::addFeature(const Config_FeatureMessage* theMessage)
177 qDebug() << "XGUI_Workshop::addFeature: NULL message.";
181 //Find or create Workbench
182 XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
183 QString aWchName = QString::fromStdString(theMessage->workbenchId());
184 XGUI_Workbench* aPage = aMenuBar->findWorkbench(aWchName);
186 aPage = addWorkbench(aWchName);
188 //Find or create Group
189 QString aGroupName = QString::fromStdString(theMessage->groupId());
190 XGUI_MenuGroupPanel* aGroup = aPage->findGroup(aGroupName);
192 aGroup = aPage->addGroup(aGroupName);
195 QString aFeatureId = QString::fromStdString(theMessage->id());
196 XGUI_Command* aCommand = aGroup->addFeature(QString::fromStdString(theMessage->id()),
197 QString::fromStdString(theMessage->text()),
198 QString::fromStdString(theMessage->tooltip()),
199 QIcon(theMessage->icon().c_str())
200 //TODO(sbh): QKeySequence
202 myPartSetModule->featureCreated(aCommand);
208 void XGUI_Workshop::fillPropertyPanel(ModuleBase_PropPanelOperation* theOperation)
210 connectToPropertyPanel(theOperation);
211 QWidget* aPropWidget = myMainWindow->findChild<QWidget*>(XGUI::PROP_PANEL_WDG);
212 qDeleteAll(aPropWidget->children());
213 theOperation->start();
214 XGUI_WidgetFactory aFactory = XGUI_WidgetFactory(theOperation);
215 aFactory.createWidget(aPropWidget);
216 myMainWindow->setPropertyPannelTitle(theOperation->description());
219 void XGUI_Workshop::setCurrentOperation(ModuleBase_Operation* theOperation)
221 //FIXME: Ask user about aborting of current operation?
222 if (myCurrentOperation) {
223 //TODO get isOperation from document
224 if (myCurrentOperation->isRunning())
225 myCurrentOperation->abort();
227 myCurrentOperation->deleteLater();
229 myCurrentOperation = theOperation;
233 * Makes a signal/slot connections between Property Panel
234 * and given operation. The given operation becomes a
235 * current operation and previous operation if exists
237 void XGUI_Workshop::connectToPropertyPanel(ModuleBase_Operation* theOperation)
239 QDockWidget* aPanel = myMainWindow->findChild<QDockWidget*>(XGUI::PROP_PANEL);
240 QPushButton* aOkBtn = aPanel->findChild<QPushButton*>(XGUI::PROP_PANEL_OK);
241 connect(aOkBtn, SIGNAL(clicked()), theOperation, SLOT(commit()));
242 QPushButton* aCancelBtn = aPanel->findChild<QPushButton*>(XGUI::PROP_PANEL_CANCEL);
243 connect(aCancelBtn, SIGNAL(clicked()), theOperation, SLOT(abort()));
245 connect(theOperation, SIGNAL(started()), myMainWindow, SLOT(showPropertyPanel()));
246 connect(theOperation, SIGNAL(stopped()), myMainWindow, SLOT(hidePropertyPanel()));
247 connect(theOperation, SIGNAL(stopped()), this, SLOT(updateCommandStatus()));
250 //******************************************************
251 void XGUI_Workshop::onExit()
256 //******************************************************
257 void XGUI_Workshop::onNew()
259 QApplication::setOverrideCursor(Qt::WaitCursor);
260 if (myMainWindow->objectBrowser() == 0) {
261 myMainWindow->createDockWidgets();
262 mySelector->connectObjectBrowser(myMainWindow->objectBrowser());
264 myMainWindow->showObjectBrowser();
265 myMainWindow->showPythonConsole();
266 QMdiSubWindow* aWnd = myMainWindow->viewer()->createView();
267 aWnd->showMaximized();
268 updateCommandStatus();
269 QApplication::restoreOverrideCursor();
272 //******************************************************
273 void XGUI_Workshop::onOpen()
275 //QString aFileName = QFileDialog::getOpenFileName(mainWindow());
276 updateCommandStatus();
279 //******************************************************
280 void XGUI_Workshop::onSave()
282 updateCommandStatus();
285 //******************************************************
286 void XGUI_Workshop::onSaveAs()
288 //QString aFileName = QFileDialog::getSaveFileName(mainWindow());
289 updateCommandStatus();
292 //******************************************************
293 void XGUI_Workshop::onUndo()
295 myMainWindow->objectBrowser()->setCurrentIndex(QModelIndex());
296 std::shared_ptr<ModelAPI_PluginManager> aMgr = ModelAPI_PluginManager::get();
297 std::shared_ptr<ModelAPI_Document> aDoc = aMgr->rootDocument();
299 updateCommandStatus();
302 //******************************************************
303 void XGUI_Workshop::onRedo()
305 myMainWindow->objectBrowser()->setCurrentIndex(QModelIndex());
306 std::shared_ptr<ModelAPI_PluginManager> aMgr = ModelAPI_PluginManager::get();
307 std::shared_ptr<ModelAPI_Document> aDoc = aMgr->rootDocument();
309 updateCommandStatus();
313 //******************************************************
314 XGUI_Module* XGUI_Workshop::loadModule(const QString& theModule)
316 QString libName = library(theModule);
317 if (libName.isEmpty()) {
319 qPrintable( tr( "Information about module \"%1\" doesn't exist." ).arg( theModule ) ));
324 CREATE_FUNC crtInst = 0;
328 HINSTANCE modLib = ::LoadLibrary((LPTSTR) qPrintable(libName));
332 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
333 0, ::GetLastError(), 0, (LPTSTR) & lpMsgBuf, 0, 0);
334 QString aMsg((char*) &lpMsgBuf);
335 err = QString("Failed to load %1. %2").arg(libName).arg(aMsg);
336 ::LocalFree(lpMsgBuf);
338 crtInst = (CREATE_FUNC) ::GetProcAddress(modLib, CREATE_MODULE);
342 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM
343 | FORMAT_MESSAGE_IGNORE_INSERTS,
344 0, ::GetLastError(), 0, (LPTSTR) & lpMsgBuf, 0, 0);
345 QString aMsg((char*) &lpMsgBuf);
346 err = QString("Failed to find %1 function. %2").arg( CREATE_MODULE).arg(aMsg);
347 ::LocalFree(lpMsgBuf);
351 void* modLib = dlopen( libName.toLatin1(), RTLD_LAZY );
353 err = QString( "Can not load library %1. %2" ).arg( libName ).arg( dlerror() );
356 crtInst = (CREATE_FUNC)dlsym( modLib, CREATE_MODULE );
358 err = QString( "Failed to find function %1. %2" ).arg( CREATE_MODULE ).arg( dlerror() );
362 XGUI_Module* aModule = crtInst ? crtInst(this) : 0;
364 if (!err.isEmpty()) {
365 if (mainWindow() && mainWindow()->isVisible())
366 QMessageBox::warning(mainWindow(), tr("Error"), err);
368 qWarning( qPrintable( err ));
373 //******************************************************
374 bool XGUI_Workshop::activateModule()
376 myPartSetModule = loadModule("PartSet");
377 if (!myPartSetModule)
379 myPartSetModule->createFeatures();
383 //******************************************************
384 void XGUI_Workshop::updateCommandStatus()
386 XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
388 QList<XGUI_Command*> aCommands = aMenuBar->features();
389 QList<XGUI_Command*>::const_iterator aIt;
391 std::shared_ptr<ModelAPI_PluginManager> aMgr = ModelAPI_PluginManager::get();
392 if (aMgr->hasRootDocument()) {
393 XGUI_Command* aUndoCmd;
394 XGUI_Command* aRedoCmd;
395 for (aIt = aCommands.constBegin(); aIt != aCommands.constEnd(); ++aIt) {
396 if ((*aIt)->id() == "UNDO_CMD")
398 else if ((*aIt)->id() == "REDO_CMD")
400 else // Enable all commands
403 std::shared_ptr<ModelAPI_Document> aDoc = aMgr->rootDocument();
404 aUndoCmd->setEnabled(aDoc->canUndo());
405 aRedoCmd->setEnabled(aDoc->canRedo());
407 for (aIt = aCommands.constBegin(); aIt != aCommands.constEnd(); ++aIt) {
408 if ((*aIt)->id() == "NEW_CMD")
410 else if ((*aIt)->id() == "EXIT_CMD")