]> SALOME platform Git repositories - modules/shaper.git/commitdiff
Salome HOME
Key_Escape processing in File selector widget. Otherwise, crash happens if file dialo...
authornds <nds@opencascade.com>
Tue, 7 Nov 2017 12:03:56 +0000 (15:03 +0300)
committernds <nds@opencascade.com>
Tue, 7 Nov 2017 12:03:56 +0000 (15:03 +0300)
src/ModuleBase/ModuleBase_WidgetFileSelector.cpp
src/ModuleBase/ModuleBase_WidgetFileSelector.h

index c7e1c0df92794d744a46abb995fcd6ad9cd41465..ae84af37e4f03a1397e91afc4252abe7541f3dbe 100644 (file)
@@ -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()
index 8cfbe91ff2769acf45e2a47dadfda0c8805a7936..be59f13b45627748a4164123f81c4f8885498641 100644 (file)
@@ -28,8 +28,9 @@
 #include <QString>
 #include <QStringList>
 
-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;