/***********************************************************************
**
**   tasklistview
**
**   This file is part of Cumulus
**
************************************************************************
**
**   Copyright (c):  2004 by André Somers
**
**   This file is distributed under the terms of the General Public
**   Licence. See the file COPYING for more information.
**
**   $Id: tasklistview.cpp,v 1.5 2004/06/17 21:53:39 eckhard Exp $
**
***********************************************************************/

#include "cumulusapp.h"
#include "tasklistview.h"
#include "flighttask.h"
#include "distance.h"

#include <qlist.h>
#include <qfont.h>

TaskListView::TaskListView(QWidget *parent, const char *name)
 : QWidget(parent, name)
{
    _task=0;
    _selected=0;
    resize(parent->size());
    QFont fnt( "Helvetica", 12, QFont::Bold  );
    this->setFont(fnt);

    QBoxLayout *topLayout = new QVBoxLayout( this );

    list= new QListView(this, "taskpointlist");
    list->addColumn(tr("Type"),60);
    list->addColumn(tr("Name"),110);
    list->addColumn(tr("Dist"),65);

    list->setAllColumnsShowFocus(true);
    list->setSelectionMode( QListView::Single );
    list->setSorting(-1,false);

    topLayout->addWidget(list,10);
    QBoxLayout *buttonrow=new QHBoxLayout(topLayout);

    /** @ee add a close button */
    QPushButton *cmdClose = new QPushButton(tr("Close"), this);
    QFont font = cmdClose->font();
    font.setPixelSize(14);
    cmdClose->setFont(font);
    buttonrow->addWidget(cmdClose);

    QPushButton *cmdInfo = new QPushButton(tr("Info"), this);
    cmdInfo->setFont(font);
    buttonrow->addWidget(cmdInfo);

    QPushButton *cmdSelect = new QPushButton(tr("Select"), this);
    cmdSelect->setFont(font);
    buttonrow->addWidget(cmdSelect);

    connect(cmdSelect, SIGNAL(clicked()), this, SLOT(slot_Select()));
    connect(cmdInfo, SIGNAL(clicked()), this, SLOT(slot_Info()));
    connect(cmdClose, SIGNAL(clicked()), this, SLOT(slot_Close()));
    connect(list, SIGNAL(selectionChanged(QListViewItem*)), this, SLOT(slot_Selected(QListViewItem*)));
}

TaskListView::~TaskListView()
{
}


void TaskListView::slot_Selected(QListViewItem * itm)
{
  _selected=((_TaskPoint*)itm)->wp;
  qDebug("Waypoint elevation: %d",_selected->elevation);
}


void TaskListView::showEvent(QShowEvent *){
    list->setFocus();
}

/** This signal is called to indicate that a selection has been made. */
void TaskListView::slot_Select()
{
    emit newWaypoint(getSelectedWaypoint());
    emit done();
}

/** This slot is called if the info button has been clicked */
void TaskListView::slot_Info(){
    if (getSelectedWaypoint()!=0) emit info(getSelectedWaypoint());
}

/** @ee This slot is called if the listview is closed without selecting */
void TaskListView::slot_Close ()
{
    emit done();
}

/** 
 * Retreives the waypoints from the task, and fills the list. 
 */
void TaskListView::slot_setTask(const FlightTask * tsk){
{
  if(_task!=0)
  {
    delete _task;
  }
  list->clear();
  if (tsk==0)
  {
    _task=0;
    return;    
  }
  else
  {
    _task=new FlightTask(*tsk);
  }

  QList<wayPoint> tmpList = _task->getWPList();

  list->clear();

  QString typeName, distance, idString;

  for(unsigned int loop = tmpList.count(); loop > 0; loop--)
    {
    qDebug("Getting item %d",loop-1);
      wayPoint* wp = tmpList.at( loop-1 );

/*      distance=Distance::getText(wp->distance*1000,true,2);
      //distance.sprintf( "%.2f km", wp->distance );
      //idString.sprintf( "%d", loop );

      QListViewItem *newLvi = new QListViewItem( list,
                typeName, wp->name, distance );
*/
      new _TaskPoint( list, wp );
      /*/ reselect last selected item
      if( lastSelectedItem == (int) loop )
	{
	  taskList->setSelected( newLvi, true );
	  lastSelectedItem = -1;
	}*/
    }
  }
}
 

/** Returns a pointer to the currently highlighted reachpoint. */
wayPoint * TaskListView::getSelectedWaypoint()
{
    return _selected;
}

TaskListView::_TaskPoint::_TaskPoint(QListView * lv, wayPoint * point):QListViewItem(lv)
{
  wp=point;
  QString typeName;
  switch( wp->type )
    {
      case FlightTask::TakeOff:
        typeName = tr("Take-off");
        break;
      case FlightTask::Begin:
        typeName = tr("Begin");
        break;
      case FlightTask::RouteP:
        typeName = tr("Route");
        break;
      case FlightTask::End:
        typeName = tr("End");
        break;
      case FlightTask::FreeP:
        typeName = tr("Free");
        break;
      case FlightTask::Landing:
        typeName = tr("Landing");
        break;
      case FlightTask::NotSet:
      default:
        typeName = tr("not set");
        break;
    }
  setText(0, typeName);
  setText(1, wp->name);
  setText(2, Distance::getText(wp->distance*1000,true,2));
}
