scatterchartitem.cpp
190 lines
| 5.0 KiB
| text/x-c
|
CppLexer
Jani Honkonen
|
r794 | /**************************************************************************** | ||
** | ||||
** 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$ | ||||
** | ||||
****************************************************************************/ | ||||
Michal Klocek
|
r470 | #include "scatterchartitem_p.h" | ||
#include "qscatterseries.h" | ||||
#include "chartpresenter_p.h" | ||||
#include <QPainter> | ||||
#include <QGraphicsScene> | ||||
QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||||
Michal Klocek
|
r677 | ScatterChartItem::ScatterChartItem(QScatterSeries *series, ChartPresenter *presenter) : | ||
XYChartItem(series,presenter), | ||||
Michal Klocek
|
r470 | m_series(series), | ||
m_items(this), | ||||
m_shape(QScatterSeries::MarkerShapeRectangle), | ||||
Tero Ahola
|
r653 | m_size(15) | ||
Michal Klocek
|
r470 | |||
{ | ||||
Michal Klocek
|
r541 | QObject::connect(m_series,SIGNAL(updated()), this, SLOT(handleUpdated())); | ||
Michal Klocek
|
r470 | |||
setZValue(ChartPresenter::ScatterSeriesZValue); | ||||
setFlags(QGraphicsItem::ItemHasNoContents); | ||||
setFlags(QGraphicsItem::ItemClipsChildrenToShape); | ||||
handleUpdated(); | ||||
Michal Klocek
|
r541 | m_items.setHandlesChildEvents(false); | ||
Michal Klocek
|
r470 | // TODO: how to draw a drop shadow? | ||
// QGraphicsDropShadowEffect *dropShadow = new QGraphicsDropShadowEffect(); | ||||
// dropShadow->setOffset(2.0); | ||||
// dropShadow->setBlurRadius(2.0); | ||||
// setGraphicsEffect(dropShadow); | ||||
} | ||||
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) { | ||||
Michal Klocek
|
r541 | case QScatterSeries::MarkerShapeCircle:{ | ||
QGraphicsEllipseItem* i = new QGraphicsEllipseItem(0,0,m_size,m_size); | ||||
const QRectF& rect = i->boundingRect(); | ||||
i->setPos(-rect.width()/2,-rect.height()/2); | ||||
item = new Marker(i,this); | ||||
Michal Klocek
|
r470 | break; | ||
Michal Klocek
|
r541 | } | ||
case QScatterSeries::MarkerShapeRectangle:{ | ||||
QGraphicsRectItem* i = new QGraphicsRectItem(0,0,m_size,m_size); | ||||
i->setPos(-m_size/2,-m_size/2); | ||||
item = new Marker(i,this); | ||||
Michal Klocek
|
r470 | break; | ||
Michal Klocek
|
r541 | } | ||
default: | ||||
qWarning()<<"Unsupported marker type"; | ||||
Michal Klocek
|
r470 | break; | ||
Michal Klocek
|
r541 | |||
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) { | ||||
delete(items.takeLast()); | ||||
} | ||||
} | ||||
sauimone
|
r743 | void ScatterChartItem::markerSelected(Marker *marker) | ||
Michal Klocek
|
r470 | { | ||
Michal Klocek
|
r571 | emit XYChartItem::clicked(QPointF(m_series->x(marker->index()), m_series->y(marker->index()))); | ||
Michal Klocek
|
r470 | } | ||
Michal Klocek
|
r557 | void ScatterChartItem::setLayout(QVector<QPointF>& points) | ||
Michal Klocek
|
r470 | { | ||
Marek Rosa
|
r545 | if(points.size()==0) | ||
{ | ||||
Michal Klocek
|
r557 | XYChartItem::setLayout(points); | ||
Marek Rosa
|
r545 | return; | ||
} | ||||
Michal Klocek
|
r470 | |||
int diff = XYChartItem::points().size() - points.size(); | ||||
if(diff>0) { | ||||
deletePoints(diff); | ||||
} | ||||
else if(diff<0) { | ||||
createPoints(-diff); | ||||
} | ||||
if(diff!=0) handleUpdated(); | ||||
QList<QGraphicsItem*> items = m_items.childItems(); | ||||
for(int i=0; i< points.size();i++) { | ||||
Michal Klocek
|
r541 | Marker* item = static_cast<Marker*>(items.at(i)); | ||
Michal Klocek
|
r470 | const QPointF& point = points.at(i); | ||
Michal Klocek
|
r536 | const QRectF& rect = item->boundingRect(); | ||
Michal Klocek
|
r541 | item->setIndex(i); | ||
Michal Klocek
|
r536 | item->setPos(point.x()-rect.width()/2,point.y()-rect.height()/2); | ||
Michal Klocek
|
r470 | if(!clipRect().contains(point)) { | ||
item->setVisible(false); | ||||
} | ||||
else { | ||||
item->setVisible(true); | ||||
} | ||||
} | ||||
prepareGeometryChange(); | ||||
m_rect = clipRect(); | ||||
Michal Klocek
|
r557 | XYChartItem::setLayout(points); | ||
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) | ||||
{ | ||||
foreach(QGraphicsItem* item , m_items.childItems()) { | ||||
Michal Klocek
|
r541 | static_cast<Marker*>(item)->setPen(pen); | ||
Michal Klocek
|
r470 | } | ||
} | ||||
void ScatterChartItem::setBrush(const QBrush& brush) | ||||
{ | ||||
foreach(QGraphicsItem* item , m_items.childItems()) { | ||||
Michal Klocek
|
r541 | static_cast<Marker*>(item)->setBrush(brush); | ||
Michal Klocek
|
r470 | } | ||
} | ||||
void ScatterChartItem::handleUpdated() | ||||
{ | ||||
int count = m_items.childItems().count(); | ||||
if(count==0) return; | ||||
bool recreate = m_size != m_series->size() || m_shape != m_series->shape(); | ||||
//TODO: only rewrite on size change | ||||
m_size = m_series->size(); | ||||
m_shape = m_series->shape(); | ||||
if(recreate){ | ||||
deletePoints(count); | ||||
createPoints(count); | ||||
} | ||||
setPen(m_series->pen()); | ||||
setBrush(m_series->brush()); | ||||
} | ||||
#include "moc_scatterchartitem_p.cpp" | ||||
QTCOMMERCIALCHART_END_NAMESPACE | ||||