History

Add optional Cancel button to JavaScript Window.form()

David Rowe committed 12 years ago

Commit 040254a1 parent 7c6f1ff1 file · +42 −21Browse files

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

Unified · Split

interface/src/scripting/WindowScriptingInterface.cpp+42−21
@@ -94,11 +94,14 @@ QScriptValue WindowScriptingInterface::showConfirm(const QString& message) { · 93 unmodified lines
9494 /// Display a form layout with an edit box
9595 /// \param const QString& title title to display
9696 /// \param const QScriptValue form to display (array containing labels and values)
97−/// \return QScriptValue result form (unchanged is dialog canceled)
97+/// \return QScriptValue `true` if 'OK' was clicked, `false` otherwise
9898 QScriptValue WindowScriptingInterface::showForm(const QString& title, QScriptValue form) {
99+
99100 if (form.isArray() && form.property("length").toInt32() > 0) {
100101 QDialog* editDialog = new QDialog(Application::getInstance()->getWindow());
101102 editDialog->setWindowTitle(title);
103+
104+ bool cancelButton = false;
102105
103106 QVBoxLayout* layout = new QVBoxLayout();
104107 editDialog->setLayout(layout);
@@ -120,42 +123,60 @@ QScriptValue WindowScriptingInterface::showForm(const QString& title, QScriptVal · 15 unmodified lines
120123 QVector<QLineEdit*> edits;
121124 for (int i = 0; i < form.property("length").toInt32(); ++i) {
122125 QScriptValue item = form.property(i);
123− edits.push_back(new QLineEdit(item.property("value").toString()));
124− formLayout->addRow(item.property("label").toString(), edits.back());
126+
127+ if (item.property("button").toString() != "") {
128+ cancelButton = cancelButton || item.property("button").toString().toLower() == "cancel";
129+ } else {
130+ edits.push_back(new QLineEdit(item.property("value").toString()));
131+ formLayout->addRow(item.property("label").toString(), edits.back());
132+ }
125133 }
126− QDialogButtonBox* buttons = new QDialogButtonBox(QDialogButtonBox::Ok);
134+
135+ QDialogButtonBox* buttons = new QDialogButtonBox(
136+ QDialogButtonBox::Ok
137+ | (cancelButton ? QDialogButtonBox::Cancel : QDialogButtonBox::NoButton)
138+ );
127139 connect(buttons, SIGNAL(accepted()), editDialog, SLOT(accept()));
140+ connect(buttons, SIGNAL(rejected()), editDialog, SLOT(reject()));
128141 layout->addWidget(buttons);
129142
130− if (editDialog->exec() == QDialog::Accepted) {
143+ int result = editDialog->exec();
144+ if (result == QDialog::Accepted) {
145+ int j = -1;
131146 for (int i = 0; i < form.property("length").toInt32(); ++i) {
132147 QScriptValue item = form.property(i);
133148 QScriptValue value = item.property("value");
134149 bool ok = true;
135− if (value.isNumber()) {
136− value = edits.at(i)->text().toDouble(&ok);
137− } else if (value.isString()) {
138− value = edits.at(i)->text();
139− } else if (value.isBool()) {
140− if (edits.at(i)->text() == "true") {
141− value = true;
142− } else if (edits.at(i)->text() == "false") {
143− value = false;
144− } else {
145− ok = false;
150+ qDebug() << "item.property(""button"").toString() = " << item.property("button").toString();
151+ if (item.property("button").toString() == "") {
152+ j += 1;
153+ if (value.isNumber()) {
154+ value = edits.at(j)->text().toDouble(&ok);
155+ } else if (value.isString()) {
156+ value = edits.at(j)->text();
157+ } else if (value.isBool()) {
158+ if (edits.at(j)->text() == "true") {
159+ value = true;
160+ } else if (edits.at(j)->text() == "false") {
161+ value = false;
162+ } else {
163+ ok = false;
164+ }
165+ }
166+ if (ok) {
167+ item.setProperty("value", value);
168+ form.setProperty(i, item);
146169 }
147− }
148− if (ok) {
149− item.setProperty("value", value);
150− form.setProperty(i, item);
151170 }
152171 }
153172 }
154173
155174 delete editDialog;
175+
176+ return (result == QDialog::Accepted);
156177 }
157178
158− return form;
179+ return false;
159180 }
160181
161182 /// Display a prompt with a text box