/***********************************************************************
**
**   wpeditdialog.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: wpeditdialog.cpp,v 1.6 2004/06/18 16:55:53 axel Exp $
**
***********************************************************************/

#include "wpeditdialog.h"
#include "wpeditdialogpagegeneral.h"
#include "wpeditdialogpageaero.h"
#include "mapcontents.h"

WPEditDialog::WPEditDialog(QWidget *parent, const char *name, wayPoint * wp ):
QTabDialog(parent,name, false, Qt::WStyle_StaysOnTop)
{
  setOkButton(QString::null);
  this->showMaximized();
  if (wp==0) {
    this->setCaption(tr("New Waypoint"));
  } else {
    this->setCaption(tr("Edit Waypoint"));
  }
  _wp=wp;  

  WPEditDialogPageGeneral * pageGeneral=new WPEditDialogPageGeneral(this);
  addTab(pageGeneral, tr("&General"));

  WPEditDialogPageAero * pageAero=new WPEditDialogPageAero(this);
  addTab(pageAero, tr("&Aero"));

  comment=new QMultiLineEdit(this);
  comment->setWordWrap(QMultiLineEdit::WidgetWidth);
  addTab(comment, tr("&Comments"));
  
  connect(this, SIGNAL(aboutToShow()), this, SLOT(slot_LoadCurrent()));
  connect(this, SIGNAL(load(wayPoint *)), pageGeneral, SLOT(slot_load(wayPoint *)));
  connect(this, SIGNAL(load(wayPoint *)), pageAero, SLOT(slot_load(wayPoint *)));
  connect(this, SIGNAL(save(wayPoint *)), pageGeneral, SLOT(slot_save(wayPoint *)));
  connect(this, SIGNAL(save(wayPoint *)), pageAero, SLOT(slot_save(wayPoint *)));

  showPage(comment);
  showPage(pageGeneral);

}

WPEditDialog::~WPEditDialog(){
}

/** This slot is called if the user presses the OK button. */
void WPEditDialog::slot_LoadCurrent(){
  emit load(_wp);
  if (_wp!=0) {
    comment->setText(_wp->comment);
  }
}

extern MapContents* _globalMapContents;
/** Called if OK button is pressed? */
void WPEditDialog::accept(){
  if (_wp==0) {
    _wp=new wayPoint;
    _wp->comment=comment->text();
    emit save(_wp);
    qDebug("new waypoint %s", (const char *)_wp->name);
    _globalMapContents->getWaypointList()->append(_wp);
  } else {
    qDebug("change waypoint %s", (const char *)_wp->name);
    _wp->comment=comment->text();
    emit save(_wp);
  }    


  QTabDialog::accept();
  emit wpListChanged(_wp);
}
