History

Add updateable message box to JavaScript API

David Rowe committed 9 years ago

Commit b41f8c71 parent 9bdcdf72 files · +88 −0Browse files

Showing the changes to interface/src/scripting/WindowScriptingInterface.cpp (1 of 2 files). Show all

Unified · Split

interface/src/scripting/WindowScriptingInterface.cpp+72−0
@@ -60,6 +60,19 @@ WindowScriptingInterface::WindowScriptingInterface() { · 59 unmodified lines
6060 });
6161 }
6262
63+WindowScriptingInterface::~WindowScriptingInterface() {
64+ QHashIterator<int, QQuickItem*> i(_messageBoxes);
65+ while (i.hasNext()) {
66+ i.next();
67+ auto messageBox = i.value();
68+ disconnect(messageBox);
69+ messageBox->setVisible(false);
70+ messageBox->deleteLater();
71+ }
72+
73+ _messageBoxes.clear();
74+}
75+
6376 QScriptValue WindowScriptingInterface::hasFocus() {
6477 return qApp->hasFocus();
6578 }
@@ -210,3 +223,62 @@ void WindowScriptingInterface::shareSnapshot(const QString& path, const QUrl& hr · 144 unmodified lines
210223 bool WindowScriptingInterface::isPhysicsEnabled() {
211224 return qApp->isPhysicsEnabled();
212225 }
226+
227+int WindowScriptingInterface::openMessageBox(QString title, QString text, int buttons, int defaultButton) {
228+ if (QThread::currentThread() != thread()) {
229+ int result;
230+ QMetaObject::invokeMethod(this, "openMessageBox", Qt::BlockingQueuedConnection,
231+ Q_RETURN_ARG(int, result),
232+ Q_ARG(QString, title),
233+ Q_ARG(QString, text),
234+ Q_ARG(int, buttons),
235+ Q_ARG(int, defaultButton));
236+ return result;
237+ }
238+
239+ return createMessageBox(title, text, buttons, defaultButton);
240+}
241+
242+int WindowScriptingInterface::createMessageBox(QString title, QString text, int buttons, int defaultButton) {
243+ auto messageBox = DependencyManager::get<OffscreenUi>()->createMessageBox(OffscreenUi::ICON_INFORMATION, title, text,
244+ static_cast<QFlags<QMessageBox::StandardButton>>(buttons), static_cast<QMessageBox::StandardButton>(defaultButton));
245+ connect(messageBox, SIGNAL(selected(int)), this, SLOT(onMessageBoxSelected(int)));
246+
247+ _lastMessageBoxID += 1;
248+ _messageBoxes.insert(_lastMessageBoxID, messageBox);
249+
250+ return _lastMessageBoxID;
251+}
252+
253+void WindowScriptingInterface::updateMessageBox(int id, QString title, QString text, int buttons, int defaultButton) {
254+ auto messageBox = _messageBoxes.value(id);
255+ if (messageBox) {
256+ messageBox->setProperty("title", title);
257+ messageBox->setProperty("text", text);
258+ messageBox->setProperty("buttons", buttons);
259+ messageBox->setProperty("defaultButton", defaultButton);
260+ }
261+}
262+
263+void WindowScriptingInterface::closeMessageBox(int id) {
264+ auto messageBox = _messageBoxes.value(id);
265+ if (messageBox) {
266+ disconnect(messageBox);
267+ messageBox->setVisible(false);
268+ messageBox->deleteLater();
269+ _messageBoxes.remove(id);
270+ }
271+}
272+
273+void WindowScriptingInterface::onMessageBoxSelected(int button) {
274+ auto messageBox = qobject_cast<QQuickItem*>(sender());
275+ auto keys = _messageBoxes.keys(messageBox);
276+ if (keys.length() > 0) {
277+ auto id = keys[0]; // Should be just one message box.
278+ emit messageBoxClosed(id, button);
279+ disconnect(messageBox);
280+ messageBox->setVisible(false);
281+ messageBox->deleteLater();
282+ _messageBoxes.remove(id);
283+ }
284+}