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"
14 #include "XGUI_Displayer.h"
15 #include "XGUI_OperationMgr.h"
17 #include <ModelAPI_PluginManager.h>
18 #include <ModelAPI_Feature.h>
19 #include <ModelAPI_Data.h>
20 #include <ModelAPI_AttributeDocRef.h>
22 #include <Event_Loop.h>
23 #include <ModuleBase_PropPanelOperation.h>
24 #include <ModuleBase_Operation.h>
25 #include <Config_FeatureMessage.h>
26 #include <Config_PointerMessage.h>
28 #include <QApplication>
29 #include <QFileDialog>
30 #include <QMessageBox>
31 #include <QMdiSubWindow>
32 #include <QPushButton>
33 #include <QDockWidget>
45 XGUI_Workshop::XGUI_Workshop()
49 myMainWindow = new XGUI_MainWindow();
50 mySelector = new XGUI_SelectionMgr(this);
51 myDisplayer = new XGUI_Displayer(myMainWindow->viewer());
52 myOperationMgr = new XGUI_OperationMgr(this);
53 connect(myOperationMgr, SIGNAL(beforeOperationStart()), this, SLOT(onBeforeOperationStart()));
54 connect(myOperationMgr, SIGNAL(afterOperationStart()), this, SLOT(onAfterOperationStart()));
57 //******************************************************
58 XGUI_Workshop::~XGUI_Workshop(void)
62 //******************************************************
63 void XGUI_Workshop::startApplication()
66 //Initialize event listening
67 Event_Loop* aLoop = Event_Loop::loop();
68 //TODO(sbh): Implement static method to extract event id [SEID]
69 Event_ID aFeatureId = aLoop->eventByName("FeatureEvent");
70 aLoop->registerListener(this, aFeatureId);
71 Event_ID aPartSetId = aLoop->eventByName("PartSetModuleEvent");
72 aLoop->registerListener(this, aPartSetId);
76 updateCommandStatus();
78 // Testing of document creation
79 //std::shared_ptr<ModelAPI_PluginManager> aMgr = ModelAPI_PluginManager::get();
80 //std::shared_ptr<ModelAPI_Feature> aPoint1 = aMgr->rootDocument()->addFeature("Point");
81 //std::shared_ptr<ModelAPI_Feature> aPart = aMgr->rootDocument()->addFeature("Part");
83 //aMgr->setCurrentDocument(aPart->data()->docRef("PartDocument")->value());
84 //std::shared_ptr<ModelAPI_Feature> aPoint2 = aMgr->rootDocument()->addFeature("Point");
85 //aPoint2 = aMgr->rootDocument()->addFeature("Point");
87 //aPart = aMgr->rootDocument()->addFeature("Part");
91 //******************************************************
92 void XGUI_Workshop::initMenu()
94 XGUI_Workbench* aPage = myMainWindow->menuObject()->generalPage();
96 // File commands group
97 XGUI_MenuGroupPanel* aGroup = aPage->addGroup("Default");
99 XGUI_Command* aCommand;
101 aCommand = aGroup->addFeature("SAVE_CMD", tr("Save..."), tr("Save the document"),
102 QIcon(":pictures/save.png"), QKeySequence::Save);
103 aCommand->connectTo(this, SLOT(onSave()));
104 //aCommand->disable();
106 aCommand = aGroup->addFeature("UNDO_CMD", tr("Undo"), tr("Undo last command"),
107 QIcon(":pictures/undo.png"), QKeySequence::Undo);
108 aCommand->connectTo(this, SLOT(onUndo()));
110 aCommand = aGroup->addFeature("REDO_CMD", tr("Redo"), tr("Redo last command"),
111 QIcon(":pictures/redo.png"), QKeySequence::Redo);
112 aCommand->connectTo(this, SLOT(onRedo()));
114 aCommand = aGroup->addFeature("REBUILD_CMD", tr("Rebuild"), tr("Rebuild data objects"),
115 QIcon(":pictures/rebuild.png"));
117 aCommand = aGroup->addFeature("SAVEAS_CMD", tr("Save as..."), tr("Save the document into a file"),
118 QIcon(":pictures/save.png"));
119 aCommand->connectTo(this, SLOT(onSaveAs()));
120 //aCommand->disable();
122 aCommand = aGroup->addFeature("OPEN_CMD", tr("Open..."), tr("Open a new document"),
123 QIcon(":pictures/open.png"), QKeySequence::Open);
124 aCommand->connectTo(this, SLOT(onOpen()));
126 //aCommand = aGroup->addFeature("NEW_CMD", tr("New"), tr("Create a new document"),
127 // QIcon(":pictures/new.png"), QKeySequence::New);
128 //aCommand->connectTo(this, SLOT(onNew()));
130 aCommand = aGroup->addFeature("EXIT_CMD", tr("Exit"), tr("Exit application"),
131 QIcon(":pictures/close.png"), QKeySequence::Close);
132 aCommand->connectTo(this, SLOT(onExit()));
136 //******************************************************
137 XGUI_Workbench* XGUI_Workshop::addWorkbench(const QString& theName)
139 XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
140 return aMenuBar->addWorkbench(theName);
143 //******************************************************
144 void XGUI_Workshop::processEvent(const Event_Message* theMessage)
146 static Event_ID aFeatureId = Event_Loop::loop()->eventByName("FeatureEvent");
147 if (theMessage->eventID() == aFeatureId) {
148 const Config_FeatureMessage* aFeatureMsg =
149 dynamic_cast<const Config_FeatureMessage*>(theMessage);
150 addFeature(aFeatureMsg);
153 const Config_PointerMessage* aPartSetMsg =
154 dynamic_cast<const Config_PointerMessage*>(theMessage);
156 ModuleBase_PropPanelOperation* anOperation =
157 (ModuleBase_PropPanelOperation*)(aPartSetMsg->pointer());
159 if (myOperationMgr->startOperation(anOperation))
161 if (anOperation->isPerformedImmediately())
163 myOperationMgr->commitCurrentOperation();
164 updateCommandStatus();
166 /*if(anOperation->xmlRepresentation().isEmpty()) { //!< No need for property panel
167 //connectToPropertyPanel(anOperation);
169 anOperation->start();
171 if (anOperation->isPerformedImmediately()) {
172 anOperation->commit();
173 updateCommandStatus();
176 connectToPropertyPanel(anOperation);
177 QWidget* aPropWidget = myMainWindow->findChild<QWidget*>(XGUI::PROP_PANEL_WDG);
178 qDeleteAll(aPropWidget->children());
180 anOperation->start();
182 XGUI_WidgetFactory aFactory = XGUI_WidgetFactory(anOperation);
183 aFactory.createWidget(aPropWidget);
184 myMainWindow->setPropertyPannelTitle(anOperation->description());
191 qDebug() << "XGUI_Workshop::ProcessEvent: "
192 << "Catch message, but it can not be processed.";
197 void XGUI_Workshop::onBeforeOperationStart()
199 ModuleBase_PropPanelOperation* aOperation =
200 (ModuleBase_PropPanelOperation*)(myOperationMgr->currentOperation());
202 if(aOperation->xmlRepresentation().isEmpty()) { //!< No need for property panel
203 //myPartSetModule->connectToPropertyPanel(aOperation);
205 connectToPropertyPanel(aOperation);
206 QWidget* aPropWidget = myMainWindow->findChild<QWidget*>(XGUI::PROP_PANEL_WDG);
207 qDeleteAll(aPropWidget->children());
211 void XGUI_Workshop::onAfterOperationStart()
213 ModuleBase_PropPanelOperation* aOperation =
214 (ModuleBase_PropPanelOperation*)(myOperationMgr->currentOperation());
216 if(aOperation->xmlRepresentation().isEmpty()) { //!< No need for property panel
218 XGUI_WidgetFactory aFactory = XGUI_WidgetFactory(aOperation);
219 QWidget* aPropWidget = myMainWindow->findChild<QWidget*>(XGUI::PROP_PANEL_WDG);
220 aFactory.createWidget(aPropWidget);
221 myMainWindow->setPropertyPannelTitle(aOperation->description());
228 void XGUI_Workshop::addFeature(const Config_FeatureMessage* theMessage)
232 qDebug() << "XGUI_Workshop::addFeature: NULL message.";
236 //Find or create Workbench
237 XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
238 QString aWchName = QString::fromStdString(theMessage->workbenchId());
239 XGUI_Workbench* aPage = aMenuBar->findWorkbench(aWchName);
241 aPage = addWorkbench(aWchName);
243 //Find or create Group
244 QString aGroupName = QString::fromStdString(theMessage->groupId());
245 XGUI_MenuGroupPanel* aGroup = aPage->findGroup(aGroupName);
247 aGroup = aPage->addGroup(aGroupName);
250 QString aFeatureId = QString::fromStdString(theMessage->id());
251 XGUI_Command* aCommand = aGroup->addFeature(QString::fromStdString(theMessage->id()),
252 QString::fromStdString(theMessage->text()),
253 QString::fromStdString(theMessage->tooltip()),
254 QIcon(theMessage->icon().c_str())
255 //TODO(sbh): QKeySequence
257 myPartSetModule->featureCreated(aCommand);
263 /*void XGUI_Workshop::fillPropertyPanel(ModuleBase_PropPanelOperation* theOperation)
265 connectToPropertyPanel(theOperation);
266 QWidget* aPropWidget = myMainWindow->findChild<QWidget*>(XGUI::PROP_PANEL_WDG);
267 qDeleteAll(aPropWidget->children());
268 theOperation->start();
269 XGUI_WidgetFactory aFactory = XGUI_WidgetFactory(theOperation);
270 aFactory.createWidget(aPropWidget);
271 myMainWindow->setPropertyPannelTitle(theOperation->description());
275 * Makes a signal/slot connections between Property Panel
276 * and given operation. The given operation becomes a
277 * current operation and previous operation if exists
279 void XGUI_Workshop::connectToPropertyPanel(ModuleBase_Operation* theOperation)
281 QDockWidget* aPanel = myMainWindow->findChild<QDockWidget*>(XGUI::PROP_PANEL);
282 QPushButton* aOkBtn = aPanel->findChild<QPushButton*>(XGUI::PROP_PANEL_OK);
283 connect(aOkBtn, SIGNAL(clicked()), theOperation, SLOT(commit()));
284 QPushButton* aCancelBtn = aPanel->findChild<QPushButton*>(XGUI::PROP_PANEL_CANCEL);
285 connect(aCancelBtn, SIGNAL(clicked()), theOperation, SLOT(abort()));
287 connect(theOperation, SIGNAL(started()), myMainWindow, SLOT(showPropertyPanel()));
288 connect(theOperation, SIGNAL(stopped()), myMainWindow, SLOT(hidePropertyPanel()));
289 connect(theOperation, SIGNAL(stopped()), this, SLOT(updateCommandStatus()));
292 //******************************************************
293 void XGUI_Workshop::onExit()
298 //******************************************************
299 void XGUI_Workshop::onNew()
301 QApplication::setOverrideCursor(Qt::WaitCursor);
302 if (myMainWindow->objectBrowser() == 0) {
303 myMainWindow->createDockWidgets();
304 mySelector->connectObjectBrowser(myMainWindow->objectBrowser());
306 myMainWindow->showObjectBrowser();
307 myMainWindow->showPythonConsole();
308 QMdiSubWindow* aWnd = myMainWindow->viewer()->createView();
309 aWnd->showMaximized();
310 updateCommandStatus();
311 QApplication::restoreOverrideCursor();
314 //******************************************************
315 void XGUI_Workshop::onOpen()
317 //QString aFileName = QFileDialog::getOpenFileName(mainWindow());
318 updateCommandStatus();
321 //******************************************************
322 void XGUI_Workshop::onSave()
324 updateCommandStatus();
327 //******************************************************
328 void XGUI_Workshop::onSaveAs()
330 //QString aFileName = QFileDialog::getSaveFileName(mainWindow());
331 updateCommandStatus();
334 //******************************************************
335 void XGUI_Workshop::onUndo()
337 myMainWindow->objectBrowser()->setCurrentIndex(QModelIndex());
338 std::shared_ptr<ModelAPI_PluginManager> aMgr = ModelAPI_PluginManager::get();
339 std::shared_ptr<ModelAPI_Document> aDoc = aMgr->rootDocument();
341 updateCommandStatus();
344 //******************************************************
345 void XGUI_Workshop::onRedo()
347 myMainWindow->objectBrowser()->setCurrentIndex(QModelIndex());
348 std::shared_ptr<ModelAPI_PluginManager> aMgr = ModelAPI_PluginManager::get();
349 std::shared_ptr<ModelAPI_Document> aDoc = aMgr->rootDocument();
351 updateCommandStatus();
355 //******************************************************
356 XGUI_Module* XGUI_Workshop::loadModule(const QString& theModule)
358 QString libName = library(theModule);
359 if (libName.isEmpty()) {
361 qPrintable( tr( "Information about module \"%1\" doesn't exist." ).arg( theModule ) ));
366 CREATE_FUNC crtInst = 0;
370 HINSTANCE modLib = ::LoadLibrary((LPTSTR) qPrintable(libName));
374 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
375 0, ::GetLastError(), 0, (LPTSTR) & lpMsgBuf, 0, 0);
376 QString aMsg((char*) &lpMsgBuf);
377 err = QString("Failed to load %1. %2").arg(libName).arg(aMsg);
378 ::LocalFree(lpMsgBuf);
380 crtInst = (CREATE_FUNC) ::GetProcAddress(modLib, CREATE_MODULE);
384 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM
385 | FORMAT_MESSAGE_IGNORE_INSERTS,
386 0, ::GetLastError(), 0, (LPTSTR) & lpMsgBuf, 0, 0);
387 QString aMsg((char*) &lpMsgBuf);
388 err = QString("Failed to find %1 function. %2").arg( CREATE_MODULE).arg(aMsg);
389 ::LocalFree(lpMsgBuf);
393 void* modLib = dlopen( libName.toLatin1(), RTLD_LAZY );
395 err = QString( "Can not load library %1. %2" ).arg( libName ).arg( dlerror() );
398 crtInst = (CREATE_FUNC)dlsym( modLib, CREATE_MODULE );
400 err = QString( "Failed to find function %1. %2" ).arg( CREATE_MODULE ).arg( dlerror() );
404 XGUI_Module* aModule = crtInst ? crtInst(this) : 0;
406 if (!err.isEmpty()) {
407 if (mainWindow() && mainWindow()->isVisible())
408 QMessageBox::warning(mainWindow(), tr("Error"), err);
410 qWarning( qPrintable( err ));
415 //******************************************************
416 bool XGUI_Workshop::activateModule()
418 myPartSetModule = loadModule("PartSet");
419 if (!myPartSetModule)
421 myPartSetModule->createFeatures();
425 //******************************************************
426 void XGUI_Workshop::updateCommandStatus()
428 XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
430 QList<XGUI_Command*> aCommands = aMenuBar->features();
431 QList<XGUI_Command*>::const_iterator aIt;
433 std::shared_ptr<ModelAPI_PluginManager> aMgr = ModelAPI_PluginManager::get();
434 if (aMgr->hasRootDocument()) {
435 XGUI_Command* aUndoCmd;
436 XGUI_Command* aRedoCmd;
437 for (aIt = aCommands.constBegin(); aIt != aCommands.constEnd(); ++aIt) {
438 if ((*aIt)->id() == "UNDO_CMD")
440 else if ((*aIt)->id() == "REDO_CMD")
442 else // Enable all commands
445 std::shared_ptr<ModelAPI_Document> aDoc = aMgr->rootDocument();
446 aUndoCmd->setEnabled(aDoc->canUndo());
447 aRedoCmd->setEnabled(aDoc->canRedo());
449 for (aIt = aCommands.constBegin(); aIt != aCommands.constEnd(); ++aIt) {
450 if ((*aIt)->id() == "NEW_CMD")
452 else if ((*aIt)->id() == "EXIT_CMD")