QGCFileDialog.h 6.84 KB
Newer Older
Don Gagne's avatar
Don Gagne committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
/*=====================================================================
 
 QGroundControl Open Source Ground Control Station
 
 (c) 2009 - 2014 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
 
 This file is part of the QGROUNDCONTROL project
 
 QGROUNDCONTROL is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.
 
 QGROUNDCONTROL is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with QGROUNDCONTROL. If not, see <http://www.gnu.org/licenses/>.
 
 ======================================================================*/

#ifndef QGCFILEDIALOG_H
#define QGCFILEDIALOG_H

#include <QFileDialog>

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

33
/*!
34 35
     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
36 37 38 39
     these can intermittently hang. So instead here we use the native dialogs. It also allows
     use to catch these dialogs for unit testing.
*/

Don Gagne's avatar
Don Gagne committed
40 41 42
class QGCFileDialog : public QFileDialog {
    
public:
43 44 45

    //! Static helper that will return an existing directory selected by the user.
    /*!
46 47 48 49 50
      @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.
51
      @sa <a href="http://qt-project.org/doc/qt-5/qfiledialog.html#getExistingDirectory">QFileDialog::getExistingDirectory()</a>
52
    */
53 54 55 56 57
    static QString getExistingDirectory(
        QWidget* parent = 0,
        const QString& caption = QString(),
        const QString& dir = QString(),
        Options options = ShowDirsOnly);
Don Gagne's avatar
Don Gagne committed
58
    
59 60
    //! Static helper that invokes a File Open dialog where the user can select a file to be opened.
    /*!
61 62 63 64 65 66 67
      @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[out] selectedFilter **NOT IMPLEMENTED - Set to NULL** Returns the filter that the user selected in the file dialog.
      @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.
68
      @sa <a href="http://qt-project.org/doc/qt-5/qfiledialog.html#getOpenFileName">QFileDialog::getOpenFileName()</a>
69
    */
70 71 72 73 74 75 76
    static QString getOpenFileName(
        QWidget* parent = 0,
        const QString& caption = QString(),
        const QString& dir = QString(),
        const QString& filter = QString(),
        QString* selectedFilter = 0,
        Options options = 0);
Don Gagne's avatar
Don Gagne committed
77
    
78 79
    //! Static helper that invokes a File Open dialog where the user can select one or more files to be opened.
    /*!
80 81 82 83 84 85
      @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[out] selectedFilter **NOT IMPLEMENTED - Set to NULL** Returns the filter that the user selected in the file dialog.
      @param[in] options Set the various options that affect the look and feel of the dialog.
86 87
      @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 95
    static QStringList getOpenFileNames(
        QWidget* parent = 0,
        const QString& caption = QString(),
        const QString& dir = QString(),
        const QString& filter = QString(),
        QString* selectedFilter = 0,
        Options options = 0);
Don Gagne's avatar
Don Gagne committed
96
    
97 98
    //! Static helper that invokes a File Save dialog where the user can select a directory and enter a filename to be saved.
    /*!
99 100 101 102 103 104 105
      @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[out] selectedFilter **NOT IMPLEMENTED - Set to NULL** Returns the filter that the user selected in the file dialog.
      @param[in] options Set the various options that affect the look and feel of the dialog.
      @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).
106
      @param[in] strict Makes the default suffix mandatory. Only files with those extensions will be allowed.
107
      @return The full path and filename to be used to save the file or \c QString("") if none.
108
      @sa <a href="http://qt-project.org/doc/qt-5/qfiledialog.html#getSaveFileName">QFileDialog::getSaveFileName()</a>
109 110
      @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, none will be added. That is, if the user enters \e foo.txt, that's what you will receive in return.
111
    */
112 113 114 115 116 117 118 119 120
    static QString getSaveFileName(
        QWidget* parent = 0,
        const QString& caption = QString(),
        const QString& dir = QString(),
        const QString& filter = QString(),
        QString* selectedFilter = 0,
        Options options = 0,
        QString* defaultSuffix = 0,
        bool strict = false);
121 122 123 124 125 126 127
    
private slots:
    /// @brief The exec slot is private becasue when only want QGCFileDialog users to use the static methods. Otherwise it will break
    ///         unit testing.
    int exec(void) { return QGCFileDialog::exec(); }
    
private:
128 129 130
    static void    _validate(QString* selectedFilter, Options& options);
    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