~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to demos/shared/hoverpoints.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-08-24 04:09:09 UTC
  • Revision ID: james.westby@ubuntu.com-20050824040909-xmxe9jfr4a0w5671
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include "hoverpoints.h"
 
2
 
 
3
#define printf
 
4
 
 
5
HoverPoints::HoverPoints(QWidget *widget, PointShape shape)
 
6
    : QObject(widget)
 
7
{
 
8
    m_widget = widget;
 
9
    widget->installEventFilter(this);
 
10
 
 
11
    m_connectionType = CurveConnection;
 
12
    m_sortType = NoSort;
 
13
    m_shape = shape;
 
14
    m_pointPen = QPen(QColor(255, 255, 255, 191), 1);
 
15
    m_connectionPen = QPen(QColor(255, 255, 255, 127), 2);
 
16
    m_pointBrush = QBrush(QColor(191, 191, 191, 127));
 
17
    m_pointSize = QSize(11, 11);
 
18
    m_currentIndex = -1;
 
19
    m_editable = true;
 
20
    m_enabled = true;
 
21
 
 
22
    connect(this, SIGNAL(pointsChanged(const QPolygonF &)),
 
23
            m_widget, SLOT(update()));
 
24
}
 
25
 
 
26
 
 
27
void HoverPoints::setEnabled(bool enabled)
 
28
{
 
29
    if (m_enabled != enabled) {
 
30
        m_enabled = enabled;
 
31
        m_widget->update();
 
32
    }
 
33
}
 
34
 
 
35
 
 
36
bool HoverPoints::eventFilter(QObject *object, QEvent *event)
 
37
{
 
38
    if (object == m_widget && m_enabled) {
 
39
        switch (event->type()) {
 
40
 
 
41
        case QEvent::MouseButtonPress:
 
42
            {
 
43
                QMouseEvent *me = (QMouseEvent *) event;
 
44
 
 
45
                QPointF clickPos = me->pos();
 
46
                int index = -1;
 
47
                for (int i=0; i<m_points.size(); ++i) {
 
48
                    QPainterPath path;
 
49
                    if (m_shape == CircleShape)
 
50
                        path.addEllipse(pointBoundingRect(i));
 
51
                    else
 
52
                        path.addRect(pointBoundingRect(i));
 
53
 
 
54
                    if (path.contains(clickPos)) {
 
55
                        index = i;
 
56
                        break;
 
57
                    }
 
58
                }
 
59
 
 
60
                if (me->button() == Qt::LeftButton) {
 
61
                    if (index == -1) {
 
62
                        if (!m_editable)
 
63
                            return false;
 
64
                        int pos = 0;
 
65
                        // Insert sort for x or y
 
66
                        if (m_sortType == XSort) {
 
67
                            for (int i=0; i<m_points.size(); ++i)
 
68
                                if (m_points.at(i).x() > clickPos.x()) {
 
69
                                    pos = i;
 
70
                                    break;
 
71
                                }
 
72
                        } else if (m_sortType == YSort) {
 
73
                            for (int i=0; i<m_points.size(); ++i)
 
74
                                if (m_points.at(i).y() > clickPos.y()) {
 
75
                                    pos = i;
 
76
                                    break;
 
77
                                }
 
78
                        }
 
79
 
 
80
                        m_points.insert(pos, clickPos);
 
81
                        m_locks.insert(pos, 0);
 
82
                        m_currentIndex = pos;
 
83
                        firePointChange();
 
84
                    } else {
 
85
                        m_currentIndex = index;
 
86
                    }
 
87
                    return true;
 
88
 
 
89
                } else if (me->button() == Qt::RightButton) {
 
90
                    if (index >= 0 && m_editable) {
 
91
                        if (m_locks[index] == 0) {
 
92
                            m_locks.remove(index);
 
93
                            m_points.remove(index);
 
94
                        }
 
95
                        firePointChange();
 
96
                        return true;
 
97
                    }
 
98
                }
 
99
 
 
100
            }
 
101
            break;
 
102
 
 
103
        case QEvent::MouseButtonRelease:
 
104
            m_currentIndex = -1;
 
105
            break;
 
106
 
 
107
        case QEvent::MouseMove:
 
108
            if (m_currentIndex >= 0)
 
109
                movePoint(m_currentIndex, ((QMouseEvent *)event)->pos());
 
110
            break;
 
111
 
 
112
        case QEvent::Resize:
 
113
            {
 
114
                QResizeEvent *e = (QResizeEvent *) event;
 
115
                double stretch_x = e->size().width() / double(e->oldSize().width());
 
116
                double stretch_y = e->size().height() / double(e->oldSize().height());
 
117
                for (int i=0; i<m_points.size(); ++i) {
 
118
                    QPointF p = m_points[i];
 
119
                    movePoint(i, QPointF(p.x() * stretch_x, p.y() * stretch_y), false);
 
120
                }
 
121
 
 
122
                firePointChange();
 
123
                break;
 
124
            }
 
125
 
 
126
        case QEvent::Paint:
 
127
            {
 
128
                QWidget *that_widget = m_widget;
 
129
                m_widget = 0;
 
130
                QApplication::sendEvent(object, event);
 
131
                m_widget = that_widget;
 
132
                paintPoints();
 
133
                return true;
 
134
            }
 
135
        default:
 
136
            break;
 
137
        }
 
138
    }
 
139
 
 
140
    return false;
 
141
}
 
142
 
 
143
 
 
144
void HoverPoints::paintPoints()
 
145
{
 
146
    QPainter p(m_widget);
 
147
 
 
148
    p.setRenderHint(QPainter::Antialiasing);
 
149
 
 
150
    if (m_connectionPen.style() != Qt::NoPen && m_connectionType != NoConnection) {
 
151
        p.setPen(m_connectionPen);
 
152
 
 
153
        if (m_connectionType == CurveConnection) {
 
154
            QPainterPath path;
 
155
            path.moveTo(m_points.at(0));
 
156
            for (int i=1; i<m_points.size(); ++i) {
 
157
                QPointF p1 = m_points.at(i-1);
 
158
                QPointF p2 = m_points.at(i);
 
159
                double distance = p2.x() - p1.x();
 
160
 
 
161
                path.cubicTo(p1.x() + distance / 2, p1.y(),
 
162
                             p1.x() + distance / 2, p2.y(),
 
163
                             p2.x(), p2.y());
 
164
            }
 
165
            p.drawPath(path);
 
166
        } else {
 
167
            p.drawPolyline(m_points);
 
168
        }
 
169
    }
 
170
 
 
171
    p.setPen(m_pointPen);
 
172
    p.setBrush(m_pointBrush);
 
173
 
 
174
    for (int i=0; i<m_points.size(); ++i) {
 
175
        QRectF bounds = pointBoundingRect(i);
 
176
        if (m_shape == CircleShape)
 
177
            p.drawEllipse(bounds);
 
178
        else
 
179
            p.drawRect(bounds);
 
180
    }
 
181
}
 
182
 
 
183
static QPointF bound_point(const QPointF &point, const QRectF &bounds, int lock)
 
184
{
 
185
    QPointF p = point;
 
186
 
 
187
    double left = bounds.left();
 
188
    double right = bounds.right();
 
189
    double top = bounds.top();
 
190
    double bottom = bounds.bottom();
 
191
 
 
192
    if (p.x() < left || (lock & HoverPoints::LockToLeft)) p.setX(left);
 
193
    else if (p.x() > right || (lock & HoverPoints::LockToRight)) p.setX(right);
 
194
 
 
195
    if (p.y() < top || (lock & HoverPoints::LockToTop)) p.setY(top);
 
196
    else if (p.y() > bottom || (lock & HoverPoints::LockToBottom)) p.setY(bottom);
 
197
 
 
198
    return p;
 
199
}
 
200
 
 
201
void HoverPoints::setPoints(const QPolygonF &points)
 
202
{
 
203
    m_points.clear();
 
204
    for (int i=0; i<points.size(); ++i)
 
205
        m_points << bound_point(points.at(i), boundingRect(), 0);
 
206
 
 
207
    m_locks.clear();
 
208
    if (m_points.size() > 0) {
 
209
        m_locks.resize(m_points.size());
 
210
 
 
211
        m_locks.fill(0);
 
212
    }
 
213
}
 
214
 
 
215
 
 
216
void HoverPoints::movePoint(int index, const QPointF &point, bool emitUpdate)
 
217
{
 
218
    m_points[index] = bound_point(point, boundingRect(), m_locks.at(index));
 
219
    if (emitUpdate)
 
220
        firePointChange();
 
221
}
 
222
 
 
223
 
 
224
inline static bool x_less_than(const QPointF &p1, const QPointF &p2)
 
225
{
 
226
    return p1.x() < p2.x();
 
227
}
 
228
 
 
229
 
 
230
inline static bool y_less_than(const QPointF &p1, const QPointF &p2)
 
231
{
 
232
    return p1.y() < p2.y();
 
233
}
 
234
 
 
235
void HoverPoints::firePointChange()
 
236
{
 
237
//    printf("HoverPoints::firePointChange(), current=%d\n", m_currentIndex);
 
238
 
 
239
    if (m_sortType != NoSort) {
 
240
 
 
241
        QPointF oldCurrent;
 
242
        if (m_currentIndex != -1) {
 
243
            oldCurrent = m_points[m_currentIndex];
 
244
        }
 
245
 
 
246
        if (m_sortType == XSort)
 
247
            qSort(m_points.begin(), m_points.end(), x_less_than);
 
248
        else if (m_sortType == YSort)
 
249
            qSort(m_points.begin(), m_points.end(), y_less_than);
 
250
 
 
251
        // Compensate for changed order...
 
252
        if (m_currentIndex != -1) {
 
253
            for (int i=0; i<m_points.size(); ++i) {
 
254
                if (m_points[i] == oldCurrent) {
 
255
                    m_currentIndex = i;
 
256
                    break;
 
257
                }
 
258
            }
 
259
        }
 
260
 
 
261
//         printf(" - firePointChange(), current=%d\n", m_currentIndex);
 
262
 
 
263
    }
 
264
 
 
265
//     for (int i=0; i<m_points.size(); ++i) {
 
266
//         printf(" - point(%2d)=[%.2f, %.2f], lock=%d\n",
 
267
//                i, m_points.at(i).x(), m_points.at(i).y(), m_locks.at(i));
 
268
//     }
 
269
 
 
270
    emit pointsChanged(m_points);
 
271
}