summaryrefslogtreecommitdiffstats
path: root/src/dialogs/qquickfiledialog.cpp
diff options
context:
space:
mode:
authorShawn Rutledge <shawn.rutledge@digia.com>2015-03-17 10:55:31 +0100
committerShawn Rutledge <shawn.rutledge@digia.com>2015-03-18 15:13:23 +0000
commitc9e558a64833a7f74e9c0423c35a73ed5229f112 (patch)
tree8a0b679c906a35cc47e423ab6369585f9caeb16e /src/dialogs/qquickfiledialog.cpp
parent9e6389591e781e76d17dc99f9395219785797b32 (diff)
FileDialog: expose several QStandardPaths in the shortcuts property
QStandardPaths::PicturesLocation is a specialized kind of file URL on iOS; if this folder is set, then when the dialog is opened, it will be the new dialog helper which uses the native picture gallery picker interface. This will always work, so we do not need to check whether the path exists. The application developer needs to have a way to get that platform-specific (and odd-looking) URL by name, so the FileDialog.shortcuts property is now public API: a map from programmatic names (similar to those in QStandardPaths) to URLs. But DefaultFileDialog.qml should not display the names from this map, because they are not translated to the user's language. So __shortcuts is added as a private property providing a map from programmatic name to an object containing the translated name and the URL. This makes possible setting FileDialog { folder: shortcuts.pictures } which will open the special image gallery browser on iOS; and several other paths from QStandardPaths can be set in the same way. [ChangeLog][QtQuick.Dialogs] added FileDialog.shortcuts to enable setting the starting folder to a standard system path. Setting folder to shortcuts.pictures will result in a special image gallery dialog on iOS. Change-Id: I14f04712eb4f44ff422ac91a8720b9e3ff8fb920 Reviewed-by: Richard Moe Gustavsen <richard.gustavsen@theqtcompany.com>
Diffstat (limited to 'src/dialogs/qquickfiledialog.cpp')
-rw-r--r--src/dialogs/qquickfiledialog.cpp73
1 files changed, 50 insertions, 23 deletions
diff --git a/src/dialogs/qquickfiledialog.cpp b/src/dialogs/qquickfiledialog.cpp
index 384642dd0..f2325a530 100644
--- a/src/dialogs/qquickfiledialog.cpp
+++ b/src/dialogs/qquickfiledialog.cpp
@@ -37,6 +37,7 @@
#include "qquickfiledialog_p.h"
#include <QQuickItem>
#include <QQmlEngine>
+#include <QJSValueIterator>
#include <private/qguiapplication_p.h>
#include <private/qv4object_p.h>
@@ -111,44 +112,70 @@ QList<QUrl> QQuickFileDialog::fileUrls() const
return m_selections;
}
-
-void QQuickFileDialog::addShortcut(int &i, const QString &name, const QString &path)
+void QQuickFileDialog::addShortcut(uint &i, const QString &name, const QString &visibleName, const QString &path)
{
QJSEngine *engine = qmlEngine(this);
+ QUrl url = QUrl::fromLocalFile(path);
QJSValue o = engine->newObject();
- o.setProperty("name", name);
- o.setProperty("url", QUrl::fromLocalFile(path).toString());
- m_shortcuts.setProperty(i++, o);
+ o.setProperty("name", visibleName);
+ // TODO maybe some day QJSValue could directly store a QUrl
+ o.setProperty("url", url.toString());
+ m_shortcutDetails.setProperty(name, o);
+ m_shortcuts.setProperty(name, url.toString());
+ ++i;
}
-void QQuickFileDialog::addIfReadable(int &i, const QString &name, QStandardPaths::StandardLocation loc)
+void QQuickFileDialog::addIfReadable(uint &i, const QString &name, const QString &visibleName, QStandardPaths::StandardLocation loc)
{
QStringList paths = QStandardPaths::standardLocations(loc);
if (!paths.isEmpty() && QDir(paths.first()).isReadable())
- addShortcut(i, name, paths.first());
+ addShortcut(i, name, visibleName, paths.first());
+}
+
+void QQuickFileDialog::populateShortcuts()
+{
+ QJSEngine *engine = qmlEngine(this);
+ m_shortcutDetails = engine->newObject();
+ m_shortcuts = engine->newObject();
+ uint i = 0;
+
+ addIfReadable(i, QLatin1String("desktop"), QStandardPaths::displayName(QStandardPaths::DesktopLocation), QStandardPaths::DesktopLocation);
+ addIfReadable(i, QLatin1String("documents"), QStandardPaths::displayName(QStandardPaths::DocumentsLocation), QStandardPaths::DocumentsLocation);
+ addIfReadable(i, QLatin1String("music"), QStandardPaths::displayName(QStandardPaths::MusicLocation), QStandardPaths::MusicLocation);
+ addIfReadable(i, QLatin1String("movies"), QStandardPaths::displayName(QStandardPaths::MoviesLocation), QStandardPaths::MoviesLocation);
+ addIfReadable(i, QLatin1String("pictures"), QStandardPaths::displayName(QStandardPaths::PicturesLocation), QStandardPaths::PicturesLocation);
+ addIfReadable(i, QLatin1String("home"), QStandardPaths::displayName(QStandardPaths::HomeLocation), QStandardPaths::HomeLocation);
+
+#ifdef Q_OS_IOS
+ // PicturesLocation is a special URL, which we cannot check with QDir::isReadable()
+ addShortcut(i, QLatin1String("pictures"), tr("Pictures"),
+ QStandardPaths::standardLocations(QStandardPaths::PicturesLocation).last());
+#else
+ // on iOS, this returns only "/", which is never a useful path to read or write anything
+ QFileInfoList drives = QDir::drives();
+ foreach (QFileInfo fi, drives)
+ addShortcut(i, fi.absoluteFilePath(), fi.absoluteFilePath(), fi.absoluteFilePath());
+#endif
+
+ m_shortcutDetails.setProperty(QLatin1String("length"), i);
}
QJSValue QQuickFileDialog::shortcuts()
{
- if (m_shortcuts.isUndefined()) {
- QJSEngine *engine = qmlEngine(this);
- m_shortcuts = engine->newArray();
- int i = 0;
-
- addIfReadable(i, "Desktop", QStandardPaths::DesktopLocation);
- addIfReadable(i, "Documents", QStandardPaths::DocumentsLocation);
- addIfReadable(i, "Music", QStandardPaths::MusicLocation);
- addIfReadable(i, "Movies", QStandardPaths::MoviesLocation);
- addIfReadable(i, "Pictures", QStandardPaths::PicturesLocation);
- addIfReadable(i, "Home", QStandardPaths::HomeLocation);
-
- QFileInfoList drives = QDir::drives();
- foreach (QFileInfo fi, drives)
- addShortcut(i, fi.absoluteFilePath(), fi.absoluteFilePath());
- }
+ if (m_shortcuts.isUndefined())
+ populateShortcuts();
+
return m_shortcuts;
}
+QJSValue QQuickFileDialog::__shortcuts()
+{
+ if (m_shortcutDetails.isUndefined())
+ populateShortcuts();
+
+ return m_shortcutDetails;
+}
+
/*!
\qmlproperty bool AbstractFileDialog::visible