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

0001 /*
0002     SPDX-FileCopyrightText: 2008 Danilo Balzaque <danilo.balzaque@ltia.fc.unesp.br>
0003     SPDX-FileCopyrightText: 2008 Tadeu Araujo <tadeu.araujo@ltia.fc.unesp.br>
0004     SPDX-FileCopyrightText: 2008 Tiago Porangaba <tiago.porangaba@ltia.fc.unesp.br>
0005 
0006     SPDX-License-Identifier: GPL-2.0-or-later
0007 */
0008 
0009 #include "FractionPainter.h"
0010 
0011 /* these includes are needed for Qt support */
0012 #include <QPainter>
0013 #include <QPaintEvent>
0014 
0015 #ifdef DEBUG
0016 #include <QDebug>
0017 #endif
0018 
0019 FractionPainter::FractionPainter(QWidget * parent) :
0020     FractionBaseWidget(parent)
0021 {
0022 #ifdef DEBUG
0023     qDebug() << "constructor FractionPainter";
0024 #endif
0025 }
0026 
0027 FractionPainter::~FractionPainter()
0028 {
0029 #ifdef DEBUG
0030     qDebug() << "destructor FractionPainter";
0031 #endif
0032 }
0033 
0034 void FractionPainter::paintEvent(QPaintEvent *)
0035 {
0036     QPainter paint(this);
0037     paintWidget(paint);
0038 }
0039 
0040 void FractionPainter::paintFraction(const QString & op, const Ratio & lr, int lm,
0041                                     const Ratio & rr, int rm)
0042 {
0043     str_operation = op;
0044     leftRatio = lr;
0045     leftMult = lm;
0046     rightRatio = rr;
0047     rightMult = rm;
0048 
0049     update();
0050 }
0051 
0052 void FractionPainter::paintWidget(QPainter & paint)
0053 {
0054     // our x position, we paint from left to right;
0055     // we don't want to start directly on the border, so add the margin
0056     int old_x = _MARGIN_X;
0057     int old_y = 0;
0058 
0059     // ratios and operation signs are painted with the same font
0060     m_font.setPointSize(24);
0061     m_font.setBold(true);
0062     paint.setFont(m_font);
0063 
0064     // set the pen for painting
0065     QPen pen(Qt::SolidLine);
0066     pen.setWidth(0);
0067     paint.setPen(pen);
0068 
0069     // get the font height; the font height doesn't change while painting
0070     QFontMetrics  fm(paint.fontMetrics());
0071 
0072     // now we can correctly set the height of the widget
0073     setMinimumHeight(2 * fm.lineSpacing() + 10);
0074     setMaximumHeight(2 * fm.lineSpacing() + 10);
0075 
0076     // get the current ratio and paint it
0077     paintRatio(paint, Ratio(leftRatio.numerator() * leftMult, leftRatio.denominator() * leftMult, false), old_x, old_y, fm);
0078 
0079     if (leftRatio.denominator() * leftMult < 10)
0080         old_x += 40;
0081     else
0082         old_x += 25;
0083 
0084     // paint the operation
0085     paintMiddle(paint, str_operation, old_x, old_y, fm, m_colorOperation);
0086 
0087     if (rightRatio.denominator() * rightMult < 10)
0088         old_x += 40;
0089     else
0090         old_x += 25;
0091 
0092     // get the current ratio and paint it
0093     paintRatio(paint, Ratio(rightRatio.numerator() * rightMult, rightRatio.denominator() * rightMult, false), old_x, old_y, fm);
0094 
0095     // stop the painter
0096     paint.end();
0097 
0098     // the space we needed for painting is the minimum width of the widget
0099     setMinimumWidth(old_x);
0100 }
0101 
0102 /* ------ public slots ------ */
0103 
0104 void FractionPainter::update()
0105 {
0106     updateAndRepaint();
0107 
0108     // update for itself
0109     ((QWidget *) this)->update();
0110 }
0111 
0112 #include "moc_FractionPainter.cpp"