diff --git a/src/kgeography.cpp b/src/kgeography.cpp index 482aaf9..6b41668 100644 --- a/src/kgeography.cpp +++ b/src/kgeography.cpp @@ -1,508 +1,508 @@ /*************************************************************************** * Copyright (C) 2004-2007 by Albert Astals Cid * * aacid@kde.org * * * * 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. * ***************************************************************************/ #include "kgeography.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "answersdialog.h" #include "capitaldivisionasker.h" #include "divisioncapitalasker.h" #include "divisionflagasker.h" #include "flagdivisionasker.h" #include "settings.h" #include "mapasker.h" #include "mapchooser.h" #include "mapparser.h" #include "map.h" #include "placeasker.h" #include "integerinputdialog.h" #include "ui_kgeographyoptions.h" kgeography::kgeography() : KXmlGuiWindow(), p_firstShow(true), p_mustShowResultsDialog(false) { p_map = 0; p_askWidget = 0; p_bigWidget = new QWidget(this); QHBoxLayout *bigWidgetLayout = new QHBoxLayout(p_bigWidget); bigWidgetLayout -> setSpacing(0); bigWidgetLayout -> setContentsMargins(0, 0, 0, 0); QWidget *leftWidget = new QWidget(p_bigWidget); QVBoxLayout *leftWidgetLayout = new QVBoxLayout(leftWidget); p_currentMap = new QLabel(leftWidget); p_currentMap -> setAlignment(Qt::AlignCenter); p_consult = new QPushButton(i18n("E&xplore Map"), leftWidget); p_consult->setWhatsThis(i18n("In this section left click on any part of the map to learn about the divisions" )); QLabel * testYourselfLabel = new QLabel( i18n("Test Yourself:"), this ); testYourselfLabel->setAlignment( Qt::AlignHCenter ); p_askMap = new QPushButton(i18n("&Location of Regions"), leftWidget); p_askMap->setWhatsThis(i18n("In this challenge you are given a division name on the left under the menu and you must find it on the map and click on it")); p_askPlaceMap = new QPushButton(i18n("&Place Regions in the Map"), leftWidget); p_askPlaceMap->setWhatsThis(i18n("In this challenge you are given the shape of a region and you must place it on the map")); p_askCapitalDivisions = new QPushButton(i18n("&Regions by Capital"), leftWidget); p_askCapitalDivisions->setWhatsThis(i18n("In this quiz you have to guess the division name given its capital")); p_askDivisionCapitals = new QPushButton(i18n("&Capitals of Regions"), leftWidget); p_askDivisionCapitals->setWhatsThis(i18n("In this quiz you have to guess the capital of a given division name")); p_askFlagDivisions = new QPushButton(i18n("Re&gions by Flag"), leftWidget); p_askFlagDivisions->setWhatsThis(i18n("In this quiz you have to guess the division name given its flag")); p_askDivisionFlags = new QPushButton(i18n("&Flags of Regions"), leftWidget); p_askDivisionFlags->setWhatsThis(i18n("In this quiz you have to guess the flag of a division given its name")); p_underLeftWidget = new QWidget(leftWidget); // where the number of answers will be shown m_underLeftWidgetLayout = new QVBoxLayout(p_underLeftWidget); m_underLeftWidgetLayout->setContentsMargins(0, 0, 0, 0); leftWidgetLayout -> addWidget(p_currentMap); leftWidgetLayout -> addWidget(p_consult); leftWidgetLayout -> addSpacing(10); leftWidgetLayout -> addWidget(testYourselfLabel); leftWidgetLayout -> addWidget(p_askMap); leftWidgetLayout -> addWidget(p_askPlaceMap); leftWidgetLayout -> addWidget(p_askCapitalDivisions); leftWidgetLayout -> addWidget(p_askDivisionCapitals); leftWidgetLayout -> addWidget(p_askFlagDivisions); leftWidgetLayout -> addWidget(p_askDivisionFlags); leftWidgetLayout -> addWidget(p_underLeftWidget); // Make p_underLeftWidget as thin as possible, and always be shown in the same place QWidget* p_underUnderLeftWidget = new QWidget(leftWidget); QVBoxLayout *underUnderLeftWidgetLayout = new QVBoxLayout(p_underUnderLeftWidget); underUnderLeftWidgetLayout -> addStretch(1); leftWidgetLayout -> addWidget(p_underUnderLeftWidget); bigWidgetLayout -> addWidget(leftWidget); setCentralWidget(p_bigWidget); connect(p_consult, &QPushButton::clicked, this, &kgeography::consult); connect(p_askMap, &QPushButton::clicked, this, &kgeography::askMap); connect(p_askPlaceMap, &QPushButton::clicked, this, &kgeography::askPlaceMap); connect(p_askCapitalDivisions, &QPushButton::clicked, this, &kgeography::askCapitalDivisions); connect(p_askDivisionCapitals, &QPushButton::clicked, this, &kgeography::askDivisionCapitals); connect(p_askFlagDivisions, &QPushButton::clicked, this, &kgeography::askFlagDivisions); connect(p_askDivisionFlags, &QPushButton::clicked, this, &kgeography::askDivisionFlags); QAction *a = KStandardAction::open(this, SLOT(openMap()), actionCollection()); a -> setText(i18n("&Open Map...")); a = KStandardAction::quit(this, SLOT(close()), actionCollection()); p_zoom = actionCollection()->add( QStringLiteral("zoom_select") ); p_zoom -> setText(i18n("&Zoom") ); p_zoom -> setIcon( QIcon::fromTheme(QStringLiteral("zoom-in")) ); p_zoom -> setEnabled(false); p_zoomOriginal = actionCollection()->addAction( QStringLiteral("zoom_original") ); p_zoomOriginal -> setText( i18n("&Original Size") ); p_zoomOriginal -> setIcon( QIcon::fromTheme(QStringLiteral("zoom-original")) ); p_zoomOriginal -> setEnabled(false); p_zoomAutomatic = actionCollection()->add( QStringLiteral("zoom_automatic") ); p_zoomAutomatic -> setText( i18n("&Automatic Zoom") ); p_zoomAutomatic -> setIcon( QIcon::fromTheme(QStringLiteral("zoom-fit-best")) ); p_zoomAutomatic -> setEnabled(false); p_move = actionCollection()->add( QStringLiteral("move") ); p_move->setText( i18n("&Move") ); p_move->setIcon( QIcon::fromTheme(QStringLiteral("transform-move")) ); p_move -> setEnabled(false); a = actionCollection()->addAction( QStringLiteral("disclaimer") ); a->setText( i18n("Disclaimer") ); connect(a, &QAction::triggered, this, &kgeography::disclaimer); p_showAuthor = actionCollection()->addAction( QStringLiteral("author") ); p_showAuthor->setText( i18n("Map author") ); p_showAuthor->setEnabled(false); connect(p_showAuthor, &QAction::triggered, this, &kgeography::showMapAuthor); KStandardAction::preferences(this, SLOT(showPreferencesDialog()), actionCollection()); setupGUI(Keys | ToolBar | Save | Create); show(); } kgeography::~kgeography() { delete p_askWidget; delete p_map; } void kgeography::showPreferencesDialog() { if ( KConfigDialog::showDialog( QStringLiteral("settings") ) ) return; // KConfigDialog didn't find an instance of this dialog, so lets // create it : KConfigDialog* dialog = new KConfigDialog(this, QStringLiteral("settings"), kgeographySettings::self()); // FIXME: Waiting for the answer from Kévin about what to do with it // dialog->setHelp("configuration", "kgeography"); QWidget *w = new QWidget(); Ui::Options confWdg; confWdg.setupUi(w); dialog->addPage(w, i18n("General"), QStringLiteral("kgeography") ); // User edited the configuration - update your local copies of the // configuration data connect(dialog, &KConfigDialog::settingsChanged, this, &kgeography::updateConfiguration); dialog->show(); } void kgeography::updateConfiguration() { boxAsker *aBoxAsker = dynamic_cast(p_askWidget); if ( aBoxAsker != NULL ) { aBoxAsker->updateLayout(); } } void kgeography::showEvent(QShowEvent *) { if (p_firstShow) { QString file = kgeographySettings::self() -> lastMap(); if (QFile::exists(file)) { mapReader reader; KGmap *kgmap = reader.parseMap(file); if (kgmap) { setMap(kgmap); } else { KMessageBox::error(this, i18n("Could not open last used map. Error parsing %1: %2", file, reader.getError())); openMap(); } } else openMap(); if (!p_map) { p_currentMap -> setText(i18n("There is no current map")); p_consult -> setEnabled(false); p_askMap -> setEnabled(false); p_askPlaceMap -> setEnabled(false); p_askFlagDivisions -> setEnabled(false); p_askDivisionFlags -> setEnabled(false); p_askCapitalDivisions -> setEnabled(false); p_askDivisionCapitals -> setEnabled(false); } // if anyone can explain why with the slot works and now without // i'll be glad to learn QTimer::singleShot(0, this, &kgeography::resizeMainWindow); // resizeMainWindow(); p_firstShow = false; } } void kgeography::openMap() { mapChooser mp(this); if (mp.exec() == mapChooser::Accepted) { showResultsDialog(); setMap(mp.getMap()); // if anyone can explain why with the slot works and now without // i'll be glad to learn QTimer::singleShot(0, this, &kgeography::resizeMainWindow); // resizeMainWindow(); } } void kgeography::consult() { // No need to create another map viewer if we are already in it mapAsker *ma = dynamic_cast(p_askWidget); if (ma && !ma -> isAsker()) return; showResultsDialog(); removeOldAskWidget(); p_askWidget = new mapAsker(p_bigWidget, p_map, p_underLeftWidget, false); p_zoom -> setEnabled(true); p_zoomOriginal -> setEnabled(true); p_zoomAutomatic -> setEnabled(true); putAskWidget(); } int kgeography::askQuestionNumber(int upto, bool *rOK) { int i; bool ok; i = IntegerInputDialog::GetInteger(this, i18n("Number of Questions"), i18n("How many questions do you want? (1 to %1)", upto), 1, upto, upto, &ok); if ( rOK != NULL ) *rOK = ok; return i; } void kgeography::askCapitalDivisions() { int i; bool ok; showResultsDialog(); i = askQuestionNumber(p_map -> count(division::eCapital), &ok); if (ok) { removeOldAskWidget(); p_askWidget = new capitalDivisionAsker(p_bigWidget, p_map, p_underLeftWidget, i); putAskWidget(); p_mustShowResultsDialog = true; } else consult(); } void kgeography::askDivisionCapitals() { int i; bool ok; showResultsDialog(); i = askQuestionNumber(p_map -> count(division::eCapital), &ok); if (ok) { removeOldAskWidget(); p_askWidget = new divisionCapitalAsker(p_bigWidget, p_map, p_underLeftWidget, i); putAskWidget(); p_mustShowResultsDialog = true; } else consult(); } void kgeography::askMap() { int i; bool ok; showResultsDialog(); i = askQuestionNumber(p_map -> count(division::eClick), &ok); if (ok) { removeOldAskWidget(); p_askWidget = new mapAsker(p_bigWidget, p_map, p_underLeftWidget, true, i); p_zoom -> setEnabled(true); p_zoomOriginal -> setEnabled(true); p_zoomAutomatic -> setEnabled(true); putAskWidget(); p_mustShowResultsDialog = true; } else consult(); } void kgeography::askPlaceMap() { int i; bool ok; showResultsDialog(); i = askQuestionNumber(p_map -> count(division::eClick), &ok); if (ok) { removeOldAskWidget(); p_askWidget = new placeAsker(p_bigWidget, p_map, p_underLeftWidget, i); p_zoom -> setEnabled(true); p_zoomOriginal -> setEnabled(true); p_zoomAutomatic -> setEnabled(true); putAskWidget(); p_mustShowResultsDialog = true; } else consult(); } void kgeography::askFlagDivisions() { int i; bool ok; showResultsDialog(); i = askQuestionNumber(p_map -> count(division::eFlag), &ok); if (ok) { removeOldAskWidget(); p_askWidget = new flagDivisionAsker(p_bigWidget, p_map, p_underLeftWidget, i); putAskWidget(); p_mustShowResultsDialog = true; } else consult(); } void kgeography::askDivisionFlags() { int i; bool ok; showResultsDialog(); i = askQuestionNumber(p_map -> count(division::eFlag), &ok); if (ok) { removeOldAskWidget(); p_askWidget = new divisionFlagAsker(p_bigWidget, p_map, p_underLeftWidget, i); putAskWidget(); p_mustShowResultsDialog = true; } else consult(); } void kgeography::removeOldAskWidget() { // next lines because setZoom(true) (may have) put cursor at application level if ( p_askWidget != NULL ) p_askWidget->setZoom(false); delete p_askWidget; p_askWidget = 0; p_zoom -> setEnabled(false); p_zoomOriginal -> setEnabled(false); p_zoomAutomatic -> setEnabled(false); p_move -> setEnabled(false); p_zoom -> setChecked(false); p_move -> setChecked(false); } QSize kgeography::getPreferredSize() { int ySize = menuBar() -> size().height() + toolBar() -> size().height() + ((mapAsker*) p_askWidget)->mapSize().height(); int xSize = p_underLeftWidget -> parentWidget()-> size().width() + ((mapAsker*) p_askWidget)->mapSize().width(); return QSize(xSize + 10, ySize + 10); } void kgeography::putAskWidget() { static_cast(p_bigWidget -> layout()) -> addWidget(p_askWidget, 1); p_askWidget -> setAutomaticZoom(p_zoomAutomatic -> isChecked()); p_askWidget -> show(); connect(p_askWidget, &askWidget::setZoomActionChecked, p_zoom, &KToggleAction::setChecked); connect(p_zoom, &KToggleAction::toggled, p_askWidget, &askWidget::setZoom); connect(p_zoomOriginal, &QAction::triggered, p_askWidget, &askWidget::setOriginalZoom); connect(p_zoomOriginal, &QAction::triggered, p_zoomAutomatic, &QAction::setChecked); connect(p_zoomAutomatic, &KToggleAction::toggled, this, &kgeography::setAutomaticZoom); connect(p_askWidget, &askWidget::setMoveActionEnabled, this, &kgeography::setMoveActionEnabled); connect(p_askWidget, &askWidget::setMoveActionChecked, p_move, &KToggleAction::setChecked); connect(p_move, &KToggleAction::toggled, p_askWidget, &askWidget::setMovement); connect(p_askWidget, &askWidget::questionsEnded, this, &kgeography::questionsEnded); } void kgeography::setMap(KGmap *m) { removeOldAskWidget(); kgeographySettings *set = kgeographySettings::self(); set -> setLastMap(m -> getFile()); set -> save(); delete p_map; p_map = m; p_showAuthor->setEnabled(m); - QString mapName = i18nc(p_map -> getFileName().toUtf8(), p_map -> getName().toUtf8()); + QString mapName = p_map -> getName(); QString divisionType = p_map->getDivisionsString(); p_askMap->setText(i18n("&Location of %1", divisionType)); p_askPlaceMap->setText(i18n("&Place %1 in the Map", divisionType)); p_askCapitalDivisions->setText(p_map->getCapitalToDivisionTitle()); p_askDivisionCapitals->setText(p_map->getDivisionToCapitalTitle()); p_askFlagDivisions->setText(i18n("%1 by Flag", divisionType)); p_askDivisionFlags->setText(i18n("&Flags of %1", divisionType)); p_currentMap -> setText(i18n("Current map:
%1
", mapName)); p_consult -> setEnabled(true); p_askMap -> setEnabled(m -> count(division::eClick) > 0); p_askPlaceMap -> setEnabled(m -> count(division::eClick) > 0); // as we always want to let user choose among 4 choices, verify we have the ammunition p_askFlagDivisions -> setEnabled(m -> count(division::eFlag) > 3); p_askDivisionFlags -> setEnabled(m -> count(division::eFlag) > 3); p_askCapitalDivisions -> setEnabled(m -> count(division::eCapital) > 3); p_askDivisionCapitals -> setEnabled(m -> count(division::eCapital) > 3); consult(); // set a maximum width for the under left widget, enabling the labels wordwrap and // avoiding the move of the map in case of large division names. int width = qMax(p_askMap->sizeHint().width(), p_askPlaceMap->sizeHint().width()); width = qMax(width, p_askCapitalDivisions->sizeHint().width()); width = qMax(width, p_askDivisionCapitals->sizeHint().width()); width = qMax(width, p_askFlagDivisions->sizeHint().width()); width = qMax(width, p_askDivisionFlags->sizeHint().width()); p_underLeftWidget->setMaximumWidth(width); } void kgeography::disclaimer() { KMessageBox::information(this, i18n("Maps, flags, translations, etc. are as accurate as their respective authors could achieve," " but KGeography should not be taken as an authoritative source."), i18n("Disclaimer")); } void kgeography::showMapAuthor() { if (p_map) { KMessageBox::information(this, i18n("This map has been created by %1.", p_map->getAuthor()), i18n("Map Author")); } } void kgeography::resizeMainWindow() { if (p_askWidget && width() ) resize(getPreferredSize().expandedTo(size())); } void kgeography::questionsEnded() { showResultsDialog(); consult(); } void kgeography::setAutomaticZoom(bool b) { p_zoom->setChecked(false); p_askWidget->setAutomaticZoom(b); } void kgeography::setMoveActionEnabled(bool b) { p_move->setEnabled(b); if (b && p_zoomAutomatic->isChecked()) { // we don't want the unchecking to bring us to the original zoom state p_zoomAutomatic->blockSignals(true); p_zoomAutomatic->setChecked(false); p_zoomAutomatic->blockSignals(false); } } void kgeography::showResultsDialog() { if (p_mustShowResultsDialog) { p_mustShowResultsDialog = false; int ca = p_askWidget -> correctAnswers(); QString q = p_askWidget -> getQuestionHook(); QVector ua = p_askWidget -> userAnswers(); answersDialog *ad = new answersDialog(this, ua, q, ca); ad -> exec(); delete ad; } } diff --git a/src/mapchooser.cpp b/src/mapchooser.cpp index e91f7aa..d414a04 100644 --- a/src/mapchooser.cpp +++ b/src/mapchooser.cpp @@ -1,143 +1,143 @@ /*************************************************************************** * Copyright (C) 2004-2006 by Albert Astals Cid * * aacid@kde.org * * * * 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. * ***************************************************************************/ #include "mapchooser.h" #include #include #include #include #include #include #include #include #include #include #include #include "settings.h" bool myLessThan(const QString &s1, const QString &s2) { return s1.localeAwareCompare(s2) < 0; } mapChooser::mapChooser(QWidget *parent) : QDialog(parent) { setWindowTitle(i18n("Choose Map to Use")); QVBoxLayout *mainLayout = new QVBoxLayout(); QHBoxLayout *horizontalLayout = new QHBoxLayout(); QVBoxLayout *listFilterLayout = new QVBoxLayout(); p_listBox = new QListWidget(); KListWidgetSearchLine *searchLine = new KListWidgetSearchLine(this, p_listBox); searchLine->setPlaceholderText(i18n("Filter Maps")); listFilterLayout -> addWidget(searchLine); listFilterLayout -> addWidget(p_listBox); horizontalLayout->addLayout(listFilterLayout); p_imageContainer = new QLabel(); p_imageContainer -> setFixedSize(300, 225); p_imageContainer -> setAlignment(Qt::AlignCenter); horizontalLayout -> addWidget(p_imageContainer); mainLayout->addLayout(horizontalLayout); QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel); connect(buttonBox, &QDialogButtonBox::accepted, this, &mapChooser::accept); connect(buttonBox, &QDialogButtonBox::rejected, this, &mapChooser::reject); mainLayout->addWidget(buttonBox); QStringList errorTexts; QString lastMapFile = kgeographySettings::self() -> lastMap(); QString stringToSelect; QStringList texts; const QStringList dirs = QStandardPaths::locateAll(QStandardPaths::DataLocation, QLatin1String(""), QStandardPaths::LocateDirectory); foreach (const QString &dir, dirs) { const QStringList fileNames = QDir(dir).entryList(QStringList() << QStringLiteral("*.kgm")); foreach (const QString &file, fileNames) { QString mapFilename = dir + '/' + file; KGmap *m = p_reader.parseMap(mapFilename); if (!m) errorTexts << i18n("Error parsing %1: %2", mapFilename, p_reader.getError()); else { - QString text = i18nc(m->getFileName().toUtf8(), m->getName().toUtf8()); + QString text = m -> getName(); // avoid multiple and should guarantee that first in XDG_DATA_DIRS is chosen) if (texts.contains(text)) delete m; else { texts << text; p_maps.insert(text, m); if ( mapFilename == lastMapFile ) stringToSelect = text; } } } } if (errorTexts.size() > 0) KMessageBox::errorList(this, i18n("Error parsing"), errorTexts); connect(p_listBox, &QListWidget::currentTextChanged, this, &mapChooser::putImage); connect(p_listBox, &QListWidget::itemActivated, this, &mapChooser::accept); std::sort(texts.begin(), texts.end(), myLessThan); foreach(const QString &text, texts) p_listBox -> addItem(text); if (p_listBox -> count() == 0) buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false); else { QList itemsWhere = p_listBox->findItems(stringToSelect, Qt::MatchExactly); if ( itemsWhere.size() > 0 ) p_listBox->setCurrentItem(itemsWhere[0]); else p_listBox -> setCurrentRow(0); } setLayout(mainLayout); p_listBox -> setFocus(); } mapChooser::~mapChooser() { qDeleteAll(p_maps); } KGmap *mapChooser::getMap() { KGmap *m; m = p_maps[p_listBox -> currentItem() -> text()]; p_maps.remove(p_listBox -> currentItem() -> text()); return m; } void mapChooser::putImage(const QString &mapName) { KGmap *m; m = p_maps[mapName]; QImage image(m -> getMapFile()); image = image.scaled(300, 225, Qt::KeepAspectRatio, Qt::SmoothTransformation); p_imageContainer -> setPixmap( QPixmap::fromImage(image) ); } diff --git a/src/mapparser.cpp b/src/mapparser.cpp index e8179fb..3005c81 100644 --- a/src/mapparser.cpp +++ b/src/mapparser.cpp @@ -1,235 +1,235 @@ /*************************************************************************** * Copyright (C) 2004-2007 by Albert Astals Cid * * aacid@kde.org * * * * 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. * ***************************************************************************/ #include "mapparser.h" #include #include #include #include #include "division.h" #include "map.h" mapReader::mapReader() { } KGmap *mapReader::parseMap(const QString &path) { QString baseDir; p_error.clear(); KGmap *kgmap = new KGmap(); kgmap -> setFile(path); baseDir = QFileInfo(path).absolutePath() + '/'; // baseDir = path but without the file name QFile xmlFile(path); if (xmlFile.exists()) { if (xmlFile.open(QIODevice::ReadOnly)) { QDomDocument doc; doc.setContent(xmlFile.readAll()); const QDomElement &root = doc.documentElement(); if (root.tagName() == QLatin1String("map")) { const QByteArray ctxt = kgmap -> getFileName().toUtf8(); // Map name - kgmap -> setName( getElementString(QStringLiteral("name"), root, Mandatory) ); + kgmap -> setName(i18nc(ctxt, getElementString(QStringLiteral("name"), root, Mandatory).toUtf8())); // Map image file if (!kgmap -> setMapFile( baseDir + getElementString(QStringLiteral("mapFile"), root, Mandatory) )) { p_error = i18n("The map image file for %1 does not exist", kgmap -> getName()); } // Divisions string QString divisionKindName = getElementString(QStringLiteral("divisionsName"), root, Mandatory); kgmap -> setDivisionsString(i18nc(ctxt, divisionKindName.toUtf8())); QString pat = getElementString(QStringLiteral("capitalToDivisionPattern"), root, Optional); if ( pat.contains('%') ) { kgmap->setCapitalToDivisionQuestionPattern(pat); } else if ( ! pat.isEmpty() ) { QTextStream stream(stderr); - stream << "capitalToDivisionPattern element should contain one '%%' in map " << kgmap->getName(); + stream << "capitalToDivisionPattern element should contain one '%%' in map " << kgmap->getName() << kgmap -> getFile(); } pat = getElementString(QStringLiteral("divisionToCapitalPattern"), root, Optional); if ( pat.contains('%') ) { kgmap->setDivisionToCapitalQuestionPattern(pat); } else if ( ! pat.isEmpty() ) { QTextStream stream(stderr); - stream << "divisionToCapitalPattern element should contain one '%%' in map " << kgmap->getName(); + stream << "divisionToCapitalPattern element should contain one '%%' in map " << kgmap->getName() << kgmap -> getFile(); } QString title = getElementString(QStringLiteral("capitalToDivisionTitle"), root, Optional); if ( ! title.isEmpty() ) { kgmap->setCapitalToDivisionTitle(i18nc("@title", title.toUtf8())); } else { kgmap->setCapitalToDivisionTitle(i18n("%1 by Capital", kgmap -> getDivisionsString())); } title = getElementString(QStringLiteral("divisionToCapitalTitle"), root, Optional); if ( ! title.isEmpty() ) { kgmap->setDivisionToCapitalTitle(i18nc("@title", title.toUtf8())); } else { kgmap->setDivisionToCapitalTitle(i18n("Capitals of %1", kgmap -> getDivisionsString())); } // Author string kgmap -> setAuthor( getElementString(QStringLiteral("author"), root, Mandatory) ); QDomElement divisionTag = root.firstChildElement(QStringLiteral("division")); while (!divisionTag.isNull()) { division *kgdiv = new division(); // division name kgdiv -> setName( getElementString(QStringLiteral("name"), divisionTag, Mandatory) ); // division capital QString capital = getElementString(QStringLiteral("capital"), divisionTag, Optional); if (!capital.isEmpty()) kgdiv -> setCapital(i18nc(ctxt, capital.toUtf8())); // division flag QString flagFile = getElementString(QStringLiteral("flag"), divisionTag, Optional); if (!flagFile.isNull()) { if (!kgdiv -> setFlagFile( baseDir + "/flags/" + flagFile )) { p_error = i18n("The flag image file for %1 does not exist", kgdiv -> getName()); } } // division ignoreness const QString &ignore = getElementString(QStringLiteral("ignore"), divisionTag, Optional).toLower(); if (!ignore.isNull()) { if (ignore == QLatin1String("yes")) kgdiv -> setCanAsk(division::eNone); else if (ignore == QLatin1String("no")) kgdiv -> setCanAsk(division::eClick | division::eCapital | division::eFlag); else if (ignore == QLatin1String("allowclickmode")) kgdiv -> setCanAsk(division::eClick); else if (ignore == QLatin1String("allowclickflagmode")) kgdiv -> setCanAsk(division::eClick | division::eFlag); else { p_error = i18n("Invalid value in tag %1", QStringLiteral("")); } } // division color const QDomElement &colorTag = getElement(QStringLiteral("color"), divisionTag); kgdiv -> setRGB(getElementString(QStringLiteral("red"), colorTag, Mandatory).toInt(), getElementString(QStringLiteral("green"), colorTag, Mandatory).toInt(), getElementString(QStringLiteral("blue"), colorTag, Mandatory).toInt()); // division false capitals QStringList falseCapitals; const QDomNodeList &falseCapitalTags = divisionTag.elementsByTagName(QStringLiteral("falseCapital")); QDomElement falseCapital; for (int i = 0; i < falseCapitalTags.count(); ++i) { falseCapital = falseCapitalTags.item(i).toElement(); if (!falseCapital.isNull()) falseCapitals << falseCapital.text(); } if (!falseCapitals.isEmpty()) kgdiv->setFalseCapitals(falseCapitals); if (!kgmap -> addDivision(kgdiv)) { p_error = i18n("There is already either a division called %1 or a division with the same colors as %2", kgdiv -> getName(), kgdiv -> getName()); delete kgdiv; } divisionTag = divisionTag.nextSiblingElement(QStringLiteral("division")); } } else { p_error = i18n("The map description file should begin with the %1 tag", QStringLiteral("map")); } xmlFile.close(); } else { p_error = i18n("Could not open %1 for reading.", path); } } else { p_error = i18n("%1 does not exist.", path); } if (p_error.isNull()) return kgmap; else { delete kgmap; return NULL; } } QString mapReader::getError() const { return p_error; } QString mapReader::getElementString(const QString &tagName, const QDomElement &parentTag, eMandatoryness mandatoryness) { QString result; const QDomElement &tag = parentTag.firstChildElement(tagName); if (tag.isNull()) { if (mandatoryness == Mandatory) { p_error = i18n("The map description file should have a %1 tag inside %2", tagName, parentTag.tagName()); } } else { if (tag == parentTag.lastChildElement(tagName)) { result = tag.text(); } else { p_error = i18n("The map description file should have exactly one %1 tag inside %2", tagName, parentTag.tagName()); } } return result; } QDomElement mapReader::getElement(const QString &tagName, const QDomElement &parentTag) { const QDomElement &tag = parentTag.firstChildElement(tagName); if (tag.isNull()) { p_error = i18n("The map description file should have a %1 tag inside %2", tagName, parentTag.tagName()); } else { if (tag != parentTag.lastChildElement(tagName)) { p_error = i18n("The map description file should have exactly one %1 tag inside %2", tagName, parentTag.tagName()); } } return tag; }