1 #include "ModuleBase_IModule.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_SelectionMgr.h"
12 #include "XGUI_Selection.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"
22 #include "XGUI_ContextMenuMgr.h"
23 #include "XGUI_ModuleConnector.h"
24 #include "XGUI_Preferences.h"
26 #include <ModelAPI_Events.h>
27 #include <ModelAPI_Session.h>
28 #include <ModelAPI_Feature.h>
29 #include <ModelAPI_Data.h>
30 #include <ModelAPI_AttributeDocRef.h>
31 #include <ModelAPI_Object.h>
32 #include <ModelAPI_Validator.h>
33 #include <ModelAPI_ResultConstruction.h>
34 #include <ModelAPI_ResultBody.h>
36 #include <PartSetPlugin_Part.h>
38 #include <Events_Loop.h>
39 #include <Events_Error.h>
40 #include <Events_LongOp.h>
42 #include <ModuleBase_Operation.h>
43 #include <ModuleBase_Operation.h>
44 #include <ModuleBase_OperationDescription.h>
45 #include <ModuleBase_SelectionValidator.h>
46 #include "ModuleBase_WidgetFactory.h"
48 #include <Config_Common.h>
49 #include <Config_FeatureMessage.h>
50 #include <Config_PointerMessage.h>
51 #include <Config_ModuleReader.h>
53 #include <QApplication>
54 #include <QFileDialog>
55 #include <QMessageBox>
56 #include <QMdiSubWindow>
57 #include <QPushButton>
58 #include <QDockWidget>
72 QMap<QString, QString> XGUI_Workshop::myIcons;
74 QString XGUI_Workshop::featureIcon(const std::string& theId)
76 QString aId(theId.c_str());
77 if (myIcons.contains(aId))
82 XGUI_Workshop::XGUI_Workshop(XGUI_SalomeConnector* theConnector)
84 myCurrentDir(QString()),
86 mySalomeConnector(theConnector),
92 myMainWindow = mySalomeConnector ? 0 : new XGUI_MainWindow();
94 myDisplayer = new XGUI_Displayer(this);
96 mySelector = new XGUI_SelectionMgr(this);
97 //connect(mySelector, SIGNAL(selectionChanged()), this, SLOT(updateModuleCommands()));
99 myOperationMgr = new XGUI_OperationMgr(this);
100 myActionsMgr = new XGUI_ActionsMgr(this);
101 myErrorDlg = new XGUI_ErrorDialog(myMainWindow);
102 myContextMenuMgr = new XGUI_ContextMenuMgr(this);
103 connect(myContextMenuMgr, SIGNAL(actionTriggered(const QString&, bool)), this,
104 SLOT(onContextMenuCommand(const QString&, bool)));
106 myViewerProxy = new XGUI_ViewerProxy(this);
107 connect(myViewerProxy, SIGNAL(selectionChanged()), this, SLOT(updateCommandsOnViewSelection()));
109 myModuleConnector = new XGUI_ModuleConnector(this);
111 connect(myOperationMgr, SIGNAL(operationStarted()), SLOT(onOperationStarted()));
112 connect(myOperationMgr, SIGNAL(operationResumed()), SLOT(onOperationStarted()));
113 connect(myOperationMgr, SIGNAL(operationStopped(ModuleBase_Operation*)),
114 SLOT(onOperationStopped(ModuleBase_Operation*)));
115 connect(myMainWindow, SIGNAL(exitKeySequence()), SLOT(onExit()));
116 connect(myOperationMgr, SIGNAL(operationStarted()), myActionsMgr, SLOT(update()));
117 connect(myOperationMgr, SIGNAL(operationStopped(ModuleBase_Operation*)), myActionsMgr,
119 connect(this, SIGNAL(errorOccurred(const QString&)), myErrorDlg, SLOT(addError(const QString&)));
122 //******************************************************
123 XGUI_Workshop::~XGUI_Workshop(void)
127 //******************************************************
128 void XGUI_Workshop::startApplication()
131 //Initialize event listening
132 Events_Loop* aLoop = Events_Loop::loop();
133 aLoop->registerListener(this, Events_Error::errorID()); //!< Listening application errors.
134 //TODO(sbh): Implement static method to extract event id [SEID]
135 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_FEATURE_LOADED));
136 // TODO Is it good to use non standard event within workshop?
137 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OPERATION_LAUNCHED));
138 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
139 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_CREATED));
140 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
141 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_DELETED));
142 aLoop->registerListener(this, Events_Loop::eventByName("LongOperation"));
143 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_PLUGIN_LOADED));
145 registerValidators();
148 myMainWindow->show();
149 updateCommandStatus();
151 XGUI_Preferences::loadCustomProps();
155 //******************************************************
156 void XGUI_Workshop::initMenu()
158 myContextMenuMgr->createActions();
160 if (isSalomeMode()) {
161 // Create only Undo, Redo commands
162 QAction* aAction = salomeConnector()->addEditCommand("UNDO_CMD", tr("Undo"),
163 tr("Undo last command"),
164 QIcon(":pictures/undo.png"),
165 QKeySequence::Undo, false);
166 connect(aAction, SIGNAL(triggered(bool)), this, SLOT(onUndo()));
167 aAction = salomeConnector()->addEditCommand("REDO_CMD", tr("Redo"), tr("Redo last command"),
168 QIcon(":pictures/redo.png"), QKeySequence::Redo,
170 connect(aAction, SIGNAL(triggered(bool)), this, SLOT(onRedo()));
171 salomeConnector()->addEditMenuSeparator();
174 // File commands group
175 XGUI_MenuGroupPanel* aGroup = myMainWindow->menuObject()->generalPage();
177 XGUI_Command* aCommand;
179 aCommand = aGroup->addFeature("SAVE_CMD", tr("Save..."), tr("Save the document"),
180 QIcon(":pictures/save.png"), QKeySequence::Save);
181 aCommand->connectTo(this, SLOT(onSave()));
182 //aCommand->disable();
184 aCommand = aGroup->addFeature("UNDO_CMD", tr("Undo"), tr("Undo last command"),
185 QIcon(":pictures/undo.png"), QKeySequence::Undo);
186 aCommand->connectTo(this, SLOT(onUndo()));
188 aCommand = aGroup->addFeature("REDO_CMD", tr("Redo"), tr("Redo last command"),
189 QIcon(":pictures/redo.png"), QKeySequence::Redo);
190 aCommand->connectTo(this, SLOT(onRedo()));
192 aCommand = aGroup->addFeature("REBUILD_CMD", tr("Rebuild"), tr("Rebuild data objects"),
193 QIcon(":pictures/rebuild.png"));
195 aCommand = aGroup->addFeature("SAVEAS_CMD", tr("Save as..."), tr("Save the document into a file"),
196 QIcon(":pictures/save.png"));
197 aCommand->connectTo(this, SLOT(onSaveAs()));
198 //aCommand->disable();
200 aCommand = aGroup->addFeature("OPEN_CMD", tr("Open..."), tr("Open a new document"),
201 QIcon(":pictures/open.png"), QKeySequence::Open);
202 aCommand->connectTo(this, SLOT(onOpen()));
204 //aCommand = aGroup->addFeature("NEW_CMD", tr("New"), tr("Create a new document"),
205 // QIcon(":pictures/new.png"), QKeySequence::New);
206 //aCommand->connectTo(this, SLOT(onNew()));
208 aCommand = aGroup->addFeature("PREF_CMD", tr("Preferences"), tr("Edit preferences"),
209 QIcon(":pictures/preferences.png"), QKeySequence::Preferences);
210 aCommand->connectTo(this, SLOT(onPreferences()));
212 aCommand = aGroup->addFeature("EXIT_CMD", tr("Exit"), tr("Exit application"),
213 QIcon(":pictures/close.png"), QKeySequence::Close);
214 aCommand->connectTo(this, SLOT(onExit()));
215 //FIXME: SBH's test action. Can be used for some GUI tests.
217 // aCommand = aGroup->addFeature("TEST_CMD", "Test!", "Private debug button",
218 // QIcon(":pictures/close.png"), QKeySequence(), true);
219 // aCommand->connectTo(myMainWindow, SLOT(dockPythonConsole()));
223 //******************************************************
224 XGUI_Workbench* XGUI_Workshop::addWorkbench(const QString& theName)
226 XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
227 return aMenuBar->addWorkbench(theName);
230 //******************************************************
231 void XGUI_Workshop::processEvent(const Events_Message* theMessage)
233 //A message to start feature creation received.
234 if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_FEATURE_LOADED)) {
235 const Config_FeatureMessage* aFeatureMsg =
236 dynamic_cast<const Config_FeatureMessage*>(theMessage);
237 if (!aFeatureMsg->isInternal()) {
238 addFeature(aFeatureMsg);
242 // Process creation of Part
243 else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_CREATED)) {
244 const ModelAPI_ObjectUpdatedMessage* aUpdMsg =
245 dynamic_cast<const ModelAPI_ObjectUpdatedMessage*>(theMessage);
246 onFeatureCreatedMsg(aUpdMsg);
248 if (mySalomeConnector)
249 mySalomeConnector->createPreferences();
250 myUpdatePrefs = false;
253 else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_PLUGIN_LOADED)) {
254 myUpdatePrefs = true;
258 else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY)) {
259 const ModelAPI_ObjectUpdatedMessage* aUpdMsg =
260 dynamic_cast<const ModelAPI_ObjectUpdatedMessage*>(theMessage);
261 onFeatureRedisplayMsg(aUpdMsg);
264 //Update property panel on corresponding message. If there is no current operation (no
265 //property panel), or received message has different feature to the current - do nothing.
266 else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_UPDATED)) {
267 const ModelAPI_ObjectUpdatedMessage* anUpdateMsg =
268 dynamic_cast<const ModelAPI_ObjectUpdatedMessage*>(theMessage);
269 onFeatureUpdatedMsg(anUpdateMsg);
272 else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_DELETED)) {
273 const ModelAPI_ObjectDeletedMessage* aDelMsg =
274 dynamic_cast<const ModelAPI_ObjectDeletedMessage*>(theMessage);
275 onObjectDeletedMsg(aDelMsg);
278 else if (theMessage->eventID() == Events_LongOp::eventID()) {
279 if (Events_LongOp::isPerformed())
280 QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
281 //QTimer::singleShot(10, this, SLOT(onStartWaiting()));
283 QApplication::restoreOverrideCursor();
286 //An operation passed by message. Start it, process and commit.
287 else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OPERATION_LAUNCHED)) {
288 const Config_PointerMessage* aPartSetMsg =
289 dynamic_cast<const Config_PointerMessage*>(theMessage);
290 //myPropertyPanel->cleanContent();
291 ModuleBase_Operation* anOperation = (ModuleBase_Operation*) aPartSetMsg->pointer();
293 if (myOperationMgr->startOperation(anOperation)) {
294 myPropertyPanel->updateContentWidget(anOperation->feature());
295 if (!anOperation->getDescription()->hasXmlRepresentation()) {
296 if (anOperation->commit())
297 updateCommandStatus();
301 //Show error dialog if error message received.
302 const Events_Error* anAppError = dynamic_cast<const Events_Error*>(theMessage);
304 emit errorOccurred(QString::fromLatin1(anAppError->description()));
307 if (!isSalomeMode()) {
308 SessionPtr aMgr = ModelAPI_Session::get();
309 if (aMgr->isModified() != myMainWindow->isModifiedState())
310 myMainWindow->setModifiedState(aMgr->isModified());
315 //******************************************************
316 void XGUI_Workshop::onStartWaiting()
318 if (Events_LongOp::isPerformed()) {
319 QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
323 //******************************************************
324 void XGUI_Workshop::onFeatureUpdatedMsg(const ModelAPI_ObjectUpdatedMessage* theMsg)
326 std::set<ObjectPtr> aFeatures = theMsg->objects();
327 if (myOperationMgr->hasOperation()) {
328 FeaturePtr aCurrentFeature = myOperationMgr->currentOperation()->feature();
329 std::set<ObjectPtr>::const_iterator aIt;
330 for (aIt = aFeatures.begin(); aIt != aFeatures.end(); ++aIt) {
331 ObjectPtr aNewFeature = (*aIt);
332 if (aNewFeature == aCurrentFeature) {
333 myPropertyPanel->updateContentWidget(aCurrentFeature);
338 myOperationMgr->validateCurrentOperation();
341 //******************************************************
342 void XGUI_Workshop::onFeatureRedisplayMsg(const ModelAPI_ObjectUpdatedMessage* theMsg)
344 std::set<ObjectPtr> aObjects = theMsg->objects();
345 std::set<ObjectPtr>::const_iterator aIt;
346 for (aIt = aObjects.begin(); aIt != aObjects.end(); ++aIt) {
347 ObjectPtr aObj = (*aIt);
348 if (!aObj->data() || !aObj->data()->isValid())
349 myDisplayer->erase(aObj, false);
351 if (myDisplayer->isVisible(aObj)) // TODO VSV: Correction sketch drawing
352 myDisplayer->display(aObj, false); // In order to update presentation
354 if (myOperationMgr->hasOperation()) {
355 ModuleBase_Operation* aOperation = myOperationMgr->currentOperation();
356 if (aOperation->hasObject(aObj)) { // Display only current operation results
357 myDisplayer->display(aObj, false);
363 myDisplayer->updateViewer();
366 //******************************************************
367 void XGUI_Workshop::onFeatureCreatedMsg(const ModelAPI_ObjectUpdatedMessage* theMsg)
369 std::set<ObjectPtr> aObjects = theMsg->objects();
371 std::set<ObjectPtr>::const_iterator aIt;
372 bool aHasPart = false;
373 bool isDisplayed = false;
374 for (aIt = aObjects.begin(); aIt != aObjects.end(); ++aIt) {
375 ResultPartPtr aPart = boost::dynamic_pointer_cast<ModelAPI_ResultPart>(*aIt);
378 // If a feature is created from the aplication's python console
379 // it doesn't stored in the operation mgr and doesn't displayed
380 } else if (myOperationMgr->hasOperation()) {
381 ModuleBase_Operation* aOperation = myOperationMgr->currentOperation();
382 if (aOperation->hasObject(*aIt)) { // Display only current operation results
383 myDisplayer->display(*aIt, false);
389 myDisplayer->updateViewer();
391 //The created part will be created in Object Browser later and we have to activate it
392 // only when it is created everywere
393 QTimer::singleShot(50, this, SLOT(activateLastPart()));
397 //******************************************************
398 void XGUI_Workshop::onObjectDeletedMsg(const ModelAPI_ObjectDeletedMessage* theMsg)
400 //std::set<ObjectPtr> aFeatures = theMsg->objects();
403 //******************************************************
404 void XGUI_Workshop::onOperationStarted()
406 ModuleBase_Operation* aOperation = myOperationMgr->currentOperation();
407 if (this->isSalomeMode())
408 aOperation->setNestedFeatures(mySalomeConnector->nestedActions(aOperation->id()));
410 aOperation->setNestedFeatures(myActionsMgr->nestedCommands(aOperation->id()));
412 if (aOperation->getDescription()->hasXmlRepresentation()) { //!< No need for property panel
413 connectWithOperation(aOperation);
416 QString aXmlRepr = aOperation->getDescription()->xmlRepresentation();
417 ModuleBase_WidgetFactory aFactory = ModuleBase_WidgetFactory(aXmlRepr.toStdString(),
420 myPropertyPanel->cleanContent();
421 aFactory.createWidget(myPropertyPanel->contentWidget());
423 QList<ModuleBase_ModelWidget*> aWidgets = aFactory.getModelWidgets();
424 QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
425 ModuleBase_ModelWidget* aWidget;
426 for (; anIt != aLast; anIt++) {
428 aWidget->setFeature(aOperation->feature());
429 QObject::connect(aWidget, SIGNAL(valuesChanged()), this, SLOT(onWidgetValuesChanged()));
430 // Init default values
431 if (!aOperation->isEditOperation() && !aWidget->isComputedDefault()) {
432 aWidget->storeValue();
436 myPropertyPanel->setModelWidgets(aWidgets);
437 myPropertyPanel->setWindowTitle(aOperation->getDescription()->description());
439 updateCommandStatus();
442 //******************************************************
443 void XGUI_Workshop::onOperationStopped(ModuleBase_Operation* theOperation)
445 //!< No need for property panel
446 updateCommandStatus();
448 myPropertyPanel->cleanContent();
454 void XGUI_Workshop::addFeature(const Config_FeatureMessage* theMessage)
458 qDebug() << "XGUI_Workshop::addFeature: NULL message.";
462 // Remember features icons
463 myIcons[QString::fromStdString(theMessage->id())] = QString::fromStdString(theMessage->icon());
465 //Find or create Workbench
466 QString aWchName = QString::fromStdString(theMessage->workbenchId());
467 QString aNestedFeatures = QString::fromStdString(theMessage->nestedFeatures());
468 bool isUsePropPanel = theMessage->isUseInput();
469 QString aFeatureId = QString::fromStdString(theMessage->id());
470 if (isSalomeMode()) {
471 QAction* aAction = salomeConnector()->addFeature(aWchName, aFeatureId,
472 QString::fromStdString(theMessage->text()),
473 QString::fromStdString(theMessage->tooltip()),
474 QIcon(theMessage->icon().c_str()),
475 QKeySequence(), isUsePropPanel);
476 salomeConnector()->setNestedActions(aFeatureId, aNestedFeatures.split(" ", QString::SkipEmptyParts));
477 myActionsMgr->addCommand(aAction);
478 myModule->featureCreated(aAction);
481 XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
482 XGUI_Workbench* aPage = aMenuBar->findWorkbench(aWchName);
484 aPage = addWorkbench(aWchName);
486 //Find or create Group
487 QString aGroupName = QString::fromStdString(theMessage->groupId());
488 XGUI_MenuGroupPanel* aGroup = aPage->findGroup(aGroupName);
490 aGroup = aPage->addGroup(aGroupName);
492 // Check if hotkey sequence is already defined:
493 QKeySequence aHotKey = myActionsMgr->registerShortcut(
494 QString::fromStdString(theMessage->keysequence()));
496 XGUI_Command* aCommand = aGroup->addFeature(aFeatureId,
497 QString::fromStdString(theMessage->text()),
498 QString::fromStdString(theMessage->tooltip()),
499 QIcon(theMessage->icon().c_str()), aHotKey,
501 aCommand->setNestedCommands(aNestedFeatures.split(" ", QString::SkipEmptyParts));
502 myActionsMgr->addCommand(aCommand);
503 myModule->featureCreated(aCommand);
508 * Makes a signal/slot connections between Property Panel
509 * and given operation. The given operation becomes a
510 * current operation and previous operation if exists
512 void XGUI_Workshop::connectWithOperation(ModuleBase_Operation* theOperation)
514 QAction* aCommand = 0;
515 if (isSalomeMode()) {
516 aCommand = salomeConnector()->command(theOperation->getDescription()->operationId());
518 XGUI_MainMenu* aMenu = myMainWindow->menuObject();
519 aCommand = aMenu->feature(theOperation->getDescription()->operationId());
521 //Abort operation on uncheck the command
523 connect(aCommand, SIGNAL(triggered(bool)), theOperation, SLOT(setRunning(bool)));
527 * Saves document with given name.
529 void XGUI_Workshop::saveDocument(const QString& theName, std::list<std::string>& theFileNames)
531 QApplication::restoreOverrideCursor();
532 SessionPtr aMgr = ModelAPI_Session::get();
533 aMgr->save(theName.toLatin1().constData(), theFileNames);
534 QApplication::restoreOverrideCursor();
537 //******************************************************
538 void XGUI_Workshop::onExit()
540 SessionPtr aMgr = ModelAPI_Session::get();
541 if (aMgr->isModified()) {
542 int anAnswer = QMessageBox::question(
543 myMainWindow, tr("Save current file"), tr("The document is modified, save before exit?"),
544 QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel, QMessageBox::Cancel);
545 if (anAnswer == QMessageBox::Save) {
546 bool saved = onSave();
550 } else if (anAnswer == QMessageBox::Cancel) {
557 //******************************************************
558 void XGUI_Workshop::onNew()
560 QApplication::setOverrideCursor(Qt::WaitCursor);
561 if (objectBrowser() == 0) {
563 mySelector->connectViewers();
565 myViewerProxy->connectToViewer();
567 if (!isSalomeMode()) {
568 myMainWindow->showPythonConsole();
569 QMdiSubWindow* aWnd = myMainWindow->viewer()->createView();
570 aWnd->showMaximized();
571 updateCommandStatus();
573 myContextMenuMgr->connectViewer();
574 QApplication::restoreOverrideCursor();
577 //******************************************************
578 void XGUI_Workshop::onOpen()
580 if(!myOperationMgr->abortOperation())
582 //save current file before close if modified
583 SessionPtr aSession = ModelAPI_Session::get();
584 if (aSession->isModified()) {
585 //TODO(sbh): re-launch the app?
586 int anAnswer = QMessageBox::question(
587 myMainWindow, tr("Save current file"),
588 tr("The document is modified, save before opening another?"),
589 QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel, QMessageBox::Cancel);
590 if (anAnswer == QMessageBox::Save) {
592 } else if (anAnswer == QMessageBox::Cancel) {
595 aSession->moduleDocument()->close();
599 //show file dialog, check if readable and open
600 myCurrentDir = QFileDialog::getExistingDirectory(mainWindow());
601 if (myCurrentDir.isEmpty())
603 QFileInfo aFileInfo(myCurrentDir);
604 if (!aFileInfo.exists() || !aFileInfo.isReadable()) {
605 QMessageBox::critical(myMainWindow, tr("Warning"), tr("Unable to open the file."));
609 QApplication::setOverrideCursor(Qt::WaitCursor);
610 aSession->load(myCurrentDir.toLatin1().constData());
611 myObjectBrowser->rebuildDataTree();
613 updateCommandStatus();
614 QApplication::restoreOverrideCursor();
617 //******************************************************
618 bool XGUI_Workshop::onSave()
620 if(!myOperationMgr->abortOperation())
622 if (myCurrentDir.isEmpty()) {
625 std::list<std::string> aFiles;
626 saveDocument(myCurrentDir, aFiles);
627 updateCommandStatus();
628 myMainWindow->setModifiedState(false);
632 //******************************************************
633 bool XGUI_Workshop::onSaveAs()
635 if(!myOperationMgr->abortOperation())
637 QFileDialog dialog(mainWindow());
638 dialog.setWindowTitle(tr("Select directory to save files..."));
639 dialog.setFileMode(QFileDialog::Directory);
640 dialog.setFilter(tr("Folders (*)"));
641 dialog.setOptions(QFileDialog::HideNameFilterDetails | QFileDialog::ShowDirsOnly);
642 dialog.setViewMode(QFileDialog::Detail);
644 if (!dialog.exec()) {
647 QString aTempDir = dialog.selectedFiles().first();
649 if (aDir.exists() && !aDir.entryInfoList(QDir::NoDotAndDotDot | QDir::AllEntries).isEmpty()) {
650 int answer = QMessageBox::question(
652 //: Title of the dialog which asks user if he wants to save study in existing non-empty folder
654 tr("The folder already contains some files, save anyway?"),
655 QMessageBox::Save | QMessageBox::Cancel);
656 if (answer == QMessageBox::Cancel) {
660 myCurrentDir = aTempDir;
661 if (!isSalomeMode()) {
662 myMainWindow->setCurrentDir(myCurrentDir, false);
663 myMainWindow->setModifiedState(false);
668 //******************************************************
669 void XGUI_Workshop::onUndo()
671 objectBrowser()->treeView()->setCurrentIndex(QModelIndex());
672 SessionPtr aMgr = ModelAPI_Session::get();
673 if (aMgr->isOperation())
674 operationMgr()->abortOperation();
676 updateCommandStatus();
679 //******************************************************
680 void XGUI_Workshop::onRedo()
682 objectBrowser()->treeView()->setCurrentIndex(QModelIndex());
683 SessionPtr aMgr = ModelAPI_Session::get();
684 if (aMgr->isOperation())
685 operationMgr()->abortOperation();
687 updateCommandStatus();
690 //******************************************************
691 void XGUI_Workshop::onPreferences()
694 XGUI_Preferences::editPreferences(aModif);
695 if (aModif.size() > 0) {
697 foreach (XGUI_Pref aPref, aModif)
699 aSection = aPref.first;
700 if (aSection == XGUI_Preferences::VIEWER_SECTION) {
702 myMainWindow->viewer()->updateFromResources();
703 } else if (aSection == XGUI_Preferences::MENU_SECTION) {
705 myMainWindow->menuObject()->updateFromResources();
711 //******************************************************
712 ModuleBase_IModule* XGUI_Workshop::loadModule(const QString& theModule)
714 QString libName = QString::fromStdString(library(theModule.toStdString()));
715 if (libName.isEmpty()) {
716 qWarning(qPrintable(tr("Information about module \"%1\" doesn't exist.").arg(theModule)));
721 CREATE_FUNC crtInst = 0;
724 HINSTANCE modLib = ::LoadLibrary((LPTSTR) qPrintable(libName));
728 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
729 0, ::GetLastError(), 0, (LPTSTR) & lpMsgBuf, 0, 0);
730 QString aMsg((char*) &lpMsgBuf);
731 err = QString("Failed to load %1. %2").arg(libName).arg(aMsg);
732 ::LocalFree(lpMsgBuf);
734 crtInst = (CREATE_FUNC) ::GetProcAddress(modLib, CREATE_MODULE);
738 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM
739 | FORMAT_MESSAGE_IGNORE_INSERTS,
740 0, ::GetLastError(), 0, (LPTSTR) & lpMsgBuf, 0, 0);
741 QString aMsg((char*) &lpMsgBuf);
742 err = QString("Failed to find %1 function. %2").arg( CREATE_MODULE).arg(aMsg);
743 ::LocalFree(lpMsgBuf);
747 void* modLib = dlopen( libName.toLatin1(), RTLD_LAZY | RTLD_GLOBAL );
749 err = QString( "Can not load library %1. %2" ).arg( libName ).arg( dlerror() );
751 crtInst = (CREATE_FUNC)dlsym( modLib, CREATE_MODULE );
753 err = QString( "Failed to find function %1. %2" ).arg( CREATE_MODULE ).arg( dlerror() );
758 ModuleBase_IModule* aModule = crtInst ? crtInst(this) : 0;
760 if (!err.isEmpty()) {
762 QMessageBox::warning(mainWindow(), tr("Error"), err);
764 qWarning(qPrintable(err));
770 //******************************************************
771 bool XGUI_Workshop::activateModule()
773 Config_ModuleReader aModuleReader;
774 QString moduleName = QString::fromStdString(aModuleReader.getModuleName());
775 myModule = loadModule(moduleName);
778 myModule->createFeatures();
779 myActionsMgr->update();
783 //******************************************************
784 void XGUI_Workshop::updateCommandStatus()
786 QList<QAction*> aCommands;
787 if (isSalomeMode()) { // update commands in SALOME mode
788 aCommands = salomeConnector()->commandList();
790 XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
791 foreach (XGUI_Command* aCmd, aMenuBar->features())
792 aCommands.append(aCmd);
794 SessionPtr aMgr = ModelAPI_Session::get();
795 if (aMgr->hasModuleDocument()) {
798 foreach(QAction* aCmd, aCommands)
800 QString aId = aCmd->data().toString();
801 if (aId == "UNDO_CMD")
803 else if (aId == "REDO_CMD")
806 // Enable all commands
807 aCmd->setEnabled(true);
809 aUndoCmd->setEnabled(aMgr->canUndo());
810 aRedoCmd->setEnabled(aMgr->canRedo());
812 foreach(QAction* aCmd, aCommands)
814 QString aId = aCmd->data().toString();
815 if (aId == "NEW_CMD")
816 aCmd->setEnabled(true);
817 else if (aId == "EXIT_CMD")
818 aCmd->setEnabled(true);
820 aCmd->setEnabled(false);
823 myActionsMgr->update();
826 //******************************************************
827 QList<QAction*> XGUI_Workshop::getModuleCommands() const
829 QList<QAction*> aCommands;
830 if (isSalomeMode()) { // update commands in SALOME mode
831 aCommands = salomeConnector()->commandList();
833 XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
834 foreach(XGUI_Command* aCmd, aMenuBar->features())
836 aCommands.append(aCmd);
842 //******************************************************
843 QDockWidget* XGUI_Workshop::createObjectBrowser(QWidget* theParent)
845 QDockWidget* aObjDock = new QDockWidget(theParent);
846 aObjDock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea | Qt::BottomDockWidgetArea);
847 aObjDock->setWindowTitle(tr("Object browser"));
848 aObjDock->setStyleSheet(
849 "::title { position: relative; padding-left: 5px; text-align: left center }");
850 myObjectBrowser = new XGUI_ObjectsBrowser(aObjDock);
851 connect(myObjectBrowser, SIGNAL(activePartChanged(ObjectPtr)), this,
852 SLOT(changeCurrentDocument(ObjectPtr)));
853 aObjDock->setWidget(myObjectBrowser);
855 myContextMenuMgr->connectObjectBrowser();
859 //******************************************************
861 * Creates dock widgets, places them in corresponding area
862 * and tabifies if necessary.
864 void XGUI_Workshop::createDockWidgets()
866 QMainWindow* aDesktop = isSalomeMode() ? salomeConnector()->desktop() : myMainWindow;
867 QDockWidget* aObjDock = createObjectBrowser(aDesktop);
868 aDesktop->addDockWidget(Qt::LeftDockWidgetArea, aObjDock);
869 myPropertyPanel = new XGUI_PropertyPanel(aDesktop);
870 myPropertyPanel->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea | Qt::BottomDockWidgetArea);
871 aDesktop->addDockWidget(Qt::LeftDockWidgetArea, myPropertyPanel);
872 hidePropertyPanel(); //<! Invisible by default
874 aDesktop->tabifyDockWidget(aObjDock, myPropertyPanel);
876 QPushButton* aOkBtn = myPropertyPanel->findChild<QPushButton*>(XGUI::PROP_PANEL_OK);
877 connect(aOkBtn, SIGNAL(clicked()), myOperationMgr, SLOT(onCommitOperation()));
878 QPushButton* aCancelBtn = myPropertyPanel->findChild<QPushButton*>(XGUI::PROP_PANEL_CANCEL);
879 connect(aCancelBtn, SIGNAL(clicked()), myOperationMgr, SLOT(onAbortOperation()));
880 //TODO(sbh): KeyReleasedProblem
881 connect(myPropertyPanel, SIGNAL(keyReleased(QKeyEvent*)), myOperationMgr,
882 SLOT(onKeyReleased(QKeyEvent*)));
884 connect(myPropertyPanel, SIGNAL(widgetActivated(ModuleBase_ModelWidget*)), myOperationMgr,
885 SLOT(onWidgetActivated(ModuleBase_ModelWidget*)));
886 connect(myOperationMgr, SIGNAL(activateNextWidget(ModuleBase_ModelWidget*)), myPropertyPanel,
887 SLOT(onActivateNextWidget(ModuleBase_ModelWidget*)));
888 connect(myOperationMgr, SIGNAL(operationValidated(bool)), myPropertyPanel,
889 SLOT(setAcceptEnabled(bool)));
893 //******************************************************
894 void XGUI_Workshop::showPropertyPanel()
896 QAction* aViewAct = myPropertyPanel->toggleViewAction();
897 //<! Restore ability to close panel from the window's menu
898 aViewAct->setEnabled(true);
899 myPropertyPanel->show();
900 myPropertyPanel->raise();
903 //******************************************************
904 void XGUI_Workshop::hidePropertyPanel()
906 QAction* aViewAct = myPropertyPanel->toggleViewAction();
907 //<! Do not allow to show empty property panel
908 aViewAct->setEnabled(false);
909 myPropertyPanel->hide();
912 //******************************************************
913 void XGUI_Workshop::showObjectBrowser()
915 myObjectBrowser->parentWidget()->show();
918 //******************************************************
919 void XGUI_Workshop::hideObjectBrowser()
921 myObjectBrowser->parentWidget()->hide();
924 //******************************************************
925 void XGUI_Workshop::onFeatureTriggered()
927 QAction* aCmd = dynamic_cast<QAction*>(sender());
929 QString aId = salomeConnector()->commandId(aCmd);
931 myModule->launchOperation(aId);
935 //******************************************************
936 void XGUI_Workshop::changeCurrentDocument(ObjectPtr theObj)
938 SessionPtr aMgr = ModelAPI_Session::get();
940 ResultPartPtr aPart = boost::dynamic_pointer_cast<ModelAPI_ResultPart>(theObj);
942 DocumentPtr aPartDoc = aPart->partDoc();
944 aMgr->setActiveDocument(aPartDoc);
949 aMgr->setActiveDocument(aMgr->moduleDocument());
952 //******************************************************
953 void XGUI_Workshop::salomeViewerSelectionChanged()
955 emit salomeViewerSelection();
958 //**************************************************************
959 XGUI_SalomeViewer* XGUI_Workshop::salomeViewer() const
961 return mySalomeConnector->viewer();
964 //**************************************************************
965 void XGUI_Workshop::onContextMenuCommand(const QString& theId, bool isChecked)
967 QList<ObjectPtr> aObjects = mySelector->selection()->selectedObjects();
968 if ((theId == "ACTIVATE_PART_CMD") && (aObjects.size() > 0)) {
969 ResultPartPtr aPart = boost::dynamic_pointer_cast<ModelAPI_ResultPart>(aObjects.first());
971 } else if (theId == "DEACTIVATE_PART_CMD")
972 activatePart(ResultPartPtr());
973 else if (theId == "DELETE_CMD")
974 deleteObjects(aObjects);
975 else if (theId == "SHOW_CMD")
976 showObjects(aObjects, true);
977 else if (theId == "HIDE_CMD")
978 showObjects(aObjects, false);
979 else if (theId == "SHOW_ONLY_CMD")
980 showOnlyObjects(aObjects);
983 //**************************************************************
984 void XGUI_Workshop::onWidgetValuesChanged()
986 ModuleBase_Operation* anOperation = myOperationMgr->currentOperation();
987 FeaturePtr aFeature = anOperation->feature();
989 ModuleBase_ModelWidget* aSenderWidget = dynamic_cast<ModuleBase_ModelWidget*>(sender());
991 // aCustom->storeValue(aFeature);
993 const QList<ModuleBase_ModelWidget*>& aWidgets = myPropertyPanel->modelWidgets();
994 QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
995 for (; anIt != aLast; anIt++) {
996 ModuleBase_ModelWidget* aCustom = *anIt;
997 if (aCustom && (/*!aCustom->isInitialized(aFeature) ||*/aCustom == aSenderWidget)) {
998 //aCustom->storeValue(aFeature);
999 aCustom->storeValue();
1004 //**************************************************************
1005 void XGUI_Workshop::activatePart(ResultPartPtr theFeature)
1008 theFeature->activate();
1009 changeCurrentDocument(theFeature);
1010 myObjectBrowser->activatePart(theFeature);
1013 //**************************************************************
1014 void XGUI_Workshop::activateLastPart()
1016 SessionPtr aMgr = ModelAPI_Session::get();
1017 DocumentPtr aDoc = aMgr->moduleDocument();
1018 std::string aGrpName = ModelAPI_ResultPart::group();
1019 ObjectPtr aLastPart = aDoc->object(aGrpName, aDoc->size(aGrpName) - 1);
1020 ResultPartPtr aPart = boost::dynamic_pointer_cast<ModelAPI_ResultPart>(aLastPart);
1022 activatePart(aPart);
1025 //**************************************************************
1026 void XGUI_Workshop::deleteObjects(const QList<ObjectPtr>& theList)
1028 QMainWindow* aDesktop = isSalomeMode() ? salomeConnector()->desktop() : myMainWindow;
1029 QMessageBox::StandardButton aRes = QMessageBox::warning(
1030 aDesktop, tr("Delete features"), tr("Seleted features will be deleted. Continue?"),
1031 QMessageBox::No | QMessageBox::Yes, QMessageBox::No);
1032 // ToDo: definbe deleting method
1033 if (aRes == QMessageBox::Yes) {
1034 SessionPtr aMgr = ModelAPI_Session::get();
1035 aMgr->startOperation();
1036 foreach (ObjectPtr aObj, theList)
1038 ResultPartPtr aPart = boost::dynamic_pointer_cast<ModelAPI_ResultPart>(aObj);
1040 DocumentPtr aDoc = aPart->document();
1041 if (aDoc == aMgr->activeDocument()) {
1044 //aMgr->moduleDocument()->removeFeature(aPart->owner());
1046 FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
1048 aObj->document()->removeFeature(aFeature);
1051 myDisplayer->updateViewer();
1052 aMgr->finishOperation();
1056 //**************************************************************
1057 void XGUI_Workshop::showObjects(const QList<ObjectPtr>& theList, bool isVisible)
1059 foreach (ObjectPtr aObj, theList)
1061 ResultPtr aRes = boost::dynamic_pointer_cast<ModelAPI_Result>(aObj);
1064 myDisplayer->display(aRes, false);
1066 myDisplayer->erase(aRes, false);
1070 myDisplayer->updateViewer();
1073 //**************************************************************
1074 void XGUI_Workshop::showOnlyObjects(const QList<ObjectPtr>& theList)
1076 myDisplayer->eraseAll(false);
1077 showObjects(theList, true);
1081 //**************************************************************
1082 void XGUI_Workshop::updateCommandsOnViewSelection()
1084 SessionPtr aMgr = ModelAPI_Session::get();
1085 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
1086 XGUI_Selection* aSelection = mySelector->selection();
1087 if (aSelection->getSelected().size() == 0)
1090 QList<QAction*> aActions = getModuleCommands();
1091 foreach(QAction* aAction, aActions)
1093 QString aId = aAction->data().toString();
1094 std::list<ModelAPI_Validator*> aValidators;
1095 std::list<std::list<std::string> > anArguments;
1096 aFactory->validators(aId.toStdString(), aValidators, anArguments);
1097 std::list<ModelAPI_Validator*>::iterator aValidator = aValidators.begin();
1098 for (; aValidator != aValidators.end(); aValidator++) {
1100 const ModuleBase_SelectionValidator* aSelValidator =
1101 dynamic_cast<const ModuleBase_SelectionValidator*>(*aValidator);
1102 if (aSelValidator) {
1103 aAction->setEnabled(aSelValidator->isValid(aSelection));
1110 //**************************************************************
1111 void XGUI_Workshop::registerValidators() const
1113 SessionPtr aMgr = ModelAPI_Session::get();
1114 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
1117 //**************************************************************
1118 void XGUI_Workshop::displayAllResults()
1120 SessionPtr aMgr = ModelAPI_Session::get();
1121 DocumentPtr aRootDoc = aMgr->moduleDocument();
1122 displayDocumentResults(aRootDoc);
1123 for (int i = 0; i < aRootDoc->size(ModelAPI_ResultPart::group()); i++) {
1124 ObjectPtr aObject = aRootDoc->object(ModelAPI_ResultPart::group(), i);
1125 ResultPartPtr aPart = boost::dynamic_pointer_cast<ModelAPI_ResultPart>(aObject);
1126 displayDocumentResults(aPart->partDoc());
1128 myDisplayer->updateViewer();
1131 //**************************************************************
1132 void XGUI_Workshop::displayDocumentResults(DocumentPtr theDoc)
1136 displayGroupResults(theDoc, ModelAPI_ResultConstruction::group());
1137 displayGroupResults(theDoc, ModelAPI_ResultBody::group());
1140 //**************************************************************
1141 void XGUI_Workshop::displayGroupResults(DocumentPtr theDoc, std::string theGroup)
1143 for (int i = 0; i < theDoc->size(theGroup); i++)
1144 myDisplayer->display(theDoc->object(theGroup, i), false);