/***************************************************************************
                          crings_dialog.cpp  -  Dialog for Custom Rigns
                             -------------------
    begin                : Sat Aug 31 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 "crings_dialog.h"
#include <kstandarddirs.h>
#include <qpixmap.h>

CustomRingWidget::CustomRingWidget(QWidget* parent, const char* name)
  : QWidget(parent,name)  {

  setFixedSize(320,310);

  render=new Render2D(0);
  data=new ChemData(this);

  widget=new QWidget(this);
  widget->setFixedSize(300,240);
  widget->setGeometry(10,10,300,240);
  widget->show();

  render->setChemData(data);
  data->setRender2D(render);

  title=new KLineEdit(this);
  title->setGeometry(100,270,160,20);

  connect(title,SIGNAL(textChanged(const QString&)),this,SIGNAL(signalTextChanged(const QString&)));

  label=new QLabel("Title: ",this);
  label->setGeometry(60,270,25,20);

  //render->setMode_SelectNone();

}

CustomRingWidget::~CustomRingWidget() {

  if(render)
    delete render;
  if(data)
    delete data;
  if(title)
    delete title;
  if(label)
    delete label;

}

void CustomRingWidget::slotTitle(QString& string) {

  data->save(string);

}

void CustomRingWidget::setMolecule(Molecule* m) {

  data->addMolecule(m);
  Render2D* real_render=m->getRender2D();

  //QPixmap pixmap=real_render->MakePixmap();

  QPixmap mol = real_render->MakeFullPixmap();
  QRect rr1 = m->BoundingBoxAll();
  rr1.setLeft(rr1.left() - 4);
  rr1.setTop(rr1.top() - 4);
  rr1.setBottom(rr1.bottom() + 4);
  rr1.setRight(rr1.right() + 4);
  QPixmap mol1(rr1.size());
  bitBlt(&mol1, QPoint(0,0), &mol, rr1);

  pixmap=mol1;
  //pixmap.fill(Qt::white);
  //if(pixmap.isNull())
    //cerr << "bad pixmpa!!!" << endl;
  //m->SelectAll();
  //QRect rect=m->BoundingBox();

  QPainter painter(widget);
  //painter.drawPixmap(QPoint(0,0),pixmap,QRect(0,0,rect.width(),rect.height()));
  //cerr << rect.x() << " " << rect.y() << " " << rect.width() << " " << endl;
  //bitBlt(widget,QPoint(0,0),&pixmap,rect);
  painter.drawPixmap(QPoint(0,0),mol1);
  widget->show();
}

void CustomRingWidget::paintEvent(QPaintEvent* pe) {

  QPainter p(widget);
  p.drawPixmap(QPoint(0,0),pixmap);
}

CustomRingDialog::CustomRingDialog(QWidget* parent,const char* name)
  : KDialogBase(parent,"ringdialog",true,"Add Custom ring...",
      KDialogBase::Ok|KDialogBase::Cancel,KDialogBase::Ok,false) {

  m_widget=new CustomRingWidget(this);
  setMainWidget(m_widget);

  connect(m_widget,SIGNAL(signalTextChanged(const QString&)),this,SLOT(slotTextChanged(const QString&)));
  connect(this,SIGNAL(signalTitle(QString&)),m_widget,SLOT(slotTitle(QString&)));

  disableResize();
  enableButtonOK(false);

}

CustomRingDialog::~CustomRingDialog() {

  if(m_widget)
    delete m_widget;
}

void CustomRingDialog::slotTextChanged(const QString& string) {

  if(string.isEmpty())
    enableButtonOK(false);
  else //if string!=QString::null
    enableButtonOK(true);
}

void CustomRingDialog::slotOk() {

  QString title=m_widget->getTitle();
  std::cerr << title << std::endl;
  QString filename=title.lower();
  filename.append(".cml");
  m_rings->addEntry(title,filename);

  KStandardDirs* dir=KGlobal::dirs();

  QString file=dir->localkdedir();
  file+="share/apps/kdrawchem/rings/";

  file+=filename;    //...share/apps/kdrawchem/rings/foobar.cml

  emit signalTitle(file);

  hide();

}


#include "crings_dialog.moc"


//eof


