~ubuntu-branches/ubuntu/oneiric/qwt/oneiric-proposed

« back to all changes in this revision

Viewing changes to examples/event_filter/canvaspicker.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Fathi Boudra
  • Date: 2011-06-10 11:22:47 UTC
  • mfrom: (1.1.6 upstream) (2.1.4 sid)
  • Revision ID: james.westby@ubuntu.com-20110610112247-0i1019vvmzaq6p86
Tags: 6.0.0-1
* New upstream release (Closes: #624107):
  - drop Qt3 support. (Closes: #604379, #626868)
* Register documentation with doc-base. (Closes: #626567)
* Drop patches:
  - 01_makefiles.diff
  - 02_add_missing_warnings.diff
  - 03_qwt_branch_pull_r544.diff
* Add qwt_install_paths.patch to fix the hardcoded installation paths.
* Update debian/control:
  - drop libqt3-mt-dev build dependency.
  - bump Standards-Version to 3.9.2 (no changes).
  - drop Qt3 related packages.
  - due to bump soname (and as we dropper Qt3 support):
    - libqwt5-qt4-dev -> libqwt-dev
    - libqwt5-qt4 -> libqwt6
    - libqwt5-doc -> libqwt-doc
* Update debian/copyright file.
* Update debian/rules: drop Qt3 packages support.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <qapplication.h>
 
2
#include <qevent.h>
 
3
#include <qwhatsthis.h>
 
4
#include <qpainter.h>
 
5
#include <qwt_plot.h>
 
6
#include <qwt_symbol.h>
 
7
#include <qwt_scale_map.h>
 
8
#include <qwt_plot_canvas.h>
 
9
#include <qwt_plot_curve.h>
 
10
#include <qwt_plot_directpainter.h>
 
11
#include "canvaspicker.h"
 
12
 
 
13
CanvasPicker::CanvasPicker(QwtPlot *plot):
 
14
    QObject(plot),
 
15
    d_selectedCurve(NULL),
 
16
    d_selectedPoint(-1)
 
17
{
 
18
    QwtPlotCanvas *canvas = plot->canvas();
 
19
 
 
20
    canvas->installEventFilter(this);
 
21
 
 
22
    // We want the focus, but no focus rect. The
 
23
    // selected point will be highlighted instead.
 
24
 
 
25
    canvas->setFocusPolicy(Qt::StrongFocus);
 
26
#ifndef QT_NO_CURSOR
 
27
    canvas->setCursor(Qt::PointingHandCursor);
 
28
#endif
 
29
    canvas->setFocusIndicator(QwtPlotCanvas::ItemFocusIndicator);
 
30
    canvas->setFocus();
 
31
 
 
32
    const char *text =
 
33
        "All points can be moved using the left mouse button "
 
34
        "or with these keys:\n\n"
 
35
        "- Up:\t\tSelect next curve\n"
 
36
        "- Down:\t\tSelect previous curve\n"
 
37
        "- Left, �-�:\tSelect next point\n"
 
38
        "- Right, �+�:\tSelect previous point\n"
 
39
        "- 7, 8, 9, 4, 6, 1, 2, 3:\tMove selected point";
 
40
    canvas->setWhatsThis(text);
 
41
 
 
42
    shiftCurveCursor(true);
 
43
}
 
44
 
 
45
bool CanvasPicker::event(QEvent *e)
 
46
{
 
47
    if ( e->type() == QEvent::User )
 
48
    {
 
49
        showCursor(true);
 
50
        return true;
 
51
    }
 
52
    return QObject::event(e);
 
53
}
 
54
 
 
55
bool CanvasPicker::eventFilter(QObject *object, QEvent *e)
 
56
{
 
57
    if ( object != (QObject *)plot()->canvas() )
 
58
        return false;
 
59
 
 
60
    switch(e->type())
 
61
    {
 
62
        case QEvent::FocusIn:
 
63
            showCursor(true);
 
64
        case QEvent::FocusOut:
 
65
            showCursor(false);
 
66
 
 
67
        case QEvent::Paint:
 
68
        {   
 
69
            QApplication::postEvent(this, new QEvent(QEvent::User));
 
70
            break;
 
71
        }
 
72
        case QEvent::MouseButtonPress:
 
73
        {
 
74
            select(((QMouseEvent *)e)->pos());
 
75
            return true; 
 
76
        }
 
77
        case QEvent::MouseMove:
 
78
        {
 
79
            move(((QMouseEvent *)e)->pos());
 
80
            return true; 
 
81
        }
 
82
        case QEvent::KeyPress:
 
83
        {
 
84
            const int delta = 5;
 
85
            switch(((const QKeyEvent *)e)->key())
 
86
            {
 
87
                case Qt::Key_Up:
 
88
                    shiftCurveCursor(true);
 
89
                    return true;
 
90
                    
 
91
                case Qt::Key_Down:
 
92
                    shiftCurveCursor(false);
 
93
                    return true;
 
94
 
 
95
                case Qt::Key_Right:
 
96
                case Qt::Key_Plus:
 
97
                    if ( d_selectedCurve )
 
98
                        shiftPointCursor(true);
 
99
                    else
 
100
                        shiftCurveCursor(true);
 
101
                    return true;
 
102
 
 
103
                case Qt::Key_Left:
 
104
                case Qt::Key_Minus:
 
105
                    if ( d_selectedCurve )
 
106
                        shiftPointCursor(false);
 
107
                    else
 
108
                        shiftCurveCursor(true);
 
109
                    return true;
 
110
 
 
111
                // The following keys represent a direction, they are
 
112
                // organized on the keyboard.
 
113
 
 
114
                case Qt::Key_1: 
 
115
                    moveBy(-delta, delta);
 
116
                    break;
 
117
                case Qt::Key_2:
 
118
                    moveBy(0, delta);
 
119
                    break;
 
120
                case Qt::Key_3: 
 
121
                    moveBy(delta, delta);
 
122
                    break;
 
123
                case Qt::Key_4:
 
124
                    moveBy(-delta, 0);
 
125
                    break;
 
126
                case Qt::Key_6: 
 
127
                    moveBy(delta, 0);
 
128
                    break;
 
129
                case Qt::Key_7:
 
130
                    moveBy(-delta, -delta);
 
131
                    break;
 
132
                case Qt::Key_8:
 
133
                    moveBy(0, -delta);
 
134
                    break;
 
135
                case Qt::Key_9:
 
136
                    moveBy(delta, -delta);
 
137
                    break;
 
138
                default:
 
139
                    break;
 
140
            }
 
141
        }
 
142
        default:
 
143
            break;
 
144
    }
 
145
    return QObject::eventFilter(object, e);
 
146
}
 
147
 
 
148
// Select the point at a position. If there is no point
 
149
// deselect the selected point
 
150
 
 
151
void CanvasPicker::select(const QPoint &pos)
 
152
{
 
153
    QwtPlotCurve *curve = NULL;
 
154
    double dist = 10e10;
 
155
    int index = -1;
 
156
 
 
157
    const QwtPlotItemList& itmList = plot()->itemList();
 
158
    for ( QwtPlotItemIterator it = itmList.begin();
 
159
        it != itmList.end(); ++it )
 
160
    {
 
161
        if ( (*it)->rtti() == QwtPlotItem::Rtti_PlotCurve )
 
162
        {
 
163
            QwtPlotCurve *c = (QwtPlotCurve*)(*it);
 
164
 
 
165
            double d;
 
166
            int idx = c->closestPoint(pos, &d);
 
167
            if ( d < dist )
 
168
            {
 
169
                curve = c;
 
170
                index = idx;
 
171
                dist = d;
 
172
            } 
 
173
        }
 
174
    }
 
175
 
 
176
    showCursor(false);
 
177
    d_selectedCurve = NULL;
 
178
    d_selectedPoint = -1;
 
179
 
 
180
    if ( curve && dist < 10 ) // 10 pixels tolerance
 
181
    {
 
182
        d_selectedCurve = curve;
 
183
        d_selectedPoint = index;
 
184
        showCursor(true);
 
185
    }
 
186
}
 
187
 
 
188
// Move the selected point
 
189
void CanvasPicker::moveBy(int dx, int dy)
 
190
{
 
191
    if ( dx == 0 && dy == 0 )
 
192
        return;
 
193
 
 
194
    if ( !d_selectedCurve )
 
195
        return;
 
196
 
 
197
    const QPointF sample = 
 
198
        d_selectedCurve->sample(d_selectedPoint);
 
199
 
 
200
    const double x = plot()->transform(
 
201
        d_selectedCurve->xAxis(), sample.x());
 
202
    const double y = plot()->transform(
 
203
        d_selectedCurve->yAxis(), sample.y());
 
204
 
 
205
    move( QPoint(qRound(x + dx), qRound(y + dy)) );
 
206
}
 
207
 
 
208
// Move the selected point
 
209
void CanvasPicker::move(const QPoint &pos)
 
210
{
 
211
    if ( !d_selectedCurve )
 
212
        return;
 
213
 
 
214
    QVector<double> xData(d_selectedCurve->dataSize());
 
215
    QVector<double> yData(d_selectedCurve->dataSize());
 
216
 
 
217
    for ( int i = 0; i < (int)d_selectedCurve->dataSize(); i++ )
 
218
    {
 
219
        if ( i == d_selectedPoint )
 
220
        {
 
221
            xData[i] = plot()->invTransform(
 
222
                d_selectedCurve->xAxis(), pos.x());
 
223
            yData[i] = plot()->invTransform(
 
224
                d_selectedCurve->yAxis(), pos.y());
 
225
        }
 
226
        else
 
227
        {
 
228
            const QPointF sample = d_selectedCurve->sample(i);
 
229
            xData[i] = sample.x();
 
230
            yData[i] = sample.y();
 
231
        }
 
232
    }
 
233
    d_selectedCurve->setSamples(xData, yData);
 
234
 
 
235
    plot()->replot();
 
236
    showCursor(true);
 
237
}
 
238
 
 
239
// Hightlight the selected point
 
240
void CanvasPicker::showCursor(bool showIt)
 
241
{
 
242
    if ( !d_selectedCurve )
 
243
        return;
 
244
 
 
245
    QwtSymbol *symbol = const_cast<QwtSymbol *>( d_selectedCurve->symbol() );
 
246
 
 
247
    const QBrush brush = symbol->brush();
 
248
    if ( showIt )
 
249
        symbol->setBrush(symbol->brush().color().dark(150));
 
250
 
 
251
    const bool doReplot = plot()->autoReplot();
 
252
 
 
253
    plot()->setAutoReplot(false);
 
254
 
 
255
    QwtPlotDirectPainter directPainter;
 
256
    directPainter.drawSeries(d_selectedCurve, d_selectedPoint, d_selectedPoint);
 
257
 
 
258
    if ( showIt )
 
259
        symbol->setBrush(brush); // reset brush
 
260
 
 
261
    plot()->setAutoReplot(doReplot);
 
262
}
 
263
 
 
264
// Select the next/previous curve 
 
265
void CanvasPicker::shiftCurveCursor(bool up)
 
266
{
 
267
    QwtPlotItemIterator it;
 
268
 
 
269
    const QwtPlotItemList &itemList = plot()->itemList();
 
270
 
 
271
    QwtPlotItemList curveList;
 
272
    for ( it = itemList.begin(); it != itemList.end(); ++it )
 
273
    {
 
274
        if ( (*it)->rtti() == QwtPlotItem::Rtti_PlotCurve )
 
275
            curveList += *it;
 
276
    }
 
277
    if ( curveList.isEmpty() )
 
278
        return;
 
279
 
 
280
    it = curveList.begin();
 
281
 
 
282
    if ( d_selectedCurve )
 
283
    {
 
284
        for ( it = curveList.begin(); it != curveList.end(); ++it )
 
285
        {
 
286
            if ( d_selectedCurve == *it )
 
287
                break;
 
288
        }
 
289
        if ( it == curveList.end() ) // not found
 
290
            it = curveList.begin();
 
291
 
 
292
        if ( up )
 
293
        {
 
294
            ++it;
 
295
            if ( it == curveList.end() )
 
296
                it = curveList.begin();
 
297
        }
 
298
        else
 
299
        {
 
300
            if ( it == curveList.begin() )
 
301
                it = curveList.end();
 
302
            --it;
 
303
        }
 
304
    }
 
305
        
 
306
    showCursor(false);
 
307
    d_selectedPoint = 0;
 
308
    d_selectedCurve = (QwtPlotCurve *)*it;
 
309
    showCursor(true);
 
310
}
 
311
 
 
312
// Select the next/previous neighbour of the selected point
 
313
void CanvasPicker::shiftPointCursor(bool up)
 
314
{
 
315
    if ( !d_selectedCurve )
 
316
        return;
 
317
 
 
318
    int index = d_selectedPoint + (up ? 1 : -1);
 
319
    index = (index + d_selectedCurve->dataSize()) % d_selectedCurve->dataSize();
 
320
 
 
321
    if ( index != d_selectedPoint )
 
322
    {
 
323
        showCursor(false);
 
324
        d_selectedPoint = index;
 
325
        showCursor(true);
 
326
    }
 
327
}