summaryrefslogtreecommitdiffstats
path: root/kcontrol/joystick/poswidget.cpp
blob: 54e619d7a882757b57c737524cfaf9fabacb39c7 (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
/***************************************************************************
 *   Copyright (C) 2003 by Martin Koller                                   *
 *   m.koller@surfeu.at                                                    *
 *   This file is part of the KDE Control Center Module for Joysticks      *
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.             *
 ***************************************************************************/
#include "poswidget.h"

#include <qpainter.h>

#define XY_WIDTH 220
#define MARK_WIDTH 10

//-----------------------------------------------------------------

PosWidget::PosWidget(QWidget *parent, const char *name)
  : QWidget(parent, name, WRepaintNoErase), x(0), y(0), trace(false)
{
  setMinimumSize(XY_WIDTH, XY_WIDTH);
  setMaximumSize(XY_WIDTH, XY_WIDTH);
  setPaletteBackgroundColor(Qt::white);
}

//-----------------------------------------------------------------

void PosWidget::paintEvent(QPaintEvent *)
{
  QPainter paint(this);

  paint.drawRect(0, 0, width(), height());
  paint.setPen(Qt::gray);

  // draw a center grid
  paint.drawLine(XY_WIDTH/2, 1,
                 XY_WIDTH/2, XY_WIDTH - 2);

  paint.drawLine(1,            XY_WIDTH/2,
                 XY_WIDTH - 2, XY_WIDTH/2);

  // draw the current position marker
  paint.setPen(Qt::blue);

  paint.drawLine(x - MARK_WIDTH/2, y - MARK_WIDTH/2,
                 x + MARK_WIDTH/2, y + MARK_WIDTH/2);

  paint.drawLine(x - MARK_WIDTH/2, y + MARK_WIDTH/2,
                 x + MARK_WIDTH/2, y - MARK_WIDTH/2);
}

//-----------------------------------------------------------------

void PosWidget::changeX(int newX)
{
  // transform coordinates from joystick to widget coordinates
  newX = int((newX/65535.0)*XY_WIDTH + XY_WIDTH/2);

  if ( x == newX ) return;  // avoid unnecessary redraw

  eraseOld();

  x = newX;
}

//-----------------------------------------------------------------

void PosWidget::changeY(int newY)
{
  // transform coordinates from joystick to widget coordinates
  newY = int((newY/65535.0)*XY_WIDTH + XY_WIDTH/2);

  if ( y == newY ) return;  // avoid unnecessary redraw

  eraseOld();

  y = newY;
}

//-----------------------------------------------------------------

void PosWidget::showTrace(bool t)
{
  trace = t;

  if ( !trace )
  {
    erase();
    update();
  }
}

//-----------------------------------------------------------------

void PosWidget::eraseOld()
{
  QPainter paint(this);

  //paint.eraseRect(x - MARK_WIDTH/2, y - MARK_WIDTH/2, MARK_WIDTH + 1, MARK_WIDTH + 1);

  // erase previous cross (don't use eraseRect() so that trace flags will be not destroyed so much)
  paint.setPen(Qt::white);

  paint.drawLine(x - MARK_WIDTH/2, y - MARK_WIDTH/2,
                 x + MARK_WIDTH/2, y + MARK_WIDTH/2);

  paint.drawLine(x - MARK_WIDTH/2, y + MARK_WIDTH/2,
                 x + MARK_WIDTH/2, y - MARK_WIDTH/2);

  if ( trace )  // show previous position with a smaller black cross
  {
    paint.setPen(Qt::black);

    paint.drawLine(x - 2, y - 2,
                   x + 2, y + 2);

    paint.drawLine(x - 2, y + 2,
                   x + 2, y - 2);
  }

  update();
}

//-----------------------------------------------------------------

#include "poswidget.moc"