~ubuntu-branches/ubuntu/utopic/kde-workspace/utopic-proposed

« back to all changes in this revision

Viewing changes to kcontrol/hardware/joystick/poswidget.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michał Zając
  • Date: 2011-07-09 08:31:15 UTC
  • Revision ID: james.westby@ubuntu.com-20110709083115-ohyxn6z93mily9fc
Tags: upstream-4.6.90
Import upstream version 4.6.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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"
 
22
 
 
23
#include <QPainter>
 
24
 
 
25
#define XY_WIDTH 220
 
26
#define MARK_WIDTH 10
 
27
#define MAX_POINTS 500
 
28
 
 
29
//-----------------------------------------------------------------
 
30
 
 
31
PosWidget::PosWidget(QWidget *parent)
 
32
  : QWidget(parent), x(0), y(0), trace(false)
 
33
{
 
34
  setMinimumSize(XY_WIDTH, XY_WIDTH);
 
35
  setMaximumSize(XY_WIDTH, XY_WIDTH);
 
36
 
 
37
  QPalette palette;
 
38
  palette.setColor(backgroundRole(), Qt::white);
 
39
  setPalette(palette);
 
40
}
 
41
 
 
42
//-----------------------------------------------------------------
 
43
 
 
44
void PosWidget::paintEvent(QPaintEvent *)
 
45
{
 
46
  QPainter paint(this);
 
47
 
 
48
  // draw a frame
 
49
  paint.drawRect(0, 0, width()-1, height()-1);
 
50
  paint.setPen(Qt::gray);
 
51
 
 
52
  // draw a center grid
 
53
  paint.drawLine(XY_WIDTH/2, 1,
 
54
                 XY_WIDTH/2, XY_WIDTH - 2);
 
55
 
 
56
  paint.drawLine(1,           XY_WIDTH/2,
 
57
                 XY_WIDTH - 2, XY_WIDTH/2);
 
58
 
 
59
  // draw the trace of previous points
 
60
  if ( trace )
 
61
  {
 
62
    paint.setPen(Qt::black);
 
63
 
 
64
    for (int i = 0; i < tracePoints.count()-2; i++)
 
65
      paint.drawLine(tracePoints[i], tracePoints[i+1]);
 
66
 
 
67
    if ( tracePoints.count() > 0 )
 
68
      paint.drawLine(tracePoints[tracePoints.count()-1], QPoint(x, y));
 
69
  }
 
70
 
 
71
  // draw the current position marker
 
72
  paint.setPen(Qt::blue);
 
73
 
 
74
  paint.drawLine(x - MARK_WIDTH/2, y - MARK_WIDTH/2,
 
75
                 x + MARK_WIDTH/2, y + MARK_WIDTH/2);
 
76
 
 
77
  paint.drawLine(x - MARK_WIDTH/2, y + MARK_WIDTH/2,
 
78
                 x + MARK_WIDTH/2, y - MARK_WIDTH/2);
 
79
}
 
80
 
 
81
//-----------------------------------------------------------------
 
82
 
 
83
void PosWidget::changeX(int newX)
 
84
{
 
85
  // transform coordinates from joystick to widget coordinates
 
86
  newX = int((newX/65535.0)*XY_WIDTH + XY_WIDTH/2);
 
87
 
 
88
  if ( x == newX ) return;  // avoid unnecessary redraw
 
89
 
 
90
  if ( trace )
 
91
  {
 
92
    tracePoints.append(QPoint(x, y));
 
93
    if ( tracePoints.count() == MAX_POINTS )
 
94
      tracePoints.removeFirst();
 
95
  }
 
96
 
 
97
  x = newX;
 
98
  update();
 
99
}
 
100
 
 
101
//-----------------------------------------------------------------
 
102
 
 
103
void PosWidget::changeY(int newY)
 
104
{
 
105
  // transform coordinates from joystick to widget coordinates
 
106
  newY = int((newY/65535.0)*XY_WIDTH + XY_WIDTH/2);
 
107
 
 
108
  if ( y == newY ) return;  // avoid unnecessary redraw
 
109
 
 
110
  if ( trace )
 
111
  {
 
112
    tracePoints.append(QPoint(x, y));
 
113
    if ( tracePoints.count() == MAX_POINTS )
 
114
      tracePoints.removeFirst();
 
115
  }
 
116
 
 
117
  y = newY;
 
118
  update();
 
119
}
 
120
 
 
121
//-----------------------------------------------------------------
 
122
 
 
123
void PosWidget::showTrace(bool t)
 
124
{
 
125
  trace = t;
 
126
  tracePoints.clear();
 
127
 
 
128
  update();
 
129
}
 
130
 
 
131
//-----------------------------------------------------------------
 
132
 
 
133
#include "poswidget.moc"