/***************************************************************************
                          mapslistview.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.                                   *
 *                                                                         *
 ***************************************************************************/
// KDE
#include <tdelistview.h>
#include <tdelocale.h>
#include <kdebug.h>

// locale
#include "mapslistview.h"


MapsListView::MapsListView(TQWidget *parent, const char *name)
: TQVBox(parent, name) {
    _listView = new TDEListView(this);
    _listView->addColumn(i18n("Maps"));
    _listView->setFullWidth(true);
    _listView->setSelectionMode(TQListView::Single);
    _listView->setItemsRenameable(true);

    connect( _listView, TQ_SIGNAL( selectionChanged(TQListViewItem*)),
             this, TQ_SLOT( slotSelectionChanged(TQListViewItem*)));

    connect( _listView, TQ_SIGNAL( itemRenamed(TQListViewItem*)),
             this, TQ_SLOT( slotItemRenamed(TQListViewItem*)));
}


MapsListView::~MapsListView() {
}

void MapsListView::addMap(const TQString & name = TQString()) {
    new TQListViewItem(_listView,name);
    //kdDebug() << "MapsListView::addMap : Added map '" << name << "'" << endl;

}

void MapsListView::addMaps(TQPtrList<MapTag> * maps) {

    for (MapTag *tag = maps->first(); tag!=0L; tag=maps->next()) {
        addMap(tag->name);
    }
}

void MapsListView::selectMap(const TQString & name) {
    TQListViewItem* item = _listView->findItem(name,0);
    if (item) {
       selectMap(item);
    } else
       kdWarning() << "MapsListView::selectMap : Couldn't found map '" << name << "'" << endl;

}

void MapsListView::selectMap(TQListViewItem* item) {
    if (item)
        _listView->setSelected(item,true);
}


TQString MapsListView::selectedMap() {
    TQString result;

    TQListViewItem* item = _listView->selectedItem();
    if (item)
        result = item->text(0);
    else
        kdWarning() << "MapsListView::selectedMap : No map selected !" << endl;

    return result;
}

void MapsListView::removeMap(const TQString & name) {
    TQListViewItem* item = _listView->findItem(name,0);
    if (item) {
        _listView->takeItem(item);
        _listView->setSelected(_listView->currentItem(),true);
//        kdDebug() << "MapsListView::removeMap : Removed map '" << name << "'" << endl;
    } else
        kdWarning() << "MapsListView::removeMap : Couldn't found map '" << name << "'" << endl;
}

void MapsListView::clear() {
    _listView->clear();
}

void MapsListView::slotSelectionChanged(TQListViewItem* item) {
    TQString name = item->text(0);
    emit mapSelected(name);
}

void MapsListView::slotItemRenamed(TQListViewItem* item) {
    TQString name = item->text(0);
    emit mapRenamed(name);
}

void MapsListView::changeMapName(const TQString & oldName, const TQString & newName) {
//    kdDebug() << "MapsListView::changeMapName : " << oldName << " to " << newName << endl;
    TQListViewItem* item = _listView->findItem(oldName,0);
    if (item) {
        item->setText(0,newName);
//        kdDebug() << "MapsListView::changeMapName : successful" << endl;
    }
    else {
        kdWarning() << "MapsListView::changeMapName : Chouldn't find map with name '" << oldName << "'" << endl;
    }

}


bool MapsListView::nameAlreadyExists(const TQString & name) {
//    kdDebug() << "MapsListView::nameAlreadyExists : " << name << " ? " << endl;
    bool result = false;
    TQListViewItem* item = 0L;
    for(item = _listView->firstChild(); item; item = item->nextSibling()) {
        TQString otherMap = item->text(0);
        if(name == otherMap) {
            result = true;
            break;
        }
    }

//    kdDebug() << "MapsListView::nameAlreadyExists : " << name << " : " << result << endl;

    return result;
}

TQStringList MapsListView::getMaps() {
    TQStringList result;
    
    TQListViewItem* item = 0L;
    for(item = _listView->firstChild(); item; item = item->nextSibling()) {
        TQString map = item->text(0);
        result << map;
    }
    
    return result;
}

TQString MapsListView::getUnusedMapName() {
    TQString result;
    TQString attempt;
    int i=0;
    while(result.isEmpty()) {
        i++;
        attempt = i18n("unnamed");
        attempt += TQString::number(i);
        if (nameAlreadyExists(attempt))
            continue;

        result = attempt;
    }

//    kdDebug() << "MapsListView::getUnusedMapName : Found an unused name : '" << result << "'" << endl;
    return result;
}

uint MapsListView::count() {
    return _listView->childCount();
}

#include "mapslistview.moc"
