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

0001 /*
0002     RatioWidget.h  -  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 "RatioWidget.h"
0011 
0012 /* these includes are needed for Qt support */
0013 #include <QPainter>
0014 #include <QPaintEvent>
0015 
0016 #ifdef DEBUG
0017 #include <QDebug>
0018 #endif
0019 
0020 RatioWidget::RatioWidget(QWidget * parent,
0021                          const Ratio &para_ratio) :
0022     FractionBaseWidget(parent), m_ratio(para_ratio)
0023 {
0024 #ifdef DEBUG
0025     qDebug() << "constructor RatioWidget";
0026 #endif
0027 }
0028 
0029 RatioWidget::~RatioWidget()
0030 {
0031 #ifdef DEBUG
0032     qDebug() << "destructor RatioWidget";
0033 #endif
0034 }
0035 
0036 void RatioWidget::setRatio(const Ratio &para_ratio)
0037 {
0038     m_ratio = para_ratio;
0039     update();
0040 }
0041 
0042 void RatioWidget::setQuestionMixed(bool value)
0043 {
0044     m_questionMixed = value;
0045 }
0046 
0047 void RatioWidget::paintEvent(QPaintEvent* /* p_paintEvent */)
0048 {
0049     // our x position, we paint from left to right;
0050     // we don't want to start directly on the border, so add the margin
0051     int x_pos = _MARGIN_X;
0052     int y_pos = 0;
0053 
0054     // start the painter
0055     QPainter paint(this);
0056 
0057     // ratios and operation signs are painted with the same font
0058     paint.setFont(m_font);
0059 
0060     // set the pen for painting
0061     QPen pen(Qt::SolidLine);
0062     pen.setWidth(0);
0063     paint.setPen(pen);
0064 
0065     // get the font height; the font height doesn't change while painting
0066     QFontMetrics fm(paint.fontMetrics());
0067 
0068     // now we can correctly set the height of the widget
0069     setMinimumHeight(2 * fm.lineSpacing() + 10);
0070     setMaximumHeight(2 * fm.lineSpacing() + 10);
0071 
0072     // result as normal ratio
0073     paintRatio(paint, m_ratio, x_pos, y_pos, fm, m_questionMixed);
0074 
0075     // stop the painter
0076     paint.end();
0077 
0078     // the space we needed for painting is the minimum width of the widget
0079     setMinimumWidth(x_pos);
0080 
0081     return;
0082 }
0083 
0084 #include "moc_RatioWidget.cpp"