Add updateable message box to JavaScript API

This commit is contained in:
David Rowe 2017-01-12 15:46:53 +13:00
parent 9bdcdf73c1
commit b41f8c754d
2 changed files with 88 additions and 0 deletions

View file

@ -60,6 +60,19 @@ WindowScriptingInterface::WindowScriptingInterface() {
});
}
WindowScriptingInterface::~WindowScriptingInterface() {
QHashIterator<int, QQuickItem*> 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<OffscreenUi>()->createMessageBox(OffscreenUi::ICON_INFORMATION, title, text,
static_cast<QFlags<QMessageBox::StandardButton>>(buttons), static_cast<QMessageBox::StandardButton>(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<QQuickItem*>(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);
}
}

View file

@ -14,7 +14,9 @@
#include <QtCore/QObject>
#include <QtCore/QString>
#include <QtQuick/QQuickItem>
#include <QtScript/QScriptValue>
#include <QtWidgets/QMessageBox>
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<int, QQuickItem*> _messageBoxes;
int _lastMessageBoxID{ -1 };
};
#endif // hifi_WindowScriptingInterface_h