Skip to content

Commit

Permalink
Rework most of infodocuments
Browse files Browse the repository at this point in the history
  • Loading branch information
peterspackman committed Aug 27, 2024
1 parent 68fc7f8 commit 674795d
Show file tree
Hide file tree
Showing 20 changed files with 678 additions and 330 deletions.
8 changes: 7 additions & 1 deletion src/core/chemicalstructure.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,8 @@ void ChemicalStructure::guessBondsBasedOnDistances() {
}

for (const auto &[edge_desc, edge] : m_bondGraph.edges()) {
std::pair<int, int> pair{static_cast<int>(edge.source), static_cast<int>(edge.target)};
std::pair<int, int> pair{static_cast<int>(edge.source),
static_cast<int>(edge.target)};
switch (edge.connectionType) {
case Connection::CovalentBond:
m_covalentBonds.push_back(pair);
Expand Down Expand Up @@ -1121,3 +1122,8 @@ std::vector<GenericAtomIndex> ChemicalStructure::atomIndices() const {
}
return result;
}

occ::Mat3N ChemicalStructure::convertCoordinates(
const occ::Mat3N &pos, ChemicalStructure::CoordinateConversion) const {
return pos;
}
7 changes: 7 additions & 0 deletions src/core/chemicalstructure.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ class ChemicalStructure : public QObject {
Crystal // 3D periodic
};

enum class CoordinateConversion {
CartToFrac,
FracToCart
};

enum class AtomColoring { Element, Fragment, Index };

explicit ChemicalStructure(QObject *parent = nullptr);
Expand Down Expand Up @@ -94,6 +99,8 @@ class ChemicalStructure : public QObject {
inline void setName(const QString &name) { m_name = name; }
inline const auto &name() const { return m_name; }

virtual occ::Mat3N convertCoordinates(const occ::Mat3N &pos, CoordinateConversion) const;

// colors
QColor atomColor(GenericAtomIndex atomIndex) const;
void overrideAtomColor(GenericAtomIndex, const QColor &);
Expand Down
22 changes: 16 additions & 6 deletions src/crystal/crystalstructure.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1048,7 +1048,8 @@ CrystalStructure::findUnitCellFragment(const Fragment &frag) const {
return FragmentIndex{-1, 0, 0, 0};
}

Fragment CrystalStructure::makeFragmentFromFragmentIndex(FragmentIndex idx) const {
Fragment
CrystalStructure::makeFragmentFromFragmentIndex(FragmentIndex idx) const {
FragmentIndex unitCellIndex{idx.u, 0, 0, 0};
// TODO add error checking
Fragment result = m_unitCellFragments.at(unitCellIndex);
Expand Down Expand Up @@ -1334,10 +1335,10 @@ void CrystalStructure::buildDimerMappingTable(double maxRadius) {
qDebug() << "Unit cell molecules" << m_unitCellFragments.size();
qDebug() << "Unique dimers:" << m_unitCellDimers.unique_dimers.size();

m_dimerMappingTable = occ::crystal::DimerMappingTable(
m_crystal, m_unitCellDimers, true);
m_dimerMappingTableNoInv = occ::crystal::DimerMappingTable(
m_crystal, m_unitCellDimers, false);
m_dimerMappingTable =
occ::crystal::DimerMappingTable(m_crystal, m_unitCellDimers, true);
m_dimerMappingTableNoInv =
occ::crystal::DimerMappingTable(m_crystal, m_unitCellDimers, false);
qDebug() << "Built dimer mapping table";
}

Expand Down Expand Up @@ -1427,7 +1428,8 @@ CrystalStructure::findFragmentPairs(FragmentPairSettings settings) const {
for (auto &[idx, vec] : result.pairs) {
std::stable_sort(vec.begin(), vec.end(), molPairSortFunc);
for (auto &[d, asym] : vec) {
auto u = FragmentIndexPair::fromDimerIndex(symmetryUniqueMap.at(d.index.toDimerIndex()));
auto u = FragmentIndexPair::fromDimerIndex(
symmetryUniqueMap.at(d.index.toDimerIndex()));
const auto it =
std::find_if(result.uniquePairs.begin(), result.uniquePairs.end(),
[&u](const FragmentDimer &x) { return x.index == u; });
Expand All @@ -1436,3 +1438,11 @@ CrystalStructure::findFragmentPairs(FragmentPairSettings settings) const {
}
return result;
}

occ::Mat3N CrystalStructure::convertCoordinates(
const occ::Mat3N &pos, ChemicalStructure::CoordinateConversion conv) const {
if (conv == ChemicalStructure::CoordinateConversion::FracToCart) {
return m_crystal.to_cartesian(pos);
}
return m_crystal.to_fractional(pos);
}
1 change: 1 addition & 0 deletions src/crystal/crystalstructure.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class CrystalStructure : public ChemicalStructure {
}

inline const auto &spaceGroup() const { return m_crystal.space_group(); }
occ::Mat3N convertCoordinates(const occ::Mat3N &pos, ChemicalStructure::CoordinateConversion) const override;

FragmentIndex fragmentIndexForAtom(int) const override;
FragmentIndex fragmentIndexForAtom(GenericAtomIndex) const override;
Expand Down
42 changes: 13 additions & 29 deletions src/crystalx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <QUrl>
#include <QtDebug>

#include "atominfodocument.h"
#include "confirmationbox.h"
#include "crystalx.h"
#include "dialoghtml.h"
Expand Down Expand Up @@ -1459,7 +1460,8 @@ void Crystalx::exportAs() {

if (filename.toLower().endsWith(".png")) {
QImage img = glWindow->exportToImage(scaleFactor, backgroundColor);
qDebug() << "Exporting image with scale factor" << scaleFactor << "resolution" << img.size();
qDebug() << "Exporting image with scale factor" << scaleFactor
<< "resolution" << img.size();
success = img.save(filename);
} else {
QFile outputFile(filename);
Expand Down Expand Up @@ -1814,7 +1816,7 @@ void Crystalx::calculatePairEnergiesWithExistingWavefunctions(
connect(calc, &PairEnergyCalculator::calculationComplete, this,
[this, calc]() {
qDebug() << "Calculation of pair energies complete";
showInfo(InfoType::InteractionEnergyInfo);
showInfo(InfoType::InteractionEnergy);
calc->deleteLater();
});

Expand Down Expand Up @@ -1913,45 +1915,27 @@ void Crystalx::showInfo(InfoType infoType) {
void Crystalx::updateInfo(InfoType infoType) {
Scene *scene = project->currentScene();
Q_ASSERT(scene);
QTextDocument *document = infoViewer->document(infoType);
document->clear();

QString title;
switch (infoType) {
case GeneralCrystalInfo:
InfoDocuments::insertGeneralCrystalInfoIntoTextDocument(document, scene);
break;
case AtomCoordinateInfo:
InfoDocuments::insertAtomicCoordinatesIntoTextDocument(document, scene);
default: {
infoViewer->setScene(scene);
break;
case InteractionEnergyInfo:
}
case InfoType::InteractionEnergy: {
QTextDocument *document = infoViewer->document(infoType);
document->clear();
InfoDocuments::insertInteractionEnergiesIntoTextDocument(document, scene);
break;
case CurrentSurfaceInfo:
// TODO surface info documents
/*
if (fingerprintWindow && scene->currentSurface() &&
scene->currentSurface()->isFingerprintable()) {
FingerprintBreakdown breakdown = fingerprintWindow->fingerprintBreakdown(
scene->crystal()->listOfElementSymbols());
InfoDocuments::insertCurrentSurfaceInfoIntoTextDocument(document, scene,
breakdown);
} else {
InfoDocuments::insertCurrentSurfaceInfoIntoTextDocument(
document, scene, FingerprintBreakdown());
}
*/
infoViewer->setDocument(document, infoType);
break;
}

infoViewer->setDocument(document, infoType);
}
}

void Crystalx::setInfoTabSpecificViewOptions(InfoType infoType) {
Scene *scene = project->currentScene();
Q_ASSERT(scene);

if (infoType == InteractionEnergyInfo) {
if (infoType == InfoType::InteractionEnergy) {
scene->togglePairHighlighting(true);
} else {
scene->togglePairHighlighting(false);
Expand Down
4 changes: 4 additions & 0 deletions src/dialogs/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,13 @@ qt_wrap_ui(DIALOG_UI_HEADERS
qt_add_library(cx_dialogs STATIC
${DIALOG_UI_HEADERS}
"${CMAKE_CURRENT_SOURCE_DIR}/animationsettingsdialog.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/atominfodocument.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/celllimitsdialog.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/closecontactcriteriawidget.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/closecontactsdialog.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/collapsibledocument.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/colordelegate.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/crystalinfodocument.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/depthfadingandclippingdialog.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/elementeditor.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/energycalculationdialog.cpp"
Expand All @@ -41,6 +44,7 @@ qt_add_library(cx_dialogs STATIC
"${CMAKE_CURRENT_SOURCE_DIR}/planegenerationdialog.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/surfacedropdown.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/surfacegenerationdialog.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/surfaceinfodocument.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/wavefunctioncalculationdialog.cpp"
)

Expand Down
148 changes: 148 additions & 0 deletions src/dialogs/atominfodocument.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
#include "atominfodocument.h"
#include <QFont>
#include <QTextCursor>
#include <fmt/format.h>

inline const char *INFO_HORIZONTAL_RULE =
"--------------------------------------------------------------------------"
"------------\n";

AtomInfoDocument::AtomInfoDocument(QWidget *parent) : QWidget(parent) {
setupUI();
populateDocument();
}

void AtomInfoDocument::setupUI() {
QVBoxLayout *layout = new QVBoxLayout(this);
m_tabWidget = new QTabWidget(this);
layout->addWidget(m_tabWidget);

QFont monoFont("Courier");
monoFont.setStyleHint(QFont::Monospace);
monoFont.setFixedPitch(true);

m_cartesianCoordinates = new QTextEdit(this);
m_cartesianCoordinates->document()->setDefaultFont(monoFont);
m_fractionalCoordinates = new QTextEdit(this);
m_fractionalCoordinates->document()->setDefaultFont(monoFont);

m_tabWidget->addTab(m_cartesianCoordinates, "Cartesian");
m_tabWidget->addTab(m_fractionalCoordinates, "Fractional");
}


void AtomInfoDocument::populateDocument() {
if (!m_scene)
return;
m_cartesianCoordinates->clear();
m_fractionalCoordinates->clear();
insertAtomicCoordinates(m_cartesianCoordinates, AtomDescription::CartesianInfo);
insertAtomicCoordinates(m_fractionalCoordinates, AtomDescription::FractionalInfo);
resetCursorsToBeginning();
}

void AtomInfoDocument::resetCursorsToBeginning()
{
QTextCursor cartesianCursor = m_cartesianCoordinates->textCursor();
cartesianCursor.movePosition(QTextCursor::Start);
m_cartesianCoordinates->setTextCursor(cartesianCursor);
m_cartesianCoordinates->ensureCursorVisible();

QTextCursor fractionalCursor = m_fractionalCoordinates->textCursor();
fractionalCursor.movePosition(QTextCursor::Start);
m_fractionalCoordinates->setTextCursor(fractionalCursor);
m_fractionalCoordinates->ensureCursorVisible();
}

void AtomInfoDocument::updateScene(Scene *scene) {
m_scene = scene;
populateDocument();
}

void AtomInfoDocument::insertAtomicCoordinates(
QTextEdit *textEdit, AtomDescription atomDescription) {

ChemicalStructure *structure = m_scene->chemicalStructure();
if (!structure)
return;

const auto selectedAtoms = structure->atomsWithFlags(AtomFlag::Selected);
const auto unselectedAtoms =
structure->atomsWithFlags(AtomFlag::Selected, false);

if (!selectedAtoms.empty()) {
insertAtomicCoordinatesSection(textEdit, "Selected Atoms", structure,
selectedAtoms, atomDescription);
}

if (!unselectedAtoms.empty()) {
insertAtomicCoordinatesSection(textEdit, "Unselected Atoms", structure,
unselectedAtoms, atomDescription);
}
}

void AtomInfoDocument::insertAtomicCoordinatesSection(
QTextEdit *textEdit,
const QString &title, ChemicalStructure *structure,
const std::vector<GenericAtomIndex> &atoms,
AtomDescription atomDescription) {
if (atoms.empty()) {
return;
}

bool frac = (atomDescription == AtomDescription::FractionalInfo);
if (frac &&
(structure->structureType() == ChemicalStructure::StructureType::Cluster))
return;

QTextCursor cursor(textEdit->document());
cursor.movePosition(QTextCursor::End);

cursor.beginEditBlock();
insertAtomicCoordinatesHeader(cursor, title, atoms.size(), atomDescription);
insertAtomicCoordinates(cursor, structure, atoms, atomDescription);
cursor.endEditBlock();
}

void AtomInfoDocument::insertAtomicCoordinatesHeader(
QTextCursor &cursor, const QString &title, int numAtoms,
AtomDescription atomDescription) {
bool frac = atomDescription == AtomDescription::FractionalInfo;
QString coords = frac ? "fractional" : "Cartesian";

cursor.insertText(title + "\n");
cursor.insertText(QString("%1 atom%2, %3 coordinates\n")
.arg(numAtoms)
.arg(numAtoms > 1 ? "s" : "")
.arg(coords));

cursor.insertText(QString::fromStdString(
fmt::format("{:<6s} {:<6s} {:>20s} {:>20s} {:>20s} {:>8s}\n", "Label",
"Symbol", "x", "y", "z", "Occ")));
cursor.insertText(INFO_HORIZONTAL_RULE);
}

void AtomInfoDocument::insertAtomicCoordinates(
QTextCursor &cursor, ChemicalStructure *structure,
const std::vector<GenericAtomIndex> &atoms,
AtomDescription atomDescription) {
if (!structure)
return;

auto nums = structure->atomicNumbersForIndices(atoms);
auto pos = structure->atomicPositionsForIndices(atoms);
if (atomDescription == AtomDescription::FractionalInfo) {
pos = structure->convertCoordinates(
pos, ChemicalStructure::CoordinateConversion::CartToFrac);
}
auto labels = structure->labelsForIndices(atoms);

for (int i = 0; i < nums.rows(); i++) {
std::string symbol = occ::core::Element(nums(i)).symbol();

cursor.insertText(QString::fromStdString(
fmt::format("{:<6s} {:<6s} {: 20.12f} {: 20.12f} {: 20.12f} {: 8.3f}\n",
labels[i].toStdString(), symbol, pos(0, i), pos(1, i),
pos(2, i), 1.0)));
}
}
49 changes: 49 additions & 0 deletions src/dialogs/atominfodocument.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#pragma once
#include "scene.h"
#include <QVBoxLayout>
#include <QTextEdit>
#include <QTabWidget>
#include <QWidget>

enum class AtomDescription {
SiteLabel,
UnitCellShift,
Hybrid,
Coordinates,
CartesianInfo,
FractionalInfo
};

class AtomInfoDocument : public QWidget {
Q_OBJECT

public:
explicit AtomInfoDocument(QWidget *parent = nullptr);

void updateScene(Scene *scene);
QTextDocument *document(int index) const;

private:
Scene *m_scene{nullptr};
QTabWidget *m_tabWidget{nullptr};
QTextEdit *m_cartesianCoordinates{nullptr};
QTextEdit *m_fractionalCoordinates{nullptr};


void setupUI();
void populateDocument();
void resetCursorsToBeginning();

void insertAtomicCoordinates(QTextEdit *, AtomDescription atomDescription);
void
insertAtomicCoordinatesSection(QTextEdit *, const QString &title,
ChemicalStructure *structure,
const std::vector<GenericAtomIndex> &atoms,
AtomDescription atomDescription);
void insertAtomicCoordinatesHeader(QTextCursor &, const QString &title,
int numAtoms,
AtomDescription atomDescription);
void insertAtomicCoordinates(QTextCursor &, ChemicalStructure *structure,
const std::vector<GenericAtomIndex> &atoms,
AtomDescription atomDescription);
};
Loading

0 comments on commit 674795d

Please sign in to comment.