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 //!< No need for property panel
250 updateCommandStatus();
252 if(myOperationMgr->operationsCount() > 1) {
253 myActionsMgr->updateAction(theOperation->getDescription()->operationId());
256 if(!aOperation->getDescription()->xmlRepresentation().isEmpty()) {
257 myActionsMgr->restoreCommandState();
264 void XGUI_Workshop::addFeature(const Config_FeatureMessage* theMessage)
268 qDebug() << "XGUI_Workshop::addFeature: NULL message.";
272 //Find or create Workbench
273 QString aWchName = QString::fromStdString(theMessage->workbenchId());
274 QString aNestedFeatures = QString::fromStdString(theMessage->nestedFeatures());
275 bool isUsePropPanel = theMessage->isUseInput();
276 if (isSalomeMode()) {
277 QString aId = QString::fromStdString(theMessage->id());
278 salomeConnector()->addFeature(aWchName,
279 QString::fromStdString(theMessage->id()),
281 QString::fromStdString(theMessage->tooltip()),
282 QIcon(theMessage->icon().c_str()),
283 isUsePropPanel, this,
284 SLOT(onFeatureTriggered()), QKeySequence());
285 myActionsMgr->addCommand(aId, salomeConnector()->command(aId));
286 salomeConnector()->setNestedActions(aId, aNestedFeatures.split(" "));
290 XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
291 XGUI_Workbench* aPage = aMenuBar->findWorkbench(aWchName);
293 aPage = addWorkbench(aWchName);
295 //Find or create Group
296 QString aGroupName = QString::fromStdString(theMessage->groupId());
297 XGUI_MenuGroupPanel* aGroup = aPage->findGroup(aGroupName);
299 aGroup = aPage->addGroup(aGroupName);
302 XGUI_Command* aCommand = aGroup->addFeature(QString::fromStdString(theMessage->id()),
303 QString::fromStdString(theMessage->text()),
304 QString::fromStdString(theMessage->tooltip()),
305 QIcon(theMessage->icon().c_str()),
306 QKeySequence(), isUsePropPanel);
307 aCommand->setUnblockableCommands(aNestedFeatures.split(" "));
308 myActionsMgr->addCommand(aCommand);
309 myPartSetModule->featureCreated(aCommand);
314 * Makes a signal/slot connections between Property Panel
315 * and given operation. The given operation becomes a
316 * current operation and previous operation if exists
318 void XGUI_Workshop::connectWithOperation(ModuleBase_Operation* theOperation)
320 QPushButton* aOkBtn = myPropertyPanel->findChild<QPushButton*>(XGUI::PROP_PANEL_OK);
321 connect(aOkBtn, SIGNAL(clicked()), theOperation, SLOT(commit()));
322 QPushButton* aCancelBtn = myPropertyPanel->findChild<QPushButton*>(XGUI::PROP_PANEL_CANCEL);
323 connect(aCancelBtn, SIGNAL(clicked()), theOperation, SLOT(abort()));
325 QAction* aCommand = 0;
326 if (isSalomeMode()) {
327 aCommand = salomeConnector()->command(theOperation->getDescription()->operationId());
329 XGUI_MainMenu* aMenu = myMainWindow->menuObject();
330 aCommand = aMenu->feature(theOperation->getDescription()->operationId());
332 //Abort operation on uncheck the command
333 connect(aCommand, SIGNAL(triggered(bool)), theOperation, SLOT(setRunning(bool)));
337 * Saves document with given name.
339 void XGUI_Workshop::saveDocument(QString theName)
341 QApplication::restoreOverrideCursor();
342 boost::shared_ptr<ModelAPI_PluginManager> aMgr = ModelAPI_PluginManager::get();
343 boost::shared_ptr<ModelAPI_Document> aDoc = aMgr->rootDocument();
344 aDoc->save(theName.toLatin1().constData());
345 QApplication::restoreOverrideCursor();
348 //******************************************************
349 void XGUI_Workshop::onExit()
351 boost::shared_ptr<ModelAPI_PluginManager> aMgr = ModelAPI_PluginManager::get();
352 boost::shared_ptr<ModelAPI_Document> aDoc = aMgr->rootDocument();
353 if(aDoc->isModified()) {
354 int anAnswer = QMessageBox::question(
355 myMainWindow, tr("Save current file"),
356 tr("The document is modified, save before exit?"),
357 QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel, QMessageBox::Cancel);
358 if(anAnswer == QMessageBox::Save) {
360 } else if (anAnswer == QMessageBox::Cancel) {
367 //******************************************************
368 void XGUI_Workshop::onNew()
370 QApplication::setOverrideCursor(Qt::WaitCursor);
371 if (objectBrowser() == 0) {
373 mySelector->connectViewers();
375 myViewerProxy->connectToViewer();
377 if (!isSalomeMode()) {
378 myMainWindow->showPythonConsole();
379 QMdiSubWindow* aWnd = myMainWindow->viewer()->createView();
380 aWnd->showMaximized();
381 updateCommandStatus();
383 QApplication::restoreOverrideCursor();
386 //******************************************************
387 void XGUI_Workshop::onOpen()
389 //save current file before close if modified
390 boost::shared_ptr<ModelAPI_PluginManager> aMgr = ModelAPI_PluginManager::get();
391 boost::shared_ptr<ModelAPI_Document> aDoc = aMgr->rootDocument();
392 if(aDoc->isModified()) {
393 //TODO(sbh): re-launch the app?
394 int anAnswer = QMessageBox::question(
395 myMainWindow, tr("Save current file"),
396 tr("The document is modified, save before opening another?"),
397 QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel, QMessageBox::Cancel);
398 if(anAnswer == QMessageBox::Save) {
400 } else if (anAnswer == QMessageBox::Cancel) {
407 //show file dialog, check if readable and open
408 myCurrentFile = QFileDialog::getOpenFileName(mainWindow());
409 if(myCurrentFile.isEmpty())
411 QFileInfo aFileInfo(myCurrentFile);
412 if(!aFileInfo.exists() || !aFileInfo.isReadable()) {
413 QMessageBox::critical(myMainWindow, tr("Warning"), tr("Unable to open the file."));
417 QApplication::setOverrideCursor(Qt::WaitCursor);
418 aDoc->load(myCurrentFile.toLatin1().constData());
419 QApplication::restoreOverrideCursor();
420 updateCommandStatus();
423 //******************************************************
424 void XGUI_Workshop::onSave()
426 if(myCurrentFile.isEmpty()) {
430 saveDocument(myCurrentFile);
431 updateCommandStatus();
434 //******************************************************
435 void XGUI_Workshop::onSaveAs()
437 QString aTemp = myCurrentFile;
438 myCurrentFile = QFileDialog::getSaveFileName(mainWindow());
439 if(myCurrentFile.isEmpty()) {
440 myCurrentFile = aTemp;
443 QFileInfo aFileInfo(myCurrentFile);
444 if(aFileInfo.exists() && !aFileInfo.isWritable()) {
445 QMessageBox::critical(myMainWindow, tr("Warning"), tr("Unable to save the file."));
451 //******************************************************
452 void XGUI_Workshop::onUndo()
454 objectBrowser()->setCurrentIndex(QModelIndex());
455 boost::shared_ptr<ModelAPI_PluginManager> aMgr = ModelAPI_PluginManager::get();
456 boost::shared_ptr<ModelAPI_Document> aDoc = aMgr->rootDocument();
458 updateCommandStatus();
461 //******************************************************
462 void XGUI_Workshop::onRedo()
464 objectBrowser()->setCurrentIndex(QModelIndex());
465 boost::shared_ptr<ModelAPI_PluginManager> aMgr = ModelAPI_PluginManager::get();
466 boost::shared_ptr<ModelAPI_Document> aDoc = aMgr->rootDocument();
468 updateCommandStatus();
471 //******************************************************
472 XGUI_Module* XGUI_Workshop::loadModule(const QString& theModule)
474 QString libName = library(theModule);
475 if (libName.isEmpty()) {
477 qPrintable( tr( "Information about module \"%1\" doesn't exist." ).arg( theModule ) ));
482 CREATE_FUNC crtInst = 0;
486 HINSTANCE modLib = ::LoadLibrary((LPTSTR) qPrintable(libName));
490 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
491 0, ::GetLastError(), 0, (LPTSTR) & lpMsgBuf, 0, 0);
492 QString aMsg((char*) &lpMsgBuf);
493 err = QString("Failed to load %1. %2").arg(libName).arg(aMsg);
494 ::LocalFree(lpMsgBuf);
496 crtInst = (CREATE_FUNC) ::GetProcAddress(modLib, CREATE_MODULE);
500 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM
501 | FORMAT_MESSAGE_IGNORE_INSERTS,
502 0, ::GetLastError(), 0, (LPTSTR) & lpMsgBuf, 0, 0);
503 QString aMsg((char*) &lpMsgBuf);
504 err = QString("Failed to find %1 function. %2").arg( CREATE_MODULE).arg(aMsg);
505 ::LocalFree(lpMsgBuf);
509 void* modLib = dlopen( libName.toLatin1(), RTLD_LAZY );
511 err = QString( "Can not load library %1. %2" ).arg( libName ).arg( dlerror() );
514 crtInst = (CREATE_FUNC)dlsym( modLib, CREATE_MODULE );
516 err = QString( "Failed to find function %1. %2" ).arg( CREATE_MODULE ).arg( dlerror() );
520 XGUI_Module* aModule = crtInst ? crtInst(this) : 0;
522 if (!err.isEmpty()) {
523 if (mainWindow() && mainWindow()->isVisible())
524 QMessageBox::warning(mainWindow(), tr("Error"), err);
526 qWarning( qPrintable( err ));
531 //******************************************************
532 bool XGUI_Workshop::activateModule()
534 myPartSetModule = loadModule("PartSet");
535 if (!myPartSetModule)
537 myPartSetModule->createFeatures();
541 //******************************************************
542 void XGUI_Workshop::updateCommandStatus()
544 if (isSalomeMode()) // TODO: update commands in SALOME
546 XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
548 QList<XGUI_Command*> aCommands = aMenuBar->features();
549 QList<XGUI_Command*>::const_iterator aIt;
551 boost::shared_ptr<ModelAPI_PluginManager> aMgr = ModelAPI_PluginManager::get();
552 if (aMgr->hasRootDocument()) {
553 XGUI_Command* aUndoCmd;
554 XGUI_Command* aRedoCmd;
555 for (aIt = aCommands.constBegin(); aIt != aCommands.constEnd(); ++aIt) {
556 if ((*aIt)->id() == "UNDO_CMD")
558 else if ((*aIt)->id() == "REDO_CMD")
560 else // Enable all commands
563 boost::shared_ptr<ModelAPI_Document> aDoc = aMgr->rootDocument();
564 aUndoCmd->setEnabled(aDoc->canUndo());
565 aRedoCmd->setEnabled(aDoc->canRedo());
567 for (aIt = aCommands.constBegin(); aIt != aCommands.constEnd(); ++aIt) {
568 if ((*aIt)->id() == "NEW_CMD")
570 else if ((*aIt)->id() == "EXIT_CMD")
578 //******************************************************
579 QDockWidget* XGUI_Workshop::createObjectBrowser(QWidget* theParent)
581 QDockWidget* aObjDock = new QDockWidget(theParent);
582 aObjDock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
583 aObjDock->setWindowTitle(tr("Object browser"));
584 myObjectBrowser = new XGUI_ObjectsBrowser(aObjDock);
585 aObjDock->setWidget(myObjectBrowser);
589 //******************************************************
591 * Creates dock widgets, places them in corresponding area
592 * and tabifies if necessary.
594 void XGUI_Workshop::createDockWidgets()
596 QMainWindow* aDesktop = isSalomeMode()? salomeConnector()->desktop() :
598 QDockWidget* aObjDock = createObjectBrowser(aDesktop);
599 aDesktop->addDockWidget(Qt::LeftDockWidgetArea, aObjDock);
600 myPropertyPanel = new XGUI_PropertyPanel(aDesktop);
601 aDesktop->addDockWidget(Qt::LeftDockWidgetArea, myPropertyPanel);
602 hidePropertyPanel(); //<! Invisible by default
604 aDesktop->tabifyDockWidget(aObjDock, myPropertyPanel);
607 //******************************************************
608 void XGUI_Workshop::showPropertyPanel()
610 QAction* aViewAct = myPropertyPanel->toggleViewAction();
611 //<! Restore ability to close panel from the window's menu
612 aViewAct->setEnabled(true);
613 myPropertyPanel->show();
614 myPropertyPanel->raise();
617 //******************************************************
618 void XGUI_Workshop::hidePropertyPanel()
620 QAction* aViewAct = myPropertyPanel->toggleViewAction();
621 //<! Do not allow to show empty property panel
622 aViewAct->setEnabled(false);
623 myPropertyPanel->hide();
626 //******************************************************
627 void XGUI_Workshop::showObjectBrowser()
629 myObjectBrowser->parentWidget()->show();
632 //******************************************************
633 void XGUI_Workshop::hideObjectBrowser()
635 myObjectBrowser->parentWidget()->hide();
638 //******************************************************
639 void XGUI_Workshop::onFeatureTriggered()
641 QAction* aCmd = dynamic_cast<QAction*>(sender());
643 QString aId = salomeConnector()->commandId(aCmd);
645 myPartSetModule->launchOperation(aId);
649 //******************************************************
650 void XGUI_Workshop::changeCurrentDocument()
652 QFeatureList aFeatures = objectBrowser()->selectedFeatures();
654 // Set current document
655 if (aFeatures.size() > 0) {
656 FeaturePtr aFeature = aFeatures.first();
658 boost::shared_ptr<ModelAPI_PluginManager> aMgr = ModelAPI_PluginManager::get();
659 boost::shared_ptr<ModelAPI_AttributeDocRef> aDocRef = aFeature->data()->docRef("PartDocument");
661 aMgr->setCurrentDocument(aDocRef->value());
665 //******************************************************
666 void XGUI_Workshop::salomeViewerSelectionChanged()
668 emit salomeViewerSelection();
672 //**************************************************************
673 XGUI_SalomeViewer* XGUI_Workshop::salomeViewer() const
675 return mySalomeConnector->viewer();