moved kdeaccessibility kdeaddons kdeadmin kdeartwork kdebindings kdeedu kdegames...
[kdeedu.git] / kbruch / src / fractionbasewidget.cpp
blob77ff30563092cfdb272089d9d1a333c3eec69616
1 /***************************************************************************
2 fractionbasewidget.cpp - base fraction painting
3 -------------------
4 begin : 2004/05/30
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 "fractionbasewidget.h"
19 #include "fractionbasewidget.moc"
21 /* these includes are needed for KDE support */
22 #include <kglobalsettings.h>
24 /* these includes are needed for Qt support */
25 #include <qpainter.h>
27 #include "settingsclass.h"
29 FractionBaseWidget::FractionBaseWidget(QWidget * parent = 0, const char * name = 0) :
30 QWidget(parent, name)
32 #ifdef DEBUG
33 kdDebug() << "constructor FractionBaseWidget" << endl;
34 #endif
36 // set colors and font used for task displaying
37 setColorAndFont();
40 FractionBaseWidget::~FractionBaseWidget()
42 #ifdef DEBUG
43 kdDebug() << "destructor FractionBaseWidget" << endl;
44 #endif
47 void FractionBaseWidget::updateAndRepaint()
49 setColorAndFont();
50 update();
53 void FractionBaseWidget::paintRatio(QPainter & paint, ratio tmp_ratio, int & x_pos, QFontMetrics & fm, bool show_mixed, bool addMargin)
55 QPen pen = paint.pen(); // get the pen
56 int fontHeight = fm.lineSpacing(); // get the font height
58 int int_numerator, int_denominator, int_mixed;
59 QString str_numerator, str_denominator;
60 QString str_mixed;
62 int fontWidth; // to store the width of the last thing painted
63 int tmp_int;
65 // check if we have to show the ratio as mixed number
66 // 11 1
67 // if yes, -- becomes 2 -
68 // 5 5
69 int_numerator = tmp_ratio.numerator();
70 int_denominator = tmp_ratio.denominator();
71 if (show_mixed == true && QABS(int_numerator) >= QABS(int_denominator))
73 // calculate the mixed number
74 int_mixed = int(int_numerator / int_denominator);
76 // the negative sign is in front of the mixed number
77 int_numerator = QABS(int_numerator);
78 int_denominator = QABS(int_denominator);
80 // we have to reduce the numerator by the mixed number * denominator
81 int_numerator = int_numerator % int_denominator;
83 // now we can convert the numbers into strings for painting
84 str_mixed.setNum(int_mixed);
85 str_numerator.setNum(int_numerator);
86 str_denominator.setNum(int_denominator);
88 // paint the front part of the mixed number
89 paintMiddle(paint, str_mixed, x_pos, fm, m_colorNumber);
90 } else {
92 // don't show the ratio as mixed number
93 str_numerator.setNum(int_numerator);
94 str_denominator.setNum(int_denominator);
95 } // if (show_mixed == true && QABS(int_numerator) > QABS(int_denominator))
97 // get the text width of the current ratio
98 fontWidth = fm.width(str_numerator);
99 tmp_int = fm.width(str_denominator);
100 if (tmp_int > fontWidth)
101 fontWidth = tmp_int;
103 // show numerator and denominator in m_colorNumber
104 pen.setColor(m_colorNumber);
105 paint.setPen(pen);
107 // make sure we don't display something like: 0
108 // 7 -
109 // 3
110 if (! (int_numerator == 0 && show_mixed == true) )
112 // paint the numerator
113 paint.drawText(x_pos, 0, fontWidth, fontHeight, AlignCenter, str_numerator);
115 // paint the fraction line between numerator and denominator
116 paint.fillRect(x_pos, fontHeight + 4, fontWidth, 2, m_colorLine);
118 // paint the denominator
119 paint.drawText(x_pos, fontHeight + 10, fontWidth, fontHeight, AlignCenter, str_denominator);
121 // move the x position to the right by adding the width used for painting
122 // the ratio and a margin
123 x_pos += fontWidth;
125 if (addMargin == true)
126 x_pos += _MARGIN_X;
129 return;
132 void FractionBaseWidget::paintMiddle(QPainter & paint, const QString& paint_str, int & x_pos, QFontMetrics & fm, QColor color, bool addMargin)
134 // get the pen, font height and font width
135 QPen pen = paint.pen();
136 int fontHeight = fm.lineSpacing();
137 int fontWidth = fm.width(paint_str);
139 // paint the string
140 pen.setColor(color);
141 paint.setPen(pen);
142 paint.drawText(x_pos, fontHeight + 5 - fontHeight / 2, fontWidth, fontHeight, AlignCenter, paint_str);
144 // move the x position to the right by adding the width used for
145 // painting the string and a margin
146 x_pos += fontWidth;
148 if (addMargin == true)
149 x_pos += _MARGIN_X;
151 return;
154 void FractionBaseWidget::setColorAndFont()
156 /* set colors */
157 m_colorNumber = SettingsClass::numberColor();
158 m_colorLine = SettingsClass::fractionBarColor();
159 m_colorOperation = SettingsClass::operationColor();
161 /* set font */
162 m_font = SettingsClass::taskFont();
164 // repaint
165 update();
167 return;