summaryrefslogtreecommitdiffstats
path: root/demos
diff options
context:
space:
mode:
authorTero Ahola <tero.ahola@digia.com>2012-08-16 12:49:51 +0300
committerTero Ahola <tero.ahola@digia.com>2012-08-16 12:49:51 +0300
commit877b494897a01b1b7d948410ca7d47285191d6d9 (patch)
treebe7bd83a96e7b28ef80184d6dacc0594e279ce21 /demos
parent9e99014de1f1e4b30d2227391bce17fb6e988e3c (diff)
Fixed paint and mouse event issues with QLineSeries
1. The bounding rectangle of QLineSeries item did not take line width into account; this caused paint issues if the width is bigger than 1. 2. The width of the shape of the item was always 1. This caused problems with mouse events; onClicked was not always signaled, even if you click on top (but not in the middle of) a thick line series.
Diffstat (limited to 'demos')
-rw-r--r--demos/chartinteractions/chart.cpp22
-rw-r--r--demos/chartinteractions/chart.h1
-rw-r--r--demos/chartinteractions/main.cpp3
3 files changed, 16 insertions, 10 deletions
diff --git a/demos/chartinteractions/chart.cpp b/demos/chartinteractions/chart.cpp
index 986f5ae6..38c40643 100644
--- a/demos/chartinteractions/chart.cpp
+++ b/demos/chartinteractions/chart.cpp
@@ -20,6 +20,7 @@
#include <QValuesAxis>
#include <QAbstractAxis>
+#include <cmath>
#include <QDebug>
#include "chart.h"
@@ -36,22 +37,23 @@ Chart::~Chart()
void Chart::clickPoint(const QPointF &point)
{
- //Get all points from the series.
- QList<QPointF> points = m_series->points();
- //Construct a small rectangle around the clicked point
- //to identify the real point clicked from the series.
- QRectF clickRect(point.x() - 0.5, point.y() - 0.5, 1.0, 1.0);
-
- //Find the clicked point to be moved.
- foreach (QPointF p, points) {
- if (clickRect.contains(p)) {
+ // Find the closes data point
+ m_movingPoint = QPoint();
+ m_clicked = false;
+ foreach (QPointF p, m_series->points()) {
+ if (distance(p, point) < distance(m_movingPoint, point)) {
m_movingPoint = p;
m_clicked = true;
- return;
}
}
}
+qreal Chart::distance(const QPointF &p1, const QPointF &p2)
+{
+ return sqrt((p1.x() - p2.x()) * (p1.x() - p2.x())
+ + (p1.y() - p2.y()) * (p1.y() - p2.y()));
+}
+
void Chart::setPointClicked(bool clicked)
{
m_clicked = clicked;
diff --git a/demos/chartinteractions/chart.h b/demos/chartinteractions/chart.h
index f9e021af..242aa65f 100644
--- a/demos/chartinteractions/chart.h
+++ b/demos/chartinteractions/chart.h
@@ -41,6 +41,7 @@ public:
void setPointClicked(bool clicked);
private:
+ qreal distance(const QPointF &p1, const QPointF &p2);
QLineSeries *m_series;
QPointF m_movingPoint;
diff --git a/demos/chartinteractions/main.cpp b/demos/chartinteractions/main.cpp
index 22ab027f..7b5db6c2 100644
--- a/demos/chartinteractions/main.cpp
+++ b/demos/chartinteractions/main.cpp
@@ -46,6 +46,9 @@ int main(int argc, char *argv[])
Chart* chart = new Chart(0, 0, series);
chart->legend()->hide();
chart->addSeries(series);
+ QPen p = series->pen();
+ p.setWidth(5);
+ series->setPen(p);
chart->createDefaultAxes();
chart->setTitle("Drag'n drop to move data points");