History

Remember last directory used for Window.browse() and Window.save()

Use last directory if not specified in method call. Default to desktop.

David Rowe committed 10 years ago

Commit 58f89c81 parent cf6945b3 files · +39 −4Browse files

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

Unified · Split

interface/src/scripting/WindowScriptingInterface.cpp+34−2
@@ -14,6 +14,8 @@ · 13 unmodified lines
1414 #include <QMessageBox>
1515 #include <QScriptValue>
1616
17+#include <SettingHandle.h>
18+
1719 #include "Application.h"
1820 #include "DomainHandler.h"
1921 #include "MainWindow.h"
@@ -23,6 +25,10 @@ · 3 unmodified lines
2325
2426 #include "WindowScriptingInterface.h"
2527
28+static const QString DESKTOP_LOCATION = QStandardPaths::writableLocation(QStandardPaths::DesktopLocation);
29+static const QString LAST_BROWSE_LOCATION_SETTING = "LastBrowseLocation";
30+
31+
2632 WindowScriptingInterface::WindowScriptingInterface() {
2733 const DomainHandler& domainHandler = DependencyManager::get<NodeList>()->getDomainHandler();
2834 connect(&domainHandler, &DomainHandler::connectedToDomain, this, &WindowScriptingInterface::domainChanged);
@@ -101,6 +107,14 @@ QString fixupPathForMac(const QString& directory) { · 72 unmodified lines
101107 return path;
102108 }
103109
110+QString WindowScriptingInterface::getPreviousBrowseLocation() const {
111+ return Setting::Handle<QString>(LAST_BROWSE_LOCATION_SETTING, DESKTOP_LOCATION).get();
112+}
113+
114+void WindowScriptingInterface::setPreviousBrowseLocation(const QString& location) {
115+ Setting::Handle<QVariant>(LAST_BROWSE_LOCATION_SETTING).set(location);
116+}
117+
104118 /// Display an open file dialog. If `directory` is an invalid file or directory the browser will start at the current
105119 /// working directory.
106120 /// \param const QString& title title of the window
@@ -108,8 +122,17 @@ QString fixupPathForMac(const QString& directory) { · 1 unmodified line
108122 /// \param const QString& nameFilter filter to filter filenames by - see `QFileDialog`
109123 /// \return QScriptValue file path as a string if one was selected, otherwise `QScriptValue::NullValue`
110124 QScriptValue WindowScriptingInterface::browse(const QString& title, const QString& directory, const QString& nameFilter) {
111− QString path = fixupPathForMac(directory);
125+ QString path = directory;
126+ if (path == "") {
127+ path = getPreviousBrowseLocation();
128+ }
129+#ifndef Q_OS_WIN
130+ path = fixupPathForMac(directory);
131+#endif
112132 QString result = OffscreenUi::getOpenFileName(nullptr, title, path, nameFilter);
133+ if (!result.isEmpty()) {
134+ setPreviousBrowseLocation(QFileInfo(result).absolutePath());
135+ }
113136 return result.isEmpty() ? QScriptValue::NullValue : QScriptValue(result);
114137 }
115138
@@ -120,8 +143,17 @@ QScriptValue WindowScriptingInterface::browse(const QString& title, const QStrin · 4 unmodified lines
120143 /// \param const QString& nameFilter filter to filter filenames by - see `QFileDialog`
121144 /// \return QScriptValue file path as a string if one was selected, otherwise `QScriptValue::NullValue`
122145 QScriptValue WindowScriptingInterface::save(const QString& title, const QString& directory, const QString& nameFilter) {
123− QString path = fixupPathForMac(directory);
146+ QString path = directory;
147+ if (path == "") {
148+ path = getPreviousBrowseLocation();
149+ }
150+#ifndef Q_OS_WIN
151+ path = fixupPathForMac(directory);
152+#endif
124153 QString result = OffscreenUi::getSaveFileName(nullptr, title, path, nameFilter);
154+ if (!result.isEmpty()) {
155+ setPreviousBrowseLocation(QFileInfo(result).absolutePath());
156+ }
125157 return result.isEmpty() ? QScriptValue::NullValue : QScriptValue(result);
126158 }
127159