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_Operation.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_Operation* aOperation = (ModuleBase_Operation*)(aPartSetMsg->pointer());
151 setCurrentOperation(aOperation);
152 if(aOperation->xmlRepresentation().isEmpty()) { //!< No need for property panel
153 myCurrentOperation->start();
154 myCurrentOperation->commit();
155 updateCommandStatus();
157 fillPropertyPanel(aOperation);
163 qDebug() << "XGUI_Workshop::ProcessEvent: "
164 << "Catch message, but it can not be processed.";
172 void XGUI_Workshop::addFeature(const Config_FeatureMessage* theMessage)
176 qDebug() << "XGUI_Workshop::addFeature: NULL message.";
180 //Find or create Workbench
181 XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
182 QString aWchName = QString::fromStdString(theMessage->workbenchId());
183 XGUI_Workbench* aPage = aMenuBar->findWorkbench(aWchName);
185 aPage = addWorkbench(aWchName);
187 //Find or create Group
188 QString aGroupName = QString::fromStdString(theMessage->groupId());
189 XGUI_MenuGroupPanel* aGroup = aPage->findGroup(aGroupName);
191 aGroup = aPage->addGroup(aGroupName);
194 QString aFeatureId = QString::fromStdString(theMessage->id());
195 XGUI_Command* aCommand = aGroup->addFeature(QString::fromStdString(theMessage->id()),
196 QString::fromStdString(theMessage->text()),
197 QString::fromStdString(theMessage->tooltip()),
198 QIcon(theMessage->icon().c_str())
199 //TODO(sbh): QKeySequence
201 myPartSetModule->featureCreated(aCommand);
207 void XGUI_Workshop::fillPropertyPanel(ModuleBase_Operation* theOperation)
209 connectToPropertyPanel(theOperation);
210 QWidget* aPropWidget = myMainWindow->findChild<QWidget*>(XGUI::PROP_PANEL_WDG);
211 qDeleteAll(aPropWidget->children());
212 theOperation->start();
213 XGUI_WidgetFactory aFactory = XGUI_WidgetFactory(theOperation);
214 aFactory.fillWidget(aPropWidget);
217 void XGUI_Workshop::setCurrentOperation(ModuleBase_Operation* theOperation)
219 //FIXME: Ask user about aborting of current operation?
220 if (myCurrentOperation) {
221 //TODO get isOperation from document
222 if (myCurrentOperation->isRunning())
223 myCurrentOperation->abort();
225 myCurrentOperation->deleteLater();
227 myCurrentOperation = theOperation;
231 * Makes a signal/slot connections between Property Panel
232 * and given operation. The given operation becomes a
233 * current operation and previous operation if exists
235 void XGUI_Workshop::connectToPropertyPanel(ModuleBase_Operation* theOperation)
237 QDockWidget* aPanel = myMainWindow->findChild<QDockWidget*>(XGUI::PROP_PANEL);
238 QPushButton* aOkBtn = aPanel->findChild<QPushButton*>(XGUI::PROP_PANEL_OK);
239 connect(aOkBtn, SIGNAL(clicked()), theOperation, SLOT(commit()));
240 QPushButton* aCancelBtn = aPanel->findChild<QPushButton*>(XGUI::PROP_PANEL_CANCEL);
241 connect(aCancelBtn, SIGNAL(clicked()), theOperation, SLOT(abort()));
243 connect(theOperation, SIGNAL(started()), myMainWindow, SLOT(showPropertyPanel()));
244 connect(theOperation, SIGNAL(stopped()), myMainWindow, SLOT(hidePropertyPanel()));
245 connect(theOperation, SIGNAL(stopped()), this, SLOT(updateCommandStatus()));
248 //******************************************************
249 void XGUI_Workshop::onExit()
254 //******************************************************
255 void XGUI_Workshop::onNew()
257 QApplication::setOverrideCursor(Qt::WaitCursor);
258 if (myMainWindow->objectBrowser() == 0) {
259 myMainWindow->createDockWidgets();
260 mySelector->connectObjectBrowser(myMainWindow->objectBrowser());
262 myMainWindow->showObjectBrowser();
263 myMainWindow->showPythonConsole();
264 QMdiSubWindow* aWnd = myMainWindow->viewer()->createView();
265 aWnd->showMaximized();
266 updateCommandStatus();
267 QApplication::restoreOverrideCursor();
270 //******************************************************
271 void XGUI_Workshop::onOpen()
273 //QString aFileName = QFileDialog::getOpenFileName(mainWindow());
274 updateCommandStatus();
277 //******************************************************
278 void XGUI_Workshop::onSave()
280 updateCommandStatus();
283 //******************************************************
284 void XGUI_Workshop::onSaveAs()
286 //QString aFileName = QFileDialog::getSaveFileName(mainWindow());
287 updateCommandStatus();
290 //******************************************************
291 void XGUI_Workshop::onUndo()
293 myMainWindow->objectBrowser()->setCurrentIndex(QModelIndex());
294 std::shared_ptr<ModelAPI_PluginManager> aMgr = ModelAPI_PluginManager::get();
295 std::shared_ptr<ModelAPI_Document> aDoc = aMgr->rootDocument();
297 updateCommandStatus();
300 //******************************************************
301 void XGUI_Workshop::onRedo()
303 myMainWindow->objectBrowser()->setCurrentIndex(QModelIndex());
304 std::shared_ptr<ModelAPI_PluginManager> aMgr = ModelAPI_PluginManager::get();
305 std::shared_ptr<ModelAPI_Document> aDoc = aMgr->rootDocument();
307 updateCommandStatus();
311 //******************************************************
312 XGUI_Module* XGUI_Workshop::loadModule(const QString& theModule)
314 QString libName = library(theModule);
315 if (libName.isEmpty()) {
317 qPrintable( tr( "Information about module \"%1\" doesn't exist." ).arg( theModule ) ));
322 CREATE_FUNC crtInst = 0;
326 HINSTANCE modLib = ::LoadLibrary((LPTSTR) qPrintable(libName));
330 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
331 0, ::GetLastError(), 0, (LPTSTR) & lpMsgBuf, 0, 0);
332 QString aMsg((char*) &lpMsgBuf);
333 err = QString("Failed to load %1. %2").arg(libName).arg(aMsg);
334 ::LocalFree(lpMsgBuf);
336 crtInst = (CREATE_FUNC) ::GetProcAddress(modLib, CREATE_MODULE);
340 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM
341 | FORMAT_MESSAGE_IGNORE_INSERTS,
342 0, ::GetLastError(), 0, (LPTSTR) & lpMsgBuf, 0, 0);
343 QString aMsg((char*) &lpMsgBuf);
344 err = QString("Failed to find %1 function. %2").arg( CREATE_MODULE).arg(aMsg);
345 ::LocalFree(lpMsgBuf);
349 void* modLib = dlopen( libName.toLatin1(), RTLD_LAZY );
351 err = QString( "Can not load library %1. %2" ).arg( libName ).arg( dlerror() );
354 crtInst = (CREATE_FUNC)dlsym( modLib, CREATE_MODULE );
356 err = QString( "Failed to find function %1. %2" ).arg( CREATE_MODULE ).arg( dlerror() );
360 XGUI_Module* aModule = crtInst ? crtInst(this) : 0;
362 if (!err.isEmpty()) {
363 if (mainWindow() && mainWindow()->isVisible())
364 QMessageBox::warning(mainWindow(), tr("Error"), err);
366 qWarning( qPrintable( err ));
371 //******************************************************
372 bool XGUI_Workshop::activateModule()
374 myPartSetModule = loadModule("PartSet");
375 if (!myPartSetModule)
377 myPartSetModule->createFeatures();
381 //******************************************************
382 void XGUI_Workshop::updateCommandStatus()
384 XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
386 QList<XGUI_Command*> aCommands = aMenuBar->features();
387 QList<XGUI_Command*>::const_iterator aIt;
389 std::shared_ptr<ModelAPI_PluginManager> aMgr = ModelAPI_PluginManager::get();
390 if (aMgr->hasRootDocument()) {
391 XGUI_Command* aUndoCmd;
392 XGUI_Command* aRedoCmd;
393 for (aIt = aCommands.constBegin(); aIt != aCommands.constEnd(); ++aIt) {
394 if ((*aIt)->id() == "UNDO_CMD")
396 else if ((*aIt)->id() == "REDO_CMD")
398 else // Enable all commands
401 std::shared_ptr<ModelAPI_Document> aDoc = aMgr->rootDocument();
402 aUndoCmd->setEnabled(aDoc->canUndo());
403 aRedoCmd->setEnabled(aDoc->canRedo());
405 for (aIt = aCommands.constBegin(); aIt != aCommands.constEnd(); ++aIt) {
406 if ((*aIt)->id() == "NEW_CMD")
408 else if ((*aIt)->id() == "EXIT_CMD")