/***************************************************************************
 *   Copyright (C) 2009 Tim Vandermeersch                                  *
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#include <QPainter>
#include <QDebug>
#include <QLibrary>

#include "obabeliface.h"
#include "stereocenteritem.h"
#include "molscene.h"
#include "mimemolecule.h"
#include "molecule.h"

namespace Molsketch {

  StereoCenterItem::StereoCenterItem() : MolInputItem(MoleculeOutput)
  {
  }

  void StereoCenterItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
  {
    Molecule *mol = molecule();
    
    painter->save();
    painter->setPen(Qt::green);

    if (!mol) {
      // not connected: default behaviour (draw connectable box)
      MolInputItem::paint(painter, option, widget);
      painter->restore();
      return;
    }

    QLibrary obabeliface("obabeliface" QTVERSIONSUFFIX);
    obabeliface.load() ;
    chiralAtomsFunctionPointer chiralAtomsPtr = (chiralAtomsFunctionPointer) obabeliface.resolve("chiralAtoms") ;
    if (chiralAtomsPtr)
      foreach(Atom* atom, chiralAtomsPtr(mol))
        painter->drawEllipse(mapFromItem(mol, atom->pos()), 10, 10);
    else
      painter->drawText(mapFromItem(mol, mol->graphicalCenterOfMass()), "OpenBabel unavailable") ;
    
    // default behavious (draw the label())
    MolInputItem::paint(painter, option, widget);
    painter->restore();
  }
  
  StereoCenterItemFactory theStereoCenterItemFactory;

}
