Showing the changes to interface/src/scripting/WindowScriptingInterface.cpp (1 of 404 files). Show all
Unified · Split
| @@ -122,14 +122,52 @@ QScriptValue WindowScriptingInterface::showConfirm(const QString& message) { · 121 unmodified lines | ||
| 122 | 122 | return QScriptValue(response == QMessageBox::Yes); |
| 123 | 123 | } |
| 124 | 124 | |
| 125 | +void WindowScriptingInterface::chooseDirectory() { | |
| 126 | + QPushButton* button = reinterpret_cast<QPushButton*>(sender()); | |
| 127 | + | |
| 128 | + QString title = button->property("title").toString(); | |
| 129 | + QString path = button->property("path").toString(); | |
| 130 | + QRegExp displayAs = button->property("displayAs").toRegExp(); | |
| 131 | + QRegExp validateAs = button->property("validateAs").toRegExp(); | |
| 132 | + QString errorMessage = button->property("errorMessage").toString(); | |
| 133 | + | |
| 134 | + QString directory = QFileDialog::getExistingDirectory(button, title, path); | |
| 135 | + if (directory.isEmpty()) { | |
| 136 | + return; | |
| 137 | + } | |
| 138 | + | |
| 139 | + if (!validateAs.exactMatch(directory)) { | |
| 140 | + QMessageBox::warning(NULL, "Invalid Directory", errorMessage); | |
| 141 | + return; | |
| 142 | + } | |
| 143 | + | |
| 144 | + button->setProperty("path", directory); | |
| 145 | + | |
| 146 | + displayAs.indexIn(directory); | |
| 147 | + QString buttonText = displayAs.cap(1) != "" ? displayAs.cap(1) : "."; | |
| 148 | + button->setText(buttonText); | |
| 149 | +} | |
| 150 | + | |
| 151 | +QString WindowScriptingInterface::jsRegExp2QtRegExp(QString string) { | |
| 152 | + // Converts string representation of RegExp from JavaScript format to Qt format. | |
| 153 | + return string.mid(1, string.length() - 2) // No enclosing slashes. | |
| 154 | + .replace("\\/", "/"); // No escaping of forward slash. | |
| 155 | +} | |
| 156 | + | |
| 125 | 157 | /// Display a form layout with an edit box |
| 126 | 158 | /// \param const QString& title title to display |
| 127 | −/// \param const QScriptValue form to display (array containing labels and values) | |
| 128 | −/// \return QScriptValue result form (unchanged is dialog canceled) | |
| 159 | +/// \param const QScriptValue form to display as an array of objects: | |
| 160 | +/// - label, value | |
| 161 | +/// - label, directory, title, display regexp, validate regexp, error message | |
| 162 | +/// - button ("Cancel") | |
| 163 | +/// \return QScriptValue `true` if 'OK' was clicked, `false` otherwise | |
| 129 | 164 | QScriptValue WindowScriptingInterface::showForm(const QString& title, QScriptValue form) { |
| 165 | + | |
| 130 | 166 | if (form.isArray() && form.property("length").toInt32() > 0) { |
| 131 | 167 | QDialog* editDialog = new QDialog(Application::getInstance()->getWindow()); |
| 132 | 168 | editDialog->setWindowTitle(title); |
| 169 | + | |
| 170 | + bool cancelButton = false; | |
| 133 | 171 | |
| 134 | 172 | QVBoxLayout* layout = new QVBoxLayout(); |
| 135 | 173 | editDialog->setLayout(layout); |
| @@ -149,44 +187,104 @@ QScriptValue WindowScriptingInterface::showForm(const QString& title, QScriptVal · 13 unmodified lines | ||
| 149 | 187 | area->setWidget(container); |
| 150 | 188 | |
| 151 | 189 | QVector<QLineEdit*> edits; |
| 190 | + QVector<QPushButton*> directories; | |
| 152 | 191 | for (int i = 0; i < form.property("length").toInt32(); ++i) { |
| 153 | 192 | QScriptValue item = form.property(i); |
| 154 | − edits.push_back(new QLineEdit(item.property("value").toString())); | |
| 155 | − formLayout->addRow(item.property("label").toString(), edits.back()); | |
| 193 | + | |
| 194 | + if (item.property("button").toString() != "") { | |
| 195 | + cancelButton = cancelButton || item.property("button").toString().toLower() == "cancel"; | |
| 196 | + | |
| 197 | + } else if (item.property("directory").toString() != "") { | |
| 198 | + QString path = item.property("directory").toString(); | |
| 199 | + QString title = item.property("title").toString(); | |
| 200 | + if (title == "") { | |
| 201 | + title = "Choose Directory"; | |
| 202 | + } | |
| 203 | + QString displayAsString = item.property("displayAs").toString(); | |
| 204 | + QRegExp displayAs = QRegExp(displayAsString != "" ? jsRegExp2QtRegExp(displayAsString) : "^(.*)$"); | |
| 205 | + QString validateAsString = item.property("validateAs").toString(); | |
| 206 | + QRegExp validateAs = QRegExp(validateAsString != "" ? jsRegExp2QtRegExp(validateAsString) : ".*"); | |
| 207 | + QString errorMessage = item.property("errorMessage").toString(); | |
| 208 | + if (errorMessage == "") { | |
| 209 | + errorMessage = "Invalid directory"; | |
| 210 | + } | |
| 211 | + | |
| 212 | + QPushButton* directory = new QPushButton(displayAs.cap(1)); | |
| 213 | + directory->setProperty("title", title); | |
| 214 | + directory->setProperty("path", path); | |
| 215 | + directory->setProperty("displayAs", displayAs); | |
| 216 | + directory->setProperty("validateAs", validateAs); | |
| 217 | + directory->setProperty("errorMessage", errorMessage); | |
| 218 | + displayAs.indexIn(path); | |
| 219 | + directory->setText(displayAs.cap(1) != "" ? displayAs.cap(1) : "."); | |
| 220 | + | |
| 221 | + directory->setMinimumWidth(200); | |
| 222 | + directories.push_back(directory); | |
| 223 | + | |
| 224 | + formLayout->addRow(new QLabel(item.property("label").toString()), directory); | |
| 225 | + connect(directory, SIGNAL(clicked(bool)), SLOT(chooseDirectory())); | |
| 226 | + | |
| 227 | + } else { | |
| 228 | + QLineEdit* edit = new QLineEdit(item.property("value").toString()); | |
| 229 | + edit->setMinimumWidth(200); | |
| 230 | + edits.push_back(edit); | |
| 231 | + formLayout->addRow(new QLabel(item.property("label").toString()), edit); | |
| 232 | + } | |
| 156 | 233 | } |
| 157 | − QDialogButtonBox* buttons = new QDialogButtonBox(QDialogButtonBox::Ok); | |
| 234 | + | |
| 235 | + QDialogButtonBox* buttons = new QDialogButtonBox( | |
| 236 | + QDialogButtonBox::Ok | |
| 237 | + | (cancelButton ? QDialogButtonBox::Cancel : QDialogButtonBox::NoButton) | |
| 238 | + ); | |
| 158 | 239 | connect(buttons, SIGNAL(accepted()), editDialog, SLOT(accept())); |
| 240 | + connect(buttons, SIGNAL(rejected()), editDialog, SLOT(reject())); | |
| 159 | 241 | layout->addWidget(buttons); |
| 160 | 242 | |
| 161 | − if (editDialog->exec() == QDialog::Accepted) { | |
| 243 | + int result = editDialog->exec(); | |
| 244 | + if (result == QDialog::Accepted) { | |
| 245 | + int e = -1; | |
| 246 | + int d = -1; | |
| 162 | 247 | for (int i = 0; i < form.property("length").toInt32(); ++i) { |
| 163 | 248 | QScriptValue item = form.property(i); |
| 164 | 249 | QScriptValue value = item.property("value"); |
| 165 | − bool ok = true; | |
| 166 | − if (value.isNumber()) { | |
| 167 | − value = edits.at(i)->text().toDouble(&ok); | |
| 168 | − } else if (value.isString()) { | |
| 169 | − value = edits.at(i)->text(); | |
| 170 | − } else if (value.isBool()) { | |
| 171 | − if (edits.at(i)->text() == "true") { | |
| 172 | − value = true; | |
| 173 | − } else if (edits.at(i)->text() == "false") { | |
| 174 | − value = false; | |
| 175 | − } else { | |
| 176 | − ok = false; | |
| 177 | − } | |
| 178 | − } | |
| 179 | − if (ok) { | |
| 180 | − item.setProperty("value", value); | |
| 250 | + | |
| 251 | + if (item.property("button").toString() != "") { | |
| 252 | + // Nothing to do | |
| 253 | + } else if (item.property("directory").toString() != "") { | |
| 254 | + d += 1; | |
| 255 | + value = directories.at(d)->property("path").toString(); | |
| 256 | + item.setProperty("directory", value); | |
| 181 | 257 | form.setProperty(i, item); |
| 258 | + } else { | |
| 259 | + e += 1; | |
| 260 | + bool ok = true; | |
| 261 | + if (value.isNumber()) { | |
| 262 | + value = edits.at(e)->text().toDouble(&ok); | |
| 263 | + } else if (value.isString()) { | |
| 264 | + value = edits.at(e)->text(); | |
| 265 | + } else if (value.isBool()) { | |
| 266 | + if (edits.at(e)->text() == "true") { | |
| 267 | + value = true; | |
| 268 | + } else if (edits.at(e)->text() == "false") { | |
| 269 | + value = false; | |
| 270 | + } else { | |
| 271 | + ok = false; | |
| 272 | + } | |
| 273 | + } | |
| 274 | + if (ok) { | |
| 275 | + item.setProperty("value", value); | |
| 276 | + form.setProperty(i, item); | |
| 277 | + } | |
| 182 | 278 | } |
| 183 | 279 | } |
| 184 | 280 | } |
| 185 | 281 | |
| 186 | 282 | delete editDialog; |
| 283 | + | |
| 284 | + return (result == QDialog::Accepted); | |
| 187 | 285 | } |
| 188 | 286 | |
| 189 | − return form; | |
| 287 | + return false; | |
| 190 | 288 | } |
| 191 | 289 | |
| 192 | 290 | /// Display a prompt with a text box |
| @@ -219,7 +317,6 @@ QScriptValue WindowScriptingInterface::showBrowse(const QString& title, const QS · 26 unmodified lines | ||
| 219 | 317 | // filename if the directory is valid. |
| 220 | 318 | QString path = ""; |
| 221 | 319 | QFileInfo fileInfo = QFileInfo(directory); |
| 222 | − qDebug() << "File: " << directory << fileInfo.isFile(); | |
| 223 | 320 | if (fileInfo.isDir()) { |
| 224 | 321 | fileInfo.setFile(directory, "__HIFI_INVALID_FILE__"); |
| 225 | 322 | path = fileInfo.filePath(); |
| @@ -227,7 +324,6 @@ QScriptValue WindowScriptingInterface::showBrowse(const QString& title, const QS · 1 unmodified line | ||
| 227 | 324 | |
| 228 | 325 | QFileDialog fileDialog(Application::getInstance()->getWindow(), title, path, nameFilter); |
| 229 | 326 | fileDialog.setAcceptMode(acceptMode); |
| 230 | − qDebug() << "Opening!"; | |
| 231 | 327 | QUrl fileUrl(directory); |
| 232 | 328 | if (acceptMode == QFileDialog::AcceptSave) { |
| 233 | 329 | fileDialog.setFileMode(QFileDialog::Directory); |