File indexing completed on 2023-09-24 04:30:51

0001 /***************************************************************************
0002                           mapslistview.cpp  -  description
0003                              -------------------
0004     begin                : Weg Feb 26 2003
0005     copyright            : (C) 2003 by Jan Schäfer
0006     email                : janschaefer@users.sourceforge.net
0007  ***************************************************************************/
0008 
0009 /***************************************************************************
0010  *                                                                         *
0011  *   This program is free software; you can redistribute it and/or modify  *
0012  *   it under the terms of the GNU General Public License as published by  *
0013  *   the Free Software Foundation; either version 2 of the License, or     *
0014  *   (at your option) any later version.                                   *
0015  *                                                                         *
0016  ***************************************************************************/
0017 // local
0018 #include "mapslistview.h"
0019 
0020 #include <QListWidget>
0021 #include <QVBoxLayout>
0022 
0023 // KDE Frameworks
0024 #include <KLocalizedString>
0025 #include "kimagemapeditor_debug.h"
0026 
0027 
0028 MapsListView::MapsListView(QWidget *parent)
0029 : QWidget(parent) {
0030 
0031   QVBoxLayout *mainLayout = new QVBoxLayout(this);
0032   mainLayout->setSpacing(0);
0033   mainLayout->setContentsMargins(0, 0, 0, 0);
0034 
0035   _listView = new QTreeWidget(this);
0036   _listView->setColumnCount(1);
0037   _listView->setHeaderLabel(i18n("Maps"));
0038   _listView->setRootIsDecorated(false);
0039 //FIXME:    _listView->setFullWidth(true);
0040 //    _listView->setItemsRenameable(true);
0041   _listView->setSelectionMode(QAbstractItemView::SingleSelection);
0042   _listView->setSortingEnabled(false);
0043   mainLayout->addWidget(_listView);
0044 
0045   connect( _listView, SIGNAL(itemSelectionChanged()),
0046            this, SLOT(slotSelectionChanged()));
0047 
0048   connect( _listView, SIGNAL(itemChanged(QTreeWidgetItem*,int)),
0049            this, SLOT(slotItemRenamed(QTreeWidgetItem*)));
0050 }
0051 
0052 
0053 MapsListView::~MapsListView() {
0054 }
0055 
0056 void MapsListView::addMap(const QString & name = "") {
0057   qCDebug(KIMAGEMAPEDITOR_LOG) << "MapsListView::addMap: " << name;
0058   QStringList list(name);
0059   new QTreeWidgetItem(_listView,list);
0060     //qCDebug(KIMAGEMAPEDITOR_LOG) << "MapsListView::addMap : Added map '" << name << "'";
0061 
0062 }
0063 
0064 void MapsListView::addMaps(const QList<MapTag*> & maps) {
0065     QListIterator<MapTag*> it(maps);
0066     while (it.hasNext()) {
0067       MapTag *tag = it.next();
0068       QString s = tag->name;
0069       qCDebug(KIMAGEMAPEDITOR_LOG) << "MapsListView::addMaps:" << s;
0070       addMap(s);
0071     }
0072 }
0073 
0074 void MapsListView::selectMap(const QString & name) {
0075     QList<QTreeWidgetItem *> items = _listView->findItems(name,Qt::MatchExactly);
0076     if (items.count()>0) {
0077        selectMap(items[0]);
0078     } else {
0079        qCWarning(KIMAGEMAPEDITOR_LOG) << "MapsListView::selectMap : Couldn't found map '" << name << "'";
0080     }
0081 
0082 }
0083 
0084 void MapsListView::selectMap(QTreeWidgetItem* item) {
0085     if (item) {
0086         item->setSelected(true);
0087     }
0088 }
0089 
0090 
0091 QString MapsListView::selectedMap() {
0092     QString result;
0093 
0094     QList<QTreeWidgetItem *> items = _listView->selectedItems();
0095     if (items.count()>0)
0096         result = items[0]->text(0);
0097     else
0098         qCWarning(KIMAGEMAPEDITOR_LOG) << "MapsListView::selectedMap : No map selected !";
0099 
0100     return result;
0101 }
0102 
0103 void MapsListView::removeMap(const QString & name) {
0104     QList<QTreeWidgetItem *> items = _listView->findItems(name,Qt::MatchExactly);
0105     if (items.count()>0) {
0106         int i = _listView->invisibleRootItem()->indexOfChild(items[0]);
0107         _listView->takeTopLevelItem(i);
0108         if (_listView->currentItem())
0109             _listView->currentItem()->setSelected(true);
0110 //        qCDebug(KIMAGEMAPEDITOR_LOG) << "MapsListView::removeMap : Removed map '" << name << "'";
0111     } else
0112         qCWarning(KIMAGEMAPEDITOR_LOG) << "MapsListView::removeMap : Couldn't found map '" << name << "'";
0113 }
0114 
0115 void MapsListView::clear() {
0116     _listView->clear();
0117 }
0118 
0119 void MapsListView::slotSelectionChanged() {
0120     QList<QTreeWidgetItem *> list = _listView->selectedItems();
0121     if (list.count()>0) {
0122         QString name = list[0]->text(0);
0123         emit mapSelected(name);
0124     }
0125 }
0126 
0127 void MapsListView::slotItemRenamed(QTreeWidgetItem* item) {
0128     QString name = item->text(0);
0129     emit mapRenamed(name);
0130 }
0131 
0132 void MapsListView::changeMapName(const QString & oldName, const QString & newName) {
0133 //    qCDebug(KIMAGEMAPEDITOR_LOG) << "MapsListView::changeMapName : " << oldName << " to " << newName;
0134     QList<QTreeWidgetItem *> items = _listView->findItems(oldName,Qt::MatchExactly);
0135     if (items.count()>0) {
0136         items[0]->setText(0,newName);
0137 //        qCDebug(KIMAGEMAPEDITOR_LOG) << "MapsListView::changeMapName : successful";
0138     }
0139     else {
0140         qCWarning(KIMAGEMAPEDITOR_LOG) << "MapsListView::changeMapName : Couldn't find map with name '" << oldName << "'";
0141     }
0142 
0143 }
0144 
0145 
0146 bool MapsListView::nameAlreadyExists(const QString & name) {
0147     return _listView->findItems(name, Qt::MatchExactly).count() > 0;  
0148 }
0149 
0150 QStringList MapsListView::getMaps() {
0151     QStringList result;
0152 
0153     for (int i=0; i<_listView->topLevelItemCount(); i++) {
0154          result << _listView->topLevelItem(i)->text(0);
0155     }
0156 
0157     return result;
0158 }
0159 
0160 QString MapsListView::getUnusedMapName() {
0161     QString result;
0162     QString attempt;
0163     int i=0;
0164     while(result.isEmpty()) {
0165         i++;
0166         attempt = i18n("unnamed");
0167         attempt += QString::number(i);
0168         if (nameAlreadyExists(attempt))
0169             continue;
0170 
0171         result = attempt;
0172     }
0173 
0174 //    qCDebug(KIMAGEMAPEDITOR_LOG) << "MapsListView::getUnusedMapName : Found an unused name : '" << result << "'";
0175     return result;
0176 }
0177 
0178 int MapsListView::count() {
0179     return _listView->topLevelItemCount();
0180 }
0181