// -*- Mode: c++; tab-width: 8; c-basic-offset: 2; indent-tabs-mode: nil -*-
// NOTE: the first line of this file sets up source code indentation rules
// for Emacs; it is also a hint to anyone modifying this file.

// File         : PreferencesDialog.cpp
// Author       : Pavel Aleksandrovich Koshevoy
// Created      : Mon Sep 25 22:02:00 MDT 2006
// Copyright    : (C) 2006
// License      : GPL
// Description  : UI for the user preferences.

// local includes:
#include "PreferencesDialog.h"
#include "the_main_window.hxx"

// Qt includes:
#include <QSettings>
#include <QCheckBox>
#include <QCloseEvent>


//----------------------------------------------------------------
// prefsAllowLinearFiltering
// 
bool
prefsAllowLinearFiltering()
{
  the_main_window_t * main_window =
    (the_main_window_t *)(the_document_ui_t::doc_ui());
  return main_window->preferences().allowLinearFiltering();
}

//----------------------------------------------------------------
// prefsAllowTransparency
// 
bool
prefsAllowTransparency()
{
  the_main_window_t * main_window =
    (the_main_window_t *)(the_document_ui_t::doc_ui());
  return main_window->preferences().allowTransparency();
}

//----------------------------------------------------------------
// prefsShouldFitToImage
// 
bool
prefsShouldFitToImage()
{
  the_main_window_t * main_window =
    (the_main_window_t *)(the_document_ui_t::doc_ui());
  return main_window->preferences().shouldFitToImage();
}

//----------------------------------------------------------------
// prefsSlideShowDelay
// 
unsigned int
prefsSlideShowDelay()
{
  the_main_window_t * main_window =
    (the_main_window_t *)(the_document_ui_t::doc_ui());
  return main_window->preferences().slideShowDelay();
}


//----------------------------------------------------------------
// to_bool
// 
static inline bool to_bool(const Qt::CheckState & chst)
{ return chst == Qt::Checked; }

//----------------------------------------------------------------
// to_chst
// 
static inline Qt::CheckState to_chst(const QVariant & qvar)
{ return qvar.toBool() ? Qt::Checked : Qt::Unchecked; }

//----------------------------------------------------------------
// to_chst
// 
static inline Qt::CheckState to_chst(const bool & checked)
{ return checked ? Qt::Checked : Qt::Unchecked; }

//----------------------------------------------------------------
// to_uint
// 
static inline unsigned int to_uint(const QVariant & qvar)
{ return qvar.toUInt(); }


//----------------------------------------------------------------
// PreferencesDialog::PreferencesDialog
// 
PreferencesDialog::PreferencesDialog(QWidget * parent, Qt::WFlags f):
  QDialog(parent, f),
  Ui::PreferencesDialog()
{
  Ui::PreferencesDialog::setupUi(this);
}

//----------------------------------------------------------------
// PreferencesDialog::loadSettings
// 
void
PreferencesDialog::loadSettings()
{
  // load the preferences:
  QCoreApplication::setOrganizationName("Pavel Koshevoy");
  QCoreApplication::setOrganizationDomain("aragog.com");
  QCoreApplication::setApplicationName("bernstein");
  QSettings settings;
  
  linearFilteringCheckBox->setCheckState
    (to_chst(settings.value("allow_linear_filtering_",
			    to_bool(linearFilteringCheckBox->checkState()))));
  allow_linear_filtering_ = to_bool(linearFilteringCheckBox->checkState());
  
  transparencyCheckBox->setCheckState
    (to_chst(settings.value("allow_transparency_",
			    to_bool(transparencyCheckBox->checkState()))));
  allow_transparency_ = to_bool(transparencyCheckBox->checkState());
  
  fitWindowCheckBox->setCheckState
    (to_chst(settings.value("should_fit_to_image_",
			    to_bool(fitWindowCheckBox->checkState()))));
  should_fit_to_image_ = to_bool(fitWindowCheckBox->checkState());
  
  delaySpinBox->setValue
    (std::max(std::min(to_uint(settings.value("slide_show_delay_",
					      delaySpinBox->value())),
		       (unsigned int)(delaySpinBox->maximum())),
	      (unsigned int)(delaySpinBox->minimum())));
  slide_show_delay_ = (unsigned int)(delaySpinBox->value());
}

//----------------------------------------------------------------
// PreferencesDialog::exec
// 
int
PreferencesDialog::exec()
{
  int r = QDialog::exec();
  if (r == QDialog::Accepted)
  {
    allow_linear_filtering_ = to_bool(linearFilteringCheckBox->checkState());
    allow_transparency_ = to_bool(transparencyCheckBox->checkState());
    should_fit_to_image_ = to_bool(fitWindowCheckBox->checkState());
    slide_show_delay_ = (unsigned int)(delaySpinBox->value());
    
    QSettings settings;
    settings.setValue("allow_linear_filtering_", allow_linear_filtering_);
    settings.setValue("allow_transparency_", allow_transparency_);
    settings.setValue("should_fit_to_image_", should_fit_to_image_);
    settings.setValue("slide_show_delay_", slide_show_delay_);
  }
  
  return r;
}

//----------------------------------------------------------------
// PreferencesDialog::closeEvent
// 
void
PreferencesDialog::closeEvent(QCloseEvent * e)
{
  e->ignore();
  done(QDialog::Rejected);
}

