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),
91 myPartActivating(false)
93 myMainWindow = mySalomeConnector ? 0 : new XGUI_MainWindow();
95 myDisplayer = new XGUI_Displayer(this);
97 mySelector = new XGUI_SelectionMgr(this);
98 //connect(mySelector, SIGNAL(selectionChanged()), this, SLOT(updateModuleCommands()));
100 myOperationMgr = new XGUI_OperationMgr(this);
101 myActionsMgr = new XGUI_ActionsMgr(this);
102 myErrorDlg = new XGUI_ErrorDialog(myMainWindow);
103 myContextMenuMgr = new XGUI_ContextMenuMgr(this);
104 connect(myContextMenuMgr, SIGNAL(actionTriggered(const QString&, bool)), this,
105 SLOT(onContextMenuCommand(const QString&, bool)));
107 myViewerProxy = new XGUI_ViewerProxy(this);
108 connect(myViewerProxy, SIGNAL(selectionChanged()), this, SLOT(updateCommandsOnViewSelection()));
110 myModuleConnector = new XGUI_ModuleConnector(this);
112 connect(myOperationMgr, SIGNAL(operationStarted()), SLOT(onOperationStarted()));
113 connect(myOperationMgr, SIGNAL(operationResumed()), SLOT(onOperationStarted()));
114 connect(myOperationMgr, SIGNAL(operationStopped(ModuleBase_Operation*)),
115 SLOT(onOperationStopped(ModuleBase_Operation*)));
116 connect(myMainWindow, SIGNAL(exitKeySequence()), SLOT(onExit()));
117 connect(myOperationMgr, SIGNAL(operationStarted()), myActionsMgr, SLOT(update()));
118 connect(myOperationMgr, SIGNAL(operationStopped(ModuleBase_Operation*)), myActionsMgr,
120 connect(this, SIGNAL(errorOccurred(const QString&)), myErrorDlg, SLOT(addError(const QString&)));
123 //******************************************************
124 XGUI_Workshop::~XGUI_Workshop(void)
128 //******************************************************
129 void XGUI_Workshop::startApplication()
132 //Initialize event listening
133 Events_Loop* aLoop = Events_Loop::loop();
134 aLoop->registerListener(this, Events_Error::errorID()); //!< Listening application errors.
135 //TODO(sbh): Implement static method to extract event id [SEID]
136 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_FEATURE_LOADED));
137 // TODO Is it good to use non standard event within workshop?
138 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OPERATION_LAUNCHED));
139 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
140 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_CREATED));
141 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
142 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_DELETED));
143 aLoop->registerListener(this, Events_Loop::eventByName("LongOperation"));
144 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_PLUGIN_LOADED));
145 aLoop->registerListener(this, Events_Loop::eventByName("CurrentDocumentChanged"));
147 registerValidators();
150 myMainWindow->show();
151 updateCommandStatus();
153 XGUI_Preferences::loadCustomProps();
157 //******************************************************
158 void XGUI_Workshop::initMenu()
160 myContextMenuMgr->createActions();
162 if (isSalomeMode()) {
163 // Create only Undo, Redo commands
164 QAction* aAction = salomeConnector()->addEditCommand("UNDO_CMD", tr("Undo"),
165 tr("Undo last command"),
166 QIcon(":pictures/undo.png"),
167 QKeySequence::Undo, false);
168 connect(aAction, SIGNAL(triggered(bool)), this, SLOT(onUndo()));
169 aAction = salomeConnector()->addEditCommand("REDO_CMD", tr("Redo"), tr("Redo last command"),
170 QIcon(":pictures/redo.png"), QKeySequence::Redo,
172 connect(aAction, SIGNAL(triggered(bool)), this, SLOT(onRedo()));
173 salomeConnector()->addEditMenuSeparator();
176 // File commands group
177 XGUI_MenuGroupPanel* aGroup = myMainWindow->menuObject()->generalPage();
179 XGUI_Command* aCommand;
181 aCommand = aGroup->addFeature("SAVE_CMD", tr("Save..."), tr("Save the document"),
182 QIcon(":pictures/save.png"), QKeySequence::Save);
183 aCommand->connectTo(this, SLOT(onSave()));
184 //aCommand->disable();
186 aCommand = aGroup->addFeature("UNDO_CMD", tr("Undo"), tr("Undo last command"),
187 QIcon(":pictures/undo.png"), QKeySequence::Undo);
188 aCommand->connectTo(this, SLOT(onUndo()));
190 aCommand = aGroup->addFeature("REDO_CMD", tr("Redo"), tr("Redo last command"),
191 QIcon(":pictures/redo.png"), QKeySequence::Redo);
192 aCommand->connectTo(this, SLOT(onRedo()));
194 aCommand = aGroup->addFeature("REBUILD_CMD", tr("Rebuild"), tr("Rebuild data objects"),
195 QIcon(":pictures/rebuild.png"));
197 aCommand = aGroup->addFeature("SAVEAS_CMD", tr("Save as..."), tr("Save the document into a file"),
198 QIcon(":pictures/save.png"));
199 aCommand->connectTo(this, SLOT(onSaveAs()));
200 //aCommand->disable();
202 aCommand = aGroup->addFeature("OPEN_CMD", tr("Open..."), tr("Open a new document"),
203 QIcon(":pictures/open.png"), QKeySequence::Open);
204 aCommand->connectTo(this, SLOT(onOpen()));
206 //aCommand = aGroup->addFeature("NEW_CMD", tr("New"), tr("Create a new document"),
207 // QIcon(":pictures/new.png"), QKeySequence::New);
208 //aCommand->connectTo(this, SLOT(onNew()));
210 aCommand = aGroup->addFeature("PREF_CMD", tr("Preferences"), tr("Edit preferences"),
211 QIcon(":pictures/preferences.png"), QKeySequence::Preferences);
212 aCommand->connectTo(this, SLOT(onPreferences()));
214 aCommand = aGroup->addFeature("EXIT_CMD", tr("Exit"), tr("Exit application"),
215 QIcon(":pictures/close.png"), QKeySequence::Close);
216 aCommand->connectTo(this, SLOT(onExit()));
217 //FIXME: SBH's test action. Can be used for some GUI tests.
219 // aCommand = aGroup->addFeature("TEST_CMD", "Test!", "Private debug button",
220 // QIcon(":pictures/close.png"), QKeySequence(), true);
221 // aCommand->connectTo(myMainWindow, SLOT(dockPythonConsole()));
225 //******************************************************
226 XGUI_Workbench* XGUI_Workshop::addWorkbench(const QString& theName)
228 XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
229 return aMenuBar->addWorkbench(theName);
232 //******************************************************
233 void XGUI_Workshop::processEvent(const Events_Message* theMessage)
235 //A message to start feature creation received.
236 if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_FEATURE_LOADED)) {
237 const Config_FeatureMessage* aFeatureMsg =
238 dynamic_cast<const Config_FeatureMessage*>(theMessage);
239 if (!aFeatureMsg->isInternal()) {
240 addFeature(aFeatureMsg);
244 // Process creation of Part
245 else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_CREATED)) {
246 const ModelAPI_ObjectUpdatedMessage* aUpdMsg =
247 dynamic_cast<const ModelAPI_ObjectUpdatedMessage*>(theMessage);
248 onFeatureCreatedMsg(aUpdMsg);
250 if (mySalomeConnector)
251 mySalomeConnector->createPreferences();
252 myUpdatePrefs = false;
255 else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_PLUGIN_LOADED)) {
256 myUpdatePrefs = true;
260 else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY)) {
261 const ModelAPI_ObjectUpdatedMessage* aUpdMsg =
262 dynamic_cast<const ModelAPI_ObjectUpdatedMessage*>(theMessage);
263 onFeatureRedisplayMsg(aUpdMsg);
266 //Update property panel on corresponding message. If there is no current operation (no
267 //property panel), or received message has different feature to the current - do nothing.
268 else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_UPDATED)) {
269 const ModelAPI_ObjectUpdatedMessage* anUpdateMsg =
270 dynamic_cast<const ModelAPI_ObjectUpdatedMessage*>(theMessage);
271 onFeatureUpdatedMsg(anUpdateMsg);
274 else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_DELETED)) {
275 const ModelAPI_ObjectDeletedMessage* aDelMsg =
276 dynamic_cast<const ModelAPI_ObjectDeletedMessage*>(theMessage);
277 onObjectDeletedMsg(aDelMsg);
280 else if (theMessage->eventID() == Events_LongOp::eventID()) {
281 if (Events_LongOp::isPerformed())
282 QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
283 //QTimer::singleShot(10, this, SLOT(onStartWaiting()));
285 QApplication::restoreOverrideCursor();
288 //An operation passed by message. Start it, process and commit.
289 else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OPERATION_LAUNCHED)) {
290 const Config_PointerMessage* aPartSetMsg =
291 dynamic_cast<const Config_PointerMessage*>(theMessage);
292 //myPropertyPanel->cleanContent();
293 ModuleBase_Operation* anOperation = (ModuleBase_Operation*) aPartSetMsg->pointer();
295 if (myOperationMgr->startOperation(anOperation)) {
296 myPropertyPanel->updateContentWidget(anOperation->feature());
297 if (!anOperation->getDescription()->hasXmlRepresentation()) {
298 if (anOperation->commit())
299 updateCommandStatus();
303 else if (theMessage->eventID() == Events_Loop::loop()->eventByName("CurrentDocumentChanged")) {
304 // Find and Activate active part
305 if (myPartActivating)
307 SessionPtr aMgr = ModelAPI_Session::get();
308 DocumentPtr aActiveDoc = aMgr->activeDocument();
309 DocumentPtr aDoc = aMgr->moduleDocument();
310 if (aActiveDoc == aDoc) {
311 activatePart(ResultPartPtr());
314 std::string aGrpName = ModelAPI_ResultPart::group();
315 for (int i = 0; i < aDoc->size(aGrpName); i++) {
316 ResultPartPtr aPart = boost::dynamic_pointer_cast<ModelAPI_ResultPart>(aDoc->object(aGrpName, i));
317 if (aPart->partDoc() == aActiveDoc) {
318 activatePart(aPart); // Activate a part which corresponds to active Doc
322 // If not found then activate global document
323 activatePart(ResultPartPtr());
326 //Show error dialog if error message received.
327 const Events_Error* anAppError = dynamic_cast<const Events_Error*>(theMessage);
329 emit errorOccurred(QString::fromLatin1(anAppError->description()));
332 if (!isSalomeMode()) {
333 SessionPtr aMgr = ModelAPI_Session::get();
334 if (aMgr->isModified() != myMainWindow->isModifiedState())
335 myMainWindow->setModifiedState(aMgr->isModified());
340 //******************************************************
341 void XGUI_Workshop::onStartWaiting()
343 if (Events_LongOp::isPerformed()) {
344 QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
348 //******************************************************
349 void XGUI_Workshop::onFeatureUpdatedMsg(const ModelAPI_ObjectUpdatedMessage* theMsg)
351 std::set<ObjectPtr> aFeatures = theMsg->objects();
352 if (myOperationMgr->hasOperation()) {
353 FeaturePtr aCurrentFeature = myOperationMgr->currentOperation()->feature();
354 std::set<ObjectPtr>::const_iterator aIt;
355 for (aIt = aFeatures.begin(); aIt != aFeatures.end(); ++aIt) {
356 ObjectPtr aNewFeature = (*aIt);
357 if (aNewFeature == aCurrentFeature) {
358 myPropertyPanel->updateContentWidget(aCurrentFeature);
363 myOperationMgr->validateCurrentOperation();
366 //******************************************************
367 void XGUI_Workshop::onFeatureRedisplayMsg(const ModelAPI_ObjectUpdatedMessage* theMsg)
369 std::set<ObjectPtr> aObjects = theMsg->objects();
370 std::set<ObjectPtr>::const_iterator aIt;
371 for (aIt = aObjects.begin(); aIt != aObjects.end(); ++aIt) {
372 ObjectPtr aObj = (*aIt);
373 if (!aObj->data() || !aObj->data()->isValid())
374 myDisplayer->erase(aObj, false);
376 if (myDisplayer->isVisible(aObj)) // TODO VSV: Correction sketch drawing
377 myDisplayer->display(aObj, false); // In order to update presentation
379 if (myOperationMgr->hasOperation()) {
380 ModuleBase_Operation* aOperation = myOperationMgr->currentOperation();
381 if (aOperation->hasObject(aObj)) { // Display only current operation results
382 myDisplayer->display(aObj, false);
388 myDisplayer->updateViewer();
391 //******************************************************
392 void XGUI_Workshop::onFeatureCreatedMsg(const ModelAPI_ObjectUpdatedMessage* theMsg)
394 std::set<ObjectPtr> aObjects = theMsg->objects();
396 std::set<ObjectPtr>::const_iterator aIt;
397 bool aHasPart = false;
398 bool isDisplayed = false;
399 for (aIt = aObjects.begin(); aIt != aObjects.end(); ++aIt) {
400 ResultPartPtr aPart = boost::dynamic_pointer_cast<ModelAPI_ResultPart>(*aIt);
403 // If a feature is created from the aplication's python console
404 // it doesn't stored in the operation mgr and doesn't displayed
405 } else if (myOperationMgr->hasOperation()) {
406 ModuleBase_Operation* aOperation = myOperationMgr->currentOperation();
407 if (aOperation->hasObject(*aIt)) { // Display only current operation results
408 myDisplayer->display(*aIt, false);
414 myDisplayer->updateViewer();
416 //The created part will be created in Object Browser later and we have to activate it
417 // only when it is created everywere
418 QTimer::singleShot(50, this, SLOT(activateLastPart()));
422 //******************************************************
423 void XGUI_Workshop::onObjectDeletedMsg(const ModelAPI_ObjectDeletedMessage* theMsg)
425 //std::set<ObjectPtr> aFeatures = theMsg->objects();
428 //******************************************************
429 void XGUI_Workshop::onOperationStarted()
431 ModuleBase_Operation* aOperation = myOperationMgr->currentOperation();
432 if (this->isSalomeMode())
433 aOperation->setNestedFeatures(mySalomeConnector->nestedActions(aOperation->id()));
435 aOperation->setNestedFeatures(myActionsMgr->nestedCommands(aOperation->id()));
437 if (aOperation->getDescription()->hasXmlRepresentation()) { //!< No need for property panel
438 connectWithOperation(aOperation);
441 QString aXmlRepr = aOperation->getDescription()->xmlRepresentation();
442 ModuleBase_WidgetFactory aFactory = ModuleBase_WidgetFactory(aXmlRepr.toStdString(),
445 myPropertyPanel->cleanContent();
446 aFactory.createWidget(myPropertyPanel->contentWidget());
448 QList<ModuleBase_ModelWidget*> aWidgets = aFactory.getModelWidgets();
449 QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
450 ModuleBase_ModelWidget* aWidget;
451 for (; anIt != aLast; anIt++) {
453 aWidget->setFeature(aOperation->feature());
454 QObject::connect(aWidget, SIGNAL(valuesChanged()), this, SLOT(onWidgetValuesChanged()));
455 // Init default values
456 if (!aOperation->isEditOperation() && !aWidget->isComputedDefault()) {
457 aWidget->storeValue();
461 myPropertyPanel->setModelWidgets(aWidgets);
462 myPropertyPanel->setWindowTitle(aOperation->getDescription()->description());
464 updateCommandStatus();
467 //******************************************************
468 void XGUI_Workshop::onOperationStopped(ModuleBase_Operation* theOperation)
470 //!< No need for property panel
471 updateCommandStatus();
473 myPropertyPanel->cleanContent();
479 void XGUI_Workshop::addFeature(const Config_FeatureMessage* theMessage)
483 qDebug() << "XGUI_Workshop::addFeature: NULL message.";
487 // Remember features icons
488 myIcons[QString::fromStdString(theMessage->id())] = QString::fromStdString(theMessage->icon());
490 //Find or create Workbench
491 QString aWchName = QString::fromStdString(theMessage->workbenchId());
492 QString aNestedFeatures = QString::fromStdString(theMessage->nestedFeatures());
493 bool isUsePropPanel = theMessage->isUseInput();
494 QString aFeatureId = QString::fromStdString(theMessage->id());
495 if (isSalomeMode()) {
496 QAction* aAction = salomeConnector()->addFeature(aWchName, aFeatureId,
497 QString::fromStdString(theMessage->text()),
498 QString::fromStdString(theMessage->tooltip()),
499 QIcon(theMessage->icon().c_str()),
500 QKeySequence(), isUsePropPanel);
501 salomeConnector()->setNestedActions(aFeatureId, aNestedFeatures.split(" ", QString::SkipEmptyParts));
502 myActionsMgr->addCommand(aAction);
503 myModule->featureCreated(aAction);
506 XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
507 XGUI_Workbench* aPage = aMenuBar->findWorkbench(aWchName);
509 aPage = addWorkbench(aWchName);
511 //Find or create Group
512 QString aGroupName = QString::fromStdString(theMessage->groupId());
513 XGUI_MenuGroupPanel* aGroup = aPage->findGroup(aGroupName);
515 aGroup = aPage->addGroup(aGroupName);
517 // Check if hotkey sequence is already defined:
518 QKeySequence aHotKey = myActionsMgr->registerShortcut(
519 QString::fromStdString(theMessage->keysequence()));
521 XGUI_Command* aCommand = aGroup->addFeature(aFeatureId,
522 QString::fromStdString(theMessage->text()),
523 QString::fromStdString(theMessage->tooltip()),
524 QIcon(theMessage->icon().c_str()), aHotKey,
526 aCommand->setNestedCommands(aNestedFeatures.split(" ", QString::SkipEmptyParts));
527 myActionsMgr->addCommand(aCommand);
528 myModule->featureCreated(aCommand);
533 * Makes a signal/slot connections between Property Panel
534 * and given operation. The given operation becomes a
535 * current operation and previous operation if exists
537 void XGUI_Workshop::connectWithOperation(ModuleBase_Operation* theOperation)
539 QAction* aCommand = 0;
540 if (isSalomeMode()) {
541 aCommand = salomeConnector()->command(theOperation->getDescription()->operationId());
543 XGUI_MainMenu* aMenu = myMainWindow->menuObject();
544 aCommand = aMenu->feature(theOperation->getDescription()->operationId());
546 //Abort operation on uncheck the command
548 connect(aCommand, SIGNAL(triggered(bool)), theOperation, SLOT(setRunning(bool)));
552 * Saves document with given name.
554 void XGUI_Workshop::saveDocument(const QString& theName, std::list<std::string>& theFileNames)
556 QApplication::restoreOverrideCursor();
557 SessionPtr aMgr = ModelAPI_Session::get();
558 aMgr->save(theName.toLatin1().constData(), theFileNames);
559 QApplication::restoreOverrideCursor();
562 bool XGUI_Workshop::isActiveOperationAborted()
564 if(!myOperationMgr->hasOperation())
566 return myOperationMgr->abortOperation();
569 //******************************************************
570 void XGUI_Workshop::onExit()
572 SessionPtr aMgr = ModelAPI_Session::get();
573 if (aMgr->isModified()) {
574 int anAnswer = QMessageBox::question(
575 myMainWindow, tr("Save current file"), tr("The document is modified, save before exit?"),
576 QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel, QMessageBox::Cancel);
577 if (anAnswer == QMessageBox::Save) {
578 bool saved = onSave();
582 } else if (anAnswer == QMessageBox::Cancel) {
589 //******************************************************
590 void XGUI_Workshop::onNew()
592 QApplication::setOverrideCursor(Qt::WaitCursor);
593 if (objectBrowser() == 0) {
595 mySelector->connectViewers();
597 myViewerProxy->connectToViewer();
599 if (!isSalomeMode()) {
600 myMainWindow->showPythonConsole();
601 QMdiSubWindow* aWnd = myMainWindow->viewer()->createView();
602 aWnd->showMaximized();
603 updateCommandStatus();
605 myContextMenuMgr->connectViewer();
606 QApplication::restoreOverrideCursor();
609 //******************************************************
610 void XGUI_Workshop::onOpen()
612 if(!isActiveOperationAborted())
614 //save current file before close if modified
615 SessionPtr aSession = ModelAPI_Session::get();
616 if (aSession->isModified()) {
617 //TODO(sbh): re-launch the app?
618 int anAnswer = QMessageBox::question(
619 myMainWindow, tr("Save current file"),
620 tr("The document is modified, save before opening another?"),
621 QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel, QMessageBox::Cancel);
622 if (anAnswer == QMessageBox::Save) {
624 } else if (anAnswer == QMessageBox::Cancel) {
627 aSession->moduleDocument()->close();
631 //show file dialog, check if readable and open
632 myCurrentDir = QFileDialog::getExistingDirectory(mainWindow());
633 if (myCurrentDir.isEmpty())
635 QFileInfo aFileInfo(myCurrentDir);
636 if (!aFileInfo.exists() || !aFileInfo.isReadable()) {
637 QMessageBox::critical(myMainWindow, tr("Warning"), tr("Unable to open the file."));
641 QApplication::setOverrideCursor(Qt::WaitCursor);
642 aSession->load(myCurrentDir.toLatin1().constData());
643 myObjectBrowser->rebuildDataTree();
645 updateCommandStatus();
646 QApplication::restoreOverrideCursor();
649 //******************************************************
650 bool XGUI_Workshop::onSave()
652 if(!isActiveOperationAborted())
654 if (myCurrentDir.isEmpty()) {
657 std::list<std::string> aFiles;
658 saveDocument(myCurrentDir, aFiles);
659 updateCommandStatus();
660 myMainWindow->setModifiedState(false);
664 //******************************************************
665 bool XGUI_Workshop::onSaveAs()
667 if(!isActiveOperationAborted())
669 QFileDialog dialog(mainWindow());
670 dialog.setWindowTitle(tr("Select directory to save files..."));
671 dialog.setFileMode(QFileDialog::Directory);
672 dialog.setFilter(tr("Folders (*)"));
673 dialog.setOptions(QFileDialog::HideNameFilterDetails | QFileDialog::ShowDirsOnly);
674 dialog.setViewMode(QFileDialog::Detail);
676 if (!dialog.exec()) {
679 QString aTempDir = dialog.selectedFiles().first();
681 if (aDir.exists() && !aDir.entryInfoList(QDir::NoDotAndDotDot | QDir::AllEntries).isEmpty()) {
682 int answer = QMessageBox::question(
684 //: Title of the dialog which asks user if he wants to save study in existing non-empty folder
686 tr("The folder already contains some files, save anyway?"),
687 QMessageBox::Save | QMessageBox::Cancel);
688 if (answer == QMessageBox::Cancel) {
692 myCurrentDir = aTempDir;
693 if (!isSalomeMode()) {
694 myMainWindow->setCurrentDir(myCurrentDir, false);
695 myMainWindow->setModifiedState(false);
700 //******************************************************
701 void XGUI_Workshop::onUndo()
703 objectBrowser()->treeView()->setCurrentIndex(QModelIndex());
704 SessionPtr aMgr = ModelAPI_Session::get();
705 if (aMgr->isOperation())
706 operationMgr()->abortOperation();
708 updateCommandStatus();
711 //******************************************************
712 void XGUI_Workshop::onRedo()
714 objectBrowser()->treeView()->setCurrentIndex(QModelIndex());
715 SessionPtr aMgr = ModelAPI_Session::get();
716 if (aMgr->isOperation())
717 operationMgr()->abortOperation();
719 updateCommandStatus();
722 //******************************************************
723 void XGUI_Workshop::onPreferences()
726 XGUI_Preferences::editPreferences(aModif);
727 if (aModif.size() > 0) {
729 foreach (XGUI_Pref aPref, aModif)
731 aSection = aPref.first;
732 if (aSection == XGUI_Preferences::VIEWER_SECTION) {
734 myMainWindow->viewer()->updateFromResources();
735 } else if (aSection == XGUI_Preferences::MENU_SECTION) {
737 myMainWindow->menuObject()->updateFromResources();
743 //******************************************************
744 ModuleBase_IModule* XGUI_Workshop::loadModule(const QString& theModule)
746 QString libName = QString::fromStdString(library(theModule.toStdString()));
747 if (libName.isEmpty()) {
748 qWarning(qPrintable(tr("Information about module \"%1\" doesn't exist.").arg(theModule)));
753 CREATE_FUNC crtInst = 0;
756 HINSTANCE modLib = ::LoadLibrary((LPTSTR) qPrintable(libName));
760 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
761 0, ::GetLastError(), 0, (LPTSTR) & lpMsgBuf, 0, 0);
762 QString aMsg((char*) &lpMsgBuf);
763 err = QString("Failed to load %1. %2").arg(libName).arg(aMsg);
764 ::LocalFree(lpMsgBuf);
766 crtInst = (CREATE_FUNC) ::GetProcAddress(modLib, CREATE_MODULE);
770 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM
771 | FORMAT_MESSAGE_IGNORE_INSERTS,
772 0, ::GetLastError(), 0, (LPTSTR) & lpMsgBuf, 0, 0);
773 QString aMsg((char*) &lpMsgBuf);
774 err = QString("Failed to find %1 function. %2").arg( CREATE_MODULE).arg(aMsg);
775 ::LocalFree(lpMsgBuf);
779 void* modLib = dlopen( libName.toLatin1(), RTLD_LAZY | RTLD_GLOBAL );
781 err = QString( "Can not load library %1. %2" ).arg( libName ).arg( dlerror() );
783 crtInst = (CREATE_FUNC)dlsym( modLib, CREATE_MODULE );
785 err = QString( "Failed to find function %1. %2" ).arg( CREATE_MODULE ).arg( dlerror() );
790 ModuleBase_IModule* aModule = crtInst ? crtInst(this) : 0;
792 if (!err.isEmpty()) {
794 QMessageBox::warning(mainWindow(), tr("Error"), err);
796 qWarning(qPrintable(err));
802 //******************************************************
803 bool XGUI_Workshop::activateModule()
805 Config_ModuleReader aModuleReader;
806 QString moduleName = QString::fromStdString(aModuleReader.getModuleName());
807 myModule = loadModule(moduleName);
810 myModule->createFeatures();
811 myActionsMgr->update();
815 //******************************************************
816 void XGUI_Workshop::updateCommandStatus()
818 QList<QAction*> aCommands;
819 if (isSalomeMode()) { // update commands in SALOME mode
820 aCommands = salomeConnector()->commandList();
822 XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
823 foreach (XGUI_Command* aCmd, aMenuBar->features())
824 aCommands.append(aCmd);
826 SessionPtr aMgr = ModelAPI_Session::get();
827 if (aMgr->hasModuleDocument()) {
830 foreach(QAction* aCmd, aCommands)
832 QString aId = aCmd->data().toString();
833 if (aId == "UNDO_CMD")
835 else if (aId == "REDO_CMD")
838 // Enable all commands
839 aCmd->setEnabled(true);
841 aUndoCmd->setEnabled(aMgr->canUndo());
842 aRedoCmd->setEnabled(aMgr->canRedo());
844 foreach(QAction* aCmd, aCommands)
846 QString aId = aCmd->data().toString();
847 if (aId == "NEW_CMD")
848 aCmd->setEnabled(true);
849 else if (aId == "EXIT_CMD")
850 aCmd->setEnabled(true);
852 aCmd->setEnabled(false);
855 myActionsMgr->update();
858 //******************************************************
859 QList<QAction*> XGUI_Workshop::getModuleCommands() const
861 QList<QAction*> aCommands;
862 if (isSalomeMode()) { // update commands in SALOME mode
863 aCommands = salomeConnector()->commandList();
865 XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
866 foreach(XGUI_Command* aCmd, aMenuBar->features())
868 aCommands.append(aCmd);
874 //******************************************************
875 QDockWidget* XGUI_Workshop::createObjectBrowser(QWidget* theParent)
877 QDockWidget* aObjDock = new QDockWidget(theParent);
878 aObjDock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea | Qt::BottomDockWidgetArea);
879 aObjDock->setWindowTitle(tr("Object browser"));
880 aObjDock->setStyleSheet(
881 "::title { position: relative; padding-left: 5px; text-align: left center }");
882 myObjectBrowser = new XGUI_ObjectsBrowser(aObjDock);
883 connect(myObjectBrowser, SIGNAL(activePartChanged(ObjectPtr)), this,
884 SLOT(changeCurrentDocument(ObjectPtr)));
885 aObjDock->setWidget(myObjectBrowser);
887 myContextMenuMgr->connectObjectBrowser();
891 //******************************************************
893 * Creates dock widgets, places them in corresponding area
894 * and tabifies if necessary.
896 void XGUI_Workshop::createDockWidgets()
898 QMainWindow* aDesktop = isSalomeMode() ? salomeConnector()->desktop() : myMainWindow;
899 QDockWidget* aObjDock = createObjectBrowser(aDesktop);
900 aDesktop->addDockWidget(Qt::LeftDockWidgetArea, aObjDock);
901 myPropertyPanel = new XGUI_PropertyPanel(aDesktop);
902 myPropertyPanel->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea | Qt::BottomDockWidgetArea);
903 aDesktop->addDockWidget(Qt::LeftDockWidgetArea, myPropertyPanel);
904 hidePropertyPanel(); //<! Invisible by default
906 aDesktop->tabifyDockWidget(aObjDock, myPropertyPanel);
908 QPushButton* aOkBtn = myPropertyPanel->findChild<QPushButton*>(XGUI::PROP_PANEL_OK);
909 connect(aOkBtn, SIGNAL(clicked()), myOperationMgr, SLOT(onCommitOperation()));
910 QPushButton* aCancelBtn = myPropertyPanel->findChild<QPushButton*>(XGUI::PROP_PANEL_CANCEL);
911 connect(aCancelBtn, SIGNAL(clicked()), myOperationMgr, SLOT(onAbortOperation()));
912 //TODO(sbh): KeyReleasedProblem
913 connect(myPropertyPanel, SIGNAL(keyReleased(QKeyEvent*)), myOperationMgr,
914 SLOT(onKeyReleased(QKeyEvent*)));
916 connect(myPropertyPanel, SIGNAL(widgetActivated(ModuleBase_ModelWidget*)), myOperationMgr,
917 SLOT(onWidgetActivated(ModuleBase_ModelWidget*)));
918 connect(myOperationMgr, SIGNAL(activateNextWidget(ModuleBase_ModelWidget*)), myPropertyPanel,
919 SLOT(onActivateNextWidget(ModuleBase_ModelWidget*)));
920 connect(myOperationMgr, SIGNAL(operationValidated(bool)), myPropertyPanel,
921 SLOT(setAcceptEnabled(bool)));
925 //******************************************************
926 void XGUI_Workshop::showPropertyPanel()
928 QAction* aViewAct = myPropertyPanel->toggleViewAction();
929 //<! Restore ability to close panel from the window's menu
930 aViewAct->setEnabled(true);
931 myPropertyPanel->show();
932 myPropertyPanel->raise();
935 //******************************************************
936 void XGUI_Workshop::hidePropertyPanel()
938 QAction* aViewAct = myPropertyPanel->toggleViewAction();
939 //<! Do not allow to show empty property panel
940 aViewAct->setEnabled(false);
941 myPropertyPanel->hide();
944 //******************************************************
945 void XGUI_Workshop::showObjectBrowser()
947 myObjectBrowser->parentWidget()->show();
950 //******************************************************
951 void XGUI_Workshop::hideObjectBrowser()
953 myObjectBrowser->parentWidget()->hide();
956 //******************************************************
957 void XGUI_Workshop::onFeatureTriggered()
959 QAction* aCmd = dynamic_cast<QAction*>(sender());
961 QString aId = salomeConnector()->commandId(aCmd);
963 myModule->launchOperation(aId);
967 //******************************************************
968 void XGUI_Workshop::changeCurrentDocument(ObjectPtr theObj)
970 SessionPtr aMgr = ModelAPI_Session::get();
972 ResultPartPtr aPart = boost::dynamic_pointer_cast<ModelAPI_ResultPart>(theObj);
974 DocumentPtr aPartDoc = aPart->partDoc();
976 aMgr->setActiveDocument(aPartDoc);
981 aMgr->setActiveDocument(aMgr->moduleDocument());
984 //******************************************************
985 void XGUI_Workshop::salomeViewerSelectionChanged()
987 emit salomeViewerSelection();
990 //**************************************************************
991 XGUI_SalomeViewer* XGUI_Workshop::salomeViewer() const
993 return mySalomeConnector->viewer();
996 //**************************************************************
997 void XGUI_Workshop::onContextMenuCommand(const QString& theId, bool isChecked)
999 QList<ObjectPtr> aObjects = mySelector->selection()->selectedObjects();
1000 if ((theId == "ACTIVATE_PART_CMD") && (aObjects.size() > 0)) {
1001 ResultPartPtr aPart = boost::dynamic_pointer_cast<ModelAPI_ResultPart>(aObjects.first());
1002 activatePart(aPart);
1003 } else if (theId == "DEACTIVATE_PART_CMD")
1004 activatePart(ResultPartPtr());
1005 else if (theId == "DELETE_CMD")
1006 deleteObjects(aObjects);
1007 else if (theId == "SHOW_CMD")
1008 showObjects(aObjects, true);
1009 else if (theId == "HIDE_CMD")
1010 showObjects(aObjects, false);
1011 else if (theId == "SHOW_ONLY_CMD")
1012 showOnlyObjects(aObjects);
1015 //**************************************************************
1016 void XGUI_Workshop::onWidgetValuesChanged()
1018 ModuleBase_Operation* anOperation = myOperationMgr->currentOperation();
1019 FeaturePtr aFeature = anOperation->feature();
1021 ModuleBase_ModelWidget* aSenderWidget = dynamic_cast<ModuleBase_ModelWidget*>(sender());
1023 // aCustom->storeValue(aFeature);
1025 const QList<ModuleBase_ModelWidget*>& aWidgets = myPropertyPanel->modelWidgets();
1026 QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
1027 for (; anIt != aLast; anIt++) {
1028 ModuleBase_ModelWidget* aCustom = *anIt;
1029 if (aCustom && (/*!aCustom->isInitialized(aFeature) ||*/aCustom == aSenderWidget)) {
1030 //aCustom->storeValue(aFeature);
1031 aCustom->storeValue();
1036 //**************************************************************
1037 void XGUI_Workshop::activatePart(ResultPartPtr theFeature)
1039 if (!myPartActivating) {
1040 myPartActivating = true;
1042 theFeature->activate();
1043 changeCurrentDocument(theFeature);
1044 myObjectBrowser->activatePart(theFeature);
1045 myPartActivating = false;
1049 //**************************************************************
1050 void XGUI_Workshop::activateLastPart()
1052 SessionPtr aMgr = ModelAPI_Session::get();
1053 DocumentPtr aDoc = aMgr->moduleDocument();
1054 std::string aGrpName = ModelAPI_ResultPart::group();
1055 ObjectPtr aLastPart = aDoc->object(aGrpName, aDoc->size(aGrpName) - 1);
1056 ResultPartPtr aPart = boost::dynamic_pointer_cast<ModelAPI_ResultPart>(aLastPart);
1058 activatePart(aPart);
1061 //**************************************************************
1062 void XGUI_Workshop::deleteObjects(const QList<ObjectPtr>& theList)
1064 QMainWindow* aDesktop = isSalomeMode() ? salomeConnector()->desktop() : myMainWindow;
1065 QMessageBox::StandardButton aRes = QMessageBox::warning(
1066 aDesktop, tr("Delete features"), tr("Seleted features will be deleted. Continue?"),
1067 QMessageBox::No | QMessageBox::Yes, QMessageBox::No);
1068 // ToDo: definbe deleting method
1069 if (aRes == QMessageBox::Yes) {
1070 SessionPtr aMgr = ModelAPI_Session::get();
1071 aMgr->startOperation();
1072 foreach (ObjectPtr aObj, theList)
1074 ResultPartPtr aPart = boost::dynamic_pointer_cast<ModelAPI_ResultPart>(aObj);
1076 DocumentPtr aDoc = aPart->document();
1077 if (aDoc == aMgr->activeDocument()) {
1080 //aMgr->moduleDocument()->removeFeature(aPart->owner());
1082 FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
1084 aObj->document()->removeFeature(aFeature);
1087 myDisplayer->updateViewer();
1088 aMgr->finishOperation();
1092 //**************************************************************
1093 void XGUI_Workshop::showObjects(const QList<ObjectPtr>& theList, bool isVisible)
1095 foreach (ObjectPtr aObj, theList)
1097 ResultPtr aRes = boost::dynamic_pointer_cast<ModelAPI_Result>(aObj);
1100 myDisplayer->display(aRes, false);
1102 myDisplayer->erase(aRes, false);
1106 myDisplayer->updateViewer();
1109 //**************************************************************
1110 void XGUI_Workshop::showOnlyObjects(const QList<ObjectPtr>& theList)
1112 myDisplayer->eraseAll(false);
1113 showObjects(theList, true);
1117 //**************************************************************
1118 void XGUI_Workshop::updateCommandsOnViewSelection()
1120 SessionPtr aMgr = ModelAPI_Session::get();
1121 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
1122 XGUI_Selection* aSelection = mySelector->selection();
1123 if (aSelection->getSelected().size() == 0)
1126 QList<QAction*> aActions = getModuleCommands();
1127 foreach(QAction* aAction, aActions)
1129 QString aId = aAction->data().toString();
1130 std::list<ModelAPI_Validator*> aValidators;
1131 std::list<std::list<std::string> > anArguments;
1132 aFactory->validators(aId.toStdString(), aValidators, anArguments);
1133 std::list<ModelAPI_Validator*>::iterator aValidator = aValidators.begin();
1134 for (; aValidator != aValidators.end(); aValidator++) {
1136 const ModuleBase_SelectionValidator* aSelValidator =
1137 dynamic_cast<const ModuleBase_SelectionValidator*>(*aValidator);
1138 if (aSelValidator) {
1139 aAction->setEnabled(aSelValidator->isValid(aSelection));
1146 //**************************************************************
1147 void XGUI_Workshop::registerValidators() const
1149 SessionPtr aMgr = ModelAPI_Session::get();
1150 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
1153 //**************************************************************
1154 void XGUI_Workshop::displayAllResults()
1156 SessionPtr aMgr = ModelAPI_Session::get();
1157 DocumentPtr aRootDoc = aMgr->moduleDocument();
1158 displayDocumentResults(aRootDoc);
1159 for (int i = 0; i < aRootDoc->size(ModelAPI_ResultPart::group()); i++) {
1160 ObjectPtr aObject = aRootDoc->object(ModelAPI_ResultPart::group(), i);
1161 ResultPartPtr aPart = boost::dynamic_pointer_cast<ModelAPI_ResultPart>(aObject);
1162 displayDocumentResults(aPart->partDoc());
1164 myDisplayer->updateViewer();
1167 //**************************************************************
1168 void XGUI_Workshop::displayDocumentResults(DocumentPtr theDoc)
1172 displayGroupResults(theDoc, ModelAPI_ResultConstruction::group());
1173 displayGroupResults(theDoc, ModelAPI_ResultBody::group());
1176 //**************************************************************
1177 void XGUI_Workshop::displayGroupResults(DocumentPtr theDoc, std::string theGroup)
1179 for (int i = 0; i < theDoc->size(theGroup); i++)
1180 myDisplayer->display(theDoc->object(theGroup, i), false);