History

Add directory picker button option to JavaScript Window.form()

David Rowe committed 12 years ago

Commit 49e0d071 parent eecdc2d2 files · +87 −10Browse files

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

Unified · Split

interface/src/scripting/WindowScriptingInterface.cpp+84−10
@@ -91,9 +91,44 @@ QScriptValue WindowScriptingInterface::showConfirm(const QString& message) { · 90 unmodified lines
91 return QScriptValue(response == QMessageBox::Yes);91 return QScriptValue(response == QMessageBox::Yes);
92}92}
93 93
94void 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
120QString 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/// Display a form layout with an edit box126/// Display a form layout with an edit box
95/// \param const QString& title title to display127/// \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/// \return QScriptValue `true` if 'OK' was clicked, `false` otherwise132/// \return QScriptValue `true` if 'OK' was clicked, `false` otherwise
98QScriptValue WindowScriptingInterface::showForm(const QString& title, QScriptValue form) {133QScriptValue WindowScriptingInterface::showForm(const QString& title, QScriptValue form) {
99 134
@@ -121,11 +156,42 @@ QScriptValue WindowScriptingInterface::showForm(const QString& title, QScriptVal · 21 unmodified lines
121 area->setWidget(container);156 area->setWidget(container);
122 157
123 QVector<QLineEdit*> edits;158 QVector<QLineEdit*> edits;
159 QVector<QPushButton*> directories;
124 for (int i = 0; i < form.property("length").toInt32(); ++i) {160 for (int i = 0; i < form.property("length").toInt32(); ++i) {
125 QScriptValue item = form.property(i);161 QScriptValue item = form.property(i);
126 162
127 if (item.property("button").toString() != "") {163 if (item.property("button").toString() != "") {
128 cancelButton = cancelButton || item.property("button").toString().toLower() == "cancel";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 } else {195 } else {
130 QLineEdit* edit = new QLineEdit(item.property("value").toString());196 QLineEdit* edit = new QLineEdit(item.property("value").toString());
131 edit->setMinimumWidth(200);197 edit->setMinimumWidth(200);
@@ -144,22 +210,30 @@ QScriptValue WindowScriptingInterface::showForm(const QString& title, QScriptVal · 12 unmodified lines
144 210
145 int result = editDialog->exec();211 int result = editDialog->exec();
146 if (result == QDialog::Accepted) {212 if (result == QDialog::Accepted) {
147 int j = -1;213 int e = -1;
214 int d = -1;
148 for (int i = 0; i < form.property("length").toInt32(); ++i) {215 for (int i = 0; i < form.property("length").toInt32(); ++i) {
149 QScriptValue item = form.property(i);216 QScriptValue item = form.property(i);
150 QScriptValue value = item.property("value");217 QScriptValue value = item.property("value");
151 bool ok = true;218
152 qDebug() << "item.property(""button"").toString() = " << item.property("button").toString();219 if (item.property("button").toString() != "") {
153 if (item.property("button").toString() == "") {220 // Nothing to do
154 j += 1;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 if (value.isNumber()) {229 if (value.isNumber()) {
156 value = edits.at(j)->text().toDouble(&ok);230 value = edits.at(e)->text().toDouble(&ok);
157 } else if (value.isString()) {231 } else if (value.isString()) {
158 value = edits.at(j)->text();232 value = edits.at(e)->text();
159 } else if (value.isBool()) {233 } else if (value.isBool()) {
160 if (edits.at(j)->text() == "true") {234 if (edits.at(e)->text() == "true") {
161 value = true;235 value = true;
162 } else if (edits.at(j)->text() == "false") {236 } else if (edits.at(e)->text() == "false") {
163 value = false;237 value = false;
164 } else {238 } else {
165 ok = false;239 ok = false;