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

« back to all changes in this revision

Viewing changes to qwt-5.1.1/src/qwt_plot_panner.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Fathi Boudra
  • Date: 2008-05-26 10:26:31 UTC
  • mfrom: (1.1.3 upstream) (2.1.1 lenny)
  • Revision ID: james.westby@ubuntu.com-20080526102631-bp95mfccnrb957nx
Tags: 5.1.1-1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
 
2
 * Qwt Widget Library
 
3
 * Copyright (C) 1997   Josef Wilgen
 
4
 * Copyright (C) 2002   Uwe Rathmann
 
5
 *
 
6
 * This library is free software; you can redistribute it and/or
 
7
 * modify it under the terms of the Qwt License, Version 1.0
 
8
 *****************************************************************************/
 
9
 
 
10
// vim: expandtab
 
11
 
 
12
#include "qwt_scale_div.h"
 
13
#include "qwt_plot.h"
 
14
#include "qwt_plot_canvas.h"
 
15
#include "qwt_plot_panner.h"
 
16
 
 
17
class QwtPlotPanner::PrivateData
 
18
{
 
19
public:
 
20
    PrivateData()
 
21
    {
 
22
        for ( int axis = 0; axis < QwtPlot::axisCnt; axis++ )
 
23
            isAxisEnabled[axis] = true;
 
24
    }
 
25
 
 
26
    bool isAxisEnabled[QwtPlot::axisCnt];
 
27
};
 
28
 
 
29
/*!
 
30
  \brief Create a plot panner
 
31
 
 
32
  The panner is enabled for all axes
 
33
 
 
34
  \param canvas Plot canvas to pan, also the parent object
 
35
 
 
36
  \sa setAxisEnabled
 
37
*/
 
38
QwtPlotPanner::QwtPlotPanner(QwtPlotCanvas *canvas):
 
39
    QwtPanner(canvas)
 
40
{
 
41
    d_data = new PrivateData();
 
42
 
 
43
    connect(this, SIGNAL(panned(int, int)),
 
44
        SLOT(moveCanvas(int, int)));
 
45
}
 
46
 
 
47
//! Destructor
 
48
QwtPlotPanner::~QwtPlotPanner()
 
49
{
 
50
    delete d_data;
 
51
}
 
52
 
 
53
/*!
 
54
   \brief En/Disable an axis
 
55
 
 
56
   Axes that are enabled will be synchronized to the
 
57
   result of panning. All other axes will remain unchanged.
 
58
 
 
59
   \param axis Axis, see QwtPlot::Axis
 
60
   \param on On/Off
 
61
 
 
62
   \sa isAxisEnabled, moveCanvas
 
63
*/
 
64
void QwtPlotPanner::setAxisEnabled(int axis, bool on)
 
65
{
 
66
    if ( axis >= 0 && axis < QwtPlot::axisCnt )
 
67
        d_data->isAxisEnabled[axis] = on;
 
68
}
 
69
 
 
70
/*!
 
71
   Test if an axis is enabled
 
72
 
 
73
   \param axis Axis, see QwtPlot::Axis
 
74
   \return True, if the axis is enabled
 
75
   
 
76
   \sa setAxisEnabled, moveCanvas
 
77
*/
 
78
bool QwtPlotPanner::isAxisEnabled(int axis) const
 
79
{
 
80
    if ( axis >= 0 && axis < QwtPlot::axisCnt )
 
81
        return d_data->isAxisEnabled[axis];
 
82
 
 
83
    return true;
 
84
}
 
85
 
 
86
//! Return observed plot canvas
 
87
QwtPlotCanvas *QwtPlotPanner::canvas()
 
88
{
 
89
    QWidget *w = parentWidget();
 
90
    if ( w && w->inherits("QwtPlotCanvas") )
 
91
        return (QwtPlotCanvas *)w;
 
92
 
 
93
    return NULL;
 
94
}
 
95
 
 
96
//! Return Observed plot canvas
 
97
const QwtPlotCanvas *QwtPlotPanner::canvas() const
 
98
{
 
99
    return ((QwtPlotPanner *)this)->canvas();
 
100
}
 
101
 
 
102
//! Return plot widget, containing the observed plot canvas
 
103
QwtPlot *QwtPlotPanner::plot()
 
104
{
 
105
    QObject *w = canvas();
 
106
    if ( w )
 
107
    {
 
108
        w = w->parent();
 
109
        if ( w && w->inherits("QwtPlot") )
 
110
            return (QwtPlot *)w;
 
111
    }
 
112
 
 
113
    return NULL;
 
114
}
 
115
 
 
116
//! Return plot widget, containing the observed plot canvas
 
117
const QwtPlot *QwtPlotPanner::plot() const
 
118
{
 
119
    return ((QwtPlotPanner *)this)->plot();
 
120
}
 
121
 
 
122
/*!
 
123
   Adjust the enabled axes according to dx/dy
 
124
 
 
125
   \param dx Pixel offset in x direction
 
126
   \param dy Pixel offset in y direction
 
127
 
 
128
   \sa QwtPanner::panned()
 
129
*/
 
130
void QwtPlotPanner::moveCanvas(int dx, int dy)
 
131
{
 
132
    if ( dx == 0 && dy == 0 )
 
133
        return;
 
134
 
 
135
    QwtPlot *plot = QwtPlotPanner::plot();
 
136
    if ( plot == NULL )
 
137
        return;
 
138
    
 
139
    const bool doAutoReplot = plot->autoReplot();
 
140
    plot->setAutoReplot(false);
 
141
 
 
142
    for ( int axis = 0; axis < QwtPlot::axisCnt; axis++ )
 
143
    {
 
144
        if ( !d_data->isAxisEnabled[axis] )
 
145
            continue;
 
146
 
 
147
        const QwtScaleMap map = plot->canvasMap(axis);
 
148
 
 
149
        const int i1 = map.transform(plot->axisScaleDiv(axis)->lBound());
 
150
        const int i2 = map.transform(plot->axisScaleDiv(axis)->hBound());
 
151
 
 
152
        double d1, d2;
 
153
        if ( axis == QwtPlot::xBottom || axis == QwtPlot::xTop )
 
154
        {
 
155
            d1 = map.invTransform(i1 - dx);
 
156
            d2 = map.invTransform(i2 - dx);
 
157
        }
 
158
        else
 
159
        {
 
160
            d1 = map.invTransform(i1 - dy);
 
161
            d2 = map.invTransform(i2 - dy);
 
162
        }
 
163
 
 
164
        plot->setAxisScale(axis, d1, d2);
 
165
    }
 
166
 
 
167
    plot->setAutoReplot(doAutoReplot);
 
168
    plot->replot();
 
169
}