mainwidget.cpp
149 lines
| 4.4 KiB
| text/x-c
|
CppLexer
Marek Rosa
|
r1365 | /**************************************************************************** | ||
** | ||||
** 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$ | ||||
** | ||||
****************************************************************************/ | ||||
sauimone
|
r1263 | #include "mainwidget.h" | ||
#include <QChart> | ||||
#include <QChartView> | ||||
#include <QPushButton> | ||||
#include <QLabel> | ||||
#include <QDebug> | ||||
#include <QBarSet> | ||||
#include <QBarSeries> | ||||
#include <QLegend> | ||||
QTCOMMERCIALCHART_USE_NAMESPACE | ||||
MainWidget::MainWidget(QWidget *parent) : | ||||
QWidget(parent) | ||||
{ | ||||
sauimone
|
r1300 | // Create buttons for ui | ||
sauimone
|
r1263 | m_buttonLayout = new QGridLayout(); | ||
QPushButton *detachLegendButton = new QPushButton("detach legend"); | ||||
connect(detachLegendButton, SIGNAL(clicked()), this, SLOT(detachLegend())); | ||||
m_buttonLayout->addWidget(detachLegendButton, 0, 0); | ||||
QPushButton *attachLegendButton = new QPushButton("attach legend"); | ||||
connect(attachLegendButton, SIGNAL(clicked()), this, SLOT(attachLegend())); | ||||
m_buttonLayout->addWidget(attachLegendButton, 1, 0); | ||||
QPushButton *addSetButton = new QPushButton("add barset"); | ||||
connect(addSetButton, SIGNAL(clicked()), this, SLOT(addBarset())); | ||||
m_buttonLayout->addWidget(addSetButton, 2, 0); | ||||
QPushButton *removeBarsetButton = new QPushButton("remove barset"); | ||||
connect(removeBarsetButton, SIGNAL(clicked()), this, SLOT(removeBarset())); | ||||
m_buttonLayout->addWidget(removeBarsetButton, 3, 0); | ||||
// Create chart view with the chart | ||||
sauimone
|
r1300 | //![1] | ||
m_chart = new QChart(); | ||||
sauimone
|
r1263 | m_chartView = new QChartView(m_chart, this); | ||
m_chartView->setRubberBand(QChartView::HorizonalRubberBand); | ||||
sauimone
|
r1300 | //![1] | ||
sauimone
|
r1263 | |||
sauimone
|
r1300 | // Create custom scene and view, where detached legend will be drawn | ||
sauimone
|
r1273 | //![2] | ||
sauimone
|
r1263 | m_customView = new QGraphicsView(this); | ||
m_customScene = new QGraphicsScene(this); | ||||
m_customView->setScene(m_customScene); | ||||
sauimone
|
r1273 | //![2] | ||
sauimone
|
r1263 | |||
// 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); | ||||
m_mainLayout->addWidget(m_customView, 0, 2, 3, 1); | ||||
setLayout(m_mainLayout); | ||||
createSeries(); | ||||
} | ||||
void MainWidget::createSeries() | ||||
{ | ||||
sauimone
|
r1300 | //![3] | ||
sauimone
|
r1263 | m_series = new QBarSeries(); | ||
addBarset(); | ||||
addBarset(); | ||||
addBarset(); | ||||
addBarset(); | ||||
m_chart->addSeries(m_series); | ||||
m_chart->setTitle("Legend detach example"); | ||||
m_chart->legend()->setVisible(true); | ||||
Tero Ahola
|
r1357 | m_chart->legend()->setAlignment(Qt::AlignBottom); | ||
sauimone
|
r1263 | m_chart->axisY()->setNiceNumbersEnabled(true); | ||
m_chartView->setRenderHint(QPainter::Antialiasing); | ||||
sauimone
|
r1300 | //![3] | ||
sauimone
|
r1263 | } | ||
sauimone
|
r1273 | void MainWidget::detachLegend() | ||
sauimone
|
r1263 | { | ||
sauimone
|
r1300 | // Detach legend from chart and | ||
// put legend to our custom scene | ||||
//![4] | ||||
sauimone
|
r1263 | QLegend *legend = m_chart->legend(); | ||
sauimone
|
r1273 | legend->detachFromChart(); | ||
sauimone
|
r1281 | legend->setGeometry(m_customView->rect()); | ||
sauimone
|
r1273 | m_customScene->addItem(legend); | ||
sauimone
|
r1300 | //![4] | ||
sauimone
|
r1263 | |||
sauimone
|
r1300 | // This forces redraw | ||
QSize delta(1,1); | ||||
resize(size() + delta); | ||||
resize(size() - delta); | ||||
sauimone
|
r1263 | } | ||
sauimone
|
r1273 | |||
void MainWidget::attachLegend() | ||||
sauimone
|
r1263 | { | ||
sauimone
|
r1300 | // Remove legend from custom scene and put it back to chartview scene. | ||
// Attach legend back to chart, so that layout works. | ||||
//![5] | ||||
sauimone
|
r1263 | QLegend *legend = m_chart->legend(); | ||
sauimone
|
r1273 | if (m_customScene->items().contains(legend)) { | ||
m_customScene->removeItem(legend); | ||||
m_chartView->scene()->addItem(legend); | ||||
legend->attachToChart(); | ||||
} | ||||
sauimone
|
r1300 | //![5] | ||
sauimone
|
r1263 | |||
sauimone
|
r1300 | // This forces redraw | ||
QSize delta(1,1); | ||||
resize(size() + delta); | ||||
resize(size() - delta); | ||||
sauimone
|
r1263 | } | ||
void MainWidget::addBarset() | ||||
{ | ||||
sauimone
|
r1300 | QBarSet *barSet = new QBarSet(QString("set ") + QString::number(m_series->barsetCount())); | ||
sauimone
|
r1263 | qreal delta = m_series->barsetCount() * 0.1; | ||
*barSet << QPointF(0.0 + delta, 1 + delta) << QPointF(1.0 + delta, 2 + delta) << QPointF(2.0 + delta, 3 + delta) << QPointF(3.0 + delta, 4 + delta); | ||||
m_series->append(barSet); | ||||
} | ||||
void MainWidget::removeBarset() | ||||
{ | ||||
sauimone
|
r1396 | QList<QBarSet*> sets = m_series->barSets(); | ||
if (sets.count() > 0) { | ||||
m_series->remove(sets.at(sets.count()-1)); | ||||
} | ||||
sauimone
|
r1263 | } | ||