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 "ModuleBase_WidgetFactory.h"
12 #include "XGUI_SelectionMgr.h"
13 #include "XGUI_ObjectsBrowser.h"
14 #include "XGUI_Displayer.h"
15 #include "XGUI_OperationMgr.h"
16 #include "XGUI_SalomeConnector.h"
17 #include "XGUI_SalomeViewer.h"
18 #include "XGUI_ActionsMgr.h"
19 #include "XGUI_ErrorDialog.h"
20 #include "XGUI_ViewerProxy.h"
21 #include "XGUI_PropertyPanel.h"
23 #include <Model_Events.h>
24 #include <ModelAPI_PluginManager.h>
25 #include <ModelAPI_Feature.h>
26 #include <ModelAPI_Data.h>
27 #include <ModelAPI_AttributeDocRef.h>
29 #include <Events_Loop.h>
30 #include <Events_Error.h>
31 #include <ModuleBase_Operation.h>
32 #include <ModuleBase_Operation.h>
33 #include <ModuleBase_OperationDescription.h>
34 #include <Config_FeatureMessage.h>
35 #include <Config_PointerMessage.h>
37 #include <QApplication>
38 #include <QFileDialog>
39 #include <QMessageBox>
40 #include <QMdiSubWindow>
41 #include <QPushButton>
42 #include <QDockWidget>
55 XGUI_Workshop::XGUI_Workshop(XGUI_SalomeConnector* theConnector)
57 myCurrentFile(QString()),
58 myPartSetModule(NULL),
59 mySalomeConnector(theConnector),
64 myMainWindow = mySalomeConnector? 0 : new XGUI_MainWindow();
66 myDisplayer = new XGUI_Displayer(this);
68 mySelector = new XGUI_SelectionMgr(this);
69 connect(mySelector, SIGNAL(selectionChanged()), this, SLOT(changeCurrentDocument()));
71 myOperationMgr = new XGUI_OperationMgr(this);
72 myActionsMgr = new XGUI_ActionsMgr(this);
73 myErrorDlg = new XGUI_ErrorDialog(myMainWindow);
75 myViewerProxy = new XGUI_ViewerProxy(this);
77 connect(myOperationMgr, SIGNAL(operationStarted()), this, SLOT(onOperationStarted()));
78 connect(myOperationMgr, SIGNAL(operationStopped(ModuleBase_Operation*)),
79 this, SLOT(onOperationStopped(ModuleBase_Operation*)));
80 connect(this, SIGNAL(errorOccurred(const QString&)), myErrorDlg, SLOT(addError(const QString&)));
83 //******************************************************
84 XGUI_Workshop::~XGUI_Workshop(void)
88 //******************************************************
89 void XGUI_Workshop::startApplication()
92 //Initialize event listening
93 Events_Loop* aLoop = Events_Loop::loop();
94 aLoop->registerListener(this, Events_Error::errorID()); //!< Listening application errors.
95 //TODO(sbh): Implement static method to extract event id [SEID]
96 Events_ID aFeatureId = aLoop->eventByName(EVENT_FEATURE_LOADED);
97 aLoop->registerListener(this, aFeatureId);
98 Events_ID aPartSetId = aLoop->eventByName("PartSetModuleEvent");
99 aLoop->registerListener(this, aPartSetId);
100 Events_ID aFeatureUpdatedId = aLoop->eventByName(EVENT_FEATURE_UPDATED);
101 aLoop->registerListener(this, aFeatureUpdatedId);
104 myMainWindow->show();
105 updateCommandStatus();
110 //******************************************************
111 void XGUI_Workshop::initMenu()
113 if (isSalomeMode()) {
114 // Create only Undo, Redo commands
115 salomeConnector()->addEditCommand("UNDO_CMD",
116 tr("Undo"), tr("Undo last command"),
117 QIcon(":pictures/undo.png"),
118 false, this, SLOT(onUndo()),
120 salomeConnector()->addEditCommand("REDO_CMD",
121 tr("Redo"), tr("Redo last command"),
122 QIcon(":pictures/redo.png"),
123 false, this, SLOT(onRedo()),
125 salomeConnector()->addEditMenuSeparator();
128 XGUI_Workbench* aPage = myMainWindow->menuObject()->generalPage();
130 // File commands group
131 XGUI_MenuGroupPanel* aGroup = aPage->addGroup("Default");
133 XGUI_Command* aCommand;
135 aCommand = aGroup->addFeature("SAVE_CMD", tr("Save..."), tr("Save the document"),
136 QIcon(":pictures/save.png"), QKeySequence::Save);
137 aCommand->connectTo(this, SLOT(onSave()));
138 //aCommand->disable();
140 aCommand = aGroup->addFeature("UNDO_CMD", tr("Undo"), tr("Undo last command"),
141 QIcon(":pictures/undo.png"), QKeySequence::Undo);
142 aCommand->connectTo(this, SLOT(onUndo()));
144 aCommand = aGroup->addFeature("REDO_CMD", tr("Redo"), tr("Redo last command"),
145 QIcon(":pictures/redo.png"), QKeySequence::Redo);
146 aCommand->connectTo(this, SLOT(onRedo()));
148 aCommand = aGroup->addFeature("REBUILD_CMD", tr("Rebuild"), tr("Rebuild data objects"),
149 QIcon(":pictures/rebuild.png"));
151 aCommand = aGroup->addFeature("SAVEAS_CMD", tr("Save as..."), tr("Save the document into a file"),
152 QIcon(":pictures/save.png"));
153 aCommand->connectTo(this, SLOT(onSaveAs()));
154 //aCommand->disable();
156 aCommand = aGroup->addFeature("OPEN_CMD", tr("Open..."), tr("Open a new document"),
157 QIcon(":pictures/open.png"), QKeySequence::Open);
158 aCommand->connectTo(this, SLOT(onOpen()));
160 //aCommand = aGroup->addFeature("NEW_CMD", tr("New"), tr("Create a new document"),
161 // QIcon(":pictures/new.png"), QKeySequence::New);
162 //aCommand->connectTo(this, SLOT(onNew()));
164 aCommand = aGroup->addFeature("EXIT_CMD", tr("Exit"), tr("Exit application"),
165 QIcon(":pictures/close.png"), QKeySequence::Close);
166 aCommand->connectTo(this, SLOT(onExit()));
170 //******************************************************
171 XGUI_Workbench* XGUI_Workshop::addWorkbench(const QString& theName)
173 XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
174 return aMenuBar->addWorkbench(theName);
177 //******************************************************
178 void XGUI_Workshop::processEvent(const Events_Message* theMessage)
180 //A message to start feature creation received.
181 static Events_ID aFeatureLoadedId = Events_Loop::loop()->eventByName(EVENT_FEATURE_LOADED);
182 if (theMessage->eventID() == aFeatureLoadedId) {
183 const Config_FeatureMessage* aFeatureMsg = dynamic_cast<const Config_FeatureMessage*>(theMessage);
184 addFeature(aFeatureMsg);
187 //Update property panel on corresponding message. If there is no current operation (no
188 //property panel), or received message has different feature to the current - do nothing.
189 static Events_ID aFeatureUpdatedId = Events_Loop::loop()->eventByName(EVENT_FEATURE_UPDATED);
190 if (theMessage->eventID() == aFeatureUpdatedId && myOperationMgr->hasOperation())
192 const Model_FeatureUpdatedMessage* anUpdateMsg =
193 dynamic_cast<const Model_FeatureUpdatedMessage*>(theMessage);
194 boost::shared_ptr<ModelAPI_Feature> aNewFeature = anUpdateMsg->feature();
195 boost::shared_ptr<ModelAPI_Feature> aCurrentFeature = myOperationMgr->currentOperation()->feature();
196 if(aNewFeature == aCurrentFeature) {
197 myPropertyPanel->updateContentWidget(aCurrentFeature);
200 //An operation passed by message. Start it, process and commit.
201 const Config_PointerMessage* aPartSetMsg = dynamic_cast<const Config_PointerMessage*>(theMessage);
203 ModuleBase_Operation* anOperation =
204 (ModuleBase_Operation*)(aPartSetMsg->pointer());
206 if (myOperationMgr->startOperation(anOperation)) {
207 if (anOperation->getDescription()->xmlRepresentation().isEmpty()) {
208 anOperation->commit();
209 updateCommandStatus();
214 //Show error dialog if error message received.
215 const Events_Error* anAppError = dynamic_cast<const Events_Error*>(theMessage);
217 emit errorOccurred(QString::fromLatin1(anAppError->description()));
220 myErrorDlg->activateWindow();
225 //******************************************************
226 void XGUI_Workshop::onOperationStarted()
228 ModuleBase_Operation* aOperation = myOperationMgr->currentOperation();
230 if(!aOperation->getDescription()->xmlRepresentation().isEmpty()) { //!< No need for property panel
231 connectWithOperation(aOperation);
235 ModuleBase_WidgetFactory aFactory = ModuleBase_WidgetFactory(aOperation);
236 QWidget* aContent = myPropertyPanel->contentWidget();
237 qDeleteAll(aContent->children());
238 aFactory.createWidget(aContent);
239 myPropertyPanel->setModelWidgets(aFactory.getModelWidgets());
240 myPropertyPanel->setWindowTitle(aOperation->getDescription()->description());
244 //******************************************************
245 void XGUI_Workshop::onOperationStopped(ModuleBase_Operation* theOperation)
247 ModuleBase_Operation* aOperation = myOperationMgr->currentOperation();
249 if(aOperation->getDescription()->xmlRepresentation().isEmpty()) { //!< No need for property panel
250 updateCommandStatus();
253 updateCommandStatus();
254 myActionsMgr->restoreCommandState();
261 void XGUI_Workshop::addFeature(const Config_FeatureMessage* theMessage)
265 qDebug() << "XGUI_Workshop::addFeature: NULL message.";
269 //Find or create Workbench
270 QString aWchName = QString::fromStdString(theMessage->workbenchId());
271 QString aNestedFeatures = QString::fromStdString(theMessage->nestedFeatures());
272 bool isUsePropPanel = theMessage->isUseInput();
273 if (isSalomeMode()) {
274 QString aId = QString::fromStdString(theMessage->id());
275 salomeConnector()->addFeature(aWchName,
276 QString::fromStdString(theMessage->id()),
278 QString::fromStdString(theMessage->tooltip()),
279 QIcon(theMessage->icon().c_str()),
280 isUsePropPanel, this,
281 SLOT(onFeatureTriggered()), QKeySequence());
282 myActionsMgr->addCommand(aId, salomeConnector()->command(aId));
283 salomeConnector()->setNestedActions(aId, aNestedFeatures.split(" "));
287 XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
288 XGUI_Workbench* aPage = aMenuBar->findWorkbench(aWchName);
290 aPage = addWorkbench(aWchName);
292 //Find or create Group
293 QString aGroupName = QString::fromStdString(theMessage->groupId());
294 XGUI_MenuGroupPanel* aGroup = aPage->findGroup(aGroupName);
296 aGroup = aPage->addGroup(aGroupName);
299 XGUI_Command* aCommand = aGroup->addFeature(QString::fromStdString(theMessage->id()),
300 QString::fromStdString(theMessage->text()),
301 QString::fromStdString(theMessage->tooltip()),
302 QIcon(theMessage->icon().c_str()),
303 QKeySequence(), isUsePropPanel);
304 aCommand->setUnblockableCommands(aNestedFeatures.split(" "));
305 myActionsMgr->addCommand(aCommand);
306 myPartSetModule->featureCreated(aCommand);
311 * Makes a signal/slot connections between Property Panel
312 * and given operation. The given operation becomes a
313 * current operation and previous operation if exists
315 void XGUI_Workshop::connectWithOperation(ModuleBase_Operation* theOperation)
317 QPushButton* aOkBtn = myPropertyPanel->findChild<QPushButton*>(XGUI::PROP_PANEL_OK);
318 connect(aOkBtn, SIGNAL(clicked()), theOperation, SLOT(commit()));
319 QPushButton* aCancelBtn = myPropertyPanel->findChild<QPushButton*>(XGUI::PROP_PANEL_CANCEL);
320 connect(aCancelBtn, SIGNAL(clicked()), theOperation, SLOT(abort()));
322 QAction* aCommand = 0;
323 if (isSalomeMode()) {
324 aCommand = salomeConnector()->command(theOperation->getDescription()->operationId());
326 XGUI_MainMenu* aMenu = myMainWindow->menuObject();
327 aCommand = aMenu->feature(theOperation->getDescription()->operationId());
329 //Abort operation on uncheck the command
330 connect(aCommand, SIGNAL(triggered(bool)), theOperation, SLOT(setRunning(bool)));
334 * Saves document with given name.
336 void XGUI_Workshop::saveDocument(QString theName)
338 QApplication::restoreOverrideCursor();
339 boost::shared_ptr<ModelAPI_PluginManager> aMgr = ModelAPI_PluginManager::get();
340 boost::shared_ptr<ModelAPI_Document> aDoc = aMgr->rootDocument();
341 aDoc->save(theName.toLatin1().constData());
342 QApplication::restoreOverrideCursor();
345 //******************************************************
346 void XGUI_Workshop::onExit()
348 boost::shared_ptr<ModelAPI_PluginManager> aMgr = ModelAPI_PluginManager::get();
349 boost::shared_ptr<ModelAPI_Document> aDoc = aMgr->rootDocument();
350 if(aDoc->isModified()) {
351 int anAnswer = QMessageBox::question(
352 myMainWindow, tr("Save current file"),
353 tr("The document is modified, save before exit?"),
354 QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel, QMessageBox::Cancel);
355 if(anAnswer == QMessageBox::Save) {
357 } else if (anAnswer == QMessageBox::Cancel) {
364 //******************************************************
365 void XGUI_Workshop::onNew()
367 QApplication::setOverrideCursor(Qt::WaitCursor);
368 if (objectBrowser() == 0) {
370 mySelector->connectViewers();
372 myViewerProxy->connectToViewer();
374 if (!isSalomeMode()) {
375 myMainWindow->showPythonConsole();
376 QMdiSubWindow* aWnd = myMainWindow->viewer()->createView();
377 aWnd->showMaximized();
378 updateCommandStatus();
380 QApplication::restoreOverrideCursor();
383 //******************************************************
384 void XGUI_Workshop::onOpen()
386 //save current file before close if modified
387 boost::shared_ptr<ModelAPI_PluginManager> aMgr = ModelAPI_PluginManager::get();
388 boost::shared_ptr<ModelAPI_Document> aDoc = aMgr->rootDocument();
389 if(aDoc->isModified()) {
390 //TODO(sbh): re-launch the app?
391 int anAnswer = QMessageBox::question(
392 myMainWindow, tr("Save current file"),
393 tr("The document is modified, save before opening another?"),
394 QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel, QMessageBox::Cancel);
395 if(anAnswer == QMessageBox::Save) {
397 } else if (anAnswer == QMessageBox::Cancel) {
404 //show file dialog, check if readable and open
405 myCurrentFile = QFileDialog::getOpenFileName(mainWindow());
406 if(myCurrentFile.isEmpty())
408 QFileInfo aFileInfo(myCurrentFile);
409 if(!aFileInfo.exists() || !aFileInfo.isReadable()) {
410 QMessageBox::critical(myMainWindow, tr("Warning"), tr("Unable to open the file."));
414 QApplication::setOverrideCursor(Qt::WaitCursor);
415 aDoc->load(myCurrentFile.toLatin1().constData());
416 QApplication::restoreOverrideCursor();
417 updateCommandStatus();
420 //******************************************************
421 void XGUI_Workshop::onSave()
423 if(myCurrentFile.isEmpty()) {
427 saveDocument(myCurrentFile);
428 updateCommandStatus();
431 //******************************************************
432 void XGUI_Workshop::onSaveAs()
434 QString aTemp = myCurrentFile;
435 myCurrentFile = QFileDialog::getSaveFileName(mainWindow());
436 if(myCurrentFile.isEmpty()) {
437 myCurrentFile = aTemp;
440 QFileInfo aFileInfo(myCurrentFile);
441 if(aFileInfo.exists() && !aFileInfo.isWritable()) {
442 QMessageBox::critical(myMainWindow, tr("Warning"), tr("Unable to save the file."));
448 //******************************************************
449 void XGUI_Workshop::onUndo()
451 objectBrowser()->setCurrentIndex(QModelIndex());
452 boost::shared_ptr<ModelAPI_PluginManager> aMgr = ModelAPI_PluginManager::get();
453 boost::shared_ptr<ModelAPI_Document> aDoc = aMgr->rootDocument();
455 updateCommandStatus();
458 //******************************************************
459 void XGUI_Workshop::onRedo()
461 objectBrowser()->setCurrentIndex(QModelIndex());
462 boost::shared_ptr<ModelAPI_PluginManager> aMgr = ModelAPI_PluginManager::get();
463 boost::shared_ptr<ModelAPI_Document> aDoc = aMgr->rootDocument();
465 updateCommandStatus();
468 //******************************************************
469 XGUI_Module* XGUI_Workshop::loadModule(const QString& theModule)
471 QString libName = library(theModule);
472 if (libName.isEmpty()) {
474 qPrintable( tr( "Information about module \"%1\" doesn't exist." ).arg( theModule ) ));
479 CREATE_FUNC crtInst = 0;
483 HINSTANCE modLib = ::LoadLibrary((LPTSTR) qPrintable(libName));
487 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
488 0, ::GetLastError(), 0, (LPTSTR) & lpMsgBuf, 0, 0);
489 QString aMsg((char*) &lpMsgBuf);
490 err = QString("Failed to load %1. %2").arg(libName).arg(aMsg);
491 ::LocalFree(lpMsgBuf);
493 crtInst = (CREATE_FUNC) ::GetProcAddress(modLib, CREATE_MODULE);
497 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM
498 | FORMAT_MESSAGE_IGNORE_INSERTS,
499 0, ::GetLastError(), 0, (LPTSTR) & lpMsgBuf, 0, 0);
500 QString aMsg((char*) &lpMsgBuf);
501 err = QString("Failed to find %1 function. %2").arg( CREATE_MODULE).arg(aMsg);
502 ::LocalFree(lpMsgBuf);
506 void* modLib = dlopen( libName.toLatin1(), RTLD_LAZY );
508 err = QString( "Can not load library %1. %2" ).arg( libName ).arg( dlerror() );
511 crtInst = (CREATE_FUNC)dlsym( modLib, CREATE_MODULE );
513 err = QString( "Failed to find function %1. %2" ).arg( CREATE_MODULE ).arg( dlerror() );
517 XGUI_Module* aModule = crtInst ? crtInst(this) : 0;
519 if (!err.isEmpty()) {
520 if (mainWindow() && mainWindow()->isVisible())
521 QMessageBox::warning(mainWindow(), tr("Error"), err);
523 qWarning( qPrintable( err ));
528 //******************************************************
529 bool XGUI_Workshop::activateModule()
531 myPartSetModule = loadModule("PartSet");
532 if (!myPartSetModule)
534 myPartSetModule->createFeatures();
538 //******************************************************
539 void XGUI_Workshop::updateCommandStatus()
541 if (isSalomeMode()) // TODO: update commands in SALOME
543 XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
545 QList<XGUI_Command*> aCommands = aMenuBar->features();
546 QList<XGUI_Command*>::const_iterator aIt;
548 boost::shared_ptr<ModelAPI_PluginManager> aMgr = ModelAPI_PluginManager::get();
549 if (aMgr->hasRootDocument()) {
550 XGUI_Command* aUndoCmd;
551 XGUI_Command* aRedoCmd;
552 for (aIt = aCommands.constBegin(); aIt != aCommands.constEnd(); ++aIt) {
553 if ((*aIt)->id() == "UNDO_CMD")
555 else if ((*aIt)->id() == "REDO_CMD")
557 else // Enable all commands
560 boost::shared_ptr<ModelAPI_Document> aDoc = aMgr->rootDocument();
561 aUndoCmd->setEnabled(aDoc->canUndo());
562 aRedoCmd->setEnabled(aDoc->canRedo());
564 for (aIt = aCommands.constBegin(); aIt != aCommands.constEnd(); ++aIt) {
565 if ((*aIt)->id() == "NEW_CMD")
567 else if ((*aIt)->id() == "EXIT_CMD")
575 //******************************************************
576 QDockWidget* XGUI_Workshop::createObjectBrowser(QWidget* theParent)
578 QDockWidget* aObjDock = new QDockWidget(theParent);
579 aObjDock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
580 aObjDock->setWindowTitle(tr("Object browser"));
581 myObjectBrowser = new XGUI_ObjectsBrowser(aObjDock);
582 aObjDock->setWidget(myObjectBrowser);
586 //******************************************************
588 * Creates dock widgets, places them in corresponding area
589 * and tabifies if necessary.
591 void XGUI_Workshop::createDockWidgets()
593 QMainWindow* aDesktop = isSalomeMode()? salomeConnector()->desktop() :
595 QDockWidget* aObjDock = createObjectBrowser(aDesktop);
596 aDesktop->addDockWidget(Qt::LeftDockWidgetArea, aObjDock);
597 myPropertyPanel = new XGUI_PropertyPanel(aDesktop);
598 aDesktop->addDockWidget(Qt::LeftDockWidgetArea, myPropertyPanel);
599 hidePropertyPanel(); //<! Invisible by default
601 aDesktop->tabifyDockWidget(aObjDock, myPropertyPanel);
604 //******************************************************
605 void XGUI_Workshop::showPropertyPanel()
607 QAction* aViewAct = myPropertyPanel->toggleViewAction();
608 //<! Restore ability to close panel from the window's menu
609 aViewAct->setEnabled(true);
610 myPropertyPanel->show();
611 myPropertyPanel->raise();
614 //******************************************************
615 void XGUI_Workshop::hidePropertyPanel()
617 QAction* aViewAct = myPropertyPanel->toggleViewAction();
618 //<! Do not allow to show empty property panel
619 aViewAct->setEnabled(false);
620 myPropertyPanel->hide();
623 //******************************************************
624 void XGUI_Workshop::showObjectBrowser()
626 myObjectBrowser->parentWidget()->show();
629 //******************************************************
630 void XGUI_Workshop::hideObjectBrowser()
632 myObjectBrowser->parentWidget()->hide();
635 //******************************************************
636 void XGUI_Workshop::onFeatureTriggered()
638 QAction* aCmd = dynamic_cast<QAction*>(sender());
640 QString aId = salomeConnector()->commandId(aCmd);
642 myPartSetModule->launchOperation(aId);
646 //******************************************************
647 void XGUI_Workshop::changeCurrentDocument()
649 QFeatureList aFeatures = objectBrowser()->selectedFeatures();
651 // Set current document
652 if (aFeatures.size() > 0) {
653 FeaturePtr aFeature = aFeatures.first();
655 boost::shared_ptr<ModelAPI_PluginManager> aMgr = ModelAPI_PluginManager::get();
656 boost::shared_ptr<ModelAPI_AttributeDocRef> aDocRef = aFeature->data()->docRef("PartDocument");
658 aMgr->setCurrentDocument(aDocRef->value());
662 //******************************************************
663 void XGUI_Workshop::salomeViewerSelectionChanged()
665 emit salomeViewerSelection();
669 //**************************************************************
670 XGUI_SalomeViewer* XGUI_Workshop::salomeViewer() const
672 return mySalomeConnector->viewer();