QGCFileDialog.cc 7.69 KB
Newer Older
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
/*=====================================================================
 
 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/>.
 
 ======================================================================*/

#include "QGCFileDialog.h"
#include "QGCApplication.h"
26
#include "MainWindow.h"
27
#ifdef QT_DEBUG
dogmaphobic's avatar
dogmaphobic committed
28
#ifndef __mobile__
29 30
#include "UnitTest.h"
#endif
dogmaphobic's avatar
dogmaphobic committed
31
#endif
32

Don Gagne's avatar
Don Gagne committed
33 34
#include <QRegularExpression>
#include <QMessageBox>
35
#include <QPushButton>
Don Gagne's avatar
Don Gagne committed
36

37 38 39 40 41
QString QGCFileDialog::getExistingDirectory(
    QWidget* parent,
    const QString& caption,
    const QString& dir,
    Options options)
42
{
43
    _validate(options);
44 45
    
#ifdef QT_DEBUG
dogmaphobic's avatar
dogmaphobic committed
46
#ifndef __mobile__
47 48 49
    if (qgcApp()->runningUnitTests()) {
        return UnitTest::_getExistingDirectory(parent, caption, dir, options);
    } else
dogmaphobic's avatar
dogmaphobic committed
50
#endif
51 52 53 54 55 56
#endif
    {
        return QFileDialog::getExistingDirectory(parent, caption, dir, options);
    }
}

57 58 59 60 61 62
QString QGCFileDialog::getOpenFileName(
    QWidget* parent,
    const QString& caption,
    const QString& dir,
    const QString& filter,
    Options options)
63
{
64
    _validate(options);
65 66
    
#ifdef QT_DEBUG
dogmaphobic's avatar
dogmaphobic committed
67
#ifndef __mobile__
68
    if (qgcApp()->runningUnitTests()) {
69
        return UnitTest::_getOpenFileName(parent, caption, dir, filter, options);
70
    } else
dogmaphobic's avatar
dogmaphobic committed
71
#endif
72 73
#endif
    {
74
        return QFileDialog::getOpenFileName(parent, caption, dir, filter, NULL, options);
75 76 77
    }
}

78 79 80 81 82 83
QStringList QGCFileDialog::getOpenFileNames(
    QWidget* parent,
    const QString& caption,
    const QString& dir,
    const QString& filter,
    Options options)
84
{
85
    _validate(options);
86 87
    
#ifdef QT_DEBUG
dogmaphobic's avatar
dogmaphobic committed
88
#ifndef __mobile__
89
    if (qgcApp()->runningUnitTests()) {
90
        return UnitTest::_getOpenFileNames(parent, caption, dir, filter, options);
91
    } else
dogmaphobic's avatar
dogmaphobic committed
92
#endif
93 94
#endif
    {
95
        return QFileDialog::getOpenFileNames(parent, caption, dir, filter, NULL, options);
96 97 98
    }
}

99 100
QString QGCFileDialog::getSaveFileName(
    QWidget* parent,
101 102 103
    const QString& caption,
    const QString& dir,
    const QString& filter,
104
    const QString& defaultSuffix,
105
    bool strict,
106
    Options options)
107
{
108
    _validate(options);
109

110
#ifdef QT_DEBUG
dogmaphobic's avatar
dogmaphobic committed
111
#ifndef __mobile__
112
    if (qgcApp()->runningUnitTests()) {
113
        return UnitTest::_getSaveFileName(parent, caption, dir, filter, defaultSuffix, options);
114
    } else
dogmaphobic's avatar
dogmaphobic committed
115
#endif
116 117
#endif
    {
118
        QString defaultSuffixCopy(defaultSuffix);
119
        QFileDialog dlg(parent, caption, dir, filter);
120
        dlg.setAcceptMode(QFileDialog::AcceptSave);
121
        if (options) {
122
            dlg.setOptions(options);
123
        }
124
        if (!defaultSuffixCopy.isEmpty()) {
125
            //-- Make sure dot is not present
126 127
            if (defaultSuffixCopy.startsWith(".")) {
                defaultSuffixCopy.remove(0,1);
128
            }
129
            dlg.setDefaultSuffix(defaultSuffixCopy);
130
        }
131 132 133
        while (true) {
            if (dlg.exec()) {
                if (dlg.selectedFiles().count()) {
134 135 136 137 138 139 140 141 142 143 144 145
                    QString result = dlg.selectedFiles().first();
                    //-- If we don't care about the extension, just return it
                    if (!strict) {
                        return result;
                    } else {
                        //-- We must enforce the file extension
                        QFileInfo fi(result);
                        QString userSuffix(fi.suffix());
                        if (_validateExtension(filter, userSuffix)) {
                            return result;
                        }
                        //-- Do we have a default extension?
146
                        if (defaultSuffixCopy.isEmpty()) {
147
                            //-- We don't, so get the first one in the filter
148
                            defaultSuffixCopy = _getFirstExtensionInFilter(filter);
149
                        }
150 151
                        //-- If this is set to strict, we have to have a default extension
                        Q_ASSERT(defaultSuffixCopy.isEmpty() == false);
152 153
                        //-- Forcefully append our desired extension
                        result += ".";
154
                        result += defaultSuffixCopy;
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
                        //-- Check and see if this new file already exists
                        fi.setFile(result);
                        if (fi.exists()) {
                            //-- Ask user what to do
                            QMessageBox msgBox(
                                QMessageBox::Warning,
                                tr("File Exists"),
                                tr("%1 already exists.\nDo you want to replace it?").arg(fi.fileName()),
                                QMessageBox::Cancel,
                                parent);
                            msgBox.addButton(QMessageBox::Retry);
                            QPushButton *overwriteButton = msgBox.addButton(tr("Replace"), QMessageBox::ActionRole);
                            msgBox.setDefaultButton(QMessageBox::Retry);
                            msgBox.setWindowModality(Qt::ApplicationModal);
                            if (msgBox.exec() == QMessageBox::Retry) {
                                continue;
                            } else if (msgBox.clickedButton() == overwriteButton) {
                                return result;
                            }
                        } else {
                            return result;
176 177
                        }
                    }
178
                }
179
            }
180
            break;
181
        }
182
        return QString("");
183 184 185
    }
}

186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
/// @brief Make sure filename is using one of the valid extensions defined in the filter
bool QGCFileDialog::_validateExtension(const QString& filter, const QString& extension) {
    QRegularExpression re("(\\*\\.\\w+)");
    QRegularExpressionMatchIterator i = re.globalMatch(filter);
    while (i.hasNext()) {
        QRegularExpressionMatch match = i.next();
        if (match.hasMatch()) {
            //-- Compare "foo" with "*.foo"
            if(extension == match.captured(0).mid(2)) {
                return true;
            }
        }
    }
    return false;
}

/// @brief Returns first extension found in filter
QString QGCFileDialog::_getFirstExtensionInFilter(const QString& filter) {
    QRegularExpression re("(\\*\\.\\w+)");
    QRegularExpressionMatchIterator i = re.globalMatch(filter);
    while (i.hasNext()) {
        QRegularExpressionMatch match = i.next();
        if (match.hasMatch()) {
209 210
            //-- Return "foo" from "*.foo"
            return match.captured(0).mid(2);
211 212 213 214 215
        }
    }
    return QString("");
}

216
/// @brief Validates and updates the parameters for the file dialog calls
217
void QGCFileDialog::_validate(Options& options)
218 219 220 221
{
    // You can't use QGCFileDialog if QGCApplication is not created yet.
    Q_ASSERT(qgcApp());
    
222
    Q_ASSERT_X(QThread::currentThread() == qgcApp()->thread(), "Threading issue", "QGCFileDialog can only be called from main thread");
dogmaphobic's avatar
dogmaphobic committed
223
#ifdef __mobile__
dogmaphobic's avatar
dogmaphobic committed
224 225
    Q_UNUSED(options)
#else
226
    // On OSX native dialog can hang so we always use Qt dialogs
Don Gagne's avatar
Don Gagne committed
227
    options |= DontUseNativeDialog;
dogmaphobic's avatar
dogmaphobic committed
228
#endif
229 230 231
    if (MainWindow::instance()) {
        MainWindow::instance()->hideSplashScreen();
    }
232
}