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(operationStarted()), this, SLOT(onOperationStarted()));
54 connect(myOperationMgr, SIGNAL(operationStopped(ModuleBase_Operation*)),
55 this, SLOT(onOperationStopped(ModuleBase_Operation*)));
58 //******************************************************
59 XGUI_Workshop::~XGUI_Workshop(void)
63 //******************************************************
64 void XGUI_Workshop::startApplication()
67 //Initialize event listening
68 Event_Loop* aLoop = Event_Loop::loop();
69 //TODO(sbh): Implement static method to extract event id [SEID]
70 Event_ID aFeatureId = aLoop->eventByName("FeatureEvent");
71 aLoop->registerListener(this, aFeatureId);
72 Event_ID aPartSetId = aLoop->eventByName("PartSetModuleEvent");
73 aLoop->registerListener(this, aPartSetId);
77 updateCommandStatus();
79 // Testing of document creation
80 //boost::shared_ptr<ModelAPI_PluginManager> aMgr = ModelAPI_PluginManager::get();
81 //boost::shared_ptr<ModelAPI_Feature> aPoint1 = aMgr->rootDocument()->addFeature("Point");
82 //boost::shared_ptr<ModelAPI_Feature> aPart = aMgr->rootDocument()->addFeature("Part");
84 //aMgr->setCurrentDocument(aPart->data()->docRef("PartDocument")->value());
85 //boost::shared_ptr<ModelAPI_Feature> aPoint2 = aMgr->rootDocument()->addFeature("Point");
86 //aPoint2 = aMgr->rootDocument()->addFeature("Point");
88 //aPart = aMgr->rootDocument()->addFeature("Part");
92 //******************************************************
93 void XGUI_Workshop::initMenu()
95 XGUI_Workbench* aPage = myMainWindow->menuObject()->generalPage();
97 // File commands group
98 XGUI_MenuGroupPanel* aGroup = aPage->addGroup("Default");
100 XGUI_Command* aCommand;
102 aCommand = aGroup->addFeature("SAVE_CMD", tr("Save..."), tr("Save the document"),
103 QIcon(":pictures/save.png"), QKeySequence::Save);
104 aCommand->connectTo(this, SLOT(onSave()));
105 //aCommand->disable();
107 aCommand = aGroup->addFeature("UNDO_CMD", tr("Undo"), tr("Undo last command"),
108 QIcon(":pictures/undo.png"), QKeySequence::Undo);
109 aCommand->connectTo(this, SLOT(onUndo()));
111 aCommand = aGroup->addFeature("REDO_CMD", tr("Redo"), tr("Redo last command"),
112 QIcon(":pictures/redo.png"), QKeySequence::Redo);
113 aCommand->connectTo(this, SLOT(onRedo()));
115 aCommand = aGroup->addFeature("REBUILD_CMD", tr("Rebuild"), tr("Rebuild data objects"),
116 QIcon(":pictures/rebuild.png"));
118 aCommand = aGroup->addFeature("SAVEAS_CMD", tr("Save as..."), tr("Save the document into a file"),
119 QIcon(":pictures/save.png"));
120 aCommand->connectTo(this, SLOT(onSaveAs()));
121 //aCommand->disable();
123 aCommand = aGroup->addFeature("OPEN_CMD", tr("Open..."), tr("Open a new document"),
124 QIcon(":pictures/open.png"), QKeySequence::Open);
125 aCommand->connectTo(this, SLOT(onOpen()));
127 //aCommand = aGroup->addFeature("NEW_CMD", tr("New"), tr("Create a new document"),
128 // QIcon(":pictures/new.png"), QKeySequence::New);
129 //aCommand->connectTo(this, SLOT(onNew()));
131 aCommand = aGroup->addFeature("EXIT_CMD", tr("Exit"), tr("Exit application"),
132 QIcon(":pictures/close.png"), QKeySequence::Close);
133 aCommand->connectTo(this, SLOT(onExit()));
137 //******************************************************
138 XGUI_Workbench* XGUI_Workshop::addWorkbench(const QString& theName)
140 XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
141 return aMenuBar->addWorkbench(theName);
144 //******************************************************
145 void XGUI_Workshop::processEvent(const Event_Message* theMessage)
147 static Event_ID aFeatureId = Event_Loop::loop()->eventByName("FeatureEvent");
148 if (theMessage->eventID() == aFeatureId) {
149 const Config_FeatureMessage* aFeatureMsg =
150 dynamic_cast<const Config_FeatureMessage*>(theMessage);
151 addFeature(aFeatureMsg);
154 const Config_PointerMessage* aPartSetMsg =
155 dynamic_cast<const Config_PointerMessage*>(theMessage);
157 ModuleBase_PropPanelOperation* anOperation =
158 (ModuleBase_PropPanelOperation*)(aPartSetMsg->pointer());
160 if (myOperationMgr->startOperation(anOperation)) {
161 if (anOperation->isPerformedImmediately())
162 anOperation->commit();
168 qDebug() << "XGUI_Workshop::ProcessEvent: "
169 << "Catch message, but it can not be processed.";
174 //******************************************************
175 void XGUI_Workshop::onOperationStarted()
177 ModuleBase_PropPanelOperation* aOperation =
178 (ModuleBase_PropPanelOperation*)(myOperationMgr->currentOperation());
180 if(aOperation->xmlRepresentation().isEmpty()) { //!< No need for property panel
182 connectWithOperation(aOperation);
183 QWidget* aPropWidget = myMainWindow->findChild<QWidget*>(XGUI::PROP_PANEL_WDG);
184 qDeleteAll(aPropWidget->children());
186 myMainWindow->showPropertyPanel();
188 XGUI_WidgetFactory aFactory = XGUI_WidgetFactory(aOperation);
189 aFactory.createWidget(aPropWidget);
190 myMainWindow->setPropertyPannelTitle(aOperation->description());
194 //******************************************************
195 void XGUI_Workshop::onOperationStopped(ModuleBase_Operation* theOperation)
197 myMainWindow->hidePropertyPanel();
198 updateCommandStatus();
200 XGUI_MainMenu* aMenu = myMainWindow->menuObject();
201 aMenu->restoreCommandState();
207 void XGUI_Workshop::addFeature(const Config_FeatureMessage* theMessage)
211 qDebug() << "XGUI_Workshop::addFeature: NULL message.";
215 //Find or create Workbench
216 XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
217 QString aWchName = QString::fromStdString(theMessage->workbenchId());
218 XGUI_Workbench* aPage = aMenuBar->findWorkbench(aWchName);
220 aPage = addWorkbench(aWchName);
222 //Find or create Group
223 QString aGroupName = QString::fromStdString(theMessage->groupId());
224 XGUI_MenuGroupPanel* aGroup = aPage->findGroup(aGroupName);
226 aGroup = aPage->addGroup(aGroupName);
228 bool isUsePropPanel = theMessage->isUseInput();
230 XGUI_Command* aCommand = aGroup->addFeature(QString::fromStdString(theMessage->id()),
231 QString::fromStdString(theMessage->text()),
232 QString::fromStdString(theMessage->tooltip()),
233 QIcon(theMessage->icon().c_str()),
234 QKeySequence(), isUsePropPanel);
236 connect(aCommand, SIGNAL(toggled(bool)),
237 myMainWindow->menuObject(), SLOT(onFeatureChecked(bool)));
238 myPartSetModule->featureCreated(aCommand);
242 * Makes a signal/slot connections between Property Panel
243 * and given operation. The given operation becomes a
244 * current operation and previous operation if exists
246 void XGUI_Workshop::connectWithOperation(ModuleBase_Operation* theOperation)
248 QDockWidget* aPanel = myMainWindow->findChild<QDockWidget*>(XGUI::PROP_PANEL);
249 QPushButton* aOkBtn = aPanel->findChild<QPushButton*>(XGUI::PROP_PANEL_OK);
250 connect(aOkBtn, SIGNAL(clicked()), theOperation, SLOT(commit()));
251 QPushButton* aCancelBtn = aPanel->findChild<QPushButton*>(XGUI::PROP_PANEL_CANCEL);
252 connect(aCancelBtn, SIGNAL(clicked()), theOperation, SLOT(abort()));
254 XGUI_MainMenu* aMenu = myMainWindow->menuObject();
255 XGUI_Command* aCommand = aMenu->feature(theOperation->operationId());
256 //Abort operation on uncheck the command
257 connect(aCommand, SIGNAL(toggled(bool)), theOperation, SLOT(setRunning(bool)));
261 //******************************************************
262 void XGUI_Workshop::onExit()
267 //******************************************************
268 void XGUI_Workshop::onNew()
270 QApplication::setOverrideCursor(Qt::WaitCursor);
271 if (myMainWindow->objectBrowser() == 0) {
272 myMainWindow->createDockWidgets();
273 mySelector->connectObjectBrowser(myMainWindow->objectBrowser());
275 myMainWindow->showObjectBrowser();
276 myMainWindow->showPythonConsole();
277 QMdiSubWindow* aWnd = myMainWindow->viewer()->createView();
278 aWnd->showMaximized();
279 updateCommandStatus();
280 QApplication::restoreOverrideCursor();
283 //******************************************************
284 void XGUI_Workshop::onOpen()
286 //QString aFileName = QFileDialog::getOpenFileName(mainWindow());
287 updateCommandStatus();
290 //******************************************************
291 void XGUI_Workshop::onSave()
293 updateCommandStatus();
296 //******************************************************
297 void XGUI_Workshop::onSaveAs()
299 //QString aFileName = QFileDialog::getSaveFileName(mainWindow());
300 updateCommandStatus();
303 //******************************************************
304 void XGUI_Workshop::onUndo()
306 myMainWindow->objectBrowser()->setCurrentIndex(QModelIndex());
307 boost::shared_ptr<ModelAPI_PluginManager> aMgr = ModelAPI_PluginManager::get();
308 boost::shared_ptr<ModelAPI_Document> aDoc = aMgr->rootDocument();
310 updateCommandStatus();
313 //******************************************************
314 void XGUI_Workshop::onRedo()
316 myMainWindow->objectBrowser()->setCurrentIndex(QModelIndex());
317 boost::shared_ptr<ModelAPI_PluginManager> aMgr = ModelAPI_PluginManager::get();
318 boost::shared_ptr<ModelAPI_Document> aDoc = aMgr->rootDocument();
320 updateCommandStatus();
323 //******************************************************
324 XGUI_Module* XGUI_Workshop::loadModule(const QString& theModule)
326 QString libName = library(theModule);
327 if (libName.isEmpty()) {
329 qPrintable( tr( "Information about module \"%1\" doesn't exist." ).arg( theModule ) ));
334 CREATE_FUNC crtInst = 0;
338 HINSTANCE modLib = ::LoadLibrary((LPTSTR) qPrintable(libName));
342 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
343 0, ::GetLastError(), 0, (LPTSTR) & lpMsgBuf, 0, 0);
344 QString aMsg((char*) &lpMsgBuf);
345 err = QString("Failed to load %1. %2").arg(libName).arg(aMsg);
346 ::LocalFree(lpMsgBuf);
348 crtInst = (CREATE_FUNC) ::GetProcAddress(modLib, CREATE_MODULE);
352 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM
353 | FORMAT_MESSAGE_IGNORE_INSERTS,
354 0, ::GetLastError(), 0, (LPTSTR) & lpMsgBuf, 0, 0);
355 QString aMsg((char*) &lpMsgBuf);
356 err = QString("Failed to find %1 function. %2").arg( CREATE_MODULE).arg(aMsg);
357 ::LocalFree(lpMsgBuf);
361 void* modLib = dlopen( libName.toLatin1(), RTLD_LAZY );
363 err = QString( "Can not load library %1. %2" ).arg( libName ).arg( dlerror() );
366 crtInst = (CREATE_FUNC)dlsym( modLib, CREATE_MODULE );
368 err = QString( "Failed to find function %1. %2" ).arg( CREATE_MODULE ).arg( dlerror() );
372 XGUI_Module* aModule = crtInst ? crtInst(this) : 0;
374 if (!err.isEmpty()) {
375 if (mainWindow() && mainWindow()->isVisible())
376 QMessageBox::warning(mainWindow(), tr("Error"), err);
378 qWarning( qPrintable( err ));
383 //******************************************************
384 bool XGUI_Workshop::activateModule()
386 myPartSetModule = loadModule("PartSet");
387 if (!myPartSetModule)
389 myPartSetModule->createFeatures();
393 //******************************************************
394 void XGUI_Workshop::updateCommandStatus()
396 XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
398 QList<XGUI_Command*> aCommands = aMenuBar->features();
399 QList<XGUI_Command*>::const_iterator aIt;
401 boost::shared_ptr<ModelAPI_PluginManager> aMgr = ModelAPI_PluginManager::get();
402 if (aMgr->hasRootDocument()) {
403 XGUI_Command* aUndoCmd;
404 XGUI_Command* aRedoCmd;
405 for (aIt = aCommands.constBegin(); aIt != aCommands.constEnd(); ++aIt) {
406 if ((*aIt)->id() == "UNDO_CMD")
408 else if ((*aIt)->id() == "REDO_CMD")
410 else // Enable all commands
413 boost::shared_ptr<ModelAPI_Document> aDoc = aMgr->rootDocument();
414 aUndoCmd->setEnabled(aDoc->canUndo());
415 aRedoCmd->setEnabled(aDoc->canRedo());
417 for (aIt = aCommands.constBegin(); aIt != aCommands.constEnd(); ++aIt) {
418 if ((*aIt)->id() == "NEW_CMD")
420 else if ((*aIt)->id() == "EXIT_CMD")