| 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 <QAction> |
| 7 |
#include <QByteArray> |
| 8 |
#include <QCoreApplication> |
| 9 |
#include <QDebug> |
| 10 |
#include <QDialog> |
| 11 |
#include <QFile> |
| 12 |
#include <QMessageBox> |
| 13 |
#include <QProcess> |
| 14 |
#include <QRegularExpression> |
| 15 |
#include <QString> |
| 16 |
|
| 17 |
#include <avogadro/io/fileformatmanager.h> |
| 18 |
#include <avogadro/qtgui/molecule.h> |
| 19 |
#include <avogadro/qtgui/chartdialog.h> |
| 20 |
#include <avogadro/qtgui/chartwidget.h> |
| 21 |
#include <avogadro/qtgui/utilities.h> |
| 22 |
|
| 23 |
#include "plotxrd.h" |
| 24 |
#include "xrdoptionsdialog.h" |
| 25 |
|
| 26 |
using Avogadro::QtGui::Molecule; |
| 27 |
|
| 28 |
namespace Avogadro::QtPlugins { |
| 29 |
|
| 30 |
PlotXrd::PlotXrd(QObject* parent_) |
| 31 |
: Avogadro::QtGui::ExtensionPlugin(parent_), m_actions(QList<QAction*>()), |
| 32 |
m_molecule(nullptr), |
| 33 |
m_xrdOptionsDialog(new XrdOptionsDialog(qobject_cast<QWidget*>(parent()))), |
| 34 |
m_displayDialogAction(new QAction(this)) |
| 35 |
{ |
| 36 |
m_displayDialogAction->setText(tr("Plot Theoretical XRD Pattern…")); |
| 37 |
connect(m_displayDialogAction.get(), &QAction::triggered, this, |
| 38 |
&PlotXrd::displayDialog); |
| 39 |
m_actions.push_back(m_displayDialogAction.get()); |
| 40 |
m_displayDialogAction->setProperty("menu priority", 90); |
| 41 |
|
| 42 |
updateActions(); |
| 43 |
} |
| 44 |
|
| 45 |
PlotXrd::~PlotXrd() = default; |
| 46 |
|
| 47 |
QList<QAction*> PlotXrd::actions() const |
| 48 |
{ |
| 49 |
return m_actions; |
| 50 |
} |
| 51 |
|
| 52 |
QStringList PlotXrd::menuPath(QAction*) const |
| 53 |
{ |
| 54 |
return QStringList() << tr("&Crystal"); |
| 55 |
} |
| 56 |
|
| 57 |
void PlotXrd::setMolecule(QtGui::Molecule* mol) |
| 58 |
{ |
| 59 |
if (m_molecule == mol) |
| 60 |
return; |
| 61 |
|
| 62 |
if (m_molecule) |
| 63 |
m_molecule->disconnect(this); |
| 64 |
|
| 65 |
m_molecule = mol; |
| 66 |
|
| 67 |
if (m_molecule) |
| 68 |
connect(m_molecule, SIGNAL(changed(uint)), SLOT(moleculeChanged(uint))); |
| 69 |
|
| 70 |
updateActions(); |
| 71 |
} |
| 72 |
|
| 73 |
void PlotXrd::moleculeChanged(unsigned int c) |
| 74 |
{ |
| 75 |
Q_ASSERT(m_molecule == qobject_cast<Molecule*>(sender())); |
| 76 |
|
| 77 |
auto changes = static_cast<Molecule::MoleculeChanges>(c); |
| 78 |
|
| 79 |
if (changes & Molecule::UnitCell) { |
| 80 |
if (changes & Molecule::Added || changes & Molecule::Removed) |
| 81 |
updateActions(); |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
void PlotXrd::updateActions() |
| 86 |
{ |
| 87 |
// Disable everything for nullptr molecules. |
| 88 |
if (!m_molecule) { |
| 89 |
foreach (QAction* action, m_actions) |
| 90 |
action->setEnabled(false); |
| 91 |
return; |
| 92 |
} |
| 93 |
|
| 94 |
// Only display the actions if there is a unit cell |
| 95 |
if (m_molecule->unitCell()) { |
| 96 |
foreach (QAction* action, m_actions) |
| 97 |
action->setEnabled(true); |
| 98 |
} else { |
| 99 |
foreach (QAction* action, m_actions) |
| 100 |
action->setEnabled(false); |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
void PlotXrd::displayDialog() |
| 105 |
{ |
| 106 |
// check if there's already an XRD in the molecule spectra |
| 107 |
bool hasXRD = false; |
| 108 |
std::vector<float> xData; |
| 109 |
std::vector<float> yData; |
| 110 |
|
| 111 |
for (const auto& type : m_molecule->spectraTypes()) { |
| 112 |
if (type == "XRD" || type == "xrd") { |
| 113 |
hasXRD = true; |
| 114 |
break; |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
if (hasXRD) { |
| 119 |
// make sure it works |
| 120 |
auto spectra = m_molecule->spectra("XRD"); |
| 121 |
// should be a MatrixXd with 2 columns |
| 122 |
if (spectra.cols() != 2 || spectra.rows() < 2) { |
| 123 |
hasXRD = false; // re-generate it |
| 124 |
} else { |
| 125 |
for (int i = 0; i < spectra.rows(); i++) { |
| 126 |
xData.push_back(spectra(i, 0)); |
| 127 |
yData.push_back(spectra(i, 1)); |
| 128 |
} |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
if (!hasXRD) { |
| 133 |
// we have to generate the XRD pattern |
| 134 |
// Do nothing if the user cancels |
| 135 |
if (m_xrdOptionsDialog->exec() != QDialog::Accepted) |
| 136 |
return; |
| 137 |
|
| 138 |
// Otherwise, fetch the options and perform the run |
| 139 |
double wavelength = m_xrdOptionsDialog->wavelength(); |
| 140 |
double peakwidth = m_xrdOptionsDialog->peakWidth(); |
| 141 |
size_t numpoints = m_xrdOptionsDialog->numDataPoints(); |
| 142 |
double max2theta = m_xrdOptionsDialog->max2Theta(); |
| 143 |
|
| 144 |
QString err; |
| 145 |
XrdData results; |
| 146 |
if (!generateXrdPattern(*m_molecule, results, err, wavelength, peakwidth, |
| 147 |
numpoints, max2theta)) { |
| 148 |
QMessageBox::critical(qobject_cast<QWidget*>(parent()), |
| 149 |
tr("Failed to generate XRD pattern"), |
| 150 |
tr("Error message: ") + err); |
| 151 |
return; |
| 152 |
} |
| 153 |
// Now generate a plot with the data |
| 154 |
for (const auto& item : results) { |
| 155 |
xData.push_back(item.first); |
| 156 |
yData.push_back(item.second); |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
QString xTitle = "2θ (°)"; |
| 161 |
QString yTitle = "Intensity"; |
| 162 |
const char* windowName = "Theoretical XRD Pattern"; |
| 163 |
|
| 164 |
if (!m_chartDialog) |
| 165 |
m_chartDialog.reset( |
| 166 |
new QtGui::ChartDialog(qobject_cast<QWidget*>(this->parent()))); |
| 167 |
|
| 168 |
m_chartDialog->setWindowTitle(windowName); |
| 169 |
auto* chart = m_chartDialog->chartWidget(); |
| 170 |
chart->clearPlots(); |
| 171 |
chart->addPlot(xData, yData, QtGui::color4ub{ 255, 0, 0, 255 }); |
| 172 |
chart->setXAxisTitle(xTitle); |
| 173 |
chart->setYAxisTitle(yTitle); |
| 174 |
m_chartDialog->show(); |
| 175 |
m_chartDialog->raise(); |
| 176 |
m_chartDialog->activateWindow(); |
| 177 |
} |
| 178 |
|
| 179 |
bool PlotXrd::generateXrdPattern(const QtGui::Molecule& mol, XrdData& results, |
| 180 |
QString& err, double wavelength, |
| 181 |
double peakwidth, size_t numpoints, |
| 182 |
double max2theta) |
| 183 |
{ |
| 184 |
// Get the molecule as a cif file |
| 185 |
std::string cifData; |
| 186 |
if (!Io::FileFormatManager::instance().writeString(mol, cifData, "cif")) { |
| 187 |
err = tr("Failed to convert molecule to CIF format."); |
| 188 |
qDebug() << "Error in" << __FUNCTION__ << ":" << err; |
| 189 |
return false; |
| 190 |
} |
| 191 |
|
| 192 |
// Now, execute genXrdPattern with the given inputs |
| 193 |
QStringList args; |
| 194 |
args << "--read-from-stdin" |
| 195 |
<< "--wavelength=" + QString::number(wavelength) |
| 196 |
<< "--peakwidth=" + QString::number(peakwidth) |
| 197 |
<< "--numpoints=" + QString::number(numpoints) |
| 198 |
<< "--max2theta=" + QString::number(max2theta); |
| 199 |
|
| 200 |
QByteArray output; |
| 201 |
if (!executeGenXrdPattern(args, cifData.c_str(), output, err)) { |
| 202 |
qDebug() << "Error in" << __FUNCTION__ << ":" << err; |
| 203 |
return false; |
| 204 |
} |
| 205 |
|
| 206 |
// Store the results |
| 207 |
results.clear(); |
| 208 |
|
| 209 |
// Find the section of data in the output |
| 210 |
bool dataStarted = false; |
| 211 |
QStringList lines = |
| 212 |
QString(output).split(QRegularExpression("[\r\n]"), Qt::SkipEmptyParts); |
| 213 |
for (const auto& line : lines) { |
| 214 |
if (!dataStarted && line.contains("# 2Theta/TOF ICalc")) { |
| 215 |
dataStarted = true; |
| 216 |
continue; |
| 217 |
} |
| 218 |
|
| 219 |
if (dataStarted) { |
| 220 |
QStringList rowData = line.split(" ", Qt::SkipEmptyParts); |
| 221 |
if (rowData.size() != 2) { |
| 222 |
err = tr("Data read from genXrdPattern appears to be corrupt!"); |
| 223 |
qDebug() << "Error in" << __FUNCTION__ << err; |
| 224 |
qDebug() << "Data is:"; |
| 225 |
for (const auto& lineTmp : lines) |
| 226 |
qDebug() << lineTmp; |
| 227 |
return false; |
| 228 |
} |
| 229 |
results.push_back( |
| 230 |
std::make_pair(rowData[0].toDouble(), rowData[1].toDouble())); |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
return true; |
| 235 |
} |
| 236 |
|
| 237 |
bool PlotXrd::executeGenXrdPattern(const QStringList& args, |
| 238 |
const QByteArray& input, QByteArray& output, |
| 239 |
QString& err) |
| 240 |
{ |
| 241 |
QString program; |
| 242 |
// If the GENXRDPATTERN_EXECUTABLE environment variable is set, then |
| 243 |
// use that |
| 244 |
QByteArray xrdExec = qgetenv("GENXRDPATTERN_EXECUTABLE"); |
| 245 |
if (!xrdExec.isEmpty()) { |
| 246 |
program = xrdExec; |
| 247 |
} else { |
| 248 |
// Otherwise, search in the current directory, and then ../bin |
| 249 |
#ifdef _WIN32 |
| 250 |
QString executable = "genXrdPattern.exe"; |
| 251 |
#else |
| 252 |
QString executable = "genXrdPattern"; |
| 253 |
#endif |
| 254 |
|
| 255 |
program = QtGui::Utilities::findExecutablePath(executable); |
| 256 |
if (program.isEmpty()) { |
| 257 |
err = tr("Error: could not find genXrdPattern executable!"); |
| 258 |
qDebug() << err; |
| 259 |
return false; |
| 260 |
} else { |
| 261 |
program += "/" + executable; |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
QProcess p; |
| 266 |
p.start(program, args); |
| 267 |
|
| 268 |
if (!p.waitForStarted()) { |
| 269 |
err = tr("Error: " + program.toLocal8Bit() + " failed to start"); |
| 270 |
qDebug() << err; |
| 271 |
return false; |
| 272 |
} |
| 273 |
|
| 274 |
// Give it the input! |
| 275 |
p.write(input.data()); |
| 276 |
|
| 277 |
// Close the write channel |
| 278 |
p.closeWriteChannel(); |
| 279 |
|
| 280 |
if (!p.waitForFinished()) { |
| 281 |
err = tr("Error: " + program.toLocal8Bit() + " failed to finish"); |
| 282 |
qDebug() << err; |
| 283 |
output = p.readAll(); |
| 284 |
qDebug() << "Output is as follows:\n" << output; |
| 285 |
return false; |
| 286 |
} |
| 287 |
|
| 288 |
int exitStatus = p.exitStatus(); |
| 289 |
output = p.readAll(); |
| 290 |
|
| 291 |
if (exitStatus == QProcess::CrashExit) { |
| 292 |
err = tr("Error: " + program.toLocal8Bit() + " crashed!"); |
| 293 |
qDebug() << err; |
| 294 |
qDebug() << "Output is as follows:\n" << output; |
| 295 |
return false; |
| 296 |
} |
| 297 |
|
| 298 |
if (exitStatus != QProcess::NormalExit) { |
| 299 |
err = tr("Error: " + program.toLocal8Bit() + |
| 300 |
" finished abnormally with exit code " + |
| 301 |
QString::number(exitStatus).toLocal8Bit()); |
| 302 |
qDebug() << err; |
| 303 |
qDebug() << "Output is as follows:\n" << output; |
| 304 |
return false; |
| 305 |
} |
| 306 |
|
| 307 |
// We did it! |
| 308 |
return true; |
| 309 |
} |
| 310 |
|
| 311 |
} // namespace Avogadro::QtPlugins |