From 1937d28dc8703ef892a686c6f4bffdb09a122604 2012-12-04 12:58:52 From: Marek Rosa Date: 2012-12-04 12:58:52 Subject: [PATCH] Updated callout example --- diff --git a/examples/callout/callout.cpp b/examples/callout/callout.cpp index 5a26da3..9f1de8c 100644 --- a/examples/callout/callout.cpp +++ b/examples/callout/callout.cpp @@ -2,7 +2,7 @@ #include #include #include -#include +#include Callout::Callout(QGraphicsItem * parent): QGraphicsItem(parent) diff --git a/examples/callout/callout.pro b/examples/callout/callout.pro index 0df4a5b..b0da66d 100644 --- a/examples/callout/callout.pro +++ b/examples/callout/callout.pro @@ -5,9 +5,11 @@ TARGET = callout TEMPLATE = app -SOURCES += main.cpp\ - widget.cpp \ - callout.cpp +SOURCES += \ + main.cpp\ + callout.cpp \ + view.cpp -HEADERS += widget.h \ - callout.h +HEADERS += \ + callout.h \ + view.h diff --git a/examples/callout/main.cpp b/examples/callout/main.cpp index 7b1f424..18858fd 100644 --- a/examples/callout/main.cpp +++ b/examples/callout/main.cpp @@ -1,10 +1,10 @@ #include -#include "widget.h" +#include "view.h" int main(int argc, char *argv[]) { QApplication a(argc, argv); - Widget w; + View w; w.show(); return a.exec(); diff --git a/examples/callout/widget.cpp b/examples/callout/view.cpp similarity index 60% rename from examples/callout/widget.cpp rename to examples/callout/view.cpp index d0ac439..be3ec13 100644 --- a/examples/callout/widget.cpp +++ b/examples/callout/view.cpp @@ -1,26 +1,47 @@ -#include "widget.h" +/**************************************************************************** + ** + ** 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$ + ** + ****************************************************************************/ + +#include "view.h" +#include #include -#include -#include #include #include #include #include -#include #include "callout.h" +#include -QTCOMMERCIALCHART_USE_NAMESPACE - -Widget::Widget(QWidget *parent) - : QWidget(parent), - m_scene(0), +View::View(QWidget *parent) + : QGraphicsView(new QGraphicsScene, parent), + m_coordX(0), + m_coordY(0), m_chart(0), - m_view(0), m_tooltip(0) { + setDragMode(QGraphicsView::NoDrag); + setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); + setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); + // chart m_chart = new QChart; - m_chart->setMinimumSize(640, 480); + m_chart->setMinimumSize(800, 600); m_chart->setTitle("Hover the line to show callout. Click the line to make it stay"); m_chart->legend()->hide(); QLineSeries *series = new QLineSeries; @@ -40,34 +61,47 @@ Widget::Widget(QWidget *parent) m_chart->addSeries(series2); m_chart->createDefaultAxes(); + m_chart->setAcceptHoverEvents(true); - m_scene = new QGraphicsScene; - m_view = new QGraphicsView(m_scene); - m_view->setRenderHint(QPainter::Antialiasing); - m_scene->addItem(m_chart); + setRenderHint(QPainter::Antialiasing); + scene()->addItem(m_chart); - QVBoxLayout *mainLayout = new QVBoxLayout; - mainLayout->addWidget(m_view); - setLayout(mainLayout); + m_coordX = new QGraphicsSimpleTextItem(m_chart); + m_coordX->setPos(m_chart->size().width()/2 - 50, m_chart->size().height()); + m_coordX->setText("X: "); + m_coordY = new QGraphicsSimpleTextItem(m_chart); + m_coordY->setPos(m_chart->size().width()/2 + 50, m_chart->size().height()); + m_coordY->setText("Y: "); connect(series, SIGNAL(clicked(QPointF)), this, SLOT(keepCallout())); connect(series, SIGNAL(hovered(QPointF, bool)), this, SLOT(tooltip(QPointF,bool))); connect(series2, SIGNAL(clicked(QPointF)), this, SLOT(keepCallout())); connect(series2, SIGNAL(hovered(QPointF, bool)), this, SLOT(tooltip(QPointF,bool))); + + this->setMouseTracking(true); +} + +void View::resizeEvent(QResizeEvent *event) +{ + if (scene()) + scene()->setSceneRect(QRect(QPoint(0, 0), event->size())); + QGraphicsView::resizeEvent(event); } -Widget::~Widget() +void View::mouseMoveEvent(QMouseEvent *event) { - + m_coordX->setText(QString("X: %1").arg(m_chart->mapToValue(event->pos()).x())); + m_coordY->setText(QString("Y: %1").arg(m_chart->mapToValue(event->pos()).y())); + QGraphicsView::mouseMoveEvent(event); } -void Widget::keepCallout() +void View::keepCallout() { m_tooltip = new Callout(m_chart); } -void Widget::tooltip(QPointF point, bool state) +void View::tooltip(QPointF point, bool state) { if (m_tooltip == 0) m_tooltip = new Callout(m_chart); diff --git a/examples/callout/view.h b/examples/callout/view.h new file mode 100644 index 0000000..06fe3c1 --- /dev/null +++ b/examples/callout/view.h @@ -0,0 +1,59 @@ +/**************************************************************************** +** +** 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$ +** +****************************************************************************/ + +#ifndef VIEW_H +#define VIEW_H +#include +#include + +QTCOMMERCIALCHART_BEGIN_NAMESPACE +class QChart; +QTCOMMERCIALCHART_END_NAMESPACE + +class QGraphicsScene; +class QResizeEvent; +class Callout; +class QMouseEvent; + +QTCOMMERCIALCHART_USE_NAMESPACE + +class View: public QGraphicsView +{ + Q_OBJECT + +public: + View(QWidget *parent = 0); + +protected: + void resizeEvent(QResizeEvent *event); + void mouseMoveEvent(QMouseEvent *event); + +public slots: + void keepCallout(); + void tooltip(QPointF point, bool state); + +private: + QGraphicsSimpleTextItem *m_coordX; + QGraphicsSimpleTextItem *m_coordY; + QChart *m_chart; + Callout *m_tooltip; +}; + +#endif diff --git a/examples/callout/widget.h b/examples/callout/widget.h deleted file mode 100644 index 7c5041a..0000000 --- a/examples/callout/widget.h +++ /dev/null @@ -1,32 +0,0 @@ -#ifndef WIDGET_H -#define WIDGET_H - -#include -#include - -class QGraphicsScene; -class QGraphicsView; -class Callout; - -QTCOMMERCIALCHART_USE_NAMESPACE - -class Widget : public QWidget -{ - Q_OBJECT - -public: - Widget(QWidget *parent = 0); - ~Widget(); - -public slots: - void keepCallout(); - void tooltip(QPointF point, bool state); - -private: - QGraphicsScene *m_scene; - QChart *m_chart; - QGraphicsView *m_view; - Callout *m_tooltip; -}; - -#endif // WIDGET_H