Showing the changes to interface/src/scripting/WindowScriptingInterface.cpp (1 of 4 files). Show all
Unified · Split
| @@ -9,20 +9,15 @@ · 8 unmodified lines | ||
| 9 | 9 | // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html |
| 10 | 10 | // |
| 11 | 11 | |
| 12 | −#include <QDir> | |
| 13 | −#include <QDialogButtonBox> | |
| 14 | −#include <QFileDialog> | |
| 15 | −#include <QInputDialog> | |
| 12 | +#include <QtCore/QDir> | |
| 16 | 13 | #include <QMessageBox> |
| 17 | 14 | #include <QScriptValue> |
| 18 | −#include <QScrollArea> | |
| 19 | 15 | |
| 20 | 16 | #include "Application.h" |
| 21 | 17 | #include "DomainHandler.h" |
| 22 | 18 | #include "MainWindow.h" |
| 23 | 19 | #include "Menu.h" |
| 24 | 20 | #include "OffscreenUi.h" |
| 25 | −#include "ui/ModelsBrowser.h" | |
| 26 | 21 | #include "WebWindowClass.h" |
| 27 | 22 | |
| 28 | 23 | #include "WindowScriptingInterface.h" |
| @@ -81,131 +76,31 @@ QScriptValue WindowScriptingInterface::getCursorPositionY() { · 52 unmodified lines | ||
| 81 | 76 | return QCursor::pos().y(); |
| 82 | 77 | } |
| 83 | 78 | |
| 84 | −QScriptValue WindowScriptingInterface::alert(const QString& message) { | |
| 85 | − QScriptValue retVal; | |
| 86 | − QMetaObject::invokeMethod(this, "showAlert", Qt::BlockingQueuedConnection, Q_RETURN_ARG(QScriptValue, retVal), Q_ARG(const QString&, message)); | |
| 87 | − return retVal; | |
| 88 | −} | |
| 89 | − | |
| 90 | −QScriptValue WindowScriptingInterface::confirm(const QString& message) { | |
| 91 | − QScriptValue retVal; | |
| 92 | − QMetaObject::invokeMethod(this, "showConfirm", Qt::BlockingQueuedConnection, | |
| 93 | − Q_RETURN_ARG(QScriptValue, retVal), Q_ARG(const QString&, message)); | |
| 94 | − return retVal; | |
| 95 | −} | |
| 96 | − | |
| 97 | −QScriptValue WindowScriptingInterface::prompt(const QString& message, const QString& defaultText) { | |
| 98 | − QScriptValue retVal; | |
| 99 | − QMetaObject::invokeMethod(this, "showPrompt", Qt::BlockingQueuedConnection, | |
| 100 | − Q_RETURN_ARG(QScriptValue, retVal), | |
| 101 | − Q_ARG(const QString&, message), Q_ARG(const QString&, defaultText)); | |
| 102 | − return retVal; | |
| 103 | −} | |
| 104 | − | |
| 105 | −QScriptValue WindowScriptingInterface::browse(const QString& title, const QString& directory, const QString& nameFilter) { | |
| 106 | − QScriptValue retVal; | |
| 107 | − QMetaObject::invokeMethod(this, "showBrowse", Qt::BlockingQueuedConnection, | |
| 108 | − Q_RETURN_ARG(QScriptValue, retVal), | |
| 109 | − Q_ARG(const QString&, title), Q_ARG(const QString&, directory), Q_ARG(const QString&, nameFilter)); | |
| 110 | − return retVal; | |
| 111 | −} | |
| 112 | − | |
| 113 | −QScriptValue WindowScriptingInterface::save(const QString& title, const QString& directory, const QString& nameFilter) { | |
| 114 | − QScriptValue retVal; | |
| 115 | − QMetaObject::invokeMethod(this, "showBrowse", Qt::BlockingQueuedConnection, | |
| 116 | − Q_RETURN_ARG(QScriptValue, retVal), | |
| 117 | − Q_ARG(const QString&, title), Q_ARG(const QString&, directory), Q_ARG(const QString&, nameFilter), | |
| 118 | − Q_ARG(QFileDialog::AcceptMode, QFileDialog::AcceptSave)); | |
| 119 | − return retVal; | |
| 120 | −} | |
| 121 | − | |
| 122 | −QScriptValue WindowScriptingInterface::s3Browse(const QString& nameFilter) { | |
| 123 | − QScriptValue retVal; | |
| 124 | − QMetaObject::invokeMethod(this, "showS3Browse", Qt::BlockingQueuedConnection, | |
| 125 | − Q_RETURN_ARG(QScriptValue, retVal), | |
| 126 | − Q_ARG(const QString&, nameFilter)); | |
| 127 | − return retVal; | |
| 128 | −} | |
| 129 | − | |
| 130 | − | |
| 131 | 79 | /// Display an alert box |
| 132 | 80 | /// \param const QString& message message to display |
| 133 | 81 | /// \return QScriptValue::UndefinedValue |
| 134 | −QScriptValue WindowScriptingInterface::showAlert(const QString& message) { | |
| 82 | +void WindowScriptingInterface::alert(const QString& message) { | |
| 135 | 83 | OffscreenUi::warning("", message); |
| 136 | − return QScriptValue::UndefinedValue; | |
| 137 | 84 | } |
| 138 | 85 | |
| 139 | 86 | /// Display a confirmation box with the options 'Yes' and 'No' |
| 140 | 87 | /// \param const QString& message message to display |
| 141 | 88 | /// \return QScriptValue `true` if 'Yes' was clicked, `false` otherwise |
| 142 | −QScriptValue WindowScriptingInterface::showConfirm(const QString& message) { | |
| 143 | − bool confirm = false; | |
| 144 | − if (QMessageBox::Yes == OffscreenUi::question("", message)) { | |
| 145 | − confirm = true; | |
| 146 | − } | |
| 147 | − return QScriptValue(confirm); | |
| 148 | −} | |
| 149 | − | |
| 150 | −void WindowScriptingInterface::chooseDirectory() { | |
| 151 | − QPushButton* button = reinterpret_cast<QPushButton*>(sender()); | |
| 152 | − | |
| 153 | − QString title = button->property("title").toString(); | |
| 154 | − QString path = button->property("path").toString(); | |
| 155 | − QRegExp displayAs = button->property("displayAs").toRegExp(); | |
| 156 | − QRegExp validateAs = button->property("validateAs").toRegExp(); | |
| 157 | − QString errorMessage = button->property("errorMessage").toString(); | |
| 158 | − | |
| 159 | − QString directory = QFileDialog::getExistingDirectory(button, title, path); | |
| 160 | − if (directory.isEmpty()) { | |
| 161 | − return; | |
| 162 | − } | |
| 163 | − | |
| 164 | − if (!validateAs.exactMatch(directory)) { | |
| 165 | − OffscreenUi::warning(NULL, "Invalid Directory", errorMessage); | |
| 166 | − return; | |
| 167 | − } | |
| 168 | − | |
| 169 | − button->setProperty("path", directory); | |
| 170 | − | |
| 171 | − displayAs.indexIn(directory); | |
| 172 | − QString buttonText = displayAs.cap(1) != "" ? displayAs.cap(1) : "."; | |
| 173 | − button->setText(buttonText); | |
| 174 | −} | |
| 175 | − | |
| 176 | −void WindowScriptingInterface::inlineButtonClicked() { | |
| 177 | − QPushButton* button = reinterpret_cast<QPushButton*>(sender()); | |
| 178 | − QString name = button->property("name").toString(); | |
| 179 | − emit inlineButtonClicked(name); | |
| 180 | −} | |
| 181 | − | |
| 182 | −QString WindowScriptingInterface::jsRegExp2QtRegExp(const QString& string) { | |
| 183 | − // Converts string representation of RegExp from JavaScript format to Qt format. | |
| 184 | − return string.mid(1, string.length() - 2) // No enclosing slashes. | |
| 185 | − .replace("\\/", "/"); // No escaping of forward slash. | |
| 89 | +QScriptValue WindowScriptingInterface::confirm(const QString& message) { | |
| 90 | + return QScriptValue((QMessageBox::Yes == OffscreenUi::question("", message))); | |
| 186 | 91 | } |
| 187 | 92 | |
| 188 | 93 | /// Display a prompt with a text box |
| 189 | 94 | /// \param const QString& message message to display |
| 190 | 95 | /// \param const QString& defaultText default text in the text box |
| 191 | 96 | /// \return QScriptValue string text value in text box if the dialog was accepted, `null` otherwise. |
| 192 | −QScriptValue WindowScriptingInterface::showPrompt(const QString& message, const QString& defaultText) { | |
| 97 | +QScriptValue WindowScriptingInterface::prompt(const QString& message, const QString& defaultText) { | |
| 193 | 98 | bool ok = false; |
| 194 | 99 | QString result = OffscreenUi::getText(nullptr, "", message, QLineEdit::Normal, defaultText, &ok); |
| 195 | − if (!ok) { | |
| 196 | − return QScriptValue::NullValue; | |
| 197 | − } | |
| 198 | − return QScriptValue(result); | |
| 100 | + return ok ? QScriptValue(result) : QScriptValue::NullValue; | |
| 199 | 101 | } |
| 200 | 102 | |
| 201 | −/// Display a file dialog. If `directory` is an invalid file or directory the browser will start at the current | |
| 202 | −/// working directory. | |
| 203 | −/// \param const QString& title title of the window | |
| 204 | −/// \param const QString& directory directory to start the file browser at | |
| 205 | −/// \param const QString& nameFilter filter to filter filenames by - see `QFileDialog` | |
| 206 | −/// \return QScriptValue file path as a string if one was selected, otherwise `QScriptValue::NullValue` | |
| 207 | −QScriptValue WindowScriptingInterface::showBrowse(const QString& title, const QString& directory, const QString& nameFilter, | |
| 208 | − QFileDialog::AcceptMode acceptMode) { | |
| 103 | +QString fixupPathForMac(const QString& directory) { | |
| 209 | 104 | // On OS X `directory` does not work as expected unless a file is included in the path, so we append a bogus |
| 210 | 105 | // filename if the directory is valid. |
| 211 | 106 | QString path = ""; |
| @@ -214,35 +109,31 @@ QScriptValue WindowScriptingInterface::showBrowse(const QString& title, const QS · 2 unmodified lines | ||
| 214 | 109 | fileInfo.setFile(directory, "__HIFI_INVALID_FILE__"); |
| 215 | 110 | path = fileInfo.filePath(); |
| 216 | 111 | } |
| 217 | − | |
| 218 | − QFileDialog fileDialog(qApp->getWindow(), title, path, nameFilter); | |
| 219 | − fileDialog.setAcceptMode(acceptMode); | |
| 220 | − QUrl fileUrl(directory); | |
| 221 | − if (acceptMode == QFileDialog::AcceptSave) { | |
| 222 | − // TODO -- Setting this breaks the dialog on Linux. Does it help something on other platforms? | |
| 223 | − // fileDialog.setFileMode(QFileDialog::Directory); | |
| 224 | − fileDialog.selectFile(fileUrl.fileName()); | |
| 225 | − } | |
| 226 | − if (fileDialog.exec()) { | |
| 227 | − return QScriptValue(fileDialog.selectedFiles().first()); | |
| 228 | − } | |
| 229 | − return QScriptValue::NullValue; | |
| 112 | + return path; | |
| 230 | 113 | } |
| 231 | 114 | |
| 232 | −/// Display a browse window for S3 models | |
| 233 | −/// \param const QString& nameFilter filter to filter filenames | |
| 115 | +/// Display an open file dialog. If `directory` is an invalid file or directory the browser will start at the current | |
| 116 | +/// working directory. | |
| 117 | +/// \param const QString& title title of the window | |
| 118 | +/// \param const QString& directory directory to start the file browser at | |
| 119 | +/// \param const QString& nameFilter filter to filter filenames by - see `QFileDialog` | |
| 234 | 120 | /// \return QScriptValue file path as a string if one was selected, otherwise `QScriptValue::NullValue` |
| 235 | −QScriptValue WindowScriptingInterface::showS3Browse(const QString& nameFilter) { | |
| 236 | − ModelsBrowser browser(FSTReader::ENTITY_MODEL); | |
| 237 | − if (nameFilter != "") { | |
| 238 | − browser.setNameFilter(nameFilter); | |
| 239 | − } | |
| 240 | − QEventLoop loop; | |
| 241 | − connect(&browser, &ModelsBrowser::selected, &loop, &QEventLoop::quit); | |
| 242 | − QMetaObject::invokeMethod(&browser, "browse", Qt::QueuedConnection); | |
| 243 | − loop.exec(); | |
| 244 | − | |
| 245 | − return browser.getSelectedFile(); | |
| 121 | +QScriptValue WindowScriptingInterface::browse(const QString& title, const QString& directory, const QString& nameFilter) { | |
| 122 | + QString path = fixupPathForMac(directory); | |
| 123 | + QString result = OffscreenUi::getOpenFileName(nullptr, title, path, nameFilter); | |
| 124 | + return result.isEmpty() ? QScriptValue::NullValue : QScriptValue(result); | |
| 125 | +} | |
| 126 | + | |
| 127 | +/// Display a save file dialog. If `directory` is an invalid file or directory the browser will start at the current | |
| 128 | +/// working directory. | |
| 129 | +/// \param const QString& title title of the window | |
| 130 | +/// \param const QString& directory directory to start the file browser at | |
| 131 | +/// \param const QString& nameFilter filter to filter filenames by - see `QFileDialog` | |
| 132 | +/// \return QScriptValue file path as a string if one was selected, otherwise `QScriptValue::NullValue` | |
| 133 | +QScriptValue WindowScriptingInterface::save(const QString& title, const QString& directory, const QString& nameFilter) { | |
| 134 | + QString path = fixupPathForMac(directory); | |
| 135 | + QString result = OffscreenUi::getSaveFileName(nullptr, title, path, nameFilter); | |
| 136 | + return result.isEmpty() ? QScriptValue::NullValue : QScriptValue(result); | |
| 246 | 137 | } |
| 247 | 138 | |
| 248 | 139 | int WindowScriptingInterface::getInnerWidth() { |