mapslistview.cpp 5.63 KB
Newer Older
Jan Schäfer's avatar
Jan Schäfer committed
1
2
3
4
/***************************************************************************
                          mapslistview.cpp  -  description
                             -------------------
    begin                : Weg Feb 26 2003
5
    copyright            : (C) 2003 by Jan Schäfer
Jan Schäfer's avatar
Jan Schäfer committed
6
7
8
9
10
11
12
13
14
15
16
    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.                                   *
 *                                                                         *
 ***************************************************************************/
Yuri Chornoivan's avatar
Yuri Chornoivan committed
17
18
19
// local
#include "mapslistview.h"

Jan Schäfer's avatar
Jan Schäfer committed
20
#include <QListWidget>
21
#include <QVBoxLayout>
Jan Schäfer's avatar
Jan Schäfer committed
22

Luigi Toscano's avatar
Luigi Toscano committed
23
// KDE Frameworks
Yuri Chornoivan's avatar
Yuri Chornoivan committed
24
#include <KLocalizedString>
25
#include "kimagemapeditor_debug.h"
Jan Schäfer's avatar
Jan Schäfer committed
26
27


Jens Herden's avatar
Jens Herden committed
28
MapsListView::MapsListView(QWidget *parent)
29
30
31
32
: QWidget(parent) {

  QVBoxLayout *mainLayout = new QVBoxLayout(this);
  mainLayout->setSpacing(0);
Laurent Montel's avatar
Laurent Montel committed
33
  mainLayout->setContentsMargins(0, 0, 0, 0);
34
35
36
37
38

  _listView = new QTreeWidget(this);
  _listView->setColumnCount(1);
  _listView->setHeaderLabel(i18n("Maps"));
  _listView->setRootIsDecorated(false);
Jan Schäfer's avatar
Jan Schäfer committed
39
40
41
42
//FIXME:    _listView->setFullWidth(true);
//    _listView->setItemsRenameable(true);
  _listView->setSelectionMode(QAbstractItemView::SingleSelection);
  _listView->setSortingEnabled(false);
43
  mainLayout->addWidget(_listView);
Jan Schäfer's avatar
Jan Schäfer committed
44

Yuri Chornoivan's avatar
Yuri Chornoivan committed
45
46
  connect( _listView, SIGNAL(itemSelectionChanged()),
           this, SLOT(slotSelectionChanged()));
Jan Schäfer's avatar
Jan Schäfer committed
47

Yuri Chornoivan's avatar
Yuri Chornoivan committed
48
49
  connect( _listView, SIGNAL(itemChanged(QTreeWidgetItem*,int)),
           this, SLOT(slotItemRenamed(QTreeWidgetItem*)));
Jan Schäfer's avatar
Jan Schäfer committed
50
51
52
53
54
55
}


MapsListView::~MapsListView() {
}

56
void MapsListView::addMap(const QString & name = "") {
57
  qCDebug(KIMAGEMAPEDITOR_LOG) << "MapsListView::addMap: " << name;
58
59
  QStringList list(name);
  new QTreeWidgetItem(_listView,list);
60
    //qCDebug(KIMAGEMAPEDITOR_LOG) << "MapsListView::addMap : Added map '" << name << "'";
Jan Schäfer's avatar
Jan Schäfer committed
61
62
63

}

Jan Schäfer's avatar
Jan Schäfer committed
64
65
66
void MapsListView::addMaps(const QList<MapTag*> & maps) {
    QListIterator<MapTag*> it(maps);
    while (it.hasNext()) {
67
68
      MapTag *tag = it.next();
      QString s = tag->name;
69
      qCDebug(KIMAGEMAPEDITOR_LOG) << "MapsListView::addMaps:" << s;
70
      addMap(s);
Jan Schäfer's avatar
Jan Schäfer committed
71
72
73
74
    }
}

void MapsListView::selectMap(const QString & name) {
Jan Schäfer's avatar
Jan Schäfer committed
75
76
77
78
    QList<QTreeWidgetItem *> items = _listView->findItems(name,Qt::MatchExactly);
    if (items.count()>0) {
       selectMap(items[0]);
    } else {
79
       qCWarning(KIMAGEMAPEDITOR_LOG) << "MapsListView::selectMap : Couldn't found map '" << name << "'";
Jan Schäfer's avatar
Jan Schäfer committed
80
    }
Jan Schäfer's avatar
Jan Schäfer committed
81
82
83

}

Jan Schäfer's avatar
Jan Schäfer committed
84
85
86
87
void MapsListView::selectMap(QTreeWidgetItem* item) {
    if (item) {
        item->setSelected(true);
    }
Jan Schäfer's avatar
Jan Schäfer committed
88
89
90
91
92
93
}


QString MapsListView::selectedMap() {
    QString result;

Jan Schäfer's avatar
Jan Schäfer committed
94
95
96
    QList<QTreeWidgetItem *> items = _listView->selectedItems();
    if (items.count()>0)
        result = items[0]->text(0);
Jan Schäfer's avatar
Jan Schäfer committed
97
    else
98
        qCWarning(KIMAGEMAPEDITOR_LOG) << "MapsListView::selectedMap : No map selected !";
Jan Schäfer's avatar
Jan Schäfer committed
99
100
101
102
103

    return result;
}

void MapsListView::removeMap(const QString & name) {
Jan Schäfer's avatar
Jan Schäfer committed
104
105
106
107
108
109
    QList<QTreeWidgetItem *> items = _listView->findItems(name,Qt::MatchExactly);
    if (items.count()>0) {
        int i = _listView->invisibleRootItem()->indexOfChild(items[0]);
        _listView->takeTopLevelItem(i);
        if (_listView->currentItem())
            _listView->currentItem()->setSelected(true);
110
//        qCDebug(KIMAGEMAPEDITOR_LOG) << "MapsListView::removeMap : Removed map '" << name << "'";
Jan Schäfer's avatar
Jan Schäfer committed
111
    } else
112
        qCWarning(KIMAGEMAPEDITOR_LOG) << "MapsListView::removeMap : Couldn't found map '" << name << "'";
Jan Schäfer's avatar
Jan Schäfer committed
113
114
115
116
117
118
}

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

Jan Schäfer's avatar
Jan Schäfer committed
119
120
121
122
123
124
void MapsListView::slotSelectionChanged() {
    QList<QTreeWidgetItem *> list = _listView->selectedItems();
    if (list.count()>0) {
        QString name = list[0]->text(0);
        emit mapSelected(name);
    }
Jan Schäfer's avatar
Jan Schäfer committed
125
126
}

Jan Schäfer's avatar
Jan Schäfer committed
127
void MapsListView::slotItemRenamed(QTreeWidgetItem* item) {
Jan Schäfer's avatar
Jan Schäfer committed
128
129
130
131
132
    QString name = item->text(0);
    emit mapRenamed(name);
}

void MapsListView::changeMapName(const QString & oldName, const QString & newName) {
133
//    qCDebug(KIMAGEMAPEDITOR_LOG) << "MapsListView::changeMapName : " << oldName << " to " << newName;
Jan Schäfer's avatar
Jan Schäfer committed
134
135
136
    QList<QTreeWidgetItem *> items = _listView->findItems(oldName,Qt::MatchExactly);
    if (items.count()>0) {
        items[0]->setText(0,newName);
137
//        qCDebug(KIMAGEMAPEDITOR_LOG) << "MapsListView::changeMapName : successful";
Jan Schäfer's avatar
Jan Schäfer committed
138
139
    }
    else {
Yuri Chornoivan's avatar
Yuri Chornoivan committed
140
        qCWarning(KIMAGEMAPEDITOR_LOG) << "MapsListView::changeMapName : Couldn't find map with name '" << oldName << "'";
Jan Schäfer's avatar
Jan Schäfer committed
141
142
143
144
145
146
    }

}


bool MapsListView::nameAlreadyExists(const QString & name) {
Jan Schäfer's avatar
Jan Schäfer committed
147
    return _listView->findItems(name, Qt::MatchExactly).count() > 0;  
Jan Schäfer's avatar
Jan Schäfer committed
148
149
}

Jan Schäfer's avatar
Jan Schäfer committed
150
151
QStringList MapsListView::getMaps() {
    QStringList result;
Jens Herden's avatar
Jens Herden committed
152

Jan Schäfer's avatar
Jan Schäfer committed
153
154
    for (int i=0; i<_listView->topLevelItemCount(); i++) {
         result << _listView->topLevelItem(i)->text(0);
Jan Schäfer's avatar
Jan Schäfer committed
155
    }
Jens Herden's avatar
Jens Herden committed
156

Jan Schäfer's avatar
Jan Schäfer committed
157
158
159
    return result;
}

Jan Schäfer's avatar
Jan Schäfer committed
160
161
162
163
164
165
166
167
168
169
170
171
172
173
QString MapsListView::getUnusedMapName() {
    QString result;
    QString attempt;
    int i=0;
    while(result.isEmpty()) {
        i++;
        attempt = i18n("unnamed");
        attempt += QString::number(i);
        if (nameAlreadyExists(attempt))
            continue;

        result = attempt;
    }

174
//    qCDebug(KIMAGEMAPEDITOR_LOG) << "MapsListView::getUnusedMapName : Found an unused name : '" << result << "'";
Jan Schäfer's avatar
Jan Schäfer committed
175
176
177
    return result;
}

Jens Herden's avatar
Jens Herden committed
178
int MapsListView::count() {
Jan Schäfer's avatar
Jan Schäfer committed
179
    return _listView->topLevelItemCount();
Jan Schäfer's avatar
Jan Schäfer committed
180
181
}