1/***************************************************************************
2 mapslistview.cpp - description
3 -------------------
4 begin : Weg Feb 26 2003
5 copyright : (C) 2003 by Jan SchÃ?fer
6 email : janschaefer@users.sourceforge.net
7 ***************************************************************************/
8
9/***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17#include <QListWidget>
18
19// KDE
20#include <klocale.h>
21#include <kdebug.h>
22
23// locale
24#include "mapslistview.h"
25//Added by qt3to4:
26#include <kvbox.h>
27
28
29MapsListView::MapsListView(QWidget *parent)
30: KVBox(parent) {
31 _listView = new QTreeWidget(this);
32 _listView->setColumnCount(1);
33 _listView->setHeaderLabel(i18n("Maps"));
34 _listView->setRootIsDecorated(false);
35//FIXME: _listView->setFullWidth(true);
36// _listView->setItemsRenameable(true);
37 _listView->setSelectionMode(QAbstractItemView::SingleSelection);
38 _listView->setSortingEnabled(false);
39
40
41 connect( _listView, SIGNAL( itemSelectionChanged()),
42 this, SLOT( slotSelectionChanged()));
43
44 connect( _listView, SIGNAL( itemChanged( QTreeWidgetItem*,int)),
45 this, SLOT( slotItemRenamed(QTreeWidgetItem*)));
46}
47
48
49MapsListView::~MapsListView() {
50}
51
52void MapsListView::addMap(const QString & name = "") {
53 kDebug() << "MapsListView::addMap: " << name;
54 QStringList list(name);
55 new QTreeWidgetItem(_listView,list);
56 //kDebug() << "MapsListView::addMap : Added map '" << name << "'";
57
58}
59
60void MapsListView::addMaps(const QList<MapTag*> & maps) {
61 QListIterator<MapTag*> it(maps);
62 while (it.hasNext()) {
63 MapTag *tag = it.next();
64 QString s = tag->name;
65 kDebug() << "MapsListView::addMaps:" << s;
66 addMap(s);
67 }
68}
69
70void MapsListView::selectMap(const QString & name) {
71 QList<QTreeWidgetItem *> items = _listView->findItems(name,Qt::MatchExactly);
72 if (items.count()>0) {
73 selectMap(items[0]);
74 } else {
75 kWarning() << "MapsListView::selectMap : Couldn't found map '" << name << "'";
76 }
77
78}
79
80void MapsListView::selectMap(QTreeWidgetItem* item) {
81 if (item) {
82 item->setSelected(true);
83 }
84}
85
86
87QString MapsListView::selectedMap() {
88 QString result;
89
90 QList<QTreeWidgetItem *> items = _listView->selectedItems();
91 if (items.count()>0)
92 result = items[0]->text(0);
93 else
94 kWarning() << "MapsListView::selectedMap : No map selected !";
95
96 return result;
97}
98
99void MapsListView::removeMap(const QString & name) {
100 QList<QTreeWidgetItem *> items = _listView->findItems(name,Qt::MatchExactly);
101 if (items.count()>0) {
102 int i = _listView->invisibleRootItem()->indexOfChild(items[0]);
103 _listView->takeTopLevelItem(i);
104 if (_listView->currentItem())
105 _listView->currentItem()->setSelected(true);
106// kDebug() << "MapsListView::removeMap : Removed map '" << name << "'";
107 } else
108 kWarning() << "MapsListView::removeMap : Couldn't found map '" << name << "'";
109}
110
111void MapsListView::clear() {
112 _listView->clear();
113}
114
115void MapsListView::slotSelectionChanged() {
116 QList<QTreeWidgetItem *> list = _listView->selectedItems();
117 if (list.count()>0) {
118 QString name = list[0]->text(0);
119 emit mapSelected(name);
120 }
121}
122
123void MapsListView::slotItemRenamed(QTreeWidgetItem* item) {
124 QString name = item->text(0);
125 emit mapRenamed(name);
126}
127
128void MapsListView::changeMapName(const QString & oldName, const QString & newName) {
129// kDebug() << "MapsListView::changeMapName : " << oldName << " to " << newName;
130 QList<QTreeWidgetItem *> items = _listView->findItems(oldName,Qt::MatchExactly);
131 if (items.count()>0) {
132 items[0]->setText(0,newName);
133// kDebug() << "MapsListView::changeMapName : successful";
134 }
135 else {
136 kWarning() << "MapsListView::changeMapName : Chouldn't find map with name '" << oldName << "'";
137 }
138
139}
140
141
142bool MapsListView::nameAlreadyExists(const QString & name) {
143 return _listView->findItems(name, Qt::MatchExactly).count() > 0;
144}
145
146QStringList MapsListView::getMaps() {
147 QStringList result;
148
149 for (int i=0; i<_listView->topLevelItemCount(); i++) {
150 result << _listView->topLevelItem(i)->text(0);
151 }
152
153 return result;
154}
155
156QString MapsListView::getUnusedMapName() {
157 QString result;
158 QString attempt;
159 int i=0;
160 while(result.isEmpty()) {
161 i++;
162 attempt = i18n("unnamed");
163 attempt += QString::number(i);
164 if (nameAlreadyExists(attempt))
165 continue;
166
167 result = attempt;
168 }
169
170// kDebug() << "MapsListView::getUnusedMapName : Found an unused name : '" << result << "'";
171 return result;
172}
173
174int MapsListView::count() {
175 return _listView->topLevelItemCount();
176}
177
178#include "mapslistview.moc"
179