File indexing completed on 2024-04-28 07:28:50

0001 /*
0002     SPDX-FileCopyrightText: 2006 Carsten Niehaus <cniehaus@kde.org>
0003     SPDX-FileCopyrightText: 2007-2008 Marcus D. Hanwell <marcus@cryos.org>
0004     SPDX-FileCopyrightText: 2016 Andreas Cord-Landwehr <cordlandwehr@kde.org>
0005     SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 #include "iowrapper.h"
0009 
0010 #include <fstream>
0011 #include <iostream>
0012 
0013 #include <avogadro/core/molecule.h>
0014 #include <avogadro/io/cmlformat.h>
0015 #include <avogadro/io/pdbformat.h>
0016 #include <avogadro/io/xyzformat.h>
0017 
0018 #include <QDebug>
0019 #include <QFile>
0020 #include <QFileInfo>
0021 #include <QMessageBox>
0022 #include <QRegularExpression>
0023 
0024 #include <KLocalizedString>
0025 
0026 std::unique_ptr<Avogadro::Core::Molecule> IoWrapper::readMolecule(const QString &filename)
0027 {
0028     std::ifstream inFileStream(QFile::encodeName(filename).constData());
0029     if (!inFileStream) {
0030         QMessageBox::warning(nullptr, i18n("Problem while opening the file"), i18n("Cannot open the specified file."));
0031         return nullptr;
0032     }
0033 
0034     auto mol = std::make_unique<Avogadro::Core::Molecule>();
0035     auto format = getFileReader(QFileInfo(filename).suffix());
0036 
0037     if (!format) {
0038         qCritical() << "Could not initialize file reader for file " << filename;
0039         return nullptr;
0040     }
0041 
0042     if (!format->read(inFileStream, *mol)) {
0043         qCritical() << "Could not read file " << filename << "; Error message: " << QString().fromStdString(format->error());
0044         return nullptr;
0045     }
0046 
0047     return mol;
0048 }
0049 
0050 bool IoWrapper::writeMolecule(const QString &filename, Avogadro::Core::Molecule *mol)
0051 {
0052     std::ofstream outFileStream(QFile::encodeName(filename).constData());
0053     if (!outFileStream) {
0054         QMessageBox::warning(nullptr, i18n("Sorry"), i18n("Cannot save to the specified file."));
0055         return false;
0056     }
0057     Avogadro::Io::CmlFormat cmlFormat;
0058     if (!cmlFormat.write(outFileStream, *mol)) {
0059         qCritical() << "Could not read file:" << filename;
0060         return false;
0061     }
0062     return true;
0063 }
0064 
0065 QString IoWrapper::getFormula(Avogadro::QtGui::Molecule *molecule)
0066 {
0067     return QString::fromStdString(molecule->formula());
0068 }
0069 
0070 QString IoWrapper::getPrettyFormula(Avogadro::QtGui::Molecule *molecule)
0071 {
0072     QString formula = QString::fromStdString(molecule->formula());
0073     formula.replace(QRegularExpression("(\\d+)"), "<sub>\\1</sub>");
0074     return formula;
0075 }
0076 
0077 std::unique_ptr<Avogadro::Io::FileFormat> IoWrapper::getFileReader(const QString &format)
0078 {
0079     if (format == QStringLiteral("cml")) {
0080         return std::make_unique<Avogadro::Io::CmlFormat>();
0081     } else if (format == QStringLiteral("pdb")) {
0082         return std::make_unique<Avogadro::Io::PdbFormat>();
0083     } else if (format == QStringLiteral("xyz")) {
0084         return std::make_unique<Avogadro::Io::XyzFormat>();
0085     } else {
0086         return nullptr;
0087     }
0088 }