| 1 |
/****************************************************************************** |
| 2 |
This source file is part of the Avogadro project. |
| 3 |
This source code is released under the 3-Clause BSD License, (see "LICENSE"). |
| 4 |
******************************************************************************/ |
| 5 |
|
| 6 |
#include "jsonwidget.h" |
| 7 |
|
| 8 |
#include <avogadro/qtgui/filebrowsewidget.h> |
| 9 |
#include <avogadro/qtgui/molecule.h> |
| 10 |
|
| 11 |
#include <QtWidgets/QCheckBox> |
| 12 |
#include <QtWidgets/QComboBox> |
| 13 |
#include <QtWidgets/QDoubleSpinBox> |
| 14 |
#include <QtWidgets/QFileDialog> |
| 15 |
#include <QtWidgets/QLabel> |
| 16 |
#include <QtWidgets/QLineEdit> |
| 17 |
#include <QtWidgets/QMessageBox> |
| 18 |
#include <QtWidgets/QSpinBox> |
| 19 |
#include <QtWidgets/QTableWidget> |
| 20 |
#include <QtWidgets/QTextBrowser> |
| 21 |
#include <QtWidgets/QTextEdit> |
| 22 |
|
| 23 |
#include <QtCore/QDebug> |
| 24 |
#include <QtCore/QJsonArray> |
| 25 |
#include <QtCore/QJsonDocument> |
| 26 |
#include <QtCore/QPointer> |
| 27 |
#include <QtCore/QSettings> |
| 28 |
#include <QtCore/QTimer> |
| 29 |
|
| 30 |
#include <QRegularExpression> |
| 31 |
|
| 32 |
using namespace Qt::StringLiterals; |
| 33 |
|
| 34 |
namespace Avogadro::QtGui { |
| 35 |
|
| 36 |
JsonWidget::JsonWidget(QWidget* parent_) |
| 37 |
: QWidget(parent_), m_molecule(nullptr), m_empty(true), m_batchMode(false), |
| 38 |
m_currentLayout(nullptr), m_centralWidget(nullptr) |
| 39 |
{ |
| 40 |
} |
| 41 |
|
| 42 |
JsonWidget::~JsonWidget() {} |
| 43 |
|
| 44 |
void JsonWidget::setMolecule(QtGui::Molecule* mol) |
| 45 |
{ |
| 46 |
if (m_molecule != nullptr) { |
| 47 |
// update charge and multiplicity only if those options exist |
| 48 |
// (command scripts don't have them, unlike input generators) |
| 49 |
auto hasUserOption = [&](const QString& key) -> bool { |
| 50 |
if (!m_options.contains(u"userOptions"_s)) |
| 51 |
return false; |
| 52 |
return m_options[u"userOptions"_s].toObject().contains(key); |
| 53 |
}; |
| 54 |
|
| 55 |
int charge = static_cast<int>(m_molecule->totalCharge()); |
| 56 |
int multiplicity = static_cast<int>(m_molecule->totalSpinMultiplicity()); |
| 57 |
|
| 58 |
if (hasUserOption(u"Charge"_s)) |
| 59 |
setOption(u"Charge"_s, charge); |
| 60 |
if (hasUserOption(u"Multiplicity"_s)) |
| 61 |
setOption(u"Multiplicity"_s, multiplicity); |
| 62 |
|
| 63 |
// check the molecule for "inputParameters" from CJSON |
| 64 |
// e.g. |
| 65 |
// https://github.com/OpenChemistry/chemicaljson/blob/main/chemicaljson.py#L130 |
| 66 |
if (m_molecule->hasData("inputParameters")) { |
| 67 |
QByteArray inputData( |
| 68 |
m_molecule->data("inputParameters").toString().c_str()); |
| 69 |
QJsonDocument doc = QJsonDocument::fromJson(inputData); |
| 70 |
if (!doc.isNull() && doc.isObject()) { |
| 71 |
QJsonObject inputParameters = doc.object(); |
| 72 |
// check for a few known keys |
| 73 |
if (inputParameters.contains(u"processors"_s)) |
| 74 |
setOption(u"Processor Cores"_s, |
| 75 |
inputParameters[u"processors"_s].toInt()); |
| 76 |
else if (inputParameters.contains(u"memory"_s)) |
| 77 |
setOption(u"Memory"_s, inputParameters[u"memory"_s].toInt()); |
| 78 |
else if (inputParameters.contains(u"basis"_s)) |
| 79 |
setOption(u"Basis"_s, inputParameters[u"basis"_s].toString()); |
| 80 |
else if (inputParameters.contains(u"functional"_s)) |
| 81 |
setOption(u"Theory"_s, inputParameters[u"functional"_s].toString()); |
| 82 |
else if (inputParameters.contains(u"task"_s)) |
| 83 |
setOption(u"Calculation Type"_s, |
| 84 |
inputParameters[u"task"_s].toString()); |
| 85 |
} |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
if (mol == m_molecule) |
| 90 |
return; |
| 91 |
|
| 92 |
if (m_molecule) |
| 93 |
m_molecule->disconnect(this); |
| 94 |
|
| 95 |
m_molecule = mol; |
| 96 |
} |
| 97 |
|
| 98 |
QString JsonWidget::lookupOptionType(const QString& name) const |
| 99 |
{ |
| 100 |
if (!m_options.contains(u"userOptions"_s)) { |
| 101 |
qWarning() << tr("'userOptions' missing."); |
| 102 |
return QString(); |
| 103 |
} |
| 104 |
|
| 105 |
QJsonObject userOptions = m_options[u"userOptions"_s].toObject(); |
| 106 |
|
| 107 |
if (!userOptions.contains(name)) { |
| 108 |
qWarning() << tr("Could not find option '%1'.").arg(name); |
| 109 |
return QString(); |
| 110 |
} |
| 111 |
|
| 112 |
QJsonObject obj = userOptions[name].toObject(); |
| 113 |
|
| 114 |
if (!obj.contains(u"type"_s) || !obj[u"type"_s].isString()) { |
| 115 |
qWarning() << tr("'type' is not a string for option '%1'.").arg(name); |
| 116 |
return QString(); |
| 117 |
} |
| 118 |
|
| 119 |
return obj[u"type"_s].toString(); |
| 120 |
} |
| 121 |
|
| 122 |
void JsonWidget::updateOptions() |
| 123 |
{ |
| 124 |
// Create the widgets, etc for the gui |
| 125 |
if (!m_centralWidget) { |
| 126 |
m_centralWidget = this; |
| 127 |
} |
| 128 |
buildOptionGui(); |
| 129 |
setOptionDefaults(); |
| 130 |
} |
| 131 |
|
| 132 |
void JsonWidget::buildOptionGui() |
| 133 |
{ |
| 134 |
if (m_centralWidget == nullptr) |
| 135 |
return; |
| 136 |
|
| 137 |
m_widgets.clear(); |
| 138 |
delete m_centralWidget->layout(); |
| 139 |
|
| 140 |
if (!m_options.contains(u"userOptions"_s)) { |
| 141 |
return; |
| 142 |
} |
| 143 |
|
| 144 |
// Always expect an object now, should never be an array |
| 145 |
if (m_options[u"userOptions"_s].isArray()) { |
| 146 |
return; |
| 147 |
} |
| 148 |
QJsonObject userOptions = m_options[u"userOptions"_s].toObject(); |
| 149 |
|
| 150 |
// If a tabbed interface is specified, we'll create tabs for it |
| 151 |
QTabWidget* tabsWidget = nullptr; |
| 152 |
QWidget* currentPage = nullptr; |
| 153 |
|
| 154 |
// First work out whether a tabbed interface is specified |
| 155 |
bool isTabbed; |
| 156 |
if (userOptions.contains(u"tabs"_s)) { |
| 157 |
isTabbed = true; |
| 158 |
} else { |
| 159 |
// Interface doesn't have tabs at all |
| 160 |
isTabbed = false; |
| 161 |
} |
| 162 |
m_isTabbed = isTabbed; |
| 163 |
|
| 164 |
QJsonArray tabs; |
| 165 |
QMap<QString, QJsonObject> sortedOptions; |
| 166 |
// We need to store the tab names in order of position |
| 167 |
if (isTabbed) { |
| 168 |
tabs = userOptions.take(u"tabs"_s).toArray(); |
| 169 |
|
| 170 |
// Sort the options by tab |
| 171 |
for (auto it = tabs.constBegin(); it != tabs.constEnd(); ++it) { |
| 172 |
QString tabName = it->toString(); |
| 173 |
sortedOptions.insert(tabName, QJsonObject()); |
| 174 |
} |
| 175 |
// Iterate over all options |
| 176 |
for (auto it = userOptions.constBegin(); it != userOptions.constEnd(); |
| 177 |
++it) { |
| 178 |
if (!it.value().isObject()) |
| 179 |
continue; |
| 180 |
QJsonObject obj = it.value().toObject(); |
| 181 |
QString tab = obj[u"tab"_s].toString(); |
| 182 |
if (sortedOptions.contains(tab)) { |
| 183 |
QJsonObject& tabObj = sortedOptions[tab]; |
| 184 |
tabObj.insert(it.key(), it.value()); |
| 185 |
} |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
// Lambda to add a set of options to the current layout |
| 190 |
auto addOptions = [this](QJsonObject options) { |
| 191 |
// Title first |
| 192 |
if (options.contains(u"Title"_s)) |
| 193 |
addOptionRow(u"Title"_s, tr("Title"), options.take(u"Title"_s)); |
| 194 |
|
| 195 |
// File basename next: |
| 196 |
if (options.contains(u"Filename Base"_s)) |
| 197 |
addOptionRow(u"Filename Base"_s, tr("Filename Base"), |
| 198 |
options.take(u"Filename Base"_s)); |
| 199 |
|
| 200 |
// Number of cores and memory next: |
| 201 |
if (options.contains(u"Processor Cores"_s) && |
| 202 |
options.contains(u"Memory"_s)) { |
| 203 |
combinedOptionRow(u"Processor Cores"_s, u"Memory"_s, |
| 204 |
tr("Processor Cores"), tr("Memory"), options, |
| 205 |
true); // both labels |
| 206 |
} else { |
| 207 |
// do them separately |
| 208 |
if (options.contains(u"Processor Cores"_s)) |
| 209 |
addOptionRow(u"Processor Cores"_s, tr("Processor Cores"), |
| 210 |
options.take(u"Processor Cores"_s)); |
| 211 |
if (options.contains(u"Memory"_s)) |
| 212 |
addOptionRow(u"Memory"_s, tr("Memory"), options.take(u"Memory"_s)); |
| 213 |
} |
| 214 |
|
| 215 |
// Calculation Type next: |
| 216 |
if (options.contains(u"Calculation Type"_s)) |
| 217 |
addOptionRow(u"Calculation Type"_s, tr("Calculation Type"), |
| 218 |
options.take(u"Calculation Type"_s)); |
| 219 |
|
| 220 |
// Theory/basis next. Combine into one row if both present. |
| 221 |
combinedOptionRow(u"Theory"_s, u"Basis"_s, tr("Theory"), tr("Basis"), |
| 222 |
options); |
| 223 |
|
| 224 |
// Other special cases: Charge / Multiplicity |
| 225 |
if (options.contains(u"Charge"_s) && options.contains(u"Multiplicity"_s)) |
| 226 |
combinedOptionRow(u"Charge"_s, u"Multiplicity"_s, tr("Charge"), |
| 227 |
tr("Multiplicity"), options, true); // both labels |
| 228 |
else { |
| 229 |
if (options.contains(u"Charge"_s)) |
| 230 |
addOptionRow(u"Charge"_s, tr("Charge"), options.take(u"Charge"_s)); |
| 231 |
if (options.contains(u"Multiplicity"_s)) |
| 232 |
addOptionRow(u"Multiplicity"_s, tr("Multiplicity"), |
| 233 |
options.take(u"Multiplicity"_s)); |
| 234 |
} |
| 235 |
|
| 236 |
// Solvation / model |
| 237 |
if (options.contains(u"Solvent"_s) && |
| 238 |
options.contains(u"Solvation Model"_s)) { |
| 239 |
combinedOptionRow(u"Solvent"_s, u"Solvation Model"_s, tr("Solvent"), |
| 240 |
tr("Model", "solvation method / model"), options, |
| 241 |
true); // both labels |
| 242 |
} |
| 243 |
|
| 244 |
// Add remaining keys at bottom. |
| 245 |
// Look for "order" key to determine order |
| 246 |
QMap<int, QString> keys; |
| 247 |
int order = 0; |
| 248 |
for (QJsonObject::const_iterator it = options.constBegin(), |
| 249 |
itEnd = options.constEnd(); |
| 250 |
it != itEnd; ++it) { |
| 251 |
if (it.value().isObject()) { |
| 252 |
QJsonObject obj = it.value().toObject(); |
| 253 |
if (obj.contains(u"order"_s) && obj[u"order"_s].isDouble()) { |
| 254 |
order = obj[u"order"_s].toInt(); |
| 255 |
keys.insert(order, it.key()); |
| 256 |
} else { // object doesn't contain "order" |
| 257 |
keys.insert(order, it.key()); |
| 258 |
order++; |
| 259 |
} |
| 260 |
} else { |
| 261 |
keys.insert(order++, it.key()); |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
// Now loop over keys and add them |
| 266 |
for (QString key : std::as_const(keys)) |
| 267 |
addOptionRow(key, key, options.take(key)); |
| 268 |
|
| 269 |
// Make connections for standard options: |
| 270 |
if (auto* combo = qobject_cast<QComboBox*>( |
| 271 |
m_widgets.value(u"Calculation Type"_s, nullptr))) { |
| 272 |
connect(combo, SIGNAL(currentIndexChanged(int)), |
| 273 |
SLOT(updateTitlePlaceholder())); |
| 274 |
} |
| 275 |
if (auto* combo = |
| 276 |
qobject_cast<QComboBox*>(m_widgets.value(u"Theory"_s, nullptr))) { |
| 277 |
connect(combo, SIGNAL(currentIndexChanged(int)), |
| 278 |
SLOT(updateTitlePlaceholder())); |
| 279 |
} |
| 280 |
if (auto* combo = |
| 281 |
qobject_cast<QComboBox*>(m_widgets.value(u"Basis"_s, nullptr))) { |
| 282 |
connect(combo, SIGNAL(currentIndexChanged(int)), |
| 283 |
SLOT(updateTitlePlaceholder())); |
| 284 |
} |
| 285 |
}; |
| 286 |
|
| 287 |
// Create new widgets using the lambda |
| 288 |
if (isTabbed) { |
| 289 |
// Create a layout for inserting the tabs |
| 290 |
tabsWidget = new QTabWidget(this); |
| 291 |
auto* layout = new QVBoxLayout; |
| 292 |
layout->addWidget(tabsWidget); |
| 293 |
m_centralWidget->setLayout(layout); |
| 294 |
// Loop over the tabs, which are the top-level key/value pairs |
| 295 |
for (auto it = tabs.constBegin(); it != tabs.constEnd(); ++it) { |
| 296 |
QString tabName = it->toString(); |
| 297 |
QJsonObject tabOptions = sortedOptions.value(tabName); |
| 298 |
// Add the new tab |
| 299 |
currentPage = new QWidget(this); |
| 300 |
auto* tabLayout = new QFormLayout(currentPage); |
| 301 |
currentPage->setLayout(tabLayout); |
| 302 |
// Make the created tab the current layout so that the lambda adds the |
| 303 |
// tab's options to it |
| 304 |
m_currentLayout = tabLayout; |
| 305 |
addOptions(tabOptions); |
| 306 |
tabsWidget->addTab(currentPage, tabName); |
| 307 |
} // End loop over tabs |
| 308 |
} else { |
| 309 |
// Create the form layout for the widgets |
| 310 |
auto* layout = new QFormLayout; |
| 311 |
m_currentLayout = layout; |
| 312 |
m_centralWidget->setLayout(layout); |
| 313 |
// Options are just the top level |
| 314 |
addOptions(userOptions); |
| 315 |
} |
| 316 |
|
| 317 |
m_empty = m_widgets.isEmpty(); |
| 318 |
} |
| 319 |
|
| 320 |
void JsonWidget::combinedOptionRow(const QString& label1, const QString& label2, |
| 321 |
const QString& tr1, const QString& tr2, |
| 322 |
QJsonObject& options, bool bothLabels) |
| 323 |
{ |
| 324 |
if (m_currentLayout == nullptr) |
| 325 |
return; |
| 326 |
|
| 327 |
bool option1 = options.contains(label1); |
| 328 |
bool option2 = options.contains(label2); |
| 329 |
if (option1 && option2) { |
| 330 |
QWidget* widget1 = createOptionWidget(options.take(label1)); |
| 331 |
QWidget* widget2 = createOptionWidget(options.take(label2)); |
| 332 |
auto* hbox = new QHBoxLayout; |
| 333 |
if (option1) { |
| 334 |
widget1->setObjectName(label1); |
| 335 |
hbox->addWidget(widget1); |
| 336 |
m_widgets.insert(label1, widget1); |
| 337 |
} |
| 338 |
if (bothLabels) { |
| 339 |
QLabel* label = new QLabel(tr2 + ":"); |
| 340 |
hbox->addWidget(label); |
| 341 |
} |
| 342 |
if (option2) { |
| 343 |
widget2->setObjectName(label2); |
| 344 |
hbox->addWidget(widget2); |
| 345 |
m_widgets.insert(label2, widget2); |
| 346 |
} |
| 347 |
hbox->addStretch(); |
| 348 |
|
| 349 |
m_currentLayout->addRow(tr1, hbox); |
| 350 |
} else { |
| 351 |
if (option1) |
| 352 |
addOptionRow(label1, tr1, options.take(label1)); |
| 353 |
if (option2) |
| 354 |
addOptionRow(label2, tr2, options.take(label2)); |
| 355 |
} |
| 356 |
} |
| 357 |
|
| 358 |
void JsonWidget::addOptionRow(const QString& key, const QString& name, |
| 359 |
const QJsonValue& option) |
| 360 |
{ |
| 361 |
QWidget* widget = createOptionWidget(option); |
| 362 |
if (!widget) |
| 363 |
return; |
| 364 |
|
| 365 |
QFormLayout* form = m_currentLayout; |
| 366 |
if (!form) { |
| 367 |
qWarning() << "Cannot add option" << name |
| 368 |
<< "to GUI -- layout is not a form."; |
| 369 |
widget->deleteLater(); |
| 370 |
return; |
| 371 |
} |
| 372 |
|
| 373 |
// For lookups during unit testing: |
| 374 |
widget->setObjectName(key); |
| 375 |
QString label(name); |
| 376 |
|
| 377 |
QJsonObject obj = option.toObject(); |
| 378 |
|
| 379 |
if (obj.contains(u"label"_s) && obj[u"label"_s].isString()) { |
| 380 |
label = obj[u"label"_s].toString(); |
| 381 |
} |
| 382 |
|
| 383 |
// also check for "User Name" or "Password" for translation |
| 384 |
// with case-insensitive comparison |
| 385 |
if (label.toLower() == u"user name"_s || label.toLower() == u"username"_s) |
| 386 |
label = tr("User Name"); |
| 387 |
else if (label.toLower() == u"password"_s) { |
| 388 |
label = tr("Password"); |
| 389 |
// make sure the widget has the right echo |
| 390 |
if (auto* lineEdit = qobject_cast<QLineEdit*>(widget)) { |
| 391 |
lineEdit->setEchoMode(QLineEdit::PasswordEchoOnEdit); |
| 392 |
} |
| 393 |
} |
| 394 |
|
| 395 |
form->addRow(label + ":", widget); |
| 396 |
m_widgets.insert(key, widget); |
| 397 |
|
| 398 |
// optionally hide rows .. can be shown by the script later |
| 399 |
bool hide = false; |
| 400 |
if (obj.contains(u"hide"_s) && obj[u"hide"_s].isBool()) { |
| 401 |
hide = obj[u"hide"_s].toBool(); |
| 402 |
} |
| 403 |
if (hide) { |
| 404 |
widget->hide(); |
| 405 |
// find the label and hide that too |
| 406 |
auto fLabel = form->labelForField(widget); |
| 407 |
if (fLabel) |
| 408 |
fLabel->hide(); |
| 409 |
} |
| 410 |
} |
| 411 |
|
| 412 |
QWidget* JsonWidget::createOptionWidget(const QJsonValue& option) |
| 413 |
{ |
| 414 |
if (!option.isObject()) |
| 415 |
return nullptr; |
| 416 |
|
| 417 |
QJsonObject obj = option.toObject(); |
| 418 |
|
| 419 |
if (!obj.contains(u"type"_s) || !obj[u"type"_s].isString()) |
| 420 |
return nullptr; |
| 421 |
|
| 422 |
QString type = obj[u"type"_s].toString(); |
| 423 |
|
| 424 |
if (type == u"stringList"_s) |
| 425 |
return createStringListWidget(obj); |
| 426 |
else if (type == u"string"_s) |
| 427 |
return createStringWidget(obj); |
| 428 |
else if (type == u"filePath"_s) |
| 429 |
return createFilePathWidget(obj); |
| 430 |
else if (type == u"integer"_s) |
| 431 |
return createIntegerWidget(obj); |
| 432 |
else if (type == u"float"_s) |
| 433 |
return createFloatWidget(obj); |
| 434 |
else if (type == u"boolean"_s) |
| 435 |
return createBooleanWidget(obj); |
| 436 |
else if (type == u"text"_s) |
| 437 |
return createTextWidget(obj); |
| 438 |
else if (type == u"table"_s) |
| 439 |
return createTableWidget(obj); |
| 440 |
|
| 441 |
qDebug() << "Unrecognized option type:" << type; |
| 442 |
return nullptr; |
| 443 |
} |
| 444 |
|
| 445 |
QWidget* JsonWidget::createStringListWidget(const QJsonObject& obj) |
| 446 |
{ |
| 447 |
if (!obj.contains(u"values"_s) || !obj[u"values"_s].isArray()) { |
| 448 |
qDebug() << "JsonWidget::createStringListWidget()" |
| 449 |
"values missing, or not array!"; |
| 450 |
return nullptr; |
| 451 |
} |
| 452 |
|
| 453 |
QJsonArray valueArray = obj[u"values"_s].toArray(); |
| 454 |
|
| 455 |
auto* combo = new QComboBox(this); |
| 456 |
for (QJsonArray::const_iterator vit = valueArray.constBegin(), |
| 457 |
vitEnd = valueArray.constEnd(); |
| 458 |
vit != vitEnd; ++vit) { |
| 459 |
if ((*vit).isString()) { |
| 460 |
QString value = (*vit).toString(); |
| 461 |
if (value == '-') |
| 462 |
combo->insertSeparator(combo->count()); |
| 463 |
else |
| 464 |
combo->addItem((*vit).toString()); |
| 465 |
} else |
| 466 |
qDebug() << "Cannot convert value to string for stringList:" << *vit; |
| 467 |
} |
| 468 |
connect(combo, SIGNAL(currentIndexChanged(int)), SLOT(updatePreviewText())); |
| 469 |
|
| 470 |
if (obj.contains(u"toolTip"_s) && obj[u"toolTip"_s].isString()) { |
| 471 |
combo->setToolTip(obj[u"toolTip"_s].toString()); |
| 472 |
} |
| 473 |
|
| 474 |
return combo; |
| 475 |
} |
| 476 |
|
| 477 |
QWidget* JsonWidget::createStringWidget(const QJsonObject& obj) |
| 478 |
{ |
| 479 |
auto* edit = new QLineEdit(this); |
| 480 |
connect(edit, SIGNAL(textChanged(QString)), SLOT(updatePreviewText())); |
| 481 |
if (obj.contains(u"toolTip"_s) && obj[u"toolTip"_s].isString()) { |
| 482 |
edit->setToolTip(obj[u"toolTip"_s].toString()); |
| 483 |
} |
| 484 |
if (obj.contains(u"placeholderText"_s) && |
| 485 |
obj[u"placeholderText"_s].isString()) { |
| 486 |
edit->setPlaceholderText(obj[u"placeholderText"_s].toString()); |
| 487 |
} |
| 488 |
// don't echo password fields |
| 489 |
if (obj.contains(u"password"_s) && obj[u"password"_s].isBool() && |
| 490 |
obj[u"password"_s].toBool()) { |
| 491 |
edit->setEchoMode(QLineEdit::PasswordEchoOnEdit); |
| 492 |
} |
| 493 |
|
| 494 |
return edit; |
| 495 |
} |
| 496 |
|
| 497 |
QWidget* JsonWidget::createTextWidget(const QJsonObject& obj) |
| 498 |
{ |
| 499 |
auto* text = new QLabel(this); |
| 500 |
text->setWordWrap(true); |
| 501 |
|
| 502 |
if (obj.contains(u"toolTip"_s) && obj[u"toolTip"_s].isString()) { |
| 503 |
text->setToolTip(obj[u"toolTip"_s].toString()); |
| 504 |
} |
| 505 |
|
| 506 |
return text; |
| 507 |
} |
| 508 |
|
| 509 |
QWidget* JsonWidget::createFilePathWidget(const QJsonObject& obj) |
| 510 |
{ |
| 511 |
auto* fileBrowse = new QtGui::FileBrowseWidget(this); |
| 512 |
connect(fileBrowse, SIGNAL(fileNameChanged(QString)), |
| 513 |
SLOT(updatePreviewText())); |
| 514 |
|
| 515 |
if (obj.contains(u"toolTip"_s) && obj[u"toolTip"_s].isString()) { |
| 516 |
fileBrowse->setToolTip(obj[u"toolTip"_s].toString()); |
| 517 |
} |
| 518 |
return fileBrowse; |
| 519 |
} |
| 520 |
|
| 521 |
QWidget* JsonWidget::createIntegerWidget(const QJsonObject& obj) |
| 522 |
{ |
| 523 |
auto* spin = new QSpinBox(this); |
| 524 |
if (obj.contains(u"minimum"_s) && obj[u"minimum"_s].isDouble()) { |
| 525 |
spin->setMinimum(static_cast<int>(obj[u"minimum"_s].toDouble())); |
| 526 |
} |
| 527 |
if (obj.contains(u"maximum"_s) && obj[u"maximum"_s].isDouble()) { |
| 528 |
spin->setMaximum(static_cast<int>(obj[u"maximum"_s].toDouble())); |
| 529 |
} |
| 530 |
if (obj.contains(u"prefix"_s) && obj[u"prefix"_s].isString()) { |
| 531 |
spin->setPrefix(obj[u"prefix"_s].toString()); |
| 532 |
} |
| 533 |
if (obj.contains(u"suffix"_s) && obj[u"suffix"_s].isString()) { |
| 534 |
spin->setSuffix(obj[u"suffix"_s].toString()); |
| 535 |
} |
| 536 |
if (obj.contains(u"toolTip"_s) && obj[u"toolTip"_s].isString()) { |
| 537 |
spin->setToolTip(obj[u"toolTip"_s].toString()); |
| 538 |
} |
| 539 |
connect(spin, SIGNAL(valueChanged(int)), SLOT(updatePreviewText())); |
| 540 |
return spin; |
| 541 |
} |
| 542 |
|
| 543 |
QWidget* JsonWidget::createFloatWidget(const QJsonObject& obj) |
| 544 |
{ |
| 545 |
auto* spin = new QDoubleSpinBox(this); |
| 546 |
if (obj.contains(u"minimum"_s) && obj[u"minimum"_s].isDouble()) { |
| 547 |
spin->setMinimum(obj[u"minimum"_s].toDouble()); |
| 548 |
} |
| 549 |
if (obj.contains(u"maximum"_s) && obj[u"maximum"_s].isDouble()) { |
| 550 |
spin->setMaximum(obj[u"maximum"_s].toDouble()); |
| 551 |
} |
| 552 |
if (obj.contains(u"precision"_s) && obj[u"precision"_s].isDouble()) { |
| 553 |
spin->setDecimals(static_cast<int>(obj[u"precision"_s].toDouble())); |
| 554 |
} |
| 555 |
if (obj.contains(u"prefix"_s) && obj[u"prefix"_s].isString()) { |
| 556 |
spin->setPrefix(obj[u"prefix"_s].toString()); |
| 557 |
} |
| 558 |
if (obj.contains(u"suffix"_s) && obj[u"suffix"_s].isString()) { |
| 559 |
spin->setSuffix(obj[u"suffix"_s].toString()); |
| 560 |
} |
| 561 |
if (obj.contains(u"toolTip"_s) && obj[u"toolTip"_s].isString()) { |
| 562 |
spin->setToolTip(obj[u"toolTip"_s].toString()); |
| 563 |
} |
| 564 |
connect(spin, SIGNAL(valueChanged(double)), SLOT(updatePreviewText())); |
| 565 |
return spin; |
| 566 |
} |
| 567 |
|
| 568 |
QWidget* JsonWidget::createBooleanWidget(const QJsonObject& obj) |
| 569 |
{ |
| 570 |
auto* checkBox = new QCheckBox(this); |
| 571 |
connect(checkBox, SIGNAL(toggled(bool)), SLOT(updatePreviewText())); |
| 572 |
|
| 573 |
if (obj.contains(u"toolTip"_s) && obj[u"toolTip"_s].isString()) { |
| 574 |
checkBox->setToolTip(obj[u"toolTip"_s].toString()); |
| 575 |
} |
| 576 |
return checkBox; |
| 577 |
} |
| 578 |
|
| 579 |
QWidget* JsonWidget::createTableWidget(const QJsonObject& obj) |
| 580 |
{ |
| 581 |
auto* tableWidget = new QTableWidget(this); |
| 582 |
connect(tableWidget, SIGNAL(cellChanged(int, int)), |
| 583 |
SLOT(updatePreviewText())); |
| 584 |
|
| 585 |
if (obj.contains(u"toolTip"_s) && obj[u"toolTip"_s].isString()) { |
| 586 |
tableWidget->setToolTip(obj[u"toolTip"_s].toString()); |
| 587 |
} |
| 588 |
if (obj.contains(u"headers"_s) && obj[u"headers"_s].isArray()) { |
| 589 |
QJsonArray headers = obj[u"headers"_s].toArray(); |
| 590 |
tableWidget->setColumnCount(headers.size()); |
| 591 |
for (int i = 0; i < headers.size(); ++i) { |
| 592 |
tableWidget->setHorizontalHeaderItem( |
| 593 |
i, new QTableWidgetItem(headers[i].toString())); |
| 594 |
} |
| 595 |
} |
| 596 |
if (obj.contains(u"delimiter"_s) && obj[u"delimiter"_s].isString()) { |
| 597 |
tableWidget->setProperty("delimiter", obj[u"delimiter"_s].toString()); |
| 598 |
} |
| 599 |
|
| 600 |
// data might be supplied as columns or rows |
| 601 |
if (obj.contains(u"columns"_s) && obj[u"columns"_s].isArray()) { |
| 602 |
QJsonArray columns = obj[u"columns"_s].toArray(); |
| 603 |
// get the row count from the first column |
| 604 |
tableWidget->setRowCount(columns[0].toArray().size()); |
| 605 |
for (int i = 0; i < columns.size(); ++i) { |
| 606 |
int j = 0; |
| 607 |
for (QJsonArray::const_iterator it = columns[i].toArray().constBegin(), |
| 608 |
itEnd = columns[i].toArray().constEnd(); |
| 609 |
it != itEnd; ++it) { |
| 610 |
tableWidget->setItem(i, j++, new QTableWidgetItem(it->toString())); |
| 611 |
} |
| 612 |
} |
| 613 |
} |
| 614 |
if (obj.contains(u"rows"_s) && obj[u"rows"_s].isArray()) { |
| 615 |
QJsonArray rows = obj[u"rows"_s].toArray(); |
| 616 |
// get the column count from the first row |
| 617 |
tableWidget->setColumnCount(rows[0].toArray().size()); |
| 618 |
for (int j = 0; j < rows.size(); ++j) { |
| 619 |
int i = 0; |
| 620 |
for (QJsonArray::const_iterator it = rows[i].toArray().constBegin(), |
| 621 |
itEnd = rows[i].toArray().constEnd(); |
| 622 |
it != itEnd; ++it) { |
| 623 |
tableWidget->setItem(i++, j, new QTableWidgetItem(it->toString())); |
| 624 |
} |
| 625 |
} |
| 626 |
} |
| 627 |
|
| 628 |
return tableWidget; |
| 629 |
} |
| 630 |
|
| 631 |
void JsonWidget::setOptionDefaults() |
| 632 |
{ |
| 633 |
if (!m_options.contains(u"userOptions"_s)) { |
| 634 |
return; |
| 635 |
} |
| 636 |
QJsonObject userOptions = m_options[u"userOptions"_s].toObject(); |
| 637 |
// Remove those keys that aren't for options |
| 638 |
userOptions.take(u"tabs"_s); |
| 639 |
|
| 640 |
// Loop over all options |
| 641 |
for (auto it = userOptions.constBegin(); it != userOptions.constEnd(); ++it) { |
| 642 |
QString label = it.key(); |
| 643 |
QJsonObject obj = it.value().toObject(); |
| 644 |
|
| 645 |
if (obj.contains(u"default"_s)) { |
| 646 |
// TODO - check QSettings for a value too |
| 647 |
setOption(label, obj[u"default"_s]); |
| 648 |
} |
| 649 |
} |
| 650 |
} |
| 651 |
|
| 652 |
void JsonWidget::setOption(const QString& name, const QJsonValue& defaultValue) |
| 653 |
{ |
| 654 |
QString type = lookupOptionType(name); |
| 655 |
|
| 656 |
if (type == u"stringList"_s) |
| 657 |
return setStringListOption(name, defaultValue); |
| 658 |
else if (type == u"string"_s) |
| 659 |
return setStringOption(name, defaultValue); |
| 660 |
else if (type == u"filePath"_s) |
| 661 |
return setFilePathOption(name, defaultValue); |
| 662 |
else if (type == u"integer"_s) |
| 663 |
return setIntegerOption(name, defaultValue); |
| 664 |
else if (type == u"float"_s) |
| 665 |
return setFloatOption(name, defaultValue); |
| 666 |
else if (type == u"boolean"_s) |
| 667 |
return setBooleanOption(name, defaultValue); |
| 668 |
else if (type == u"text"_s) |
| 669 |
return setTextOption(name, defaultValue); |
| 670 |
else if (type == u"table"_s) |
| 671 |
return setTableOption(name, defaultValue); |
| 672 |
|
| 673 |
qWarning() |
| 674 |
<< tr("Unrecognized option type '%1' for option '%2'.").arg(type).arg(name); |
| 675 |
return; |
| 676 |
} |
| 677 |
|
| 678 |
void JsonWidget::setStringListOption(const QString& name, |
| 679 |
const QJsonValue& value) |
| 680 |
{ |
| 681 |
auto* combo = qobject_cast<QComboBox*>(m_widgets.value(name, nullptr)); |
| 682 |
if (!combo) { |
| 683 |
qWarning() << tr("Error setting default for option '%1'. " |
| 684 |
"Bad widget type.") |
| 685 |
.arg(name); |
| 686 |
return; |
| 687 |
} |
| 688 |
|
| 689 |
if (!value.isDouble() && !value.isString()) { |
| 690 |
qWarning() << tr("Error setting default for option '%1'. " |
| 691 |
"Bad default value:") |
| 692 |
.arg(name) |
| 693 |
<< value; |
| 694 |
return; |
| 695 |
} |
| 696 |
|
| 697 |
int index = -1; |
| 698 |
if (value.isDouble()) |
| 699 |
index = static_cast<int>(value.toDouble() + 0.5); |
| 700 |
else if (value.isString()) |
| 701 |
index = combo->findText(value.toString()); |
| 702 |
|
| 703 |
if (index < 0 || index > combo->count()) { |
| 704 |
qWarning() << tr("Error setting default for option '%1'. " |
| 705 |
"Could not find valid combo entry index from value:") |
| 706 |
.arg(name) |
| 707 |
<< value; |
| 708 |
return; |
| 709 |
} |
| 710 |
|
| 711 |
combo->setCurrentIndex(index); |
| 712 |
} |
| 713 |
|
| 714 |
void JsonWidget::setStringOption(const QString& name, const QJsonValue& value) |
| 715 |
{ |
| 716 |
auto* lineEdit = qobject_cast<QLineEdit*>(m_widgets.value(name, nullptr)); |
| 717 |
if (!lineEdit) { |
| 718 |
qWarning() << tr("Error setting default for option '%1'. " |
| 719 |
"Bad widget type.") |
| 720 |
.arg(name); |
| 721 |
return; |
| 722 |
} |
| 723 |
|
| 724 |
if (!value.isString()) { |
| 725 |
qWarning() << tr("Error setting default for option '%1'. " |
| 726 |
"Bad default value:") |
| 727 |
.arg(name) |
| 728 |
<< value; |
| 729 |
return; |
| 730 |
} |
| 731 |
|
| 732 |
lineEdit->setText(value.toString()); |
| 733 |
} |
| 734 |
|
| 735 |
void JsonWidget::setTextOption(const QString& name, const QJsonValue& value) |
| 736 |
{ |
| 737 |
auto* text = qobject_cast<QLabel*>(m_widgets.value(name, nullptr)); |
| 738 |
if (text == nullptr) { |
| 739 |
qWarning() << tr("Error setting default for option '%1'. " |
| 740 |
"Bad widget type.") |
| 741 |
.arg(name); |
| 742 |
return; |
| 743 |
} |
| 744 |
|
| 745 |
if (!value.isString()) { |
| 746 |
qWarning() << tr("Error setting default for option '%1'. " |
| 747 |
"Bad default value:") |
| 748 |
.arg(name) |
| 749 |
<< value; |
| 750 |
return; |
| 751 |
} |
| 752 |
|
| 753 |
text->setText(value.toString()); |
| 754 |
} |
| 755 |
|
| 756 |
void JsonWidget::setTableOption(const QString& name, const QJsonValue& value) |
| 757 |
{ |
| 758 |
auto* table = qobject_cast<QTableWidget*>(m_widgets.value(name, nullptr)); |
| 759 |
if (table == nullptr) { |
| 760 |
qWarning() << tr("Error setting default for option '%1'. " |
| 761 |
"Bad widget type.") |
| 762 |
.arg(name); |
| 763 |
return; |
| 764 |
} |
| 765 |
|
| 766 |
if (!value.isString()) { |
| 767 |
qWarning() << tr("Error setting default for option '%1'. " |
| 768 |
"Bad default value:") |
| 769 |
.arg(name) |
| 770 |
<< value; |
| 771 |
return; |
| 772 |
} |
| 773 |
|
| 774 |
// parse the table (default delimiter is tab) |
| 775 |
QString delimiter; |
| 776 |
if (table->property("delimiter").isValid()) |
| 777 |
delimiter = table->property("delimiter").toString(); |
| 778 |
else |
| 779 |
delimiter = "\t"; |
| 780 |
|
| 781 |
// parse the table |
| 782 |
table->clearContents(); |
| 783 |
QStringList tableLines = value.toString().split("\n"); |
| 784 |
table->setRowCount(tableLines.size()); |
| 785 |
for (int i = 0; i < tableLines.size(); ++i) { |
| 786 |
QStringList entry = tableLines[i].split(delimiter); |
| 787 |
for (int j = 0; j < entry.size(); ++j) { |
| 788 |
table->setItem(i, j, new QTableWidgetItem(entry[j])); |
| 789 |
} |
| 790 |
} |
| 791 |
} |
| 792 |
|
| 793 |
void JsonWidget::setFilePathOption(const QString& name, const QJsonValue& value) |
| 794 |
{ |
| 795 |
auto* fileBrowse = |
| 796 |
qobject_cast<QtGui::FileBrowseWidget*>(m_widgets.value(name, nullptr)); |
| 797 |
if (!fileBrowse) { |
| 798 |
qWarning() << tr("Error setting default for option '%1'. " |
| 799 |
"Bad widget type.") |
| 800 |
.arg(name); |
| 801 |
return; |
| 802 |
} |
| 803 |
|
| 804 |
if (!value.isString()) { |
| 805 |
qWarning() << tr("Error setting default for option '%1'. " |
| 806 |
"Bad default value:") |
| 807 |
.arg(name) |
| 808 |
<< value; |
| 809 |
return; |
| 810 |
} |
| 811 |
|
| 812 |
fileBrowse->setFileName(value.toString()); |
| 813 |
} |
| 814 |
|
| 815 |
void JsonWidget::setIntegerOption(const QString& name, const QJsonValue& value) |
| 816 |
{ |
| 817 |
auto* spin = qobject_cast<QSpinBox*>(m_widgets.value(name, nullptr)); |
| 818 |
if (!spin) { |
| 819 |
qWarning() << tr("Error setting default for option '%1'. " |
| 820 |
"Bad widget type.") |
| 821 |
.arg(name); |
| 822 |
return; |
| 823 |
} |
| 824 |
|
| 825 |
if (!value.isDouble()) { |
| 826 |
qWarning() << tr("Error setting default for option '%1'. " |
| 827 |
"Bad default value:") |
| 828 |
.arg(name) |
| 829 |
<< value; |
| 830 |
return; |
| 831 |
} |
| 832 |
|
| 833 |
int intVal = static_cast<int>(value.toDouble()); |
| 834 |
spin->setValue(intVal); |
| 835 |
} |
| 836 |
|
| 837 |
void JsonWidget::setFloatOption(const QString& name, const QJsonValue& value) |
| 838 |
{ |
| 839 |
auto* spin = qobject_cast<QDoubleSpinBox*>(m_widgets.value(name, nullptr)); |
| 840 |
if (!spin) { |
| 841 |
qWarning() << tr("Error setting default for option '%1'. " |
| 842 |
"Bad widget type.") |
| 843 |
.arg(name); |
| 844 |
return; |
| 845 |
} |
| 846 |
|
| 847 |
if (!value.isDouble()) { |
| 848 |
qWarning() << tr("Error setting default for option '%1'. " |
| 849 |
"Bad default value:") |
| 850 |
.arg(name) |
| 851 |
<< value; |
| 852 |
return; |
| 853 |
} |
| 854 |
|
| 855 |
spin->setValue(value.toDouble()); |
| 856 |
} |
| 857 |
|
| 858 |
void JsonWidget::setBooleanOption(const QString& name, const QJsonValue& value) |
| 859 |
{ |
| 860 |
auto* checkBox = qobject_cast<QCheckBox*>(m_widgets.value(name, nullptr)); |
| 861 |
if (!checkBox) { |
| 862 |
qWarning() << tr("Error setting default for option '%1'. " |
| 863 |
"Bad widget type.") |
| 864 |
.arg(name); |
| 865 |
return; |
| 866 |
} |
| 867 |
|
| 868 |
if (!value.isBool()) { |
| 869 |
qWarning() << tr("Error setting default for option '%1'. " |
| 870 |
"Bad default value:") |
| 871 |
.arg(name) |
| 872 |
<< value; |
| 873 |
return; |
| 874 |
} |
| 875 |
|
| 876 |
checkBox->setChecked(value.toBool()); |
| 877 |
} |
| 878 |
|
| 879 |
bool JsonWidget::optionString(const QString& option, QString& value) const |
| 880 |
{ |
| 881 |
QWidget* widget = m_widgets.value(option, nullptr); |
| 882 |
bool retval = false; |
| 883 |
value.clear(); |
| 884 |
|
| 885 |
if (auto* edit = qobject_cast<QLineEdit*>(widget)) { |
| 886 |
retval = true; |
| 887 |
value = edit->text(); |
| 888 |
} else if (auto* combo = qobject_cast<QComboBox*>(widget)) { |
| 889 |
retval = true; |
| 890 |
value = combo->currentText(); |
| 891 |
} else if (auto* spinbox = qobject_cast<QSpinBox*>(widget)) { |
| 892 |
retval = true; |
| 893 |
value = QString::number(spinbox->value()); |
| 894 |
} else if (auto* dspinbox = qobject_cast<QDoubleSpinBox*>(widget)) { |
| 895 |
retval = true; |
| 896 |
value = QString::number(dspinbox->value()); |
| 897 |
} else if (auto* fileBrowse = |
| 898 |
qobject_cast<QtGui::FileBrowseWidget*>(widget)) { |
| 899 |
retval = true; |
| 900 |
value = fileBrowse->fileName(); |
| 901 |
} |
| 902 |
|
| 903 |
return retval; |
| 904 |
} |
| 905 |
|
| 906 |
QJsonObject JsonWidget::collectOptions() const |
| 907 |
{ |
| 908 |
QJsonObject ret; |
| 909 |
|
| 910 |
foreach (QString label, m_widgets.keys()) { |
| 911 |
QWidget* widget = m_widgets.value(label, nullptr); |
| 912 |
if (auto* combo = qobject_cast<QComboBox*>(widget)) { |
| 913 |
ret.insert(label, combo->currentText()); |
| 914 |
} else if (auto* lineEdit = qobject_cast<QLineEdit*>(widget)) { |
| 915 |
QString value(lineEdit->text()); |
| 916 |
if (value.isEmpty() && label == u"Title"_s) |
| 917 |
value = generateJobTitle(); |
| 918 |
ret.insert(label, value); |
| 919 |
} else if (auto* spinBox = qobject_cast<QSpinBox*>(widget)) { |
| 920 |
ret.insert(label, spinBox->value()); |
| 921 |
} else if (auto* doubleSpinBox = qobject_cast<QDoubleSpinBox*>(widget)) { |
| 922 |
ret.insert(label, doubleSpinBox->value()); |
| 923 |
} else if (auto* checkBox = qobject_cast<QCheckBox*>(widget)) { |
| 924 |
ret.insert(label, checkBox->isChecked()); |
| 925 |
} else if (auto* fileBrowse = |
| 926 |
qobject_cast<QtGui::FileBrowseWidget*>(widget)) { |
| 927 |
ret.insert(label, fileBrowse->fileName()); |
| 928 |
} else { |
| 929 |
qWarning() |
| 930 |
<< tr("Unhandled widget in collectOptions for option '%1'.").arg(label); |
| 931 |
} |
| 932 |
} |
| 933 |
|
| 934 |
return ret; |
| 935 |
} |
| 936 |
|
| 937 |
void JsonWidget::applyOptions(const QJsonObject& opts) |
| 938 |
{ |
| 939 |
foreach (const QString& label, opts.keys()) { |
| 940 |
setOption(label, opts[label]); |
| 941 |
|
| 942 |
qDebug() << "Setting option" << label << "to" << opts[label]; |
| 943 |
} |
| 944 |
} |
| 945 |
|
| 946 |
QString JsonWidget::generateJobTitle() const |
| 947 |
{ |
| 948 |
QString calculation; |
| 949 |
bool haveCalculation(optionString(u"Calculation Type"_s, calculation)); |
| 950 |
|
| 951 |
QString theory; |
| 952 |
bool haveTheory(optionString(u"Theory"_s, theory)); |
| 953 |
|
| 954 |
QString basis; |
| 955 |
bool haveBasis(optionString(u"Basis"_s, basis)); |
| 956 |
|
| 957 |
// Merge theory/basis into theory |
| 958 |
if (haveBasis) { |
| 959 |
if (haveTheory) |
| 960 |
theory += "/"; |
| 961 |
theory += basis; |
| 962 |
theory.replace(QRegularExpression("\\s+"), ""); |
| 963 |
haveTheory = true; |
| 964 |
} |
| 965 |
|
| 966 |
if (m_batchMode) { |
| 967 |
QString result; |
| 968 |
result = haveCalculation ? calculation : QString(); |
| 969 |
result += haveTheory ? (result.size() != 0 ? " | " : QString()) + theory |
| 970 |
: QString(); |
| 971 |
return result; |
| 972 |
} |
| 973 |
|
| 974 |
QString formula(m_molecule ? QString::fromStdString(m_molecule->formula()) |
| 975 |
: tr("[no molecule]")); |
| 976 |
|
| 977 |
return QString("%1%2%3") |
| 978 |
.arg(formula) |
| 979 |
.arg(haveCalculation ? " | " + calculation : QString()) |
| 980 |
.arg(haveTheory ? " | " + theory : QString()); |
| 981 |
} |
| 982 |
|
| 983 |
} // namespace Avogadro::QtGui |