Showing the changes to interface/src/scripting/WindowScriptingInterface.cpp (1 of 4 files). Show all
Unified · Split
| @@ -0,0 +1,85 @@ | |||
| 1 | // | ||
| 2 | // WindowScriptingInterface.cpp | ||
| 3 | // interface/src/scripting | ||
| 4 | // | ||
| 5 | // Created by Ryan Huffman on 4/29/14. | ||
| 6 | // Copyright 2014 High Fidelity, Inc. | ||
| 7 | // | ||
| 8 | // Distributed under the Apache License, Version 2.0. | ||
| 9 | // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html | ||
| 10 | // | ||
| 11 | |||
| 12 | #include <QMessageBox> | ||
| 13 | #include <QInputDialog> | ||
| 14 | |||
| 15 | #include "Application.h" | ||
| 16 | #include "Menu.h" | ||
| 17 | |||
| 18 | #include "WindowScriptingInterface.h" | ||
| 19 | |||
| 20 | WindowScriptingInterface* WindowScriptingInterface::getInstance() { | ||
| 21 | static WindowScriptingInterface sharedInstance; | ||
| 22 | return &sharedInstance; | ||
| 23 | } | ||
| 24 | |||
| 25 | QScriptValue WindowScriptingInterface::alert(const QString& message) { | ||
| 26 | QScriptValue retVal; | ||
| 27 | QMetaObject::invokeMethod(this, "showAlert", Qt::BlockingQueuedConnection, Q_RETURN_ARG(QScriptValue, retVal), Q_ARG(const QString&, message)); | ||
| 28 | return retVal; | ||
| 29 | } | ||
| 30 | |||
| 31 | QScriptValue WindowScriptingInterface::confirm(const QString& message) { | ||
| 32 | bool retVal; | ||
| 33 | QMetaObject::invokeMethod(this, "showConfirm", Qt::BlockingQueuedConnection, | ||
| 34 | Q_RETURN_ARG(bool, retVal), Q_ARG(const QString&, message)); | ||
| 35 | return QScriptValue(retVal); | ||
| 36 | } | ||
| 37 | |||
| 38 | QScriptValue WindowScriptingInterface::prompt(const QString& message, const QString& defaultText) { | ||
| 39 | QScriptValue retVal; | ||
| 40 | QMetaObject::invokeMethod(this, "showPrompt", Qt::BlockingQueuedConnection, | ||
| 41 | Q_RETURN_ARG(QScriptValue, retVal), | ||
| 42 | Q_ARG(const QString&, message), Q_ARG(const QString&, defaultText)); | ||
| 43 | return retVal; | ||
| 44 | } | ||
| 45 | |||
| 46 | /// Display an alert box | ||
| 47 | /// \param const QString& message message to display | ||
| 48 | /// \return QScriptValue::UndefinedValue | ||
| 49 | QScriptValue WindowScriptingInterface::showAlert(const QString& message) { | ||
| 50 | QMessageBox::warning(Application::getInstance()->getWindow(), "", message); | ||
| 51 | return QScriptValue::UndefinedValue; | ||
| 52 | } | ||
| 53 | |||
| 54 | /// Display a confirmation box with the options 'Yes' and 'No' | ||
| 55 | /// \param const QString& message message to display | ||
| 56 | /// \return QScriptValue `true` if 'Yes' was clicked, `false` otherwise | ||
| 57 | QScriptValue WindowScriptingInterface::showConfirm(const QString& message) { | ||
| 58 | QMessageBox::StandardButton response = QMessageBox::question(Application::getInstance()->getWindow(), "", message); | ||
| 59 | return QScriptValue(response == QMessageBox::Yes); | ||
| 60 | } | ||
| 61 | |||
| 62 | /// Display a prompt with a text box | ||
| 63 | /// \param const QString& message message to display | ||
| 64 | /// \param const QString& defaultText default text in the text box | ||
| 65 | /// \return QScriptValue string text value in text box if the dialog was accepted, `null` otherwise. | ||
| 66 | QScriptValue WindowScriptingInterface::showPrompt(const QString& message, const QString& defaultText) { | ||
| 67 | QInputDialog promptDialog(Application::getInstance()->getWindow()); | ||
| 68 | promptDialog.setWindowTitle(""); | ||
| 69 | promptDialog.setLabelText(message); | ||
| 70 | promptDialog.setTextValue(defaultText); | ||
| 71 | |||
| 72 | if (promptDialog.exec() == QDialog::Accepted) { | ||
| 73 | return QScriptValue(promptDialog.textValue()); | ||
| 74 | } | ||
| 75 | |||
| 76 | return QScriptValue::NullValue; | ||
| 77 | } | ||
| 78 | |||
| 79 | int WindowScriptingInterface::getInnerWidth() { | ||
| 80 | return Application::getInstance()->getWindow()->geometry().width(); | ||
| 81 | } | ||
| 82 | |||
| 83 | int WindowScriptingInterface::getInnerHeight() { | ||
| 84 | return Application::getInstance()->getWindow()->geometry().height(); | ||
| 85 | } | ||