| 1 | /**************************************************************************** | 
| 2 | ** | 
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. | 
| 4 | ** Contact: https://www.qt.io/licensing/ | 
| 5 | ** | 
| 6 | ** This file is part of the Qt Mobility Components. | 
| 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 | #include <QtCore/QStandardPaths> | 
| 52 | #include <QtCore/QStringList> | 
| 53 | #include <QtQml/QQmlContext> | 
| 54 | #include <QtGui/QGuiApplication> | 
| 55 | #include <QtQuick/QQuickItem> | 
| 56 | #include <QtQuick/QQuickView> | 
| 57 | #include "filereader.h" | 
| 58 | #include "trace.h" | 
| 59 |  | 
| 60 | #ifdef PERFORMANCEMONITOR_SUPPORT | 
| 61 | #include "performancemonitordeclarative.h" | 
| 62 | #endif | 
| 63 |  | 
| 64 | #ifdef REQUEST_PERMISSIONS_ON_ANDROID | 
| 65 | #include <QtAndroid> | 
| 66 |  | 
| 67 | bool requestStoragePermission() { | 
| 68 |     using namespace QtAndroid; | 
| 69 |  | 
| 70 |     QString permission = QStringLiteral("android.permission.WRITE_EXTERNAL_STORAGE" ); | 
| 71 |     const QHash<QString, PermissionResult> results = requestPermissionsSync(QStringList({permission})); | 
| 72 |     if (!results.contains(permission) || results[permission] == PermissionResult::Denied) { | 
| 73 |         qWarning() << "Couldn't get permission: "  << permission; | 
| 74 |         return false; | 
| 75 |     } | 
| 76 |  | 
| 77 |     return true; | 
| 78 | } | 
| 79 | #endif | 
| 80 |  | 
| 81 | int main(int argc, char *argv[]) | 
| 82 | { | 
| 83 |     QGuiApplication app(argc, argv); | 
| 84 |  | 
| 85 | #ifdef PERFORMANCEMONITOR_SUPPORT | 
| 86 |     PerformanceMonitor::qmlRegisterTypes(); | 
| 87 | #endif | 
| 88 | #ifdef REQUEST_PERMISSIONS_ON_ANDROID | 
| 89 |     if (!requestStoragePermission()) | 
| 90 |         return -1; | 
| 91 | #endif | 
| 92 |  | 
| 93 |     QUrl fileName; | 
| 94 |     qreal volume = 0.5; | 
| 95 |     QStringList args = app.arguments(); | 
| 96 | #ifdef PERFORMANCEMONITOR_SUPPORT | 
| 97 |     PerformanceMonitor::State performanceMonitorState; | 
| 98 | #endif | 
| 99 |     for (int i = 1; i < args.size(); ++i) { | 
| 100 |         const QByteArray arg = args.at(i).toUtf8(); | 
| 101 |         if (arg.startsWith(c: '-')) { | 
| 102 |             if ("-volume"  == arg) { | 
| 103 |                 if (i + 1 < args.size()) | 
| 104 |                     volume = 0.01 * args.at(i: ++i).toInt(); | 
| 105 |                 else | 
| 106 |                     qtTrace() << "Option \"-volume\" takes a value" ; | 
| 107 |             } | 
| 108 | #ifdef PERFORMANCEMONITOR_SUPPORT | 
| 109 |             else if (performanceMonitorState.parseArgument(arg)) { | 
| 110 |                 // Do nothing | 
| 111 |             } | 
| 112 | #endif | 
| 113 |             else { | 
| 114 |                 qtTrace() << "Option"  << arg << "ignored" ; | 
| 115 |             } | 
| 116 |         } else { | 
| 117 |             if (fileName.isEmpty()) | 
| 118 |                 fileName = QUrl::fromLocalFile(localfile: arg); | 
| 119 |             else | 
| 120 |                 qtTrace() << "Argument"  << arg << "ignored" ; | 
| 121 |         } | 
| 122 |     } | 
| 123 |  | 
| 124 |     QQuickView viewer; | 
| 125 |  | 
| 126 |     viewer.setSource(QUrl(QLatin1String("qrc:///qml/qmlvideofx/Main.qml" ))); | 
| 127 |     QQuickItem *rootObject = viewer.rootObject(); | 
| 128 |     rootObject->setProperty(name: "fileName" , value: fileName); | 
| 129 |     viewer.rootObject()->setProperty(name: "volume" , value: volume); | 
| 130 |  | 
| 131 | #ifdef PERFORMANCEMONITOR_SUPPORT | 
| 132 |     if (performanceMonitorState.valid) { | 
| 133 |         rootObject->setProperty(name: "perfMonitorsLogging" , value: performanceMonitorState.logging); | 
| 134 |         rootObject->setProperty(name: "perfMonitorsVisible" , value: performanceMonitorState.visible); | 
| 135 |     } | 
| 136 |     QObject::connect(sender: &viewer, SIGNAL(afterRendering()), | 
| 137 |                      receiver: rootObject, SLOT(qmlFramePainted())); | 
| 138 | #endif | 
| 139 |  | 
| 140 |     FileReader fileReader; | 
| 141 |     viewer.rootContext()->setContextProperty("fileReader" , &fileReader); | 
| 142 |  | 
| 143 |     const QUrl appPath(QUrl::fromLocalFile(localfile: app.applicationDirPath())); | 
| 144 |     const QStringList picturesLocation = QStandardPaths::standardLocations(type: QStandardPaths::PicturesLocation); | 
| 145 |     const QUrl imagePath = picturesLocation.isEmpty() ? appPath : QUrl::fromLocalFile(localfile: picturesLocation.first()); | 
| 146 |     viewer.rootContext()->setContextProperty("imagePath" , imagePath); | 
| 147 |  | 
| 148 |     const QStringList moviesLocation = QStandardPaths::standardLocations(type: QStandardPaths::MoviesLocation); | 
| 149 |     const QUrl videoPath = moviesLocation.isEmpty() ? appPath : QUrl::fromLocalFile(localfile: moviesLocation.first()); | 
| 150 |     viewer.rootContext()->setContextProperty("videoPath" , videoPath); | 
| 151 |  | 
| 152 |     viewer.setTitle("qmlvideofx" ); | 
| 153 |     viewer.setFlags(Qt::Window | Qt::WindowSystemMenuHint | Qt::WindowTitleHint | | 
| 154 |                           Qt::WindowMinMaxButtonsHint | Qt::WindowCloseButtonHint); | 
| 155 |     viewer.setMinimumSize(QSize(1280, 720)); | 
| 156 |     viewer.setResizeMode(QQuickView::SizeRootObjectToView); | 
| 157 |  | 
| 158 |     viewer.show(); | 
| 159 |  | 
| 160 |     // Delay invocation of init until the event loop has started, to work around | 
| 161 |     // a GL context issue on Harmattan: without this, we get the following error | 
| 162 |     // when the first ShaderEffectItem is created: | 
| 163 |     // "QGLShaderProgram::addShader: Program and shader are not associated with same context" | 
| 164 |     QMetaObject::invokeMethod(obj: viewer.rootObject(), member: "init" , type: Qt::QueuedConnection); | 
| 165 |  | 
| 166 |     return app.exec(); | 
| 167 | } | 
| 168 |  | 
| 169 |  |