summaryrefslogtreecommitdiffstats
path: root/examples/widgets/symbianvibration/vibrationsurface.cpp
blob: 5e2e962badf9363d23d8ffe1c5a19e8f779d4e4f (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
#include "vibrationsurface.h"
#include <QtGui/QPainter>
#include <QtCore/QLine>
#include <QtGui/QMouseEvent>
#include <QtGui/QScreen>
#include <QtCore/QRect>
#include <QtGui/QColor>

#include "xqvibra.h"

//! [4]
const int NumberOfLevels = 10;
const double IntensityFactor = XQVibra::MaxIntensity / NumberOfLevels;
//! [4]

VibrationSurface::VibrationSurface(XQVibra *vibra, QWidget *parent) :
    QWidget(parent),
    vibra(vibra),
    lastIntensity(0)
{
}

//! [0]
void VibrationSurface::mousePressEvent(QMouseEvent *event)
{
    applyIntensity(event->x(), event->y());
    vibra->start();
}
//! [0]

//! [1]
void VibrationSurface::mouseMoveEvent(QMouseEvent *event)
{
    applyIntensity(event->x(), event->y());
}
//! [1]

//! [2]
void VibrationSurface::mouseReleaseEvent(QMouseEvent *)
{
    vibra->stop();
}
//! [2]

//! [5]
void VibrationSurface::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    QRect rect = geometry();
    int dx = 0, dy = 0;

    if (height() > width()) {
        dy = height() / NumberOfLevels;
        rect.setHeight(dy);
    } else {
        dx = width() / NumberOfLevels;
        rect.setWidth(dx);
    }
//! [5]
//! [6] 
    for (int i = 0; i < NumberOfLevels; i++) {
        int x = i * dx;
        int y = i * dy;
        int intensity = getIntensity(x, y);
        QColor color = QColor(40, 80, 10).lighter(100 + intensity);

        rect.moveTo(x, y);
        painter.fillRect(rect, color);
        painter.setPen(color.darker());
        painter.drawText(rect, Qt::AlignCenter, QString::number(intensity));
    }
}
//! [6] 

//! [7] 
int VibrationSurface::getIntensity(int x, int y)
{
    int level;
    int coord;

    if (height() > width()) {
        level = height() / NumberOfLevels;
        coord = y;
    } else {
        level = width() / NumberOfLevels;
        coord = x;
    }

    if (level == 0) {
        return 0;
    }
//! [7] 
//! [8] 
    int intensity = (coord / level + 1) * IntensityFactor;

    if (intensity < 0) {
        intensity = 0;
    } else if (intensity > XQVibra::MaxIntensity) {
        intensity = XQVibra::MaxIntensity;
    }

    return intensity;
}
//! [8] 

//! [3]
void VibrationSurface::applyIntensity(int x, int y)
{
    int intensity = getIntensity(x, y);

    if (intensity != lastIntensity) {
        vibra->setIntensity(intensity);
        lastIntensity = intensity;
    }
}
//! [3]