moved kdeaccessibility kdeaddons kdeadmin kdeartwork kdebindings kdeedu kdegames...
[kdeedu.git] / kbruch / src / taskwidget.cpp
blob6aead26ec3f630889a74fcc0fd92c852dafbcf09
1 /***************************************************************************
2 taskwidget.cpp - paint a task
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 "taskwidget.h"
19 #include "taskwidget.moc"
21 /* these includes are needed for KDE support */
22 #include <klocale.h>
24 /* these includes are needed for Qt support */
25 #include <qpainter.h>
27 TaskWidget::TaskWidget(QWidget * parent = 0, const char * name = 0,
28 const task para_task = *new task()) :
29 FractionBaseWidget(parent, name), m_task(para_task)
31 #ifdef DEBUG
32 kdDebug() << "constructor TaskWidget" << endl;
33 #endif
36 TaskWidget::~TaskWidget()
38 #ifdef DEBUG
39 kdDebug() << "destructor TaskWidget" << endl;
40 #endif
43 void TaskWidget::setTask(const task para_task)
45 m_task = para_task;
46 update();
49 void TaskWidget::paintEvent(QPaintEvent* /* p_paintEvent */)
51 // our x position, we paint from left to right;
52 // we don't want to start directly on the border, so add the margin
53 int old_x = _MARGIN_X;
55 // strings holding numerator, denominator and the operation sign
56 QString str_numerator, str_denominator, str_operation;
58 // operation sign as number
59 short tmp_operation;
61 // start the painter
62 QPainter paint(this);
64 // ratios and operation signs are painted with the same font
65 paint.setFont(m_font);
67 // set the pen for painting
68 QPen pen(Qt::SolidLine);
69 pen.setWidth(0);
70 paint.setPen(pen);
72 // get the font height; the font height doesn't change while painting
73 QFontMetrics & fm = * new QFontMetrics(paint.fontMetrics());
75 // now we can correctly set the height of the widget
76 setMinimumHeight(2 * fm.lineSpacing() + 10);
77 setMaximumHeight(2 * fm.lineSpacing() + 10);
79 // loop through all ratios and paint them
80 for (unsigned short tmp_counter = 0; tmp_counter < m_task.getNumberOfRatios(); tmp_counter++)
82 // get the current ratio and paint it
83 paintRatio(paint, m_task.get_ratio_n(tmp_counter), old_x, fm, false);
85 // now check if we have another operation to show
86 // if not we will stop showing ratios as well
87 if (tmp_counter < m_task.getNumberOfOperations())
89 // get the operation sign
90 tmp_operation = m_task.get_op_n(tmp_counter);
92 // we have to convert the operation sign into a string
93 switch (tmp_operation)
95 case ADD :
96 str_operation = "+";
97 break;
98 case SUB :
99 str_operation = "-";
100 break;
101 case MUL :
102 str_operation = "x";
103 break;
104 case DIV :
105 // there seems to be different division signs around the world
106 // so please translate it to the right one for your country
107 str_operation = i18n("division symbol", "/");
108 break;
109 } /* switch (operation) */
111 // paint the operation
112 paintMiddle(paint, str_operation, old_x, fm, m_colorOperation);
114 } else {
115 // no further operations to show, so we always show the = sign at the
116 // end of a task
117 paintMiddle(paint, "=", old_x, fm, m_colorOperation);
119 break;
120 } // if (tmp_counter < m_task.getNumberOfOperations())
123 // stop the painter
124 paint.end();
126 // the space we needed for painting is the minimum width of the widget
127 setMinimumWidth(old_x);
129 return;