File indexing completed on 2024-04-21 03:41:49

0001 /*
0002     RatioWidget.cpp  -  paint a ratio
0003     SPDX-FileCopyrightText: 2004 Sebastian Stein <seb.kde@hpfsc.de>
0004     SPDX-FileCopyrightText: 2008 Tadeu Araujo <tadeu.araujo@ltia.fc.unesp.br>
0005     SPDX-FileCopyrightText: 2008 Danilo Balzaque <danilo.balzaque@ltia.fc.unesp.br>
0006 
0007     SPDX-License-Identifier: GPL-2.0-or-later
0008 */
0009 
0010 #include "ResultWidget.h"
0011 
0012 /* these includes are needed for KDE support */
0013 #include <KLocalizedString>
0014 
0015 /* these includes are needed for Qt support */
0016 #include <QLabel>
0017 #include <QPainter>
0018 #include <QPaintEvent>
0019 #include <QGridLayout>
0020 
0021 #ifdef DEBUG
0022 #include <QDebug>
0023 #endif
0024 
0025 #include "settingsclass.h"
0026 
0027 ResultWidget::ResultWidget(QWidget * parent, const uintList &para_factors) :
0028     FractionBaseWidget(parent), m_factors(para_factors)
0029 {
0030 #ifdef DEBUG
0031     qDebug() << QStringLiteral("constructor ResultWidget");
0032 #endif
0033     Init();
0034     m_ExerciseView = 0;
0035 }
0036 
0037 ResultWidget::ResultWidget(QWidget * parent,
0038                            const Ratio &para_result) :
0039     FractionBaseWidget(parent), m_result(para_result)
0040 {
0041 #ifdef DEBUG
0042     qDebug() << QStringLiteral("constructor ResultWidget");
0043 #endif
0044     Init();
0045     m_ExerciseView = 1;
0046 }
0047 
0048 ResultWidget::ResultWidget(QWidget * parent) :
0049     FractionBaseWidget(parent)
0050 {
0051 #ifdef DEBUG
0052     qDebug() << QStringLiteral("constructor ResultWidget");
0053 #endif
0054     Init();
0055     m_ExerciseView = 2;
0056 }
0057 
0058 void ResultWidget::setFactors(const uintList &para_factors)
0059 {
0060     m_factors = para_factors;
0061     update();
0062 }
0063 
0064 void ResultWidget::Init()
0065 {
0066 #ifdef DEBUG
0067     qDebug() << QStringLiteral("ResultWidget::Init()");
0068 #endif
0069     setMinimumWidth(160);
0070     setMaximumHeight(213);
0071     setMinimumHeight(213);
0072 
0073     m_answerMixed = true;
0074 
0075     m_kindView = -1;
0076 
0077     defaultFont = SettingsClass::taskFont();
0078     defaultFont.setBold(true);
0079 
0080     QPalette pal = palette();
0081     pal.setColor(QPalette::WindowText, Qt::white);
0082     setPalette(pal);
0083 
0084     m_primaryText = new QLabel(this);
0085     m_primaryText->setObjectName(QStringLiteral("primaryText"));
0086     m_primaryText->setPalette(pal);
0087     defaultFont.setPointSize(20);
0088     m_primaryText->setMinimumHeight(75);
0089     m_primaryText->setAlignment(Qt::AlignCenter);
0090     m_primaryText->setFont(defaultFont);
0091     m_primaryText->hide();
0092 
0093     m_secondaryText = new QLabel(i18n("Solution:"), this);
0094     m_secondaryText->setObjectName(QStringLiteral("secondaryText"));
0095     m_secondaryText->setPalette(pal);
0096     defaultFont.setPointSize(10);
0097     m_secondaryText->setAlignment(Qt::AlignCenter);
0098     m_secondaryText->setAlignment(Qt::AlignVCenter | Qt::AlignLeft);
0099     m_secondaryText->setFont(defaultFont);
0100     m_secondaryText->hide();
0101 
0102     defaultFont.setPointSize(20);
0103 
0104     layout = new QGridLayout(this);
0105     layout->addWidget(m_primaryText, 0, 0);
0106     layout->addWidget(m_secondaryText, 1, 0);
0107     setLayout(layout);
0108 }
0109 
0110 ResultWidget::~ResultWidget()
0111 {
0112 #ifdef DEBUG
0113     qDebug() << QStringLiteral("destructor ResultWidget");
0114 #endif
0115 }
0116 
0117 int ResultWidget::KindView()
0118 {
0119     return m_kindView;
0120 }
0121 
0122 void ResultWidget::showResult()
0123 {
0124 #ifdef DEBUG
0125     qDebug() << QStringLiteral("ResultWidget::showResult");
0126 #endif
0127     switch (m_kindView) {
0128     case 1:
0129         m_primaryText->show();
0130         m_secondaryText->hide();
0131         m_primaryText->setText(i18n("Correct!"));
0132         layout->setRowStretch(2, 0);
0133         layout->setRowStretch(0, 1);
0134         break;
0135     case 0:
0136         m_primaryText->show();
0137         m_secondaryText->show();
0138         m_primaryText->setText(i18n("Incorrect!"));
0139         layout->setRowStretch(2, 1);
0140         layout->setRowStretch(0, 0);
0141         break;
0142     case 2:
0143         m_primaryText->show();
0144         m_secondaryText->hide();
0145         m_primaryText->setText(i18n("Incorrect!"));
0146         layout->setRowStretch(2, 0);
0147         layout->setRowStretch(0, 1);
0148         break;
0149     default:
0150         setFixedSize(160, 213);
0151         m_primaryText->hide();
0152         m_secondaryText->hide();
0153     }
0154     update();
0155 }
0156 
0157 void ResultWidget::setResult(const Ratio &para_result, int k)
0158 {
0159 #ifdef DEBUG
0160     qDebug() << QStringLiteral("ResultWidget::setResult");
0161     qDebug() << ("m_kindView = ") + k;
0162 #endif
0163     m_kindView = k;
0164     m_result = para_result;
0165 
0166     showResult();
0167 }
0168 
0169 void ResultWidget::setAnswerMixed(bool value)
0170 {
0171 #ifdef DEBUG
0172     qDebug() << QStringLiteral("ResultWidget::setAnswerMixed");
0173 #endif
0174     m_answerMixed = value;
0175 }
0176 
0177 void ResultWidget::setResult(int k)
0178 {
0179 #ifdef DEBUG
0180     qDebug() << QStringLiteral("ResultWidget::setResult");
0181     qDebug() << QStringLiteral("m_kindView = ") + k;
0182 #endif
0183     m_kindView = k;
0184     showResult();
0185 }
0186 
0187 void ResultWidget::paintEvent(QPaintEvent* /* p_paintEvent */)
0188 {
0189     // our x position, we paint from left to right;
0190     // we don't want to start directly on the border, so add the margin
0191     int old_x = 30;
0192     int old_y = 123;
0193     int fontWidth; // to store the width of the last thing painted
0194 
0195     // start the painter
0196     QPainter paint(this);
0197 
0198     paint.setRenderHint(QPainter::Antialiasing);
0199     paint.setPen(QPen(Qt::NoPen));
0200     switch (m_kindView) {
0201     case 2:
0202         paint.setBrush(QColor(191, 0, 0));
0203         paint.drawRoundedRect(2.0, 2.0, 157.0, 209.0, 10, 10);
0204         break;
0205     case 1:
0206         paint.setBrush(QColor(0, 191, 0));
0207         paint.drawRoundedRect(2.0, 2.0, 157.0, 209.0, 10, 10);
0208         break;
0209     case 0:
0210         paint.setBrush(QColor(191, 0, 0));
0211         paint.drawRoundedRect(2.0, 2.0, width() - 3, 209.0, 10, 10);  // 2.0, 2.0, 157.0, 209.0, 10, 10
0212         paint.setBrush(QColor(255, 255, 255));
0213         paint.drawRoundedRect(8.0, 110.0, width() - 15, 93.0, 10, 10);  // 8.0, 110.0, 145.0, 93.0, 10, 10
0214 
0215         // ratios and operation signs are painted with the same font
0216         paint.setFont(defaultFont);
0217 
0218         // set the pen for painting
0219         QPen pen(Qt::SolidLine);
0220         pen.setWidth(0);
0221         paint.setPen(pen);
0222 
0223         // get the font height; the font height doesn't change while painting
0224         QFontMetrics fm(paint.fontMetrics());
0225 
0226         // m_ExerciseView != 1 only for exercise factorize!
0227         if (m_ExerciseView == 1) {
0228             if (SettingsClass::showSpecialRatioNotation() == true &&
0229                     qAbs(m_result.numerator()) >= qAbs(m_result.denominator()) &&
0230                     m_result.denominator() != 1 &&
0231                     m_answerMixed == true) {
0232                 paintRatio(paint, m_result, old_x, old_y, fm, true, true, true);
0233             } else {
0234                 paintRatio(paint, m_result, old_x, old_y, fm, false, true, true);
0235             }
0236         } else {
0237             // show solution of a factorization exercise
0238             old_y += 20;
0239             QString tmpStr;
0240             int fontHeight = fm.lineSpacing(); // get the font height
0241 
0242             int tmpWidth = 0;
0243             for (int tmpInt = 0; tmpInt < m_factors.count(); tmpInt++) {
0244                 if (tmpInt != 0) {
0245                     tmpWidth += fm.boundingRect(QStringLiteral("*")).width();
0246                 }
0247                 tmpStr.setNum(m_factors[tmpInt]);
0248                 fontWidth = fm.boundingRect(tmpStr).width();
0249                 tmpWidth += fontWidth;
0250             }
0251             if (tmpWidth <= 100)
0252                 old_x += 50 - tmpWidth / 2;
0253 
0254             for (int tmpInt = 0; tmpInt < m_factors.count(); tmpInt++) {
0255                 if (tmpInt != 0) {
0256                     fontWidth = fm.boundingRect(QStringLiteral("*")).width();
0257                     paint.drawText(old_x, old_y, fontWidth, fontHeight, Qt::AlignCenter, QStringLiteral("*"));
0258                     old_x += fontWidth;
0259                 }
0260 
0261                 tmpStr.setNum(m_factors[tmpInt]);
0262 
0263                 fontWidth = fm.boundingRect(tmpStr).width();
0264                 paint.drawText(old_x, old_y, fontWidth, fontHeight, Qt::AlignCenter, tmpStr);
0265                 old_x += fontWidth;
0266             }
0267         }
0268         old_x += 30;
0269         if (old_x > 160)
0270             setMinimumWidth(old_x);
0271         break;
0272     }
0273     // stop the painter
0274     paint.end();
0275 
0276     return;
0277 }
0278 
0279 #include "moc_ResultWidget.cpp"