diff --git a/src/capitaldivisionasker.cpp b/src/capitaldivisionasker.cpp index 6b909ff..bf3b520 100644 --- a/src/capitaldivisionasker.cpp +++ b/src/capitaldivisionasker.cpp @@ -1,67 +1,66 @@ /*************************************************************************** * Copyright (C) 2004 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 "capitaldivisionasker.h" #include #include #include "map.h" capitalDivisionAsker::capitalDivisionAsker(QWidget *parent, KGmap *m, QWidget *w, uint count) : boxAsker(parent, m, w, count) { init(); } bool capitalDivisionAsker::nextBoxAskerQuestionHook(const QString &division, int i, bool isAnswer) { bool b; if (isAnswer) { p_capital = p_map -> getDivisionCapital(division); - p_currentAnswer.setQuestion(i18nc("@item:intable column Question, %1 is capital name", "%1", i18nc(p_map -> getFileName().toUtf8(), p_capital.toUtf8()))); + p_currentAnswer.setQuestion(i18nc("@item:intable column Question, %1 is capital name", "%1", p_capital)); p_currentAnswer.setCorrectAnswer(i18nc("@option:radio is the capital of...", "%1", i18nc(p_map -> getFileName().toUtf8(), division.toUtf8()))); - QString capitalName = i18nc(p_map -> getFileName().toUtf8(), p_capital.toUtf8()); - setQuestion(i18nc("@title:group", p_map->getCapitalToDivisionQuestionPattern().toUtf8(), capitalName)); + setQuestion(i18nc("@title:group", p_map->getCapitalToDivisionQuestionPattern().toUtf8(), p_capital)); p_radioButtons[i] -> setText(i18nc("@option:radio is the capital of...", "%1", i18nc(p_map -> getFileName().toUtf8(), division.toUtf8()))); b = true; } else { // There are crazy countries like Norway where two different divisions // have the same capital, avoid asking // Oslo is the capital of ... // Oslo (true) // Akershus (true) // Buskerud // Hedmark if (p_map -> getDivisionCapital(division) != p_capital) { p_radioButtons[i] -> setText(i18nc("@option:radio is the capital of...", "%1", i18nc(p_map -> getFileName().toUtf8(), division.toUtf8()))); b = true; } else b = false; } return b; } void capitalDivisionAsker::setAnswerHook(int userSays) { p_currentAnswer.setAnswer(p_radioButtons[userSays] -> text()); } QString capitalDivisionAsker::getQuestionHook() const { QString divisionType = i18nc(p_map -> getFileName().toUtf8(), p_map->getDivisionsString().toUtf8()); return i18nc("@title", p_map->getCapitalToDivisionTitle().toUtf8()); //return i18nc("@title", p_map->getCapitalToDivisionTitle().toUtf8(), divisionType); } diff --git a/src/divisioncapitalasker.cpp b/src/divisioncapitalasker.cpp index fd7a8c4..4844264 100644 --- a/src/divisioncapitalasker.cpp +++ b/src/divisioncapitalasker.cpp @@ -1,81 +1,81 @@ /*************************************************************************** * Copyright (C) 2004 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 "divisioncapitalasker.h" #include #include #include #include "map.h" divisionCapitalAsker::divisionCapitalAsker(QWidget *parent, KGmap *m, QWidget *w, uint count) : boxAsker(parent, m, w, count) { init(); } bool divisionCapitalAsker::nextBoxAskerQuestionHook(const QString &division, int i, bool isAnswer) { bool b; if (isAnswer) { QString divisionName = i18nc(p_map -> getFileName().toUtf8(), division.toUtf8()); setQuestion(i18nc("@title:group", p_map->getDivisionToCapitalQuestionPattern().toUtf8(), divisionName)); p_currentAnswer.setQuestion(i18nc("@item:intable column Question, %1 is region name", "%1", i18nc(p_map -> getFileName().toUtf8(), division.toUtf8()))); p_capital = p_map -> getDivisionCapital(division); - p_currentAnswer.setCorrectAnswer(i18nc("@option:radio The capital of is...", "%1", i18nc(p_map -> getFileName().toUtf8(), p_capital.toUtf8()))); - p_radioButtons[i] -> setText(i18nc("@option:radio The capital of is...", "%1", i18nc(p_map -> getFileName().toUtf8(), p_capital.toUtf8()))); + p_currentAnswer.setCorrectAnswer(i18nc("@option:radio The capital of is...", "%1", p_capital)); + p_radioButtons[i] -> setText(i18nc("@option:radio The capital of is...", "%1", p_capital)); b = true; QStringList falseCapitals = p_map -> getDivisionFalseCapitals(division); int index = 0; while (!falseCapitals.isEmpty() && index < 4) { if (index != i) { int random = (int)((float)falseCapitals.size() * KRandom::random() / (RAND_MAX + 1.0)); QString falseCapital = falseCapitals.at(random); falseCapitals.removeAt(random); p_radioButtons[index] -> setText(i18nc("@option:radio The capital of is...", "%1", i18nc(p_map -> getFileName().toUtf8(), falseCapital.toUtf8()))); } ++index; } } else { QString capital; capital = p_map -> getDivisionCapital(division); // There are crazy countries like Norway where two different divisions // have the same capital, avoid asking // The capital of Akershus is ... // Oslo (true) // Oslo (true) // Lillehammer // Moss if (capital != p_capital) { - p_radioButtons[i] -> setText(i18nc("@option:radio The capital of is...", "%1", i18nc(p_map -> getFileName().toUtf8(), capital.toUtf8()))); + p_radioButtons[i] -> setText(i18nc("@option:radio The capital of is...", "%1", capital)); b = true; } else b = false; } return b; } void divisionCapitalAsker::setAnswerHook(int userSays) { p_currentAnswer.setAnswer(p_radioButtons[userSays] -> text()); } QString divisionCapitalAsker::getQuestionHook() const { return i18nc("@title", p_map->getDivisionToCapitalTitle().toUtf8()); } diff --git a/src/mapasker.cpp b/src/mapasker.cpp index 971b516..d9d5553 100644 --- a/src/mapasker.cpp +++ b/src/mapasker.cpp @@ -1,257 +1,257 @@ /*************************************************************************** * 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 "mapasker.h" #include #include #include #include #include #include #include #include "map.h" #include "mapwidget.h" #include "settings.h" static QString guessWikipediaDomain() { QLocale systemLocale = QLocale::system(); QString languageCode; QLocale::Language systemLanguage = systemLocale.language(); if ( systemLanguage == QLocale::AnyLanguage || systemLanguage == QLocale::C ) { languageCode = QStringLiteral("en"); } else { languageCode = systemLocale.name().split('_').first(); } return QStringLiteral( "https://%1.wikipedia.org/wiki/" ).arg( languageCode ); } mapAsker::mapAsker(QWidget *parent, KGmap *m, QWidget *w, bool asker, uint count) : askWidget(parent, m, w, count, asker), p_asker(asker), p_firstShow(true) { QVBoxLayout *lay = new QVBoxLayout(this); lay -> setContentsMargins(0, 0, 0, 0); lay -> setSpacing(0); p_mapWidget = new mapWidget(this); p_popupManager.setWidget(p_mapWidget->viewport()); lay -> addWidget(p_mapWidget); p_shouldClearPopup = false; connect(p_mapWidget, &mapWidget::clicked, this, &mapAsker::handleMapClick); connect(p_mapWidget, &mapWidget::setMoveActionChecked, this, &mapAsker::setMoveActionChecked); connect(p_mapWidget, &mapWidget::setZoomActionChecked, this, &mapAsker::setZoomActionChecked); connect(p_mapWidget, &mapWidget::setMoveActionEnabled, this, &mapAsker::setMoveActionEnabled); if (asker) { QVBoxLayout *vbl = static_cast(w -> layout()); p_next = new QLabel(w); p_next -> setAlignment(Qt::AlignTop | Qt::AlignHCenter); p_next -> setWordWrap(true); p_fill = new QWidget(w); p_fill -> show(); vbl -> addWidget(p_next); vbl -> addWidget(p_fill, 1); nextQuestion(); } else { p_next = 0; p_fill = 0; } } mapAsker::~mapAsker() { delete p_next; delete p_fill; } bool mapAsker::isAsker() const { return p_answers; } void mapAsker::mousePressEvent(QMouseEvent*) { p_popupManager.clear(); } void mapAsker::setMovement(bool b) { p_mapWidget -> setMapMove(b); p_popupManager.clear(); } void mapAsker::setZoom(bool b) { askWidget::setZoom(b); p_mapWidget -> setMapZoom(b); p_popupManager.clear(); } void mapAsker::setOriginalZoom() { p_mapWidget -> setOriginalImage(); p_popupManager.clear(); } void mapAsker::setAutomaticZoom(bool automaticZoom) { p_mapWidget -> setAutomaticZoom(automaticZoom); p_popupManager.clear(); } void mapAsker::handleMapClick(QRgb c, const QPoint &p) { QRgb colorSeen = c; QString correctDivision; QRgb correctRgb; if ( p_asker ) { correctDivision = lastDivisionAsked(); correctRgb = p_map -> getColor(correctDivision).rgb(); if ( ! p_shuffledColormap.empty() ) { int i = p_originalColormap.indexOf(correctRgb); correctRgb = p_shuffledColormap[i]; i = p_shuffledColormap.indexOf(c); c = p_originalColormap[i]; } } QString aux, cap; aux = p_map -> getWhatIs(c, !p_asker); if (aux == QLatin1String("nothing")) KMessageBox::error(this, i18nc("@info", "You have found a bug in a map. Please contact the author and tell the %1 map has nothing associated to color %2,%3,%4.", p_map -> getFile(), qRed(c), qGreen(c), qBlue(c))); else if (p_shouldClearPopup) { p_popupManager.clear(); p_shouldClearPopup = false; } else if (!p_asker) { QString flagFile = p_map -> getDivisionFlagFile(aux); if (p_map -> getDivisionCanAsk(aux, division::eCapital)) cap = p_map -> getDivisionCapital(aux); - if (!cap.isEmpty()) cap = i18nc("@item Capital name in map popup", "%1", i18nc(p_map -> getFileName().toUtf8(), cap.toUtf8())); + if (!cap.isEmpty()) cap = i18nc("@item Capital name in map popup", "%1", cap); QString wikiLink (guessWikipediaDomain()); wikiLink.append(i18nc(p_map -> getFileName().toUtf8(), aux.toUtf8())); if (!p_map -> getDivisionCanAsk(aux, division::eClick)) wikiLink = QLatin1String(""); aux = i18nc("@item Region name in map popup", "%1", i18nc(p_map -> getFileName().toUtf8(), aux.toUtf8())); if (!flagFile.isEmpty()) p_popupManager.show(aux, wikiLink, cap, p, flagFile); else if (!cap.isEmpty()) p_popupManager.show(aux, wikiLink, cap , p); else p_popupManager.show(aux, wikiLink, p); } else if (!aux.isEmpty()) { p_currentAnswer.setCorrectAnswer(QColor(correctRgb)); QVariantList vAnswer; QColor vColor = QColor(colorSeen); QRgb vColorRgb = vColor.toRgb().rgb(); vAnswer.append(vColor); QString vDivisionName; QRgb origColor; division * vDivision; if (kgeographySettings::self()->colorDisguise() == kgeographySettings::EnumColorDisguise::Scramble) { int i = p_shuffledColormap.indexOf(vColorRgb); origColor = p_originalColormap[i]; vDivision = p_map->getDivision(origColor); } else { vDivision = p_map->getDivision(vColorRgb); } vDivisionName = i18nc(p_map -> getFileName().toUtf8(), vDivision->getName().toUtf8()); vAnswer.append(vDivisionName); p_currentAnswer.setAnswer(vAnswer); questionAnswered(aux == correctDivision); nextQuestion(); } } void mapAsker::nextQuestionHook(const QString &division) { QString divisionName = i18nc(p_map -> getFileName().toUtf8(), division.toUtf8()); p_next -> setText(i18nc("@info:status", "Please click on:
%1", divisionName)); p_currentAnswer.setQuestion(i18nc("@item:intable column Question, %1 is region name", "%1", i18nc(p_map -> getFileName().toUtf8(), division.toUtf8()))); p_next -> show(); QRgb c = p_map -> getColor(division).rgb(); p_currentAnswer.setCorrectAnswer(QColor(c)); } QString mapAsker::getQuestionHook() const { QString divisionType = i18nc(p_map -> getFileName().toUtf8(), p_map->getDivisionsString().toUtf8()); return i18nc("@title", "%1 in Map", divisionType); } void mapAsker::showEvent(QShowEvent *) { if (p_firstShow) { bool isForAsking = p_next != NULL; QImage image; image.load(p_map->getMapFile()); if ( isForAsking && kgeographySettings::self()->colorDisguise() == kgeographySettings::EnumColorDisguise::Scramble ) { QVector colormap = image.colorTable(); p_originalColormap = colormap; QVector swapableIndexes; QList divisions = p_map->getAllDivisionsOrdered(); foreach(const division *id, divisions) { if ( id->canAsk(division::eClick) ) { const QRgb rgb = id->getRGB(); const int colorIdx = colormap.indexOf(rgb); swapableIndexes << colorIdx; } } QVector shuffling = swapableIndexes; const int n = swapableIndexes.size(); for ( int i = 2; i < n ; ++i ) { int o = int(float(i) * KRandom::random() / (RAND_MAX + 1.0)); int ci = shuffling[i]; int co = shuffling[o]; qSwap(shuffling[i], shuffling[o]); qSwap(colormap[ci], colormap[co]); } p_shuffledColormap = colormap; image.setColorTable(colormap); } p_mapWidget -> init(image); p_firstShow = false; } } QSize mapAsker::mapSize() const { return p_mapWidget -> mapSize(); } diff --git a/src/mapparser.cpp b/src/mapparser.cpp index fedd00d..968d9da 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) ); // 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(divisionKindName); 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(); } 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(); } QString title = getElementString(QStringLiteral("capitalToDivisionTitle"), root, Optional); if ( ! title.isEmpty() ) { kgmap->setCapitalToDivisionTitle(title); } else { kgmap->setCapitalToDivisionTitle(i18n("%1 by Capital", i18nc(ctxt, divisionKindName.toUtf8()))); } title = getElementString(QStringLiteral("divisionToCapitalTitle"), root, Optional); if ( ! title.isEmpty() ) { kgmap->setDivisionToCapitalTitle(title); } else { kgmap->setDivisionToCapitalTitle(i18n("Capitals of %1", i18nc(ctxt, divisionKindName.toUtf8()))); } // 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.isNull()) kgdiv -> setCapital( capital ); + 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; }