/*
 * ImageListView.cpp
 *
 * (c) 2002 by Thorsten Scheuermann <tscheuermann@gmx.net>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 */

#include "ImageListView.h"
#include "ImageInfo.h"
#include "ImageSet.h"
#include <qstring.h>
#include <qlist.h>

ImageListViewItem::ImageListViewItem(QListView *parent, ImageInfo *iInfo)
: QListViewItem(parent)
{
  QString sizeString;

  info = iInfo;
  sizeString = sizeString.sprintf("%dx%d", info->imageWidth(), info->imageHeight());
  setText(0, info->fileName());
  setText(1, sizeString);
}

ImageListViewItem::~ImageListViewItem()
{
}

ImageInfo *ImageListViewItem::imageInfo()
{
  return info;
}

ImageListView::ImageListView(QWidget *parent, const char *name)
: QListView(parent, name)
{
  imgSet = NULL;

  addColumn("Name");
  addColumn("Size");
  setSorting(-1); // no sorting

  connect(this, SIGNAL(clicked(QListViewItem*)),
          this, SLOT(itemClicked(QListViewItem*)));
  connect(this, SIGNAL(returnPressed(QListViewItem*)),
          this, SLOT(itemClicked(QListViewItem*)));
}

ImageListView::~ImageListView()
{
}


void ImageListView::setImageSet(ImageSet *set)
{
  imgSet = set;
  updateList();
}

void ImageListView::updateList()
{
  clear();

  if(!imgSet)
    return;

  QListIterator<ImageInfo> iit(imgSet->children());

  for( iit.toLast(); iit.current(); --iit)
  {
    new ImageListViewItem(this, *iit);
  }
}

void ImageListView::itemClicked(QListViewItem *item)
{
  if(!item)
    return;

  emit fileSelected(((ImageListViewItem *)item)->imageInfo());
}

void ImageListView::keyPressEvent(QKeyEvent *ke)
{
  if(ke->key() == Key_Space)
    emit returnPressed(currentItem());
  else
    QListView::keyPressEvent(ke);
}
