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();
71 // Testing of document creation
72 //std::shared_ptr<ModelAPI_PluginManager> aMgr = ModelAPI_PluginManager::get();
73 //std::shared_ptr<ModelAPI_Feature> aPoint1 = aMgr->rootDocument()->addFeature("Point");
74 //std::shared_ptr<ModelAPI_Feature> aPart = aMgr->rootDocument()->addFeature("Part");
76 //aMgr->setCurrentDocument(aPart->data()->docRef("PartDocument")->value());
77 //std::shared_ptr<ModelAPI_Feature> aPoint2 = aMgr->rootDocument()->addFeature("Point");
78 //aPoint2 = aMgr->rootDocument()->addFeature("Point");
80 //aPart = aMgr->rootDocument()->addFeature("Part");
84 //******************************************************
85 void XGUI_Workshop::initMenu()
87 XGUI_Workbench* aPage = myMainWindow->menuObject()->generalPage();
89 // File commands group
90 XGUI_MenuGroupPanel* aGroup = aPage->addGroup("Default");
92 XGUI_Command* aCommand;
94 aCommand = aGroup->addFeature("SAVE_CMD", tr("Save..."), tr("Save the document"),
95 QIcon(":pictures/save.png"), QKeySequence::Save);
96 aCommand->connectTo(this, SLOT(onSave()));
97 //aCommand->disable();
99 aCommand = aGroup->addFeature("UNDO_CMD", tr("Undo"), tr("Undo last command"),
100 QIcon(":pictures/undo.png"), QKeySequence::Undo);
101 aCommand->connectTo(this, SLOT(onUndo()));
103 aCommand = aGroup->addFeature("REDO_CMD", tr("Redo"), tr("Redo last command"),
104 QIcon(":pictures/redo.png"), QKeySequence::Redo);
105 aCommand->connectTo(this, SLOT(onRedo()));
107 aCommand = aGroup->addFeature("REBUILD_CMD", tr("Rebuild"), tr("Rebuild data objects"),
108 QIcon(":pictures/rebuild.png"));
110 aCommand = aGroup->addFeature("SAVEAS_CMD", tr("Save as..."), tr("Save the document into a file"),
111 QIcon(":pictures/save.png"));
112 aCommand->connectTo(this, SLOT(onSaveAs()));
113 //aCommand->disable();
115 aCommand = aGroup->addFeature("OPEN_CMD", tr("Open..."), tr("Open a new document"),
116 QIcon(":pictures/open.png"), QKeySequence::Open);
117 aCommand->connectTo(this, SLOT(onOpen()));
119 aCommand = aGroup->addFeature("NEW_CMD", tr("New"), tr("Create a new document"),
120 QIcon(":pictures/new.png"), QKeySequence::New);
121 aCommand->connectTo(this, SLOT(onNew()));
123 aCommand = aGroup->addFeature("EXIT_CMD", tr("Exit"), tr("Exit application"),
124 QIcon(":pictures/close.png"), QKeySequence::Close);
125 aCommand->connectTo(this, SLOT(onExit()));
129 //******************************************************
130 XGUI_Workbench* XGUI_Workshop::addWorkbench(const QString& theName)
132 XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
133 return aMenuBar->addWorkbench(theName);
136 //******************************************************
137 void XGUI_Workshop::processEvent(const Event_Message* theMessage)
139 static Event_ID aFeatureId = Event_Loop::loop()->eventByName("FeatureEvent");
140 if (theMessage->eventID() == aFeatureId) {
141 const Config_FeatureMessage* aFeatureMsg =
142 dynamic_cast<const Config_FeatureMessage*>(theMessage);
143 addFeature(aFeatureMsg);
146 const Config_PointerMessage* aPartSetMsg =
147 dynamic_cast<const Config_PointerMessage*>(theMessage);
149 ModuleBase_Operation* aOperation = (ModuleBase_Operation*)(aPartSetMsg->pointer());
150 setCurrentOperation(aOperation);
151 if(aOperation->xmlRepresentation().isEmpty()) { //!< No need for property panel
152 myCurrentOperation->start();
153 myCurrentOperation->commit();
154 updateCommandStatus();
156 fillPropertyPanel(aOperation);
162 qDebug() << "XGUI_Workshop::ProcessEvent: "
163 << "Catch message, but it can not be processed.";
171 void XGUI_Workshop::addFeature(const Config_FeatureMessage* theMessage)
175 qDebug() << "XGUI_Workshop::addFeature: NULL message.";
179 //Find or create Workbench
180 XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
181 QString aWchName = QString::fromStdString(theMessage->workbenchId());
182 XGUI_Workbench* aPage = aMenuBar->findWorkbench(aWchName);
184 aPage = addWorkbench(aWchName);
186 //Find or create Group
187 QString aGroupName = QString::fromStdString(theMessage->groupId());
188 XGUI_MenuGroupPanel* aGroup = aPage->findGroup(aGroupName);
190 aGroup = aPage->addGroup(aGroupName);
193 QString aFeatureId = QString::fromStdString(theMessage->id());
194 XGUI_Command* aCommand = aGroup->addFeature(QString::fromStdString(theMessage->id()),
195 QString::fromStdString(theMessage->text()),
196 QString::fromStdString(theMessage->tooltip()),
197 QIcon(theMessage->icon().c_str())
198 //TODO(sbh): QKeySequence
200 myPartSetModule->featureCreated(aCommand);
206 void XGUI_Workshop::fillPropertyPanel(ModuleBase_Operation* theOperation)
208 connectToPropertyPanel(theOperation);
209 QWidget* aPropWidget = myMainWindow->findChild<QWidget*>(XGUI::PROP_PANEL_WDG);
210 qDeleteAll(aPropWidget->children());
211 theOperation->start();
212 XGUI_WidgetFactory aFactory = XGUI_WidgetFactory(theOperation);
213 aFactory.fillWidget(aPropWidget);
216 void XGUI_Workshop::setCurrentOperation(ModuleBase_Operation* theOperation)
218 //FIXME: Ask user about aborting of current operation?
219 if (myCurrentOperation) {
220 //TODO get isOperation from document
221 if (myCurrentOperation->isRunning())
222 myCurrentOperation->abort();
224 myCurrentOperation->deleteLater();
226 myCurrentOperation = theOperation;
230 * Makes a signal/slot connections between Property Panel
231 * and given operation. The given operation becomes a
232 * current operation and previous operation if exists
234 void XGUI_Workshop::connectToPropertyPanel(ModuleBase_Operation* theOperation)
236 QDockWidget* aPanel = myMainWindow->findChild<QDockWidget*>(XGUI::PROP_PANEL);
237 QPushButton* aOkBtn = aPanel->findChild<QPushButton*>(XGUI::PROP_PANEL_OK);
238 connect(aOkBtn, SIGNAL(clicked()), theOperation, SLOT(commit()));
239 QPushButton* aCancelBtn = aPanel->findChild<QPushButton*>(XGUI::PROP_PANEL_CANCEL);
240 connect(aCancelBtn, SIGNAL(clicked()), theOperation, SLOT(abort()));
242 connect(theOperation, SIGNAL(started()), myMainWindow, SLOT(showPropertyPanel()));
243 connect(theOperation, SIGNAL(stopped()), myMainWindow, SLOT(hidePropertyPanel()));
244 connect(theOperation, SIGNAL(stopped()), this, SLOT(updateCommandStatus()));
247 //******************************************************
248 void XGUI_Workshop::onExit()
253 //******************************************************
254 void XGUI_Workshop::onNew()
256 QApplication::setOverrideCursor(Qt::WaitCursor);
257 if (myMainWindow->objectBrowser() == 0) {
258 myMainWindow->createDockWidgets();
259 mySelector->connectObjectBrowser(myMainWindow->objectBrowser());
261 myMainWindow->showObjectBrowser();
262 myMainWindow->showPythonConsole();
263 QMdiSubWindow* aWnd = myMainWindow->viewer()->createView();
264 aWnd->showMaximized();
265 updateCommandStatus();
266 QApplication::restoreOverrideCursor();
269 //******************************************************
270 void XGUI_Workshop::onOpen()
272 //QString aFileName = QFileDialog::getOpenFileName(mainWindow());
273 updateCommandStatus();
276 //******************************************************
277 void XGUI_Workshop::onSave()
279 updateCommandStatus();
282 //******************************************************
283 void XGUI_Workshop::onSaveAs()
285 //QString aFileName = QFileDialog::getSaveFileName(mainWindow());
286 updateCommandStatus();
289 //******************************************************
290 void XGUI_Workshop::onUndo()
292 myMainWindow->objectBrowser()->setCurrentIndex(QModelIndex());
293 std::shared_ptr<ModelAPI_PluginManager> aMgr = ModelAPI_PluginManager::get();
294 std::shared_ptr<ModelAPI_Document> aDoc = aMgr->rootDocument();
296 updateCommandStatus();
299 //******************************************************
300 void XGUI_Workshop::onRedo()
302 myMainWindow->objectBrowser()->setCurrentIndex(QModelIndex());
303 std::shared_ptr<ModelAPI_PluginManager> aMgr = ModelAPI_PluginManager::get();
304 std::shared_ptr<ModelAPI_Document> aDoc = aMgr->rootDocument();
306 updateCommandStatus();
310 //******************************************************
311 XGUI_Module* XGUI_Workshop::loadModule(const QString& theModule)
313 QString libName = library(theModule);
314 if (libName.isEmpty()) {
316 qPrintable( tr( "Information about module \"%1\" doesn't exist." ).arg( theModule ) ));
321 CREATE_FUNC crtInst = 0;
325 HINSTANCE modLib = ::LoadLibrary((LPTSTR) qPrintable(libName));
329 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
330 0, ::GetLastError(), 0, (LPTSTR) & lpMsgBuf, 0, 0);
331 QString aMsg((char*) &lpMsgBuf);
332 err = QString("Failed to load %1. %2").arg(libName).arg(aMsg);
333 ::LocalFree(lpMsgBuf);
335 crtInst = (CREATE_FUNC) ::GetProcAddress(modLib, CREATE_MODULE);
339 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM
340 | FORMAT_MESSAGE_IGNORE_INSERTS,
341 0, ::GetLastError(), 0, (LPTSTR) & lpMsgBuf, 0, 0);
342 QString aMsg((char*) &lpMsgBuf);
343 err = QString("Failed to find %1 function. %2").arg( CREATE_MODULE).arg(aMsg);
344 ::LocalFree(lpMsgBuf);
348 void* modLib = dlopen( libName.toLatin1(), RTLD_LAZY );
350 err = QString( "Can not load library %1. %2" ).arg( libName ).arg( dlerror() );
353 crtInst = (CREATE_FUNC)dlsym( modLib, GET_MODULE_NAME );
355 err = QString( "Failed to find function %1. %2" ).arg( CREATE_MODULE ).arg( dlerror() );
359 XGUI_Module* aModule = crtInst ? crtInst(this) : 0;
361 if (!err.isEmpty()) {
362 if (mainWindow() && mainWindow()->isVisible())
363 QMessageBox::warning(mainWindow(), tr("Error"), err);
365 qWarning( qPrintable( err ));
370 //******************************************************
371 bool XGUI_Workshop::activateModule()
373 myPartSetModule = loadModule("PartSet");
374 if (!myPartSetModule)
376 myPartSetModule->createFeatures();
380 //******************************************************
381 void XGUI_Workshop::updateCommandStatus()
383 XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
385 QList<XGUI_Command*> aCommands = aMenuBar->features();
386 QList<XGUI_Command*>::const_iterator aIt;
388 std::shared_ptr<ModelAPI_PluginManager> aMgr = ModelAPI_PluginManager::get();
389 if (aMgr->hasRootDocument()) {
390 XGUI_Command* aUndoCmd;
391 XGUI_Command* aRedoCmd;
392 for (aIt = aCommands.constBegin(); aIt != aCommands.constEnd(); ++aIt) {
393 if ((*aIt)->id() == "UNDO_CMD")
395 else if ((*aIt)->id() == "REDO_CMD")
397 else // Enable all commands
400 std::shared_ptr<ModelAPI_Document> aDoc = aMgr->rootDocument();
401 aUndoCmd->setEnabled(aDoc->canUndo());
402 aRedoCmd->setEnabled(aDoc->canRedo());
404 for (aIt = aCommands.constBegin(); aIt != aCommands.constEnd(); ++aIt) {
405 if ((*aIt)->id() == "NEW_CMD")
407 else if ((*aIt)->id() == "EXIT_CMD")