1
/***************************************************************************
2
* Copyright (C) 2006 by Carsten Niehaus <cniehaus@kde.org>
3
* Copyright (C) 2007-2008 by Marcus D. Hanwell <marcus@cryos.org>
4
***************************************************************************/
6
/***************************************************************************
8
* This program is free software; you can redistribute it and/or modify *
9
* it under the terms of the GNU General Public License as published by *
10
* the Free Software Foundation; either version 2 of the License, or *
11
* (at your option) any later version. *
13
***************************************************************************/
14
#include "openbabel2wrapper.h"
18
#include <KMessageBox>
27
#include <QMouseEvent>
32
Avogadro::Molecule* OpenBabel2Wrapper::readMolecule( const QString& filename )
34
OpenBabel::OBConversion Conv;
35
OpenBabel::OBFormat *inFormat = NULL;
37
// the Avogadro Molecule
38
Avogadro::Molecule *mol = new Avogadro::Molecule;
39
OpenBabel::OBMol *obmol = new OpenBabel::OBMol;
40
std::ifstream inFileStream( QFile::encodeName(filename) );
41
if ( !inFileStream ) {
42
KMessageBox::error( 0,
43
i18n( "Problem while opening the file" ),
44
i18n( "Cannot open the specified file." ) );
49
//find out which format the file has...
50
inFormat = Conv.FormatFromExt( QFile::encodeName(filename) );
51
if (!inFormat || !Conv.SetInFormat(inFormat))
53
KMessageBox::error( 0, i18n("Cannot read the file format. Check your OpenBabel installation."), i18n("Problem reading file format"));
57
Conv.SetInAndOutFormats( inFormat,inFormat );
58
Conv.Read( obmol, &inFileStream );
60
kDebug() << QString::fromLatin1( obmol->GetFormula().c_str() ) << " (Weight: " << obmol->GetMolWt() << ", Title: "<< obmol->GetTitle() << ")";
65
bool OpenBabel2Wrapper::writeMolecule( const QString& filename, Avogadro::Molecule* mol )
67
OpenBabel::OBConversion Conv;
68
OpenBabel::OBFormat *outFormat = NULL;
70
std::ofstream outFileStream( QFile::encodeName(filename) );
71
if ( !outFileStream ) {
72
KMessageBox::error( 0,
73
i18n( "Cannot save to the specified file." )
77
outFormat = Conv.FormatFromExt( QFile::encodeName(filename) );
78
if (!outFormat || !Conv.SetOutFormat(outFormat))
80
KMessageBox::error( 0, i18n("Unrecognized file format extension. Please append an extension to the file name, "
81
"for example \".cml\".") );
85
Conv.SetInAndOutFormats( outFormat,outFormat );
86
OpenBabel::OBMol obmol = mol->OBMol();
87
Conv.Write( &obmol, &outFileStream );
91
QString OpenBabel2Wrapper::getFormula( Avogadro::Molecule* molecule )
93
QString formula( molecule->OBMol().GetFormula().c_str() );
97
QString OpenBabel2Wrapper::getPrettyFormula( Avogadro::Molecule* molecule )
99
QString formula( molecule->OBMol().GetSpacedFormula(1,"").c_str() );
100
formula.replace( QRegExp( "(\\d+)" ), "<sub>\\1</sub>" );