| 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 "forcefielddialog.h" |
| 7 |
#include "ui_forcefielddialog.h" |
| 8 |
|
| 9 |
#include <avogadro/qtgui/jsonwidget.h> |
| 10 |
|
| 11 |
#include <QtCore/QDebug> |
| 12 |
#include <QtCore/QJsonDocument> |
| 13 |
#include <QtCore/QJsonObject> |
| 14 |
#include <QtCore/QJsonParseError> |
| 15 |
#include <QtCore/QSettings> |
| 16 |
#include <QtCore/QString> |
| 17 |
#include <QtCore/QStringList> |
| 18 |
|
| 19 |
#include <QtWidgets/QCheckBox> |
| 20 |
#include <QtWidgets/QComboBox> |
| 21 |
#include <QtWidgets/QDialogButtonBox> |
| 22 |
#include <QtWidgets/QFormLayout> |
| 23 |
#include <QtWidgets/QMessageBox> |
| 24 |
#include <QtWidgets/QPushButton> |
| 25 |
#include <QtWidgets/QVBoxLayout> |
| 26 |
|
| 27 |
#include <cmath> // for log10 |
| 28 |
|
| 29 |
namespace Avogadro { |
| 30 |
namespace QtPlugins { |
| 31 |
|
| 32 |
namespace { |
| 33 |
// Minimal JsonWidget subclass that renders a fixed options schema with no |
| 34 |
// backing script. The schema must be the flat "userOptions" object (not |
| 35 |
// wrapped in an outer object). |
| 36 |
class SchemaWidget : public QtGui::JsonWidget |
| 37 |
{ |
| 38 |
public: |
| 39 |
explicit SchemaWidget(const QJsonObject& schema, QWidget* parent = nullptr) |
| 40 |
: QtGui::JsonWidget(parent) |
| 41 |
{ |
| 42 |
m_options.insert(QStringLiteral("userOptions"), schema); |
| 43 |
updateOptions(); |
| 44 |
} |
| 45 |
}; |
| 46 |
} // namespace |
| 47 |
|
| 48 |
ForceFieldDialog::ForceFieldDialog(const QStringList& forceFields, |
| 49 |
QWidget* parent_) |
| 50 |
: QDialog(parent_), ui(new Ui::ForceFieldDialog) |
| 51 |
{ |
| 52 |
ui->setupUi(this); |
| 53 |
ui->forceField->addItems(forceFields); |
| 54 |
updateRecommendedForceField(); |
| 55 |
|
| 56 |
m_optionsButton = new QPushButton(tr("Options…"), this); |
| 57 |
ui->formLayout->setWidget(2, QFormLayout::FieldRole, m_optionsButton); |
| 58 |
|
| 59 |
connect(ui->useRecommended, &QCheckBox::toggled, this, |
| 60 |
&ForceFieldDialog::useRecommendedForceFieldToggled); |
| 61 |
connect(ui->forceField, &QComboBox::currentTextChanged, this, |
| 62 |
&ForceFieldDialog::updateOptionsButton); |
| 63 |
connect(m_optionsButton, &QPushButton::clicked, this, |
| 64 |
&ForceFieldDialog::modelOptionsClicked); |
| 65 |
|
| 66 |
QSettings settings; |
| 67 |
bool autoDetect = |
| 68 |
settings.value("openbabel/optimizeGeometry/autoDetect", true).toBool(); |
| 69 |
ui->useRecommended->setChecked(autoDetect); |
| 70 |
updateOptionsButton(); |
| 71 |
} |
| 72 |
|
| 73 |
ForceFieldDialog::~ForceFieldDialog() |
| 74 |
{ |
| 75 |
delete ui; |
| 76 |
} |
| 77 |
|
| 78 |
QVariantMap ForceFieldDialog::prompt(QWidget* parent_, |
| 79 |
const QStringList& forceFields, |
| 80 |
const QVariantMap& startingOptions, |
| 81 |
const QString& recommendedForceField_) |
| 82 |
{ |
| 83 |
ForceFieldDialog dlg(forceFields, parent_); |
| 84 |
dlg.setOptions(startingOptions); |
| 85 |
dlg.setRecommendedForceField(recommendedForceField_); |
| 86 |
|
| 87 |
QVariantMap options; |
| 88 |
if (dlg.exec() == QDialog::Accepted) |
| 89 |
options = dlg.options(); |
| 90 |
|
| 91 |
return options; |
| 92 |
} |
| 93 |
|
| 94 |
QVariantMap ForceFieldDialog::options() const |
| 95 |
{ |
| 96 |
QVariantMap opts; |
| 97 |
|
| 98 |
opts["forcefield"] = ui->forceField->currentText(); |
| 99 |
opts["maxSteps"] = ui->stepLimit->value(); |
| 100 |
opts["tolerance"] = pow(10, ui->energyConv->value()); |
| 101 |
opts["gradientTolerance"] = pow(10, ui->gradConv->value()); |
| 102 |
opts["autodetect"] = ui->useRecommended->isChecked(); |
| 103 |
opts["modelUserOptions"] = m_modelUserOptions; |
| 104 |
|
| 105 |
return opts; |
| 106 |
} |
| 107 |
|
| 108 |
void ForceFieldDialog::setOptions(const QVariantMap& opts) |
| 109 |
{ |
| 110 |
if (opts.contains("modelUserOptionsSchemas") && |
| 111 |
opts["modelUserOptionsSchemas"].canConvert<QVariantMap>()) { |
| 112 |
m_modelUserOptionSchemas = opts["modelUserOptionsSchemas"].toMap(); |
| 113 |
} |
| 114 |
if (opts.contains("modelUserOptions") && |
| 115 |
opts["modelUserOptions"].canConvert<QVariantMap>()) { |
| 116 |
m_modelUserOptions = opts["modelUserOptions"].toMap(); |
| 117 |
} |
| 118 |
|
| 119 |
if (opts.contains("forcefield") && opts["forcefield"].canConvert<QString>()) |
| 120 |
ui->forceField->setCurrentText(opts["forcefield"].toString()); |
| 121 |
if (opts.contains("maxSteps") && opts["maxSteps"].canConvert<int>()) |
| 122 |
ui->stepLimit->setValue(opts["maxSteps"].toInt()); |
| 123 |
if (opts.contains("tolerance") && opts["tolerance"].canConvert<double>()) |
| 124 |
ui->energyConv->setValue(log10(opts["tolerance"].toDouble())); |
| 125 |
if (opts.contains("gradientTolerance") && |
| 126 |
opts["gradientTolerance"].canConvert<double>()) |
| 127 |
ui->gradConv->setValue(log10(opts["gradientTolerance"].toDouble())); |
| 128 |
if (opts.contains("autodetect") && opts["autodetect"].canConvert<bool>()) |
| 129 |
ui->useRecommended->setChecked(opts["autodetect"].toBool()); |
| 130 |
|
| 131 |
updateOptionsButton(); |
| 132 |
} |
| 133 |
|
| 134 |
void ForceFieldDialog::setRecommendedForceField(const QString& rff) |
| 135 |
{ |
| 136 |
if (rff == m_recommendedForceField) |
| 137 |
return; |
| 138 |
|
| 139 |
if (ui->forceField->findText(rff) == -1) |
| 140 |
return; |
| 141 |
|
| 142 |
m_recommendedForceField = rff; |
| 143 |
updateRecommendedForceField(); |
| 144 |
} |
| 145 |
|
| 146 |
void ForceFieldDialog::useRecommendedForceFieldToggled(bool state) |
| 147 |
{ |
| 148 |
if (!m_recommendedForceField.isEmpty()) { |
| 149 |
if (state) { |
| 150 |
int index = ui->forceField->findText(m_recommendedForceField); |
| 151 |
if (index >= 0) { |
| 152 |
ui->forceField->setCurrentIndex(index); |
| 153 |
} |
| 154 |
} |
| 155 |
} |
| 156 |
ui->forceField->setEnabled(!state); |
| 157 |
updateOptionsButton(); |
| 158 |
|
| 159 |
QSettings().setValue("forcefield/autoDetect", state); |
| 160 |
} |
| 161 |
|
| 162 |
void ForceFieldDialog::modelOptionsClicked() |
| 163 |
{ |
| 164 |
const QString forceField = ui->forceField->currentText(); |
| 165 |
const QString schemaJson = |
| 166 |
m_modelUserOptionSchemas.value(forceField).toString().trimmed(); |
| 167 |
if (schemaJson.isEmpty()) |
| 168 |
return; |
| 169 |
|
| 170 |
QJsonParseError parseError; |
| 171 |
const QJsonDocument schemaDoc = |
| 172 |
QJsonDocument::fromJson(schemaJson.toUtf8(), &parseError); |
| 173 |
if (parseError.error != QJsonParseError::NoError || !schemaDoc.isObject()) { |
| 174 |
QMessageBox::warning( |
| 175 |
this, tr("Invalid Model Options"), |
| 176 |
tr("Could not parse user options for model '%1'.").arg(forceField)); |
| 177 |
return; |
| 178 |
} |
| 179 |
|
| 180 |
QDialog dlg(this); |
| 181 |
dlg.setWindowTitle(tr("%1 Options").arg(forceField)); |
| 182 |
auto* layout = new QVBoxLayout(&dlg); |
| 183 |
auto* widget = new SchemaWidget(schemaDoc.object(), &dlg); |
| 184 |
|
| 185 |
const QString valuesJson = m_modelUserOptions.value(forceField).toString(); |
| 186 |
if (!valuesJson.trimmed().isEmpty()) { |
| 187 |
QJsonParseError valueParseError; |
| 188 |
const QJsonDocument valueDoc = |
| 189 |
QJsonDocument::fromJson(valuesJson.toUtf8(), &valueParseError); |
| 190 |
if (valueParseError.error == QJsonParseError::NoError && |
| 191 |
valueDoc.isObject()) { |
| 192 |
widget->applyOptions(valueDoc.object()); |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
layout->addWidget(widget); |
| 197 |
auto* buttonBox = new QDialogButtonBox( |
| 198 |
QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal, &dlg); |
| 199 |
connect(buttonBox, &QDialogButtonBox::accepted, &dlg, &QDialog::accept); |
| 200 |
connect(buttonBox, &QDialogButtonBox::rejected, &dlg, &QDialog::reject); |
| 201 |
layout->addWidget(buttonBox); |
| 202 |
|
| 203 |
if (dlg.exec() != QDialog::Accepted) |
| 204 |
return; |
| 205 |
|
| 206 |
const QJsonObject collected = widget->collectOptions(); |
| 207 |
if (collected.isEmpty()) { |
| 208 |
m_modelUserOptions.remove(forceField); |
| 209 |
} else { |
| 210 |
m_modelUserOptions[forceField] = QString::fromUtf8( |
| 211 |
QJsonDocument(collected).toJson(QJsonDocument::Compact)); |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
void ForceFieldDialog::updateRecommendedForceField() |
| 216 |
{ |
| 217 |
if (m_recommendedForceField.isEmpty()) { |
| 218 |
ui->useRecommended->hide(); |
| 219 |
ui->forceField->setEnabled(true); |
| 220 |
} else { |
| 221 |
ui->useRecommended->setText( |
| 222 |
tr("Autodetect (%1)").arg(m_recommendedForceField)); |
| 223 |
// Force the combo box to update if needed: |
| 224 |
useRecommendedForceFieldToggled(ui->useRecommended->isChecked()); |
| 225 |
ui->useRecommended->show(); |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
void ForceFieldDialog::updateOptionsButton() |
| 230 |
{ |
| 231 |
if (m_optionsButton == nullptr) |
| 232 |
return; |
| 233 |
|
| 234 |
const QString forceField = ui->forceField->currentText(); |
| 235 |
const QString schemaJson = |
| 236 |
m_modelUserOptionSchemas.value(forceField).toString().trimmed(); |
| 237 |
m_optionsButton->setEnabled(!schemaJson.isEmpty()); |
| 238 |
} |
| 239 |
|
| 240 |
} // namespace QtPlugins |
| 241 |
} // namespace Avogadro |