scatterchartitem.cpp
190 lines
| 5.4 KiB
| text/x-c
|
CppLexer
Jani Honkonen
|
r794 | /**************************************************************************** | ||
Michal Klocek
|
r1218 | ** | ||
** Copyright (C) 2012 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 Commercial Charts Add-on. | ||||
** | ||||
** $QT_BEGIN_LICENSE$ | ||||
** Licensees holding valid Qt Commercial licenses may use this file in | ||||
** accordance with the Qt Commercial 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$ | ||||
** | ||||
****************************************************************************/ | ||||
Jani Honkonen
|
r794 | |||
Michal Klocek
|
r470 | #include "scatterchartitem_p.h" | ||
#include "qscatterseries.h" | ||||
Michal Klocek
|
r938 | #include "qscatterseries_p.h" | ||
Michal Klocek
|
r470 | #include "chartpresenter_p.h" | ||
#include <QPainter> | ||||
#include <QGraphicsScene> | ||||
Michal Klocek
|
r996 | #include <QDebug> | ||
Michal Klocek
|
r1218 | #include <QGraphicsSceneMouseEvent> | ||
Michal Klocek
|
r470 | |||
QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||||
Jani Honkonen
|
r2097 | ScatterChartItem::ScatterChartItem(QScatterSeries *series, ChartPresenter *presenter) | ||
: XYChart(series, presenter), | ||||
QGraphicsItem(presenter ? presenter->rootItem() : 0), | ||||
m_series(series), | ||||
m_items(this), | ||||
m_visible(true), | ||||
m_shape(QScatterSeries::MarkerShapeRectangle), | ||||
m_size(15) | ||||
Michal Klocek
|
r470 | { | ||
Jani Honkonen
|
r2097 | QObject::connect(m_series->d_func(), SIGNAL(updated()), this, SLOT(handleUpdated())); | ||
Tero Ahola
|
r1349 | QObject::connect(m_series, SIGNAL(visibleChanged()), this, SLOT(handleUpdated())); | ||
Tero Ahola
|
r2067 | QObject::connect(m_series, SIGNAL(opacityChanged()), this, SLOT(handleUpdated())); | ||
Michal Klocek
|
r470 | |||
setZValue(ChartPresenter::ScatterSeriesZValue); | ||||
setFlags(QGraphicsItem::ItemClipsChildrenToShape); | ||||
handleUpdated(); | ||||
Michal Klocek
|
r541 | m_items.setHandlesChildEvents(false); | ||
Michal Klocek
|
r470 | } | ||
QRectF ScatterChartItem::boundingRect() const | ||||
{ | ||||
return m_rect; | ||||
} | ||||
void ScatterChartItem::createPoints(int count) | ||||
{ | ||||
for (int i = 0; i < count; ++i) { | ||||
Michal Klocek
|
r689 | QGraphicsItem *item = 0; | ||
Michal Klocek
|
r470 | |||
switch (m_shape) { | ||||
Jani Honkonen
|
r2097 | case QScatterSeries::MarkerShapeCircle: { | ||
item = new CircleMarker(0, 0, m_size, m_size, this); | ||||
const QRectF& rect = item->boundingRect(); | ||||
item->setPos(-rect.width() / 2, -rect.height() / 2); | ||||
break; | ||||
Michal Klocek
|
r1218 | } | ||
Jani Honkonen
|
r2097 | case QScatterSeries::MarkerShapeRectangle: { | ||
item = new RectangleMarker(0, 0, m_size, m_size, this); | ||||
item->setPos(-m_size / 2, -m_size / 2); | ||||
break; | ||||
Michal Klocek
|
r1218 | } | ||
Jani Honkonen
|
r2097 | default: | ||
qWarning() << "Unsupported marker type"; | ||||
Michal Klocek
|
r1218 | break; | ||
Michal Klocek
|
r470 | } | ||
m_items.addToGroup(item); | ||||
} | ||||
} | ||||
void ScatterChartItem::deletePoints(int count) | ||||
{ | ||||
QList<QGraphicsItem *> items = m_items.childItems(); | ||||
for (int i = 0; i < count; ++i) { | ||||
Michal Klocek
|
r1763 | QGraphicsItem * item = items.takeLast(); | ||
m_markerMap.remove(item); | ||||
delete(item); | ||||
Michal Klocek
|
r470 | } | ||
} | ||||
Michal Klocek
|
r1763 | void ScatterChartItem::markerSelected(QGraphicsItem *marker) | ||
Michal Klocek
|
r470 | { | ||
Michal Klocek
|
r1763 | emit XYChart::clicked(calculateDomainPoint(m_markerMap[marker])); | ||
Michal Klocek
|
r470 | } | ||
Michal Klocek
|
r1217 | void ScatterChartItem::updateGeometry() | ||
Michal Klocek
|
r470 | { | ||
Michal Klocek
|
r1217 | |||
const QVector<QPointF>& points = geometryPoints(); | ||||
Jani Honkonen
|
r2097 | if (points.size() == 0) { | ||
Marek Rosa
|
r1085 | deletePoints(m_items.childItems().count()); | ||
Marek Rosa
|
r545 | return; | ||
} | ||||
Michal Klocek
|
r470 | |||
Michal Klocek
|
r869 | int diff = m_items.childItems().size() - points.size(); | ||
Michal Klocek
|
r470 | |||
Jani Honkonen
|
r2097 | if (diff > 0) | ||
Michal Klocek
|
r470 | deletePoints(diff); | ||
Jani Honkonen
|
r2097 | else if (diff < 0) | ||
Michal Klocek
|
r470 | createPoints(-diff); | ||
Jani Honkonen
|
r2097 | if (diff != 0) | ||
handleUpdated(); | ||||
Michal Klocek
|
r470 | |||
QList<QGraphicsItem*> items = m_items.childItems(); | ||||
Tero Ahola
|
r847 | for (int i = 0; i < points.size(); i++) { | ||
Michal Klocek
|
r1763 | QGraphicsItem* item = items.at(i); | ||
Michal Klocek
|
r470 | const QPointF& point = points.at(i); | ||
Michal Klocek
|
r536 | const QRectF& rect = item->boundingRect(); | ||
Jani Honkonen
|
r2097 | m_markerMap[item] = point; | ||
item->setPos(point.x() - rect.width() / 2, point.y() - rect.height() / 2); | ||||
if (!m_visible || !clipRect().contains(point)) | ||||
Michal Klocek
|
r470 | item->setVisible(false); | ||
Jani Honkonen
|
r2097 | else | ||
Michal Klocek
|
r470 | item->setVisible(true); | ||
} | ||||
prepareGeometryChange(); | ||||
m_rect = clipRect(); | ||||
Michal Klocek
|
r1218 | setPos(origin()); | ||
Michal Klocek
|
r470 | } | ||
void ScatterChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) | ||||
{ | ||||
Tero Ahola
|
r611 | Q_UNUSED(painter) | ||
Q_UNUSED(option) | ||||
Q_UNUSED(widget) | ||||
Michal Klocek
|
r470 | } | ||
void ScatterChartItem::setPen(const QPen& pen) | ||||
{ | ||||
Jani Honkonen
|
r2097 | foreach (QGraphicsItem *item , m_items.childItems()) | ||
Michal Klocek
|
r1763 | static_cast<QAbstractGraphicsShapeItem*>(item)->setPen(pen); | ||
Michal Klocek
|
r470 | } | ||
void ScatterChartItem::setBrush(const QBrush& brush) | ||||
{ | ||||
Jani Honkonen
|
r2097 | foreach (QGraphicsItem *item , m_items.childItems()) | ||
Michal Klocek
|
r1763 | static_cast<QAbstractGraphicsShapeItem*>(item)->setBrush(brush); | ||
Michal Klocek
|
r470 | } | ||
void ScatterChartItem::handleUpdated() | ||||
{ | ||||
int count = m_items.childItems().count(); | ||||
Jani Honkonen
|
r2097 | if (count == 0) | ||
return; | ||||
Michal Klocek
|
r470 | |||
Tero Ahola
|
r1346 | bool recreate = m_visible != m_series->isVisible() | ||
Jani Honkonen
|
r2097 | || m_size != m_series->markerSize() | ||
|| m_shape != m_series->markerShape(); | ||||
Michal Klocek
|
r470 | |||
Tero Ahola
|
r1346 | m_visible = m_series->isVisible(); | ||
Tero Ahola
|
r1276 | m_size = m_series->markerSize(); | ||
m_shape = m_series->markerShape(); | ||||
Tero Ahola
|
r2067 | setOpacity(m_series->opacity()); | ||
Michal Klocek
|
r470 | |||
Jani Honkonen
|
r2097 | if (recreate) { | ||
Tero Ahola
|
r1276 | // TODO: optimize handleUpdate to recreate points only in case shape changed | ||
Michal Klocek
|
r470 | deletePoints(count); | ||
createPoints(count); | ||||
Tero Ahola
|
r1276 | |||
// Updating geometry is now safe, because it won't call handleUpdated unless it creates/deletes points | ||||
updateGeometry(); | ||||
Michal Klocek
|
r470 | } | ||
setPen(m_series->pen()); | ||||
setBrush(m_series->brush()); | ||||
Tero Ahola
|
r1305 | update(); | ||
Michal Klocek
|
r470 | } | ||
#include "moc_scatterchartitem_p.cpp" | ||||
QTCOMMERCIALCHART_END_NAMESPACE | ||||