Showing the changes to interface/src/scripting/WindowScriptingInterface.cpp (1 of 3 files). Show all
Unified · Split
| @@ -10,9 +10,11 @@ · 9 unmodified lines | |||
| 10 | // | 10 | // |
| 11 | 11 | ||
| 12 | #include <QDir> | 12 | #include <QDir> |
| 13 | #include <QDialogButtonBox> | ||
| 13 | #include <QFileDialog> | 14 | #include <QFileDialog> |
| 14 | #include <QInputDialog> | 15 | #include <QInputDialog> |
| 15 | #include <QMessageBox> | 16 | #include <QMessageBox> |
| 17 | #include <QScriptValue> | ||
| 16 | 18 | ||
| 17 | #include "Application.h" | 19 | #include "Application.h" |
| 18 | #include "Menu.h" | 20 | #include "Menu.h" |
| @@ -24,6 +26,9 @@ WindowScriptingInterface* WindowScriptingInterface::getInstance() { · 5 unmodified lines | |||
| 24 | return &sharedInstance; | 26 | return &sharedInstance; |
| 25 | } | 27 | } |
| 26 | 28 | ||
| 29 | WindowScriptingInterface::WindowScriptingInterface() { | ||
| 30 | } | ||
| 31 | |||
| 27 | QScriptValue WindowScriptingInterface::alert(const QString& message) { | 32 | QScriptValue WindowScriptingInterface::alert(const QString& message) { |
| 28 | QScriptValue retVal; | 33 | QScriptValue retVal; |
| 29 | QMetaObject::invokeMethod(this, "showAlert", Qt::BlockingQueuedConnection, Q_RETURN_ARG(QScriptValue, retVal), Q_ARG(const QString&, message)); | 34 | QMetaObject::invokeMethod(this, "showAlert", Qt::BlockingQueuedConnection, Q_RETURN_ARG(QScriptValue, retVal), Q_ARG(const QString&, message)); |
| @@ -37,6 +42,14 @@ QScriptValue WindowScriptingInterface::confirm(const QString& message) { · 7 unmodified lines | |||
| 37 | return retVal; | 42 | return retVal; |
| 38 | } | 43 | } |
| 39 | 44 | ||
| 45 | QScriptValue WindowScriptingInterface::arrayEdit(const QString& title, QScriptValue array) { | ||
| 46 | QScriptValue retVal; | ||
| 47 | QMetaObject::invokeMethod(this, "showArrayEdit", Qt::BlockingQueuedConnection, | ||
| 48 | Q_RETURN_ARG(QScriptValue, retVal), | ||
| 49 | Q_ARG(const QString&, title), Q_ARG(QScriptValue, array)); | ||
| 50 | return retVal; | ||
| 51 | } | ||
| 52 | |||
| 40 | QScriptValue WindowScriptingInterface::prompt(const QString& message, const QString& defaultText) { | 53 | QScriptValue WindowScriptingInterface::prompt(const QString& message, const QString& defaultText) { |
| 41 | QScriptValue retVal; | 54 | QScriptValue retVal; |
| 42 | QMetaObject::invokeMethod(this, "showPrompt", Qt::BlockingQueuedConnection, | 55 | QMetaObject::invokeMethod(this, "showPrompt", Qt::BlockingQueuedConnection, |
| @@ -69,6 +82,71 @@ QScriptValue WindowScriptingInterface::showConfirm(const QString& message) { · 26 unmodified lines | |||
| 69 | return QScriptValue(response == QMessageBox::Yes); | 82 | return QScriptValue(response == QMessageBox::Yes); |
| 70 | } | 83 | } |
| 71 | 84 | ||
| 85 | /// Display a prompt with an edit box | ||
| 86 | /// \param const QString& title title to display | ||
| 87 | /// \param const QString& defaultText default text in the text box | ||
| 88 | /// \return QScriptValue string text value in text box if the dialog was accepted, `null` otherwise. | ||
| 89 | QScriptValue WindowScriptingInterface::showArrayEdit(const QString& title, QScriptValue array) { | ||
| 90 | if (array.isArray() && array.property("length").toInt32() > 0) { | ||
| 91 | QDialog* editDialog = new QDialog(Application::getInstance()->getWindow()); | ||
| 92 | editDialog->setWindowTitle(title); | ||
| 93 | |||
| 94 | QVBoxLayout* layout = new QVBoxLayout(); | ||
| 95 | editDialog->setLayout(layout); | ||
| 96 | |||
| 97 | QScrollArea* area = new QScrollArea(); | ||
| 98 | layout->addWidget(area); | ||
| 99 | area->setWidgetResizable(true); | ||
| 100 | QWidget* container = new QWidget(); | ||
| 101 | QFormLayout* arrayLayout = new QFormLayout(); | ||
| 102 | container->setLayout(arrayLayout); | ||
| 103 | container->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Preferred); | ||
| 104 | area->setWidget(container); | ||
| 105 | |||
| 106 | QVector<QLineEdit*> edits; | ||
| 107 | for (int i = 0; i < array.property("length").toInt32(); ++i) { | ||
| 108 | QScriptValue item = array.property(i); | ||
| 109 | edits.push_back(new QLineEdit(item.property("value").toString())); | ||
| 110 | arrayLayout->addRow(item.property("label").toString(), | ||
| 111 | edits.back()); | ||
| 112 | } | ||
| 113 | QDialogButtonBox* buttons = new QDialogButtonBox(QDialogButtonBox::Ok); | ||
| 114 | connect(buttons, SIGNAL(accepted()), editDialog, SLOT(accept())); | ||
| 115 | layout->addWidget(buttons); | ||
| 116 | |||
| 117 | editDialog->setMinimumSize(600, 600); | ||
| 118 | |||
| 119 | if (editDialog->exec() == QDialog::Accepted) { | ||
| 120 | for (int i = 0; i < array.property("length").toInt32(); ++i) { | ||
| 121 | QScriptValue item = array.property(i); | ||
| 122 | QScriptValue value = item.property("value"); | ||
| 123 | bool ok = true; | ||
| 124 | if (value.isNumber()) { | ||
| 125 | value = edits.at(i)->text().toDouble(&ok); | ||
| 126 | } else if (value.isString()) { | ||
| 127 | value = edits.at(i)->text(); | ||
| 128 | } else if (value.isBool()) { | ||
| 129 | if (edits.at(i)->text() == "true") { | ||
| 130 | value = true; | ||
| 131 | } else if (edits.at(i)->text() == "false") { | ||
| 132 | value = false; | ||
| 133 | } else { | ||
| 134 | ok = false; | ||
| 135 | } | ||
| 136 | } | ||
| 137 | if (ok) { | ||
| 138 | item.setProperty("value", value); | ||
| 139 | array.setProperty(i, item); | ||
| 140 | } | ||
| 141 | } | ||
| 142 | } | ||
| 143 | |||
| 144 | delete editDialog; | ||
| 145 | } | ||
| 146 | |||
| 147 | return array; | ||
| 148 | } | ||
| 149 | |||
| 72 | /// Display a prompt with a text box | 150 | /// Display a prompt with a text box |
| 73 | /// \param const QString& message message to display | 151 | /// \param const QString& message message to display |
| 74 | /// \param const QString& defaultText default text in the text box | 152 | /// \param const QString& defaultText default text in the text box |
| @@ -79,11 +157,11 @@ QScriptValue WindowScriptingInterface::showPrompt(const QString& message, const · 4 unmodified lines | |||
| 79 | promptDialog.setLabelText(message); | 157 | promptDialog.setLabelText(message); |
| 80 | promptDialog.setTextValue(defaultText); | 158 | promptDialog.setTextValue(defaultText); |
| 81 | promptDialog.setFixedSize(600, 200); | 159 | promptDialog.setFixedSize(600, 200); |
| 82 | 160 | ||
| 83 | if (promptDialog.exec() == QDialog::Accepted) { | 161 | if (promptDialog.exec() == QDialog::Accepted) { |
| 84 | return QScriptValue(promptDialog.textValue()); | 162 | return QScriptValue(promptDialog.textValue()); |
| 85 | } | 163 | } |
| 86 | 164 | ||
| 87 | return QScriptValue::NullValue; | 165 | return QScriptValue::NullValue; |
| 88 | } | 166 | } |
| 89 | 167 | ||