scatterchartitem.cpp
293 lines
| 9.7 KiB
| text/x-c
|
CppLexer
|
r2854 | /**************************************************************************** | ||
|
r2845 | ** | ||
|
r2854 | ** Copyright (C) 2016 The Qt Company Ltd. | ||
** Contact: https://www.qt.io/licensing/ | ||||
|
r2845 | ** | ||
|
r2854 | ** This file is part of the Qt Charts module of the Qt Toolkit. | ||
|
r2845 | ** | ||
|
r2854 | ** $QT_BEGIN_LICENSE:GPL$ | ||
|
r2845 | ** Commercial License Usage | ||
** Licensees holding valid commercial Qt licenses may use this file in | ||||
** accordance with the commercial license agreement provided with the | ||||
** Software or, alternatively, in accordance with the terms contained in | ||||
** a written agreement between you and The Qt Company. For licensing terms | ||||
|
r2854 | ** and conditions see https://www.qt.io/terms-conditions. For further | ||
** information use the contact form at https://www.qt.io/contact-us. | ||||
** | ||||
** GNU General Public License Usage | ||||
** Alternatively, this file may be used under the terms of the GNU | ||||
** General Public License version 3 or (at your option) any later version | ||||
** approved by the KDE Free Qt Foundation. The licenses are as published by | ||||
** the Free Software Foundation and appearing in the file LICENSE.GPL3 | ||||
** included in the packaging of this file. Please review the following | ||||
** information to ensure the GNU General Public License requirements will | ||||
** be met: https://www.gnu.org/licenses/gpl-3.0.html. | ||||
|
r2845 | ** | ||
** $QT_END_LICENSE$ | ||||
** | ||||
|
r2854 | ****************************************************************************/ | ||
|
r794 | |||
|
r2714 | #include <private/scatterchartitem_p.h> | ||
#include <QtCharts/QScatterSeries> | ||||
#include <private/qscatterseries_p.h> | ||||
#include <private/chartpresenter_p.h> | ||||
#include <private/abstractdomain_p.h> | ||||
#include <QtCharts/QChart> | ||||
#include <QtGui/QPainter> | ||||
#include <QtWidgets/QGraphicsScene> | ||||
#include <QtCore/QDebug> | ||||
#include <QtWidgets/QGraphicsSceneMouseEvent> | ||||
|
r470 | |||
|
r2712 | QT_CHARTS_BEGIN_NAMESPACE | ||
|
r470 | |||
|
r2483 | ScatterChartItem::ScatterChartItem(QScatterSeries *series, QGraphicsItem *item) | ||
|
r2273 | : XYChart(series,item), | ||
|
r2097 | m_series(series), | ||
m_items(this), | ||||
m_visible(true), | ||||
m_shape(QScatterSeries::MarkerShapeRectangle), | ||||
|
r2689 | m_size(15), | ||
m_pointLabelsVisible(false), | ||||
|
r2695 | m_pointLabelsFormat(series->pointLabelsFormat()), | ||
|
r2689 | m_pointLabelsFont(series->pointLabelsFont()), | ||
|
r2739 | m_pointLabelsColor(series->pointLabelsColor()), | ||
|
r2815 | m_pointLabelsClipping(true), | ||
|
r2739 | m_mousePressed(false) | ||
|
r470 | { | ||
|
r2097 | QObject::connect(m_series->d_func(), SIGNAL(updated()), this, SLOT(handleUpdated())); | ||
|
r1349 | QObject::connect(m_series, SIGNAL(visibleChanged()), this, SLOT(handleUpdated())); | ||
|
r2067 | QObject::connect(m_series, SIGNAL(opacityChanged()), this, SLOT(handleUpdated())); | ||
|
r2689 | QObject::connect(series, SIGNAL(pointLabelsFormatChanged(QString)), | ||
this, SLOT(handleUpdated())); | ||||
QObject::connect(series, SIGNAL(pointLabelsVisibilityChanged(bool)), | ||||
this, SLOT(handleUpdated())); | ||||
QObject::connect(series, SIGNAL(pointLabelsFontChanged(QFont)), this, SLOT(handleUpdated())); | ||||
QObject::connect(series, SIGNAL(pointLabelsColorChanged(QColor)), this, SLOT(handleUpdated())); | ||||
|
r2815 | QObject::connect(series, SIGNAL(pointLabelsClippingChanged(bool)), this, SLOT(handleUpdated())); | ||
|
r470 | |||
setZValue(ChartPresenter::ScatterSeriesZValue); | ||||
setFlags(QGraphicsItem::ItemClipsChildrenToShape); | ||||
handleUpdated(); | ||||
|
r541 | m_items.setHandlesChildEvents(false); | ||
|
r470 | } | ||
QRectF ScatterChartItem::boundingRect() const | ||||
{ | ||||
return m_rect; | ||||
} | ||||
void ScatterChartItem::createPoints(int count) | ||||
{ | ||||
for (int i = 0; i < count; ++i) { | ||||
|
r689 | QGraphicsItem *item = 0; | ||
|
r470 | |||
switch (m_shape) { | ||||
|
r2097 | case QScatterSeries::MarkerShapeCircle: { | ||
item = new CircleMarker(0, 0, m_size, m_size, this); | ||||
|
r2104 | const QRectF &rect = item->boundingRect(); | ||
|
r2097 | item->setPos(-rect.width() / 2, -rect.height() / 2); | ||
break; | ||||
|
r2131 | } | ||
|
r2104 | case QScatterSeries::MarkerShapeRectangle: | ||
|
r2097 | item = new RectangleMarker(0, 0, m_size, m_size, this); | ||
item->setPos(-m_size / 2, -m_size / 2); | ||||
break; | ||||
default: | ||||
qWarning() << "Unsupported marker type"; | ||||
|
r1218 | break; | ||
|
r470 | } | ||
m_items.addToGroup(item); | ||||
} | ||||
} | ||||
void ScatterChartItem::deletePoints(int count) | ||||
{ | ||||
QList<QGraphicsItem *> items = m_items.childItems(); | ||||
for (int i = 0; i < count; ++i) { | ||||
|
r2104 | QGraphicsItem *item = items.takeLast(); | ||
|
r1763 | m_markerMap.remove(item); | ||
delete(item); | ||||
|
r470 | } | ||
} | ||||
|
r1763 | void ScatterChartItem::markerSelected(QGraphicsItem *marker) | ||
|
r470 | { | ||
|
r2483 | emit XYChart::clicked(m_markerMap[marker]); | ||
|
r470 | } | ||
|
r2261 | void ScatterChartItem::markerHovered(QGraphicsItem *marker, bool state) | ||
{ | ||||
|
r2483 | emit XYChart::hovered(m_markerMap[marker], state); | ||
|
r2261 | } | ||
|
r2739 | void ScatterChartItem::markerPressed(QGraphicsItem *marker) | ||
{ | ||||
emit XYChart::pressed(m_markerMap[marker]); | ||||
} | ||||
void ScatterChartItem::markerReleased(QGraphicsItem *marker) | ||||
{ | ||||
emit XYChart::released(m_markerMap[marker]); | ||||
} | ||||
void ScatterChartItem::markerDoubleClicked(QGraphicsItem *marker) | ||||
{ | ||||
emit XYChart::doubleClicked(m_markerMap[marker]); | ||||
} | ||||
|
r1217 | void ScatterChartItem::updateGeometry() | ||
|
r470 | { | ||
|
r2820 | if (m_series->useOpenGL()) { | ||
if (m_items.childItems().count()) | ||||
deletePoints(m_items.childItems().count()); | ||||
|
r2830 | if (!m_rect.isEmpty()) { | ||
|
r2820 | prepareGeometryChange(); | ||
|
r2830 | // Changed signal seems to trigger even with empty region | ||
m_rect = QRectF(); | ||||
|
r2820 | } | ||
update(); | ||||
return; | ||||
} | ||||
|
r1217 | |||
const QVector<QPointF>& points = geometryPoints(); | ||||
|
r2097 | if (points.size() == 0) { | ||
|
r1085 | deletePoints(m_items.childItems().count()); | ||
|
r545 | return; | ||
} | ||||
|
r470 | |||
|
r869 | int diff = m_items.childItems().size() - points.size(); | ||
|
r470 | |||
|
r2097 | if (diff > 0) | ||
|
r470 | deletePoints(diff); | ||
|
r2097 | else if (diff < 0) | ||
|
r470 | createPoints(-diff); | ||
|
r2097 | if (diff != 0) | ||
handleUpdated(); | ||||
|
r470 | |||
|
r2104 | QList<QGraphicsItem *> items = m_items.childItems(); | ||
|
r470 | |||
|
r2273 | QRectF clipRect(QPointF(0,0),domain()->size()); | ||
|
r2603 | // Only zoom in if the clipRect fits inside int limits. QWidget::update() uses | ||
// a region that has to be compatible with QRect. | ||||
if (clipRect.height() <= INT_MAX | ||||
&& clipRect.width() <= INT_MAX) { | ||||
QVector<bool> offGridStatus = offGridStatusVector(); | ||||
const int seriesLastIndex = m_series->count() - 1; | ||||
for (int i = 0; i < points.size(); i++) { | ||||
QGraphicsItem *item = items.at(i); | ||||
const QPointF &point = points.at(i); | ||||
const QRectF &rect = item->boundingRect(); | ||||
// During remove animation series may have different number of points, | ||||
// so ensure we don't go over the index. Animation handling itself ensures that | ||||
// if there is actually no points in the series, then it won't generate a fake point, | ||||
// so we can be assured there is always at least one point in m_series here. | ||||
// Note that marker map values can be technically incorrect during the animation, | ||||
// if it was caused by an insert, but this shouldn't be a problem as the points are | ||||
// fake anyway. After remove animation stops, geometry is updated to correct one. | ||||
m_markerMap[item] = m_series->at(qMin(seriesLastIndex, i)); | ||||
|
r2781 | QPointF position; | ||
if (seriesPrivate()->reverseXAxis()) | ||||
position.setX(domain()->size().width() - point.x() - rect.width() / 2); | ||||
else | ||||
position.setX(point.x() - rect.width() / 2); | ||||
if (seriesPrivate()->reverseYAxis()) | ||||
position.setY(domain()->size().height() - point.y() - rect.height() / 2); | ||||
else | ||||
position.setY(point.y() - rect.height() / 2); | ||||
item->setPos(position); | ||||
|
r2603 | |||
if (!m_visible || offGridStatus.at(i)) | ||||
item->setVisible(false); | ||||
else | ||||
item->setVisible(true); | ||||
} | ||||
|
r470 | |||
|
r2603 | prepareGeometryChange(); | ||
m_rect = clipRect; | ||||
} | ||||
|
r470 | } | ||
void ScatterChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) | ||||
{ | ||||
|
r611 | Q_UNUSED(option) | ||
Q_UNUSED(widget) | ||||
|
r2689 | |||
|
r2820 | if (m_series->useOpenGL()) | ||
return; | ||||
|
r2689 | QRectF clipRect = QRectF(QPointF(0, 0), domain()->size()); | ||
painter->save(); | ||||
painter->setClipRect(clipRect); | ||||
|
r2696 | if (m_pointLabelsVisible) { | ||
|
r2815 | if (m_pointLabelsClipping) | ||
painter->setClipping(true); | ||||
else | ||||
painter->setClipping(false); | ||||
|
r2696 | m_series->d_func()->drawSeriesPointLabels(painter, m_points, | ||
m_series->markerSize() / 2 | ||||
+ m_series->pen().width()); | ||||
} | ||||
|
r2689 | |||
painter->restore(); | ||||
|
r470 | } | ||
|
r2104 | void ScatterChartItem::setPen(const QPen &pen) | ||
|
r470 | { | ||
|
r2097 | foreach (QGraphicsItem *item , m_items.childItems()) | ||
|
r1763 | static_cast<QAbstractGraphicsShapeItem*>(item)->setPen(pen); | ||
|
r470 | } | ||
|
r2104 | void ScatterChartItem::setBrush(const QBrush &brush) | ||
|
r470 | { | ||
|
r2097 | foreach (QGraphicsItem *item , m_items.childItems()) | ||
|
r1763 | static_cast<QAbstractGraphicsShapeItem*>(item)->setBrush(brush); | ||
|
r470 | } | ||
void ScatterChartItem::handleUpdated() | ||||
{ | ||||
int count = m_items.childItems().count(); | ||||
|
r2097 | if (count == 0) | ||
return; | ||||
|
r470 | |||
|
r1346 | bool recreate = m_visible != m_series->isVisible() | ||
|
r2097 | || m_size != m_series->markerSize() | ||
|| m_shape != m_series->markerShape(); | ||||
|
r470 | |||
|
r1346 | m_visible = m_series->isVisible(); | ||
|
r1276 | m_size = m_series->markerSize(); | ||
m_shape = m_series->markerShape(); | ||||
|
r2067 | setOpacity(m_series->opacity()); | ||
|
r2689 | m_pointLabelsFormat = m_series->pointLabelsFormat(); | ||
m_pointLabelsVisible = m_series->pointLabelsVisible(); | ||||
m_pointLabelsFont = m_series->pointLabelsFont(); | ||||
m_pointLabelsColor = m_series->pointLabelsColor(); | ||||
|
r2815 | m_pointLabelsClipping = m_series->pointLabelsClipping(); | ||
|
r470 | |||
|
r2407 | if (recreate) { | ||
|
r470 | deletePoints(count); | ||
createPoints(count); | ||||
|
r1276 | |||
// Updating geometry is now safe, because it won't call handleUpdated unless it creates/deletes points | ||||
updateGeometry(); | ||||
|
r470 | } | ||
setPen(m_series->pen()); | ||||
setBrush(m_series->brush()); | ||||
|
r1305 | update(); | ||
|
r470 | } | ||
#include "moc_scatterchartitem_p.cpp" | ||||
|
r2712 | QT_CHARTS_END_NAMESPACE | ||