| 1 | /**************************************************************************** | 
| 2 | ** | 
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. | 
| 4 | ** Contact: https://www.qt.io/licensing/ | 
| 5 | ** | 
| 6 | ** This file is part of the Qt Charts module of the Qt Toolkit. | 
| 7 | ** | 
| 8 | ** $QT_BEGIN_LICENSE:GPL$ | 
| 9 | ** Commercial License Usage | 
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in | 
| 11 | ** accordance with the commercial license agreement provided with the | 
| 12 | ** Software or, alternatively, in accordance with the terms contained in | 
| 13 | ** a written agreement between you and The Qt Company. For licensing terms | 
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further | 
| 15 | ** information use the contact form at https://www.qt.io/contact-us. | 
| 16 | ** | 
| 17 | ** GNU General Public License Usage | 
| 18 | ** Alternatively, this file may be used under the terms of the GNU | 
| 19 | ** General Public License version 3 or (at your option) any later version | 
| 20 | ** approved by the KDE Free Qt Foundation. The licenses are as published by | 
| 21 | ** the Free Software Foundation and appearing in the file LICENSE.GPL3 | 
| 22 | ** included in the packaging of this file. Please review the following | 
| 23 | ** information to ensure the GNU General Public License requirements will | 
| 24 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. | 
| 25 | ** | 
| 26 | ** $QT_END_LICENSE$ | 
| 27 | ** | 
| 28 | ****************************************************************************/ | 
| 29 |  | 
| 30 | #include "mainwidget.h" | 
| 31 | #include <QtCharts/QChart> | 
| 32 | #include <QtCharts/QChartView> | 
| 33 | #include <QtWidgets/QPushButton> | 
| 34 | #include <QtWidgets/QLabel> | 
| 35 | #include <QtCore/QDebug> | 
| 36 | #include <QtCharts/QBarSet> | 
| 37 | #include <QtCharts/QBarSeries> | 
| 38 | #include <QtCharts/QLegend> | 
| 39 | #include <QtWidgets/QFormLayout> | 
| 40 |  | 
| 41 | QT_CHARTS_USE_NAMESPACE | 
| 42 |  | 
| 43 | MainWidget::MainWidget(QWidget *parent) : | 
| 44 |     QWidget(parent) | 
| 45 | { | 
| 46 |     // Create buttons for ui | 
| 47 |     m_buttonLayout = new QGridLayout(); | 
| 48 |     QPushButton *detachLegendButton = new QPushButton("Toggle attached" ); | 
| 49 |     connect(sender: detachLegendButton, signal: &QPushButton::clicked, receiver: this, slot: &MainWidget::toggleAttached); | 
| 50 |     m_buttonLayout->addWidget(detachLegendButton, row: 0, column: 0); | 
| 51 |  | 
| 52 |     QPushButton *addSetButton = new QPushButton("add barset" ); | 
| 53 |     connect(sender: addSetButton, signal: &QPushButton::clicked, receiver: this, slot: &MainWidget::addBarset); | 
| 54 |     m_buttonLayout->addWidget(addSetButton, row: 2, column: 0); | 
| 55 |     QPushButton *removeBarsetButton = new QPushButton("remove barset" ); | 
| 56 |     connect(sender: removeBarsetButton, signal: &QPushButton::clicked, receiver: this, slot: &MainWidget::removeBarset); | 
| 57 |     m_buttonLayout->addWidget(removeBarsetButton, row: 3, column: 0); | 
| 58 |  | 
| 59 |     QPushButton *alignButton = new QPushButton("Align (Bottom)" ); | 
| 60 |     connect(sender: alignButton, signal: &QPushButton::clicked, receiver: this, slot: &MainWidget::setLegendAlignment); | 
| 61 |     m_buttonLayout->addWidget(alignButton, row: 4, column: 0); | 
| 62 |  | 
| 63 |     QPushButton *boldButton = new QPushButton("Toggle bold" ); | 
| 64 |     connect(sender: boldButton, signal: &QPushButton::clicked, receiver: this, slot: &MainWidget::toggleBold); | 
| 65 |     m_buttonLayout->addWidget(boldButton, row: 8, column: 0); | 
| 66 |  | 
| 67 |     QPushButton *italicButton = new QPushButton("Toggle italic" ); | 
| 68 |     connect(sender: italicButton, signal: &QPushButton::clicked, receiver: this, slot: &MainWidget::toggleItalic); | 
| 69 |     m_buttonLayout->addWidget(italicButton, row: 9, column: 0); | 
| 70 |  | 
| 71 |     m_legendPosX = new QDoubleSpinBox(); | 
| 72 |     m_legendPosY = new QDoubleSpinBox(); | 
| 73 |     m_legendWidth = new QDoubleSpinBox(); | 
| 74 |     m_legendHeight = new QDoubleSpinBox(); | 
| 75 |  | 
| 76 |     connect(sender: m_legendPosX, | 
| 77 |             signal: static_cast<void (QDoubleSpinBox::*)(double)>(&QDoubleSpinBox::valueChanged), | 
| 78 |             receiver: this, slot: &MainWidget::updateLegendLayout); | 
| 79 |     connect(sender: m_legendPosY, | 
| 80 |             signal: static_cast<void (QDoubleSpinBox::*)(double)>(&QDoubleSpinBox::valueChanged), | 
| 81 |             receiver: this, slot: &MainWidget::updateLegendLayout); | 
| 82 |     connect(sender: m_legendWidth, | 
| 83 |             signal: static_cast<void (QDoubleSpinBox::*)(double)>(&QDoubleSpinBox::valueChanged), | 
| 84 |             receiver: this, slot: &MainWidget::updateLegendLayout); | 
| 85 |     connect(sender: m_legendHeight, | 
| 86 |             signal: static_cast<void (QDoubleSpinBox::*)(double)>(&QDoubleSpinBox::valueChanged), | 
| 87 |             receiver: this, slot: &MainWidget::updateLegendLayout); | 
| 88 |  | 
| 89 |     QFormLayout *legendLayout = new QFormLayout(); | 
| 90 |     legendLayout->addRow(labelText: "HPos" , field: m_legendPosX); | 
| 91 |     legendLayout->addRow(labelText: "VPos" , field: m_legendPosY); | 
| 92 |     legendLayout->addRow(labelText: "Width" , field: m_legendWidth); | 
| 93 |     legendLayout->addRow(labelText: "Height" , field: m_legendHeight); | 
| 94 |     m_legendSettings = new QGroupBox("Detached legend" ); | 
| 95 |     m_legendSettings->setLayout(legendLayout); | 
| 96 |     m_buttonLayout->addWidget(w: m_legendSettings); | 
| 97 |     m_legendSettings->setVisible(false); | 
| 98 |  | 
| 99 |     // Create chart view with the chart | 
| 100 |     m_chart = new QChart(); | 
| 101 |     m_chartView = new QChartView(m_chart, this); | 
| 102 |  | 
| 103 |     // Create spinbox to modify font size | 
| 104 |     m_fontSize = new QDoubleSpinBox(); | 
| 105 |     m_fontSize->setValue(m_chart->legend()->font().pointSizeF()); | 
| 106 |     connect(sender: m_fontSize, | 
| 107 |             signal: static_cast<void (QDoubleSpinBox::*)(double)>(&QDoubleSpinBox::valueChanged), | 
| 108 |             receiver: this, slot: &MainWidget::fontSizeChanged); | 
| 109 |  | 
| 110 |     QFormLayout *fontLayout = new QFormLayout(); | 
| 111 |     fontLayout->addRow(labelText: "Legend font size" , field: m_fontSize); | 
| 112 |  | 
| 113 |     // Create layout for grid and detached legend | 
| 114 |     m_mainLayout = new QGridLayout(); | 
| 115 |     m_mainLayout->addLayout(m_buttonLayout, row: 0, column: 0); | 
| 116 |     m_mainLayout->addLayout(fontLayout, row: 1, column: 0); | 
| 117 |     m_mainLayout->addWidget(m_chartView, row: 0, column: 1, rowSpan: 3, columnSpan: 1); | 
| 118 |     setLayout(m_mainLayout); | 
| 119 |  | 
| 120 |     createSeries(); | 
| 121 | } | 
| 122 |  | 
| 123 | void MainWidget::createSeries() | 
| 124 | { | 
| 125 |     m_series = new QBarSeries(); | 
| 126 |     addBarset(); | 
| 127 |     addBarset(); | 
| 128 |     addBarset(); | 
| 129 |     addBarset(); | 
| 130 |  | 
| 131 |     m_chart->addSeries(series: m_series); | 
| 132 |     m_chart->setTitle("Legend detach example" ); | 
| 133 |     m_chart->createDefaultAxes(); | 
| 134 | //![1] | 
| 135 |     m_chart->legend()->setVisible(true); | 
| 136 |     m_chart->legend()->setAlignment(Qt::AlignBottom); | 
| 137 | //![1] | 
| 138 |  | 
| 139 |     m_chartView->setRenderHint(hint: QPainter::Antialiasing); | 
| 140 | } | 
| 141 |  | 
| 142 | void MainWidget::showLegendSpinbox() | 
| 143 | { | 
| 144 |     m_legendSettings->setVisible(true); | 
| 145 |     QRectF chartViewRect = m_chartView->rect(); | 
| 146 |  | 
| 147 |     m_legendPosX->setMinimum(0); | 
| 148 |     m_legendPosX->setMaximum(chartViewRect.width()); | 
| 149 |     m_legendPosX->setValue(150); | 
| 150 |  | 
| 151 |     m_legendPosY->setMinimum(0); | 
| 152 |     m_legendPosY->setMaximum(chartViewRect.height()); | 
| 153 |     m_legendPosY->setValue(150); | 
| 154 |  | 
| 155 |     m_legendWidth->setMinimum(0); | 
| 156 |     m_legendWidth->setMaximum(chartViewRect.width()); | 
| 157 |     m_legendWidth->setValue(150); | 
| 158 |  | 
| 159 |     m_legendHeight->setMinimum(0); | 
| 160 |     m_legendHeight->setMaximum(chartViewRect.height()); | 
| 161 |     m_legendHeight->setValue(75); | 
| 162 | } | 
| 163 |  | 
| 164 | void MainWidget::hideLegendSpinbox() | 
| 165 | { | 
| 166 |     m_legendSettings->setVisible(false); | 
| 167 | } | 
| 168 |  | 
| 169 |  | 
| 170 | void MainWidget::toggleAttached() | 
| 171 | { | 
| 172 |     QLegend *legend = m_chart->legend(); | 
| 173 |     if (legend->isAttachedToChart()) { | 
| 174 |         //![2] | 
| 175 |         legend->detachFromChart(); | 
| 176 |         m_chart->legend()->setBackgroundVisible(true); | 
| 177 |         m_chart->legend()->setBrush(QBrush(QColor(128, 128, 128, 128))); | 
| 178 |         m_chart->legend()->setPen(QPen(QColor(192, 192, 192, 192))); | 
| 179 |         //![2] | 
| 180 |         showLegendSpinbox(); | 
| 181 |         updateLegendLayout(); | 
| 182 |     } else { | 
| 183 |         //![3] | 
| 184 |         legend->attachToChart(); | 
| 185 |         legend->setBackgroundVisible(false); | 
| 186 |         //![3] | 
| 187 |         hideLegendSpinbox(); | 
| 188 |     } | 
| 189 |     update(); | 
| 190 | } | 
| 191 |  | 
| 192 | void MainWidget::addBarset() | 
| 193 | { | 
| 194 |     QBarSet *barSet = new QBarSet(QString("set " ) + QString::number(m_series->count())); | 
| 195 |     qreal delta = m_series->count() * 0.1; | 
| 196 |     *barSet << 1 + delta << 2 + delta << 3 + delta << 4 + delta; | 
| 197 |     m_series->append(set: barSet); | 
| 198 | } | 
| 199 |  | 
| 200 | void MainWidget::removeBarset() | 
| 201 | { | 
| 202 |     QList<QBarSet *> sets = m_series->barSets(); | 
| 203 |     if (sets.count() > 0) { | 
| 204 |         m_series->remove(set: sets.at(i: sets.count() - 1)); | 
| 205 |     } | 
| 206 | } | 
| 207 |  | 
| 208 | void MainWidget::setLegendAlignment() | 
| 209 | { | 
| 210 |     QPushButton *button = qobject_cast<QPushButton *>(object: sender()); | 
| 211 |  | 
| 212 |     switch (m_chart->legend()->alignment()) { | 
| 213 |     case Qt::AlignTop: | 
| 214 |         m_chart->legend()->setAlignment(Qt::AlignLeft); | 
| 215 |         if (button) | 
| 216 |             button->setText("Align (Left)" ); | 
| 217 |         break; | 
| 218 |     case Qt::AlignLeft: | 
| 219 |         m_chart->legend()->setAlignment(Qt::AlignBottom); | 
| 220 |         if (button) | 
| 221 |             button->setText("Align (Bottom)" ); | 
| 222 |         break; | 
| 223 |     case Qt::AlignBottom: | 
| 224 |         m_chart->legend()->setAlignment(Qt::AlignRight); | 
| 225 |         if (button) | 
| 226 |             button->setText("Align (Right)" ); | 
| 227 |         break; | 
| 228 |     default: | 
| 229 |         if (button) | 
| 230 |             button->setText("Align (Top)" ); | 
| 231 |         m_chart->legend()->setAlignment(Qt::AlignTop); | 
| 232 |         break; | 
| 233 |     } | 
| 234 | } | 
| 235 |  | 
| 236 | void MainWidget::toggleBold() | 
| 237 | { | 
| 238 |     QFont font = m_chart->legend()->font(); | 
| 239 |     font.setBold(!font.bold()); | 
| 240 |     m_chart->legend()->setFont(font); | 
| 241 | } | 
| 242 |  | 
| 243 | void MainWidget::toggleItalic() | 
| 244 | { | 
| 245 |     QFont font = m_chart->legend()->font(); | 
| 246 |     font.setItalic(!font.italic()); | 
| 247 |     m_chart->legend()->setFont(font); | 
| 248 | } | 
| 249 |  | 
| 250 | void MainWidget::fontSizeChanged() | 
| 251 | { | 
| 252 |     QFont font = m_chart->legend()->font(); | 
| 253 |     font.setPointSizeF(m_fontSize->value()); | 
| 254 |     m_chart->legend()->setFont(font); | 
| 255 | } | 
| 256 |  | 
| 257 | void MainWidget::updateLegendLayout() | 
| 258 | { | 
| 259 | //![4] | 
| 260 |     m_chart->legend()->setGeometry(QRectF(m_legendPosX->value(), | 
| 261 |                                           m_legendPosY->value(), | 
| 262 |                                           m_legendWidth->value(), | 
| 263 |                                           m_legendHeight->value())); | 
| 264 |     m_chart->legend()->update(); | 
| 265 | //![4] | 
| 266 | } | 
| 267 |  |