File indexing completed on 2022-12-06 18:52:36

0001 /*
0002     TaskWidget.cpp  -  paint a task
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 "TaskWidget.h"
0011 
0012 /* these includes are needed for KDE support */
0013 #include <KLocalizedString>
0014 
0015 /* these includes are needed for Qt support */
0016 #include <QPainter>
0017 #include <QPaintEvent>
0018 
0019 #ifdef DEBUG
0020 #include <QDebug>
0021 #endif
0022 
0023 TaskWidget::TaskWidget(QWidget * parent,
0024                        const Task &para_task) :
0025     FractionBaseWidget(parent), m_task(para_task)
0026 {
0027 #ifdef DEBUG
0028     qDebug() << QStringLiteral("constructor TaskWidget");
0029 #endif
0030 }
0031 
0032 TaskWidget::~TaskWidget()
0033 {
0034 #ifdef DEBUG
0035     qDebug() << QStringLiteral("destructor TaskWidget");
0036 #endif
0037 }
0038 
0039 void TaskWidget::setTask(const Task &para_task)
0040 {
0041     m_task = para_task;
0042     update();
0043 }
0044 
0045 void TaskWidget::setQuestionMixed(bool value)
0046 {
0047     m_questionMixed = value;
0048     update();
0049 }
0050 
0051 void TaskWidget::paintEvent(QPaintEvent* /* p_paintEvent */)
0052 {
0053     // our x position, we paint from left to right;
0054     // we don't want to start directly on the border, so add the margin
0055     int old_x = _MARGIN_X;
0056     int old_y = 0;
0057 
0058     // strings holding numerator, denominator and the operation sign
0059     QString str_operation;
0060 
0061     // operation sign as number
0062     short tmp_operation;
0063 
0064     // start the painter
0065     QPainter paint(this);
0066 
0067     // ratios and operation signs are painted with the same font
0068     paint.setFont(m_font);
0069 
0070     // set the pen for painting
0071     QPen pen(Qt::SolidLine);
0072     pen.setWidth(0);
0073     paint.setPen(pen);
0074 
0075     // get the font height; the font height doesn't change while painting
0076     QFontMetrics  fm(paint.fontMetrics());
0077 
0078     // now we can correctly set the height of the widget
0079     setMinimumHeight(2 * fm.lineSpacing() + 10);
0080     setMaximumHeight(2 * fm.lineSpacing() + 10);
0081 
0082     // loop through all ratios and paint them
0083     for (unsigned short tmp_counter = 0; tmp_counter < m_task.getNumberOfRatios(); tmp_counter++) {
0084         // get the current ratio and paint it
0085         paintRatio(paint, m_task.get_ratio_n(tmp_counter), old_x, old_y, fm, m_questionMixed);
0086 
0087         // now check if we have another operation to show
0088         // if not we will stop showing ratios as well
0089         if (tmp_counter < m_task.getNumberOfOperations()) {
0090             // get the operation sign
0091             tmp_operation = m_task.get_op_n(tmp_counter);
0092 
0093             // we have to convert the operation sign into a string
0094             switch (tmp_operation) {
0095             case ADD :
0096                 str_operation = i18nc("addition symbol", "+");
0097                 break;
0098             case SUB :
0099                 str_operation = i18nc("subtraction symbol", "−");
0100                 break;
0101             case MUL :
0102                 str_operation = i18nc("multiplication symbol", "×");
0103                 break;
0104             case DIV :
0105                 str_operation = i18nc("division symbol", "/");
0106                 break;
0107             } /* switch (operation) */
0108 
0109             // add proper spacing around the operator
0110             str_operation = QStringLiteral(" ") + str_operation + QStringLiteral(" ");
0111 
0112             // paint the operation
0113             paintMiddle(paint, str_operation, old_x, old_y, fm, m_colorOperation);
0114 
0115         } else {
0116             // no further operations to show, so we always show the = sign at the
0117             // end of a task
0118             paintMiddle(paint, QStringLiteral(" = "), old_x, old_y, fm, m_colorOperation);
0119 
0120             break;
0121         } // if (tmp_counter < m_task.getNumberOfOperations())
0122     }
0123 
0124     // stop the painter
0125     paint.end();
0126 
0127     // the space we needed for painting is the minimum width of the widget
0128     setMinimumWidth(old_x);
0129 
0130     return;
0131 }