File indexing completed on 2024-04-21 03:41:49
0001 /* 0002 RationalWidget.cpp - paint a rational number 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 SPDX-FileCopyrightText: 2008 Tiago Porangaba <tiago.porangaba@ltia.fc.unesp.br> 0007 0008 SPDX-License-Identifier: GPL-2.0-or-later 0009 */ 0010 0011 #include "RationalWidget.h" 0012 0013 /* these includes are needed for Qt support */ 0014 #include <QPainter> 0015 #include <QPaintEvent> 0016 0017 #ifdef DEBUG 0018 #include <QDebug> 0019 #endif 0020 0021 RationalWidget::RationalWidget(QWidget * parent, const QString &pnumber, const int pperiodStart, const int pperiodLength) : 0022 FractionBaseWidget(parent), m_number(pnumber), 0023 m_periodStart(pperiodStart), m_periodLength(pperiodLength) 0024 { 0025 #ifdef DEBUG 0026 qDebug() << QStringLiteral("constructor RationalWidget"); 0027 #endif 0028 } 0029 0030 RationalWidget::~RationalWidget() 0031 { 0032 #ifdef DEBUG 0033 qDebug() << QStringLiteral("destructor RationalWidget"); 0034 #endif 0035 } 0036 0037 void RationalWidget::setRational(const QString &pnumber, const int pperiodStart, const int pperiodLength) 0038 { 0039 m_number = pnumber; 0040 m_periodStart = pperiodStart; 0041 m_periodLength = pperiodLength; 0042 0043 update(); 0044 0045 return; 0046 } 0047 0048 void RationalWidget::paintEvent(QPaintEvent* /* p_paintEvent */) 0049 { 0050 // our x position, we paint from left to right; 0051 // we don't want to start directly on the border, so add the margin 0052 int x_pos = _MARGIN_X; 0053 int y_pos = 0; 0054 int x_startPos = _MARGIN_X; 0055 bool tmp_painting = false; 0056 0057 // start the painter 0058 QPainter paint(this); 0059 0060 // ratios and operation signs are painted with the same font 0061 paint.setFont(m_font); 0062 0063 // set the pen for painting 0064 QPen pen(Qt::SolidLine); 0065 pen.setWidth(0); 0066 paint.setPen(pen); 0067 0068 // get the font height; the font height doesn't change while painting 0069 QFontMetrics fm(paint.fontMetrics()); 0070 int fontHeight = fm.lineSpacing(); // get the font height 0071 0072 // now we can correctly set the height of the widget 0073 setMinimumHeight(2 * fontHeight + 10); 0074 setMaximumHeight(2 * fontHeight + 10); 0075 0076 // paint each char one by one 0077 for (int stringPos = 0; stringPos < m_number.length(); stringPos++) { 0078 // check if the period line starts over the current number 0079 if (m_periodLength > 0 && stringPos == m_periodStart && tmp_painting == false) { 0080 x_startPos = x_pos; 0081 tmp_painting = true; 0082 } 0083 0084 // paint the current number (or comma) 0085 paintMiddle(paint, QString(m_number[stringPos]), x_pos, y_pos, fm, m_colorNumber, false); 0086 0087 // check if the period line ends over the current number; in this case 0088 // draw the period line 0089 if (tmp_painting == true && m_periodStart + m_periodLength - 1 == stringPos) { 0090 tmp_painting = false; 0091 0092 // paint the period line above the numbers 0093 paint.fillRect(x_startPos, fontHeight / 2, x_pos - x_startPos, fm.lineWidth(), m_colorNumber); 0094 } 0095 } 0096 0097 // paint a = at the end 0098 x_pos += _MARGIN_X; 0099 paintMiddle(paint, QStringLiteral(" = "), x_pos, y_pos, fm, m_colorOperation); 0100 0101 // stop the painter 0102 paint.end(); 0103 0104 // the space we needed for painting is the minimum width of the widget 0105 setMinimumWidth(x_pos); 0106 0107 return; 0108 } 0109 0110 #include "moc_RationalWidget.cpp"