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