/***************************************************************************
                          imageslistview.cpp  -  description
                             -------------------
    begin                : Weg Feb 26 2003
    copyright            : (C) 2003 by Jan Schäfer
    email                : janschaefer@users.sourceforge.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.                                   *
 *                                                                         *
 ***************************************************************************/

// QT
#include <tqptrlist.h>
 
 
// KDE
#include <tdelocale.h> 
#include <kdebug.h>

// locale 
#include "imageslistview.h"

ImagesListViewItem::ImagesListViewItem(ImagesListView* parent, ImageTag* tag) 
  : TQListViewItem(parent) 
{
    _imageTag = tag;
    update();
}


void ImagesListViewItem::update() {
    TQString src="";
    TQString usemap="";
    if (_imageTag->find("src"))
      src=*_imageTag->find("src");
    if (_imageTag->find("usemap"))
      usemap=*_imageTag->find("usemap");

    setText(0,src);
    setText(1,usemap); 
}

ImageTag* ImagesListViewItem::imageTag() {
  return _imageTag;
}


ImagesListView::ImagesListView(TQWidget *parent, const char *name)
  : TDEListView(parent, name)
{
  addColumn(i18n("Images"));
  addColumn(i18n("Usemap"));
  //addColumn(i18n("Preview"));
  setFullWidth(true);
  
  
  connect( this, TQ_SIGNAL( selectionChanged(TQListViewItem*)),
           this, TQ_SLOT( slotSelectionChanged(TQListViewItem*)));
}


ImagesListView::~ImagesListView()
{
}

void ImagesListView::addImage(ImageTag* tag)
{
  if (!tag) {
    kdDebug() << "ImageListView::addImage: Parameter is null !" << endl;
    return;
  }

  new ImagesListViewItem(this, tag);
}

void ImagesListView::addImages(TQPtrList<ImageTag> * images)
{
	for (ImageTag *tag = images->first(); tag!=0L; tag=images->next()) {
    addImage(tag);
	}
}

void ImagesListView::clear() {
  TQListView::clear();
}

void ImagesListView::removeImage(ImageTag* tag) {
  ImagesListViewItem *item = findListViewItem(tag);
  if (item) {
     takeItem(item);
     setSelected(currentItem(),true);
  }
  else {
    kdDebug() << "ImageListView::removeImage: ListViewItem was not found !" << endl;
  }
}

void ImagesListView::updateImage(ImageTag* tag) {
  ImagesListViewItem *item = findListViewItem(tag);
  if (item)
    item->update();
  else {
    kdDebug() << "ImageListView::updateImage: ListViewItem was not found !" << endl;
  }     
}

ImagesListViewItem* ImagesListView::findListViewItem(ImageTag* tag) {
  
  kdDebug() << "ImageListView::findListViewItem: start searching ... " << endl;

  for (TQListViewItem* item = firstChild(); item ; item = item->nextSibling()) {
     ImagesListViewItem *imageItem = static_cast<ImagesListViewItem*>(item);
     if (imageItem->imageTag() == tag) {
        kdDebug() << "ImageListView::findListViewItem: found it " << endl;
     
        return imageItem;
     }        
  } 
  
  kdDebug() << "ImageListView::findListViewItem: found nothing " << endl;
  return 0L;
  
}

void ImagesListView::slotSelectionChanged(TQListViewItem* item) {
  TQString src = item->text(0);
  
  emit imageSelected(KURL(_baseUrl,src));
}

ImageTag* ImagesListView::selectedImage() {
  ImagesListViewItem* item = static_cast<ImagesListViewItem*>(selectedItem());
  if ( ! item) {
     kdDebug() << "ImagesListView::selectedImage: No Image is selected !" << endl;
     return 0L;
  }
  
  return item->imageTag();
     
  
}

void ImagesListView::selectImage(ImageTag* tag) {
  ImagesListViewItem* item = findListViewItem(tag);
  if (item) {
     setSelected(item, true);
  }
}

#include "imageslistview.moc"

