diff --git a/compoundviewer/CMakeLists.txt b/compoundviewer/CMakeLists.txt --- a/compoundviewer/CMakeLists.txt +++ b/compoundviewer/CMakeLists.txt @@ -31,6 +31,8 @@ set_target_properties(compoundviewer PROPERTIES VERSION ${KALZIUMLIB_VERSION} SOVERSION ${KALZIUMLIB_SOVERSION} + CXX_STANDARD 14 + CXX_STANDARD_REQUIRED ON ) install(TARGETS compoundviewer ${INSTALL_TARGETS_DEFAULT_ARGS}) diff --git a/compoundviewer/iowrapper.h b/compoundviewer/iowrapper.h --- a/compoundviewer/iowrapper.h +++ b/compoundviewer/iowrapper.h @@ -16,10 +16,13 @@ #ifndef IOWRAPPER_H #define IOWRAPPER_H +#include #include #include +#include + /** * @author Carsten Niehaus */ @@ -30,13 +33,19 @@ * This class reads the molecule in the file @p filename. It returns 0 if * the file couldn't be read. */ - static Avogadro::Core::Molecule * readMolecule(const QString &filename); + static std::unique_ptr readMolecule(const QString &filename); static bool writeMolecule(const QString& filename, Avogadro::Core::Molecule *); static QString getFormula(Avogadro::QtGui::Molecule *molecule); static QString getPrettyFormula(Avogadro::QtGui::Molecule *molecule); + +private: + /** + * Get file format reader appropriate for the file format + */ + static std::unique_ptr getFileReader(const QString &format); }; #endif diff --git a/compoundviewer/iowrapper.cpp b/compoundviewer/iowrapper.cpp --- a/compoundviewer/iowrapper.cpp +++ b/compoundviewer/iowrapper.cpp @@ -20,29 +20,40 @@ #include #include +#include +#include #include #include +#include #include #include #include -Avogadro::Core::Molecule * IoWrapper::readMolecule(const QString &filename) +std::unique_ptr IoWrapper::readMolecule(const QString &filename) { std::ifstream inFileStream(QFile::encodeName(filename).constData()); if (!inFileStream) { QMessageBox::warning(nullptr, i18n("Problem while opening the file"), i18n("Cannot open the specified file.")); return nullptr; } - auto mol = new Avogadro::Core::Molecule; - Avogadro::Io::CmlFormat cmlFormat; - if (!cmlFormat.read(inFileStream, *mol)) { - qCritical() << "Could not read file:" << filename; + auto mol = std::make_unique(); + auto format = getFileReader(QFileInfo(filename).suffix()); + + if (!format){ + qCritical() << "Could not initialize file reader for file " << filename; + return nullptr; + } + + if (!format->read(inFileStream, *mol)) { + qCritical() << "Could not read file " << filename << "; Error message: " + << QString().fromStdString(format->error()); return nullptr; } + return mol; } @@ -72,3 +83,16 @@ formula.replace(QRegularExpression("(\\d+)"), "\\1"); return formula; } + +std::unique_ptr IoWrapper::getFileReader(const QString &format) +{ + if (format == QStringLiteral("cml")) { + return std::make_unique(); + } else if (format == QStringLiteral("pdb")) { + return std::make_unique(); + } else if (format == QStringLiteral("xyz")) { + return std::make_unique(); + } else { + return nullptr; + } +} diff --git a/src/tools/moleculeview.cpp b/src/tools/moleculeview.cpp --- a/src/tools/moleculeview.cpp +++ b/src/tools/moleculeview.cpp @@ -187,7 +187,14 @@ // 2. another workaround for broken copy-constructor that does not // initialize the m_undoMolecule private member variable; // this molecule should be created on the heap instead of the stack - m_molecule = *IoWrapper::readMolecule(filename); + auto molecule_ptr = IoWrapper::readMolecule(filename); + if (!molecule_ptr) + { + KMessageBox::error(this, i18n("Could not load molecule"), i18n("Loading the molecule failed.")); + return; + } + + m_molecule = *molecule_ptr; if (m_molecule.atomCount() != 0) { disconnect(ui.glWidget->molecule(), 0, this, 0);