| 1 | /**************************************************************************** | 
| 2 | ** | 
| 3 | ** Copyright (C) 2017 The Qt Company Ltd. | 
| 4 | ** Contact: https://www.qt.io/licensing/ | 
| 5 | ** | 
| 6 | ** This file is part of the examples of the Qt Toolkit. | 
| 7 | ** | 
| 8 | ** $QT_BEGIN_LICENSE:BSD$ | 
| 9 | ** Commercial License Usage | 
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in | 
| 11 | ** accordance with the commercial license agreement provided with the | 
| 12 | ** Software or, alternatively, in accordance with the terms contained in | 
| 13 | ** a written agreement between you and The Qt Company. For licensing terms | 
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further | 
| 15 | ** information use the contact form at https://www.qt.io/contact-us. | 
| 16 | ** | 
| 17 | ** BSD License Usage | 
| 18 | ** Alternatively, you may use this file under the terms of the BSD license | 
| 19 | ** as follows: | 
| 20 | ** | 
| 21 | ** "Redistribution and use in source and binary forms, with or without | 
| 22 | ** modification, are permitted provided that the following conditions are | 
| 23 | ** met: | 
| 24 | **   * Redistributions of source code must retain the above copyright | 
| 25 | **     notice, this list of conditions and the following disclaimer. | 
| 26 | **   * Redistributions in binary form must reproduce the above copyright | 
| 27 | **     notice, this list of conditions and the following disclaimer in | 
| 28 | **     the documentation and/or other materials provided with the | 
| 29 | **     distribution. | 
| 30 | **   * Neither the name of The Qt Company Ltd nor the names of its | 
| 31 | **     contributors may be used to endorse or promote products derived | 
| 32 | **     from this software without specific prior written permission. | 
| 33 | ** | 
| 34 | ** | 
| 35 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 
| 36 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 
| 37 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | 
| 38 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 
| 39 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 
| 40 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 
| 41 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 
| 42 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 
| 43 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 
| 44 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 
| 45 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." | 
| 46 | ** | 
| 47 | ** $QT_END_LICENSE$ | 
| 48 | ** | 
| 49 | ****************************************************************************/ | 
| 50 |  | 
| 51 | #ifdef QT_WIDGETS_LIB | 
| 52 | #include <QtWidgets/QApplication> | 
| 53 | #else | 
| 54 | #include <QtGui/QGuiApplication> | 
| 55 | #endif | 
| 56 | #include <QtQml/QQmlApplicationEngine> | 
| 57 | #include <QtQml/QQmlContext> | 
| 58 | #include <QtQuick/QQuickWindow> | 
| 59 | #include <QtGui/QImageReader> | 
| 60 | #include <QtCore/QCommandLineParser> | 
| 61 | #include <QtCore/QCommandLineOption> | 
| 62 | #include <QtCore/QDebug> | 
| 63 | #include <QtCore/QDir> | 
| 64 | #include <QtCore/QMimeDatabase> | 
| 65 | #include <QtCore/QStandardPaths> | 
| 66 | #include <QtCore/QUrl> | 
| 67 | #ifdef REQUEST_PERMISSIONS_ON_ANDROID | 
| 68 | #include <QtAndroid> | 
| 69 |  | 
| 70 | bool requestStoragePermission() { | 
| 71 |     using namespace QtAndroid; | 
| 72 |  | 
| 73 |     QString permission = QStringLiteral("android.permission.WRITE_EXTERNAL_STORAGE" ); | 
| 74 |     const QHash<QString, PermissionResult> results = requestPermissionsSync(QStringList({permission})); | 
| 75 |     if (!results.contains(permission) || results[permission] == PermissionResult::Denied) { | 
| 76 |         qWarning() << "Couldn't get permission: "  << permission; | 
| 77 |         return false; | 
| 78 |     } | 
| 79 |  | 
| 80 |     return true; | 
| 81 | } | 
| 82 | #endif | 
| 83 |  | 
| 84 | static QStringList imageNameFilters() | 
| 85 | { | 
| 86 |     QStringList result; | 
| 87 |     QMimeDatabase mimeDatabase; | 
| 88 |     const auto supportedMimeTypes = QImageReader::supportedMimeTypes(); | 
| 89 |     for (const QByteArray &m : supportedMimeTypes) { | 
| 90 |         const auto suffixes = mimeDatabase.mimeTypeForName(nameOrAlias: m).suffixes(); | 
| 91 |         for (const QString &suffix : suffixes) | 
| 92 |             result.append(t: QLatin1String("*." ) + suffix); | 
| 93 |     } | 
| 94 |     return result; | 
| 95 | } | 
| 96 |  | 
| 97 | int main(int argc, char* argv[]) | 
| 98 | { | 
| 99 |     // The reason to use QApplication is that QWidget-based dialogs | 
| 100 |     // are the native dialogs on Qt-based platforms like KDE, | 
| 101 |     // but they cannot be instantiated if this is a QGuiApplication. | 
| 102 | #ifdef QT_WIDGETS_LIB | 
| 103 |     QApplication app(argc, argv); | 
| 104 | #else | 
| 105 |     QGuiApplication app(argc, argv); | 
| 106 | #endif | 
| 107 | #ifdef REQUEST_PERMISSIONS_ON_ANDROID | 
| 108 |     if (!requestStoragePermission()) | 
| 109 |         return -1; | 
| 110 | #endif | 
| 111 |     QQuickWindow::setDefaultAlphaBuffer(true); | 
| 112 |  | 
| 113 |     QCoreApplication::setApplicationName(QStringLiteral("Photosurface" )); | 
| 114 |     QCoreApplication::setOrganizationName(QStringLiteral("QtProject" )); | 
| 115 |     QCoreApplication::setApplicationVersion(QLatin1String(QT_VERSION_STR)); | 
| 116 |     QCommandLineParser parser; | 
| 117 |     parser.setApplicationDescription(QStringLiteral("Qt Quick Demo - Photo Surface" )); | 
| 118 |     parser.addHelpOption(); | 
| 119 |     parser.addVersionOption(); | 
| 120 |     parser.addPositionalArgument(QStringLiteral("directory" ), | 
| 121 |                                  QStringLiteral("The image directory or URL to show." )); | 
| 122 |     parser.process(app); | 
| 123 |  | 
| 124 |     QUrl initialUrl; | 
| 125 |     if (!parser.positionalArguments().isEmpty()) { | 
| 126 |         initialUrl = QUrl::fromUserInput(userInput: parser.positionalArguments().first(), | 
| 127 |                                          workingDirectory: QDir::currentPath(), options: QUrl::AssumeLocalFile); | 
| 128 |         if (!initialUrl.isValid()) { | 
| 129 |             qWarning().nospace() << "Invalid argument: \""  | 
| 130 |                 << parser.positionalArguments().first() << "\": "  << initialUrl.errorString(); | 
| 131 |             return 1; | 
| 132 |         } | 
| 133 |     } | 
| 134 |  | 
| 135 |     const QStringList nameFilters = imageNameFilters(); | 
| 136 |  | 
| 137 |     QQmlApplicationEngine engine; | 
| 138 |     QQmlContext *context = engine.rootContext(); | 
| 139 |  | 
| 140 |     QUrl picturesLocationUrl = QUrl::fromLocalFile(localfile: QDir::homePath()); | 
| 141 |     const QStringList picturesLocations = QStandardPaths::standardLocations(type: QStandardPaths::PicturesLocation); | 
| 142 |     if (!picturesLocations.isEmpty()) { | 
| 143 |         picturesLocationUrl = QUrl::fromLocalFile(localfile: picturesLocations.first()); | 
| 144 |         if (initialUrl.isEmpty() | 
| 145 |             && !QDir(picturesLocations.first()).entryInfoList(nameFilters, filters: QDir::Files).isEmpty()) { | 
| 146 |             initialUrl = picturesLocationUrl; | 
| 147 |         } | 
| 148 |     } | 
| 149 |  | 
| 150 |     context->setContextProperty(QStringLiteral("contextPicturesLocation" ), picturesLocationUrl); | 
| 151 |     context->setContextProperty(QStringLiteral("contextInitialUrl" ), initialUrl); | 
| 152 |     context->setContextProperty(QStringLiteral("contextImageNameFilters" ), nameFilters); | 
| 153 |  | 
| 154 |     engine.load(url: QUrl("qrc:///photosurface.qml" )); | 
| 155 |     if (engine.rootObjects().isEmpty()) | 
| 156 |         return -1; | 
| 157 |  | 
| 158 |     return app.exec(); | 
| 159 | } | 
| 160 |  |