Explorar o código

Merge branch '281-qfiledialog-return-key'

* 281-qfiledialog-return-key:
  Tweak ctkFileDialog to better handle return key
Julien Finet %!s(int64=12) %!d(string=hai) anos
pai
achega
4276efebf2
Modificáronse 2 ficheiros con 25 adicións e 1 borrados
  1. 17 0
      Libs/Widgets/ctkFileDialog.cpp
  2. 8 1
      Libs/Widgets/ctkFileDialog.h

+ 17 - 0
Libs/Widgets/ctkFileDialog.cpp

@@ -24,6 +24,7 @@
 #include <QEvent>
 #include <QGridLayout>
 #include <QLabel>
+#include <QLineEdit>
 #include <QListView>
 #include <QPushButton>
 
@@ -187,3 +188,19 @@ void ctkFileDialog::onSelectionChanged()
 {
   emit this->fileSelectionChanged(this->selectedFiles());
 }
+
+//------------------------------------------------------------------------------
+void ctkFileDialog::accept()
+{
+  QLineEdit* fileNameEdit = qobject_cast<QLineEdit*>(this->sender());
+  if (fileNameEdit)
+    {
+    QFileInfo info(fileNameEdit->text());
+    if (info.isDir())
+      {
+      setDirectory(info.absoluteFilePath());
+      return;
+      }
+    }
+  this->Superclass::accept();
+}

+ 8 - 1
Libs/Widgets/ctkFileDialog.h

@@ -34,7 +34,10 @@ class ctkFileDialogPrivate;
 /// Customizable QFileDialog.
 /// An extra widget can be added at the bottom of the dialog
 /// under the file format combobox. The Accept button is also controllable
-/// using setAcceptButtonEnable()
+/// using setAcceptButtonEnable().
+/// The behavior of the "return" key is the following:
+///  - it selects the directory written in the line edit or it
+///  - it accepts the dialog if the directory is already selected.
 class CTK_WIDGETS_EXPORT ctkFileDialog : public QFileDialog
 {
   Q_OBJECT
@@ -61,6 +64,7 @@ public:
 
   /// Internally used
   bool eventFilter(QObject *obj, QEvent *event);
+
 public Q_SLOTS:
   /// Can be used to prevent the accept button to be enabled. It's typically
   /// a slot that can be connected to assure that the user doesn't accept the
@@ -79,6 +83,9 @@ protected Q_SLOTS:
 protected:
   QScopedPointer<ctkFileDialogPrivate> d_ptr;
 
+  /// Reimplemented to override the return key behavior
+  virtual void accept();
+
 private:
   Q_DECLARE_PRIVATE(ctkFileDialog);
   Q_DISABLE_COPY(ctkFileDialog);