1 /***************************************************************************
2 rationalwidget.h - paint a rational number
5 copyright : (C) 2004 by Sebastian Stein
6 email : seb.kde@hpfsc.de
7 ***************************************************************************/
9 /***************************************************************************
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. *
16 ***************************************************************************/
18 #include "rationalwidget.h"
19 #include "rationalwidget.moc"
21 /* these includes are needed for Qt support */
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
)
30 kdDebug() << "constructor RationalWidget" << endl
;
34 RationalWidget::~RationalWidget()
37 kdDebug() << "destructor RationalWidget" << endl
;
41 void RationalWidget::setRational(const QString pnumber
, const uint pperiodStart
, const uint pperiodLength
)
44 m_periodStart
= pperiodStart
;
45 m_periodLength
= pperiodLength
;
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;
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
);
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)
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
)
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
105 paintMiddle(paint
, "=", x_pos
, fm
, m_colorOperation
);
110 // the space we needed for painting is the minimum width of the widget
111 setMinimumWidth(x_pos
);