/***************************************************************************
                          kdcpreview.cpp  -  preview widget for file dialogs
                             -------------------
    begin                : Mon Aug 26 2002
    copyright            : (C) 2002 by Adam Tenderholt
    email                : atenderholt@users.sourceforge.net
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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 <qstring.h>
#include <qpainter.h>
#include <qlayout.h>
#include <kio/netaccess.h>
#include <kdialog.h>

#include "kdcpreview.h"
#include "kdrawchem.h"
#include "chemdata.h"
#include <mol.h>
#include <fstream>

using namespace OpenBabel;

KDCPreviewWidget::KDCPreviewWidget(QWidget* parent, const char* name)
  : QFrame(parent) {

    //viewport()->setBackgroundMode( PaletteBase );
  //setSizePolicy(QSizePolicy(QSizePolicy::Preferred,QSizePolicy::Preferred));
    setFixedSize(200,250);
    m_pixmap=new QPixmap(200,250);
    m_pixmap->fill(Qt::white);

    QPainter painter(m_pixmap);
    QRect rect=m_pixmap->rect();
    painter.setPen(QColor(48,48,48));
    painter.drawLine(rect.width()-1,0,rect.width()-1,rect.height());
    painter.drawLine(0,rect.height()-1,rect.width(),rect.height()-1);

}

KDCPreviewWidget::~KDCPreviewWidget() {

  if(m_pixmap)
    delete m_pixmap;
}

void KDCPreviewWidget::setFile(QString filename) {

  ChemData* data=new ChemData(this,0);
  data->setRender2D(m_render);
  QString ext=filename.right(3);

  if(ext=="mol"||ext=="xdc"||ext=="cml")
    data->load(filename);

  else  {   //assume that OpenBabel can read

    OBMol *mol=new OBMol;

    OBExtensionTable extab;
	  enum io_type importType;

	  importType=extab.FilenameToType(const_cast<char*>(filename.latin1()));  //doesn't like being a const char*--casted it to a char*

	  mol->SetInputType(importType);
	  std::ifstream fin(filename,std::ios::in);
	  if(fin==NULL) {
      std::cerr << "Something wrong" << std::endl;
      return /*false*/;
    }

	    //fin >> mol;  Old school code from OELib--now use OpenBabel code;
	  OBFileFormat file;
	  file.ReadMolecule(fin,*mol);

    KDrawChemApp::interface.setChemData(data);
    KDrawChemApp::interface.setOBMol(mol);
    KDrawChemApp::interface.convertToChemData();
  }

  m_pixmap->fill(Qt::white);
  QRect rect=m_pixmap->rect();
  QPainter painter(m_pixmap);
  painter.setPen(QColor(48,48,48));
  painter.drawLine(rect.width()-1,0,rect.width()-1,rect.height());
  painter.drawLine(0,rect.height()-1,rect.width(),rect.height()-1);
  painter.setPen(Qt::black);
  data->drawBonds(&painter,0.5);
  //data->drawText(&painter,0.5);
  bitBlt(this,0,0,m_pixmap,0,0,200,250);

  delete data;

}

void KDCPreviewWidget::paintEvent(QPaintEvent* e) {

  bitBlt(this,0,0,m_pixmap,0,0,200,250);

}

KDCPreview::KDCPreview(QWidget *parent, const char *name)
  : KPreviewWidgetBase(parent,name)  {

   m_widget=new KDCPreviewWidget(this);
   //QVBoxLayout *vb = new QVBoxLayout( this, KDialog::marginHint() );
   QGridLayout *vb=new QGridLayout(this,3,3);
   vb->addWidget( m_widget, 1,1 );
   m_widget->setFrameStyle(QFrame::Panel | QFrame::Raised);
   m_widget->setLineWidth(2);
   m_widget->show();
   setFixedSize(260,350);
}

KDCPreview::~KDCPreview() {

  if(m_widget)
    delete m_widget;
}

void KDCPreview::showPreview(const KURL& url) {

  m_url=url;
  m_widget->setRender2D(m_render);

  if(url.isLocalFile())
    m_widget->setFile(m_url.path());
  else
  {
     QString tmpfile;
     KIO::NetAccess::download( m_url, tmpfile );
     m_widget->setFile(tmpfile);
     KIO::NetAccess::removeTempFile( tmpfile );

  }
}

void KDCPreview::clearPreview() {

}



//eof


