/***********************************************************************
**
**   preflightdialog.cpp
**
**   This file is part of Cumulus
**
************************************************************************
**
**   Copyright (c):  2003 by André Somers
**
**   This file is distributed under the terms of the General Public
**   Licence. See the file COPYING for more information.
**
**   $Id: preflightdialog.cpp,v 1.11 2004/06/16 16:06:10 axel Exp $
**
***********************************************************************/

#include "preflightdialog.h"
#include "mapcontents.h"
#include "qlayout.h"
#include "preflightgliderpage.h"
#include "preflightmiscpage.h"
#include <qaccel.h>

PreFlightDialog::PreFlightDialog(QWidget * parent, const char * name):
QTabDialog(parent, "preflightdialog", false, Qt::WStyle_StaysOnTop)
{
  setCaption(tr("Cumulus Pre-Flight settings"));
  setOkButton(QString::null);
  gliderpage =new PreFlightGliderPage(this,"gliderpage");
  addTab(gliderpage, tr("&Glider"));

  taskpage=new TaskList(this, "taskpage");
  addTab(taskpage, tr("&Task"));

  miscpage =new PreFlightMiscPage(this,"miscpage");
  addTab(miscpage, tr("&Misc."));

  QAccel *acc = new QAccel(this);
  acc->connectItem(acc->insertItem(Key_Space), this, SLOT(accept()));
  acc->connectItem(acc->insertItem(Key_Left), this, SLOT(keyLeft()));
  acc->connectItem(acc->insertItem(Key_Right), this, SLOT(keyRight()));
  
  //check to see which tab to bring forward
  if (QString (name) == "taskselection")
  {
    showPage (taskpage);
  }
  else
  {
    showPage (gliderpage);
  }

  showMaximized();
}

PreFlightDialog::~PreFlightDialog()
{
  qDebug("PreFlightDialog::~PreFlightDialog()");
}

void PreFlightDialog::accept(){
  extern MapContents * _globalMapContents;
  _globalMapContents->setCurrentTask( taskpage->selectedTask() );
  gliderpage->save();
  miscpage->save();

  emit settingsChanged();
  QDialog::accept();
  delete this;
}

void PreFlightDialog::reject(){
  qDebug("PreFlightDialog::reject()");
  QDialog::reject();
  delete this;
}

void PreFlightDialog::keyRight()
{
  if( currentPage() == gliderpage )
    {
      showPage(taskpage);
    }
  else if( currentPage() == taskpage )
    {
      showPage(miscpage);
    }
  else if( currentPage() == miscpage )
    {
      showPage(gliderpage);
    }
}

void PreFlightDialog::keyLeft()
{      
  if(  currentPage() == miscpage )
    {
      showPage(taskpage);
    }
  else if(  currentPage() == taskpage )
    {
      showPage(gliderpage);
    }
  else if(  currentPage() == gliderpage )
    {
      showPage(miscpage);
    }
}
