Commit 2f4bdcdf authored by Pino Toscano's avatar Pino Toscano
Browse files

Switch askWidget/boxAsker hooks to division class

Instead of pass around the untranslated name of a division, pass the
division pointer instead: this way the hooks will not need to look it up
over and over.

There should be no behaviour change.
parent fdc0f476
......@@ -84,7 +84,8 @@ void askWidget::nextQuestion()
aux = p_map -> getRandomDivision(askMode());
while (p_asked.indexOf(aux) != -1) aux = p_map -> getRandomDivision(askMode());
p_asked << aux;
nextQuestionHook(aux);
const division *div = p_map -> getDivision(aux);
nextQuestionHook(div);
}
else emit questionsEnded();
}
......
......@@ -50,7 +50,7 @@ Q_OBJECT
void clearAsked();
QString lastDivisionAsked();
void nextQuestion();
virtual void nextQuestionHook(const QString &division) = 0;
virtual void nextQuestionHook(const division *div) = 0;
virtual division::askMode askMode() const = 0;
void questionAnswered(bool wasCorrect);
void resetAnswers();
......
......@@ -175,7 +175,7 @@ void boxAsker::keyReleaseEvent(QKeyEvent *e)
else askWidget::keyReleaseEvent(e);
}
void boxAsker::nextQuestionHook(const QString &division)
void boxAsker::nextQuestionHook(const division *div)
{
QString otherDivision;
QStringList auxList;
......@@ -189,11 +189,11 @@ void boxAsker::nextQuestionHook(const QString &division)
p_accept -> setEnabled(false);
auxList << division;
auxList << div -> getUntranslatedName ();
// we put the division in a random place
p_position = (int)((float)NB_CHOICES * KRandom::random() / (RAND_MAX + 1.0));
nextBoxAskerQuestionHook(division, p_position, true);
nextBoxAskerQuestionHook(div, p_position, true);
// fill the other names
j = 0;
......@@ -204,7 +204,8 @@ void boxAsker::nextQuestionHook(const QString &division)
otherDivision = p_map -> getRandomDivision(askMode());
while (auxList.contains(otherDivision))
otherDivision = p_map -> getRandomDivision(askMode());
if (nextBoxAskerQuestionHook(otherDivision, j, false))
const division *otherDiv = p_map -> getDivision(otherDivision);
if (nextBoxAskerQuestionHook(otherDiv, j, false))
++j;
auxList << otherDivision;
}
......
......@@ -32,8 +32,8 @@ Q_OBJECT
void updateLayout();
protected:
virtual bool nextBoxAskerQuestionHook(const QString &division, int i, bool isAnswer) = 0;
void nextQuestionHook(const QString &division) override;
virtual bool nextBoxAskerQuestionHook(const division *div, int i, bool isAnswer) = 0;
void nextQuestionHook(const division *div) override;
void setQuestion(const QString &q);
virtual void setAnswerHook(int userSays) = 0;
......
......@@ -21,13 +21,13 @@ capitalDivisionAsker::capitalDivisionAsker(QWidget *parent, KGmap *m, QWidget *w
init();
}
bool capitalDivisionAsker::nextBoxAskerQuestionHook(const QString &division, int i, bool isAnswer)
bool capitalDivisionAsker::nextBoxAskerQuestionHook(const division *div, int i, bool isAnswer)
{
bool b;
if (isAnswer)
{
const QString divisionName = p_map -> getDivision(division) -> getName();
p_capital = p_map -> getDivisionCapital(division);
const QString divisionName = div -> getName();
p_capital = div -> getCapital();
p_currentAnswer.setQuestion(i18nc("@item:intable column Question, %1 is capital name", "%1", p_capital));
p_currentAnswer.setCorrectAnswer(i18nc("@option:radio <City> is the capital of...", "%1", divisionName));
......@@ -44,9 +44,9 @@ bool capitalDivisionAsker::nextBoxAskerQuestionHook(const QString &division, int
// Akershus (true)
// Buskerud
// Hedmark
if (p_map -> getDivisionCapital(division) != p_capital)
if (div -> getCapital() != p_capital)
{
const QString divisionName = p_map -> getDivision(division) -> getName();
const QString divisionName = div -> getName();
p_radioButtons[i] -> setText(i18nc("@option:radio <City> is the capital of...", "%1", divisionName));
b = true;
}
......
......@@ -20,7 +20,7 @@ class capitalDivisionAsker : public boxAsker
capitalDivisionAsker(QWidget *parent, KGmap *m, QWidget *w, uint count);
protected:
bool nextBoxAskerQuestionHook(const QString &division, int i, bool isAnswer) override;
bool nextBoxAskerQuestionHook(const division *div, int i, bool isAnswer) override;
void setAnswerHook(int userSays) override;
QString getQuestionHook() const override;
division::askMode askMode() const override { return division::eCapital; }
......
......@@ -22,20 +22,20 @@ divisionCapitalAsker::divisionCapitalAsker(QWidget *parent, KGmap *m, QWidget *w
init();
}
bool divisionCapitalAsker::nextBoxAskerQuestionHook(const QString &division, int i, bool isAnswer)
bool divisionCapitalAsker::nextBoxAskerQuestionHook(const division *div, int i, bool isAnswer)
{
bool b;
if (isAnswer)
{
const QString divisionName = p_map -> getDivision(division) -> getName();
const QString divisionName = div -> getName();
setQuestion(p_map->getDivisionToCapitalQuestionPattern().subs(divisionName).toString());
p_currentAnswer.setQuestion(i18nc("@item:intable column Question, %1 is region name", "%1", divisionName));
p_capital = p_map -> getDivisionCapital(division);
p_capital = div -> getCapital();
p_currentAnswer.setCorrectAnswer(i18nc("@option:radio The capital of <Region> is...", "%1", p_capital));
p_radioButtons[i] -> setText(i18nc("@option:radio The capital of <Region> is...", "%1", p_capital));
b = true;
QStringList falseCapitals = p_map -> getDivisionFalseCapitals(division);
QStringList falseCapitals = div -> getFalseCapitals();
int index = 0;
while (!falseCapitals.isEmpty() && index < 4)
{
......@@ -52,7 +52,7 @@ bool divisionCapitalAsker::nextBoxAskerQuestionHook(const QString &division, int
else
{
QString capital;
capital = p_map -> getDivisionCapital(division);
capital = div -> getCapital();
// There are crazy countries like Norway where two different divisions
// have the same capital, avoid asking
// The capital of Akershus is ...
......
......@@ -19,7 +19,7 @@ class divisionCapitalAsker : public boxAsker
divisionCapitalAsker(QWidget *parent, KGmap *m, QWidget *w, uint count);
protected:
bool nextBoxAskerQuestionHook(const QString &division, int i, bool isAnswer) override;
bool nextBoxAskerQuestionHook(const division *div, int i, bool isAnswer) override;
void setAnswerHook(int userSays) override;
QString getQuestionHook() const override;
division::askMode askMode() const override { return division::eCapital; }
......
......@@ -22,13 +22,13 @@ divisionFlagAsker::divisionFlagAsker(QWidget *parent, KGmap *m, QWidget *w, uint
init();
}
bool divisionFlagAsker::nextBoxAskerQuestionHook(const QString &division, int i, bool isAnswer)
bool divisionFlagAsker::nextBoxAskerQuestionHook(const division *div, int i, bool isAnswer)
{
QSize pixmapSize(60, 40);
QIcon icon(p_map -> getDivisionFlagFile(division));
QIcon icon(div -> getFlagFile());
if (isAnswer)
{
const QString divisionName = p_map -> getDivision(division) -> getName();
const QString divisionName = div -> getName();
setQuestion(i18nc("@title:group", "The flag of %1 is...", divisionName));
p_currentAnswer.setCorrectAnswer(icon.pixmap(QSize(60, 40)));
p_currentAnswer.setQuestion(i18nc("@item:intable column Question, %1 is region name", "%1", divisionName));
......
......@@ -19,7 +19,7 @@ class divisionFlagAsker : public boxAsker
divisionFlagAsker(QWidget *parent, KGmap *m, QWidget *w, uint count);
protected:
bool nextBoxAskerQuestionHook(const QString &division, int i, bool isAnswer) override;
bool nextBoxAskerQuestionHook(const division *div, int i, bool isAnswer) override;
void setAnswerHook(int userSays) override;
QString getQuestionHook() const override;
division::askMode askMode() const override { return division::eFlag; }
......
......@@ -45,27 +45,26 @@ flagDivisionAsker::flagDivisionAsker(QWidget *parent, KGmap *m, QWidget *w, uint
init();
}
bool flagDivisionAsker::nextBoxAskerQuestionHook(const QString &division, int i, bool isAnswer)
bool flagDivisionAsker::nextBoxAskerQuestionHook(const division *div, int i, bool isAnswer)
{
const class division *vDivision = p_map -> getDivision(division);
if (isAnswer)
{
// we put the flag image
QImage image(vDivision -> getFlagFile());
QImage image(div -> getFlagFile());
p_flag -> img = image;
p_flag -> setMinimumSize(image.size());
p_flag -> update();
p_currentAnswer.setQuestion(QPixmap::fromImage(image.scaled(image.width()/5, image.height()/5, Qt::IgnoreAspectRatio, Qt::SmoothTransformation)));
p_currentAnswer.setCorrectAnswer(i18nc("@option:radio This flag belongs to:", "%1", vDivision -> getName()));
p_currentAnswer.setCorrectAnswer(i18nc("@option:radio This flag belongs to:", "%1", div -> getName()));
}
else
{
// There are some maps like the Pacific one where two divisions have the same flag
QImage image(vDivision -> getFlagFile());
QImage image(div -> getFlagFile());
if (p_flag -> img == image) return false;
}
p_radioButtons[i] -> setText(i18nc("@option:radio This flag belongs to:", "%1", vDivision -> getName()));
p_radioButtons[i] -> setText(i18nc("@option:radio This flag belongs to:", "%1", div -> getName()));
return true;
}
......
......@@ -21,7 +21,7 @@ class flagDivisionAsker : public boxAsker
flagDivisionAsker(QWidget *parent, KGmap *m, QWidget *w, uint count);
protected:
bool nextBoxAskerQuestionHook(const QString &division, int i, bool isAnswer) override;
bool nextBoxAskerQuestionHook(const division *div, int i, bool isAnswer) override;
void setAnswerHook(int userSays) override;
QString getQuestionHook() const override;
division::askMode askMode() const override { return division::eFlag; }
......
......@@ -188,13 +188,13 @@ void mapAsker::handleMapClick(QRgb c, const QPoint &p)
}
}
void mapAsker::nextQuestionHook(const QString &division)
void mapAsker::nextQuestionHook(const division *div)
{
const QString divisionName = p_map -> getDivision(division) -> getName();
const QString divisionName = div -> getName();
p_next -> setText(i18nc("@info:status", "Please click on:<br/><b>%1</b>", divisionName));
p_currentAnswer.setQuestion(i18nc("@item:intable column Question, %1 is region name", "%1", divisionName));
p_next -> show();
QRgb c = p_map -> getColor(division).rgb();
QRgb c = div -> getRGB();
p_currentAnswer.setCorrectAnswer(QColor(c));
}
......
......@@ -31,7 +31,7 @@ Q_OBJECT
bool isAsker() const;
protected:
void nextQuestionHook(const QString &division) override;
void nextQuestionHook(const division *div) override;
void mousePressEvent(QMouseEvent *e) override;
QString getQuestionHook() const override;
void showEvent(QShowEvent *e) override;
......
......@@ -184,16 +184,16 @@ void placeAsker::handleMapClick(QRgb c, const QPoint & , const QPointF &mapPoint
}
}
void placeAsker::nextQuestionHook(const QString &division)
void placeAsker::nextQuestionHook(const division *div)
{
const QString divisionName = p_map -> getDivision(division) -> getName();
const QString divisionName = div -> getName();
p_next -> setText(i18nc("@info:status", "Please place in the map:<br/><b>%1</b>", divisionName));
p_next -> show();
p_currentAnswer.setQuestion(i18nc("@item:intable column Question, %1 is region name", "%1", divisionName));
QColor color = p_map -> getColor(division);
QColor color = QColor(div -> getRGB());
p_currentRgb = color.rgb();
p_currentAnswer.setCorrectAnswer(color);
setCurrentDivision(division);
setCurrentDivision(div);
p_mapWidget->setCurrentDivisionImage(p_currentDivisionImage);
}
......@@ -208,7 +208,7 @@ QSize placeAsker::mapSize() const
return p_mapWidget -> mapSize();
}
void placeAsker::setCurrentDivision(const QString& division)
void placeAsker::setCurrentDivision(const division *div)
{
int width = p_mapImage->width();
int height = p_mapImage->height();
......@@ -218,7 +218,7 @@ void placeAsker::setCurrentDivision(const QString& division)
int minY = height;
int maxY = 0;
QRgb divColor = p_map->getColor(division).rgb();
QRgb divColor = div -> getRGB();
//first iteration, detect size required by the image
for (int x = 0; x < width; x++)
......
......@@ -33,7 +33,7 @@ Q_OBJECT
bool isAsker() const;
protected:
void nextQuestionHook(const QString &division) override;
void nextQuestionHook(const division *div) override;
void mousePressEvent(QMouseEvent *e) override;
QString getQuestionHook() const override;
division::askMode askMode() const override { return division::eClick; }
......@@ -48,7 +48,7 @@ Q_OBJECT
void handleMapClick(QRgb c, const QPoint &, const QPointF &mapPoint);
private:
void setCurrentDivision(const QString& division);
void setCurrentDivision(const division *div);
QLabel *p_next;
QWidget *p_fill;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment