| 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 "spacegroup.h" |
| 7 |
|
| 8 |
#include <cmath> |
| 9 |
|
| 10 |
#include <avogadro/core/avospglib.h> |
| 11 |
#include <avogadro/core/crystaltools.h> |
| 12 |
#include <avogadro/core/spacegroups.h> |
| 13 |
#include <avogadro/core/unitcell.h> |
| 14 |
|
| 15 |
#include <avogadro/qtgui/molecule.h> |
| 16 |
#include <avogadro/qtgui/rwmolecule.h> |
| 17 |
#include <avogadro/qtgui/richtextdelegate.h> |
| 18 |
|
| 19 |
#include <QAction> |
| 20 |
#include <QDebug> |
| 21 |
#include <QtWidgets/QAbstractItemView> |
| 22 |
#include <QtWidgets/QDialogButtonBox> |
| 23 |
#include <QtWidgets/QHeaderView> |
| 24 |
#include <QtWidgets/QInputDialog> |
| 25 |
#include <QtWidgets/QLayout> |
| 26 |
#include <QtWidgets/QMessageBox> |
| 27 |
#include <QtWidgets/QScrollBar> |
| 28 |
#include <QtWidgets/QLineEdit> |
| 29 |
#include <QtWidgets/QTableView> |
| 30 |
#include <QtWidgets/QVBoxLayout> |
| 31 |
|
| 32 |
#include <QtCore/QSet> |
| 33 |
#include <QtCore/QSortFilterProxyModel> |
| 34 |
#include <QtCore/QStringList> |
| 35 |
|
| 36 |
#include <QtGui/QFont> |
| 37 |
#include <QtGui/QStandardItemModel> |
| 38 |
|
| 39 |
#include <cmath> |
| 40 |
#include <sstream> |
| 41 |
|
| 42 |
using Avogadro::Core::AvoSpglib; |
| 43 |
using Avogadro::QtGui::Molecule; |
| 44 |
using Avogadro::QtGui::RichTextDelegate; |
| 45 |
|
| 46 |
namespace Avogadro::QtPlugins { |
| 47 |
|
| 48 |
SpaceGroup::SpaceGroup(QObject* parent_) |
| 49 |
: Avogadro::QtGui::ExtensionPlugin(parent_), m_actions(QList<QAction*>()), |
| 50 |
m_molecule(nullptr), m_spgTol(1e-5), |
| 51 |
m_perceiveSpaceGroupAction(new QAction(this)), |
| 52 |
m_reduceToPrimitiveAction(new QAction(this)), |
| 53 |
m_conventionalizeCellAction(new QAction(this)), |
| 54 |
m_symmetrizeAction(new QAction(this)), |
| 55 |
m_fillTranslationalCellAction(new QAction(this)), |
| 56 |
m_fillUnitCellAction(new QAction(this)), |
| 57 |
m_reduceToAsymmetricUnitAction(new QAction(this)), |
| 58 |
m_setToleranceAction(new QAction(this)) |
| 59 |
{ |
| 60 |
m_perceiveSpaceGroupAction->setText(tr("Perceive Space Group…")); |
| 61 |
connect(m_perceiveSpaceGroupAction, SIGNAL(triggered()), |
| 62 |
SLOT(perceiveSpaceGroup())); |
| 63 |
m_actions.push_back(m_perceiveSpaceGroupAction); |
| 64 |
m_perceiveSpaceGroupAction->setProperty("menu priority", 90); |
| 65 |
|
| 66 |
m_reduceToPrimitiveAction->setText(tr("Reduce to Primitive")); |
| 67 |
connect(m_reduceToPrimitiveAction, SIGNAL(triggered()), |
| 68 |
SLOT(reduceToPrimitive())); |
| 69 |
m_actions.push_back(m_reduceToPrimitiveAction); |
| 70 |
m_reduceToPrimitiveAction->setProperty("menu priority", 80); |
| 71 |
|
| 72 |
m_conventionalizeCellAction->setText(tr("Conventionalize Cell")); |
| 73 |
connect(m_conventionalizeCellAction, SIGNAL(triggered()), |
| 74 |
SLOT(conventionalizeCell())); |
| 75 |
m_actions.push_back(m_conventionalizeCellAction); |
| 76 |
m_conventionalizeCellAction->setProperty("menu priority", 70); |
| 77 |
|
| 78 |
m_symmetrizeAction->setText(tr("Symmetrize")); |
| 79 |
connect(m_symmetrizeAction, SIGNAL(triggered()), SLOT(symmetrize())); |
| 80 |
m_actions.push_back(m_symmetrizeAction); |
| 81 |
m_symmetrizeAction->setProperty("menu priority", 60); |
| 82 |
|
| 83 |
m_reduceToAsymmetricUnitAction->setText(tr("Reduce to Asymmetric Unit")); |
| 84 |
connect(m_reduceToAsymmetricUnitAction, SIGNAL(triggered()), |
| 85 |
SLOT(reduceToAsymmetricUnit())); |
| 86 |
m_actions.push_back(m_reduceToAsymmetricUnitAction); |
| 87 |
m_reduceToAsymmetricUnitAction->setProperty("menu priority", 40); |
| 88 |
|
| 89 |
m_setToleranceAction->setText(tr("Set Tolerance…")); |
| 90 |
connect(m_setToleranceAction, SIGNAL(triggered()), SLOT(setTolerance())); |
| 91 |
m_actions.push_back(m_setToleranceAction); |
| 92 |
m_setToleranceAction->setProperty("menu priority", 0); |
| 93 |
|
| 94 |
// should fall next to the "Wrap Atoms to Unit Cell" action on Crystal menu |
| 95 |
m_fillUnitCellAction->setText(tr("Fill Unit Cell…")); |
| 96 |
connect(m_fillUnitCellAction, SIGNAL(triggered()), SLOT(fillUnitCell())); |
| 97 |
m_actions.push_back(m_fillUnitCellAction); |
| 98 |
m_fillUnitCellAction->setProperty("menu priority", 185); |
| 99 |
|
| 100 |
m_fillTranslationalCellAction->setText(tr( |
| 101 |
"Fill Translation Cell…", "fill the translationally unique repeat unit")); |
| 102 |
connect(m_fillTranslationalCellAction, SIGNAL(triggered()), |
| 103 |
SLOT(fillTranslationalCell())); |
| 104 |
m_actions.push_back(m_fillTranslationalCellAction); |
| 105 |
m_fillTranslationalCellAction->setProperty("menu priority", 184); |
| 106 |
|
| 107 |
updateActions(); |
| 108 |
} |
| 109 |
|
| 110 |
SpaceGroup::~SpaceGroup() |
| 111 |
{ |
| 112 |
qDeleteAll(m_actions); |
| 113 |
m_actions.clear(); |
| 114 |
} |
| 115 |
|
| 116 |
QList<QAction*> SpaceGroup::actions() const |
| 117 |
{ |
| 118 |
return m_actions; |
| 119 |
} |
| 120 |
|
| 121 |
QStringList SpaceGroup::menuPath(QAction* action) const |
| 122 |
{ |
| 123 |
if (action == m_fillUnitCellAction || action == m_fillTranslationalCellAction) |
| 124 |
return QStringList() << tr("&Crystal"); |
| 125 |
|
| 126 |
return QStringList() << tr("&Crystal") << tr("Space Group"); |
| 127 |
} |
| 128 |
|
| 129 |
void SpaceGroup::registerCommands() |
| 130 |
{ |
| 131 |
emit registerCommand( |
| 132 |
"fillUnitCell", |
| 133 |
tr("Fill symmetric atoms based on the crystal space group.")); |
| 134 |
emit registerCommand("fillTranslationalCell", |
| 135 |
tr("Fill all atoms based on the crystal space group.")); |
| 136 |
} |
| 137 |
|
| 138 |
bool SpaceGroup::handleCommand(const QString& command, |
| 139 |
[[maybe_unused]] const QVariantMap& options) |
| 140 |
{ |
| 141 |
if (m_molecule == nullptr) |
| 142 |
return false; // No molecule to handle the command. |
| 143 |
|
| 144 |
if (command == "fillUnitCell") { |
| 145 |
fillUnitCell(); |
| 146 |
return true; |
| 147 |
} else if (command == "fillTranslationalCell") { |
| 148 |
fillTranslationalCell(); |
| 149 |
return true; |
| 150 |
} |
| 151 |
return false; |
| 152 |
} |
| 153 |
|
| 154 |
const QString SpaceGroup::toleranceToString() |
| 155 |
{ |
| 156 |
// Convert to scientific notation to extract mantissa and exponent |
| 157 |
int exponent = static_cast<int>(std::floor(std::log10(m_spgTol))); |
| 158 |
double mantissa = m_spgTol / std::pow(10.0, exponent); |
| 159 |
|
| 160 |
// UTF-8 superscript characters |
| 161 |
const QString superscriptDigits[] = { |
| 162 |
QStringLiteral("⁰"), QStringLiteral("¹"), QStringLiteral("²"), |
| 163 |
QStringLiteral("³"), QStringLiteral("⁴"), QStringLiteral("⁵"), |
| 164 |
QStringLiteral("⁶"), QStringLiteral("⁷"), QStringLiteral("⁸"), |
| 165 |
QStringLiteral("⁹") |
| 166 |
}; |
| 167 |
const QString superscriptMinus = QStringLiteral("⁻"); |
| 168 |
|
| 169 |
// Build the exponent string with superscripts |
| 170 |
QString expStr; |
| 171 |
int absExp = std::abs(exponent); |
| 172 |
if (exponent < 0) |
| 173 |
expStr += superscriptMinus; |
| 174 |
|
| 175 |
QString digits = QString::number(absExp); |
| 176 |
for (const QChar& c : digits) { |
| 177 |
expStr += superscriptDigits[c.digitValue()]; |
| 178 |
} |
| 179 |
|
| 180 |
return QStringLiteral("%1 × 10%2").arg(mantissa, 0, 'f', 1).arg(expStr); |
| 181 |
} |
| 182 |
|
| 183 |
const QString SpaceGroup::symbolToString(unsigned short hallNumber, |
| 184 |
bool replaceOverlines) |
| 185 |
{ |
| 186 |
QString symbol(Core::SpaceGroups::internationalShort(hallNumber)); |
| 187 |
|
| 188 |
// Replace -N notation with N̅ (number with combining overline U+0305) |
| 189 |
// for rotoinversion axes in Hermann-Mauguin symbols |
| 190 |
if (replaceOverlines) { |
| 191 |
QString htmlOverline( |
| 192 |
"<span style=\"text-decoration: overline;\">%1</span>"); |
| 193 |
symbol.replace(QStringLiteral("-1"), htmlOverline.arg(QStringLiteral("1"))); |
| 194 |
symbol.replace(QStringLiteral("-2"), htmlOverline.arg(QStringLiteral("2"))); |
| 195 |
symbol.replace(QStringLiteral("-3"), htmlOverline.arg(QStringLiteral("3"))); |
| 196 |
symbol.replace(QStringLiteral("-4"), htmlOverline.arg(QStringLiteral("4"))); |
| 197 |
symbol.replace(QStringLiteral("-6"), htmlOverline.arg(QStringLiteral("6"))); |
| 198 |
} |
| 199 |
|
| 200 |
// Replace screw axis notation with subscripts |
| 201 |
// e.g., "21" -> "2₁", "42" -> "4₂", etc. |
| 202 |
// Unicode subscript digits: ₁=U+2081, ₂=U+2082, ₃=U+2083, ₄=U+2084, ₅=U+2085 |
| 203 |
symbol.replace(QStringLiteral("2_1"), QStringLiteral("2₁")); |
| 204 |
symbol.replace(QStringLiteral("3_1"), QStringLiteral("3₁")); |
| 205 |
symbol.replace(QStringLiteral("3_2"), QStringLiteral("3₂")); |
| 206 |
symbol.replace(QStringLiteral("4_1"), QStringLiteral("4₁")); |
| 207 |
symbol.replace(QStringLiteral("4_2"), QStringLiteral("4₂")); |
| 208 |
symbol.replace(QStringLiteral("4_3"), QStringLiteral("4₃")); |
| 209 |
symbol.replace(QStringLiteral("6_1"), QStringLiteral("6₁")); |
| 210 |
symbol.replace(QStringLiteral("6_2"), QStringLiteral("6₂")); |
| 211 |
symbol.replace(QStringLiteral("6_3"), QStringLiteral("6₃")); |
| 212 |
symbol.replace(QStringLiteral("6_4"), QStringLiteral("6₄")); |
| 213 |
symbol.replace(QStringLiteral("6_5"), QStringLiteral("6₅")); |
| 214 |
|
| 215 |
return symbol; |
| 216 |
} |
| 217 |
|
| 218 |
const QString SpaceGroup::hallSymbolToString(unsigned short hallNumber) |
| 219 |
{ |
| 220 |
// Format a Hall symbol like "-P 2yc" with proper super/subscripts and |
| 221 |
// overlines |
| 222 |
QString symbol(Core::SpaceGroups::hallSymbol(hallNumber)); |
| 223 |
QString newSymbol; |
| 224 |
|
| 225 |
// Superscript characters for axis directions |
| 226 |
const QSet<QChar> superscriptChars = { 'x', 'y', 'z' }; |
| 227 |
|
| 228 |
// HTML template for overline |
| 229 |
QString htmlOverline("<span style=\"text-decoration: overline;\">%1</span>"); |
| 230 |
|
| 231 |
// Split by spaces and process each token |
| 232 |
QStringList tokens = symbol.split(' '); |
| 233 |
bool firstToken = true; |
| 234 |
|
| 235 |
for (const QString& token : tokens) { |
| 236 |
if (!firstToken) |
| 237 |
newSymbol += ' '; |
| 238 |
firstToken = false; |
| 239 |
|
| 240 |
// Parenthesized tokens like "(0 0 1)" are output as-is |
| 241 |
if (token.startsWith('(')) { |
| 242 |
newSymbol += token; |
| 243 |
continue; |
| 244 |
} |
| 245 |
|
| 246 |
QString mainPart; |
| 247 |
QString superPart; |
| 248 |
QString subPart; |
| 249 |
QString tailPart; // For *, ″ (from =), etc. that don't get subscripted |
| 250 |
bool pastMain = false; |
| 251 |
|
| 252 |
for (int i = 0; i < token.length(); ++i) { |
| 253 |
QChar c = token[i]; |
| 254 |
|
| 255 |
// "-" means overline on the next character |
| 256 |
if (c == '-' && i + 1 < token.length()) { |
| 257 |
QChar next = token[i + 1]; |
| 258 |
mainPart += htmlOverline.arg(next); |
| 259 |
++i; // Skip the next character since we processed it |
| 260 |
pastMain = true; |
| 261 |
continue; |
| 262 |
} |
| 263 |
|
| 264 |
if (!pastMain) { |
| 265 |
// This is the main rotation/lattice symbol - keep as-is |
| 266 |
mainPart += c; |
| 267 |
pastMain = true; |
| 268 |
} else { |
| 269 |
// Past the main symbol: x/y/z → superscript, others → subscript |
| 270 |
if (superscriptChars.contains(c)) { |
| 271 |
superPart += c; |
| 272 |
} else if (c == '*' || c == ')') { |
| 273 |
tailPart += c; |
| 274 |
} else if (c == '=') { |
| 275 |
// Double-prime character |
| 276 |
tailPart += QStringLiteral("″"); |
| 277 |
} else { |
| 278 |
subPart += c; |
| 279 |
} |
| 280 |
} |
| 281 |
} |
| 282 |
|
| 283 |
// Build the token: main + superscripts + grouped subscript + tail |
| 284 |
newSymbol += mainPart; |
| 285 |
if (!superPart.isEmpty()) { |
| 286 |
newSymbol += |
| 287 |
QStringLiteral("<sup>") + superPart + QStringLiteral("</sup>"); |
| 288 |
} |
| 289 |
if (!subPart.isEmpty()) { |
| 290 |
newSymbol += QStringLiteral("<sub>") + subPart + QStringLiteral("</sub>"); |
| 291 |
} |
| 292 |
newSymbol += tailPart; |
| 293 |
} |
| 294 |
|
| 295 |
return newSymbol; |
| 296 |
} |
| 297 |
|
| 298 |
void SpaceGroup::fillHeuristic() |
| 299 |
{ |
| 300 |
// Don't re-fill when we just performed a reduce/conventionalize/symmetrize |
| 301 |
if (m_blockFillHeuristic) |
| 302 |
return; |
| 303 |
|
| 304 |
// add a heuristic to completely fill the cell if it's a solid |
| 305 |
// (vs. a molecule) — skip if the cell was already filled |
| 306 |
if (m_molecule != nullptr && m_molecule->unitCell()) { |
| 307 |
// check if there's carbon and hydrogen atoms and at least 5 total atoms |
| 308 |
bool hasCarbon = false; |
| 309 |
bool hasHydrogen = false; |
| 310 |
|
| 311 |
for (unsigned int i = 0; i < m_molecule->atomCount(); ++i) { |
| 312 |
if (m_molecule->atom(i).atomicNumber() == 6) |
| 313 |
hasCarbon = true; |
| 314 |
else if (m_molecule->atom(i).atomicNumber() == 1) |
| 315 |
hasHydrogen = true; |
| 316 |
|
| 317 |
if (hasCarbon && hasHydrogen) |
| 318 |
break; |
| 319 |
} |
| 320 |
|
| 321 |
if (m_molecule->atomCount() <= 250 && |
| 322 |
(m_molecule->atomCount() <= 5 || !(hasCarbon && hasHydrogen))) { |
| 323 |
fillUnitCell(); |
| 324 |
} |
| 325 |
} |
| 326 |
} |
| 327 |
|
| 328 |
void SpaceGroup::setMolecule(QtGui::Molecule* mol) |
| 329 |
{ |
| 330 |
if (m_molecule == mol) |
| 331 |
return; |
| 332 |
|
| 333 |
if (m_molecule) |
| 334 |
m_molecule->disconnect(this); |
| 335 |
|
| 336 |
m_molecule = mol; |
| 337 |
|
| 338 |
if (m_molecule) |
| 339 |
connect(m_molecule, SIGNAL(changed(uint)), SLOT(moleculeChanged(uint))); |
| 340 |
|
| 341 |
updateActions(); |
| 342 |
|
| 343 |
// possibly fill the cell |
| 344 |
fillHeuristic(); |
| 345 |
} |
| 346 |
|
| 347 |
void SpaceGroup::moleculeChanged(unsigned int c) |
| 348 |
{ |
| 349 |
Q_ASSERT(m_molecule == qobject_cast<Molecule*>(sender())); |
| 350 |
|
| 351 |
auto changes = static_cast<Molecule::MoleculeChanges>(c); |
| 352 |
|
| 353 |
if (changes & Molecule::UnitCell) { |
| 354 |
if (changes & Molecule::Added || changes & Molecule::Removed) |
| 355 |
updateActions(); |
| 356 |
|
| 357 |
// Only fill when a new crystal is imported (not on every modification) |
| 358 |
if (changes & Molecule::NewCrystal) |
| 359 |
fillHeuristic(); |
| 360 |
} |
| 361 |
} |
| 362 |
|
| 363 |
void SpaceGroup::updateActions() |
| 364 |
{ |
| 365 |
// Disable everything for nullptr molecules. |
| 366 |
if (!m_molecule) { |
| 367 |
foreach (QAction* action, m_actions) |
| 368 |
action->setEnabled(false); |
| 369 |
return; |
| 370 |
} |
| 371 |
|
| 372 |
if (m_molecule->unitCell()) { |
| 373 |
foreach (QAction* action, m_actions) |
| 374 |
action->setEnabled(true); |
| 375 |
|
| 376 |
} else { |
| 377 |
foreach (QAction* action, m_actions) |
| 378 |
action->setEnabled(false); |
| 379 |
} |
| 380 |
} |
| 381 |
|
| 382 |
void SpaceGroup::perceiveSpaceGroup() |
| 383 |
{ |
| 384 |
// only do this if we don't have a Hall number set |
| 385 |
if (m_molecule == nullptr) |
| 386 |
return; |
| 387 |
|
| 388 |
if (m_molecule->hallNumber() != 0) { |
| 389 |
// Ask if the user wants to overwrite the current space group |
| 390 |
std::string hallSymbol = |
| 391 |
Core::SpaceGroups::hallSymbol(m_molecule->hallNumber()); |
| 392 |
|
| 393 |
QMessageBox::StandardButton reply; |
| 394 |
reply = QMessageBox::question(nullptr, tr("Perceive Space Group"), |
| 395 |
tr("The space group is already set to: %1.\n" |
| 396 |
"Would you like to overwrite it?") |
| 397 |
.arg(hallSymbol.c_str()), |
| 398 |
QMessageBox::Yes | QMessageBox::No); |
| 399 |
if (reply == QMessageBox::No) |
| 400 |
return; |
| 401 |
} |
| 402 |
|
| 403 |
unsigned short hallNumber = AvoSpglib::getHallNumber(*m_molecule, m_spgTol); |
| 404 |
unsigned short intNum = Core::SpaceGroups::internationalNumber(hallNumber); |
| 405 |
std::string hallSymbol = Core::SpaceGroups::hallSymbol(hallNumber); |
| 406 |
QString intShort = symbolToString(hallNumber); |
| 407 |
|
| 408 |
// Success! |
| 409 |
if (hallNumber != 0) { |
| 410 |
// Let's make the message |
| 411 |
QString message = tr("Space group perception succeeded:\n" |
| 412 |
"Tolerance: %1 Å\n" |
| 413 |
"Space Group: %2\n" |
| 414 |
"Hall symbol: %3\n" |
| 415 |
"International symbol: %4") |
| 416 |
.arg(toleranceToString()) |
| 417 |
.arg(intNum) |
| 418 |
.arg(hallSymbol.c_str()) |
| 419 |
.arg(intShort); |
| 420 |
|
| 421 |
// Now let's make the Message Box |
| 422 |
QMessageBox::information(nullptr, tr("Perceive Space Group"), message); |
| 423 |
} |
| 424 |
// Failure |
| 425 |
else { |
| 426 |
// Ask if the user wants to try again with a different tolerance |
| 427 |
QMessageBox::StandardButton reply; |
| 428 |
reply = QMessageBox::question(nullptr, tr("Failure"), |
| 429 |
tr("Space group perception failed.\n" |
| 430 |
"Would you like to try again with a " |
| 431 |
"different tolerance?"), |
| 432 |
QMessageBox::Yes | QMessageBox::No); |
| 433 |
if (reply == QMessageBox::Yes) { |
| 434 |
setTolerance(); |
| 435 |
perceiveSpaceGroup(); // Recursion! |
| 436 |
} |
| 437 |
} |
| 438 |
} |
| 439 |
|
| 440 |
void SpaceGroup::reduceToPrimitive() |
| 441 |
{ |
| 442 |
// Confirm the tolerance |
| 443 |
QMessageBox::StandardButton reply; |
| 444 |
reply = QMessageBox::question(nullptr, tr("Primitive Reduction"), |
| 445 |
tr("The tolerance is currently set to: %1.\n" |
| 446 |
"Proceed with this tolerance?") |
| 447 |
.arg(toleranceToString()), |
| 448 |
QMessageBox::Yes | QMessageBox::No); |
| 449 |
if (reply == QMessageBox::No) |
| 450 |
setTolerance(); |
| 451 |
|
| 452 |
// Primitive reduction (with boundary copies in the same undo step) |
| 453 |
m_blockFillHeuristic = true; |
| 454 |
bool success = |
| 455 |
m_molecule->undoMolecule()->reduceCellToPrimitive(m_spgTol, 0.25); |
| 456 |
m_blockFillHeuristic = false; |
| 457 |
|
| 458 |
if (!success) { |
| 459 |
// Print an error message. |
| 460 |
QMessageBox retMsgBox; |
| 461 |
retMsgBox.setText(tr("Primitive reduction failed.\n" |
| 462 |
"Please check your crystal and try again " |
| 463 |
"with a different tolerance.")); |
| 464 |
retMsgBox.exec(); |
| 465 |
} |
| 466 |
} |
| 467 |
|
| 468 |
void SpaceGroup::conventionalizeCell() |
| 469 |
{ |
| 470 |
// Confirm the tolerance |
| 471 |
QMessageBox::StandardButton reply; |
| 472 |
reply = QMessageBox::question(nullptr, tr("Conventionalize Cell"), |
| 473 |
tr("The tolerance is currently set to: %1.\n" |
| 474 |
"Proceed with this tolerance?") |
| 475 |
.arg(toleranceToString()), |
| 476 |
QMessageBox::Yes | QMessageBox::No); |
| 477 |
if (reply == QMessageBox::No) |
| 478 |
setTolerance(); |
| 479 |
|
| 480 |
// Conventionalize the cell (with boundary copies in the same undo step) |
| 481 |
m_blockFillHeuristic = true; |
| 482 |
bool success = |
| 483 |
m_molecule->undoMolecule()->conventionalizeCell(m_spgTol, 0.25); |
| 484 |
m_blockFillHeuristic = false; |
| 485 |
|
| 486 |
if (!success) { |
| 487 |
// Print an error message. |
| 488 |
QMessageBox retMsgBox; |
| 489 |
retMsgBox.setText(tr("Conventionalize cell failed.\n" |
| 490 |
"Please check your crystal and try again " |
| 491 |
"with a different tolerance.")); |
| 492 |
retMsgBox.exec(); |
| 493 |
} |
| 494 |
} |
| 495 |
|
| 496 |
void SpaceGroup::symmetrize() |
| 497 |
{ |
| 498 |
// Confirm the tolerance |
| 499 |
QMessageBox::StandardButton reply; |
| 500 |
reply = QMessageBox::question(nullptr, tr("Symmetrize Cell"), |
| 501 |
tr("The tolerance is currently set to: %1.\n" |
| 502 |
"Proceed with this tolerance?") |
| 503 |
.arg(toleranceToString()), |
| 504 |
QMessageBox::Yes | QMessageBox::No); |
| 505 |
if (reply == QMessageBox::No) |
| 506 |
setTolerance(); |
| 507 |
|
| 508 |
m_blockFillHeuristic = true; |
| 509 |
bool success = m_molecule->undoMolecule()->symmetrizeCell(m_spgTol, 0.25); |
| 510 |
m_blockFillHeuristic = false; |
| 511 |
|
| 512 |
if (!success) { |
| 513 |
// Print an error message. |
| 514 |
QMessageBox retMsgBox; |
| 515 |
retMsgBox.setText(tr("Symmetrization failed.\n" |
| 516 |
"Please check your crystal and try again " |
| 517 |
"with a different tolerance.")); |
| 518 |
retMsgBox.exec(); |
| 519 |
} |
| 520 |
} |
| 521 |
|
| 522 |
void SpaceGroup::fillUnitCell() |
| 523 |
{ |
| 524 |
unsigned short hallNumber = m_molecule->hallNumber(); |
| 525 |
|
| 526 |
// If it's not set, ask the user to select a space group |
| 527 |
if (hallNumber == 0) |
| 528 |
hallNumber = selectSpaceGroup(); |
| 529 |
// If the hall number is zero, the user canceled |
| 530 |
if (hallNumber == 0) |
| 531 |
return; |
| 532 |
|
| 533 |
if (!checkPrimitiveCell(hallNumber)) |
| 534 |
return; |
| 535 |
|
| 536 |
// true here to fill all copies, including edges and corners |
| 537 |
// 0.25 indicates the distance in A that two atoms must be apart |
| 538 |
m_molecule->undoMolecule()->fillUnitCell(hallNumber, 0.25, true); |
| 539 |
} |
| 540 |
|
| 541 |
void SpaceGroup::fillTranslationalCell() |
| 542 |
{ |
| 543 |
unsigned short hallNumber = m_molecule->hallNumber(); |
| 544 |
|
| 545 |
// If it's not set, ask the user to select a space group |
| 546 |
if (hallNumber == 0) |
| 547 |
hallNumber = selectSpaceGroup(); |
| 548 |
// If the hall number is zero, the user canceled |
| 549 |
if (hallNumber == 0) |
| 550 |
return; |
| 551 |
|
| 552 |
if (!checkPrimitiveCell(hallNumber)) |
| 553 |
return; |
| 554 |
|
| 555 |
// 0.25 indicates the distance in A that two atoms must be apart |
| 556 |
m_molecule->undoMolecule()->fillUnitCell(hallNumber, 0.25); |
| 557 |
} |
| 558 |
|
| 559 |
void SpaceGroup::reduceToAsymmetricUnit() |
| 560 |
{ |
| 561 |
// Let's gather some information about the space group first |
| 562 |
unsigned short hallNumber = AvoSpglib::getHallNumber(*m_molecule, m_spgTol); |
| 563 |
unsigned short intNum = Core::SpaceGroups::internationalNumber(hallNumber); |
| 564 |
std::string hallSymbol = Core::SpaceGroups::hallSymbol(hallNumber); |
| 565 |
QString intShort = symbolToString(hallNumber); |
| 566 |
|
| 567 |
// Ask the user if he/she wants to use this space group |
| 568 |
QString message = |
| 569 |
tr("With a tolerance of %1 Å, " |
| 570 |
"the space group information was perceived to be the following:\n" |
| 571 |
"International number: %2\n" |
| 572 |
"Hall symbol: %3\n" |
| 573 |
"International symbol: %4\n\n" |
| 574 |
"Proceed with this space group?") |
| 575 |
.arg(toleranceToString()) |
| 576 |
.arg(intNum) |
| 577 |
.arg(hallSymbol.c_str()) |
| 578 |
.arg(intShort); |
| 579 |
QMessageBox::StandardButton reply; |
| 580 |
reply = QMessageBox::question(nullptr, tr("Reduce to Asymmetric Unit"), |
| 581 |
message, QMessageBox::Yes | QMessageBox::No); |
| 582 |
|
| 583 |
// If the user does not want to use the perceived space group, |
| 584 |
// let the user set it. |
| 585 |
if (reply == QMessageBox::No) |
| 586 |
hallNumber = selectSpaceGroup(); |
| 587 |
|
| 588 |
// If 0 was set, that means the user cancelled |
| 589 |
if (hallNumber == 0) |
| 590 |
return; |
| 591 |
|
| 592 |
// Perform the operation! |
| 593 |
m_molecule->undoMolecule()->reduceCellToAsymmetricUnit(hallNumber, m_spgTol); |
| 594 |
} |
| 595 |
|
| 596 |
void SpaceGroup::setTolerance() |
| 597 |
{ |
| 598 |
bool ok; |
| 599 |
double tol = QInputDialog::getDouble(nullptr, |
| 600 |
tr("Avogadro2"), // title |
| 601 |
tr("Select tolerance in Å:"), // label |
| 602 |
m_spgTol, // initial |
| 603 |
1e-5, // min |
| 604 |
0.5, // max |
| 605 |
5, // decimals |
| 606 |
&ok); |
| 607 |
if (!ok) |
| 608 |
return; |
| 609 |
|
| 610 |
m_spgTol = tol; |
| 611 |
} |
| 612 |
|
| 613 |
const QString SpaceGroup::crystalSystem(unsigned short hallNumber) |
| 614 |
{ |
| 615 |
auto cs = Core::SpaceGroups::crystalSystem(hallNumber); |
| 616 |
switch (cs) { |
| 617 |
case Core::Triclinic: |
| 618 |
return tr("Triclinic"); |
| 619 |
case Core::Monoclinic: |
| 620 |
return tr("Monoclinic"); |
| 621 |
case Core::Orthorhombic: |
| 622 |
return tr("Orthorhombic"); |
| 623 |
case Core::Tetragonal: |
| 624 |
return tr("Tetragonal"); |
| 625 |
case Core::Trigonal: |
| 626 |
return tr("Trigonal"); |
| 627 |
case Core::Rhombohedral: |
| 628 |
return tr("Rhombohedral"); |
| 629 |
case Core::Hexagonal: |
| 630 |
return tr("Hexagonal"); |
| 631 |
case Core::Cubic: |
| 632 |
return tr("Cubic"); |
| 633 |
default: |
| 634 |
return tr("Unknown"); |
| 635 |
} |
| 636 |
} |
| 637 |
|
| 638 |
bool SpaceGroup::checkPrimitiveCell(unsigned short hallNumber) |
| 639 |
{ |
| 640 |
// Check if the cell appears to be primitive but the space group expects |
| 641 |
// a centered cell. This can cause unexpected atom duplication. |
| 642 |
std::string hallSymbol = Core::SpaceGroups::hallSymbol(hallNumber); |
| 643 |
if (!hallSymbol.empty()) { |
| 644 |
char centering = hallSymbol[0]; |
| 645 |
// F, I, A, B, C are centered lattice types |
| 646 |
if (centering == 'F' || centering == 'I' || centering == 'A' || |
| 647 |
centering == 'B' || centering == 'C') { |
| 648 |
// Check if cell angles deviate significantly from 90 degrees |
| 649 |
// which would suggest a primitive cell basis |
| 650 |
Core::UnitCell* uc = m_molecule->unitCell(); |
| 651 |
if (uc) { |
| 652 |
double alpha = uc->alpha() * 180.0 / M_PI; |
| 653 |
double beta = uc->beta() * 180.0 / M_PI; |
| 654 |
double gamma = uc->gamma() * 180.0 / M_PI; |
| 655 |
double tolerance = 5.0; // degrees |
| 656 |
|
| 657 |
bool nonConventionalAngles = (std::abs(alpha - 90.0) > tolerance) || |
| 658 |
(std::abs(beta - 90.0) > tolerance) || |
| 659 |
(std::abs(gamma - 90.0) > tolerance); |
| 660 |
|
| 661 |
if (nonConventionalAngles) { |
| 662 |
QMessageBox::StandardButton reply; |
| 663 |
reply = QMessageBox::warning( |
| 664 |
nullptr, tr("Primitive Cell Detected"), |
| 665 |
tr( |
| 666 |
"The current unit cell appears to be a primitive cell " |
| 667 |
"(non-90° angles), but the space group %1 expects a " |
| 668 |
"centered conventional cell.\n\n" |
| 669 |
"Filling the unit cell may create unexpected duplicate atoms.\n\n" |
| 670 |
"Would you like to create a conventional cell first?") |
| 671 |
.arg(hallSymbol.c_str()), |
| 672 |
QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel); |
| 673 |
|
| 674 |
if (reply == QMessageBox::Cancel) |
| 675 |
return false; |
| 676 |
if (reply == QMessageBox::Yes) { |
| 677 |
// Conventionalize first, then fill |
| 678 |
if (!m_molecule->undoMolecule()->conventionalizeCell(m_spgTol)) { |
| 679 |
QMessageBox::warning(nullptr, tr("Error"), |
| 680 |
tr("Failed to conventionalize the cell.")); |
| 681 |
return false; |
| 682 |
} |
| 683 |
} |
| 684 |
// If No, continue with fill anyway (user's choice) |
| 685 |
} |
| 686 |
} |
| 687 |
} |
| 688 |
} |
| 689 |
return true; |
| 690 |
} |
| 691 |
|
| 692 |
unsigned short SpaceGroup::selectSpaceGroup() |
| 693 |
{ |
| 694 |
QStandardItemModel spacegroups; |
| 695 |
QStringList modelHeader; |
| 696 |
modelHeader << tr("International") << tr("Hall") << tr("Hermann-Mauguin") |
| 697 |
<< tr("Crystal System"); |
| 698 |
spacegroups.setHorizontalHeaderLabels(modelHeader); |
| 699 |
for (unsigned short i = 1; i <= 530; ++i) { |
| 700 |
QList<QStandardItem*> row; |
| 701 |
// Use setData with int type so the column sorts numerically |
| 702 |
auto* intItem = new QStandardItem(); |
| 703 |
intItem->setData(Core::SpaceGroups::internationalNumber(i), |
| 704 |
Qt::DisplayRole); |
| 705 |
intItem->setEditable(false); |
| 706 |
intItem->setTextAlignment(Qt::AlignCenter); |
| 707 |
auto* hallItem = new QStandardItem(hallSymbolToString(i)); |
| 708 |
hallItem->setEditable(false); |
| 709 |
hallItem->setTextAlignment(Qt::AlignCenter); |
| 710 |
// true = replace the overlines with HTML |
| 711 |
auto* hmItem = new QStandardItem(symbolToString(i, true)); |
| 712 |
hmItem->setEditable(false); |
| 713 |
hmItem->setTextAlignment(Qt::AlignCenter); |
| 714 |
auto* csItem = new QStandardItem(crystalSystem(i)); |
| 715 |
csItem->setEditable(false); |
| 716 |
csItem->setTextAlignment(Qt::AlignCenter); |
| 717 |
row << intItem << hallItem << hmItem << csItem; |
| 718 |
spacegroups.appendRow(row); |
| 719 |
} |
| 720 |
|
| 721 |
// Create a proxy model for filtering |
| 722 |
QSortFilterProxyModel proxyModel; |
| 723 |
proxyModel.setSourceModel(&spacegroups); |
| 724 |
proxyModel.setFilterCaseSensitivity(Qt::CaseInsensitive); |
| 725 |
proxyModel.setFilterKeyColumn(-1); // Search all columns |
| 726 |
|
| 727 |
QDialog dialog; |
| 728 |
dialog.setLayout(new QVBoxLayout); |
| 729 |
dialog.setWindowTitle(tr("Select Space Group")); |
| 730 |
|
| 731 |
// Add search box |
| 732 |
auto* searchBox = new QLineEdit; |
| 733 |
searchBox->setClearButtonEnabled(true); |
| 734 |
|
| 735 |
// Pre-filter by crystal system based on unit cell parameters |
| 736 |
if (m_molecule && m_molecule->unitCell()) { |
| 737 |
Core::UnitCell* uc = m_molecule->unitCell(); |
| 738 |
double a = uc->a(); |
| 739 |
double b = uc->b(); |
| 740 |
double c = uc->c(); |
| 741 |
double alpha = uc->alpha() * 180.0 / M_PI; |
| 742 |
double beta = uc->beta() * 180.0 / M_PI; |
| 743 |
double gamma = uc->gamma() * 180.0 / M_PI; |
| 744 |
double angTol = 1.0; // degrees |
| 745 |
double lenTol = 0.01; // fractional |
| 746 |
|
| 747 |
bool eq_ab = std::abs(a - b) < lenTol * a; |
| 748 |
bool eq_bc = std::abs(b - c) < lenTol * b; |
| 749 |
bool eq_abc = eq_ab && eq_bc; |
| 750 |
bool a90 = std::abs(alpha - 90.0) < angTol; |
| 751 |
bool b90 = std::abs(beta - 90.0) < angTol; |
| 752 |
bool g90 = std::abs(gamma - 90.0) < angTol; |
| 753 |
bool g120 = std::abs(gamma - 120.0) < angTol; |
| 754 |
bool eq_angles = |
| 755 |
std::abs(alpha - beta) < angTol && std::abs(beta - gamma) < angTol; |
| 756 |
|
| 757 |
if (eq_abc && a90 && b90 && g90) |
| 758 |
searchBox->setText(tr("Cubic")); |
| 759 |
else if (eq_ab && a90 && b90 && g120) |
| 760 |
searchBox->setText(tr("Hexagonal")); |
| 761 |
else if (eq_ab && a90 && b90 && g90) |
| 762 |
searchBox->setText(tr("Tetragonal")); |
| 763 |
else if (eq_abc && eq_angles && !a90) |
| 764 |
searchBox->setText(tr("Rhombohedral")); |
| 765 |
else if (a90 && b90 && g90) |
| 766 |
searchBox->setText(tr("Orthorhombic")); |
| 767 |
else if ((a90 && g90 && !b90) || (a90 && b90 && !g90) || |
| 768 |
(!a90 && b90 && g90)) |
| 769 |
searchBox->setText(tr("Monoclinic")); |
| 770 |
} |
| 771 |
|
| 772 |
dialog.layout()->addWidget(searchBox); |
| 773 |
|
| 774 |
auto* view = new QTableView; |
| 775 |
|
| 776 |
// Rich text delegate for symbol |
| 777 |
auto* symbolDelegate = new RichTextDelegate(this); |
| 778 |
view->setItemDelegateForColumn(1, symbolDelegate); |
| 779 |
view->setItemDelegateForColumn(2, symbolDelegate); |
| 780 |
|
| 781 |
QFont font = view->font(); |
| 782 |
font.setPointSize(font.pointSize() + 1); |
| 783 |
view->setFont(font); |
| 784 |
view->setAlternatingRowColors(true); |
| 785 |
view->setSelectionBehavior(QAbstractItemView::SelectRows); |
| 786 |
view->setSelectionMode(QAbstractItemView::SingleSelection); |
| 787 |
view->setCornerButtonEnabled(false); |
| 788 |
view->setVerticalScrollMode(QAbstractItemView::ScrollPerPixel); |
| 789 |
view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); |
| 790 |
view->verticalHeader()->hide(); |
| 791 |
view->setModel(&proxyModel); |
| 792 |
view->setSortingEnabled(true); |
| 793 |
view->sortByColumn(0, Qt::AscendingOrder); |
| 794 |
view->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch); |
| 795 |
dialog.layout()->addWidget(view); |
| 796 |
view->resizeColumnsToContents(); |
| 797 |
view->resizeRowsToContents(); |
| 798 |
view->setMinimumWidth(view->horizontalHeader()->length() + |
| 799 |
view->verticalScrollBar()->sizeHint().width()); |
| 800 |
|
| 801 |
// Connect search box to filter |
| 802 |
QObject::connect(searchBox, &QLineEdit::textChanged, &proxyModel, |
| 803 |
&QSortFilterProxyModel::setFilterFixedString); |
| 804 |
// Apply the pre-populated filter |
| 805 |
proxyModel.setFilterFixedString(searchBox->text()); |
| 806 |
if (proxyModel.rowCount() > 0) |
| 807 |
view->selectRow(0); |
| 808 |
|
| 809 |
connect(view, SIGNAL(activated(QModelIndex)), &dialog, SLOT(accept())); |
| 810 |
auto* buttons = |
| 811 |
new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel); |
| 812 |
connect(buttons, SIGNAL(accepted()), &dialog, SLOT(accept())); |
| 813 |
connect(buttons, SIGNAL(rejected()), &dialog, SLOT(reject())); |
| 814 |
dialog.layout()->addWidget(buttons); |
| 815 |
if (dialog.exec() != QDialog::Accepted) |
| 816 |
return 0; |
| 817 |
|
| 818 |
// Map the proxy index back to the source model to get the hall number |
| 819 |
QModelIndex proxyIndex = view->currentIndex(); |
| 820 |
QModelIndex sourceIndex = proxyModel.mapToSource(proxyIndex); |
| 821 |
return sourceIndex.row() + 1; |
| 822 |
} |
| 823 |
|
| 824 |
} // namespace Avogadro::QtPlugins |