summaryrefslogtreecommitdiffstats
path: root/kbruch/src/fractionbasewidget.cpp
blob: 02fd1cfb0ab86c2fd4ba1eba4f88053d69bb1ed4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/***************************************************************************
                          fractionbasewidget.cpp  -  base fraction painting
                             -------------------
    begin                : 2004/05/30
    copyright            : (C) 2004 by Sebastian Stein
    email                : seb.kde@hpfsc.de
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include "fractionbasewidget.h"
#include "fractionbasewidget.moc"

/* these includes are needed for KDE support */
#include <tdeglobalsettings.h>

/* these includes are needed for TQt support */
#include <tqpainter.h>

#include "settingsclass.h"

FractionBaseWidget::FractionBaseWidget(TQWidget * parent = 0, const char * name = 0) :
			TQWidget(parent, name)
{
#ifdef DEBUG
	kdDebug() << "constructor FractionBaseWidget" << endl;
#endif

	// set colors and font used for task displaying
	setColorAndFont();
}

FractionBaseWidget::~FractionBaseWidget()
{
#ifdef DEBUG
	kdDebug() << "destructor FractionBaseWidget" << endl;
#endif
}

void FractionBaseWidget::updateAndRepaint()
{
	setColorAndFont();
	update();
}

void FractionBaseWidget::paintRatio(TQPainter & paint, ratio tmp_ratio, int & x_pos, TQFontMetrics & fm, bool show_mixed, bool addMargin)
{
	TQPen pen = paint.pen(); // get the pen
	int fontHeight = fm.lineSpacing(); // get the font height

	int int_numerator, int_denominator, int_mixed;
	TQString str_numerator, str_denominator;
	TQString str_mixed;

	int fontWidth; // to store the width of the last thing painted
	int tmp_int;

	// check if we have to show the ratio as mixed number
	//         11            1
	// if yes, -- becomes  2 -
	//         5             5
	int_numerator = tmp_ratio.numerator();
	int_denominator = tmp_ratio.denominator();
	if (show_mixed == true && TQABS(int_numerator) >= TQABS(int_denominator))
	{
		// calculate the mixed number
		int_mixed = int(int_numerator / int_denominator);

		// the negative sign is in front of the mixed number
		int_numerator = TQABS(int_numerator);
		int_denominator = TQABS(int_denominator);

		// we have to reduce the numerator by the mixed number * denominator
		int_numerator = int_numerator % int_denominator;

		// now we can convert the numbers into strings for painting
		str_mixed.setNum(int_mixed);
		str_numerator.setNum(int_numerator);
		str_denominator.setNum(int_denominator);

		// paint the front part of the mixed number
		paintMiddle(paint, str_mixed, x_pos, fm, m_colorNumber);
	} else {

		// don't show the ratio as mixed number
		str_numerator.setNum(int_numerator);
		str_denominator.setNum(int_denominator);
	} // if (show_mixed == true && TQABS(int_numerator) > TQABS(int_denominator))

	// get the text width of the current ratio
	fontWidth = fm.width(str_numerator);
	tmp_int = fm.width(str_denominator);
	if (tmp_int > fontWidth)
		fontWidth = tmp_int;
		
	// show numerator and denominator in m_colorNumber
	pen.setColor(m_colorNumber);
	paint.setPen(pen);

	// make sure we don't display something like:   0
	//                                            7 -
	//                                              3
	if (! (int_numerator == 0 && show_mixed == true) )
	{
		// paint the numerator
		paint.drawText(x_pos, 0, fontWidth, fontHeight, AlignCenter, str_numerator);
	
		// paint the fraction line between numerator and denominator
		paint.fillRect(x_pos, fontHeight + 4, fontWidth, 2, m_colorLine);
	
		// paint the denominator
		paint.drawText(x_pos, fontHeight + 10, fontWidth, fontHeight, AlignCenter, str_denominator);
	
		// move the x position to the right by adding the width used for painting
		// the ratio and a margin
		x_pos += fontWidth;

		if (addMargin == true)
			x_pos += _MARGIN_X;
	}

	return;
}

void FractionBaseWidget::paintMiddle(TQPainter & paint, const TQString& paint_str, int & x_pos, TQFontMetrics & fm, TQColor color, bool addMargin)
{
	// get the pen, font height and font width
	TQPen pen = paint.pen();
	int fontHeight = fm.lineSpacing();
	int fontWidth = fm.width(paint_str);

	// paint the string
	pen.setColor(color);
	paint.setPen(pen);
	paint.drawText(x_pos, fontHeight + 5 - fontHeight / 2, fontWidth, fontHeight, AlignCenter, paint_str);

	// move the x position to the right by adding the width used for
	// painting the string and a margin
	x_pos += fontWidth;

	if (addMargin == true)
		x_pos += _MARGIN_X;

	return;
}

void FractionBaseWidget::setColorAndFont()
{
	/* set colors */
	m_colorNumber = SettingsClass::numberColor();
	m_colorLine = SettingsClass::fractionBarColor();
	m_colorOperation = SettingsClass::operationColor();

	/* set font */
	m_font = SettingsClass::taskFont();

	// repaint
	update();

	return;
}