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 | // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html | 9 | // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html |
| 10 | // | 10 | // |
| 11 | 11 | ||
| 12 | #include <QDir> | 12 | #include <QtCore/QDir> |
| 13 | #include <QDialogButtonBox> | ||
| 14 | #include <QFileDialog> | ||
| 15 | #include <QInputDialog> | ||
| 16 | #include <QMessageBox> | 13 | #include <QMessageBox> |
| 17 | #include <QScriptValue> | 14 | #include <QScriptValue> |
| 18 | #include <QScrollArea> | ||
| 19 | 15 | ||
| 20 | #include "Application.h" | 16 | #include "Application.h" |
| 21 | #include "DomainHandler.h" | 17 | #include "DomainHandler.h" |
| 22 | #include "MainWindow.h" | 18 | #include "MainWindow.h" |
| 23 | #include "Menu.h" | 19 | #include "Menu.h" |
| 24 | #include "OffscreenUi.h" | 20 | #include "OffscreenUi.h" |
| 25 | #include "ui/ModelsBrowser.h" | ||
| 26 | #include "WebWindowClass.h" | 21 | #include "WebWindowClass.h" |
| 27 | 22 | ||
| 28 | #include "WindowScriptingInterface.h" | 23 | #include "WindowScriptingInterface.h" |
| @@ -81,131 +76,31 @@ QScriptValue WindowScriptingInterface::getCursorPositionY() { · 52 unmodified lines | |||
| 81 | return QCursor::pos().y(); | 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 | /// Display an alert box | 79 | /// Display an alert box |
| 132 | /// \param const QString& message message to display | 80 | /// \param const QString& message message to display |
| 133 | /// \return QScriptValue::UndefinedValue | 81 | /// \return QScriptValue::UndefinedValue |
| 134 | QScriptValue WindowScriptingInterface::showAlert(const QString& message) { | 82 | void WindowScriptingInterface::alert(const QString& message) { |
| 135 | OffscreenUi::warning("", message); | 83 | OffscreenUi::warning("", message); |
| 136 | return QScriptValue::UndefinedValue; | ||
| 137 | } | 84 | } |
| 138 | 85 | ||
| 139 | /// Display a confirmation box with the options 'Yes' and 'No' | 86 | /// Display a confirmation box with the options 'Yes' and 'No' |
| 140 | /// \param const QString& message message to display | 87 | /// \param const QString& message message to display |
| 141 | /// \return QScriptValue `true` if 'Yes' was clicked, `false` otherwise | 88 | /// \return QScriptValue `true` if 'Yes' was clicked, `false` otherwise |
| 142 | QScriptValue WindowScriptingInterface::showConfirm(const QString& message) { | 89 | QScriptValue WindowScriptingInterface::confirm(const QString& message) { |
| 143 | bool confirm = false; | 90 | return QScriptValue((QMessageBox::Yes == OffscreenUi::question("", message))); |
| 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. | ||
| 186 | } | 91 | } |
| 187 | 92 | ||
| 188 | /// Display a prompt with a text box | 93 | /// Display a prompt with a text box |
| 189 | /// \param const QString& message message to display | 94 | /// \param const QString& message message to display |
| 190 | /// \param const QString& defaultText default text in the text box | 95 | /// \param const QString& defaultText default text in the text box |
| 191 | /// \return QScriptValue string text value in text box if the dialog was accepted, `null` otherwise. | 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 | bool ok = false; | 98 | bool ok = false; |
| 194 | QString result = OffscreenUi::getText(nullptr, "", message, QLineEdit::Normal, defaultText, &ok); | 99 | QString result = OffscreenUi::getText(nullptr, "", message, QLineEdit::Normal, defaultText, &ok); |
| 195 | if (!ok) { | 100 | return ok ? QScriptValue(result) : QScriptValue::NullValue; |
| 196 | return QScriptValue::NullValue; | ||
| 197 | } | ||
| 198 | return QScriptValue(result); | ||
| 199 | } | 101 | } |
| 200 | 102 | ||
| 201 | /// Display a file dialog. If `directory` is an invalid file or directory the browser will start at the current | 103 | QString fixupPathForMac(const QString& directory) { |
| 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) { | ||
| 209 | // On OS X `directory` does not work as expected unless a file is included in the path, so we append a bogus | 104 | // On OS X `directory` does not work as expected unless a file is included in the path, so we append a bogus |
| 210 | // filename if the directory is valid. | 105 | // filename if the directory is valid. |
| 211 | QString path = ""; | 106 | QString path = ""; |
| @@ -214,35 +109,31 @@ QScriptValue WindowScriptingInterface::showBrowse(const QString& title, const QS · 2 unmodified lines | |||
| 214 | fileInfo.setFile(directory, "__HIFI_INVALID_FILE__"); | 109 | fileInfo.setFile(directory, "__HIFI_INVALID_FILE__"); |
| 215 | path = fileInfo.filePath(); | 110 | path = fileInfo.filePath(); |
| 216 | } | 111 | } |
| 217 | 112 | return path; | |
| 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; | ||
| 230 | } | 113 | } |
| 231 | 114 | ||
| 232 | /// Display a browse window for S3 models | 115 | /// Display an open file dialog. If `directory` is an invalid file or directory the browser will start at the current |
| 233 | /// \param const QString& nameFilter filter to filter filenames | 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 | /// \return QScriptValue file path as a string if one was selected, otherwise `QScriptValue::NullValue` | 120 | /// \return QScriptValue file path as a string if one was selected, otherwise `QScriptValue::NullValue` |
| 235 | QScriptValue WindowScriptingInterface::showS3Browse(const QString& nameFilter) { | 121 | QScriptValue WindowScriptingInterface::browse(const QString& title, const QString& directory, const QString& nameFilter) { |
| 236 | ModelsBrowser browser(FSTReader::ENTITY_MODEL); | 122 | QString path = fixupPathForMac(directory); |
| 237 | if (nameFilter != "") { | 123 | QString result = OffscreenUi::getOpenFileName(nullptr, title, path, nameFilter); |
| 238 | browser.setNameFilter(nameFilter); | 124 | return result.isEmpty() ? QScriptValue::NullValue : QScriptValue(result); |
| 239 | } | 125 | } |
| 240 | QEventLoop loop; | 126 | |
| 241 | connect(&browser, &ModelsBrowser::selected, &loop, &QEventLoop::quit); | 127 | /// Display a save file dialog. If `directory` is an invalid file or directory the browser will start at the current |
| 242 | QMetaObject::invokeMethod(&browser, "browse", Qt::QueuedConnection); | 128 | /// working directory. |
| 243 | loop.exec(); | 129 | /// \param const QString& title title of the window |
| 244 | 130 | /// \param const QString& directory directory to start the file browser at | |
| 245 | return browser.getSelectedFile(); | 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 | int WindowScriptingInterface::getInnerWidth() { | 139 | int WindowScriptingInterface::getInnerWidth() { |