1 /***************************************************************************
2 fractionbasewidget.cpp - base fraction painting
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 "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 */
27 #include "settingsclass.h"
29 FractionBaseWidget::FractionBaseWidget(QWidget
* parent
= 0, const char * name
= 0) :
33 kdDebug() << "constructor FractionBaseWidget" << endl
;
36 // set colors and font used for task displaying
40 FractionBaseWidget::~FractionBaseWidget()
43 kdDebug() << "destructor FractionBaseWidget" << endl
;
47 void FractionBaseWidget::updateAndRepaint()
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
;
62 int fontWidth
; // to store the width of the last thing painted
65 // check if we have to show the ratio as mixed number
67 // if yes, -- becomes 2 -
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
);
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
)
103 // show numerator and denominator in m_colorNumber
104 pen
.setColor(m_colorNumber
);
107 // make sure we don't display something like: 0
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
125 if (addMargin
== true)
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
);
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
148 if (addMargin
== true)
154 void FractionBaseWidget::setColorAndFont()
157 m_colorNumber
= SettingsClass::numberColor();
158 m_colorLine
= SettingsClass::fractionBarColor();
159 m_colorOperation
= SettingsClass::operationColor();
162 m_font
= SettingsClass::taskFont();