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 static Events_ID aFeatureLoadedId = Events_Loop::loop()->eventByName(EVENT_FEATURE_LOADED);
181 if (theMessage->eventID() == aFeatureLoadedId) {
182 const Config_FeatureMessage* aFeatureMsg = dynamic_cast<const Config_FeatureMessage*>(theMessage);
183 addFeature(aFeatureMsg);
186 static Events_ID aFeatureUpdatedId = Events_Loop::loop()->eventByName(EVENT_FEATURE_UPDATED);
187 if (theMessage->eventID() == aFeatureUpdatedId)
189 myPropertyPanel->updateContentWidget();
191 const Config_PointerMessage* aPartSetMsg = dynamic_cast<const Config_PointerMessage*>(theMessage);
193 ModuleBase_Operation* anOperation =
194 (ModuleBase_Operation*)(aPartSetMsg->pointer());
196 if (myOperationMgr->startOperation(anOperation)) {
197 if (anOperation->getDescription()->xmlRepresentation().isEmpty()) {
198 anOperation->commit();
199 updateCommandStatus();
204 const Events_Error* anAppError = dynamic_cast<const Events_Error*>(theMessage);
206 emit errorOccurred(QString::fromLatin1(anAppError->description()));
209 myErrorDlg->activateWindow();
214 //******************************************************
215 void XGUI_Workshop::onOperationStarted()
217 ModuleBase_Operation* aOperation = myOperationMgr->currentOperation();
219 if(!aOperation->getDescription()->xmlRepresentation().isEmpty()) { //!< No need for property panel
220 connectWithOperation(aOperation);
224 ModuleBase_WidgetFactory aFactory = ModuleBase_WidgetFactory(aOperation);
225 aFactory.createWidget(myPropertyPanel->contentWidget());
226 myPropertyPanel->setModelWidgets(aFactory.getWrappedWidgets());
227 myPropertyPanel->setWindowTitle(aOperation->getDescription()->description());
231 //******************************************************
232 void XGUI_Workshop::onOperationStopped(ModuleBase_Operation* theOperation)
234 ModuleBase_Operation* aOperation = myOperationMgr->currentOperation();
236 if(aOperation->getDescription()->xmlRepresentation().isEmpty()) { //!< No need for property panel
237 updateCommandStatus();
240 updateCommandStatus();
241 myActionsMgr->restoreCommandState();
248 void XGUI_Workshop::addFeature(const Config_FeatureMessage* theMessage)
252 qDebug() << "XGUI_Workshop::addFeature: NULL message.";
256 //Find or create Workbench
257 QString aWchName = QString::fromStdString(theMessage->workbenchId());
258 QString aNestedFeatures = QString::fromStdString(theMessage->nestedFeatures());
259 bool isUsePropPanel = theMessage->isUseInput();
260 if (isSalomeMode()) {
261 QString aId = QString::fromStdString(theMessage->id());
262 salomeConnector()->addFeature(aWchName,
263 QString::fromStdString(theMessage->id()),
265 QString::fromStdString(theMessage->tooltip()),
266 QIcon(theMessage->icon().c_str()),
267 isUsePropPanel, this,
268 SLOT(onFeatureTriggered()), QKeySequence());
269 myActionsMgr->addCommand(aId, salomeConnector()->command(aId));
270 salomeConnector()->setNestedActions(aId, aNestedFeatures.split(" "));
274 XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
275 XGUI_Workbench* aPage = aMenuBar->findWorkbench(aWchName);
277 aPage = addWorkbench(aWchName);
279 //Find or create Group
280 QString aGroupName = QString::fromStdString(theMessage->groupId());
281 XGUI_MenuGroupPanel* aGroup = aPage->findGroup(aGroupName);
283 aGroup = aPage->addGroup(aGroupName);
286 XGUI_Command* aCommand = aGroup->addFeature(QString::fromStdString(theMessage->id()),
287 QString::fromStdString(theMessage->text()),
288 QString::fromStdString(theMessage->tooltip()),
289 QIcon(theMessage->icon().c_str()),
290 QKeySequence(), isUsePropPanel);
291 aCommand->setUnblockableCommands(aNestedFeatures.split(" "));
292 myActionsMgr->addCommand(aCommand);
293 myPartSetModule->featureCreated(aCommand);
298 * Makes a signal/slot connections between Property Panel
299 * and given operation. The given operation becomes a
300 * current operation and previous operation if exists
302 void XGUI_Workshop::connectWithOperation(ModuleBase_Operation* theOperation)
304 QPushButton* aOkBtn = myPropertyPanel->findChild<QPushButton*>(XGUI::PROP_PANEL_OK);
305 connect(aOkBtn, SIGNAL(clicked()), theOperation, SLOT(commit()));
306 QPushButton* aCancelBtn = myPropertyPanel->findChild<QPushButton*>(XGUI::PROP_PANEL_CANCEL);
307 connect(aCancelBtn, SIGNAL(clicked()), theOperation, SLOT(abort()));
309 QAction* aCommand = 0;
310 if (isSalomeMode()) {
311 aCommand = salomeConnector()->command(theOperation->getDescription()->operationId());
313 XGUI_MainMenu* aMenu = myMainWindow->menuObject();
314 aCommand = aMenu->feature(theOperation->getDescription()->operationId());
316 //Abort operation on uncheck the command
317 connect(aCommand, SIGNAL(triggered(bool)), theOperation, SLOT(setRunning(bool)));
321 * Saves document with given name.
323 void XGUI_Workshop::saveDocument(QString theName)
325 QApplication::restoreOverrideCursor();
326 boost::shared_ptr<ModelAPI_PluginManager> aMgr = ModelAPI_PluginManager::get();
327 boost::shared_ptr<ModelAPI_Document> aDoc = aMgr->rootDocument();
328 aDoc->save(theName.toLatin1().constData());
329 QApplication::restoreOverrideCursor();
332 //******************************************************
333 void XGUI_Workshop::onExit()
335 boost::shared_ptr<ModelAPI_PluginManager> aMgr = ModelAPI_PluginManager::get();
336 boost::shared_ptr<ModelAPI_Document> aDoc = aMgr->rootDocument();
337 if(aDoc->isModified()) {
338 int anAnswer = QMessageBox::question(
339 myMainWindow, tr("Save current file"),
340 tr("The document is modified, save before exit?"),
341 QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel, QMessageBox::Cancel);
342 if(anAnswer == QMessageBox::Save) {
344 } else if (anAnswer == QMessageBox::Cancel) {
351 //******************************************************
352 void XGUI_Workshop::onNew()
354 QApplication::setOverrideCursor(Qt::WaitCursor);
355 if (objectBrowser() == 0) {
357 mySelector->connectViewers();
359 myViewerProxy->connectToViewer();
361 if (!isSalomeMode()) {
362 myMainWindow->showPythonConsole();
363 QMdiSubWindow* aWnd = myMainWindow->viewer()->createView();
364 aWnd->showMaximized();
365 updateCommandStatus();
367 QApplication::restoreOverrideCursor();
370 //******************************************************
371 void XGUI_Workshop::onOpen()
373 //save current file before close if modified
374 boost::shared_ptr<ModelAPI_PluginManager> aMgr = ModelAPI_PluginManager::get();
375 boost::shared_ptr<ModelAPI_Document> aDoc = aMgr->rootDocument();
376 if(aDoc->isModified()) {
377 //TODO(sbh): re-launch the app?
378 int anAnswer = QMessageBox::question(
379 myMainWindow, tr("Save current file"),
380 tr("The document is modified, save before opening another?"),
381 QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel, QMessageBox::Cancel);
382 if(anAnswer == QMessageBox::Save) {
384 } else if (anAnswer == QMessageBox::Cancel) {
391 //show file dialog, check if readable and open
392 myCurrentFile = QFileDialog::getOpenFileName(mainWindow());
393 if(myCurrentFile.isEmpty())
395 QFileInfo aFileInfo(myCurrentFile);
396 if(!aFileInfo.exists() || !aFileInfo.isReadable()) {
397 QMessageBox::critical(myMainWindow, tr("Warning"), tr("Unable to open the file."));
401 QApplication::setOverrideCursor(Qt::WaitCursor);
402 aDoc->load(myCurrentFile.toLatin1().constData());
403 QApplication::restoreOverrideCursor();
404 updateCommandStatus();
407 //******************************************************
408 void XGUI_Workshop::onSave()
410 if(myCurrentFile.isEmpty()) {
414 saveDocument(myCurrentFile);
415 updateCommandStatus();
418 //******************************************************
419 void XGUI_Workshop::onSaveAs()
421 QString aTemp = myCurrentFile;
422 myCurrentFile = QFileDialog::getSaveFileName(mainWindow());
423 if(myCurrentFile.isEmpty()) {
424 myCurrentFile = aTemp;
427 QFileInfo aFileInfo(myCurrentFile);
428 if(aFileInfo.exists() && !aFileInfo.isWritable()) {
429 QMessageBox::critical(myMainWindow, tr("Warning"), tr("Unable to save the file."));
435 //******************************************************
436 void XGUI_Workshop::onUndo()
438 objectBrowser()->setCurrentIndex(QModelIndex());
439 boost::shared_ptr<ModelAPI_PluginManager> aMgr = ModelAPI_PluginManager::get();
440 boost::shared_ptr<ModelAPI_Document> aDoc = aMgr->rootDocument();
442 updateCommandStatus();
445 //******************************************************
446 void XGUI_Workshop::onRedo()
448 objectBrowser()->setCurrentIndex(QModelIndex());
449 boost::shared_ptr<ModelAPI_PluginManager> aMgr = ModelAPI_PluginManager::get();
450 boost::shared_ptr<ModelAPI_Document> aDoc = aMgr->rootDocument();
452 updateCommandStatus();
455 //******************************************************
456 XGUI_Module* XGUI_Workshop::loadModule(const QString& theModule)
458 QString libName = library(theModule);
459 if (libName.isEmpty()) {
461 qPrintable( tr( "Information about module \"%1\" doesn't exist." ).arg( theModule ) ));
466 CREATE_FUNC crtInst = 0;
470 HINSTANCE modLib = ::LoadLibrary((LPTSTR) qPrintable(libName));
474 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
475 0, ::GetLastError(), 0, (LPTSTR) & lpMsgBuf, 0, 0);
476 QString aMsg((char*) &lpMsgBuf);
477 err = QString("Failed to load %1. %2").arg(libName).arg(aMsg);
478 ::LocalFree(lpMsgBuf);
480 crtInst = (CREATE_FUNC) ::GetProcAddress(modLib, CREATE_MODULE);
484 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM
485 | FORMAT_MESSAGE_IGNORE_INSERTS,
486 0, ::GetLastError(), 0, (LPTSTR) & lpMsgBuf, 0, 0);
487 QString aMsg((char*) &lpMsgBuf);
488 err = QString("Failed to find %1 function. %2").arg( CREATE_MODULE).arg(aMsg);
489 ::LocalFree(lpMsgBuf);
493 void* modLib = dlopen( libName.toLatin1(), RTLD_LAZY );
495 err = QString( "Can not load library %1. %2" ).arg( libName ).arg( dlerror() );
498 crtInst = (CREATE_FUNC)dlsym( modLib, CREATE_MODULE );
500 err = QString( "Failed to find function %1. %2" ).arg( CREATE_MODULE ).arg( dlerror() );
504 XGUI_Module* aModule = crtInst ? crtInst(this) : 0;
506 if (!err.isEmpty()) {
507 if (mainWindow() && mainWindow()->isVisible())
508 QMessageBox::warning(mainWindow(), tr("Error"), err);
510 qWarning( qPrintable( err ));
515 //******************************************************
516 bool XGUI_Workshop::activateModule()
518 myPartSetModule = loadModule("PartSet");
519 if (!myPartSetModule)
521 myPartSetModule->createFeatures();
525 //******************************************************
526 void XGUI_Workshop::updateCommandStatus()
528 if (isSalomeMode()) // TODO: update commands in SALOME
530 XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
532 QList<XGUI_Command*> aCommands = aMenuBar->features();
533 QList<XGUI_Command*>::const_iterator aIt;
535 boost::shared_ptr<ModelAPI_PluginManager> aMgr = ModelAPI_PluginManager::get();
536 if (aMgr->hasRootDocument()) {
537 XGUI_Command* aUndoCmd;
538 XGUI_Command* aRedoCmd;
539 for (aIt = aCommands.constBegin(); aIt != aCommands.constEnd(); ++aIt) {
540 if ((*aIt)->id() == "UNDO_CMD")
542 else if ((*aIt)->id() == "REDO_CMD")
544 else // Enable all commands
547 boost::shared_ptr<ModelAPI_Document> aDoc = aMgr->rootDocument();
548 aUndoCmd->setEnabled(aDoc->canUndo());
549 aRedoCmd->setEnabled(aDoc->canRedo());
551 for (aIt = aCommands.constBegin(); aIt != aCommands.constEnd(); ++aIt) {
552 if ((*aIt)->id() == "NEW_CMD")
554 else if ((*aIt)->id() == "EXIT_CMD")
562 //******************************************************
563 QDockWidget* XGUI_Workshop::createObjectBrowser(QWidget* theParent)
565 QDockWidget* aObjDock = new QDockWidget(theParent);
566 aObjDock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
567 aObjDock->setWindowTitle(tr("Object browser"));
568 myObjectBrowser = new XGUI_ObjectsBrowser(aObjDock);
569 aObjDock->setWidget(myObjectBrowser);
573 //******************************************************
575 * Creates dock widgets, places them in corresponding area
576 * and tabifies if necessary.
578 void XGUI_Workshop::createDockWidgets()
580 QMainWindow* aDesktop = isSalomeMode()? salomeConnector()->desktop() :
582 QDockWidget* aObjDock = createObjectBrowser(aDesktop);
583 aDesktop->addDockWidget(Qt::LeftDockWidgetArea, aObjDock);
584 myPropertyPanel = new XGUI_PropertyPanel(aDesktop);
585 aDesktop->addDockWidget(Qt::LeftDockWidgetArea, myPropertyPanel);
586 hidePropertyPanel(); //<! Invisible by default
588 aDesktop->tabifyDockWidget(aObjDock, myPropertyPanel);
591 //******************************************************
592 void XGUI_Workshop::showPropertyPanel()
594 QAction* aViewAct = myPropertyPanel->toggleViewAction();
595 //<! Restore ability to close panel from the window's menu
596 aViewAct->setEnabled(true);
597 myPropertyPanel->show();
598 myPropertyPanel->raise();
601 //******************************************************
602 void XGUI_Workshop::hidePropertyPanel()
604 QAction* aViewAct = myPropertyPanel->toggleViewAction();
605 //<! Do not allow to show empty property panel
606 aViewAct->setEnabled(false);
607 myPropertyPanel->hide();
610 //******************************************************
611 void XGUI_Workshop::showObjectBrowser()
613 myObjectBrowser->parentWidget()->show();
616 //******************************************************
617 void XGUI_Workshop::hideObjectBrowser()
619 myObjectBrowser->parentWidget()->hide();
622 //******************************************************
623 void XGUI_Workshop::onFeatureTriggered()
625 QAction* aCmd = dynamic_cast<QAction*>(sender());
627 QString aId = salomeConnector()->commandId(aCmd);
629 myPartSetModule->launchOperation(aId);
633 //******************************************************
634 void XGUI_Workshop::changeCurrentDocument()
636 QFeatureList aFeatures = objectBrowser()->selectedFeatures();
638 // Set current document
639 if (aFeatures.size() > 0) {
640 FeaturePtr aFeature = aFeatures.first();
642 boost::shared_ptr<ModelAPI_PluginManager> aMgr = ModelAPI_PluginManager::get();
643 boost::shared_ptr<ModelAPI_AttributeDocRef> aDocRef = aFeature->data()->docRef("PartDocument");
645 aMgr->setCurrentDocument(aDocRef->value());
649 //******************************************************
650 void XGUI_Workshop::salomeViewerSelectionChanged()
652 emit salomeViewerSelection();
656 //**************************************************************
657 XGUI_SalomeViewer* XGUI_Workshop::salomeViewer() const
659 return mySalomeConnector->viewer();