File indexing completed on 2024-04-14 03:40:26

0001 /*
0002     FractionBaseWidget.cpp  -  base fraction painting
0003     SPDX-FileCopyrightText: 2004 Sebastian Stein <seb.kde@hpfsc.de>
0004     SPDX-FileCopyrightText: 2008 Tiago Porangaba <tiago.porangaba@ltia.fc.unesp.br>
0005     SPDX-FileCopyrightText: 2008 Tadeu Araujo <tadeu.araujo@ltia.fc.unesp.br>
0006     SPDX-FileCopyrightText: 2008 Danilo Balzaque <danilo.balzaque@ltia.fc.unesp.br>
0007 
0008     SPDX-License-Identifier: GPL-2.0-or-later
0009 */
0010 
0011 #include "FractionBaseWidget.h"
0012 
0013 /* these includes are needed for KDE support */
0014 
0015 /* these includes are needed for Qt support */
0016 #include <QPainter>
0017 
0018 #ifdef DEBUG
0019 #include <QDebug>
0020 #endif
0021 
0022 #include "settingsclass.h"
0023 
0024 FractionBaseWidget::FractionBaseWidget(QWidget * parent) :
0025     QWidget(parent)
0026 {
0027 #ifdef DEBUG
0028     qDebug() << "constructor FractionBaseWidget";
0029 #endif
0030 
0031     // set colors and font used for task displaying
0032     setColorAndFont();
0033 }
0034 
0035 FractionBaseWidget::~FractionBaseWidget()
0036 {
0037 #ifdef DEBUG
0038     qDebug() << "destructor FractionBaseWidget";
0039 #endif
0040 }
0041 
0042 void FractionBaseWidget::updateAndRepaint()
0043 {
0044     setColorAndFont();
0045     update();
0046 }
0047 
0048 void FractionBaseWidget::paintRatio(QPainter & paint, const Ratio &tmp_ratio, int & x_pos, int & y_pos, QFontMetrics & fm, bool show_mixed, bool addMargin, bool show_center)
0049 {
0050     QPen pen = paint.pen(); // get the pen
0051     int fontHeight = fm.lineSpacing(); // get the font height
0052 
0053     int int_numerator, int_denominator, int_mixed;
0054     QString str_numerator, str_denominator;
0055     QString str_mixed;
0056 
0057     int fontWidth; // to store the width of the last thing painted
0058     int tmp_int;
0059 
0060     // check if we have to show the ratio as mixed number
0061     //         11            1
0062     // if yes, -- becomes  2 -
0063     //         5             5
0064     int_numerator = tmp_ratio.numerator();
0065     int_denominator = tmp_ratio.denominator();
0066     if (show_mixed == true && qAbs(int_numerator) >= qAbs(int_denominator)) {
0067         // calculate the mixed number
0068         int_mixed = int (int_numerator / int_denominator);
0069 
0070         // the negative sign is in front of the mixed number
0071         int_numerator = qAbs(int_numerator);
0072         int_denominator = qAbs(int_denominator);
0073 
0074         // we have to reduce the numerator by the mixed number * denominator
0075         int_numerator = int_numerator % int_denominator;
0076 
0077         // now we can convert the numbers into strings for painting
0078         str_mixed.setNum(int_mixed);
0079         str_numerator.setNum(int_numerator);
0080         str_denominator.setNum(int_denominator);
0081 
0082         // paint the front part of the mixed number
0083         paintMiddle(paint, str_mixed, x_pos, y_pos, fm, m_colorNumber);
0084     } else {
0085 
0086         // don't show the ratio as mixed number
0087         str_numerator.setNum(int_numerator);
0088         str_denominator.setNum(int_denominator);
0089     } // if (show_mixed == true && qAbs(int_numerator) > qAbs(int_denominator))
0090 
0091     // get the text width of the current ratio
0092     fontWidth = fm.boundingRect(str_numerator).width();
0093     tmp_int = fm.boundingRect(str_denominator).width();
0094     if (tmp_int > fontWidth)
0095         fontWidth = tmp_int;
0096 
0097     // show numerator and denominator in m_colorNumber
0098     pen.setColor(m_colorNumber);
0099     paint.setPen(pen);
0100 
0101     // make sure we don't display something like:   0
0102     //                                            7 -
0103     //                                              3
0104     if (!(show_mixed == true && int_numerator == 0)) {
0105         if (show_center == true)
0106             x_pos = 80 - fontWidth / 2;
0107         if (!(int_denominator == 1 && show_mixed == false)) {
0108             // paint the numerator
0109             paint.drawText(x_pos, y_pos, fontWidth, fontHeight, Qt::AlignCenter, str_numerator);
0110             // paint the fraction line between numerator and denominator
0111             paint.fillRect(x_pos, y_pos + fontHeight + 4, fontWidth, 2, m_colorLine);
0112             // paint the denominator
0113             paint.drawText(x_pos, y_pos + fontHeight + 10, fontWidth, fontHeight, Qt::AlignCenter, str_denominator);
0114         } else {
0115             // paint the numerator and move the y position down to align with the + signal
0116             paint.drawText(x_pos, y_pos + fontHeight - fontHeight / 2, fontWidth,
0117                            fontHeight, Qt::AlignCenter, str_numerator);
0118         }
0119 
0120         // move the x position to the right by adding the width used for painting
0121         // the ratio and a margin
0122         x_pos += fontWidth;
0123 
0124         if (addMargin == true)
0125             x_pos += _MARGIN_X;
0126     }
0127 
0128     return;
0129 }
0130 
0131 void FractionBaseWidget::paintMiddle(QPainter & paint,
0132                                      const QString &paint_str,
0133                                      int & x_pos, int & y_pos, QFontMetrics & fm,
0134                                      const QColor &color, bool addMargin)
0135 {
0136     // get the pen, font height and font width
0137     QPen pen = paint.pen();
0138     int fontHeight = fm.lineSpacing();
0139     int fontWidth = fm.boundingRect(paint_str).width();
0140 
0141     // paint the string
0142     pen.setColor(color);
0143     paint.setPen(pen);
0144     paint.drawText(x_pos, y_pos + fontHeight - fontHeight / 2, fontWidth, fontHeight, Qt::AlignCenter, paint_str);
0145 
0146     // move the x position to the right by adding the width used for
0147     // painting the string and a margin
0148     x_pos += fontWidth;
0149 
0150     if (addMargin == true)
0151         x_pos += _MARGIN_X;
0152 
0153     return;
0154 }
0155 
0156 void FractionBaseWidget::setColorAndFont()
0157 {
0158     /* set colors */
0159     m_colorNumber = SettingsClass::numberColor();
0160     m_colorLine = SettingsClass::fractionBarColor();
0161     m_colorOperation = SettingsClass::operationColor();
0162 
0163     /* set font */
0164     m_font = SettingsClass::taskFont();
0165 
0166     // repaint
0167     update();
0168 
0169     return;
0170 }
0171 
0172 #include "moc_FractionBaseWidget.cpp"