aboutsummaryrefslogtreecommitdiffstats
path: root/examples
diff options
context:
space:
mode:
authorFriedemann Kleint <Friedemann.Kleint@theqtcompany.com>2016-01-26 16:49:31 +0100
committerShawn Rutledge <shawn.rutledge@theqtcompany.com>2016-01-29 08:23:56 +0000
commit666bc731a0ba930ca0cfda18daf836913fd91361 (patch)
treead83a62b470f07b1ece2c720fa3854a0297ad397 /examples
parentc14c3a1054f68886413e8698cdf67275d91d3e58 (diff)
photosurface demo: Improve handling of image directories.
Make it possible to pass URLs on the command line. If no argument is given and the pictures location as returned by QStandardPaths exists and has contents, show it, Otherwise, show the file dialog as was before. Set context properties containing pictures location, image name filters and initial URL. Derive the image filter string from QImageReader/QMimeDatabase. Change-Id: I89bdff27416bf8ef725aa4e17853b2f634cf059b Reviewed-by: Shawn Rutledge <shawn.rutledge@theqtcompany.com>
Diffstat (limited to 'examples')
-rw-r--r--examples/quick/demos/photosurface/main.cpp66
-rw-r--r--examples/quick/demos/photosurface/photosurface.qml21
2 files changed, 84 insertions, 3 deletions
diff --git a/examples/quick/demos/photosurface/main.cpp b/examples/quick/demos/photosurface/main.cpp
index 3058238494..9456694032 100644
--- a/examples/quick/demos/photosurface/main.cpp
+++ b/examples/quick/demos/photosurface/main.cpp
@@ -44,9 +44,28 @@
#include <QtGui/QGuiApplication>
#endif
#include <QtQml/QQmlApplicationEngine>
+#include <QtQml/QQmlContext>
#include <QtQuick/QQuickWindow>
+#include <QtGui/QImageReader>
+#include <QtCore/QCommandLineParser>
+#include <QtCore/QCommandLineOption>
+#include <QtCore/QDebug>
+#include <QtCore/QDir>
+#include <QtCore/QMimeDatabase>
+#include <QtCore/QStandardPaths>
#include <QtCore/QUrl>
+static QStringList imageNameFilters()
+{
+ QStringList result;
+ QMimeDatabase mimeDatabase;
+ foreach (const QByteArray &m, QImageReader::supportedMimeTypes()) {
+ foreach (const QString &suffix, mimeDatabase.mimeTypeForName(m).suffixes())
+ result.append(QStringLiteral("*.") + suffix);
+ }
+ return result;
+}
+
int main(int argc, char* argv[])
{
// The reason to use QApplication is that QWidget-based dialogs
@@ -58,6 +77,51 @@ int main(int argc, char* argv[])
QGuiApplication app(argc, argv);
#endif
QQuickWindow::setDefaultAlphaBuffer(true);
- QQmlApplicationEngine engine(QUrl("qrc:///photosurface.qml"));
+
+ QCoreApplication::setApplicationName(QStringLiteral("Photosurface"));
+ QCoreApplication::setOrganizationName(QStringLiteral("QtProject"));
+ QCoreApplication::setApplicationVersion(QLatin1String(QT_VERSION_STR));
+ QCommandLineParser parser;
+ parser.setApplicationDescription(QStringLiteral("Qt Quick Demo - Photo Surface"));
+ parser.addHelpOption();
+ parser.addVersionOption();
+ parser.addPositionalArgument(QStringLiteral("directory"),
+ QStringLiteral("The image directory or URL to show."));
+ parser.process(app);
+
+ QUrl initialUrl;
+ if (!parser.positionalArguments().isEmpty()) {
+ initialUrl = QUrl::fromUserInput(parser.positionalArguments().first(),
+ QDir::currentPath(), QUrl::AssumeLocalFile);
+ if (!initialUrl.isValid()) {
+ qWarning().nospace() << "Invalid argument: \""
+ << parser.positionalArguments().first() << "\": " << initialUrl.errorString();
+ return 1;
+ }
+ }
+
+ const QStringList nameFilters = imageNameFilters();
+
+ QQmlApplicationEngine engine;
+ QQmlContext *context = engine.rootContext();
+
+ QUrl picturesLocationUrl = QUrl::fromLocalFile(QDir::homePath());
+ const QStringList picturesLocations = QStandardPaths::standardLocations(QStandardPaths::PicturesLocation);
+ if (!picturesLocations.isEmpty()) {
+ picturesLocationUrl = QUrl::fromLocalFile(picturesLocations.first());
+ if (initialUrl.isEmpty()
+ && !QDir(picturesLocations.first()).entryInfoList(nameFilters, QDir::Files).isEmpty()) {
+ initialUrl = picturesLocationUrl;
+ }
+ }
+
+ context->setContextProperty(QStringLiteral("contextPicturesLocation"), picturesLocationUrl);
+ context->setContextProperty(QStringLiteral("contextInitialUrl"), initialUrl);
+ context->setContextProperty(QStringLiteral("contextImageNameFilters"), nameFilters);
+
+ engine.load(QUrl("qrc:///photosurface.qml"));
+ if (engine.rootObjects().isEmpty())
+ return -1;
+
return app.exec();
}
diff --git a/examples/quick/demos/photosurface/photosurface.qml b/examples/quick/demos/photosurface/photosurface.qml
index 22cef62157..d8a424e8f8 100644
--- a/examples/quick/demos/photosurface/photosurface.qml
+++ b/examples/quick/demos/photosurface/photosurface.qml
@@ -56,6 +56,7 @@ Window {
id: fileDialog
title: "Choose a folder with some images"
selectFolder: true
+ folder: picturesLocation
onAccepted: folderModel.folder = fileUrl + "/"
}
@@ -69,7 +70,7 @@ Window {
id: folderModel
objectName: "folderModel"
showDirs: false
- nameFilters: ["*.png", "*.jpg", "*.gif"]
+ nameFilters: imageNameFilters
}
Rectangle {
id: photoFrame
@@ -255,5 +256,21 @@ Window {
Shortcut { sequence: StandardKey.Quit; onActivated: Qt.quit() }
- Component.onCompleted: fileDialog.open()
+ Component.onCompleted: {
+ if (typeof contextInitialUrl !== 'undefined') {
+ // Launched from C++ with context properties set.
+ imageNameFilters = contextImageNameFilters;
+ picturesLocation = contextPicturesLocation;
+ if (contextInitialUrl == "")
+ fileDialog.open();
+ else
+ folderModel.folder = contextInitialUrl + "/";
+ } else {
+ // Launched via QML viewer without context properties set.
+ fileDialog.open();
+ }
+ }
+
+ property var imageNameFilters : ["*.png", "*.jpg", "*.gif"];
+ property string picturesLocation : "";
}