summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorsauimone <samu.uimonen@digia.com>2012-10-10 11:08:27 +0300
committersauimone <samu.uimonen@digia.com>2012-10-17 10:46:33 +0300
commitaa4c8d495212569ddfca197e7b3bf7048309eec8 (patch)
tree55e8b800069b28bc99528fea9270b3a92b7da5e4
parent10de8edb29c8c42ce4caed306fb12bf2f6fb69de (diff)
legend markers example. bugfix to legend scrolling
-rw-r--r--examples/examples.pro3
-rw-r--r--examples/legendmarkers/legendmarkers.pro10
-rw-r--r--examples/legendmarkers/main.cpp35
-rw-r--r--examples/legendmarkers/mainwidget.cpp181
-rw-r--r--examples/legendmarkers/mainwidget.h63
-rw-r--r--src/legend/legendlayout.cpp11
6 files changed, 301 insertions, 2 deletions
diff --git a/examples/examples.pro b/examples/examples.pro
index c62cf9e5..9b6fe175 100644
--- a/examples/examples.pro
+++ b/examples/examples.pro
@@ -33,7 +33,8 @@ SUBDIRS += \
donutchart \
multiaxis \
callout \
- newlegend
+ newlegend \
+ legendmarkers
!linux-arm*: {
SUBDIRS += \
diff --git a/examples/legendmarkers/legendmarkers.pro b/examples/legendmarkers/legendmarkers.pro
new file mode 100644
index 00000000..f79bdba4
--- /dev/null
+++ b/examples/legendmarkers/legendmarkers.pro
@@ -0,0 +1,10 @@
+!include( ../examples.pri ) {
+ error( "Couldn't find the examples.pri file!" )
+}
+
+TARGET = legendmarkers
+SOURCES += main.cpp \
+ mainwidget.cpp
+
+HEADERS += \
+ mainwidget.h
diff --git a/examples/legendmarkers/main.cpp b/examples/legendmarkers/main.cpp
new file mode 100644
index 00000000..9dec87f4
--- /dev/null
+++ b/examples/legendmarkers/main.cpp
@@ -0,0 +1,35 @@
+/****************************************************************************
+**
+** 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 "mainwidget.h"
+
+#include <QApplication>
+#include <QMainWindow>
+
+int main(int argc, char *argv[])
+{
+ QApplication a(argc, argv);
+
+ MainWidget w;
+ w.resize(720, 480);
+ w.show();
+
+ return a.exec();
+}
diff --git a/examples/legendmarkers/mainwidget.cpp b/examples/legendmarkers/mainwidget.cpp
new file mode 100644
index 00000000..9d34ba03
--- /dev/null
+++ b/examples/legendmarkers/mainwidget.cpp
@@ -0,0 +1,181 @@
+/****************************************************************************
+**
+** 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 "mainwidget.h"
+#include <QChart>
+#include <QChartView>
+#include <QPushButton>
+#include <QLabel>
+#include <QDebug>
+#include <QLegend>
+#include <QFormLayout>
+#include <QLegendMarker>
+#include <QLineSeries>
+#include <QXYLegendMarker>
+#include <qmath.h>
+
+QTCOMMERCIALCHART_USE_NAMESPACE
+
+MainWidget::MainWidget(QWidget *parent) :
+ QWidget(parent)
+{
+ // Create buttons for ui
+ m_buttonLayout = new QGridLayout();
+
+ QPushButton *addSliceButton = new QPushButton("add series");
+ connect(addSliceButton, SIGNAL(clicked()), this, SLOT(addSeries()));
+ m_buttonLayout->addWidget(addSliceButton, 1, 0);
+
+ QPushButton *removeSliceButton = new QPushButton("remove series");
+ connect(removeSliceButton, SIGNAL(clicked()), this, SLOT(removeSeries()));
+ m_buttonLayout->addWidget(removeSliceButton, 2, 0);
+
+ QPushButton *connectButton = new QPushButton("Connect markers");
+ connect(connectButton, SIGNAL(clicked()), this, SLOT(connectMarkers()));
+ m_buttonLayout->addWidget(connectButton, 3, 0);
+
+ QPushButton *disConnectButton = new QPushButton("Disconnect markers");
+ connect(disConnectButton, SIGNAL(clicked()), this, SLOT(disconnectMarkers()));
+ m_buttonLayout->addWidget(disConnectButton, 4, 0);
+
+ // Create chart view with the chart
+ m_chart = new QChart();
+ m_chartView = new QChartView(m_chart, this);
+
+ // Create layout for grid and detached legend
+ m_mainLayout = new QGridLayout();
+ m_mainLayout->addLayout(m_buttonLayout, 0, 0);
+ m_mainLayout->addWidget(m_chartView, 0, 1, 3, 1);
+ setLayout(m_mainLayout);
+
+ // Add few series
+ addSeries();
+ addSeries();
+ addSeries();
+ addSeries();
+
+ // Set the title and show legend
+ m_chart->setTitle("Legendmarker example");
+ m_chart->legend()->setVisible(true);
+ m_chart->legend()->setAlignment(Qt::AlignBottom);
+
+ m_chartView->setRenderHint(QPainter::Antialiasing);
+}
+
+void MainWidget::addSeries()
+{
+ QLineSeries *series = new QLineSeries();
+ m_series.append(series);
+
+ series->setName(QString("line " + QString::number(m_series.count())));
+
+ // Make some sine wave for data
+ QList<QPointF> data;
+ int offset = m_chart->series().count();
+ for (int i = 0; i < 360; i++) {
+ qreal x = offset * 20 + i;
+ data.append(QPointF(i, qSin(2.0 * 3.141592 * x / 360.0)));
+ }
+
+ series->append(data);
+ m_chart->addSeries(series);
+
+ if (m_series.count() == 1) {
+ m_chart->createDefaultAxes();
+ }
+}
+
+void MainWidget::removeSeries()
+{
+ // Remove last series from chart
+ if (m_series.count() > 0) {
+ QLineSeries *series = m_series.last();
+ m_chart->removeSeries(series);
+ m_series.removeLast();
+ delete series;
+ }
+}
+
+void MainWidget::connectMarkers()
+{
+//![1]
+ // Connect all markers to handler
+ foreach (QLegendMarker* marker, m_chart->legend()->markers()) {
+ // Disconnect possible existing connection to avoid multiple connections
+ QObject::disconnect(marker, SIGNAL(clicked()), this, SLOT(handleMarkerClicked()));
+ QObject::connect(marker, SIGNAL(clicked()), this, SLOT(handleMarkerClicked()));
+ }
+//![1]
+}
+
+void MainWidget::disconnectMarkers()
+{
+//![2]
+ foreach (QLegendMarker* marker, m_chart->legend()->markers()) {
+ QObject::disconnect(marker, SIGNAL(clicked()), this, SLOT(handleMarkerClicked()));
+ }
+//![2]
+}
+
+void MainWidget::handleMarkerClicked()
+{
+//![3]
+ QLegendMarker* marker = qobject_cast<QLegendMarker*> (sender());
+ Q_ASSERT(marker);
+//![3]
+
+
+//![4]
+ switch (marker->type())
+//![4]
+ {
+ case QLegendMarker::LegendMarkerTypeXY:
+ {
+//![5]
+ // Toggle visibility of series
+ marker->series()->setVisible(!marker->series()->isVisible());
+
+ // Turn legend marker back to visible, since hiding series also hides the marker
+ // and we don't want it to happen now.
+ marker->setVisible(true);
+//![5]
+
+//![6]
+ // Dim the marker, if series is not visible
+ QXYLegendMarker *xymarker = qobject_cast<QXYLegendMarker *> (marker);
+ QColor color = xymarker->labelBrush().color();
+
+ if (marker->series()->isVisible()) {
+ color.setAlphaF(1.0);
+ } else {
+ color.setAlphaF(0.5);
+ }
+
+ xymarker->setLabelBrush(QBrush(color));
+//![6]
+ break;
+ }
+ default:
+ {
+ qDebug() << "Unknown marker type";
+ break;
+ }
+ }
+}
diff --git a/examples/legendmarkers/mainwidget.h b/examples/legendmarkers/mainwidget.h
new file mode 100644
index 00000000..4ffe26e8
--- /dev/null
+++ b/examples/legendmarkers/mainwidget.h
@@ -0,0 +1,63 @@
+/****************************************************************************
+**
+** 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 MAINWIDGET_H
+#define MAINWIDGET_H
+
+#include "qchartglobal.h"
+#include "qchart.h"
+#include "qchartview.h"
+#include <QWidget>
+#include <QGraphicsWidget>
+#include <QGridLayout>
+#include <QGraphicsGridLayout>
+#include <QDoubleSpinBox>
+#include <QGroupBox>
+#include <QLineSeries>
+
+QTCOMMERCIALCHART_USE_NAMESPACE
+
+class MainWidget : public QWidget
+{
+ Q_OBJECT
+public:
+ explicit MainWidget(QWidget *parent = 0);
+
+public slots:
+ void addSeries();
+ void removeSeries();
+ void connectMarkers();
+ void disconnectMarkers();
+
+ void handleMarkerClicked();
+
+private:
+
+ QChart *m_chart;
+ QList<QLineSeries *> m_series;
+
+ QChartView *m_chartView;
+ QGridLayout *m_mainLayout;
+ QGridLayout *m_buttonLayout;
+ QGridLayout *m_fontLayout;
+
+};
+
+#endif // MAINWIDGET_H
diff --git a/src/legend/legendlayout.cpp b/src/legend/legendlayout.cpp
index 9e37c9db..37138452 100644
--- a/src/legend/legendlayout.cpp
+++ b/src/legend/legendlayout.cpp
@@ -30,7 +30,9 @@
QTCOMMERCIALCHART_BEGIN_NAMESPACE
LegendLayout::LegendLayout(QLegend *legend)
- : m_legend(legend)
+ : m_legend(legend),
+ m_offsetX(0),
+ m_offsetY(0)
{
}
@@ -112,6 +114,8 @@ void LegendLayout::setAttachedGeometry(const QRectF &rect)
if (!rect.isValid())
return;
+ qreal oldOffsetX = m_offsetX;
+ qreal oldOffsetY = m_offsetY;
m_offsetX = 0;
m_offsetY = 0;
@@ -181,6 +185,8 @@ void LegendLayout::setAttachedGeometry(const QRectF &rect)
m_minOffsetY = - top;
m_maxOffsetX = m_width - geometry.width() - right;
m_maxOffsetY = m_height - geometry.height() - bottom;
+
+ setOffset(oldOffsetX, oldOffsetY);
}
void LegendLayout::setDettachedGeometry(const QRectF &rect)
@@ -193,6 +199,8 @@ void LegendLayout::setDettachedGeometry(const QRectF &rect)
// differ a log from attached mode.
// Also the scrolling logic is bit different.
+ qreal oldOffsetX = m_offsetX;
+ qreal oldOffsetY = m_offsetY;
m_offsetX = 0;
m_offsetY = 0;
@@ -350,6 +358,7 @@ void LegendLayout::setDettachedGeometry(const QRectF &rect)
break;
}
+ setOffset(oldOffsetX, oldOffsetY);
}
QSizeF LegendLayout::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const