/***************************************************************************
                          kdrawchem.cpp  -  description
                             -------------------
    begin                : Mon Jul 15 12:24:12 CDT 2002
    copyright            : (C) 2002 by Adam Tenderholt
    email                : a-tenderholt@northwestern.edu
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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 files for QT
#include <qdir.h>
//#include <qprinter.h>
#include <qpainter.h>
#include <qfontdatabase.h>
#include <qinputdialog.h>
#include <qstringlist.h>
#include <qstring.h>

// include files for KDE
#include <kaction.h>
#include <kmessagebox.h>
#include <kfiledialog.h>
#include <kmenubar.h>
#include <kstatusbar.h>
#include <klocale.h>
#include <kconfig.h>
#include <kpopupmenu.h>
#include <kprinter.h>
#include <kstandarddirs.h>
#include <kcolordialog.h>

// application specific includes
#include "kdrawchem.h"
#include "kdrawchemview.h"
#include "kdrawchemdoc.h"
#include "kdcpreview.h"
#include "arrows.h"  //contains xpms for arrows
#include "brackets.h"  //contains xpms for brackets
#include "symbol_xpm.h" //contains xpms for symbols
#include "curve_xpm.h"

#include "crings_dialog.h"
#include "fixeddialog.h"

#include "kemistry-openbabel.h"

#define ID_STATUS_MSG 1
/////////////////////////////////////////////////////////////////////
// SLOT IMPLEMENTATION
/////////////////////////////////////////////////////////////////////

void KDrawChemApp::slotFileNewWindow()
{
  slotStatusMsg(i18n("Opening a new application window..."));

  KDrawChemApp *new_window= new KDrawChemApp();
  new_window->show();

  slotStatusMsg(i18n("Ready."));
}

void KDrawChemApp::slotFileNew()
{
  slotStatusMsg(i18n("Creating new document..."));
  std::cerr << doc->isModified() << std::endl;

  if(!doc->saveModified())
  {
     // here saving wasn't successful

  }
  else
  {

    if(doc)
      delete doc;
    doc = new KDrawChemDoc(this);

    if(_clipboard)
      delete _clipboard;
    _clipboard = new Clipboard();


    view->setChemData(doc);
    doc->setRender2D(view);
    doc->setClipboard(_clipboard);
    setCaption("Untitled", false);
    connect(view,SIGNAL(signalModified()),doc,SLOT(slotModified()));


    doc->setModified(false);

    fileSave->setEnabled(false);
    fileSaveAs->setEnabled(false);
    filePrint->setEnabled(false);
    editCut->setEnabled(false);
    editCopy->setEnabled(false);
    editPaste->setEnabled(false);

  }

  slotStatusMsg(i18n("Ready."));
}

void KDrawChemApp::slotFileOpen()
{
  slotStatusMsg(i18n("Opening file..."));

  if(!doc->saveModified())
  {
     // here saving wasn't successful

  }
  else
  {

    if(!dialog)
      dialog = new KFileDialog(QDir::currentDirPath(),KDChemFilter,this,"file_dia",false);

    KDCPreview* kdcpreview=new KDCPreview(dialog,"kdc_preview");
    kdcpreview->setRender2D(view);
    dialog->setPreviewWidget(kdcpreview);
    dialog->setCaption(i18n( "Open file..." ));
    kdcpreview->show();
    dialog->exec();
    KURL url=dialog->selectedURL();
    /*KURL url=KFileDialog::getOpenURL(QString::null,
        KDChemFilter, this, i18n("Open File..."));*/
    if(!url.isEmpty())
    {
      slotFileNew();
      doc->openDocument(url);
      setCaption(url.fileName(), false);
      fileOpenRecent->addURL( url );
    }

    if(dialog) {
      delete dialog;
      dialog=0;
    }
  }
  slotStatusMsg(i18n("Ready."));
}

void KDrawChemApp::slotFileImport()
{
  slotStatusMsg(i18n("Importing file..."));

  if(false)
  {
     // here saving wasn't successful

  }
  else
  {
      QString mime;
      Kemistry::getOBReadMimeTypes(mime);

    if(!dialog)
      dialog = new KFileDialog(QDir::currentDirPath(), mime,this,"file_dia",false);

    KDCPreview* kdcpreview=new KDCPreview(dialog,"kdc_preview");
    kdcpreview->setRender2D(view);
    dialog->setPreviewWidget(kdcpreview);
    dialog->setCaption("Import file...");
    kdcpreview->show();
    dialog->exec();
    KURL url=dialog->selectedURL();
    /*KURL url=KFileDialog::getOpenURL(QString::null,
        "chemical/x-openbabel-input", this, i18n("Import File..."));*/

    if(!url.isEmpty())
      doc->import(url);

    if(dialog) {
      delete dialog;
      dialog=0;
    }
  }
  slotStatusMsg(i18n("Ready."));
}

void KDrawChemApp::slotFileOpenRecent(const KURL& url)
{
  slotStatusMsg(i18n("Opening file..."));

  if(!doc->saveModified())
  {
     // here saving wasn't successful
  }
  else
  {
    doc->openDocument(url);
    setCaption(url.fileName(), false);
  }

  slotStatusMsg(i18n("Ready."));
}

void KDrawChemApp::slotFileSave()
{
  slotStatusMsg(i18n("Saving file..."));
	KURL tmpurl=doc->URL();
  std::cerr << tmpurl.fileName().latin1() << std::endl;
  if(tmpurl.fileName()=="Untitled")
    return slotFileSaveAs();

  doc->saveDocument(doc->URL());

  slotStatusMsg(i18n("Ready."));
  fileSave->setEnabled(false);
  fileSaveAs->setEnabled(false);
}

void KDrawChemApp::slotFileSaveAs()
{
  slotStatusMsg(i18n("Saving file with a new filename..."));

  //KURL url=KFileDialog::getSaveURL(QDir::currentDirPath(),
        //KDChemFilter, this, i18n("Save as..."));

  if(!dialog)
    dialog = new KFileDialog(QDir::currentDirPath(),KDChemFilter,this,"file_dia",false);

  KDCPreview* kdcpreview=new KDCPreview(dialog,"kdc_preview");
  kdcpreview->setRender2D(view);
  dialog->setPreviewWidget(kdcpreview);
  dialog->setCaption("Save file as...");
  kdcpreview->show();
  dialog->exec();
  KURL url=dialog->selectedURL();
  QString filename=url.path(-1);
  std::cerr << "filename: " << filename << std::endl;


  QString extension=dialog->currentFilter();
  extension.remove(0,1);    //remove '*' from filter
  std::cerr << extension.latin1() << std::endl;

  if(filename.find(extension)==-1)
    filename+=extension;
  if (filename.length()<=4)
    filename="";
  //url.setFileName(filename);
  url.setPath(filename);
  if(dialog) {
     delete dialog;
     dialog=0;
  }
  if(!url.isEmpty())
  {
    //cerr << filename.latin1() << endl;
    bool wasSaved=doc->saveDocument(url);
    fileOpenRecent->addURL(url);
    if(wasSaved)
      setCaption(url.fileName(),false);
  }

  slotStatusMsg(i18n("Ready."));
  fileSave->setEnabled(false);
  fileSaveAs->setEnabled(false);
}

void KDrawChemApp::slotFileClose()
{
  slotStatusMsg(i18n("Closing file..."));

  close();

  slotStatusMsg(i18n("Ready."));
}

void KDrawChemApp::slotFilePrint()
{
  slotStatusMsg(i18n("Printing..."));

  view->Print();

  slotStatusMsg(i18n("Ready."));
}

void KDrawChemApp::slotFileQuit()
{
  slotStatusMsg(i18n("Exiting..."));
  saveOptions();
  // close the first window, the list makes the next one the first again.
  // This ensures that queryClose() is called on each window to ask for closing
  KMainWindow* w;
  if(memberList)
  {
    for(w=memberList->first(); w!=0; w=memberList->next())
    {
      // only close the window if the closeEvent is accepted. If the user presses Cancel on the saveModified() dialog,
      // the window and the application stay open.
      if(!w->close())
	break;
    }
  }

  //m_rings->save();
}

void KDrawChemApp::slotEditCut()
{
  slotStatusMsg(i18n("Cutting selection..."));
  view->Cut();
  editPaste->setEnabled(true);
  slotStatusMsg(i18n("Ready."));
}

void KDrawChemApp::slotEditCopy()
{
  slotStatusMsg(i18n("Copying selection to clipboard..."));
  view->Copy();
  editPaste->setEnabled(true);
  slotStatusMsg(i18n("Ready."));
}

void KDrawChemApp::slotEditPaste()
{
  slotStatusMsg(i18n("Inserting clipboard contents..."));
  view->Paste();
  slotStatusMsg(i18n("Ready."));
}

void KDrawChemApp::slotRotate90()
{
  view->Rotate90();
}

void KDrawChemApp::slotRotate180()
{
  view->Rotate180();
}

void KDrawChemApp::slotRotate270()
{
  view->Rotate270();
}

void KDrawChemApp::slotFlipH() {

  view->Flip(FLIP_H);
}

void KDrawChemApp::slotFlipV() {

  view->Flip(FLIP_H);
}

void KDrawChemApp::slotViewToolBar()
{
  slotStatusMsg(i18n("Toggling toolbar..."));
  ///////////////////////////////////////////////////////////////////
  // turn Toolbar on or off
  if(!viewToolBar->isChecked())
  {
    toolBar("mainToolBar")->hide();
  }
  else
  {
    toolBar("mainToolBar")->show();
  }

  slotStatusMsg(i18n("Ready."));
}

void KDrawChemApp::slotViewStatusBar()
{
  slotStatusMsg(i18n("Toggle the statusbar..."));
  ///////////////////////////////////////////////////////////////////
  //turn Statusbar on or off
  if(!viewStatusBar->isChecked())
  {
    statusBar()->hide();
  }
  else
  {
    statusBar()->show();
  }

  slotStatusMsg(i18n("Ready."));
}


void KDrawChemApp::slotStatusMsg(QString text)
{
  ///////////////////////////////////////////////////////////////////
  // change status message permanently
  statusBar()->clear();
  statusBar()->changeItem(text, ID_STATUS_MSG);
}

/** No descriptions */
void KDrawChemApp::setMode_Select(){

  view->setMode_Select();
}

/** Connects to view->setMode_Erase */
void KDrawChemApp::setMode_Erase(){
  view->setMode_Erase();
}

/** Calls view->setMode_DrawLine */
void KDrawChemApp::setMode_DrawLine(){
  view->setMode_DrawLine();
}
/** Calls view->setMode_DrawDashLine */
void KDrawChemApp::setMode_DrawDashLine(){
  view->setMode_DrawDashLine();
}
/** Returns a pointer to the current view */
KDrawChemView * KDrawChemApp::getView(){

    return view;
}
/** Enables Save and Save as actions */
void KDrawChemApp::slotModified(){
  fileSave->setEnabled(true);
  fileSaveAs->setEnabled(true);
  filePrint->setEnabled(true);
  editCut->setEnabled(true);
  editCopy->setEnabled(true);

  KURL tmpurl=doc->URL();

  setCaption(tmpurl.fileName(),doc->isModified());
  //editPaste->setEnabled(true);
}
/** Calls view->setMode_DrawLineUp */
void KDrawChemApp::setMode_DrawLineUp(){
  view->setMode_DrawUpLine();
}
/** Calls view->setMode_DrawDownLine */
void KDrawChemApp::setMode_DrawLineDown(){
  view->setMode_DrawDownLine();
}
/** Calls view->setMode_DrawText */
void KDrawChemApp::setMode_DrawText(){
  view->setMode_DrawText();
}
/** Calls view->setMode_DrawArrow(btype) */
void KDrawChemApp::setMode_DrawArrow(QString btype){
  //view->setMode_DrawArrow(btype);
  std::cerr << btype.latin1() << std::endl;
}
/** Calls view->setMode_DrawArrow("REGULAR") */
void KDrawChemApp::setMode_DrawArrow(){
  view->setMode_DrawArrow("REGULAR");
  currentArrow=0;
  arrowMenu->setIconSet(QIconSet(QPixmap(arrow_regular_xpm)));
}
/** No descriptions */
void KDrawChemApp::setMode_DrawArrowDash(){
  view->setMode_DrawArrow("DASH");
  currentArrow=1;
  arrowMenu->setIconSet(QIconSet(QPixmap(arrow_dash_xpm)));
}
/** No descriptions */
void KDrawChemApp::setMode_DrawArrowBI1(){
  view->setMode_DrawArrow("BI1");
  currentArrow=2;
  arrowMenu->setIconSet(QIconSet(QPixmap(arrow_bi1_xpm)));
}
/** No descriptions */
void KDrawChemApp::setMode_DrawArrowBI2(){
  view->setMode_DrawArrow("BI2");
  currentArrow=3;
  arrowMenu->setIconSet(QIconSet(QPixmap(arrow_bi2_xpm)));
}
/** No descriptions */
void KDrawChemApp::setMode_DrawArrowRetro(){
  view->setMode_DrawArrow("RETRO");
  currentArrow=4;
  arrowMenu->setIconSet(QIconSet(QPixmap(arrow_retro_xpm)));
}
/** When this is clicked, last arrow menu item used is used. */
void KDrawChemApp::slotArrowMenuClicked(){

  std::cerr << "in slotArrowMenuClicked()" << std::endl;
  switch(currentArrow) {

    case 0:
      return setMode_DrawArrow();
    case 1:
      return setMode_DrawArrowDash();
    case 2:
      return setMode_DrawArrowBI1();
    case 3:
      return setMode_DrawArrowBI2();
    case 4:
      return setMode_DrawArrowRetro();
  }
}
/** No descriptions */
void KDrawChemApp::setMode_Curve90CW(){
  currentCurveArrow=0;
  curveArrowMenu->setIconSet(QIconSet(QPixmap(cw90_xpm)));
  view->setMode_DrawCurveArrow("CW90");
}

void KDrawChemApp::setMode_Curve90CCW(){
  currentCurveArrow=1;
  curveArrowMenu->setIconSet(QIconSet(QPixmap(ccw90_xpm)));
  view->setMode_DrawCurveArrow("CCW90");
}

void KDrawChemApp::setMode_Curve180CW(){
  currentCurveArrow=2;
  curveArrowMenu->setIconSet(QIconSet(QPixmap(cw180_xpm)));
  view->setMode_DrawCurveArrow("CW180");
}

void KDrawChemApp::setMode_Curve180CCW(){
  currentCurveArrow=3;
  curveArrowMenu->setIconSet(QIconSet(QPixmap(ccw180_xpm)));
  view->setMode_DrawCurveArrow("CCW180");
}

void KDrawChemApp::setMode_Curve270CW(){
  currentCurveArrow=4;
  curveArrowMenu->setIconSet(QIconSet(QPixmap(cw270_xpm)));
  view->setMode_DrawCurveArrow("CW270");
}

void KDrawChemApp::setMode_Curve270CCW(){
  currentCurveArrow=5;
  curveArrowMenu->setIconSet(QIconSet(QPixmap(ccw270_xpm)));
  view->setMode_DrawCurveArrow("CCW270");
}

void KDrawChemApp::slotCurveArrowMenuClicked() {

  switch(currentCurveArrow) {

    case 0:
      return setMode_Curve90CW();
    case 1:
      return setMode_Curve90CCW();
    case 2:
      return setMode_Curve180CW();
    case 3:
      return setMode_Curve180CCW();
    case 4:
      return setMode_Curve270CW();
    case 5:
      return setMode_Curve270CCW();
  }
}

/** Calls view->Undo() */
void KDrawChemApp::slotEditUndo(){
  view->Undo();
}
/** Calls view->CalcMW() */
void KDrawChemApp::calcMW(){

  std::cerr << "in KDrawChemApp::calcMW()" << std::endl;
  view->Tool(MODE_TOOL_CALCMW);
}
/** No descriptions */
void KDrawChemApp::calcEmpiricalFormula(){
  view->Tool(MODE_TOOL_CALCEF);
}
/** No descriptions */
void KDrawChemApp::calcElementalAnalysis(){
  view->Tool(MODE_TOOL_CALCEA);
}
/** No descriptions */
void KDrawChemApp::calcMolecularInfo(){
  view->Tool(MODE_TOOL_MOLECULE_INFO);
}
void KDrawChemApp::calcIR() {
  view->Tool(MODE_TOOL_IR);
}

/** No descriptions */
void KDrawChemApp::calc13CNMR(){
  view->Tool(MODE_TOOL_13CNMR);
}

void KDrawChemApp::calc1HNMR() {
  view->Tool(MODE_TOOL_1HNMR);
}
/** No descriptions */
void KDrawChemApp::cleanupStruct(){
  view->Tool(MODE_TOOL_CLEANUPMOL);
}
/** No descriptions */
void KDrawChemApp::slotSelectAll(){
  view->SelectAll();
}

void KDrawChemApp::slotDelete() {

  view->EraseSelected();
}
/** setMode_DrawBracket* calls view->setModeDrawBracket(QString) */
void KDrawChemApp::setMode_DrawBracketSquare(){
  bracketMenu->setIconSet(QIconSet(QPixmap(squarebracket_xpm)));
  view->setMode_DrawBracket("SQUARE");
  currentBracket=0;
}
/** No descriptions */
void KDrawChemApp::setMode_DrawBracketCurve(){
  bracketMenu->setIconSet(QIconSet(QPixmap(curvebracket_xpm)));
  view->setMode_DrawBracket("CURVE");
  currentBracket=1;
}
/** No descriptions */
void KDrawChemApp::setMode_DrawBracketBrace(){
  bracketMenu->setIconSet(QIconSet(QPixmap(bracebracket_xpm)));
  view->setMode_DrawBracket("BRACE");
  currentBracket=3;
}
/** No descriptions */
void KDrawChemApp::slotBracketMenuClicked(){

  switch(currentBracket) {

    case 0:
      currentBracket=0;
      return setMode_DrawBracketSquare();
    case 1:
      currentBracket=1;
      return setMode_DrawBracketCurve();
    case 2:
      currentBracket=2;
      return setMode_DrawBracketBrace();
  }
}

/** No descriptions */
void KDrawChemApp::setMode_DrawSymbolPOrbital() {
    symbolMenu->setIconSet(QIconSet(QPixmap(p_orbital_xpm)));
    currentSymbol=7;
    view->setMode_DrawSymbol("p_orbital");
}
/** No descriptions */
void KDrawChemApp::setMode_DrawSymbolPlus(){
  symbolMenu->setIconSet(QIconSet(QPixmap(sym_plus_xpm)));
  currentSymbol=0;
  view->setMode_DrawSymbol("sym_plus");
}
/** No descriptions */
void KDrawChemApp::setMode_DrawSymbolMinus(){
  symbolMenu->setIconSet(QIconSet(QPixmap(sym_minus_xpm)));
  currentSymbol=1;
  view->setMode_DrawSymbol("sym_minus");
}
/** No descriptions */
void KDrawChemApp::setMode_DrawSymbolDeltaPlus(){
  symbolMenu->setIconSet(QIconSet(QPixmap(sym_delta_plus_xpm)));
  currentSymbol=2;
  view->setMode_DrawSymbol("sym_delta_plus");
}
/** No descriptions */
void KDrawChemApp::setMode_DrawSymbolDeltaMinus(){
  symbolMenu->setIconSet(QIconSet(QPixmap(sym_delta_minus_xpm)));
  currentSymbol=3;
  view->setMode_DrawSymbol("sym_delta_minus");
}
/** No descriptions */
void KDrawChemApp::setMode_DrawSymbol1e(){

  symbolMenu->setIconSet(QIconSet(QPixmap(sym_1e_xpm)));
  currentSymbol=4;
  view->setMode_DrawSymbol("sym_1e");
}
/** No descriptions */
void KDrawChemApp::setMode_DrawSymbol2e(){

  symbolMenu->setIconSet(QIconSet(QPixmap(sym_2e_xpm)));
  currentSymbol=5;
  view->setMode_DrawSymbol("sym_2e");
}
/** No descriptions */
void KDrawChemApp::setMode_DrawSymbolRingUp(){

  symbolMenu->setIconSet(QIconSet(QPixmap(sym_ring_up_xpm)));
  currentSymbol=6;
  view->setMode_DrawSymbol("sym_ring_up");
}
/** No descriptions */
void KDrawChemApp::slotSymbolMenuClicked(){

  switch(currentSymbol) {

    case 0:
      setMode_DrawSymbolPlus();
      return;
    case 1:
      setMode_DrawSymbolMinus();
      return;
    case 2:
      setMode_DrawSymbolDeltaPlus();
      return;
    case 3:
      setMode_DrawSymbolDeltaMinus();
      return;
    case 4:
      setMode_DrawSymbol1e();
      return;
    case 5:
      setMode_DrawSymbol2e();
      return;
    case 6:
      setMode_DrawSymbolRingUp();
      return;
  case 7:
      setMode_DrawSymbolPOrbital();
  }
}


/** No descriptions */
void KDrawChemApp::setFontFamily(const QString& text){
  //dunno how this works
  QFont temp=view->GetFont();
  temp.setFamily(text);
  view->SetFont(QFont(text));
}
/** No descriptions */
void KDrawChemApp::setFontSize(int size){
  QFont temp=view->GetFont();
  temp.setPointSize(size);
  view->SetFont(temp);
}
/** No descriptions */
void KDrawChemApp::toggleFontBold(){
  bool flag=setBold->isChecked();
  view->Bold(flag); //if checked, then it's already bold and we want to unbold
  if(flag) {
    //setItalic->setChecked(false);
    view->Italic(false);
    view->Underline(false);
    view->Superscript(false);
    view->Subscript(false);
  }
}

void KDrawChemApp::toggleFontItalic(){
  bool flag=setItalic->isChecked();
  view->Italic(flag);
  if(flag) {
    view->Bold(false);
    view->Underline(false);
    view->Superscript(false);
    view->Subscript(false);
  }
}

void KDrawChemApp::toggleFontUnderline(){
  bool flag=setUnderline->isChecked();
  view->Underline(flag);
  if(flag) {
    view->Bold(false);
    view->Italic(false);
    view->Superscript(false);
    view->Subscript(false);
  }
}

void KDrawChemApp::toggleFontSuperscript(){
  bool flag=setSuperscript->isChecked();
  view->Superscript(flag);
  if(flag) {
    view->Bold(false);
    view->Italic(false);
    view->Underline(false);
    view->Subscript(false);
  }
}

void KDrawChemApp::toggleFontSubscript(){
  bool flag=setSubscript->isChecked();
  view->Subscript(flag);
  if(flag) {
    view->Bold(false);
    view->Italic(false);
    view->Underline(false);
    view->Superscript(false);
  }
}

/** No descriptions */
void KDrawChemApp::slotHasBold(bool flag){

  //cerr << "in slotHasBold with " << flag << endl;
  setBold->setChecked(flag);
}

void KDrawChemApp::slotHasItalic(bool flag){

  //cerr << "in slotHasBold with " << flag << endl;
  setItalic->setChecked(flag);
}

void KDrawChemApp::slotHasUnderline(bool flag){

  //cerr << "in slotHasBold with " << flag << endl;
  setUnderline->setChecked(flag);
}

void KDrawChemApp::slotHasSuperscript(bool flag){

  //cerr << "in slotHasBold with " << flag << endl;
  setSuperscript->setChecked(flag);
}

void KDrawChemApp::slotHasSubscript(bool flag){

  //cerr << "in slotHasBold with " << flag << endl;
  setSubscript->setChecked(flag);
}

void KDrawChemApp::slotFont(QString font) {

  fontAction->setFont(font);
}

void KDrawChemApp::slotFontSize(int size) {

  fontSize->setFontSize(size);
}

void KDrawChemApp::toggleFixedBondLength() {

  if(fixedBond->isChecked()) {
    preferences.setBond_fixed(!false);  //when function is called, this has already changed
    fixedBond->setChecked(!false);
  }
  else {

    preferences.setBond_fixed(!true);
    fixedBond->setChecked(!true);
  }

}

void KDrawChemApp::toggleFixedArrowLength() {

  if(fixedArrow->isChecked()) {
    preferences.setArrow_fixed(!false);  //when function is called, this is already changed
    fixedArrow->setChecked(!false);
  }
  else {

    preferences.setArrow_fixed(!true);
    fixedArrow->setChecked(!true);
  }

}
//end of file

/** No descriptions */
void KDrawChemApp::slotSavePicture(){

  QString filters("*.png|Portable Network Graphic (*.png)\n*.bmp|Windows Bitmap (*.bmp)\n*.eps|Encapsulated PostScript (*.eps)\n*.svg|Scalable Vector Graphics (*.svg)");

  KFileDialog fd(QDir::currentDirPath(),filters,this,"picture_dia",false);
  if(!fd.exec())
    return;
  KURL url=fd.selectedURL();
  QString filename=url.fileName();

  QString ext=fd.currentFilter();

  ext.remove(0,1);

  if(filename.find(ext)==-1)
    filename+=ext;

  //cerr << filename << endl;
  bool flag;
  if(ext==".eps")
    flag=view->SaveEPS(filename);
  else if(ext==".svg")
    flag=view->SaveSVG(filename);
  else {

    QPixmap pic=view->MakePixmap();
    if(ext==".bmp")
      flag=pic.save(filename,"BMP");
    else if(ext==".png")
      flag=pic.save(filename,"PNG");
    else; //error message
  }

  if(flag)
    slotStatusMsg(i18n("Saved picture ")+filename);
  else {

    KMessageBox::error(this,i18n("Error saving file"));

  }

}

void KDrawChemApp::slotSugar(int x) {

  std::cerr << "Inside slotSugar with " << x << std::endl;
  QString fname;
  switch(x) {

    case 0:
      std::cerr << "Inside slotSugar" << std::endl;
      fname="ribose.cml";
      break;
    case 1:
      fname="deoxyribose.cml";
      break;
    case 2:
      fname="d-fructose.cml";
      break;
    case 3:
      fname="d-glucose.cml";
      break;
  }

  view->setMode_DrawRing(RingDir+fname, fname.left(fname.length() - 4), 3);


}

void KDrawChemApp::slotNucleic(int x) {

   QString fname;
   switch(x) {

    case 0:
      fname="adenine.cml";
      break;

    case 1:
      fname="cytosine.cml";
      break;

    case 2:
      fname="guanine.cml";
      break;

    case 3:
      fname="thymine.cml";
      break;

    case 4:
      fname="uracil.cml";
      break;

  }

  view->setMode_DrawRing(RingDir+fname, fname.left(fname.length() - 4), 3);

}

void KDrawChemApp::slotAmino(int x) {

	QString fname;
	switch(x) {

		case 0:
			fname="alanine.cml";
			break;
		case 1:
			fname="arginine.cml";
			break;
		case 2:
			fname="asparagine.cml";
			break;
		case 3:
			fname="aspartic_acid.cml";
			break;
		case 4:
			fname="cysteine.cml";
			break;
		case 5:
			fname="glutamine.cml";
			break;
		case 6:
			fname="glutamic_acid.cml";
			break;
		case 7:
			fname="glycine.cml";
			break;
		case 8:
			fname="histidine.cml";
			break;
		case 9:
			fname="isoleucine.cml";
			break;
		case 10:
			fname="leucine.cml";
			break;
		case 11:
			fname="lysine.cml";
			break;
		case 12:
			fname="methionine.cml";
			break;
		case 13:
			fname="phenylalanine.cml";
			break;
		case 14:
			fname="proline.cml";
			break;
		case 15:
			fname="serine.cml";
			break;
		case 16:
			fname="threonine.cml";
			break;
		case 17:
			fname="tryptophan.cml";
			break;
		case 18:
			fname="tyrosine.cml";
			break;
		case 19:
			fname="valine.cml";
			break;
	}

	view->setMode_DrawRing(RingDir + fname, fname.left(fname.length() - 4), 3);


}

void KDrawChemApp::slotRing(int x) {

  QString fname;
  switch(x) {

    case 0:
      fname="cyclopropane.cml";
      break;
    case 1:
      fname="cyclobutane.cml";
      break;
    case 2:
      fname="cyclopentane.cml";
      break;
    case 3:
      fname="cyclohexane.cml";
      break;
    case 4:
      fname="6ring_chair.cml";
      break;
    case 5:
      fname="6ring_boat.cml";
      break;
    case 6:
      fname="cycloheptane.cml";
      break;
    case 7:
      fname="cyclooctane.cml";
      break;
    case 8:
      fname="cyclopentadiene.cml";
      break;
    case 9:
      fname="benzene.cml";
      break;
    case 10:
      fname="naphthalene.cml";
      break;
    case 11:
      fname="anthracene.cml";
      break;
    case 12:
      fname="biphenyl.cml";
      break;
    case 13:
      fname="imidazole.cml";
      break;
    case 14:
      fname="indole.cml";
      break;
    case 15:
      fname="purine.cml";
      break;
    case 16:
      fname="pyrimidine.cml";
      break;
    case 17:
      fname="steroid.cml";
      break;
  }

  //if (fname == "cyclopropane.cml") {
    view->setMode_DrawRing(RingDir + fname, fname.left(fname.length() - 4), 1);
    return;
  //}

}

void KDrawChemApp::slotGroup(int x) {

	QString fname;
	switch(x) {

		case 0:
			fname="fmoc.cml";
			break;
		case 1:
			fname="boc.cml";
			break;
		case 2:
			fname="dabcyl.cml";
			break;
		case 3:
			fname="dabsyl.cml";
			break;
		case 4:
			fname="dansyl.cml";
			break;
		case 5:
			fname="edans.cml";
			break;

	}

	view->setMode_DrawRing(RingDir + fname, fname.left(fname.length() - 4), 2);

}

void KDrawChemApp::slotCustom(int x) {

  if(x==0) {

    /*CustomRingDialog crdia(this);
    crdia.exec(); */
    view->Tool(MODE_TOOL_CUSTOMRING);

    return;

  }

  QString fname;

  if(m_rings->size()==0)
    return;

  fname=m_rings->getRingFile(x-1);

  KStandardDirs* dir=KGlobal::dirs();
  QString dname=dir->localkdedir();

  dname+="share/apps/kdrawchem/rings/";

  view->setMode_DrawRing(dname+fname,m_rings->getTitle(x-1),2);

}

void KDrawChemApp::slotNewRingsSize(int x) {

  customMenu->insertItem(m_rings->getTitle(x-1),x);
}
// inserts a molecule created from a SMILES string
// for example the string c1ccccc1 is the SMILES string for the benzene
// molecule
void KDrawChemApp::inputSMILES()
{
	QString sm = QInputDialog::getText( kapp->caption(), i18n( "Enter SMILES string:" ) );
	if ( sm.isNull() || sm.isEmpty() )
		return;
	doc->fromSMILES( sm );
	view->Inserted();
	view->repaint( false );
}

void KDrawChemApp::outputSMILES()
{
	view->Tool( MODE_TOOL_TOSMILES );
}

void KDrawChemApp::slotShowFixedDialog()
{
      double na, nl;
  FixedDialog i(this, QString("fixed_dialog"));
  i.setAngle_arrow( preferences.getArrow_fixedangle() );
  i.setLength_arrow( preferences.getArrow_fixedlength() );
  i.setAngle_bond( preferences.getBond_fixedangle() );
  i.setLength_bond( preferences.getBond_fixedlength() );
  i.setDoubleBondOffset( preferences.getDoubleBondOffset() );
  if ( !i.exec() ) return;
  na = i.getAngle_arrow();
  nl = i.getLength_arrow();
  if (na < 0.0) return;
  if (nl < 0.0) return;
  preferences.setArrow_fixedangle(na);
  preferences.setArrow_fixedlength(nl);
  na = i.getAngle_bond();
  nl = i.getLength_bond();
  if (na < 0.0) return;
  if (nl < 0.0) return;
  preferences.setBond_fixedangle(na);
  preferences.setBond_fixedlength(nl);
  na = i.getDoubleBondOffset();
  if (na < 0.0) return;
  preferences.setDoubleBondOffset(na);
}

void KDrawChemApp::slotSetBackgroundColor()
{
 QColor nc1;
 KColorDialog::getColor(nc1, view->getBGColor());
  if (nc1.isValid()) {
    view->setBGColor(nc1);
    view->repaint(false);
  }
}
//eof

