From e21e24989fda288a6540dda6941c67754b400c74 2012-03-13 07:53:17 From: Michal Klocek Date: 2012-03-13 07:53:17 Subject: [PATCH] Adds clicked(Point) to lineSeries, changes visible points handling --- diff --git a/src/charttheme.cpp b/src/charttheme.cpp index d6b9789..eaa6116 100644 --- a/src/charttheme.cpp +++ b/src/charttheme.cpp @@ -96,12 +96,13 @@ void ChartTheme::decorate(LineChartItem* item, QLineSeries* series,int index) { QPen pen; if(pen != series->pen()){ - item->setLinePen(series->pen()); + series->setPen(series->pen()); return; + }else{ + pen.setColor(m_seriesColors.at(index%m_seriesColors.size())); + pen.setWidthF(2); + series->setPen(pen); } - pen.setColor(m_seriesColors.at(index%m_seriesColors.size())); - pen.setWidthF(2); - item->setLinePen(pen); } void ChartTheme::decorate(BarPresenter* item, QBarSeries* series,int index) diff --git a/src/linechart/linechartitem.cpp b/src/linechart/linechartitem.cpp index 11f3653..dec0881 100644 --- a/src/linechart/linechartitem.cpp +++ b/src/linechart/linechartitem.cpp @@ -2,6 +2,7 @@ #include "qlineseries.h" #include "chartpresenter_p.h" #include +#include QTCOMMERCIALCHART_BEGIN_NAMESPACE @@ -10,11 +11,11 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE LineChartItem::LineChartItem(QLineSeries* series,QGraphicsItem *parent):XYChartItem(series,parent), m_series(series), -m_items(this) +m_pointsVisible(false) { setZValue(ChartPresenter::LineChartZValue); QObject::connect(series,SIGNAL(updated()),this,SLOT(handleUpdated())); - QObject::connect(this,SIGNAL(clicked()),series,SIGNAL(clicked())); + QObject::connect(this,SIGNAL(clicked(const QPointF&)),series,SIGNAL(clicked(const QPointF&))); handleUpdated(); } @@ -28,79 +29,31 @@ QPainterPath LineChartItem::shape() const return m_path; } -void LineChartItem::createPoints(int count) -{ - for (int i = 0; i < count; ++i) { - QGraphicsRectItem* item = new QGraphicsRectItem(0,0,3,3); - m_items.addToGroup(item); - } -} - -void LineChartItem::deletePoints(int count) -{ - QList items = m_items.childItems(); - - for (int i = 0; i < count; ++i) { - delete(items.takeLast()); - } -} - void LineChartItem::setGeometry(QVector& points) { if(points.size()==0) return; - int diff = XYChartItem::points().size() - points.size(); - - if(diff>0) { - deletePoints(diff); - } - else if(diff<0) { - createPoints(-diff); - } - QList items = m_items.childItems(); - QPainterPath path; - const QPointF& point = points.at(0); - path.moveTo(point); - QGraphicsItem* item = items.at(0); - item->setPos(point.x()-1,point.y()-1); - if(!clipRect().contains(point)) { - item->setVisible(false); - } - else { - item->setVisible(true); - } + QPainterPath linePath(points.at(0)); for(int i=1; i< points.size();i++) { - QGraphicsItem* item = items.at(i); - const QPointF& point = points.at(i); - item->setPos(point.x()-1,point.y()-1); - if(!clipRect().contains(point)) { - item->setVisible(false); - } - else { - item->setVisible(true); - } - path.lineTo(point); + linePath.lineTo(points.at(i)); } prepareGeometryChange(); - m_path = path; - m_rect = path.boundingRect(); + m_path = linePath; + m_rect = linePath.boundingRect(); XYChartItem::setGeometry(points); } -void LineChartItem::setLinePen(const QPen& pen) -{ - m_pen = pen; -} - void LineChartItem::handleUpdated() { - m_items.setVisible(m_series->pointsVisible()); - setLinePen(m_series->pen()); + m_pointsVisible = m_series->pointsVisible(); + m_linePen = m_series->pen(); + m_pointPen = m_series->pen(); + m_pointPen.setWidthF(2*m_pointPen.width()); update(); } @@ -111,15 +64,19 @@ void LineChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *opt Q_UNUSED(widget); Q_UNUSED(option); painter->save(); - painter->setPen(m_pen); + painter->setPen(m_linePen); painter->setClipRect(clipRect()); painter->drawPath(m_path); + if(m_pointsVisible){ + painter->setPen(m_pointPen); + painter->drawPoints(points()); + } painter->restore(); } void LineChartItem::mousePressEvent( QGraphicsSceneMouseEvent * event ) { - emit clicked(); + emit clicked(calculateDomainPoint(event->pos())); } #include "moc_linechartitem_p.cpp" diff --git a/src/linechart/linechartitem_p.h b/src/linechart/linechartitem_p.h index c65e97d..3026f42 100644 --- a/src/linechart/linechartitem_p.h +++ b/src/linechart/linechartitem_p.h @@ -21,29 +21,24 @@ public: void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); QPainterPath shape() const; - void setLinePen(const QPen& pen); - void setPointsVisible(bool visible); - public slots: void handleUpdated(); signals: - void clicked(); + void clicked(const QPointF& point); protected: virtual void setGeometry(QVector& points); void mousePressEvent( QGraphicsSceneMouseEvent * event ); private: - void createPoints(int count); - void deletePoints(int count); - -private: QLineSeries* m_series; QGraphicsItemGroup m_items; QPainterPath m_path; QRectF m_rect; - QPen m_pen; + QPen m_linePen; + QPen m_pointPen; + bool m_pointsVisible; }; diff --git a/src/linechart/qlineseries.h b/src/linechart/qlineseries.h index 6f7a9d4..2847b59 100644 --- a/src/linechart/qlineseries.h +++ b/src/linechart/qlineseries.h @@ -23,7 +23,7 @@ public: bool pointsVisible() const {return m_pointsVisible;} signals: - void clicked(); + void clicked(const QPointF& point); public: // from QChartSeries virtual QSeriesType type() const {return QSeries::SeriesTypeLine;} diff --git a/src/xychart/xychartitem.cpp b/src/xychart/xychartitem.cpp index 31ac314..25dd43f 100644 --- a/src/xychart/xychartitem.cpp +++ b/src/xychart/xychartitem.cpp @@ -56,6 +56,15 @@ QVector XYChartItem::calculateGeometryPoints() const return points; } +QPointF XYChartItem::calculateDomainPoint(const QPointF& point) const +{ + const qreal deltaX = m_size.width()/(m_maxX-m_minX); + const qreal deltaY = m_size.height()/(m_maxY-m_minY); + qreal x = point.x()/deltaX +m_minX; + qreal y = (point.y()-m_size.height())/(-deltaY)+ m_minY; + return QPointF(x,y); +} + void XYChartItem::updatePoints(QVector& points) { if(m_animator){ diff --git a/src/xychart/xychartitem_p.h b/src/xychart/xychartitem_p.h index 7fde2d7..a3eb83c 100644 --- a/src/xychart/xychartitem_p.h +++ b/src/xychart/xychartitem_p.h @@ -31,6 +31,7 @@ protected: virtual void setGeometry(QVector& points); QPointF calculateGeometryPoint(const QPointF& point) const; QPointF calculateGeometryPoint(int index) const; + QPointF calculateDomainPoint(const QPointF& point) const; QVector calculateGeometryPoints() const; private: