summaryrefslogtreecommitdiffstats
path: root/kimagemapeditor/mapslistview.cpp
blob: d3a9ebe90d9215f6035afab67c17e09a096115a2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/***************************************************************************
                          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 <klistview.h>
#include <klocale.h>
#include <kdebug.h>

// locale
#include "mapslistview.h"


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

    connect( _listView, TQT_SIGNAL( selectionChanged(TQListViewItem*)),
             this, TQT_SLOT( slotSelectionChanged(TQListViewItem*)));

    connect( _listView, TQT_SIGNAL( itemRenamed(TQListViewItem*)),
             this, TQT_SLOT( slotItemRenamed(TQListViewItem*)));
}


MapsListView::~MapsListView() {
}

void MapsListView::addMap(const TQString & name = TQString::null) {
    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"