add more spacing
[personal-kdebase.git] / workspace / kcontrol / hardware / joystick / poswidget.cpp
blobed2d6c33cfa107448e6b85841716a609855f95af
1 /***************************************************************************
2 * Copyright (C) 2003,2005,2006 by Martin Koller *
3 * m.koller@surfeu.at *
4 * This file is part of the KDE Control Center Module for Joysticks *
5 * *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the *
18 * Free Software Foundation, Inc., *
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
20 ***************************************************************************/
21 #include "poswidget.h"
23 #include <QPainter>
25 #define XY_WIDTH 220
26 #define MARK_WIDTH 10
27 #define MAX_POINTS 500
29 //-----------------------------------------------------------------
31 PosWidget::PosWidget(QWidget *parent)
32 : QWidget(parent), x(0), y(0), trace(false)
34 setMinimumSize(XY_WIDTH, XY_WIDTH);
35 setMaximumSize(XY_WIDTH, XY_WIDTH);
37 QPalette palette;
38 palette.setColor(backgroundRole(), Qt::white);
39 setPalette(palette);
42 //-----------------------------------------------------------------
44 void PosWidget::paintEvent(QPaintEvent *)
46 QPainter paint(this);
48 // draw a frame
49 paint.drawRect(0, 0, width()-1, height()-1);
50 paint.setPen(Qt::gray);
52 // draw a center grid
53 paint.drawLine(XY_WIDTH/2, 1,
54 XY_WIDTH/2, XY_WIDTH - 2);
56 paint.drawLine(1, XY_WIDTH/2,
57 XY_WIDTH - 2, XY_WIDTH/2);
59 // draw the trace of previous points
60 if ( trace )
62 paint.setPen(Qt::black);
64 for (int i = 0; i < tracePoints.count()-2; i++)
65 paint.drawLine(tracePoints[i], tracePoints[i+1]);
67 if ( tracePoints.count() > 0 )
68 paint.drawLine(tracePoints[tracePoints.count()-1], QPoint(x, y));
71 // draw the current position marker
72 paint.setPen(Qt::blue);
74 paint.drawLine(x - MARK_WIDTH/2, y - MARK_WIDTH/2,
75 x + MARK_WIDTH/2, y + MARK_WIDTH/2);
77 paint.drawLine(x - MARK_WIDTH/2, y + MARK_WIDTH/2,
78 x + MARK_WIDTH/2, y - MARK_WIDTH/2);
81 //-----------------------------------------------------------------
83 void PosWidget::changeX(int newX)
85 // transform coordinates from joystick to widget coordinates
86 newX = int((newX/65535.0)*XY_WIDTH + XY_WIDTH/2);
88 if ( x == newX ) return; // avoid unnecessary redraw
90 if ( trace )
92 tracePoints.append(QPoint(x, y));
93 if ( tracePoints.count() == MAX_POINTS )
94 tracePoints.removeFirst();
97 x = newX;
98 update();
101 //-----------------------------------------------------------------
103 void PosWidget::changeY(int newY)
105 // transform coordinates from joystick to widget coordinates
106 newY = int((newY/65535.0)*XY_WIDTH + XY_WIDTH/2);
108 if ( y == newY ) return; // avoid unnecessary redraw
110 if ( trace )
112 tracePoints.append(QPoint(x, y));
113 if ( tracePoints.count() == MAX_POINTS )
114 tracePoints.removeFirst();
117 y = newY;
118 update();
121 //-----------------------------------------------------------------
123 void PosWidget::showTrace(bool t)
125 trace = t;
126 tracePoints.clear();
128 update();
131 //-----------------------------------------------------------------
133 #include "poswidget.moc"