summaryrefslogtreecommitdiffstats
path: root/src/charts/qchartview.cpp
blob: 234ef0808164aaeaeae9601b2699c42c41a1f1eb (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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/****************************************************************************
**
** Copyright (C) 2014 Digia Plc
** All rights reserved.
** For any questions to Digia, please use contact form at http://qt.digia.com
**
** This file is part of the Qt Enterprise Charts Add-on.
**
** $QT_BEGIN_LICENSE$
** Licensees holding valid Qt Enterprise licenses may use this file in
** accordance with the Qt Enterprise License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.
**
** If you have questions regarding the use of this file, please use
** contact form at http://qt.digia.com
** $QT_END_LICENSE$
**
****************************************************************************/

#include "qchartview.h"
#include "qchartview_p.h"
#include "qchart_p.h"
#include <QGraphicsScene>
#include <QRubberBand>

/*!
    \enum QChartView::RubberBand

    This enum describes the different types of rubber bands that can be used for zoom rect selection

    \value NoRubberBand
    \value VerticalRubberBand
    \value HorizonalRubberBand
    \value RectangleRubberBand
*/

/*!
    \class QChartView
    \inmodule Qt Charts
    \brief Standalone charting widget.

    QChartView is a standalone widget that can display charts. It does not require separate
    QGraphicsScene to work. If you want to display a chart in your existing QGraphicsScene,
    you need to use the QChart (or QPolarChart) class instead.

    \sa QChart, QPolarChart
*/

QT_CHARTS_BEGIN_NAMESPACE

/*!
    Constructs a chartView object with parent \a parent.
*/

QChartView::QChartView(QWidget *parent)
    : QGraphicsView(parent),
      d_ptr(new QChartViewPrivate(this))
{

}

/*!
    Constructs a chartview object with parent \a parent to display a \a chart.
    Ownership of the \a chart is passed to chartview.
*/

QChartView::QChartView(QChart *chart, QWidget *parent)
    : QGraphicsView(parent),
      d_ptr(new QChartViewPrivate(this, chart))
{

}


/*!
    Destroys the chartview object and the associated chart.
*/
QChartView::~QChartView()
{
}

/*!
    Returns the pointer to the associated chart.
*/
QChart *QChartView::chart() const
{
    return d_ptr->m_chart;
}

/*!
    Sets the current chart to \a chart. Ownership of the new chart is passed to chartview
    and ownership of the previous chart is released.

    To avoid memory leaks users need to make sure the previous chart is deleted.
*/

void QChartView::setChart(QChart *chart)
{
    d_ptr->setChart(chart);
}

/*!
    Sets the rubber band flags to \a rubberBand.
    Selected flags determine the way zooming is performed.

    \note Rubber band zooming is not supported for polar charts.
*/
void QChartView::setRubberBand(const RubberBands &rubberBand)
{
#ifndef QT_NO_RUBBERBAND
    d_ptr->m_rubberBandFlags = rubberBand;

    if (!d_ptr->m_rubberBandFlags) {
        delete d_ptr->m_rubberBand;
        d_ptr->m_rubberBand = 0;
        return;
    }

    if (!d_ptr->m_rubberBand) {
        d_ptr->m_rubberBand = new QRubberBand(QRubberBand::Rectangle, this);
        d_ptr->m_rubberBand->setEnabled(true);
    }
#else
    Q_UNUSED(rubberBand);
    qWarning("Unable to set rubber band because Qt is configured without it.");
#endif
}

/*!
    Returns the rubber band flags that are currently being used by the widget.
*/
QChartView::RubberBands QChartView::rubberBand() const
{
    return d_ptr->m_rubberBandFlags;
}

/*!
    If Left mouse button is pressed and the rubber band is enabled the \a event is accepted and the rubber band is displayed on the screen allowing the user to select the zoom area.
    If different mouse button is pressed and/or the rubber band is disabled then the \a event is passed to QGraphicsView::mousePressEvent() implementation.
*/
void QChartView::mousePressEvent(QMouseEvent *event)
{
#ifndef QT_NO_RUBBERBAND
    QRectF plotArea = d_ptr->m_chart->plotArea();
    if (d_ptr->m_rubberBand && d_ptr->m_rubberBand->isEnabled()
            && event->button() == Qt::LeftButton && plotArea.contains(event->pos())) {
        d_ptr->m_rubberBandOrigin = event->pos();
        d_ptr->m_rubberBand->setGeometry(QRect(d_ptr->m_rubberBandOrigin, QSize()));
        d_ptr->m_rubberBand->show();
        event->accept();
    } else {
#endif
        QGraphicsView::mousePressEvent(event);
#ifndef QT_NO_RUBBERBAND
    }
#endif
}

/*!
    If the rubber band rectange has been displayed in pressEvent then \a event data is used to update the rubber band geometry.
    Otherwise the default QGraphicsView::mouseMoveEvent implementation is called.
*/
void QChartView::mouseMoveEvent(QMouseEvent *event)
{
#ifndef QT_NO_RUBBERBAND
    if (d_ptr->m_rubberBand && d_ptr->m_rubberBand->isVisible()) {
        QRect rect = d_ptr->m_chart->plotArea().toRect();
        int width = event->pos().x() - d_ptr->m_rubberBandOrigin.x();
        int height = event->pos().y() - d_ptr->m_rubberBandOrigin.y();
        if (!d_ptr->m_rubberBandFlags.testFlag(VerticalRubberBand)) {
            d_ptr->m_rubberBandOrigin.setY(rect.top());
            height = rect.height();
        }
        if (!d_ptr->m_rubberBandFlags.testFlag(HorizonalRubberBand)) {
            d_ptr->m_rubberBandOrigin.setX(rect.left());
            width = rect.width();
        }
        d_ptr->m_rubberBand->setGeometry(QRect(d_ptr->m_rubberBandOrigin.x(), d_ptr->m_rubberBandOrigin.y(), width, height).normalized());
    } else {
#endif
        QGraphicsView::mouseMoveEvent(event);
#ifndef QT_NO_RUBBERBAND
    }
#endif
}

/*!
    If left mouse button is released and the rubber band is enabled then \a event is accepted and
    the view is zoomed into the rect specified by the rubber band.
    If it is a right mouse button \a event then the view is zoomed out.
*/
void QChartView::mouseReleaseEvent(QMouseEvent *event)
{
#ifndef QT_NO_RUBBERBAND
    if (d_ptr->m_rubberBand && d_ptr->m_rubberBand->isVisible()) {
        if (event->button() == Qt::LeftButton) {
            d_ptr->m_rubberBand->hide();
            QRectF rect = d_ptr->m_rubberBand->geometry();
            // Since plotArea uses QRectF and rubberband uses QRect, we can't just blindly use
            // rubberband's dimensions for vertical and horizontal rubberbands, where one
            // dimension must match the corresponding plotArea dimension exactly.
            if (d_ptr->m_rubberBandFlags == VerticalRubberBand) {
                rect.setX(d_ptr->m_chart->plotArea().x());
                rect.setWidth(d_ptr->m_chart->plotArea().width());
            } else if (d_ptr->m_rubberBandFlags == HorizonalRubberBand) {
                rect.setY(d_ptr->m_chart->plotArea().y());
                rect.setHeight(d_ptr->m_chart->plotArea().height());
            }
            d_ptr->m_chart->zoomIn(rect);
            event->accept();
        }

    } else if (d_ptr->m_rubberBand && event->button() == Qt::RightButton) {
            // If vertical or horizontal rubberband mode, restrict zoom out to specified axis.
            // Since there is no suitable API for that, use zoomIn with rect bigger than the
            // plot area.
            if (d_ptr->m_rubberBandFlags == VerticalRubberBand
                || d_ptr->m_rubberBandFlags == HorizonalRubberBand) {
                QRectF rect = d_ptr->m_chart->plotArea();
                if (d_ptr->m_rubberBandFlags == VerticalRubberBand) {
                    qreal adjustment = rect.height() / 2;
                    rect.adjust(0, -adjustment, 0, adjustment);
                } else if (d_ptr->m_rubberBandFlags == HorizonalRubberBand) {
                    qreal adjustment = rect.width() / 2;
                    rect.adjust(-adjustment, 0, adjustment, 0);
                }
                d_ptr->m_chart->zoomIn(rect);
            } else {
                d_ptr->m_chart->zoomOut();
            }
            event->accept();
    } else {
#endif
        QGraphicsView::mouseReleaseEvent(event);
#ifndef QT_NO_RUBBERBAND
    }
#endif
}

/*!
    Resizes and updates the chart area using the \a event data
*/
void QChartView::resizeEvent(QResizeEvent *event)
{
    QGraphicsView::resizeEvent(event);
    d_ptr->resize();
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

QChartViewPrivate::QChartViewPrivate(QChartView *q, QChart *chart)
    : q_ptr(q),
      m_scene(new QGraphicsScene(q)),
      m_chart(chart),
#ifndef QT_NO_RUBBERBAND
      m_rubberBand(0),
#endif
      m_rubberBandFlags(QChartView::NoRubberBand)
{
    q_ptr->setFrameShape(QFrame::NoFrame);
    q_ptr->setBackgroundRole(QPalette::Window);
    q_ptr->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    q_ptr->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    q_ptr->setScene(m_scene);
    q_ptr->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    if (!m_chart)
        m_chart = new QChart();
    m_scene->addItem(m_chart);
}

QChartViewPrivate::~QChartViewPrivate()
{
}

void QChartViewPrivate::setChart(QChart *chart)
{
    Q_ASSERT(chart);

    if (m_chart == chart)
        return;

    if (m_chart)
        m_scene->removeItem(m_chart);

    m_chart = chart;
    m_scene->addItem(m_chart);

    resize();
}

void QChartViewPrivate::resize()
{
    // Fit the chart into view if it has been rotated
    qreal sinA = qAbs(q_ptr->transform().m21());
    qreal cosA = qAbs(q_ptr->transform().m11());
    QSize chartSize = q_ptr->size();

    if (sinA == 1.0) {
        chartSize.setHeight(q_ptr->size().width());
        chartSize.setWidth(q_ptr->size().height());
    } else if (sinA != 0.0) {
        // Non-90 degree rotation, find largest square chart that can fit into the view.
        qreal minDimension = qMin(q_ptr->size().width(), q_ptr->size().height());
        qreal h = (minDimension - (minDimension / ((sinA / cosA) + 1.0))) / sinA;
        chartSize.setHeight(h);
        chartSize.setWidth(h);
    }

    m_chart->resize(chartSize);
    q_ptr->setMinimumSize(m_chart->minimumSize().toSize());
    q_ptr->setSceneRect(m_chart->geometry());
}

#include "moc_qchartview.cpp"

QT_CHARTS_END_NAMESPACE