File: mapslistview.cpp

package info (click to toggle)
kimagemapeditor 4%3A25.04.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,140 kB
  • sloc: cpp: 6,817; xml: 207; sh: 5; makefile: 3
file content (181 lines) | stat: -rw-r--r-- 5,768 bytes parent folder | download | duplicates (3)
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
175
176
177
178
179
180
181
/***************************************************************************
                          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.                                   *
 *                                                                         *
 ***************************************************************************/
// local
#include "mapslistview.h"

#include <QListWidget>
#include <QVBoxLayout>

// KDE Frameworks
#include <KLocalizedString>
#include "kimagemapeditor_debug.h"


MapsListView::MapsListView(QWidget *parent)
: QWidget(parent) {

  QVBoxLayout *mainLayout = new QVBoxLayout(this);
  mainLayout->setSpacing(0);
  mainLayout->setContentsMargins(0, 0, 0, 0);

  _listView = new QTreeWidget(this);
  _listView->setColumnCount(1);
  _listView->setHeaderLabel(i18n("Maps"));
  _listView->setRootIsDecorated(false);
//FIXME:    _listView->setFullWidth(true);
//    _listView->setItemsRenameable(true);
  _listView->setSelectionMode(QAbstractItemView::SingleSelection);
  _listView->setSortingEnabled(false);
  mainLayout->addWidget(_listView);

  connect( _listView, SIGNAL(itemSelectionChanged()),
           this, SLOT(slotSelectionChanged()));

  connect( _listView, SIGNAL(itemChanged(QTreeWidgetItem*,int)),
           this, SLOT(slotItemRenamed(QTreeWidgetItem*)));
}


MapsListView::~MapsListView() {
}

void MapsListView::addMap(const QString & name = "") {
  qCDebug(KIMAGEMAPEDITOR_LOG) << "MapsListView::addMap: " << name;
  QStringList list(name);
  new QTreeWidgetItem(_listView,list);
    //qCDebug(KIMAGEMAPEDITOR_LOG) << "MapsListView::addMap : Added map '" << name << "'";

}

void MapsListView::addMaps(const QList<MapTag*> & maps) {
    QListIterator<MapTag*> it(maps);
    while (it.hasNext()) {
      MapTag *tag = it.next();
      QString s = tag->name;
      qCDebug(KIMAGEMAPEDITOR_LOG) << "MapsListView::addMaps:" << s;
      addMap(s);
    }
}

void MapsListView::selectMap(const QString & name) {
    QList<QTreeWidgetItem *> items = _listView->findItems(name,Qt::MatchExactly);
    if (items.count()>0) {
       selectMap(items[0]);
    } else {
       qCWarning(KIMAGEMAPEDITOR_LOG) << "MapsListView::selectMap : Couldn't found map '" << name << "'";
    }

}

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


QString MapsListView::selectedMap() {
    QString result;

    QList<QTreeWidgetItem *> items = _listView->selectedItems();
    if (items.count()>0)
        result = items[0]->text(0);
    else
        qCWarning(KIMAGEMAPEDITOR_LOG) << "MapsListView::selectedMap : No map selected !";

    return result;
}

void MapsListView::removeMap(const QString & name) {
    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);
//        qCDebug(KIMAGEMAPEDITOR_LOG) << "MapsListView::removeMap : Removed map '" << name << "'";
    } else
        qCWarning(KIMAGEMAPEDITOR_LOG) << "MapsListView::removeMap : Couldn't found map '" << name << "'";
}

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

void MapsListView::slotSelectionChanged() {
    QList<QTreeWidgetItem *> list = _listView->selectedItems();
    if (list.count()>0) {
        QString name = list[0]->text(0);
        emit mapSelected(name);
    }
}

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

void MapsListView::changeMapName(const QString & oldName, const QString & newName) {
//    qCDebug(KIMAGEMAPEDITOR_LOG) << "MapsListView::changeMapName : " << oldName << " to " << newName;
    QList<QTreeWidgetItem *> items = _listView->findItems(oldName,Qt::MatchExactly);
    if (items.count()>0) {
        items[0]->setText(0,newName);
//        qCDebug(KIMAGEMAPEDITOR_LOG) << "MapsListView::changeMapName : successful";
    }
    else {
        qCWarning(KIMAGEMAPEDITOR_LOG) << "MapsListView::changeMapName : Couldn't find map with name '" << oldName << "'";
    }

}


bool MapsListView::nameAlreadyExists(const QString & name) {
    return _listView->findItems(name, Qt::MatchExactly).count() > 0;  
}

QStringList MapsListView::getMaps() {
    QStringList result;

    for (int i=0; i<_listView->topLevelItemCount(); i++) {
         result << _listView->topLevelItem(i)->text(0);
    }

    return result;
}

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;
    }

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

int MapsListView::count() {
    return _listView->topLevelItemCount();
}