moved kdeaccessibility kdeaddons kdeadmin kdeartwork kdebindings kdeedu kdegames...
[kdeedu.git] / kbruch / src / rationalwidget.cpp
blobe4a07e1cb8054f1076223e390351f0ce7d6e8a10
1 /***************************************************************************
2 rationalwidget.h - paint a rational number
3 -------------------
4 begin : 2004/06/04
5 copyright : (C) 2004 by Sebastian Stein
6 email : seb.kde@hpfsc.de
7 ***************************************************************************/
9 /***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
18 #include "rationalwidget.h"
19 #include "rationalwidget.moc"
21 /* these includes are needed for Qt support */
22 #include <qpainter.h>
23 #include <qstring.h>
25 RationalWidget::RationalWidget(QWidget * parent, const char * name, const QString pnumber, const uint pperiodStart, const uint pperiodLength):
26 FractionBaseWidget(parent, name), m_number(pnumber),
27 m_periodStart(pperiodStart), m_periodLength(pperiodLength)
29 #ifdef DEBUG
30 kdDebug() << "constructor RationalWidget" << endl;
31 #endif
34 RationalWidget::~RationalWidget()
36 #ifdef DEBUG
37 kdDebug() << "destructor RationalWidget" << endl;
38 #endif
41 void RationalWidget::setRational(const QString pnumber, const uint pperiodStart, const uint pperiodLength)
43 m_number = pnumber;
44 m_periodStart = pperiodStart;
45 m_periodLength = pperiodLength;
47 update();
49 return;
52 void RationalWidget::paintEvent(QPaintEvent* /* p_paintEvent */)
54 // our x position, we paint from left to right;
55 // we don't want to start directly on the border, so add the margin
56 int x_pos = _MARGIN_X;
57 int x_startPos = _MARGIN_X;
58 bool tmp_painting = false;
60 // start the painter
61 QPainter paint(this);
63 // ratios and operation signs are painted with the same font
64 paint.setFont(m_font);
66 // set the pen for painting
67 QPen pen(Qt::SolidLine);
68 pen.setWidth(0);
69 paint.setPen(pen);
71 // get the font height; the font height doesn't change while painting
72 QFontMetrics & fm = * new QFontMetrics(paint.fontMetrics());
73 int fontHeight = fm.lineSpacing(); // get the font height
75 // now we can correctly set the height of the widget
76 setMinimumHeight(2 * fontHeight + 10);
77 setMaximumHeight(2 * fontHeight + 10);
79 // paint each char one by one
80 for (uint stringPos = 0; stringPos < m_number.length(); stringPos++)
82 // check if the period line starts over the current number
83 if (m_periodLength > 0 && stringPos == m_periodStart && tmp_painting == false)
85 x_startPos = x_pos;
86 tmp_painting = true;
89 // paint the current number (or comma)
90 paintMiddle(paint, QString(m_number[stringPos]), x_pos, fm, m_colorNumber, false);
92 // check if the period line ends over the current number; in this case
93 // draw the period line
94 if (tmp_painting == true && m_periodStart + m_periodLength - 1 == stringPos)
96 tmp_painting = false;
98 // paint the period line above the numbers
99 paint.fillRect(x_startPos, fontHeight - 3, x_pos - x_startPos, 1, m_colorNumber);
103 // paint a = at the end
104 x_pos += _MARGIN_X;
105 paintMiddle(paint, "=", x_pos, fm, m_colorOperation);
107 // stop the painter
108 paint.end();
110 // the space we needed for painting is the minimum width of the widget
111 setMinimumWidth(x_pos);
113 return;