summaryrefslogtreecommitdiffstats
path: root/khotkeys/kcontrol/gesturedrawer.cpp
blob: 2364e6e515a2b890543960ccf613403570f46f51 (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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/****************************************************************************

 KHotKeys
 
 Copyright (C) 2003 Mike Pilone <mpilone@slac.com>
 Copyright (C) 2003 Lubos Lunak <l.lunak@kde.org>

 Distributed under the terms of the GNU General Public License version 2.
 
****************************************************************************/

#include <tqcolor.h>
#include <tqpainter.h>

#include "gesturedrawer.h"

namespace KHotKeys
{

GestureDrawer::GestureDrawer(TQWidget *parent, const char *name)
  : TQFrame(parent, name), _data(TQString::null)
    {
    setBackgroundColor( colorGroup().base());
    setFrameStyle(TQFrame::Panel | TQFrame::Sunken);
    setMinimumSize(30, 30);
    }

GestureDrawer::~GestureDrawer()
    {
    }

void GestureDrawer::setData(const TQString &data)
    {
    _data = data;

    repaint();
    }

void GestureDrawer::paintEvent(TQPaintEvent *ev)
    {
  // Iterate through the data points and draw a line to each of them
    TQ_UINT32 startCell = 0;
    TQ_UINT32 endCell = 0;
    TQPoint startPoint;
    TQPoint endPoint;

    TQPainter p(this);

    if (_data.length() > 0)
        {
        startCell = TQString(_data[0]).toUInt();
        }

    for (TQ_UINT32 index = 1; index < _data.length(); ++index)
        {
        endCell = TQString(_data[index]).toUInt();

        startPoint = lookupCellCoords(startCell);
        endPoint = lookupCellCoords(endCell);

        if (index == 1)
            {
      // Draw something to show the starting point
            p.drawRect(startPoint.x()-2, startPoint.y()-2, 4, 4);
            p.fillRect(startPoint.x()-2, startPoint.y()-2, 4, 4,
                       TQBrush(black));
            }

        p.drawLine(startPoint, endPoint);
        drawArrowHead(startPoint, endPoint, p);

        startCell = endCell;
        }

    p.end();

    TQFrame::paintEvent(ev);
    }

TQPoint GestureDrawer::lookupCellCoords(TQ_UINT32 cell)
    {
  // First divide the widget into thirds, horizontally and vertically
    TQ_UINT32 w = width();
    TQ_UINT32 h = height();

    TQ_UINT32 wThird = w / 3;
    TQ_UINT32 hThird = h / 3;

    switch(cell)
        {
        case 1:
            return TQPoint(wThird/2, 2*hThird+hThird/2);

        case 2:
            return TQPoint(wThird+wThird/2, 2*hThird+hThird/2);

        case 3:
            return TQPoint(2*wThird+wThird/2, 2*hThird+hThird/2);

        case 4:
            return TQPoint(wThird/2, hThird+hThird/2);

        case 5:
            return TQPoint(wThird+wThird/2, hThird+hThird/2);

        case 6:
            return TQPoint(2*wThird+wThird/2, hThird+hThird/2);

        case 7:
            return TQPoint(wThird/2, hThird/2);

        case 8:
            return TQPoint(wThird+wThird/2, hThird/2);

        case 9:
            return TQPoint(2*wThird+wThird/2, hThird/2);
        }

    return TQPoint(0, 0);
    }

void GestureDrawer::drawArrowHead(TQPoint &start, TQPoint &end,
                                  TQPainter &p)
    {
    int deltaX = end.x() - start.x();
    int deltaY = end.y() - start.y();

    if (deltaY == 0)
        {
    // horizontal line
        int offset = 0;
        if (deltaX > 0)
          offset = -3;
        else
          offset = 3;

        p.drawLine(TQPoint(end.x()+offset, end.y()+2), end);
        p.drawLine(TQPoint(end.x()+offset, end.y()-2), end);
        }
    else if (deltaX == 0)
        {
    // vertical line
        int offset = 0;
        if (deltaY > 0)
          offset = -3;
        else
          offset = +3;

        p.drawLine(TQPoint(end.x()+2, end.y()+offset), end);
        p.drawLine(TQPoint(end.x()-2, end.y()+offset), end);
        }
    else
        {
    // diagnal - The math would be pretty complex, so don't do anything
        }

    }

} // namespace KHotKeys

#include "gesturedrawer.moc"