QGCQFileDialog.h 6.87 KB
Newer Older
1 2 3 4 5 6 7 8 9
/****************************************************************************
 *
 *   (c) 2009-2016 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
 *
 * QGroundControl is licensed according to the terms in the file
 * COPYING.md in the root of the source code directory.
 *
 ****************************************************************************/

Don Gagne's avatar
Don Gagne committed
10 11 12 13

#ifndef QGCFILEDIALOG_H
#define QGCFILEDIALOG_H

14 15 16 17
#ifdef __mobile__
#error Should not be included in mobile builds
#endif

Gus Grubba's avatar
Gus Grubba committed
18
#include <QCoreApplication>
Don Gagne's avatar
Don Gagne committed
19 20 21
#include <QFileDialog>

/// @file
22
///     @brief Subclass of <a href="http://qt-project.org/doc/qt-5/qfiledialog.html">QFileDialog</a>
Don Gagne's avatar
Don Gagne committed
23 24
///     @author Don Gagne <don@thegagnes.com>

25
/*!
26 27 28 29 30 31
    Subclass of <a href="http://qt-project.org/doc/qt-5/qfiledialog.html">QFileDialog</a> which re-implements the static public functions. The reason for this
    is that the <a href="http://qt-project.org/doc/qt-5/qfiledialog.html">QFileDialog</a> implementations of these use the native os dialogs. On OSX these
    these can intermittently hang. So instead here we use the native dialogs. It also allows
    use to catch these dialogs for unit testing.
    @remark If you need to know what type of file was returned by these functions, you can use something like:
    @code{.cpp}
32
    QString filename = QGCQFileDialog::getSaveFileName(this, tr("Save File"), "~/", "Foo files (*.foo);;All Files (*.*)", "foo");
33 34 35 36 37 38 39 40
    if (!filename.isEmpty()) {
        QFileInfo fi(filename);
        QString fileExtension(fi.suffix());
        if (fileExtension == QString("foo")) {
            // do something
        }
    }
    @endcode
41 42
*/

43
class QGCQFileDialog : public QFileDialog {
Gus Grubba's avatar
Gus Grubba committed
44
    Q_DECLARE_TR_FUNCTIONS(QGCQFileDialog)
Don Gagne's avatar
Don Gagne committed
45
public:
46 47 48

    //! Static helper that will return an existing directory selected by the user.
    /*!
dogmaphobic's avatar
dogmaphobic committed
49 50 51 52 53 54
        @param[in] parent The parent QWidget.
        @param[in] caption The caption displayed at the top of the dialog.
        @param[in] dir The initial directory shown to the user.
        @param[in] options Set the various options that affect the look and feel of the dialog.
        @return The chosen path or \c QString("") if none.
        @sa <a href="http://qt-project.org/doc/qt-5/qfiledialog.html#getExistingDirectory">QFileDialog::getExistingDirectory()</a>
55
    */
56 57 58 59 60
    static QString getExistingDirectory(
        QWidget* parent = 0,
        const QString& caption = QString(),
        const QString& dir = QString(),
        Options options = ShowDirsOnly);
Don Gagne's avatar
Don Gagne committed
61
    
62 63
    //! Static helper that invokes a File Open dialog where the user can select a file to be opened.
    /*!
dogmaphobic's avatar
dogmaphobic committed
64 65 66 67 68 69 70
        @param[in] parent The parent QWidget.
        @param[in] caption The caption displayed at the top of the dialog.
        @param[in] dir The initial directory shown to the user.
        @param[in] filter The filter used for selecting the file type.
        @param[in] options Set the various options that affect the look and feel of the dialog.
        @return The full path and filename to be opened or \c QString("") if none.
        @sa <a href="http://qt-project.org/doc/qt-5/qfiledialog.html#getOpenFileName">QFileDialog::getOpenFileName()</a>
71
    */
72 73 74 75 76 77
    static QString getOpenFileName(
        QWidget* parent = 0,
        const QString& caption = QString(),
        const QString& dir = QString(),
        const QString& filter = QString(),
        Options options = 0);
Don Gagne's avatar
Don Gagne committed
78
    
79 80
    //! Static helper that invokes a File Open dialog where the user can select one or more files to be opened.
    /*!
dogmaphobic's avatar
dogmaphobic committed
81 82 83 84 85 86 87
        @param[in] parent The parent QWidget.
        @param[in] caption The caption displayed at the top of the dialog.
        @param[in] dir The initial directory shown to the user.
        @param[in] filter The filter used for selecting the file type.
        @param[in] options Set the various options that affect the look and feel of the dialog.
        @return A <a href="http://qt-project.org/doc/qt-5/qstringlist.html">QStringList</a> object containing zero or more files to be opened.
        @sa <a href="http://qt-project.org/doc/qt-5/qfiledialog.html#getOpenFileNames">QFileDialog::getOpenFileNames()</a>
88
    */
89 90 91 92 93 94
    static QStringList getOpenFileNames(
        QWidget* parent = 0,
        const QString& caption = QString(),
        const QString& dir = QString(),
        const QString& filter = QString(),
        Options options = 0);
Don Gagne's avatar
Don Gagne committed
95
    
96 97
    //! Static helper that invokes a File Save dialog where the user can select a directory and enter a filename to be saved.
    /*!
dogmaphobic's avatar
dogmaphobic committed
98 99 100 101 102 103 104 105 106 107 108 109 110 111
        @param[in] parent The parent QWidget.
        @param[in] caption The caption displayed at the top of the dialog.
        @param[in] dir The initial directory shown to the user.
        @param[in] filter The filter used for selecting the file type.
        @param[in] defaultSuffix Specifies a string that will be added to the filename if it has no suffix already. The suffix is typically used to indicate the file type (e.g. "txt" indicates a text file).
        @param[in] strict Makes the default suffix mandatory. Only files with those extensions will be allowed.
        @param[in] options Set the various options that affect the look and feel of the dialog.
        @return The full path and filename to be used to save the file or \c QString("") if none.
        @sa <a href="http://qt-project.org/doc/qt-5/qfiledialog.html#getSaveFileName">QFileDialog::getSaveFileName()</a>
        @remark If a default suffix is given, it will be appended to the filename if the user does not enter one themselves. That is, if the user simply enters \e foo and the default suffix is set to \e bar,
        the returned filename will be \e foo.bar. However, if the user specifies a suffix, the \e strict flag will determine what is done. If the user enters \e foo.txt and \e strict is false, the function
        returns \e foo.txt (no suffix enforced). If \e strict is true however, the default suffix is appended no matter what. In the case above, the function will return \e foo.txt.bar (suffix enforced).
        @remark If \e strict is set and the file name given by the user is renamed (the \e foo.txt.bar example above), the function will check and see if the file already exists. If that's the case, it will
        ask the user if they want to overwrite it.
112
    */
113 114 115 116 117
    static QString getSaveFileName(
        QWidget* parent = 0,
        const QString& caption = QString(),
        const QString& dir = QString(),
        const QString& filter = QString(),
118
        const QString& defaultSuffix = QString(),
119
        bool strict = false,
120 121
        Options options = 0);

122
private slots:
123
    /// @brief The exec slot is private because we only want QGCQFileDialog users to use the static methods. Otherwise it will break
124
    ///        unit testing.
Don Gagne's avatar
Don Gagne committed
125
    int exec(void) { return QFileDialog::exec(); }
126 127
    
private:
128
    static void    _validate(Options& options);
129 130
    static bool    _validateExtension(const QString& filter, const QString& extension);
    static QString _getFirstExtensionInFilter(const QString& filter);
Don Gagne's avatar
Don Gagne committed
131 132 133 134
};


#endif