From 1e63ae393cf1ce1923329c90aa66fa33f957f268 Mon Sep 17 00:00:00 2001 From: nds Date: Tue, 7 Nov 2017 15:03:56 +0300 Subject: [PATCH] Key_Escape processing in File selector widget. Otherwise, crash happens if file dialog is opened. --- .../ModuleBase_WidgetFileSelector.cpp | 40 ++++++++++++++----- .../ModuleBase_WidgetFileSelector.h | 9 ++++- 2 files changed, 36 insertions(+), 13 deletions(-) diff --git a/src/ModuleBase/ModuleBase_WidgetFileSelector.cpp b/src/ModuleBase/ModuleBase_WidgetFileSelector.cpp index c7e1c0df9..ae84af37e 100644 --- a/src/ModuleBase/ModuleBase_WidgetFileSelector.cpp +++ b/src/ModuleBase/ModuleBase_WidgetFileSelector.cpp @@ -44,7 +44,7 @@ ModuleBase_WidgetFileSelector::ModuleBase_WidgetFileSelector(QWidget* theParent, const Config_WidgetAPI* theData) -: ModuleBase_ModelWidget(theParent, theData) +: ModuleBase_ModelWidget(theParent, theData), myFileDialog(0) { myTitle = translate(theData->getProperty("title")); myType = (theData->getProperty("type") == "save") ? WFS_SAVE : WFS_OPEN; @@ -123,6 +123,16 @@ bool ModuleBase_WidgetFileSelector::isCurrentPathValid() return aFile.exists(); } +bool ModuleBase_WidgetFileSelector::processEscape() +{ + if (myFileDialog) { + myFileDialog->reject(); + return true; + } + return ModuleBase_ModelWidget::processEscape(); +} + + void ModuleBase_WidgetFileSelector::onPathSelectionBtn() { QString aDefaultPath = myPathField->text().isEmpty() @@ -131,17 +141,25 @@ void ModuleBase_WidgetFileSelector::onPathSelectionBtn() QString aFilter = filterString(); // use Option prohibited native dialog using to have both lower/upper extensions of files // satisfied to dialog filter on Linux(Calibre) Issue #2055 - QString aFileName = (myType == WFS_SAVE) - ? QFileDialog::getSaveFileName(this, myTitle, aDefaultPath, aFilter, &mySelectedFilter, - QFileDialog::DontUseNativeDialog) - : QFileDialog::getOpenFileName(this, myTitle, aDefaultPath, aFilter, &mySelectedFilter, - QFileDialog::DontUseNativeDialog); - if (!aFileName.isEmpty()) { - if (myType == WFS_SAVE) - aFileName = applyExtension(aFileName, mySelectedFilter); - myPathField->setText(aFileName); - emit focusOutWidget(this); + myFileDialog = new QFileDialog(this, myTitle, aDefaultPath, aFilter); + myFileDialog->setNameFilter(aFilter); + myFileDialog->setOptions(QFileDialog::DontUseNativeDialog); + myFileDialog->setAcceptMode(myType == WFS_SAVE ? QFileDialog::AcceptSave : QFileDialog::AcceptOpen); + if (myFileDialog->exec() == QDialog::Accepted) + { + mySelectedFilter = myFileDialog->selectedNameFilter(); + QStringList aFileNames = myFileDialog->selectedFiles(); + if (!aFileNames.empty()) { + QString aFileName = aFileNames.first(); + if (!aFileName.isEmpty()) { + if (myType == WFS_SAVE) + aFileName = applyExtension(aFileName, mySelectedFilter); + myPathField->setText(aFileName); + emit focusOutWidget(this); + } + } } + myFileDialog = 0; } void ModuleBase_WidgetFileSelector::onPathChanged() diff --git a/src/ModuleBase/ModuleBase_WidgetFileSelector.h b/src/ModuleBase/ModuleBase_WidgetFileSelector.h index 8cfbe91ff..be59f13b4 100644 --- a/src/ModuleBase/ModuleBase_WidgetFileSelector.h +++ b/src/ModuleBase/ModuleBase_WidgetFileSelector.h @@ -28,8 +28,9 @@ #include #include -class QWidget; +class QFileDialog; class QLineEdit; +class QWidget; /** * \ingroup GUI @@ -65,7 +66,10 @@ class MODULEBASE_EXPORT ModuleBase_WidgetFileSelector : public ModuleBase_ModelW /// exists and has supported format bool isCurrentPathValid(); - public slots: + /// Reject the current editor dialog if it is shown and returns true. + virtual bool processEscape(); + +public slots: /// Processing of path selection button press void onPathSelectionBtn(); @@ -104,6 +108,7 @@ protected: protected: /// A control for path input QLineEdit* myPathField; + QFileDialog* myFileDialog; ///< started dialog /// A title of open file dialog box QString myTitle; -- 2.39.2