summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMarek Rosa <marek.rosa@digia.com>2012-10-11 12:49:43 +0300
committerMarek Rosa <marek.rosa@digia.com>2012-10-11 12:50:20 +0300
commit128c1c36c6738ba46367c37212367bb8cae0ae96 (patch)
treef83cdb78b60e5c18a656008d46e551c256ce851e
parent15c75629b37272b395c795ce3e4ecb6acdd0fe33 (diff)
Added callout example (Drawing on top of chart)
-rw-r--r--examples/callout/callout.cpp102
-rw-r--r--examples/callout/callout.h32
-rw-r--r--examples/callout/callout.pro13
-rw-r--r--examples/callout/main.cpp11
-rw-r--r--examples/callout/widget.cpp56
-rw-r--r--examples/callout/widget.h29
-rw-r--r--examples/examples.pro3
7 files changed, 245 insertions, 1 deletions
diff --git a/examples/callout/callout.cpp b/examples/callout/callout.cpp
new file mode 100644
index 00000000..c49a7431
--- /dev/null
+++ b/examples/callout/callout.cpp
@@ -0,0 +1,102 @@
+#include "callout.h"
+#include <QPainter>
+#include <QFontMetrics>
+#include <QGraphicsSceneMouseEvent>
+#include <QCursor>
+
+Callout::Callout(QGraphicsItem * parent):
+ QGraphicsItem(parent)
+{
+}
+
+QRectF Callout::boundingRect() const
+{
+ QPointF anchor = mapFromParent(m_anchor);
+ QRectF rect;
+ rect.setLeft(qMin(m_textRect.left(), anchor.x()));
+ rect.setRight(qMax(m_textRect.right(), anchor.x()));
+ rect.setTop(qMin(m_textRect.top(), anchor.y()));
+ rect.setBottom(qMax(m_textRect.bottom(), anchor.y()));
+ return rect;
+}
+
+void Callout::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
+{
+ Q_UNUSED(option)
+ Q_UNUSED(widget)
+ QPainterPath path;
+ path.addRoundedRect(m_textRect, 5, 5);
+
+ QPointF anchor = mapFromParent(m_anchor);
+ if (!m_textRect.contains(anchor)) {
+ QPointF point1, point2;
+
+ // establish the position of the anchor point in relation to m_textRect
+ bool above = anchor.y() <= m_textRect.top();
+ bool aboveCenter = anchor.y() > m_textRect.top() && anchor.y() <= m_textRect.center().y();
+ bool belowCenter = anchor.y() > m_textRect.center().y() && anchor.y() <= m_textRect.bottom();
+ bool below = anchor.y() > m_textRect.bottom();
+
+ bool onLeft = anchor.x() <= m_textRect.left();
+ bool leftOfCenter = anchor.x() > m_textRect.left() && anchor.x() <= m_textRect.center().x();
+ bool rightOfCenter = anchor.x() > m_textRect.center().x() && anchor.x() <= m_textRect.right();
+ bool onRight = anchor.x() > m_textRect.right();
+
+ // get the nearest m_textRect corner.
+ qreal x = (onRight + rightOfCenter) * m_textRect.width();
+ qreal y = (below + belowCenter) * m_textRect.height();
+ bool cornerCase = (above && onLeft) || (above && onRight) || (below && onLeft) || (below && onRight);
+ bool vertical = qAbs(anchor.x() - x) > qAbs(anchor.y() - y);
+
+ qreal x1 = x + leftOfCenter * 10 - rightOfCenter * 20 + cornerCase * !vertical * (onLeft * 10 - onRight * 20);
+ qreal y1 = y + aboveCenter * 10 - belowCenter * 20 + cornerCase * vertical * (above * 10 - below * 20);;
+ point1.setX(x1);
+ point1.setY(y1);
+
+ qreal x2 = x + leftOfCenter * 20 - rightOfCenter * 10 + cornerCase * !vertical * (onLeft * 20 - onRight * 10);;
+ qreal y2 = y + aboveCenter * 20 - belowCenter * 10 + cornerCase * vertical * (above * 20 - below * 10);;
+ point2.setX(x2);
+ point2.setY(y2);
+
+ path.moveTo(point1);
+ path.lineTo(mapFromParent(m_anchor));
+ path.lineTo(point2);
+ path = path.simplified();
+ }
+ painter->setBrush(QColor(255, 255, 255));
+ painter->drawPath(path);
+ painter->drawText(m_textRect.adjusted(2, 2, 0, 0), m_text);
+}
+
+void Callout::mousePressEvent(QGraphicsSceneMouseEvent *event)
+{
+ if (m_textRect.contains(event->pos())) {
+ m_clickOffset = event->pos();
+ event->setAccepted(true);
+ } else {
+ event->setAccepted(false);
+ }
+}
+
+void Callout::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
+{
+ if (m_textRect.contains(event->pos())){
+ setPos(mapToParent(event->pos() - m_clickOffset));
+ event->setAccepted(true);
+ } else {
+ event->setAccepted(false);
+ }
+}
+
+void Callout::setText(const QString &text)
+{
+ m_text = text;
+ QFontMetrics metrics(m_font);
+ prepareGeometryChange();
+ m_textRect = metrics.boundingRect(QRect(0, 0, 150, 150), Qt::AlignLeft, m_text).adjusted(0, 0, 4, 4);
+}
+
+void Callout::setAnchor(QPointF point)
+{
+ m_anchor = point;
+}
diff --git a/examples/callout/callout.h b/examples/callout/callout.h
new file mode 100644
index 00000000..90499f52
--- /dev/null
+++ b/examples/callout/callout.h
@@ -0,0 +1,32 @@
+#ifndef CALLOUT_H
+#define CALLOUT_H
+
+#include <QGraphicsItem>
+#include <QFont>
+
+class QGraphicsSceneMouseEvent;
+
+class Callout : public QGraphicsItem
+{
+public:
+ Callout(QGraphicsItem * parent = 0);
+
+ void setText(const QString &text);
+ void setAnchor(QPointF point);
+
+ QRectF boundingRect() const;
+ void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,QWidget *widget);
+
+protected:
+ void mousePressEvent(QGraphicsSceneMouseEvent *event);
+ void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
+
+private:
+ QString m_text;
+ QRectF m_textRect;
+ QPointF m_anchor;
+ QFont m_font;
+ QPointF m_clickOffset;
+};
+
+#endif // CALLOUT_H
diff --git a/examples/callout/callout.pro b/examples/callout/callout.pro
new file mode 100644
index 00000000..0df4a5b5
--- /dev/null
+++ b/examples/callout/callout.pro
@@ -0,0 +1,13 @@
+!include( ../examples.pri ) {
+ error( "Couldn't find the examples.pri file!" )
+}
+
+TARGET = callout
+TEMPLATE = app
+
+SOURCES += main.cpp\
+ widget.cpp \
+ callout.cpp
+
+HEADERS += widget.h \
+ callout.h
diff --git a/examples/callout/main.cpp b/examples/callout/main.cpp
new file mode 100644
index 00000000..7b1f424b
--- /dev/null
+++ b/examples/callout/main.cpp
@@ -0,0 +1,11 @@
+#include <QApplication>
+#include "widget.h"
+
+int main(int argc, char *argv[])
+{
+ QApplication a(argc, argv);
+ Widget w;
+ w.show();
+
+ return a.exec();
+}
diff --git a/examples/callout/widget.cpp b/examples/callout/widget.cpp
new file mode 100644
index 00000000..51748347
--- /dev/null
+++ b/examples/callout/widget.cpp
@@ -0,0 +1,56 @@
+#include "widget.h"
+#include <QGraphicsScene>
+#include <QGraphicsView>
+#include <QVBoxLayout>
+#include <QChart>
+#include <QLineSeries>
+#include <QGraphicsTextItem>
+#include <QGraphicsLineItem>
+#include "callout.h"
+
+QTCOMMERCIALCHART_USE_NAMESPACE
+
+Widget::Widget(QWidget *parent)
+ : QWidget(parent),
+ m_scene(0),
+ m_chart(0),
+ m_view(0)
+{
+ // chart
+ m_chart = new QChart;
+ m_chart->setMinimumSize(640, 480);
+ QLineSeries *series = new QLineSeries;
+ series->setName("Click the line to create a movable callout");
+ series->append(1, 3);
+ series->append(4, 5);
+ series->append(5, 4.5);
+ series->append(7, 1);
+ series->append(11, 2);
+ m_chart->addSeries(series);
+ m_chart->createDefaultAxes();
+
+ m_scene = new QGraphicsScene;
+ m_view = new QGraphicsView(m_scene);
+ m_view->setRenderHint(QPainter::Antialiasing);
+ m_scene->addItem(m_chart);
+
+ QVBoxLayout *mainLayout = new QVBoxLayout;
+ mainLayout->addWidget(m_view);
+ setLayout(mainLayout);
+
+ connect(series, SIGNAL(clicked(QPointF)), this, SLOT(addCallout(QPointF)));
+}
+
+Widget::~Widget()
+{
+
+}
+
+void Widget::addCallout(QPointF point)
+{
+ Callout *label = new Callout(m_chart);
+ label->setText(QString("X: %1\nY: %2").arg(point.x()).arg(point.y()));
+ label->setAnchor(m_chart->mapFromParent(m_view->mapToScene(m_view->mapFromGlobal(QCursor::pos()))));
+ label->setPos(m_chart->mapFromParent(m_view->mapToScene(m_view->mapFromGlobal(QCursor::pos() + QPoint(10, -50)))));
+ label->setZValue(11);
+}
diff --git a/examples/callout/widget.h b/examples/callout/widget.h
new file mode 100644
index 00000000..5cf07336
--- /dev/null
+++ b/examples/callout/widget.h
@@ -0,0 +1,29 @@
+#ifndef WIDGET_H
+#define WIDGET_H
+
+#include <QWidget>
+#include <QChart>
+
+class QGraphicsScene;
+class QGraphicsView;
+
+QTCOMMERCIALCHART_USE_NAMESPACE
+
+class Widget : public QWidget
+{
+ Q_OBJECT
+
+public:
+ Widget(QWidget *parent = 0);
+ ~Widget();
+
+public slots:
+ void addCallout(QPointF point);
+
+private:
+ QGraphicsScene *m_scene;
+ QChart *m_chart;
+ QGraphicsView *m_view;
+};
+
+#endif // WIDGET_H
diff --git a/examples/examples.pro b/examples/examples.pro
index 4aaddc4a..185b8426 100644
--- a/examples/examples.pro
+++ b/examples/examples.pro
@@ -31,7 +31,8 @@ SUBDIRS += \
scrollchart \
temperaturerecords \
donutchart \
- multiaxis
+ multiaxis \
+ callout
!linux-arm*: {
SUBDIRS += \