~ubuntu-branches/ubuntu/precise/kalzium/precise

« back to all changes in this revision

Viewing changes to compoundviewer/openbabel2wrapper.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Philip Muškovac
  • Date: 2011-07-03 12:28:58 UTC
  • Revision ID: james.westby@ubuntu.com-20110703122858-q1yyxncs89e4w0hs
Tags: upstream-4.6.90+repack
Import upstream version 4.6.90+repack

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
 *  Copyright (C) 2006 by Carsten Niehaus <cniehaus@kde.org>
 
3
 *  Copyright (C) 2007-2008 by Marcus D. Hanwell <marcus@cryos.org>
 
4
 ***************************************************************************/
 
5
 
 
6
/***************************************************************************
 
7
 *                                                                         *
 
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.                                   *
 
12
 *                                                                         *
 
13
 ***************************************************************************/
 
14
#include "openbabel2wrapper.h"
 
15
 
 
16
#include <kdebug.h>
 
17
#include <klocale.h>
 
18
#include <KMessageBox>
 
19
 
 
20
#include <sstream>
 
21
#include <stdio.h>
 
22
#include <iostream>
 
23
#include <fstream>
 
24
#include <string.h>
 
25
#include <stdlib.h>
 
26
 
 
27
#include <QMouseEvent>
 
28
#include <QRegExp>
 
29
#include <QFile>
 
30
 
 
31
 
 
32
Avogadro::Molecule* OpenBabel2Wrapper::readMolecule( const QString& filename )
 
33
{
 
34
        OpenBabel::OBConversion Conv;
 
35
        OpenBabel::OBFormat *inFormat = NULL;
 
36
 
 
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." ) );
 
45
    delete mol;
 
46
    return 0;
 
47
        }
 
48
 
 
49
        //find out which format the file has...
 
50
        inFormat = Conv.FormatFromExt( QFile::encodeName(filename) );
 
51
  if (!inFormat || !Conv.SetInFormat(inFormat))
 
52
  {
 
53
    KMessageBox::error( 0, i18n("Cannot read the file format. Check your OpenBabel installation."), i18n("Problem reading file format"));
 
54
    delete mol;
 
55
    return 0;
 
56
  }
 
57
        Conv.SetInAndOutFormats( inFormat,inFormat );
 
58
        Conv.Read( obmol, &inFileStream );
 
59
 
 
60
        kDebug() << QString::fromLatin1( obmol->GetFormula().c_str() )  << " (Weight: " << obmol->GetMolWt() << ", Title: "<< obmol->GetTitle() << ")";
 
61
        mol->setOBMol(obmol);
 
62
        return mol;
 
63
}
 
64
 
 
65
bool OpenBabel2Wrapper::writeMolecule( const QString& filename, Avogadro::Molecule* mol )
 
66
{
 
67
  OpenBabel::OBConversion Conv;
 
68
  OpenBabel::OBFormat *outFormat = NULL;
 
69
 
 
70
  std::ofstream outFileStream( QFile::encodeName(filename) );
 
71
  if ( !outFileStream ) {
 
72
    KMessageBox::error(  0,
 
73
      i18n( "Cannot save to the specified file." )
 
74
      );
 
75
    return false;
 
76
  }
 
77
  outFormat = Conv.FormatFromExt( QFile::encodeName(filename) );
 
78
  if (!outFormat || !Conv.SetOutFormat(outFormat))
 
79
  {
 
80
    KMessageBox::error( 0, i18n("Unrecognized file format extension. Please append an extension to the file name, "
 
81
                                "for example \".cml\".") );
 
82
    delete mol;
 
83
    return false;
 
84
  }
 
85
  Conv.SetInAndOutFormats( outFormat,outFormat );
 
86
  OpenBabel::OBMol obmol = mol->OBMol();
 
87
  Conv.Write( &obmol, &outFileStream );
 
88
  return true;
 
89
}
 
90
 
 
91
QString OpenBabel2Wrapper::getFormula( Avogadro::Molecule* molecule )
 
92
{
 
93
        QString formula( molecule->OBMol().GetFormula().c_str() );
 
94
        return formula;
 
95
}
 
96
                
 
97
QString OpenBabel2Wrapper::getPrettyFormula( Avogadro::Molecule* molecule )
 
98
{
 
99
        QString formula( molecule->OBMol().GetSpacedFormula(1,"").c_str() );
 
100
        formula.replace( QRegExp( "(\\d+)" ), "<sub>\\1</sub>" );
 
101
        return formula;
 
102
}