scatterpresenter.cpp
129 lines
| 4.0 KiB
| text/x-c
|
CppLexer
Tero Ahola
|
r194 | #include "scatterpresenter_p.h" | ||
Tero Ahola
|
r158 | #include "qscatterseries.h" | ||
#include <QPen> | ||||
#include <QPainter> | ||||
#include <QGraphicsScene> | ||||
Tero Ahola
|
r316 | #include <QGraphicsSceneMouseEvent> | ||
Tero Ahola
|
r346 | #include <QGraphicsDropShadowEffect> | ||
Tero Ahola
|
r158 | #include <QDebug> | ||
Tero Ahola
|
r278 | #include <QTime> | ||
Tero Ahola
|
r158 | |||
QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||||
ScatterPresenter::ScatterPresenter(QScatterSeries *series, QGraphicsObject *parent) : | ||||
ChartItem(parent), | ||||
m_series(series), | ||||
m_boundingRect(), | ||||
m_visibleChartArea() | ||||
{ | ||||
if (parent) | ||||
m_boundingRect = parent->boundingRect(); | ||||
if (series) { | ||||
connect(series, SIGNAL(changed()), this, SLOT(handleModelChanged())); | ||||
} | ||||
Tero Ahola
|
r346 | |||
QGraphicsDropShadowEffect *dropShadow = new QGraphicsDropShadowEffect(); | ||||
dropShadow->setOffset(2.0); | ||||
setGraphicsEffect(dropShadow); | ||||
Tero Ahola
|
r158 | } | ||
void ScatterPresenter::handleDomainChanged(const Domain& domain) | ||||
{ | ||||
m_visibleChartArea = domain; | ||||
changeGeometry(); | ||||
} | ||||
void ScatterPresenter::handleGeometryChanged(const QRectF& rect) | ||||
{ | ||||
m_boundingRect = rect; | ||||
changeGeometry(); | ||||
} | ||||
void ScatterPresenter::handleModelChanged() | ||||
{ | ||||
// TODO: more fine grained modelChanged signaling | ||||
changeGeometry(); | ||||
} | ||||
void ScatterPresenter::paint(QPainter *painter, const QStyleOptionGraphicsItem */*option*/, QWidget */*widget*/) | ||||
{ | ||||
Tero Ahola
|
r278 | painter->save(); | ||
painter->setClipRect(m_boundingRect); | ||||
// Paint the shape | ||||
Tero Ahola
|
r316 | // The custom settings in series override those defined by the theme | ||
Tero Ahola
|
r278 | QPen pen = m_markerPen; | ||
if (m_series->markerPen().color().isValid()) | ||||
pen = m_series->markerPen(); | ||||
Tero Ahola
|
r261 | if (m_series->markerBrush().color().isValid()) | ||
painter->setBrush(m_series->markerBrush()); | ||||
else | ||||
Tero Ahola
|
r195 | painter->setBrush(m_markerBrush); | ||
Tero Ahola
|
r278 | painter->setPen(pen); | ||
Tero Ahola
|
r316 | painter->drawPath(m_path); | ||
Tero Ahola
|
r278 | painter->restore(); | ||
Tero Ahola
|
r158 | } | ||
Tero Ahola
|
r300 | void ScatterPresenter::mousePressEvent(QGraphicsSceneMouseEvent *event) | ||
{ | ||||
Tero Ahola
|
r316 | qDebug() << "ScatterPresenter::mousePressEvent" << event << " cont: " | ||
<< m_path.contains(event->lastPos()); | ||||
if (m_path.contains(event->lastPos())) | ||||
emit clicked(); | ||||
Tero Ahola
|
r300 | } | ||
Tero Ahola
|
r158 | void ScatterPresenter::changeGeometry() | ||
{ | ||||
if (m_boundingRect.isValid()) { | ||||
prepareGeometryChange(); | ||||
qreal scalex = m_boundingRect.width() / m_visibleChartArea.spanX(); | ||||
qreal scaley = m_boundingRect.height() / m_visibleChartArea.spanY(); | ||||
Tero Ahola
|
r316 | int shape = m_series->markerShape(); | ||
m_path = QPainterPath(); | ||||
Tero Ahola
|
r158 | foreach (QPointF point, m_series->data()) { | ||
Tero Ahola
|
r316 | // Convert relative coordinates to absolute pixel coordinates that can be used for drawing | ||
qreal x = m_boundingRect.left() + point.x() * scalex - m_visibleChartArea.m_minX * scalex; | ||||
qreal y = m_boundingRect.bottom() - point.y() * scaley + m_visibleChartArea.m_minY * scaley; | ||||
if (scene()->width() > x && scene()->height() > y) { | ||||
switch (shape) { | ||||
case QScatterSeries::MarkerShapeDefault: | ||||
// Fallthrough, defaults to circle | ||||
case QScatterSeries::MarkerShapeCircle: | ||||
m_path.addEllipse(x, y, 9, 9); | ||||
break; | ||||
case QScatterSeries::MarkerShapePoint: | ||||
m_path.addEllipse(x, y, 2, 2); | ||||
break; | ||||
case QScatterSeries::MarkerShapeRectangle: | ||||
m_path.addRect(x, y, 9, 9); | ||||
break; | ||||
case QScatterSeries::MarkerShapeTiltedRectangle: { | ||||
// TODO: | ||||
// static const QPointF points[4] = { | ||||
// QPointF(-1.0 + x, 0.0 + y), | ||||
// QPointF(0.0 + x, 1.0 + y), | ||||
// QPointF(1.0 + x, 0.0 + y), | ||||
// QPointF(0.0 + x, -1.0 + y) | ||||
// }; | ||||
//m_path.addPolygon(QPolygon(4, &points)); | ||||
break; | ||||
} | ||||
default: | ||||
// TODO: implement the rest of the shapes | ||||
Q_ASSERT(false); | ||||
break; | ||||
} | ||||
} | ||||
Tero Ahola
|
r158 | } | ||
} | ||||
} | ||||
Tero Ahola
|
r194 | #include "moc_scatterpresenter_p.cpp" | ||
Tero Ahola
|
r158 | |||
QTCOMMERCIALCHART_END_NAMESPACE | ||||