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 //******************************************************
563 void XGUI_Workshop::onExit()
565 SessionPtr aMgr = ModelAPI_Session::get();
566 if (aMgr->isModified()) {
567 int anAnswer = QMessageBox::question(
568 myMainWindow, tr("Save current file"), tr("The document is modified, save before exit?"),
569 QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel, QMessageBox::Cancel);
570 if (anAnswer == QMessageBox::Save) {
571 bool saved = onSave();
575 } else if (anAnswer == QMessageBox::Cancel) {
582 //******************************************************
583 void XGUI_Workshop::onNew()
585 QApplication::setOverrideCursor(Qt::WaitCursor);
586 if (objectBrowser() == 0) {
588 mySelector->connectViewers();
590 myViewerProxy->connectToViewer();
592 if (!isSalomeMode()) {
593 myMainWindow->showPythonConsole();
594 QMdiSubWindow* aWnd = myMainWindow->viewer()->createView();
595 aWnd->showMaximized();
596 updateCommandStatus();
598 myContextMenuMgr->connectViewer();
599 QApplication::restoreOverrideCursor();
602 //******************************************************
603 void XGUI_Workshop::onOpen()
605 if(!myOperationMgr->abortOperation())
607 //save current file before close if modified
608 SessionPtr aSession = ModelAPI_Session::get();
609 if (aSession->isModified()) {
610 //TODO(sbh): re-launch the app?
611 int anAnswer = QMessageBox::question(
612 myMainWindow, tr("Save current file"),
613 tr("The document is modified, save before opening another?"),
614 QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel, QMessageBox::Cancel);
615 if (anAnswer == QMessageBox::Save) {
617 } else if (anAnswer == QMessageBox::Cancel) {
620 aSession->moduleDocument()->close();
624 //show file dialog, check if readable and open
625 myCurrentDir = QFileDialog::getExistingDirectory(mainWindow());
626 if (myCurrentDir.isEmpty())
628 QFileInfo aFileInfo(myCurrentDir);
629 if (!aFileInfo.exists() || !aFileInfo.isReadable()) {
630 QMessageBox::critical(myMainWindow, tr("Warning"), tr("Unable to open the file."));
634 QApplication::setOverrideCursor(Qt::WaitCursor);
635 aSession->load(myCurrentDir.toLatin1().constData());
636 myObjectBrowser->rebuildDataTree();
638 updateCommandStatus();
639 myMainWindow->setCurrentDir(myCurrentDir);
640 QApplication::restoreOverrideCursor();
643 //******************************************************
644 bool XGUI_Workshop::onSave()
646 if(!myOperationMgr->abortOperation())
648 if (myCurrentDir.isEmpty()) {
651 std::list<std::string> aFiles;
652 saveDocument(myCurrentDir, aFiles);
653 updateCommandStatus();
654 myMainWindow->setModifiedState(false);
658 //******************************************************
659 bool XGUI_Workshop::onSaveAs()
661 if(!myOperationMgr->abortOperation())
663 QFileDialog dialog(mainWindow());
664 dialog.setWindowTitle(tr("Select directory to save files..."));
665 dialog.setFileMode(QFileDialog::Directory);
666 dialog.setFilter(tr("Folders (*)"));
667 dialog.setOptions(QFileDialog::HideNameFilterDetails | QFileDialog::ShowDirsOnly);
668 dialog.setViewMode(QFileDialog::Detail);
670 if (!dialog.exec()) {
673 QString aTempDir = dialog.selectedFiles().first();
675 if (aDir.exists() && !aDir.entryInfoList(QDir::NoDotAndDotDot | QDir::AllEntries).isEmpty()) {
676 int answer = QMessageBox::question(
678 //: Title of the dialog which asks user if he wants to save study in existing non-empty folder
680 tr("The folder already contains some files, save anyway?"),
681 QMessageBox::Save | QMessageBox::Cancel);
682 if (answer == QMessageBox::Cancel) {
686 myCurrentDir = aTempDir;
687 if (!isSalomeMode()) {
688 myMainWindow->setCurrentDir(myCurrentDir, false);
689 myMainWindow->setModifiedState(false);
694 //******************************************************
695 void XGUI_Workshop::onUndo()
697 objectBrowser()->treeView()->setCurrentIndex(QModelIndex());
698 SessionPtr aMgr = ModelAPI_Session::get();
699 if (aMgr->isOperation())
700 operationMgr()->abortOperation();
702 updateCommandStatus();
705 //******************************************************
706 void XGUI_Workshop::onRedo()
708 objectBrowser()->treeView()->setCurrentIndex(QModelIndex());
709 SessionPtr aMgr = ModelAPI_Session::get();
710 if (aMgr->isOperation())
711 operationMgr()->abortOperation();
713 updateCommandStatus();
716 //******************************************************
717 void XGUI_Workshop::onPreferences()
720 XGUI_Preferences::editPreferences(aModif);
721 if (aModif.size() > 0) {
723 foreach (XGUI_Pref aPref, aModif)
725 aSection = aPref.first;
726 if (aSection == XGUI_Preferences::VIEWER_SECTION) {
728 myMainWindow->viewer()->updateFromResources();
729 } else if (aSection == XGUI_Preferences::MENU_SECTION) {
731 myMainWindow->menuObject()->updateFromResources();
737 //******************************************************
738 ModuleBase_IModule* XGUI_Workshop::loadModule(const QString& theModule)
740 QString libName = QString::fromStdString(library(theModule.toStdString()));
741 if (libName.isEmpty()) {
742 qWarning(qPrintable(tr("Information about module \"%1\" doesn't exist.").arg(theModule)));
747 CREATE_FUNC crtInst = 0;
750 HINSTANCE modLib = ::LoadLibrary((LPTSTR) qPrintable(libName));
754 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
755 0, ::GetLastError(), 0, (LPTSTR) & lpMsgBuf, 0, 0);
756 QString aMsg((char*) &lpMsgBuf);
757 err = QString("Failed to load %1. %2").arg(libName).arg(aMsg);
758 ::LocalFree(lpMsgBuf);
760 crtInst = (CREATE_FUNC) ::GetProcAddress(modLib, CREATE_MODULE);
764 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM
765 | FORMAT_MESSAGE_IGNORE_INSERTS,
766 0, ::GetLastError(), 0, (LPTSTR) & lpMsgBuf, 0, 0);
767 QString aMsg((char*) &lpMsgBuf);
768 err = QString("Failed to find %1 function. %2").arg( CREATE_MODULE).arg(aMsg);
769 ::LocalFree(lpMsgBuf);
773 void* modLib = dlopen( libName.toLatin1(), RTLD_LAZY | RTLD_GLOBAL );
775 err = QString( "Can not load library %1. %2" ).arg( libName ).arg( dlerror() );
777 crtInst = (CREATE_FUNC)dlsym( modLib, CREATE_MODULE );
779 err = QString( "Failed to find function %1. %2" ).arg( CREATE_MODULE ).arg( dlerror() );
784 ModuleBase_IModule* aModule = crtInst ? crtInst(this) : 0;
786 if (!err.isEmpty()) {
788 QMessageBox::warning(mainWindow(), tr("Error"), err);
790 qWarning(qPrintable(err));
796 //******************************************************
797 bool XGUI_Workshop::activateModule()
799 Config_ModuleReader aModuleReader;
800 QString moduleName = QString::fromStdString(aModuleReader.getModuleName());
801 myModule = loadModule(moduleName);
804 myModule->createFeatures();
805 myActionsMgr->update();
809 //******************************************************
810 void XGUI_Workshop::updateCommandStatus()
812 QList<QAction*> aCommands;
813 if (isSalomeMode()) { // update commands in SALOME mode
814 aCommands = salomeConnector()->commandList();
816 XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
817 foreach (XGUI_Command* aCmd, aMenuBar->features())
818 aCommands.append(aCmd);
820 SessionPtr aMgr = ModelAPI_Session::get();
821 if (aMgr->hasModuleDocument()) {
824 foreach(QAction* aCmd, aCommands)
826 QString aId = aCmd->data().toString();
827 if (aId == "UNDO_CMD")
829 else if (aId == "REDO_CMD")
832 // Enable all commands
833 aCmd->setEnabled(true);
835 aUndoCmd->setEnabled(aMgr->canUndo());
836 aRedoCmd->setEnabled(aMgr->canRedo());
838 foreach(QAction* aCmd, aCommands)
840 QString aId = aCmd->data().toString();
841 if (aId == "NEW_CMD")
842 aCmd->setEnabled(true);
843 else if (aId == "EXIT_CMD")
844 aCmd->setEnabled(true);
846 aCmd->setEnabled(false);
849 myActionsMgr->update();
852 //******************************************************
853 QList<QAction*> XGUI_Workshop::getModuleCommands() const
855 QList<QAction*> aCommands;
856 if (isSalomeMode()) { // update commands in SALOME mode
857 aCommands = salomeConnector()->commandList();
859 XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
860 foreach(XGUI_Command* aCmd, aMenuBar->features())
862 aCommands.append(aCmd);
868 //******************************************************
869 QDockWidget* XGUI_Workshop::createObjectBrowser(QWidget* theParent)
871 QDockWidget* aObjDock = new QDockWidget(theParent);
872 aObjDock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea | Qt::BottomDockWidgetArea);
873 aObjDock->setWindowTitle(tr("Object browser"));
874 aObjDock->setStyleSheet(
875 "::title { position: relative; padding-left: 5px; text-align: left center }");
876 myObjectBrowser = new XGUI_ObjectsBrowser(aObjDock);
877 connect(myObjectBrowser, SIGNAL(activePartChanged(ObjectPtr)), this,
878 SLOT(changeCurrentDocument(ObjectPtr)));
879 aObjDock->setWidget(myObjectBrowser);
881 myContextMenuMgr->connectObjectBrowser();
885 //******************************************************
887 * Creates dock widgets, places them in corresponding area
888 * and tabifies if necessary.
890 void XGUI_Workshop::createDockWidgets()
892 QMainWindow* aDesktop = isSalomeMode() ? salomeConnector()->desktop() : myMainWindow;
893 QDockWidget* aObjDock = createObjectBrowser(aDesktop);
894 aDesktop->addDockWidget(Qt::LeftDockWidgetArea, aObjDock);
895 myPropertyPanel = new XGUI_PropertyPanel(aDesktop);
896 myPropertyPanel->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea | Qt::BottomDockWidgetArea);
897 aDesktop->addDockWidget(Qt::LeftDockWidgetArea, myPropertyPanel);
898 hidePropertyPanel(); //<! Invisible by default
900 aDesktop->tabifyDockWidget(aObjDock, myPropertyPanel);
902 QPushButton* aOkBtn = myPropertyPanel->findChild<QPushButton*>(XGUI::PROP_PANEL_OK);
903 connect(aOkBtn, SIGNAL(clicked()), myOperationMgr, SLOT(onCommitOperation()));
904 QPushButton* aCancelBtn = myPropertyPanel->findChild<QPushButton*>(XGUI::PROP_PANEL_CANCEL);
905 connect(aCancelBtn, SIGNAL(clicked()), myOperationMgr, SLOT(onAbortOperation()));
906 //TODO(sbh): KeyReleasedProblem
907 connect(myPropertyPanel, SIGNAL(keyReleased(QKeyEvent*)), myOperationMgr,
908 SLOT(onKeyReleased(QKeyEvent*)));
910 connect(myPropertyPanel, SIGNAL(widgetActivated(ModuleBase_ModelWidget*)), myOperationMgr,
911 SLOT(onWidgetActivated(ModuleBase_ModelWidget*)));
912 connect(myOperationMgr, SIGNAL(activateNextWidget(ModuleBase_ModelWidget*)), myPropertyPanel,
913 SLOT(onActivateNextWidget(ModuleBase_ModelWidget*)));
914 connect(myOperationMgr, SIGNAL(operationValidated(bool)), myPropertyPanel,
915 SLOT(setAcceptEnabled(bool)));
919 //******************************************************
920 void XGUI_Workshop::showPropertyPanel()
922 QAction* aViewAct = myPropertyPanel->toggleViewAction();
923 //<! Restore ability to close panel from the window's menu
924 aViewAct->setEnabled(true);
925 myPropertyPanel->show();
926 myPropertyPanel->raise();
929 //******************************************************
930 void XGUI_Workshop::hidePropertyPanel()
932 QAction* aViewAct = myPropertyPanel->toggleViewAction();
933 //<! Do not allow to show empty property panel
934 aViewAct->setEnabled(false);
935 myPropertyPanel->hide();
938 //******************************************************
939 void XGUI_Workshop::showObjectBrowser()
941 myObjectBrowser->parentWidget()->show();
944 //******************************************************
945 void XGUI_Workshop::hideObjectBrowser()
947 myObjectBrowser->parentWidget()->hide();
950 //******************************************************
951 void XGUI_Workshop::onFeatureTriggered()
953 QAction* aCmd = dynamic_cast<QAction*>(sender());
955 QString aId = salomeConnector()->commandId(aCmd);
957 myModule->launchOperation(aId);
961 //******************************************************
962 void XGUI_Workshop::changeCurrentDocument(ObjectPtr theObj)
964 SessionPtr aMgr = ModelAPI_Session::get();
966 ResultPartPtr aPart = boost::dynamic_pointer_cast<ModelAPI_ResultPart>(theObj);
968 DocumentPtr aPartDoc = aPart->partDoc();
970 aMgr->setActiveDocument(aPartDoc);
975 aMgr->setActiveDocument(aMgr->moduleDocument());
978 //******************************************************
979 void XGUI_Workshop::salomeViewerSelectionChanged()
981 emit salomeViewerSelection();
984 //**************************************************************
985 XGUI_SalomeViewer* XGUI_Workshop::salomeViewer() const
987 return mySalomeConnector->viewer();
990 //**************************************************************
991 void XGUI_Workshop::onContextMenuCommand(const QString& theId, bool isChecked)
993 QList<ObjectPtr> aObjects = mySelector->selection()->selectedObjects();
994 if ((theId == "ACTIVATE_PART_CMD") && (aObjects.size() > 0)) {
995 ResultPartPtr aPart = boost::dynamic_pointer_cast<ModelAPI_ResultPart>(aObjects.first());
997 } else if (theId == "DEACTIVATE_PART_CMD")
998 activatePart(ResultPartPtr());
999 else if (theId == "DELETE_CMD")
1000 deleteObjects(aObjects);
1001 else if (theId == "SHOW_CMD")
1002 showObjects(aObjects, true);
1003 else if (theId == "HIDE_CMD")
1004 showObjects(aObjects, false);
1005 else if (theId == "SHOW_ONLY_CMD")
1006 showOnlyObjects(aObjects);
1009 //**************************************************************
1010 void XGUI_Workshop::onWidgetValuesChanged()
1012 ModuleBase_Operation* anOperation = myOperationMgr->currentOperation();
1013 FeaturePtr aFeature = anOperation->feature();
1015 ModuleBase_ModelWidget* aSenderWidget = dynamic_cast<ModuleBase_ModelWidget*>(sender());
1017 // aCustom->storeValue(aFeature);
1019 const QList<ModuleBase_ModelWidget*>& aWidgets = myPropertyPanel->modelWidgets();
1020 QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
1021 for (; anIt != aLast; anIt++) {
1022 ModuleBase_ModelWidget* aCustom = *anIt;
1023 if (aCustom && (/*!aCustom->isInitialized(aFeature) ||*/aCustom == aSenderWidget)) {
1024 //aCustom->storeValue(aFeature);
1025 aCustom->storeValue();
1030 //**************************************************************
1031 void XGUI_Workshop::activatePart(ResultPartPtr theFeature)
1033 if (!myPartActivating) {
1034 myPartActivating = true;
1036 theFeature->activate();
1037 changeCurrentDocument(theFeature);
1038 myObjectBrowser->activatePart(theFeature);
1039 myPartActivating = false;
1043 //**************************************************************
1044 void XGUI_Workshop::activateLastPart()
1046 SessionPtr aMgr = ModelAPI_Session::get();
1047 DocumentPtr aDoc = aMgr->moduleDocument();
1048 std::string aGrpName = ModelAPI_ResultPart::group();
1049 ObjectPtr aLastPart = aDoc->object(aGrpName, aDoc->size(aGrpName) - 1);
1050 ResultPartPtr aPart = boost::dynamic_pointer_cast<ModelAPI_ResultPart>(aLastPart);
1052 activatePart(aPart);
1055 //**************************************************************
1056 void XGUI_Workshop::deleteObjects(const QList<ObjectPtr>& theList)
1058 QMainWindow* aDesktop = isSalomeMode() ? salomeConnector()->desktop() : myMainWindow;
1059 QMessageBox::StandardButton aRes = QMessageBox::warning(
1060 aDesktop, tr("Delete features"), tr("Seleted features will be deleted. Continue?"),
1061 QMessageBox::No | QMessageBox::Yes, QMessageBox::No);
1062 // ToDo: definbe deleting method
1063 if (aRes == QMessageBox::Yes) {
1064 SessionPtr aMgr = ModelAPI_Session::get();
1065 aMgr->startOperation();
1066 foreach (ObjectPtr aObj, theList)
1068 ResultPartPtr aPart = boost::dynamic_pointer_cast<ModelAPI_ResultPart>(aObj);
1070 DocumentPtr aDoc = aPart->document();
1071 if (aDoc == aMgr->activeDocument()) {
1074 //aMgr->moduleDocument()->removeFeature(aPart->owner());
1076 FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
1078 aObj->document()->removeFeature(aFeature);
1081 myDisplayer->updateViewer();
1082 aMgr->finishOperation();
1086 //**************************************************************
1087 void XGUI_Workshop::showObjects(const QList<ObjectPtr>& theList, bool isVisible)
1089 foreach (ObjectPtr aObj, theList)
1091 ResultPtr aRes = boost::dynamic_pointer_cast<ModelAPI_Result>(aObj);
1094 myDisplayer->display(aRes, false);
1096 myDisplayer->erase(aRes, false);
1100 myDisplayer->updateViewer();
1103 //**************************************************************
1104 void XGUI_Workshop::showOnlyObjects(const QList<ObjectPtr>& theList)
1106 myDisplayer->eraseAll(false);
1107 showObjects(theList, true);
1111 //**************************************************************
1112 void XGUI_Workshop::updateCommandsOnViewSelection()
1114 SessionPtr aMgr = ModelAPI_Session::get();
1115 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
1116 XGUI_Selection* aSelection = mySelector->selection();
1117 if (aSelection->getSelected().size() == 0)
1120 QList<QAction*> aActions = getModuleCommands();
1121 foreach(QAction* aAction, aActions)
1123 QString aId = aAction->data().toString();
1124 std::list<ModelAPI_Validator*> aValidators;
1125 std::list<std::list<std::string> > anArguments;
1126 aFactory->validators(aId.toStdString(), aValidators, anArguments);
1127 std::list<ModelAPI_Validator*>::iterator aValidator = aValidators.begin();
1128 for (; aValidator != aValidators.end(); aValidator++) {
1130 const ModuleBase_SelectionValidator* aSelValidator =
1131 dynamic_cast<const ModuleBase_SelectionValidator*>(*aValidator);
1132 if (aSelValidator) {
1133 aAction->setEnabled(aSelValidator->isValid(aSelection));
1140 //**************************************************************
1141 void XGUI_Workshop::registerValidators() const
1143 SessionPtr aMgr = ModelAPI_Session::get();
1144 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
1147 //**************************************************************
1148 void XGUI_Workshop::displayAllResults()
1150 SessionPtr aMgr = ModelAPI_Session::get();
1151 DocumentPtr aRootDoc = aMgr->moduleDocument();
1152 displayDocumentResults(aRootDoc);
1153 for (int i = 0; i < aRootDoc->size(ModelAPI_ResultPart::group()); i++) {
1154 ObjectPtr aObject = aRootDoc->object(ModelAPI_ResultPart::group(), i);
1155 ResultPartPtr aPart = boost::dynamic_pointer_cast<ModelAPI_ResultPart>(aObject);
1156 displayDocumentResults(aPart->partDoc());
1158 myDisplayer->updateViewer();
1161 //**************************************************************
1162 void XGUI_Workshop::displayDocumentResults(DocumentPtr theDoc)
1166 displayGroupResults(theDoc, ModelAPI_ResultConstruction::group());
1167 displayGroupResults(theDoc, ModelAPI_ResultBody::group());
1170 //**************************************************************
1171 void XGUI_Workshop::displayGroupResults(DocumentPtr theDoc, std::string theGroup)
1173 for (int i = 0; i < theDoc->size(theGroup); i++)
1174 myDisplayer->display(theDoc->object(theGroup, i), false);