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"
16 #include <ModelAPI_PluginManager.h>
17 #include <ModelAPI_Feature.h>
18 #include <ModelAPI_Data.h>
19 #include <ModelAPI_AttributeDocRef.h>
21 #include <Event_Loop.h>
22 #include <ModuleBase_PropPanelOperation.h>
23 #include <ModuleBase_Operation.h>
24 #include <Config_FeatureMessage.h>
25 #include <Config_PointerMessage.h>
27 #include <QApplication>
28 #include <QFileDialog>
29 #include <QMessageBox>
30 #include <QMdiSubWindow>
31 #include <QPushButton>
32 #include <QDockWidget>
44 XGUI_Workshop::XGUI_Workshop()
46 myCurrentOperation(NULL),
49 myMainWindow = new XGUI_MainWindow();
50 mySelector = new XGUI_SelectionMgr(this);
51 myDisplayer = new XGUI_Displayer(myMainWindow->viewer());
54 //******************************************************
55 XGUI_Workshop::~XGUI_Workshop(void)
59 //******************************************************
60 void XGUI_Workshop::startApplication()
63 //Initialize event listening
64 Event_Loop* aLoop = Event_Loop::loop();
65 //TODO(sbh): Implement static method to extract event id [SEID]
66 Event_ID aFeatureId = aLoop->eventByName("FeatureEvent");
67 aLoop->registerListener(this, aFeatureId);
68 Event_ID aPartSetId = aLoop->eventByName("PartSetModuleEvent");
69 aLoop->registerListener(this, aPartSetId);
73 updateCommandStatus();
75 // Testing of document creation
76 //std::shared_ptr<ModelAPI_PluginManager> aMgr = ModelAPI_PluginManager::get();
77 //std::shared_ptr<ModelAPI_Feature> aPoint1 = aMgr->rootDocument()->addFeature("Point");
78 //std::shared_ptr<ModelAPI_Feature> aPart = aMgr->rootDocument()->addFeature("Part");
80 //aMgr->setCurrentDocument(aPart->data()->docRef("PartDocument")->value());
81 //std::shared_ptr<ModelAPI_Feature> aPoint2 = aMgr->rootDocument()->addFeature("Point");
82 //aPoint2 = aMgr->rootDocument()->addFeature("Point");
84 //aPart = aMgr->rootDocument()->addFeature("Part");
88 //******************************************************
89 void XGUI_Workshop::initMenu()
91 XGUI_Workbench* aPage = myMainWindow->menuObject()->generalPage();
93 // File commands group
94 XGUI_MenuGroupPanel* aGroup = aPage->addGroup("Default");
96 XGUI_Command* aCommand;
98 aCommand = aGroup->addFeature("SAVE_CMD", tr("Save..."), tr("Save the document"),
99 QIcon(":pictures/save.png"), QKeySequence::Save);
100 aCommand->connectTo(this, SLOT(onSave()));
101 //aCommand->disable();
103 aCommand = aGroup->addFeature("UNDO_CMD", tr("Undo"), tr("Undo last command"),
104 QIcon(":pictures/undo.png"), QKeySequence::Undo);
105 aCommand->connectTo(this, SLOT(onUndo()));
107 aCommand = aGroup->addFeature("REDO_CMD", tr("Redo"), tr("Redo last command"),
108 QIcon(":pictures/redo.png"), QKeySequence::Redo);
109 aCommand->connectTo(this, SLOT(onRedo()));
111 aCommand = aGroup->addFeature("REBUILD_CMD", tr("Rebuild"), tr("Rebuild data objects"),
112 QIcon(":pictures/rebuild.png"));
114 aCommand = aGroup->addFeature("SAVEAS_CMD", tr("Save as..."), tr("Save the document into a file"),
115 QIcon(":pictures/save.png"));
116 aCommand->connectTo(this, SLOT(onSaveAs()));
117 //aCommand->disable();
119 aCommand = aGroup->addFeature("OPEN_CMD", tr("Open..."), tr("Open a new document"),
120 QIcon(":pictures/open.png"), QKeySequence::Open);
121 aCommand->connectTo(this, SLOT(onOpen()));
123 //aCommand = aGroup->addFeature("NEW_CMD", tr("New"), tr("Create a new document"),
124 // QIcon(":pictures/new.png"), QKeySequence::New);
125 //aCommand->connectTo(this, SLOT(onNew()));
127 aCommand = aGroup->addFeature("EXIT_CMD", tr("Exit"), tr("Exit application"),
128 QIcon(":pictures/close.png"), QKeySequence::Close);
129 aCommand->connectTo(this, SLOT(onExit()));
133 //******************************************************
134 XGUI_Workbench* XGUI_Workshop::addWorkbench(const QString& theName)
136 XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
137 return aMenuBar->addWorkbench(theName);
140 //******************************************************
141 void XGUI_Workshop::processEvent(const Event_Message* theMessage)
143 static Event_ID aFeatureId = Event_Loop::loop()->eventByName("FeatureEvent");
144 if (theMessage->eventID() == aFeatureId) {
145 const Config_FeatureMessage* aFeatureMsg =
146 dynamic_cast<const Config_FeatureMessage*>(theMessage);
147 addFeature(aFeatureMsg);
150 const Config_PointerMessage* aPartSetMsg =
151 dynamic_cast<const Config_PointerMessage*>(theMessage);
153 ModuleBase_PropPanelOperation* aOperation =
154 (ModuleBase_PropPanelOperation*)(aPartSetMsg->pointer());
155 setCurrentOperation(aOperation);
156 if(aOperation->xmlRepresentation().isEmpty()) { //!< No need for property panel
157 myCurrentOperation->start();
158 myCurrentOperation->commit();
159 updateCommandStatus();
161 fillPropertyPanel(aOperation);
167 qDebug() << "XGUI_Workshop::ProcessEvent: "
168 << "Catch message, but it can not be processed.";
176 void XGUI_Workshop::addFeature(const Config_FeatureMessage* theMessage)
180 qDebug() << "XGUI_Workshop::addFeature: NULL message.";
184 //Find or create Workbench
185 XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
186 QString aWchName = QString::fromStdString(theMessage->workbenchId());
187 XGUI_Workbench* aPage = aMenuBar->findWorkbench(aWchName);
189 aPage = addWorkbench(aWchName);
191 //Find or create Group
192 QString aGroupName = QString::fromStdString(theMessage->groupId());
193 XGUI_MenuGroupPanel* aGroup = aPage->findGroup(aGroupName);
195 aGroup = aPage->addGroup(aGroupName);
197 bool isUsePropPanel = theMessage->isUseInput();
199 XGUI_Command* aCommand = aGroup->addFeature(QString::fromStdString(theMessage->id()),
200 QString::fromStdString(theMessage->text()),
201 QString::fromStdString(theMessage->tooltip()),
202 QIcon(theMessage->icon().c_str()),
203 QKeySequence(), isUsePropPanel);
205 connect(aCommand, SIGNAL(toggled(bool)),
206 myMainWindow->menuObject(), SLOT(onFeatureChecked(bool)));
207 myPartSetModule->featureCreated(aCommand);
213 void XGUI_Workshop::fillPropertyPanel(ModuleBase_PropPanelOperation* theOperation)
215 connectWithOperation(theOperation);
216 QWidget* aPropWidget = myMainWindow->findChild<QWidget*>(XGUI::PROP_PANEL_WDG);
217 qDeleteAll(aPropWidget->children());
218 theOperation->start();
219 XGUI_WidgetFactory aFactory = XGUI_WidgetFactory(theOperation);
220 aFactory.createWidget(aPropWidget);
221 myMainWindow->setPropertyPannelTitle(theOperation->description());
224 void XGUI_Workshop::setCurrentOperation(ModuleBase_Operation* theOperation)
226 //FIXME: Ask user about aborting of current operation?
227 if (myCurrentOperation) {
228 //TODO get isOperation from document
229 if (myCurrentOperation->isRunning())
230 myCurrentOperation->abort();
232 myCurrentOperation->deleteLater();
234 myCurrentOperation = theOperation;
238 * Makes a signal/slot connections between Property Panel
239 * and given operation. The given operation becomes a
240 * current operation and previous operation if exists
242 void XGUI_Workshop::connectWithOperation(ModuleBase_Operation* theOperation)
244 QDockWidget* aPanel = myMainWindow->findChild<QDockWidget*>(XGUI::PROP_PANEL);
245 QPushButton* aOkBtn = aPanel->findChild<QPushButton*>(XGUI::PROP_PANEL_OK);
246 connect(aOkBtn, SIGNAL(clicked()), theOperation, SLOT(commit()));
247 QPushButton* aCancelBtn = aPanel->findChild<QPushButton*>(XGUI::PROP_PANEL_CANCEL);
248 connect(aCancelBtn, SIGNAL(clicked()), theOperation, SLOT(abort()));
250 connect(theOperation, SIGNAL(started()), myMainWindow, SLOT(showPropertyPanel()));
251 connect(theOperation, SIGNAL(stopped()), myMainWindow, SLOT(hidePropertyPanel()));
252 connect(theOperation, SIGNAL(stopped()), this, SLOT(updateCommandStatus()));
254 XGUI_MainMenu* aMenu = myMainWindow->menuObject();
255 connect(theOperation, SIGNAL(stopped()), aMenu, SLOT(restoreCommandState()));
257 XGUI_Command* aCommand = aMenu->feature(theOperation->operationId());
258 //Abort operation on uncheck the command
259 connect(aCommand, SIGNAL(toggled(bool)), theOperation, SLOT(setRunning(bool)));
263 //******************************************************
264 void XGUI_Workshop::onExit()
269 //******************************************************
270 void XGUI_Workshop::onNew()
272 QApplication::setOverrideCursor(Qt::WaitCursor);
273 if (myMainWindow->objectBrowser() == 0) {
274 myMainWindow->createDockWidgets();
275 mySelector->connectObjectBrowser(myMainWindow->objectBrowser());
277 myMainWindow->showObjectBrowser();
278 myMainWindow->showPythonConsole();
279 QMdiSubWindow* aWnd = myMainWindow->viewer()->createView();
280 aWnd->showMaximized();
281 updateCommandStatus();
282 QApplication::restoreOverrideCursor();
285 //******************************************************
286 void XGUI_Workshop::onOpen()
288 //QString aFileName = QFileDialog::getOpenFileName(mainWindow());
289 updateCommandStatus();
292 //******************************************************
293 void XGUI_Workshop::onSave()
295 updateCommandStatus();
298 //******************************************************
299 void XGUI_Workshop::onSaveAs()
301 //QString aFileName = QFileDialog::getSaveFileName(mainWindow());
302 updateCommandStatus();
305 //******************************************************
306 void XGUI_Workshop::onUndo()
308 myMainWindow->objectBrowser()->setCurrentIndex(QModelIndex());
309 std::shared_ptr<ModelAPI_PluginManager> aMgr = ModelAPI_PluginManager::get();
310 std::shared_ptr<ModelAPI_Document> aDoc = aMgr->rootDocument();
312 updateCommandStatus();
315 //******************************************************
316 void XGUI_Workshop::onRedo()
318 myMainWindow->objectBrowser()->setCurrentIndex(QModelIndex());
319 std::shared_ptr<ModelAPI_PluginManager> aMgr = ModelAPI_PluginManager::get();
320 std::shared_ptr<ModelAPI_Document> aDoc = aMgr->rootDocument();
322 updateCommandStatus();
325 //******************************************************
326 XGUI_Module* XGUI_Workshop::loadModule(const QString& theModule)
328 QString libName = library(theModule);
329 if (libName.isEmpty()) {
331 qPrintable( tr( "Information about module \"%1\" doesn't exist." ).arg( theModule ) ));
336 CREATE_FUNC crtInst = 0;
340 HINSTANCE modLib = ::LoadLibrary((LPTSTR) qPrintable(libName));
344 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
345 0, ::GetLastError(), 0, (LPTSTR) & lpMsgBuf, 0, 0);
346 QString aMsg((char*) &lpMsgBuf);
347 err = QString("Failed to load %1. %2").arg(libName).arg(aMsg);
348 ::LocalFree(lpMsgBuf);
350 crtInst = (CREATE_FUNC) ::GetProcAddress(modLib, CREATE_MODULE);
354 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM
355 | FORMAT_MESSAGE_IGNORE_INSERTS,
356 0, ::GetLastError(), 0, (LPTSTR) & lpMsgBuf, 0, 0);
357 QString aMsg((char*) &lpMsgBuf);
358 err = QString("Failed to find %1 function. %2").arg( CREATE_MODULE).arg(aMsg);
359 ::LocalFree(lpMsgBuf);
363 void* modLib = dlopen( libName.toLatin1(), RTLD_LAZY );
365 err = QString( "Can not load library %1. %2" ).arg( libName ).arg( dlerror() );
368 crtInst = (CREATE_FUNC)dlsym( modLib, CREATE_MODULE );
370 err = QString( "Failed to find function %1. %2" ).arg( CREATE_MODULE ).arg( dlerror() );
374 XGUI_Module* aModule = crtInst ? crtInst(this) : 0;
376 if (!err.isEmpty()) {
377 if (mainWindow() && mainWindow()->isVisible())
378 QMessageBox::warning(mainWindow(), tr("Error"), err);
380 qWarning( qPrintable( err ));
385 //******************************************************
386 bool XGUI_Workshop::activateModule()
388 myPartSetModule = loadModule("PartSet");
389 if (!myPartSetModule)
391 myPartSetModule->createFeatures();
395 //******************************************************
396 void XGUI_Workshop::updateCommandStatus()
398 XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
400 QList<XGUI_Command*> aCommands = aMenuBar->features();
401 QList<XGUI_Command*>::const_iterator aIt;
403 std::shared_ptr<ModelAPI_PluginManager> aMgr = ModelAPI_PluginManager::get();
404 if (aMgr->hasRootDocument()) {
405 XGUI_Command* aUndoCmd;
406 XGUI_Command* aRedoCmd;
407 for (aIt = aCommands.constBegin(); aIt != aCommands.constEnd(); ++aIt) {
408 if ((*aIt)->id() == "UNDO_CMD")
410 else if ((*aIt)->id() == "REDO_CMD")
412 else // Enable all commands
415 std::shared_ptr<ModelAPI_Document> aDoc = aMgr->rootDocument();
416 aUndoCmd->setEnabled(aDoc->canUndo());
417 aRedoCmd->setEnabled(aDoc->canRedo());
419 for (aIt = aCommands.constBegin(); aIt != aCommands.constEnd(); ++aIt) {
420 if ((*aIt)->id() == "NEW_CMD")
422 else if ((*aIt)->id() == "EXIT_CMD")