File indexing completed on 2024-04-28 07:29:11
0001 /*************************************************************************** 0002 * Copyright (C) 2004-2006 by Albert Astals Cid * 0003 * aacid@kde.org * 0004 * * 0005 * This program is free software; you can redistribute it and/or modify * 0006 * it under the terms of the GNU General Public License as published by * 0007 * the Free Software Foundation; either version 2 of the License, or * 0008 * (at your option) any later version. * 0009 ***************************************************************************/ 0010 0011 #include "mapchooser.h" 0012 0013 #include <KLocalizedString> 0014 #include <KMessageBox> 0015 #include <KListWidgetSearchLine> 0016 #include <KFileUtils> 0017 0018 #include <QDir> 0019 #include <QImage> 0020 #include <QLabel> 0021 #include <QLayout> 0022 #include <QListWidget> 0023 #include <QDialogButtonBox> 0024 #include <QPushButton> 0025 #include <QSet> 0026 0027 #include <algorithm> 0028 0029 #include "settings.h" 0030 0031 bool myLessThan(const QString &s1, const QString &s2) 0032 { 0033 return s1.localeAwareCompare(s2) < 0; 0034 } 0035 0036 mapChooser::mapChooser(QWidget *parent) : QDialog(parent) 0037 { 0038 setWindowTitle(i18n("Choose Map to Use")); 0039 0040 QVBoxLayout *mainLayout = new QVBoxLayout(); 0041 0042 QHBoxLayout *horizontalLayout = new QHBoxLayout(); 0043 0044 QVBoxLayout *listFilterLayout = new QVBoxLayout(); 0045 p_listBox = new QListWidget(); 0046 KListWidgetSearchLine *searchLine = new KListWidgetSearchLine(this, p_listBox); 0047 searchLine->setPlaceholderText(i18n("Filter Maps")); 0048 listFilterLayout -> addWidget(searchLine); 0049 listFilterLayout -> addWidget(p_listBox); 0050 0051 horizontalLayout->addLayout(listFilterLayout); 0052 0053 p_imageContainer = new QLabel(); 0054 p_imageContainer -> setFixedSize(300, 225); 0055 p_imageContainer -> setAlignment(Qt::AlignCenter); 0056 horizontalLayout -> addWidget(p_imageContainer); 0057 0058 mainLayout->addLayout(horizontalLayout); 0059 0060 QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok 0061 | QDialogButtonBox::Cancel); 0062 connect(buttonBox, &QDialogButtonBox::accepted, this, &mapChooser::accept); 0063 connect(buttonBox, &QDialogButtonBox::rejected, this, &mapChooser::reject); 0064 mainLayout->addWidget(buttonBox); 0065 0066 QStringList errorTexts; 0067 QString lastMapFile = kgeographySettings::self() -> lastMap(); 0068 QString stringToSelect; 0069 QStringList texts; 0070 0071 const QStringList mapFiles = KFileUtils::findAllUniqueFiles(QStandardPaths::standardLocations(QStandardPaths::AppDataLocation), {QStringLiteral("*.kgm")}); 0072 0073 for (const QString &mapFilename : mapFiles) { 0074 KGmap *m = p_reader.parseMap(mapFilename); 0075 if (!m) 0076 errorTexts << i18n("Error parsing %1: %2", mapFilename, p_reader.getError()); 0077 else 0078 { 0079 QString text = m -> getName(); 0080 KGmap* existingMap = p_maps.value(text); 0081 if (existingMap) 0082 { 0083 errorTexts << i18n("The map %1 has the same name of map %2", mapFilename, existingMap -> getFile()); 0084 delete m; 0085 } 0086 else 0087 { 0088 texts << text; 0089 p_maps.insert(text, m); 0090 if ( mapFilename == lastMapFile ) 0091 stringToSelect = text; 0092 } 0093 } 0094 } 0095 0096 if (errorTexts.size() > 0) 0097 KMessageBox::errorList(this, i18n("Error parsing"), errorTexts); 0098 0099 connect(p_listBox, &QListWidget::currentTextChanged, this, &mapChooser::putImage); 0100 connect(p_listBox, &QListWidget::itemActivated, this, &mapChooser::accept); 0101 0102 std::sort(texts.begin(), texts.end(), myLessThan); 0103 for(const QString &text : std::as_const(texts)) 0104 p_listBox -> addItem(text); 0105 0106 if (p_listBox -> count() == 0) 0107 buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false); 0108 else { 0109 QList<QListWidgetItem *> itemsWhere = p_listBox->findItems(stringToSelect, Qt::MatchExactly); 0110 if ( itemsWhere.size() > 0 ) 0111 p_listBox->setCurrentItem(itemsWhere[0]); 0112 else 0113 p_listBox -> setCurrentRow(0); 0114 } 0115 0116 setLayout(mainLayout); 0117 0118 p_listBox -> setFocus(); 0119 } 0120 0121 mapChooser::~mapChooser() 0122 { 0123 qDeleteAll(p_maps); 0124 } 0125 0126 KGmap *mapChooser::getMap() 0127 { 0128 KGmap *m; 0129 m = p_maps[p_listBox -> currentItem() -> text()]; 0130 p_maps.remove(p_listBox -> currentItem() -> text()); 0131 return m; 0132 } 0133 0134 void mapChooser::putImage(const QString &mapName) 0135 { 0136 KGmap *m; 0137 m = p_maps[mapName]; 0138 QImage image(m -> getMapFile()); 0139 image = image.scaled(300, 225, Qt::KeepAspectRatio, Qt::SmoothTransformation); 0140 p_imageContainer -> setPixmap( QPixmap::fromImage(image) ); 0141 } 0142 0143 #include "moc_mapchooser.cpp"