History

Add Window.browse to scripting environment

Ryan Huffman committed 12 years ago

Commit 5a5e6281 parent 76a8afa2 files · +37 −1Browse files

Showing the changes to interface/src/scripting/WindowScriptingInterface.cpp (1 of 2 files). Show all

Unified · Split

interface/src/scripting/WindowScriptingInterface.cpp+35−1
@@ -9,8 +9,10 @@ · 8 unmodified lines
99 // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
1010 //
1111
12−#include <QMessageBox>
12+#include <QDir>
13+#include <QFileDialog>
1314 #include <QInputDialog>
15+#include <QMessageBox>
1416
1517 #include "Application.h"
1618 #include "Menu.h"
@@ -43,6 +45,14 @@ QScriptValue WindowScriptingInterface::prompt(const QString& message, const QStr · 26 unmodified lines
4345 return retVal;
4446 }
4547
48+QScriptValue WindowScriptingInterface::browse(const QString& title, const QString& directory, const QString& nameFilter) {
49+ QScriptValue retVal;
50+ QMetaObject::invokeMethod(this, "showBrowse", Qt::BlockingQueuedConnection,
51+ Q_RETURN_ARG(QScriptValue, retVal),
52+ Q_ARG(const QString&, title), Q_ARG(const QString&, directory), Q_ARG(const QString&, nameFilter));
53+ return retVal;
54+}
55+
4656 /// Display an alert box
4757 /// \param const QString& message message to display
4858 /// \return QScriptValue::UndefinedValue
@@ -76,6 +86,30 @@ QScriptValue WindowScriptingInterface::showPrompt(const QString& message, const · 27 unmodified lines
7686 return QScriptValue::NullValue;
7787 }
7888
89+/// Display a file dialog. If `directory` is an invalid file or directory the browser will start at the current
90+/// working directory.
91+/// \param const QString& title title of the window
92+/// \param const QString& directory directory to start the file browser at
93+/// \param const QString& nameFilter filter to filter filenames by - see `QFileDialog`
94+/// \return QScriptValue file path as a string if one was selected, otherwise `QScriptValue::NullValue`
95+QScriptValue WindowScriptingInterface::showBrowse(const QString& title, const QString& directory, const QString& nameFilter) {
96+ // On OS X `directory` does not work as expected unless a file is included in the path, so we append a bogus
97+ // filename if the directory is valid.
98+ QString path = "";
99+ QFileInfo fileInfo = QFileInfo(directory);
100+ if (fileInfo.isDir()) {
101+ fileInfo.setFile(directory, "__HIFI_INVALID_FILE__");
102+ path = fileInfo.filePath();
103+ }
104+
105+ QFileDialog fileDialog(Application::getInstance()->getWindow(), title, path, nameFilter);
106+ fileDialog.setFileMode(QFileDialog::ExistingFile);
107+ if (fileDialog.exec()) {
108+ return QScriptValue(fileDialog.selectedFiles().first());
109+ }
110+ return QScriptValue::NullValue;
111+}
112+
79113 int WindowScriptingInterface::getInnerWidth() {
80114 return Application::getInstance()->getWindow()->geometry().width();
81115 }