| 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 "unitcelldialog.h" |
| 7 |
#include "ui_unitcelldialog.h" |
| 8 |
|
| 9 |
#include <avogadro/qtgui/molecule.h> |
| 10 |
#include <avogadro/qtgui/rwmolecule.h> |
| 11 |
|
| 12 |
#include <avogadro/core/crystaltools.h> |
| 13 |
#include <avogadro/core/unitcell.h> |
| 14 |
|
| 15 |
#include <QtWidgets/QApplication> |
| 16 |
#include <QtWidgets/QMessageBox> |
| 17 |
#include <QtWidgets/QPlainTextEdit> |
| 18 |
|
| 19 |
#include <QRegularExpression> |
| 20 |
|
| 21 |
using Avogadro::QtGui::Molecule; |
| 22 |
|
| 23 |
namespace { |
| 24 |
// Matrix formatting: |
| 25 |
const int MATRIX_WIDTH = 9; |
| 26 |
const int MATRIX_PREC = 5; |
| 27 |
const char MATRIX_FMT = 'f'; |
| 28 |
|
| 29 |
// Valid value separators in matrix editors: |
| 30 |
const static QRegularExpression MATRIX_SEP( |
| 31 |
R"(\s|,|;|\||\[|\]|\{|\}|\(|\)|\&|/|<|>)"); |
| 32 |
} // namespace |
| 33 |
|
| 34 |
namespace Avogadro::QtPlugins { |
| 35 |
|
| 36 |
UnitCellDialog::UnitCellDialog(QWidget* p) |
| 37 |
: QDialog(p), m_ui(new Ui::UnitCellDialog), m_molecule(nullptr), |
| 38 |
m_mode(Invalid) |
| 39 |
{ |
| 40 |
m_ui->setupUi(this); |
| 41 |
|
| 42 |
connect(m_ui->a, SIGNAL(valueChanged(double)), SLOT(parametersEdited())); |
| 43 |
connect(m_ui->b, SIGNAL(valueChanged(double)), SLOT(parametersEdited())); |
| 44 |
connect(m_ui->c, SIGNAL(valueChanged(double)), SLOT(parametersEdited())); |
| 45 |
connect(m_ui->alpha, SIGNAL(valueChanged(double)), SLOT(parametersEdited())); |
| 46 |
connect(m_ui->beta, SIGNAL(valueChanged(double)), SLOT(parametersEdited())); |
| 47 |
connect(m_ui->gamma, SIGNAL(valueChanged(double)), SLOT(parametersEdited())); |
| 48 |
|
| 49 |
connect(m_ui->cellMatrix, SIGNAL(textChanged()), SLOT(cellMatrixEdited())); |
| 50 |
|
| 51 |
connect(m_ui->fractionalMatrix, SIGNAL(textChanged()), |
| 52 |
SLOT(fractionalMatrixEdited())); |
| 53 |
|
| 54 |
connect(m_ui->apply, SIGNAL(clicked()), SLOT(apply())); |
| 55 |
connect(m_ui->revert, SIGNAL(clicked()), SLOT(revert())); |
| 56 |
} |
| 57 |
|
| 58 |
UnitCellDialog::~UnitCellDialog() |
| 59 |
{ |
| 60 |
delete m_ui; |
| 61 |
} |
| 62 |
|
| 63 |
void UnitCellDialog::setMolecule(QtGui::Molecule* molecule) |
| 64 |
{ |
| 65 |
if (molecule != m_molecule) { |
| 66 |
if (m_molecule) |
| 67 |
m_molecule->disconnect(this); |
| 68 |
|
| 69 |
m_molecule = molecule; |
| 70 |
|
| 71 |
if (m_molecule) |
| 72 |
connect(m_molecule, SIGNAL(changed(uint)), SLOT(moleculeChanged(uint))); |
| 73 |
|
| 74 |
revert(); |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
void UnitCellDialog::moleculeChanged(unsigned int changes) |
| 79 |
{ |
| 80 |
if (changes & Molecule::UnitCell) |
| 81 |
revert(); |
| 82 |
} |
| 83 |
|
| 84 |
void UnitCellDialog::parametersEdited() |
| 85 |
{ |
| 86 |
setMode(Parameters); |
| 87 |
updateParameters(); |
| 88 |
revertCellMatrix(); |
| 89 |
revertFractionalMatrix(); |
| 90 |
} |
| 91 |
|
| 92 |
void UnitCellDialog::cellMatrixEdited() |
| 93 |
{ |
| 94 |
setMode(CellMatrix); |
| 95 |
if (validateCellMatrix()) { |
| 96 |
updateCellMatrix(); |
| 97 |
revertParameters(); |
| 98 |
revertFractionalMatrix(); |
| 99 |
enableApply(true); |
| 100 |
} else { |
| 101 |
enableApply(false); |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
void UnitCellDialog::fractionalMatrixEdited() |
| 106 |
{ |
| 107 |
setMode(FractionalMatrix); |
| 108 |
if (validateFractionalMatrix()) { |
| 109 |
updateFractionalMatrix(); |
| 110 |
revertParameters(); |
| 111 |
revertCellMatrix(); |
| 112 |
enableApply(true); |
| 113 |
} else { |
| 114 |
enableApply(false); |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
void UnitCellDialog::apply() |
| 119 |
{ |
| 120 |
if (!isCrystal()) { |
| 121 |
revert(); |
| 122 |
return; |
| 123 |
} |
| 124 |
|
| 125 |
switch (m_mode) { |
| 126 |
case Invalid: |
| 127 |
case Clean: |
| 128 |
revert(); |
| 129 |
break; |
| 130 |
default: { |
| 131 |
Core::CrystalTools::Options options = Core::CrystalTools::None; |
| 132 |
if (m_ui->transformAtoms->isChecked()) |
| 133 |
options |= Core::CrystalTools::TransformAtoms; |
| 134 |
m_molecule->undoMolecule()->editUnitCell(m_tempCell.cellMatrix(), |
| 135 |
options); |
| 136 |
break; |
| 137 |
} |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
void UnitCellDialog::revert() |
| 142 |
{ |
| 143 |
if (isCrystal()) |
| 144 |
m_tempCell = *m_molecule->unitCell(); |
| 145 |
|
| 146 |
revertParameters(); |
| 147 |
revertCellMatrix(); |
| 148 |
validateCellMatrix(); |
| 149 |
revertFractionalMatrix(); |
| 150 |
validateFractionalMatrix(); |
| 151 |
|
| 152 |
setMode(isCrystal() ? Clean : Invalid); |
| 153 |
} |
| 154 |
|
| 155 |
bool UnitCellDialog::isCrystal() const |
| 156 |
{ |
| 157 |
return m_molecule && m_molecule->unitCell(); |
| 158 |
} |
| 159 |
|
| 160 |
void UnitCellDialog::setMode(UnitCellDialog::Mode m) |
| 161 |
{ |
| 162 |
if (m != m_mode) { |
| 163 |
m_mode = m; |
| 164 |
enableParameters(m == Clean || m == Parameters); |
| 165 |
enableCellMatrix(m == Clean || m == CellMatrix); |
| 166 |
enableFractionalMatrix(m == Clean || m == FractionalMatrix); |
| 167 |
enableApply(m != Clean && m != Invalid); |
| 168 |
enableRevert(m != Clean && m != Invalid); |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
void UnitCellDialog::enableParameters(bool e) |
| 173 |
{ |
| 174 |
m_ui->a->setEnabled(e); |
| 175 |
m_ui->b->setEnabled(e); |
| 176 |
m_ui->c->setEnabled(e); |
| 177 |
m_ui->alpha->setEnabled(e); |
| 178 |
m_ui->beta->setEnabled(e); |
| 179 |
m_ui->gamma->setEnabled(e); |
| 180 |
} |
| 181 |
|
| 182 |
void UnitCellDialog::enableCellMatrix(bool e) |
| 183 |
{ |
| 184 |
m_ui->cellMatrix->setEnabled(e); |
| 185 |
} |
| 186 |
|
| 187 |
void UnitCellDialog::enableFractionalMatrix(bool e) |
| 188 |
{ |
| 189 |
m_ui->fractionalMatrix->setEnabled(e); |
| 190 |
} |
| 191 |
|
| 192 |
void UnitCellDialog::enableApply(bool e) |
| 193 |
{ |
| 194 |
m_ui->apply->setEnabled(e); |
| 195 |
} |
| 196 |
|
| 197 |
void UnitCellDialog::enableRevert(bool e) |
| 198 |
{ |
| 199 |
m_ui->revert->setEnabled(e); |
| 200 |
} |
| 201 |
|
| 202 |
void UnitCellDialog::blockParametersSignals(bool e) |
| 203 |
{ |
| 204 |
m_ui->a->blockSignals(e); |
| 205 |
m_ui->b->blockSignals(e); |
| 206 |
m_ui->c->blockSignals(e); |
| 207 |
m_ui->alpha->blockSignals(e); |
| 208 |
m_ui->beta->blockSignals(e); |
| 209 |
m_ui->gamma->blockSignals(e); |
| 210 |
} |
| 211 |
|
| 212 |
void UnitCellDialog::blockCellMatrixSignals(bool e) |
| 213 |
{ |
| 214 |
m_ui->cellMatrix->blockSignals(e); |
| 215 |
} |
| 216 |
|
| 217 |
void UnitCellDialog::blockFractionalMatrixSignals(bool e) |
| 218 |
{ |
| 219 |
m_ui->fractionalMatrix->blockSignals(e); |
| 220 |
} |
| 221 |
|
| 222 |
void UnitCellDialog::revertParameters() |
| 223 |
{ |
| 224 |
blockParametersSignals(true); |
| 225 |
if (isCrystal()) { |
| 226 |
m_ui->a->setValue(static_cast<double>(m_tempCell.a())); |
| 227 |
m_ui->b->setValue(static_cast<double>(m_tempCell.b())); |
| 228 |
m_ui->c->setValue(static_cast<double>(m_tempCell.c())); |
| 229 |
m_ui->alpha->setValue(static_cast<double>(m_tempCell.alpha() * RAD_TO_DEG)); |
| 230 |
m_ui->beta->setValue(static_cast<double>(m_tempCell.beta() * RAD_TO_DEG)); |
| 231 |
m_ui->gamma->setValue(static_cast<double>(m_tempCell.gamma() * RAD_TO_DEG)); |
| 232 |
} else { |
| 233 |
enableParameters(false); |
| 234 |
m_ui->a->setValue(3.); |
| 235 |
m_ui->b->setValue(3.); |
| 236 |
m_ui->c->setValue(3.); |
| 237 |
m_ui->alpha->setValue(90.); |
| 238 |
m_ui->beta->setValue(90.); |
| 239 |
m_ui->gamma->setValue(90.); |
| 240 |
} |
| 241 |
blockParametersSignals(false); |
| 242 |
} |
| 243 |
|
| 244 |
void UnitCellDialog::revertCellMatrix() |
| 245 |
{ |
| 246 |
blockCellMatrixSignals(true); |
| 247 |
if (isCrystal()) { |
| 248 |
m_ui->cellMatrix->setPlainText(matrixToString(m_tempCell.cellMatrix())); |
| 249 |
} else { |
| 250 |
enableCellMatrix(false); |
| 251 |
m_ui->cellMatrix->setPlainText(tr("No unit cell present.")); |
| 252 |
} |
| 253 |
blockCellMatrixSignals(false); |
| 254 |
} |
| 255 |
|
| 256 |
void UnitCellDialog::revertFractionalMatrix() |
| 257 |
{ |
| 258 |
blockFractionalMatrixSignals(true); |
| 259 |
if (isCrystal()) { |
| 260 |
m_ui->fractionalMatrix->setPlainText( |
| 261 |
matrixToString(m_tempCell.fractionalMatrix())); |
| 262 |
} else { |
| 263 |
enableFractionalMatrix(false); |
| 264 |
m_ui->fractionalMatrix->setPlainText(tr("No unit cell present.")); |
| 265 |
} |
| 266 |
blockFractionalMatrixSignals(false); |
| 267 |
} |
| 268 |
|
| 269 |
void UnitCellDialog::updateParameters() |
| 270 |
{ |
| 271 |
const auto a = static_cast<Real>(m_ui->a->value()); |
| 272 |
const auto b = static_cast<Real>(m_ui->b->value()); |
| 273 |
const auto c = static_cast<Real>(m_ui->c->value()); |
| 274 |
assert(a > 0.0 && b > 0.0 && c > 0.0); |
| 275 |
Core::UnitCell tmp; |
| 276 |
tmp.setCellParameters(a, b, c, |
| 277 |
static_cast<Real>(m_ui->alpha->value()) * DEG_TO_RAD, |
| 278 |
static_cast<Real>(m_ui->beta->value()) * DEG_TO_RAD, |
| 279 |
static_cast<Real>(m_ui->gamma->value()) * DEG_TO_RAD); |
| 280 |
if (!tmp.isRegular()) { |
| 281 |
QMessageBox::warning(nullptr, tr("Unit Cell Editor"), |
| 282 |
tr("Ignoring singular cell matrix")); |
| 283 |
return; |
| 284 |
} |
| 285 |
m_tempCell = tmp; |
| 286 |
} |
| 287 |
|
| 288 |
void UnitCellDialog::updateCellMatrix() |
| 289 |
{ |
| 290 |
const Matrix3 tmp = stringToMatrix(m_ui->cellMatrix->toPlainText()); |
| 291 |
if (!Core::UnitCell::isRegular(tmp)) { |
| 292 |
QMessageBox::warning(nullptr, tr("Unit Cell Editor"), |
| 293 |
tr("Ignoring singular cell matrix")); |
| 294 |
return; |
| 295 |
} |
| 296 |
m_tempCell.setCellMatrix(tmp); |
| 297 |
} |
| 298 |
|
| 299 |
void UnitCellDialog::updateFractionalMatrix() |
| 300 |
{ |
| 301 |
const Matrix3 tmp = stringToMatrix(m_ui->fractionalMatrix->toPlainText()); |
| 302 |
if (!Core::UnitCell::isRegular(tmp)) { |
| 303 |
QMessageBox::warning(nullptr, tr("Unit Cell Editor"), |
| 304 |
tr("Ignoring singular fractional cell matrix")); |
| 305 |
return; |
| 306 |
} |
| 307 |
m_tempCell.setFractionalMatrix(tmp); |
| 308 |
} |
| 309 |
|
| 310 |
bool UnitCellDialog::validateCellMatrix() |
| 311 |
{ |
| 312 |
return validateMatrixEditor(m_ui->cellMatrix); |
| 313 |
} |
| 314 |
|
| 315 |
bool UnitCellDialog::validateFractionalMatrix() |
| 316 |
{ |
| 317 |
return validateMatrixEditor(m_ui->fractionalMatrix); |
| 318 |
} |
| 319 |
|
| 320 |
void UnitCellDialog::initializeMatrixEditor(QPlainTextEdit* edit) |
| 321 |
{ |
| 322 |
#if defined(Q_OS_WIN) || defined(Q_OS_MACOS) |
| 323 |
QFont font("Courier"); |
| 324 |
#else |
| 325 |
QFont font("Monospace"); |
| 326 |
#endif |
| 327 |
edit->setFont(font); |
| 328 |
|
| 329 |
QFontMetrics metrics(font); |
| 330 |
int minWidth = 3 * metrics.horizontalAdvance('0') * (MATRIX_WIDTH + 1); |
| 331 |
int minHeight = metrics.lineSpacing() * 3; |
| 332 |
|
| 333 |
edit->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum); |
| 334 |
edit->setMinimumSize(minWidth, minHeight); |
| 335 |
} |
| 336 |
|
| 337 |
bool UnitCellDialog::validateMatrixEditor(QPlainTextEdit* edit) |
| 338 |
{ |
| 339 |
bool valid = stringToMatrix(edit->toPlainText()) != Matrix3::Zero(); |
| 340 |
QPalette pal = edit->palette(); |
| 341 |
// get the window text color |
| 342 |
// works for both dark theme and light theme |
| 343 |
QColor text = qApp->palette().color(QPalette::WindowText); |
| 344 |
pal.setColor(QPalette::Text, valid ? text : Qt::red); |
| 345 |
edit->setPalette(pal); |
| 346 |
return valid; |
| 347 |
} |
| 348 |
|
| 349 |
QString UnitCellDialog::matrixToString(const Matrix3& mat) |
| 350 |
{ |
| 351 |
// Transpose into the more intuitive row-vector format. |
| 352 |
return QString("%1 %2 %3\n%4 %5 %6\n%7 %8 %9") |
| 353 |
.arg(static_cast<double>(mat(0, 0)), MATRIX_WIDTH, MATRIX_FMT, MATRIX_PREC) |
| 354 |
.arg(static_cast<double>(mat(1, 0)), MATRIX_WIDTH, MATRIX_FMT, MATRIX_PREC) |
| 355 |
.arg(static_cast<double>(mat(2, 0)), MATRIX_WIDTH, MATRIX_FMT, MATRIX_PREC) |
| 356 |
.arg(static_cast<double>(mat(0, 1)), MATRIX_WIDTH, MATRIX_FMT, MATRIX_PREC) |
| 357 |
.arg(static_cast<double>(mat(1, 1)), MATRIX_WIDTH, MATRIX_FMT, MATRIX_PREC) |
| 358 |
.arg(static_cast<double>(mat(2, 1)), MATRIX_WIDTH, MATRIX_FMT, MATRIX_PREC) |
| 359 |
.arg(static_cast<double>(mat(0, 2)), MATRIX_WIDTH, MATRIX_FMT, MATRIX_PREC) |
| 360 |
.arg(static_cast<double>(mat(1, 2)), MATRIX_WIDTH, MATRIX_FMT, MATRIX_PREC) |
| 361 |
.arg(static_cast<double>(mat(2, 2)), MATRIX_WIDTH, MATRIX_FMT, MATRIX_PREC); |
| 362 |
} |
| 363 |
|
| 364 |
Matrix3 UnitCellDialog::stringToMatrix(const QString& str) |
| 365 |
{ |
| 366 |
Matrix3 result; |
| 367 |
QStringList lines = str.split('\n'); |
| 368 |
if (lines.size() != 3) |
| 369 |
return Matrix3::Zero(); |
| 370 |
|
| 371 |
bool ok; |
| 372 |
int row = 0; |
| 373 |
int col = 0; |
| 374 |
foreach (const QString& line, lines) { |
| 375 |
QStringList values = line.split(MATRIX_SEP, Qt::SkipEmptyParts); |
| 376 |
if (values.size() != 3) |
| 377 |
return Matrix3::Zero(); |
| 378 |
|
| 379 |
foreach (const QString& value, values) { |
| 380 |
Real val = static_cast<Real>(value.toDouble(&ok)); |
| 381 |
if (!ok) |
| 382 |
return Matrix3::Zero(); |
| 383 |
|
| 384 |
// Transpose from the more intuitive row-vector format. |
| 385 |
result(col++, row) = val; |
| 386 |
} |
| 387 |
row++; |
| 388 |
col = 0; |
| 389 |
} |
| 390 |
|
| 391 |
return result; |
| 392 |
} |
| 393 |
|
| 394 |
} // namespace Avogadro::QtPlugins |