moved kdeaccessibility kdeaddons kdeadmin kdeartwork kdebindings kdeedu kdegames...
[kdeedu.git] / kbruch / src / factorizedwidget.cpp
blob90297d2cdbc5812f3582d7113f19d038d8557741
1 /***************************************************************************
2 factorizedwidget.h - paint a factorization
3 -------------------
4 begin : 2004/07/11
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 "factorizedwidget.h"
19 #include "factorizedwidget.moc"
21 /* these includes are needed for Qt support */
22 #include <qpainter.h>
24 FactorizedWidget::FactorizedWidget(QWidget * parent, const char * name, const uintList para_factors) :
25 FractionBaseWidget(parent, name), m_factors(para_factors)
27 #ifdef DEBUG
28 kdDebug() << "constructor FactorizedWidget" << endl;
29 #endif
32 FactorizedWidget::~FactorizedWidget()
34 #ifdef DEBUG
35 kdDebug() << "destructor FactorizedWidget" << endl;
36 #endif
39 void FactorizedWidget::setFactors(const uintList para_factors)
41 m_factors = para_factors;
42 update();
45 void FactorizedWidget::paintEvent(QPaintEvent* /* p_paintEvent */)
47 // our x position, we paint from left to right;
48 // we don't want to start directly on the border, so add the margin
49 int x_pos = _MARGIN_X;
51 int fontWidth; // to store the width of the last thing painted
53 // start the painter
54 QPainter paint(this);
56 // ratios and operation signs are painted with the same font
57 paint.setFont(m_font);
59 // set the pen for painting
60 QPen pen(Qt::SolidLine);
61 pen.setWidth(0);
62 paint.setPen(pen);
64 // get the font height; the font height doesn't change while painting
65 QFontMetrics & fm = * new QFontMetrics(paint.fontMetrics());
67 // now we can correctly set the height of the widget
68 setMinimumHeight(fm.lineSpacing());
69 setMaximumHeight(fm.lineSpacing());
71 QString tmpStr;
72 int fontHeight = fm.lineSpacing(); // get the font height
74 for (uint tmpInt = 0; tmpInt < m_factors.count(); tmpInt++)
76 // set color for operation sign
77 pen.setColor(m_colorOperation);
78 paint.setPen(pen);
80 if (tmpInt == 0)
82 fontWidth = fm.width("=");
83 paint.drawText(x_pos, 0, fontWidth, fontHeight, AlignCenter, "=");
84 x_pos += fontWidth;
85 x_pos += _MARGIN_X;
86 } else {
87 fontWidth = fm.width("*");
88 paint.drawText(x_pos, 0, fontWidth, fontHeight, AlignCenter, "*");
89 x_pos += fontWidth;
90 x_pos += _MARGIN_X;
93 // set color for number
94 pen.setColor(m_colorNumber);
95 paint.setPen(pen);
97 tmpStr.setNum(m_factors[tmpInt]);
99 fontWidth = fm.width(tmpStr);
100 paint.drawText(x_pos, 0, fontWidth, fontHeight, AlignCenter, tmpStr);
101 x_pos += fontWidth;
102 x_pos += _MARGIN_X;
105 // stop the painter
106 paint.end();
108 // the space we needed for painting is the minimum width of the widget
109 setMinimumWidth(x_pos);
111 return;