File indexing completed on 2024-04-28 07:29:10

0001 /***************************************************************************
0002  *   Copyright (C) 2004-2005 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 "flagdivisionasker.h"
0012 
0013 #include <KLocalizedString>
0014 
0015 #include <QImage>
0016 #include <QLayout>
0017 #include <QPainter>
0018 #include <QRadioButton>
0019 
0020 #include "map.h"
0021 #include "division.h"
0022 
0023 class flagWidget : public QWidget
0024 {
0025     public:
0026         flagWidget(QWidget *parent) : QWidget(parent)
0027         {
0028         }
0029 
0030         QImage img;
0031 
0032     protected:
0033         void paintEvent(QPaintEvent *) override
0034         {
0035             QPainter p(this);
0036             p.drawImage((width() - img.width()) / 2, 0, img);
0037         }
0038 };
0039 
0040 flagDivisionAsker::flagDivisionAsker(QWidget *parent, KGmap *m, QWidget *w, uint count) : boxAsker(parent, m, w, count)
0041 {
0042     p_flag = new flagWidget(this);
0043     setHeadWidget(p_flag);
0044     setQuestion(i18nc("@title:group", "This flag belongs to:"));
0045     init();
0046 }
0047 
0048 bool flagDivisionAsker::nextBoxAskerQuestionHook(const division *div, int i, bool isAnswer)
0049 {
0050     if (isAnswer)
0051     {
0052         // we put the flag image
0053         QImage image(div -> getFlagForQuestion());
0054         p_flag -> img = image;
0055         p_flag -> setMinimumSize(image.size());
0056         p_flag -> update();
0057         
0058         p_currentAnswer.setQuestion(QPixmap::fromImage(image.scaled(image.width()/5, image.height()/5, Qt::IgnoreAspectRatio, Qt::SmoothTransformation)));
0059         p_currentAnswer.setCorrectAnswer(i18nc("@option:radio This flag belongs to:", "%1", div -> getName()));
0060     }
0061     else
0062     {
0063         // There are some maps like the Pacific one where two divisions have the same flag
0064         QImage image(div -> getFlagForQuestion());
0065         if (p_flag -> img == image) return false;
0066     }
0067     p_radioButtons[i] -> setText(i18nc("@option:radio This flag belongs to:", "%1", div -> getName()));
0068     
0069     return true;
0070 }
0071 
0072 void flagDivisionAsker::setAnswerHook(int userSays)
0073 {
0074     p_currentAnswer.setAnswer(p_radioButtons[userSays] -> text());
0075 }
0076 
0077 QString flagDivisionAsker::getQuestionHook() const
0078 {
0079     QString divisionType = p_map->getDivisionsString();
0080     return i18nc("@title", "%1 by Flag", divisionType);
0081 }