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>
This commit is contained in:
parent
c14c3a1054
commit
666bc731a0
|
@ -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();
|
||||
}
|
||||
|
|
|
@ -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 : "";
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue