mainwidget.cpp
256 lines
| 8.3 KiB
| text/x-c
|
CppLexer
|
r2854 | /**************************************************************************** | ||
|
r1365 | ** | ||
|
r2854 | ** Copyright (C) 2016 The Qt Company Ltd. | ||
** Contact: https://www.qt.io/licensing/ | ||||
|
r1365 | ** | ||
|
r2854 | ** This file is part of the Qt Charts module of the Qt Toolkit. | ||
|
r1365 | ** | ||
|
r2854 | ** $QT_BEGIN_LICENSE:GPL$ | ||
|
r2845 | ** Commercial License Usage | ||
** Licensees holding valid commercial Qt licenses may use this file in | ||||
** accordance with the commercial license agreement provided with the | ||||
** Software or, alternatively, in accordance with the terms contained in | ||||
** a written agreement between you and The Qt Company. For licensing terms | ||||
|
r2854 | ** and conditions see https://www.qt.io/terms-conditions. For further | ||
** information use the contact form at https://www.qt.io/contact-us. | ||||
** | ||||
** GNU General Public License Usage | ||||
** Alternatively, this file may be used under the terms of the GNU | ||||
** General Public License version 3 or (at your option) any later version | ||||
** approved by the KDE Free Qt Foundation. The licenses are as published by | ||||
** the Free Software Foundation and appearing in the file LICENSE.GPL3 | ||||
** included in the packaging of this file. Please review the following | ||||
** information to ensure the GNU General Public License requirements will | ||||
** be met: https://www.gnu.org/licenses/gpl-3.0.html. | ||||
|
r1365 | ** | ||
|
r2845 | ** $QT_END_LICENSE$ | ||
** | ||||
|
r2854 | ****************************************************************************/ | ||
|
r1365 | |||
|
r1263 | #include "mainwidget.h" | ||
|
r2714 | #include <QtCharts/QChart> | ||
#include <QtCharts/QChartView> | ||||
#include <QtWidgets/QPushButton> | ||||
#include <QtWidgets/QLabel> | ||||
#include <QtCore/QDebug> | ||||
#include <QtCharts/QBarSet> | ||||
#include <QtCharts/QBarSeries> | ||||
#include <QtCharts/QLegend> | ||||
#include <QtWidgets/QFormLayout> | ||||
|
r1263 | |||
|
r2712 | QT_CHARTS_USE_NAMESPACE | ||
|
r1263 | |||
MainWidget::MainWidget(QWidget *parent) : | ||||
QWidget(parent) | ||||
{ | ||||
|
r1300 | // Create buttons for ui | ||
|
r1263 | m_buttonLayout = new QGridLayout(); | ||
|
r2034 | QPushButton *detachLegendButton = new QPushButton("Toggle attached"); | ||
connect(detachLegendButton, SIGNAL(clicked()), this, SLOT(toggleAttached())); | ||||
|
r1263 | m_buttonLayout->addWidget(detachLegendButton, 0, 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); | ||||
|
r2034 | QPushButton *alignButton = new QPushButton("Align (Bottom)"); | ||
connect(alignButton, SIGNAL(clicked()), this, SLOT(setLegendAlignment())); | ||||
m_buttonLayout->addWidget(alignButton, 4, 0); | ||||
|
r1444 | |||
|
r1522 | QPushButton *boldButton = new QPushButton("Toggle bold"); | ||
connect(boldButton, SIGNAL(clicked()), this, SLOT(toggleBold())); | ||||
m_buttonLayout->addWidget(boldButton, 8, 0); | ||||
QPushButton *italicButton = new QPushButton("Toggle italic"); | ||||
connect(italicButton, SIGNAL(clicked()), this, SLOT(toggleItalic())); | ||||
m_buttonLayout->addWidget(italicButton, 9, 0); | ||||
|
r1444 | m_legendPosX = new QDoubleSpinBox(); | ||
m_legendPosY = new QDoubleSpinBox(); | ||||
m_legendWidth = new QDoubleSpinBox(); | ||||
m_legendHeight = new QDoubleSpinBox(); | ||||
connect(m_legendPosX, SIGNAL(valueChanged(double)), this, SLOT(updateLegendLayout())); | ||||
connect(m_legendPosY, SIGNAL(valueChanged(double)), this, SLOT(updateLegendLayout())); | ||||
connect(m_legendWidth, SIGNAL(valueChanged(double)), this, SLOT(updateLegendLayout())); | ||||
connect(m_legendHeight, SIGNAL(valueChanged(double)), this, SLOT(updateLegendLayout())); | ||||
|
r2102 | QFormLayout *legendLayout = new QFormLayout(); | ||
|
r2034 | legendLayout->addRow("HPos", m_legendPosX); | ||
legendLayout->addRow("VPos", m_legendPosY); | ||||
|
r1444 | legendLayout->addRow("Width", m_legendWidth); | ||
legendLayout->addRow("Height", m_legendHeight); | ||||
m_legendSettings = new QGroupBox("Detached legend"); | ||||
m_legendSettings->setLayout(legendLayout); | ||||
m_buttonLayout->addWidget(m_legendSettings); | ||||
m_legendSettings->setVisible(false); | ||||
|
r1263 | // Create chart view with the chart | ||
|
r1300 | m_chart = new QChart(); | ||
|
r1263 | m_chartView = new QChartView(m_chart, this); | ||
|
r1522 | // Create spinbox to modify font size | ||
m_fontSize = new QDoubleSpinBox(); | ||||
m_fontSize->setValue(m_chart->legend()->font().pointSizeF()); | ||||
connect(m_fontSize, SIGNAL(valueChanged(double)), this, SLOT(fontSizeChanged())); | ||||
|
r2102 | QFormLayout *fontLayout = new QFormLayout(); | ||
|
r1522 | fontLayout->addRow("Legend font size", m_fontSize); | ||
|
r1263 | // Create layout for grid and detached legend | ||
m_mainLayout = new QGridLayout(); | ||||
m_mainLayout->addLayout(m_buttonLayout, 0, 0); | ||||
|
r2098 | m_mainLayout->addLayout(fontLayout, 1, 0); | ||
|
r1263 | m_mainLayout->addWidget(m_chartView, 0, 1, 3, 1); | ||
setLayout(m_mainLayout); | ||||
createSeries(); | ||||
} | ||||
void MainWidget::createSeries() | ||||
{ | ||||
|
r1594 | m_series = new QBarSeries(); | ||
|
r1263 | addBarset(); | ||
addBarset(); | ||||
addBarset(); | ||||
addBarset(); | ||||
m_chart->addSeries(m_series); | ||||
m_chart->setTitle("Legend detach example"); | ||||
|
r1585 | m_chart->createDefaultAxes(); | ||
|
r1447 | //![1] | ||
|
r1263 | m_chart->legend()->setVisible(true); | ||
|
r1357 | m_chart->legend()->setAlignment(Qt::AlignBottom); | ||
|
r1447 | //![1] | ||
|
r1263 | |||
m_chartView->setRenderHint(QPainter::Antialiasing); | ||||
} | ||||
|
r1444 | void MainWidget::showLegendSpinbox() | ||
{ | ||||
m_legendSettings->setVisible(true); | ||||
QRectF chartViewRect = m_chartView->rect(); | ||||
m_legendPosX->setMinimum(0); | ||||
m_legendPosX->setMaximum(chartViewRect.width()); | ||||
m_legendPosX->setValue(150); | ||||
m_legendPosY->setMinimum(0); | ||||
m_legendPosY->setMaximum(chartViewRect.height()); | ||||
m_legendPosY->setValue(150); | ||||
m_legendWidth->setMinimum(0); | ||||
m_legendWidth->setMaximum(chartViewRect.width()); | ||||
m_legendWidth->setValue(150); | ||||
m_legendHeight->setMinimum(0); | ||||
m_legendHeight->setMaximum(chartViewRect.height()); | ||||
|
r1446 | m_legendHeight->setValue(75); | ||
|
r1444 | } | ||
void MainWidget::hideLegendSpinbox() | ||||
{ | ||||
m_legendSettings->setVisible(false); | ||||
} | ||||
|
r2034 | void MainWidget::toggleAttached() | ||
|
r1263 | { | ||
QLegend *legend = m_chart->legend(); | ||||
|
r2034 | if (legend->isAttachedToChart()) { | ||
//![2] | ||||
legend->detachFromChart(); | ||||
m_chart->legend()->setBackgroundVisible(true); | ||||
|
r2098 | m_chart->legend()->setBrush(QBrush(QColor(128, 128, 128, 128))); | ||
m_chart->legend()->setPen(QPen(QColor(192, 192, 192, 192))); | ||||
|
r2034 | //![2] | ||
showLegendSpinbox(); | ||||
updateLegendLayout(); | ||||
} else { | ||||
//![3] | ||||
legend->attachToChart(); | ||||
legend->setBackgroundVisible(false); | ||||
//![3] | ||||
hideLegendSpinbox(); | ||||
} | ||||
|
r1444 | update(); | ||
|
r1263 | } | ||
void MainWidget::addBarset() | ||||
{ | ||||
|
r1463 | QBarSet *barSet = new QBarSet(QString("set ") + QString::number(m_series->count())); | ||
qreal delta = m_series->count() * 0.1; | ||||
|
r1580 | *barSet << 1 + delta << 2 + delta << 3 + delta << 4 + delta; | ||
|
r1263 | m_series->append(barSet); | ||
} | ||||
void MainWidget::removeBarset() | ||||
{ | ||||
|
r2102 | QList<QBarSet *> sets = m_series->barSets(); | ||
|
r1396 | if (sets.count() > 0) { | ||
|
r2098 | m_series->remove(sets.at(sets.count() - 1)); | ||
|
r1396 | } | ||
|
r1263 | } | ||
|
r1444 | |||
|
r2034 | void MainWidget::setLegendAlignment() | ||
{ | ||||
QPushButton *button = qobject_cast<QPushButton *>(sender()); | ||||
switch (m_chart->legend()->alignment()) { | ||||
case Qt::AlignTop: | ||||
m_chart->legend()->setAlignment(Qt::AlignLeft); | ||||
if (button) | ||||
button->setText("Align (Left)"); | ||||
break; | ||||
case Qt::AlignLeft: | ||||
m_chart->legend()->setAlignment(Qt::AlignBottom); | ||||
if (button) | ||||
button->setText("Align (Bottom)"); | ||||
break; | ||||
case Qt::AlignBottom: | ||||
m_chart->legend()->setAlignment(Qt::AlignRight); | ||||
if (button) | ||||
button->setText("Align (Right)"); | ||||
break; | ||||
default: | ||||
if (button) | ||||
button->setText("Align (Top)"); | ||||
m_chart->legend()->setAlignment(Qt::AlignTop); | ||||
break; | ||||
} | ||||
|
r1444 | } | ||
|
r1522 | void MainWidget::toggleBold() | ||
{ | ||||
QFont font = m_chart->legend()->font(); | ||||
font.setBold(!font.bold()); | ||||
m_chart->legend()->setFont(font); | ||||
} | ||||
void MainWidget::toggleItalic() | ||||
{ | ||||
QFont font = m_chart->legend()->font(); | ||||
font.setItalic(!font.italic()); | ||||
m_chart->legend()->setFont(font); | ||||
} | ||||
void MainWidget::fontSizeChanged() | ||||
{ | ||||
QFont font = m_chart->legend()->font(); | ||||
font.setPointSizeF(m_fontSize->value()); | ||||
m_chart->legend()->setFont(font); | ||||
} | ||||
|
r1444 | void MainWidget::updateLegendLayout() | ||
{ | ||||
|
r1447 | //![4] | ||
|
r2098 | m_chart->legend()->setGeometry(QRectF(m_legendPosX->value(), | ||
m_legendPosY->value(), | ||||
m_legendWidth->value(), | ||||
m_legendHeight->value())); | ||||
|
r1444 | m_chart->legend()->update(); | ||
|
r1447 | //![4] | ||
|
r1444 | } | ||