ctkFileDialog.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*=========================================================================
  2. Library: CTK
  3. Copyright (c) Kitware Inc.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0.txt
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. =========================================================================*/
  14. #ifndef __ctkFileDialog_h
  15. #define __ctkFileDialog_h
  16. // Qt includes
  17. #include <QAbstractItemView>
  18. #include <QFileDialog>
  19. // CTK includes
  20. #include <ctkPimpl.h>
  21. #include "ctkWidgetsExport.h"
  22. class ctkFileDialogPrivate;
  23. /// \ingroup Widgets
  24. /// Customizable QFileDialog.
  25. /// An extra widget can be added at the bottom of the dialog
  26. /// under the file format combobox. The Accept button is also controllable
  27. /// using setAcceptButtonEnable().
  28. /// The behavior of the "return" key is the following:
  29. /// - it selects the directory written in the line edit or it
  30. /// - it accepts the dialog if the directory is already selected.
  31. class CTK_WIDGETS_EXPORT ctkFileDialog : public QFileDialog
  32. {
  33. Q_OBJECT
  34. Q_PROPERTY(QAbstractItemView::SelectionMode SelectionMode READ selectionMode WRITE setSelectionMode)
  35. public:
  36. // Superclass typedef
  37. typedef QFileDialog Superclass;
  38. /// Constructor
  39. /// By default, behaves like a QFileDialog
  40. /// \sa QFileDialog()
  41. explicit ctkFileDialog(QWidget *parent = 0,
  42. const QString &caption = QString(),
  43. const QString &directory = QString(),
  44. const QString &filter = QString());
  45. virtual ~ctkFileDialog();
  46. /// Add an extra widget under the file format combobox. If a label is
  47. /// given, it will appear in the first column.
  48. /// The widget is reparented to ctkFileDialog
  49. Q_INVOKABLE void setBottomWidget(QWidget* widget, const QString& label=QString());
  50. /// Return the extra widget if any
  51. Q_INVOKABLE QWidget* bottomWidget()const;
  52. /// Set the selection mode the views operate in.
  53. ///
  54. /// \warning The selection mode must explicitly be set each time
  55. /// QFileDialog::setFileMode(FileMode mode) is invoked. This is required
  56. /// because the QFileDialog::setFileMode(FileMode mode) method is not virtual
  57. /// and it internally resets the selection mode.
  58. ///
  59. /// \sa clearSelection()
  60. void setSelectionMode(QAbstractItemView::SelectionMode mode);
  61. /// Get the selection mode of the views.
  62. ///
  63. /// \sa setSelectionMode(QAbstractItemView::SelectionMode)
  64. QAbstractItemView::SelectionMode selectionMode() const;
  65. /// Internally used
  66. bool eventFilter(QObject *obj, QEvent *event);
  67. public Q_SLOTS:
  68. /// Can be used to prevent the accept button to be enabled. It's typically
  69. /// a slot that can be connected to assure that the user doesn't accept the
  70. /// dialog if a value is not set in the extra bottom widget.
  71. void setAcceptButtonEnable(bool enable);
  72. /// Deselect all selected directories or files.
  73. void clearSelection();
  74. Q_SIGNALS:
  75. /// Signals QFileDialog::file[s]Selected() are fired only when the Ok button
  76. /// is pressed, fileSelectionChanged(QStringList) is emitted when the
  77. /// selection is changed, not just when the dialog is accepted.
  78. void fileSelectionChanged(const QStringList& selected);
  79. protected Q_SLOTS:
  80. void onSelectionChanged();
  81. protected:
  82. QScopedPointer<ctkFileDialogPrivate> d_ptr;
  83. /// Reimplemented to override the return key behavior
  84. virtual void accept();
  85. private:
  86. Q_DECLARE_PRIVATE(ctkFileDialog);
  87. Q_DISABLE_COPY(ctkFileDialog);
  88. };
  89. #endif