Skip to content

Commit

Permalink
Tidy up globals
Browse files Browse the repository at this point in the history
  • Loading branch information
peterspackman committed Oct 18, 2024
1 parent 102759a commit 746976f
Show file tree
Hide file tree
Showing 11 changed files with 120 additions and 335 deletions.
51 changes: 16 additions & 35 deletions src/core/globals.h
Original file line number Diff line number Diff line change
@@ -1,43 +1,24 @@
#pragma once
inline const char *CE_NAME = "CrystalExplorer";
inline const char *GLOBAL_MAINWINDOW_TITLE = CE_NAME;

inline const char *CE_AUTHORS =
namespace cx::globals {
inline constexpr const char *name = "CrystalExplorer";
inline constexpr const char *mainWindowTitle = name;

inline constexpr const char *authors =
"P.R. Spackman, M. J. Turner, J. J. McKinnon,<br>S. K. Wolff, D. "
"J. Grimwood, <br>D. Jayatilaka and M. A. Spackman";
inline const char *CE_URL = "https://crystalexplorer.net";
inline const char *CE_CITATION_URL =
inline constexpr const char *url = "https://crystalexplorer.net";
inline constexpr const char *citationUrl =
"https://crystalexplorer.net/docs/how-to-cite.html";
inline const char *CE_EMAIL = "[email protected]";
inline const char *COPYRIGHT_NOTICE =
inline constexpr const char *email = "[email protected]";
inline constexpr const char *copyrightNoticeTemplate =
"&copy;2005-%1 CrystalExplorer authors"; // Insert current year at
// %1
inline const char *OCC_URL = "https://github.com/peterspackman/occ";

inline const char *ANGSTROM_SYMBOL{"Å"}; // Å
inline const char *DEGREE_SYMBOL{"°"}; // °
inline const char *SQUARED_SYMBOL{"²"}; // ²
inline const char *CUBED_SYMBOL{"³"}; // ³

inline const char *ANY_ITEM = "Any"; // Used by CloseContactsDialog
// Used by CloseContactsDialog and Crystal to define the default distance
// for close contacts where X = "Any" and Y = "Any"
inline const double GLOBAL_CC_DISTANCE_CRITERIA = 3.3;

inline const int CC1_INDEX = 0;
inline const int CC2_INDEX = 1;
inline const int CC3_INDEX = 2;
inline const int CCMAX_INDEX = CC3_INDEX;

inline const double GLOBAL_FRONT_CLIPPING_PLANE = 10.0;
// %1
inline constexpr const char *occUrl = "https://github.com/peterspackman/occ";

// When calculating close contacts we adjust the sum of the VdW radii by this
// factor so the user has access to more close contacts than just those that are
// less than the sum of the VdW radii.
// This factor should at least 1.0 because this would just be the sum of the VdW
// radii.
inline const double CLOSECONTACT_FACTOR = 1.5;
inline constexpr const char *angstromSymbol{"Å"}; // Å
inline constexpr const char *degreeSymbol{"°"}; // °

// Used by SurfaceGenerationDialog and EnergyCalculationDialog to default
// wavefunction combobox option
inline const char *NEW_WAVEFUNCTION_ITEM = "Generate New Wavefunction";
inline constexpr const char *anyItemLabel = "Any";
inline constexpr const double frontClippingPlane = 10.0;
} // namespace cx::globals
48 changes: 25 additions & 23 deletions src/crystalx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -547,10 +547,10 @@ void Crystalx::enableExperimentalFeatures(bool enable) {
}

void Crystalx::gotoCrystalExplorerWebsite() {
QDesktopServices::openUrl(QUrl(CE_URL));
QDesktopServices::openUrl(QUrl(cx::globals::url));
}
void Crystalx::gotoHowToCiteCrystalExplorer() {
QDesktopServices::openUrl(QUrl(CE_CITATION_URL));
QDesktopServices::openUrl(QUrl(cx::globals::citationUrl));
}

void Crystalx::setShowAtomsWithinRadius() {
Expand All @@ -560,9 +560,9 @@ void Crystalx::setShowAtomsWithinRadius() {
QString title = "Show atoms within a radius...";

QString label;
QString msgStart =
QString("Show atoms within a ") +
DialogHtml::bold(QString("radius (") + ANGSTROM_SYMBOL + ")");
QString msgStart = QString("Show atoms within a ") +
DialogHtml::bold(QString("radius (") +
cx::globals::angstromSymbol + ")");
if (generateClusterForSelection) {
label = DialogHtml::paragraph(msgStart + " of the selected atoms");
} else {
Expand Down Expand Up @@ -592,7 +592,8 @@ void Crystalx::selectAtomsOutsideRadius() {
bool ok;
QString title = "Select Atoms";
QString label = QString("Select atoms outside a <b>radius (") +
ANGSTROM_SYMBOL + ")</b> of the currently selected atoms:";
cx::globals::angstromSymbol +
")</b> of the currently selected atoms:";
double radius = QInputDialog::getDouble(this, title, label, 5.0, 0.0, 25.0,
2, &ok, Qt::Tool);
if (ok) {
Expand Down Expand Up @@ -624,9 +625,10 @@ void Crystalx::toggleAnimation(bool animate) {

void Crystalx::clearCurrent() { crystalController->clearCurrentCrystal(); }

void Crystalx::clearAll() {
crystalController->clearAllCrystals();
if(childPropertyController) childPropertyController->reset();
void Crystalx::clearAll() {
crystalController->clearAllCrystals();
if (childPropertyController)
childPropertyController->reset();
}

void Crystalx::generateSlab() {
Expand All @@ -644,19 +646,20 @@ void Crystalx::generateSlab() {
}

void Crystalx::helpAboutActionDialog() {
using namespace cx::globals;
QString message = "";
message += DialogHtml::paragraph(CE_AUTHORS);
message += DialogHtml::paragraph(authors);
message += DialogHtml::paragraph(
"Website: " + DialogHtml::website(CE_URL, CE_URL) +
DialogHtml::linebreak() + "Email: " +
DialogHtml::email(CE_EMAIL, CE_EMAIL, "CrystalExplorer",
"Website: " + DialogHtml::website(url, url) + DialogHtml::linebreak() +
"Email: " +
DialogHtml::email(email, email, "CrystalExplorer",
QString("Re: Build= %1").arg(CX_BUILD_DATE)) +
DialogHtml::linebreak() + "Citation: " +
DialogHtml::website(CE_CITATION_URL, "How to Cite CrystalExplorer"));
DialogHtml::website(citationUrl, "How to Cite CrystalExplorer"));
message += DialogHtml::paragraph(
QString(COPYRIGHT_NOTICE).arg(QDate::currentDate().year()));
message += DialogHtml::paragraph(QString(CE_NAME) + " uses " +
DialogHtml::website(OCC_URL, "OCC") +
QString(copyrightNoticeTemplate).arg(QDate::currentDate().year()));
message += DialogHtml::paragraph(QString(name) + " uses " +
DialogHtml::website(occUrl, "OCC") +
"<br/>by P.R. Spackman");
message += DialogHtml::line();
message += DialogHtml::paragraph(QString("Version: %1").arg(CX_VERSION) +
Expand Down Expand Up @@ -1252,19 +1255,18 @@ void Crystalx::colorHighlightHtml(QString &lineOfText, QString regExp,
}

void Crystalx::updateWindowTitle() {
QString title;
QString title{cx::globals::mainWindowTitle};
if (project->previouslySaved()) {
QFileInfo fi(project->saveFilename());
title = QString(GLOBAL_MAINWINDOW_TITLE) + " - " + fi.fileName();
title += " - " + fi.fileName();
if (project->hasUnsavedChanges()) {
title += "*";
}
} else {
if (project->currentScene()) {
title = QString(GLOBAL_MAINWINDOW_TITLE) + " - " +
project->currentScene()->title();
title += " - " + project->currentScene()->title();
} else {
title = QString(GLOBAL_MAINWINDOW_TITLE) + " - Untitled";
title += " - Untitled";
}
}

Expand Down Expand Up @@ -1740,7 +1742,7 @@ void Crystalx::showEnergyCalculationDialog() {
"calculate interaction energies for a %1%2 "
"cluster around the selected fragment?")
.arg(CLUSTER_RADIUS, 0, 'f', 1)
.arg(ANGSTROM_SYMBOL);
.arg(cx::globals::angstromSymbol);
QMessageBox msgBox(this);
msgBox.setWindowTitle("Interaction Energy Calculation");
msgBox.setText(question);
Expand Down
12 changes: 6 additions & 6 deletions src/dialogs/closecontactcriteriawidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ void CloseContactCriteriaWidget::updateContactDistanceCriteria(
float donorVDWRadius;
float acceptorVDWRadius;
if (donor == ANY_ITEM) {
if (donor == cx::globals::anyItemLabel) {
donorVDWRadius = largestVdwRadiusForAllElements();
} else {
donorVDWRadius = ElementData::elementFromSymbol(donor)->vdwRadius();
}
if (acceptor == ANY_ITEM) {
if (acceptor == cx::globals::anyItemLabel) {
acceptorVDWRadius = largestVdwRadiusForAllElements();
} else {
acceptorVDWRadius = ElementData::elementFromSymbol(acceptor)->vdwRadius();
Expand All @@ -45,7 +45,7 @@ double CloseContactCriteriaWidget::largestVdwRadiusForAllElements() const {

for (int i = 0; i < m_elements.size(); ++i) {
QString elementSymbol = m_elements[i];
if (elementSymbol != ANY_ITEM) {
if (elementSymbol != cx::globals::anyItemLabel) {
vdwRadius = std::max(
vdwRadius,
static_cast<double>(
Expand Down Expand Up @@ -96,7 +96,7 @@ void CloseContactCriteriaWidget::updateElements(
m_elements = elements;
m_vdwMax = largestVdwRadiusForAllElements();
// CLEAR COMBOBOXES,
// ADD ELEMENT ITEMS (POSSIBLE PREPEND ANY_ITEM)
// ADD ELEMENT ITEMS (POSSIBLE PREPEND cx::globals::anyItemLabel)
}

CloseContactCriteria CloseContactCriteriaWidget::getCriteria(int row) {
Expand All @@ -112,7 +112,7 @@ CloseContactCriteria CloseContactCriteriaWidget::getCriteria(int row) {
if (donorItem && donorItem->widget()) {
QComboBox *donorBox = qobject_cast<QComboBox *>(donorItem->widget());
QString donor = donorBox ? donorBox->currentText() : QString();
if (donor != ANY_ITEM) {
if (donor != cx::globals::anyItemLabel) {
auto *element = ElementData::elementFromSymbol(donor);
if (element) {
criteria.donors.insert(element->number());
Expand All @@ -123,7 +123,7 @@ CloseContactCriteria CloseContactCriteriaWidget::getCriteria(int row) {
if (acceptorItem && acceptorItem->widget()) {
QComboBox *acceptorBox = qobject_cast<QComboBox *>(acceptorItem->widget());
QString acceptor = acceptorBox ? acceptorBox->currentText() : QString();
if (acceptor != ANY_ITEM) {
if (acceptor != cx::globals::anyItemLabel) {
auto *element = ElementData::elementFromSymbol(acceptor);
if (element) {
criteria.acceptors.insert(element->number());
Expand Down
16 changes: 9 additions & 7 deletions src/dialogs/closecontactsdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,17 @@ void CloseContactDialog::initConnections() {
connect(hbondColorButton, &QAbstractButton::clicked, this,
&CloseContactDialog::reportHBondColorChange);

connect(addContactPushButton, &QPushButton::clicked, closeContactsWidget, &CloseContactCriteriaWidget::addRow);
connect(closeContactsWidget, &CloseContactCriteriaWidget::closeContactsSettingsChanged,
this, &CloseContactDialog::closeContactsSettingsChanged);
connect(addContactPushButton, &QPushButton::clicked, closeContactsWidget,
&CloseContactCriteriaWidget::addRow);
connect(closeContactsWidget,
&CloseContactCriteriaWidget::closeContactsSettingsChanged, this,
&CloseContactDialog::closeContactsSettingsChanged);
}

void CloseContactDialog::updateDonorsAndAcceptors(QStringList elements,
QStringList hydrogenDonors) {
elements.prepend(ANY_ITEM);
hydrogenDonors.prepend(ANY_ITEM);
elements.prepend(cx::globals::anyItemLabel);
hydrogenDonors.prepend(cx::globals::anyItemLabel);

QStringList hydrogenAcceptors = elements;
hydrogenAcceptors.removeAll("H");
Expand Down Expand Up @@ -143,14 +145,14 @@ HBondCriteria CloseContactDialog::currentHBondCriteria() {

QString donor = hbondDonorComboBox->currentText();
QString acceptor = hbondAcceptorComboBox->currentText();
if (donor != ANY_ITEM) {
if (donor != cx::globals::anyItemLabel) {
auto *element = ElementData::elementFromSymbol(donor);
if (element) {
criteria.donors.insert(element->number());
}
}

if (acceptor != ANY_ITEM) {
if (acceptor != cx::globals::anyItemLabel) {
auto *element = ElementData::elementFromSymbol(acceptor);
if (element) {
criteria.acceptors.insert(element->number());
Expand Down
26 changes: 15 additions & 11 deletions src/dialogs/crystalinfodocument.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
#include <QTextCursor>
#include <fmt/format.h>

using cx::globals::angstromSymbol;
using cx::globals::degreeSymbol;

inline const char *INFO_HORIZONTAL_RULE =
"--------------------------------------------------------------------------"
"------------\n";
Expand Down Expand Up @@ -65,25 +68,26 @@ void CrystalInfoDocument::insertGeneralInformation(QTextCursor &cursor,
occ::Vec3 lengths = crystal->cellLengths();
occ::Vec3 angles = crystal->cellAngles() * 180.0 / M_PI;

cursor.insertText(QString::fromStdString(
fmt::format("{:<12s} {:>12s}\n", "Crystal", crystal->name().toStdString())));
cursor.insertText(QString::fromStdString(
fmt::format("{:<12s} {:>12s}\n", "CIF", fileInfo.fileName().toStdString())));
cursor.insertText(QString::fromStdString(fmt::format(
"{:<12s} {:>12s}\n", "Formula", crystal->chemicalFormula().toStdString())));
"{:<12s} {:>12s}\n", "Crystal", crystal->name().toStdString())));
cursor.insertText(QString::fromStdString(fmt::format(
"{:<12s} {:>12s}\n", "CIF", fileInfo.fileName().toStdString())));
cursor.insertText(QString::fromStdString(
fmt::format("{:<12s} {:>12s}\n", "Formula",
crystal->chemicalFormula().toStdString())));
cursor.insertText(QString::fromStdString(fmt::format(
"{:<12s} {:>12s}\n", "Space Group", crystal->spaceGroup().symbol())));
cursor.insertText(QString::fromStdString(fmt::format(
"{:<12s} {: 12.6f} {}\n", "Length A", lengths(0), ANGSTROM_SYMBOL)));
"{:<12s} {: 12.6f} {}\n", "Length A", lengths(0), angstromSymbol)));
cursor.insertText(QString::fromStdString(fmt::format(
"{:<12s} {: 12.6f} {}\n", "Length B", lengths(1), ANGSTROM_SYMBOL)));
"{:<12s} {: 12.6f} {}\n", "Length B", lengths(1), angstromSymbol)));
cursor.insertText(QString::fromStdString(fmt::format(
"{:<12s} {: 12.6f} {}\n", "Length C", lengths(2), ANGSTROM_SYMBOL)));
"{:<12s} {: 12.6f} {}\n", "Length C", lengths(2), angstromSymbol)));

cursor.insertText(QString::fromStdString(fmt::format(
"{:<12s} {: 12.6f} {}\n", "Angle Alpha", angles(0), DEGREE_SYMBOL)));
"{:<12s} {: 12.6f} {}\n", "Angle Alpha", angles(0), degreeSymbol)));
cursor.insertText(QString::fromStdString(fmt::format(
"{:<12s} {: 12.6f} {}\n", "Angle Beta", angles(1), DEGREE_SYMBOL)));
"{:<12s} {: 12.6f} {}\n", "Angle Beta", angles(1), degreeSymbol)));
cursor.insertText(QString::fromStdString(fmt::format(
"{:<12s} {: 12.6f} {}\n", "Angle Gamma", angles(2), DEGREE_SYMBOL)));
"{:<12s} {: 12.6f} {}\n", "Angle Gamma", angles(2), degreeSymbol)));
}
2 changes: 1 addition & 1 deletion src/dialogs/depthfadingandclippingdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ void DepthFadingAndClippingDialog::initConnections() {
void DepthFadingAndClippingDialog::initClippingSlider() {
frontClippingSlider->setSingleStep(CLIPPING_STEP);
frontClippingSlider->setTickInterval(CLIPPING_INTERVAL);
frontClippingSlider->setMinimum(CLIPPING_SCALE * GLOBAL_FRONT_CLIPPING_PLANE);
frontClippingSlider->setMinimum(CLIPPING_SCALE * cx::globals::frontClippingPlane);
frontClippingSlider->setMaximum(CLIPPING_SCALE * CLIPPING_MAXIMUM);
}

Expand Down
4 changes: 2 additions & 2 deletions src/dialogs/surfacegenerationdialog.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "surfacegenerationdialog.h"
#include "globals.h" // only NEW_WAVEFUNCTION_ITEM
#include "globals.h"
#include "settings.h"
#include "globalconfiguration.h"

Expand Down Expand Up @@ -256,7 +256,7 @@ void SurfaceGenerationDialog::updateWavefunctionComboBox(bool selectLast) {
if (needWavefunction()) {
ui->wavefunctionCombobox->clear();

ui->wavefunctionCombobox->addItem(NEW_WAVEFUNCTION_ITEM);
ui->wavefunctionCombobox->addItem(tr("New wavefunction"));

for(const auto &[wavefunction, transform] : m_availableWavefunctions) {
if(wavefunction) {
Expand Down
Loading

0 comments on commit 746976f

Please sign in to comment.