Showing the changes to interface/src/scripting/WindowScriptingInterface.cpp (1 of 2 files). Show all
Unified · Split
| @@ -91,9 +91,44 @@ QScriptValue WindowScriptingInterface::showConfirm(const QString& message) { · 90 unmodified lines | ||
| 91 | 91 | return QScriptValue(response == QMessageBox::Yes); |
| 92 | 92 | } |
| 93 | 93 | |
| 94 | +void WindowScriptingInterface::chooseDirectory() { | |
| 95 | + QPushButton* button = reinterpret_cast<QPushButton*>(sender()); | |
| 96 | + | |
| 97 | + QString title = button->property("title").toString(); | |
| 98 | + QString path = button->property("path").toString(); | |
| 99 | + QRegExp displayAs = button->property("displayAs").toRegExp(); | |
| 100 | + QRegExp validateAs = button->property("validateAs").toRegExp(); | |
| 101 | + QString errorMessage = button->property("errorMessage").toString(); | |
| 102 | + | |
| 103 | + QString directory = QFileDialog::getExistingDirectory(button, title, path); | |
| 104 | + if (directory.isEmpty()) { | |
| 105 | + return; | |
| 106 | + } | |
| 107 | + | |
| 108 | + if (!validateAs.exactMatch(directory)) { | |
| 109 | + QMessageBox::warning(NULL, "Invalid Directory", errorMessage); | |
| 110 | + return; | |
| 111 | + } | |
| 112 | + | |
| 113 | + button->setProperty("path", directory); | |
| 114 | + | |
| 115 | + displayAs.indexIn(directory); | |
| 116 | + QString buttonText = displayAs.cap(1) != "" ? displayAs.cap(1) : "."; | |
| 117 | + button->setText(buttonText); | |
| 118 | +} | |
| 119 | + | |
| 120 | +QString WindowScriptingInterface::jsRegExp2QtRegExp(QString string) { | |
| 121 | + // Converts string representation of RegExp from JavaScript format to Qt format. | |
| 122 | + return string.mid(1, string.length() - 2) // No enclosing slashes. | |
| 123 | + .replace("\\/", "/"); // No escaping of forward slash. | |
| 124 | +} | |
| 125 | + | |
| 94 | 126 | /// Display a form layout with an edit box |
| 95 | 127 | /// \param const QString& title title to display |
| 96 | −/// \param const QScriptValue form to display (array containing labels and values) | |
| 128 | +/// \param const QScriptValue form to display as an array of objects: | |
| 129 | +/// - label, value | |
| 130 | +/// - label, directory, title, display regexp, validate regexp, error message | |
| 131 | +/// - button ("Cancel") | |
| 97 | 132 | /// \return QScriptValue `true` if 'OK' was clicked, `false` otherwise |
| 98 | 133 | QScriptValue WindowScriptingInterface::showForm(const QString& title, QScriptValue form) { |
| 99 | 134 | |
| @@ -121,11 +156,42 @@ QScriptValue WindowScriptingInterface::showForm(const QString& title, QScriptVal · 21 unmodified lines | ||
| 121 | 156 | area->setWidget(container); |
| 122 | 157 | |
| 123 | 158 | QVector<QLineEdit*> edits; |
| 159 | + QVector<QPushButton*> directories; | |
| 124 | 160 | for (int i = 0; i < form.property("length").toInt32(); ++i) { |
| 125 | 161 | QScriptValue item = form.property(i); |
| 126 | 162 | |
| 127 | 163 | if (item.property("button").toString() != "") { |
| 128 | 164 | cancelButton = cancelButton || item.property("button").toString().toLower() == "cancel"; |
| 165 | + | |
| 166 | + } else if (item.property("directory").toString() != "") { | |
| 167 | + QString path = item.property("directory").toString(); | |
| 168 | + QString title = item.property("title").toString(); | |
| 169 | + if (title == "") { | |
| 170 | + title = "Choose Directory"; | |
| 171 | + } | |
| 172 | + QString displayAsString = item.property("displayAs").toString(); | |
| 173 | + QRegExp displayAs = QRegExp(displayAsString != "" ? jsRegExp2QtRegExp(displayAsString) : "^(.*)$"); | |
| 174 | + QString validateAsString = item.property("validateAs").toString(); | |
| 175 | + QRegExp validateAs = QRegExp(validateAsString != "" ? jsRegExp2QtRegExp(validateAsString) : ".*"); | |
| 176 | + QString errorMessage = item.property("errorMessage").toString(); | |
| 177 | + if (errorMessage == "") { | |
| 178 | + errorMessage = "Invalid directory"; | |
| 179 | + } | |
| 180 | + | |
| 181 | + QPushButton* directory = new QPushButton(displayAs.cap(1)); | |
| 182 | + directory->setProperty("title", title); | |
| 183 | + directory->setProperty("path", path); | |
| 184 | + directory->setProperty("displayAs", displayAs); | |
| 185 | + directory->setProperty("validateAs", validateAs); | |
| 186 | + directory->setProperty("errorMessage", errorMessage); | |
| 187 | + directory->setText(displayAs.cap(1) != "" ? displayAs.cap(1) : "."); | |
| 188 | + | |
| 189 | + directory->setMinimumWidth(200); | |
| 190 | + directories.push_back(directory); | |
| 191 | + | |
| 192 | + formLayout->addRow(new QLabel(item.property("label").toString()), directory); | |
| 193 | + connect(directory, SIGNAL(clicked(bool)), SLOT(chooseDirectory())); | |
| 194 | + | |
| 129 | 195 | } else { |
| 130 | 196 | QLineEdit* edit = new QLineEdit(item.property("value").toString()); |
| 131 | 197 | edit->setMinimumWidth(200); |
| @@ -144,22 +210,30 @@ QScriptValue WindowScriptingInterface::showForm(const QString& title, QScriptVal · 12 unmodified lines | ||
| 144 | 210 | |
| 145 | 211 | int result = editDialog->exec(); |
| 146 | 212 | if (result == QDialog::Accepted) { |
| 147 | − int j = -1; | |
| 213 | + int e = -1; | |
| 214 | + int d = -1; | |
| 148 | 215 | for (int i = 0; i < form.property("length").toInt32(); ++i) { |
| 149 | 216 | QScriptValue item = form.property(i); |
| 150 | 217 | QScriptValue value = item.property("value"); |
| 151 | − bool ok = true; | |
| 152 | − qDebug() << "item.property(""button"").toString() = " << item.property("button").toString(); | |
| 153 | − if (item.property("button").toString() == "") { | |
| 154 | − j += 1; | |
| 218 | + | |
| 219 | + if (item.property("button").toString() != "") { | |
| 220 | + // Nothing to do | |
| 221 | + } else if (item.property("directory").toString() != "") { | |
| 222 | + d += 1; | |
| 223 | + value = directories.at(d)->property("path").toString(); | |
| 224 | + item.setProperty("directory", value); | |
| 225 | + form.setProperty(i, item); | |
| 226 | + } else { | |
| 227 | + e += 1; | |
| 228 | + bool ok = true; | |
| 155 | 229 | if (value.isNumber()) { |
| 156 | − value = edits.at(j)->text().toDouble(&ok); | |
| 230 | + value = edits.at(e)->text().toDouble(&ok); | |
| 157 | 231 | } else if (value.isString()) { |
| 158 | − value = edits.at(j)->text(); | |
| 232 | + value = edits.at(e)->text(); | |
| 159 | 233 | } else if (value.isBool()) { |
| 160 | − if (edits.at(j)->text() == "true") { | |
| 234 | + if (edits.at(e)->text() == "true") { | |
| 161 | 235 | value = true; |
| 162 | − } else if (edits.at(j)->text() == "false") { | |
| 236 | + } else if (edits.at(e)->text() == "false") { | |
| 163 | 237 | value = false; |
| 164 | 238 | } else { |
| 165 | 239 | ok = false; |