/*
 * SettingsDialog.cpp
 *
 * (c) 2002 by Thorsten Scheuermann <tscheuermann@gmx.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 "SettingsDialog.h"
#include "ViewerSettings.h"
#include "Bookmark.h"
#include <qcheckbox.h>
#include <qbuttongroup.h>
#include <qlistbox.h>
#include <qlineedit.h>
#include <qcombobox.h>
#include <qpushbutton.h>

SettingsDialog::SettingsDialog(QWidget *parent = 0, const char *name = 0)
: SettingsDialogBase(parent, name, true)
{
  currBm = NULL;
  bmList.setAutoDelete(true);
}

SettingsDialog::~SettingsDialog()
{
}

void SettingsDialog::initialize(ViewerSettings *s)
{
  Bookmark *bm;

  rotationCheck->setChecked(s->allowRotation());
  switch(s->rotation())
  {
    case ROTATION_90:
      rotationGroup->setButton(0);
      break;

    case ROTATION_270:
      rotationGroup->setButton(1);
      break;

    default:
      rotationGroup->setButton(0);
      break;
  }

  bmList.clear();
  QListIterator<Bookmark> bmIt(s->bookmarks());
  for( ; bmIt.current(); ++bmIt)
  {
    bm = new Bookmark(**bmIt);
    bmList.append(bm);
    bmListBox->insertItem(bm->name());
  }
  currBm = bmList.first();
  if(currBm)
  {
    bmEditFrame->setEnabled(true);
    deleteBmButton->setEnabled(true);
  }
  bmListBox->setCurrentItem(0);
}

void SettingsDialog::getSettings(ViewerSettings *s)
{
  s->setAllowRotation(rotationCheck->isChecked());
  switch(rotationGroup->id(rotationGroup->selected()))
  {
    case 0:
      s->setRotation(ROTATION_90);
      break;

    case 1:
      s->setRotation(ROTATION_270);
      break;

    default:
      break;
  }

  s->bookmarks().clear();
  QListIterator<Bookmark> bmIt(bmList);
  for( ; bmIt.current(); ++bmIt)
  {
    s->bookmarks().append(*bmIt);
  }
  bmList.setAutoDelete(false);  // don't delete the bookmark objects now
                                // that they are in the settings
}

int SettingsDialog::exec()
{
  showMaximized();
  return SettingsDialogBase::exec();
}

void SettingsDialog::newBookmark()
{
  currBm = new Bookmark;

  bmEditFrame->setEnabled(true);
  deleteBmButton->setEnabled(true);
  currBm->setName("New Bookmark");
  currBm->setDirectory("");
  bmList.append(currBm);
  bmListBox->insertItem(currBm->name());
  bmListBox->setCurrentItem(bmList.at());
  bmNameEdit->setFocus();
  //bmListBox->setSelected(bmList.at(), true);
}

void SettingsDialog::deleteBookmark()
{
  int i = bmListBox->currentItem();

  if(i == -1)
    return;

  bmList.remove(i);
  currBm = bmList.current();
  bmListBox->removeItem(i);
  bmListBox->setCurrentItem(bmList.at());

  if(!currBm)
  {
    deleteBmButton->setEnabled(false);
    bmEditFrame->setEnabled(false);
    bmNameEdit->setText("");
    bmDirEdit->setText("");
  }
}

void SettingsDialog::showBookmark(int i)
{
  currBm = bmList.at(i);

  if(!currBm)
    return;

  bmNameEdit->setText(currBm->name());
  bmDirEdit->setText(currBm->directory());
  bmStorageCombo->setCurrentItem((int)currBm->storage());
}

void SettingsDialog::bmNameChanged(const QString& name)
{
  if(!currBm)
    return;

  currBm->setName(name);

  // prevent infinite recursion (listbox signals highlighted(int) on
  // changeItem(...)
  disconnect(bmListBox, SIGNAL(highlighted(int)),
             this, SLOT(showBookmark(int)));
  bmListBox->changeItem(name, bmList.at());
  connect(bmListBox, SIGNAL(highlighted(int)),
          this, SLOT(showBookmark(int)));
}

void SettingsDialog::bmDirChanged(const QString& dir)
{
  if(!currBm)
    return;

  currBm->setDirectory(dir);
}

void SettingsDialog::bmStorageChanged(int s)
{
  if(!currBm)
    return;

  currBm->setStorage((Bookmark::StorageType)s);
}
