summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndreas Cord-Landwehr <cordlandwehr@kde.org>2016-04-03 14:42:03 (GMT)
committerAndreas Cord-Landwehr <cordlandwehr@kde.org>2016-04-05 17:59:42 (GMT)
commit1a67cb2142dc2dbf7a540666c3cffab21f32ddf5 (patch)
tree8af5245d9579eb6db7a0d1a4cb175d0e9af25c26
parentd0d765ecbe8d5ef796b02390439bddf4f0044546 (diff)
Complete port of OpenBabel2Wrapper and rename to IoWrapper.
There is on OpenBabel2 specific code left in it.
-rw-r--r--compoundviewer/CMakeLists.txt2
-rw-r--r--compoundviewer/iowrapper.cpp74
-rw-r--r--compoundviewer/iowrapper.h (renamed from compoundviewer/openbabel2wrapper.h)13
-rw-r--r--compoundviewer/kalziumglwidget.cpp6
-rw-r--r--compoundviewer/openbabel2wrapper.cpp99
-rw-r--r--src/tools/moleculeview.cpp13
6 files changed, 94 insertions, 113 deletions
diff --git a/compoundviewer/CMakeLists.txt b/compoundviewer/CMakeLists.txt
index b1db8ff..2109d84 100644
--- a/compoundviewer/CMakeLists.txt
+++ b/compoundviewer/CMakeLists.txt
@@ -6,7 +6,7 @@ include_directories(
)
set(compoundviewer_SRCS
- openbabel2wrapper.cpp
+ iowrapper.cpp
kalziumglwidget.cpp
)
diff --git a/compoundviewer/iowrapper.cpp b/compoundviewer/iowrapper.cpp
new file mode 100644
index 0000000..5fba606
--- /dev/null
+++ b/compoundviewer/iowrapper.cpp
@@ -0,0 +1,74 @@
+/***************************************************************************
+ * Copyright (C) 2006 by Carsten Niehaus <cniehaus@kde.org>
+ * Copyright (C) 2007-2008 by Marcus D. Hanwell <marcus@cryos.org>
+ * Copyright (C) 2016 by Andreas Cord-Landwehr <cordlandwehr@kde.org>
+ ***************************************************************************/
+
+/***************************************************************************
+ * *
+ * This program is free software; you can redistribute it and/or modify *
+ * it under the terms of the GNU General Public License as published by *
+ * the Free Software Foundation; either version 2 of the License, or *
+ * (at your option) any later version. *
+ * *
+ ***************************************************************************/
+
+#include "iowrapper.h"
+
+#include <iostream>
+#include <fstream>
+
+#include <avogadro/core/molecule.h>
+#include <avogadro/io/cmlformat.h>
+
+#include <QDebug>
+#include <QMessageBox>
+#include <QRegExp>
+#include <QFile>
+
+#include <KLocalizedString>
+
+Avogadro::Core::Molecule * IoWrapper::readMolecule(const QString &filename)
+{
+ std::ifstream inFileStream(QFile::encodeName(filename));
+ 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;
+ return nullptr;
+ }
+ return mol;
+}
+
+bool IoWrapper::writeMolecule(const QString &filename, Avogadro::Core::Molecule *mol)
+{
+ std::ofstream outFileStream(QFile::encodeName(filename));
+ if (!outFileStream) {
+ QMessageBox::warning(nullptr, i18n("Sorry"), i18n("Cannot save to the specified file."));
+ return false;
+ }
+ Avogadro::Io::CmlFormat cmlFormat;
+ if (!cmlFormat.write(outFileStream, *mol)) {
+ qCritical() << "Could not read file:" << filename;
+ return false;
+ }
+ return true;
+}
+
+QString IoWrapper::getFormula(Avogadro::QtGui::Molecule *molecule)
+{
+ return QString::fromStdString(molecule->formula());
+}
+
+QString IoWrapper::getPrettyFormula(Avogadro::QtGui::Molecule *molecule)
+{
+ QString formula = QString::fromStdString(molecule->formula());
+ formula.replace(QRegExp("(\\d+)"), "<sub>\\1</sub>");
+ return formula;
+}
diff --git a/compoundviewer/openbabel2wrapper.h b/compoundviewer/iowrapper.h
index 58e5890..8746f38 100644
--- a/compoundviewer/openbabel2wrapper.h
+++ b/compoundviewer/iowrapper.h
@@ -1,6 +1,7 @@
/***************************************************************************
* Copyright (C) 2006 by Carsten Niehaus <cniehaus@kde.org>
* Copyright (C) 2007-2008 by Marcus D. Hanwell <marcus@cryos.org>
+ * Copyright (C) 2016 by Andreas Cord-Landwehr <cordlandwehr@kde.org>
***************************************************************************/
/***************************************************************************
@@ -12,8 +13,8 @@
* *
***************************************************************************/
-#ifndef OPENBABEL2WRAPPER_H
-#define OPENBABEL2WRAPPER_H
+#ifndef IOWRAPPER_H
+#define IOWRAPPER_H
#include <avogadro/qtgui/molecule.h>
@@ -22,20 +23,20 @@
/**
* @author Carsten Niehaus
*/
-class COMPOUNDVIEWER_EXPORT OpenBabel2Wrapper
+class COMPOUNDVIEWER_EXPORT IoWrapper
{
public:
/**
* This class reads the molecule in the file @p filename. It returns 0 if
* the file couldn't be read.
*/
- static Avogadro::QtGui::Molecule *readMolecule(const QString& filename);
+ static Avogadro::Core::Molecule * readMolecule(const QString &filename);
- static bool writeMolecule(const QString& filename, Avogadro::QtGui::Molecule *);
+ static bool writeMolecule(const QString& filename, Avogadro::Core::Molecule *);
static QString getFormula(Avogadro::QtGui::Molecule *molecule);
static QString getPrettyFormula(Avogadro::QtGui::Molecule *molecule);
};
-#endif // OPENBABEL2WRAPPER_H
+#endif
diff --git a/compoundviewer/kalziumglwidget.cpp b/compoundviewer/kalziumglwidget.cpp
index 03f5bd3..1305c9e 100644
--- a/compoundviewer/kalziumglwidget.cpp
+++ b/compoundviewer/kalziumglwidget.cpp
@@ -21,7 +21,7 @@
#include <avogadro/qtgui/molecule.h>
// #include <avogadro/toolgroup.h>//FIXME:Avogadro2 port away from toolgroup
#include <QWidget>
-#include "openbabel2wrapper.h"
+#include "iowrapper.h"
#include <avogadro/qtopengl/glwidget.h>
#include <config-kalzium.h>
@@ -71,7 +71,9 @@ KalziumGLWidget::~KalziumGLWidget()
bool KalziumGLWidget::openFile(const QString &file)
{
- Avogadro::QtGui::Molecule* mol = OpenBabel2Wrapper::readMolecule(file);
+ auto tmpMol = IoWrapper::readMolecule(file);
+ auto mol = new Avogadro::QtGui::Molecule(*tmpMol);
+ delete tmpMol;
if (!mol) {
return false;
}
diff --git a/compoundviewer/openbabel2wrapper.cpp b/compoundviewer/openbabel2wrapper.cpp
deleted file mode 100644
index 1787846..0000000
--- a/compoundviewer/openbabel2wrapper.cpp
+++ /dev/null
@@ -1,99 +0,0 @@
-/***************************************************************************
- * Copyright (C) 2006 by Carsten Niehaus <cniehaus@kde.org>
- * Copyright (C) 2007-2008 by Marcus D. Hanwell <marcus@cryos.org>
- ***************************************************************************/
-
-/***************************************************************************
- * *
- * This program is free software; you can redistribute it and/or modify *
- * it under the terms of the GNU General Public License as published by *
- * the Free Software Foundation; either version 2 of the License, or *
- * (at your option) any later version. *
- * *
- ***************************************************************************/
-
-#include "openbabel2wrapper.h"
-
-#include <sstream>
-#include <iostream>
-#include <fstream>
-
-#include <openbabel/obconversion.h>
-#include <openbabel/mol.h>
-
-#include <QDebug>
-#include <QMessageBox>
-#include <QMouseEvent>
-#include <QRegExp>
-#include <QFile>
-
-#include <KLocalizedString>
-
-Avogadro::QtGui::Molecule * OpenBabel2Wrapper::readMolecule(const QString &filename)
-{
- OpenBabel::OBConversion Conv;
- OpenBabel::OBFormat *inFormat = nullptr;
-
- // the Avogadro Molecule
- Avogadro::QtGui::Molecule *mol = new Avogadro::QtGui::Molecule;
- OpenBabel::OBMol *obmol = new OpenBabel::OBMol;
- std::ifstream inFileStream(QFile::encodeName(filename));
- if (!inFileStream) {
- QMessageBox::warning(nullptr, i18n("Problem while opening the file"),
- i18n("Cannot open the specified file."));
- delete mol;
- return nullptr;
- }
-
- //find out which format the file has...
- inFormat = Conv.FormatFromExt(QFile::encodeName(filename));
- if (!inFormat || !Conv.SetInFormat(inFormat)) {
- QMessageBox::warning(nullptr, i18n("Cannot read the file format. Check your OpenBabel installation."),
- i18n("Problem reading file format"));
- delete mol;
- return nullptr;
- }
- Conv.SetInAndOutFormats(inFormat,inFormat);
- Conv.Read(obmol, &inFileStream);
-
- qDebug() << QString::fromLatin1(obmol->GetFormula().c_str())
- << " (Weight: " << obmol->GetMolWt() << ", Title: "<< obmol->GetTitle() << ")";
-
-// mol->setOBMol(obmol);///*FIXME:Avogadro2*/ method gone
- return mol;
-}
-
-bool OpenBabel2Wrapper::writeMolecule(const QString &filename, Avogadro::QtGui::Molecule *mol)
-{
- OpenBabel::OBConversion Conv;
- OpenBabel::OBFormat *outFormat = nullptr;
-
- std::ofstream outFileStream(QFile::encodeName(filename));
- if (!outFileStream) {
- QMessageBox::warning(nullptr, i18n("Sorry"), i18n("Cannot save to the specified file."));
- return false;
- }
- outFormat = Conv.FormatFromExt(QFile::encodeName(filename));
- if (!outFormat || !Conv.SetOutFormat(outFormat)) {
- QMessageBox::warning(nullptr, i18n("Unrecognized file format extension. Please append an extension to the file name, "),
- i18n("for example \".cml\"."));
- delete mol;
- return false;
- }
- Conv.SetInAndOutFormats(outFormat,outFormat);
-// OpenBabel::OBMol obmol = mol->OBMol();//FIXME:Avogadro2 method gone
-// Conv.Write(&obmol, &outFileStream);//FIXME:Avogadro2
- return true;
-}
-
-QString OpenBabel2Wrapper::getFormula(Avogadro::QtGui::Molecule *molecule)
-{
- return QString::fromStdString(molecule->formula());
-}
-
-QString OpenBabel2Wrapper::getPrettyFormula(Avogadro::QtGui::Molecule *molecule)
-{
- QString formula = QString::fromStdString(molecule->formula());
- formula.replace(QRegExp("(\\d+)"), "<sub>\\1</sub>");
- return formula;
-}
diff --git a/src/tools/moleculeview.cpp b/src/tools/moleculeview.cpp
index 39563c3..19feded 100644
--- a/src/tools/moleculeview.cpp
+++ b/src/tools/moleculeview.cpp
@@ -32,7 +32,7 @@
#include <knewstuff3/downloaddialog.h>
#include <kio/job.h>
-#include <openbabel2wrapper.h>
+#include "iowrapper.h"
#include <openbabel/mol.h>
#include <openbabel/obiter.h>
@@ -46,7 +46,6 @@
using namespace OpenBabel;
using namespace Avogadro::QtGui;
-OpenBabel2Wrapper openBabel;
MoleculeDialog::MoleculeDialog(QWidget * parent)
: KDialog(parent), m_periodicTable(0), m_addHydrogens(false)
{
@@ -195,7 +194,11 @@ void MoleculeDialog::loadMolecule(const QString &filename)
qDebug() << "Filename to load: " << filename;
- Avogadro::QtGui::Molecule* molecule = OpenBabel2Wrapper::readMolecule(filename);
+ auto tmpMol = IoWrapper::readMolecule(filename);
+ auto molecule = new Avogadro::QtGui::Molecule(*tmpMol);
+ if (tmpMol) {
+ delete tmpMol;
+ }
// Check that a valid molecule object was returned
if (!molecule) {
@@ -238,8 +241,8 @@ void MoleculeDialog::slotSaveMolecule()
filename.append(".cml");
}
- OpenBabel2Wrapper openBabel;
- openBabel.writeMolecule(filename, ui.glWidget->molecule());
+ IoWrapper io;
+ io.writeMolecule(filename, ui.glWidget->molecule());
}
void MoleculeDialog::setViewEdit(int mode)