From b41f8c754d2742fa3395a9d1c7f43d8d948280a7 Mon Sep 17 00:00:00 2001 From: David Rowe Date: Thu, 12 Jan 2017 15:46:53 +1300 Subject: [PATCH] Add updateable message box to JavaScript API --- .../scripting/WindowScriptingInterface.cpp | 72 +++++++++++++++++++ .../src/scripting/WindowScriptingInterface.h | 16 +++++ 2 files changed, 88 insertions(+) diff --git a/interface/src/scripting/WindowScriptingInterface.cpp b/interface/src/scripting/WindowScriptingInterface.cpp index 765c9a8499..7cfbfb174e 100644 --- a/interface/src/scripting/WindowScriptingInterface.cpp +++ b/interface/src/scripting/WindowScriptingInterface.cpp @@ -60,6 +60,19 @@ WindowScriptingInterface::WindowScriptingInterface() { }); } +WindowScriptingInterface::~WindowScriptingInterface() { + QHashIterator i(_messageBoxes); + while (i.hasNext()) { + i.next(); + auto messageBox = i.value(); + disconnect(messageBox); + messageBox->setVisible(false); + messageBox->deleteLater(); + } + + _messageBoxes.clear(); +} + QScriptValue WindowScriptingInterface::hasFocus() { return qApp->hasFocus(); } @@ -210,3 +223,62 @@ void WindowScriptingInterface::shareSnapshot(const QString& path, const QUrl& hr bool WindowScriptingInterface::isPhysicsEnabled() { return qApp->isPhysicsEnabled(); } + +int WindowScriptingInterface::openMessageBox(QString title, QString text, int buttons, int defaultButton) { + if (QThread::currentThread() != thread()) { + int result; + QMetaObject::invokeMethod(this, "openMessageBox", Qt::BlockingQueuedConnection, + Q_RETURN_ARG(int, result), + Q_ARG(QString, title), + Q_ARG(QString, text), + Q_ARG(int, buttons), + Q_ARG(int, defaultButton)); + return result; + } + + return createMessageBox(title, text, buttons, defaultButton); +} + +int WindowScriptingInterface::createMessageBox(QString title, QString text, int buttons, int defaultButton) { + auto messageBox = DependencyManager::get()->createMessageBox(OffscreenUi::ICON_INFORMATION, title, text, + static_cast>(buttons), static_cast(defaultButton)); + connect(messageBox, SIGNAL(selected(int)), this, SLOT(onMessageBoxSelected(int))); + + _lastMessageBoxID += 1; + _messageBoxes.insert(_lastMessageBoxID, messageBox); + + return _lastMessageBoxID; +} + +void WindowScriptingInterface::updateMessageBox(int id, QString title, QString text, int buttons, int defaultButton) { + auto messageBox = _messageBoxes.value(id); + if (messageBox) { + messageBox->setProperty("title", title); + messageBox->setProperty("text", text); + messageBox->setProperty("buttons", buttons); + messageBox->setProperty("defaultButton", defaultButton); + } +} + +void WindowScriptingInterface::closeMessageBox(int id) { + auto messageBox = _messageBoxes.value(id); + if (messageBox) { + disconnect(messageBox); + messageBox->setVisible(false); + messageBox->deleteLater(); + _messageBoxes.remove(id); + } +} + +void WindowScriptingInterface::onMessageBoxSelected(int button) { + auto messageBox = qobject_cast(sender()); + auto keys = _messageBoxes.keys(messageBox); + if (keys.length() > 0) { + auto id = keys[0]; // Should be just one message box. + emit messageBoxClosed(id, button); + disconnect(messageBox); + messageBox->setVisible(false); + messageBox->deleteLater(); + _messageBoxes.remove(id); + } +} diff --git a/interface/src/scripting/WindowScriptingInterface.h b/interface/src/scripting/WindowScriptingInterface.h index 6aa8707496..4652e00661 100644 --- a/interface/src/scripting/WindowScriptingInterface.h +++ b/interface/src/scripting/WindowScriptingInterface.h @@ -14,7 +14,9 @@ #include #include +#include #include +#include class CustomPromptResult { public: @@ -35,6 +37,7 @@ class WindowScriptingInterface : public QObject, public Dependency { Q_PROPERTY(int y READ getY) public: WindowScriptingInterface(); + ~WindowScriptingInterface(); int getInnerWidth(); int getInnerHeight(); int getX(); @@ -56,6 +59,13 @@ public slots: void shareSnapshot(const QString& path, const QUrl& href = QUrl("")); bool isPhysicsEnabled(); + int openMessageBox(QString title, QString text, int buttons, int defaultButton); + void updateMessageBox(int id, QString title, QString text, int buttons, int defaultButton); + void closeMessageBox(int id); + +private slots: + void onMessageBoxSelected(int button); + signals: void domainChanged(const QString& domainHostname); void svoImportRequested(const QString& url); @@ -64,9 +74,15 @@ signals: void snapshotShared(const QString& error); void processingGif(); + void messageBoxClosed(int id, int button); + private: QString getPreviousBrowseLocation() const; void setPreviousBrowseLocation(const QString& location); + + int createMessageBox(QString title, QString text, int buttons, int defaultButton); + QHash _messageBoxes; + int _lastMessageBoxID{ -1 }; }; #endif // hifi_WindowScriptingInterface_h