/***********************************************************************
**
**   configdialog.cpp
**
**   This file is part of Cumulus.
**
************************************************************************
**
**   Copyright (c):  2002 by André Somers
**
**   This file is distributed under the terms of the General Public
**   Licence. See the file COPYING for more information.
**
**   $Id: configdialog.cpp,v 1.17 2005/02/27 22:12:19 axel Exp $
**
***********************************************************************/

#include "configdialog.h"
#include "settingspagepersonal.h"
#include "settingspagegps.h"
#include "settingspageunits.h"
#include "settingspagemap.h"
#include "settingspageairspace.h"
#include "settingspageinformation.h"
#include "gliderlist.h"

#include <qmessagebox.h> 

ConfigDialog::ConfigDialog(QWidget * parent, const char * name):
  QTabDialog(parent, name, false, Qt::WStyle_StaysOnTop), loadConfig(true)
{
  setOkButton(QString::null);
  showMaximized();
  setCaption(tr("Cumulus settings"));

  SettingsPagePersonal * spp=new SettingsPagePersonal(this);
  addTab(spp, tr("&Personal"));

  SettingsPageGliderList* spl=new SettingsPageGliderList(this);
  addTab(spl, tr("G&liders"));

  SettingsPageGPS * spg=new SettingsPageGPS(this);
  addTab(spg, tr("&GPS"));

  SettingsPageMap * spm=new SettingsPageMap(this);
  addTab(spm, tr("&Map"));

  SettingsPageAirspace * spa=new SettingsPageAirspace(this);
  addTab(spa, tr("&Airspace"));

  SettingsPageUnits * spu=new SettingsPageUnits(this);
  addTab(spu, tr("&Units"));

  SettingsPageInformation * spi=new SettingsPageInformation(this);
  addTab(spi, tr("&Information"));

  connect(this, SIGNAL(aboutToShow()), this, SLOT(slot_LoadCurrent()));
  connect(this, SIGNAL(load()), spp, SLOT(slot_load()));
  connect(this, SIGNAL(load()), spl, SLOT(slot_load()));
  connect(this, SIGNAL(load()), spg, SLOT(slot_load()));
  connect(this, SIGNAL(load()), spm, SLOT(slot_load()));
  connect(this, SIGNAL(load()), spa, SLOT(slot_load()));
  connect(this, SIGNAL(load()), spu, SLOT(slot_load()));
  connect(this, SIGNAL(load()), spi, SLOT(slot_load()));

  connect(this, SIGNAL(reload()), spu, SLOT(slot_load()));

  connect(this, SIGNAL(save()), spp, SLOT(slot_save()));
  connect(this, SIGNAL(save()), spl, SLOT(slot_save()));
  connect(this, SIGNAL(save()), spg, SLOT(slot_save()));
  connect(this, SIGNAL(save()), spm, SLOT(slot_save()));
  connect(this, SIGNAL(save()), spa, SLOT(slot_save()));
  connect(this, SIGNAL(save()), spu, SLOT(slot_save()));
  connect(this, SIGNAL(save()), spi, SLOT(slot_save()));

  connect(this, SIGNAL(query_close(bool&, QStringList& )), 
          spl, SLOT(slot_query_close(bool&, QStringList&)));
  connect(this, SIGNAL(query_close(bool&, QStringList& )), 
          spm, SLOT(slot_query_close(bool&, QStringList&)));
  
  showPage(spp);
}

ConfigDialog::~ConfigDialog()
{
  // qDebug("~ConfigDialog() is called");
}

/** This slot is called if the window will be shown or resized */
void ConfigDialog::slot_LoadCurrent(){

  // Block multiple loads to avoid reset of changed values in the
  // config tabs. This can appear, if the screen keypad is pop up and
  // pop down. In this case the signal aboutToShow() is fired.

  // qDebug("slot_LoadCurrent() in config.cpp is called");

  if( loadConfig )
    loadConfig = false;
  else
    return;

  emit load();
}

/** Called if OK button is pressed */
void ConfigDialog::accept()
{
  emit save();
  QTabDialog::accept();
  emit settingsChanged();
  delete this;
}

/** Called if the Cancel button is pressed */
void ConfigDialog::reject()
{
  bool need_warning=false;
  QStringList changed_pages;
  emit query_close(need_warning, changed_pages);
  QString pagelist;
  QString separator;
  if (need_warning) {
    for (uint i=0; i<changed_pages.count(); i++) {
        if (i==changed_pages.count()-1) {
            separator=".";
        } else {
            separator=",";
        }
        pagelist+=QString("<li>%1%2</li>").arg(changed_pages[i]).arg(separator);            
    }
    int answer=QMessageBox::warning(this, 
                             tr("Close w/o saving?"), 
                             tr("<qt>You have made changes to:<ul>%1</ul> If you continue, these changes will <b>not</b> be saved, and your changes will be lost.<br>Do you wish to close without saving anyway?</qt>").arg(pagelist),
                             tr("Close w/o saving"),
                             tr("Save all changes"),
                             QString::null,
                             0);
    if (answer == 1) { //the user pressed cancel
      accept();
      return; //abort closing dialog
    }
  }
  QTabDialog::reject();
  emit reload();
  delete this;
}
