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 connectWithOperation(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);
249 bool isUsePropPanel = theMessage->isUseInput();
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 QKeySequence(), isUsePropPanel);
257 connect(aCommand, SIGNAL(toggled(bool)),
258 myMainWindow->menuObject(), SLOT(onFeatureChecked(bool)));
259 myPartSetModule->featureCreated(aCommand);
265 /*void XGUI_Workshop::fillPropertyPanel(ModuleBase_PropPanelOperation* theOperation)
267 connectWithOperation(theOperation);
268 QWidget* aPropWidget = myMainWindow->findChild<QWidget*>(XGUI::PROP_PANEL_WDG);
269 qDeleteAll(aPropWidget->children());
270 theOperation->start();
271 XGUI_WidgetFactory aFactory = XGUI_WidgetFactory(theOperation);
272 aFactory.createWidget(aPropWidget);
273 myMainWindow->setPropertyPannelTitle(theOperation->description());
277 * Makes a signal/slot connections between Property Panel
278 * and given operation. The given operation becomes a
279 * current operation and previous operation if exists
281 void XGUI_Workshop::connectWithOperation(ModuleBase_Operation* theOperation)
283 QDockWidget* aPanel = myMainWindow->findChild<QDockWidget*>(XGUI::PROP_PANEL);
284 QPushButton* aOkBtn = aPanel->findChild<QPushButton*>(XGUI::PROP_PANEL_OK);
285 connect(aOkBtn, SIGNAL(clicked()), theOperation, SLOT(commit()));
286 QPushButton* aCancelBtn = aPanel->findChild<QPushButton*>(XGUI::PROP_PANEL_CANCEL);
287 connect(aCancelBtn, SIGNAL(clicked()), theOperation, SLOT(abort()));
289 connect(theOperation, SIGNAL(started()), myMainWindow, SLOT(showPropertyPanel()));
290 connect(theOperation, SIGNAL(stopped()), myMainWindow, SLOT(hidePropertyPanel()));
291 connect(theOperation, SIGNAL(stopped()), this, SLOT(updateCommandStatus()));
293 XGUI_MainMenu* aMenu = myMainWindow->menuObject();
294 connect(theOperation, SIGNAL(stopped()), aMenu, SLOT(restoreCommandState()));
296 XGUI_Command* aCommand = aMenu->feature(theOperation->operationId());
297 //Abort operation on uncheck the command
298 connect(aCommand, SIGNAL(toggled(bool)), theOperation, SLOT(setRunning(bool)));
302 //******************************************************
303 void XGUI_Workshop::onExit()
308 //******************************************************
309 void XGUI_Workshop::onNew()
311 QApplication::setOverrideCursor(Qt::WaitCursor);
312 if (myMainWindow->objectBrowser() == 0) {
313 myMainWindow->createDockWidgets();
314 mySelector->connectObjectBrowser(myMainWindow->objectBrowser());
316 myMainWindow->showObjectBrowser();
317 myMainWindow->showPythonConsole();
318 QMdiSubWindow* aWnd = myMainWindow->viewer()->createView();
319 aWnd->showMaximized();
320 updateCommandStatus();
321 QApplication::restoreOverrideCursor();
324 //******************************************************
325 void XGUI_Workshop::onOpen()
327 //QString aFileName = QFileDialog::getOpenFileName(mainWindow());
328 updateCommandStatus();
331 //******************************************************
332 void XGUI_Workshop::onSave()
334 updateCommandStatus();
337 //******************************************************
338 void XGUI_Workshop::onSaveAs()
340 //QString aFileName = QFileDialog::getSaveFileName(mainWindow());
341 updateCommandStatus();
344 //******************************************************
345 void XGUI_Workshop::onUndo()
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();
354 //******************************************************
355 void XGUI_Workshop::onRedo()
357 myMainWindow->objectBrowser()->setCurrentIndex(QModelIndex());
358 std::shared_ptr<ModelAPI_PluginManager> aMgr = ModelAPI_PluginManager::get();
359 std::shared_ptr<ModelAPI_Document> aDoc = aMgr->rootDocument();
361 updateCommandStatus();
364 //******************************************************
365 XGUI_Module* XGUI_Workshop::loadModule(const QString& theModule)
367 QString libName = library(theModule);
368 if (libName.isEmpty()) {
370 qPrintable( tr( "Information about module \"%1\" doesn't exist." ).arg( theModule ) ));
375 CREATE_FUNC crtInst = 0;
379 HINSTANCE modLib = ::LoadLibrary((LPTSTR) qPrintable(libName));
383 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
384 0, ::GetLastError(), 0, (LPTSTR) & lpMsgBuf, 0, 0);
385 QString aMsg((char*) &lpMsgBuf);
386 err = QString("Failed to load %1. %2").arg(libName).arg(aMsg);
387 ::LocalFree(lpMsgBuf);
389 crtInst = (CREATE_FUNC) ::GetProcAddress(modLib, CREATE_MODULE);
393 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM
394 | FORMAT_MESSAGE_IGNORE_INSERTS,
395 0, ::GetLastError(), 0, (LPTSTR) & lpMsgBuf, 0, 0);
396 QString aMsg((char*) &lpMsgBuf);
397 err = QString("Failed to find %1 function. %2").arg( CREATE_MODULE).arg(aMsg);
398 ::LocalFree(lpMsgBuf);
402 void* modLib = dlopen( libName.toLatin1(), RTLD_LAZY );
404 err = QString( "Can not load library %1. %2" ).arg( libName ).arg( dlerror() );
407 crtInst = (CREATE_FUNC)dlsym( modLib, CREATE_MODULE );
409 err = QString( "Failed to find function %1. %2" ).arg( CREATE_MODULE ).arg( dlerror() );
413 XGUI_Module* aModule = crtInst ? crtInst(this) : 0;
415 if (!err.isEmpty()) {
416 if (mainWindow() && mainWindow()->isVisible())
417 QMessageBox::warning(mainWindow(), tr("Error"), err);
419 qWarning( qPrintable( err ));
424 //******************************************************
425 bool XGUI_Workshop::activateModule()
427 myPartSetModule = loadModule("PartSet");
428 if (!myPartSetModule)
430 myPartSetModule->createFeatures();
434 //******************************************************
435 void XGUI_Workshop::updateCommandStatus()
437 XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
439 QList<XGUI_Command*> aCommands = aMenuBar->features();
440 QList<XGUI_Command*>::const_iterator aIt;
442 std::shared_ptr<ModelAPI_PluginManager> aMgr = ModelAPI_PluginManager::get();
443 if (aMgr->hasRootDocument()) {
444 XGUI_Command* aUndoCmd;
445 XGUI_Command* aRedoCmd;
446 for (aIt = aCommands.constBegin(); aIt != aCommands.constEnd(); ++aIt) {
447 if ((*aIt)->id() == "UNDO_CMD")
449 else if ((*aIt)->id() == "REDO_CMD")
451 else // Enable all commands
454 std::shared_ptr<ModelAPI_Document> aDoc = aMgr->rootDocument();
455 aUndoCmd->setEnabled(aDoc->canUndo());
456 aRedoCmd->setEnabled(aDoc->canRedo());
458 for (aIt = aCommands.constBegin(); aIt != aCommands.constEnd(); ++aIt) {
459 if ((*aIt)->id() == "NEW_CMD")
461 else if ((*aIt)->id() == "EXIT_CMD")