| 1 |
// Copyright (C) 2016 The Qt Company Ltd. |
| 2 |
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 |
|
| 4 |
#undef QT_NO_FOREACH // this file contains unported legacy Q_FOREACH uses |
| 5 |
|
| 6 |
#include <QtCharts/QBarCategoryAxis> |
| 7 |
#include <QtCharts/QCandlestickSeries> |
| 8 |
#include <QtCharts/QCandlestickSet> |
| 9 |
#include <QtCharts/QDateTimeAxis> |
| 10 |
#include <QtCharts/QHCandlestickModelMapper> |
| 11 |
#include <QtCharts/QValueAxis> |
| 12 |
#include <QtCore/QDateTime> |
| 13 |
#include <QtCore/QDebug> |
| 14 |
#include <QtCore/QRandomGenerator> |
| 15 |
#include <QtWidgets/QCheckBox> |
| 16 |
#include <QtWidgets/QComboBox> |
| 17 |
#include <QtWidgets/QDoubleSpinBox> |
| 18 |
#include <QtWidgets/QGridLayout> |
| 19 |
#include <QtWidgets/QHeaderView> |
| 20 |
#include <QtWidgets/QLabel> |
| 21 |
#include <QtWidgets/QPushButton> |
| 22 |
#include <QtWidgets/QTableView> |
| 23 |
#include "customtablemodel.h" |
| 24 |
#include "mainwidget.h" |
| 25 |
|
| 26 |
QT_USE_NAMESPACE |
| 27 |
|
| 28 |
MainWidget::MainWidget(QWidget *parent) |
| 29 |
: QWidget(parent), |
| 30 |
m_chart(new QChart()), |
| 31 |
m_chartView(new QChartView(m_chart, this)), |
| 32 |
m_axisX(nullptr), |
| 33 |
m_axisY(nullptr), |
| 34 |
m_maximumColumnWidth(-1.0), |
| 35 |
m_minimumColumnWidth(5.0), |
| 36 |
m_bodyOutlineVisible(true), |
| 37 |
m_capsVisible(false), |
| 38 |
m_bodyWidth(0.5), |
| 39 |
m_capsWidth(0.5), |
| 40 |
m_customIncreasingColor(false), |
| 41 |
m_customDecreasingColor(false), |
| 42 |
m_hModelMapper(new QHCandlestickModelMapper(this)) |
| 43 |
{ |
| 44 |
m_chartView->setRenderHint(QPainter::Antialiasing, false); |
| 45 |
|
| 46 |
m_hModelMapper->setModel(new CustomTableModel(this)); |
| 47 |
m_hModelMapper->setTimestampColumn(0); |
| 48 |
m_hModelMapper->setOpenColumn(1); |
| 49 |
m_hModelMapper->setHighColumn(2); |
| 50 |
m_hModelMapper->setLowColumn(3); |
| 51 |
m_hModelMapper->setCloseColumn(4); |
| 52 |
|
| 53 |
QGridLayout *mainLayout = new QGridLayout(); |
| 54 |
mainLayout->addLayout(createSeriesControlsLayout(), 0, 0); |
| 55 |
mainLayout->addLayout(createSetsControlsLayout(), 1, 0); |
| 56 |
mainLayout->addLayout(createCandlestickControlsLayout(), 2, 0); |
| 57 |
mainLayout->addLayout(createMiscellaneousControlsLayout(), 3, 0); |
| 58 |
mainLayout->addWidget(m_chartView, 0, 1, mainLayout->rowCount() + 1, 1); |
| 59 |
mainLayout->addLayout(createModelMapperControlsLayout(), 0, 2, mainLayout->rowCount(), 1); |
| 60 |
setLayout(mainLayout); |
| 61 |
|
| 62 |
addSeries(); |
| 63 |
} |
| 64 |
|
| 65 |
MainWidget::~MainWidget() |
| 66 |
{ |
| 67 |
} |
| 68 |
|
| 69 |
QGridLayout *MainWidget::createSeriesControlsLayout() |
| 70 |
{ |
| 71 |
QGridLayout *layout = new QGridLayout(); |
| 72 |
int row = 0; |
| 73 |
|
| 74 |
layout->addWidget(new QLabel(QStringLiteral("Series controls:")), row, 0, Qt::AlignLeft); |
| 75 |
|
| 76 |
QPushButton *addSeriesButton = new QPushButton(QStringLiteral("Add a series")); |
| 77 |
connect(addSeriesButton, SIGNAL(clicked(bool)), this, SLOT(addSeries())); |
| 78 |
layout->addWidget(addSeriesButton, row++, 1, Qt::AlignLeft); |
| 79 |
|
| 80 |
QPushButton *removeSeriesButton = new QPushButton(QStringLiteral("Remove a series")); |
| 81 |
connect(removeSeriesButton, SIGNAL(clicked(bool)), this, SLOT(removeSeries())); |
| 82 |
layout->addWidget(removeSeriesButton, row++, 1, Qt::AlignLeft); |
| 83 |
|
| 84 |
QPushButton *removeAllSeriesButton = new QPushButton(QStringLiteral("Remove all series")); |
| 85 |
connect(removeAllSeriesButton, SIGNAL(clicked(bool)), this, SLOT(removeAllSeries())); |
| 86 |
layout->addWidget(removeAllSeriesButton, row++, 1, Qt::AlignLeft); |
| 87 |
|
| 88 |
return layout; |
| 89 |
} |
| 90 |
|
| 91 |
QGridLayout *MainWidget::createSetsControlsLayout() |
| 92 |
{ |
| 93 |
QGridLayout *layout = new QGridLayout(); |
| 94 |
int row = 0; |
| 95 |
|
| 96 |
layout->addWidget(new QLabel(QStringLiteral("Sets controls:")), row, 0, Qt::AlignLeft); |
| 97 |
|
| 98 |
QPushButton *addSetButton = new QPushButton(QStringLiteral("Add a set")); |
| 99 |
connect(addSetButton, SIGNAL(clicked(bool)), this, SLOT(addSet())); |
| 100 |
layout->addWidget(addSetButton, row++, 1, Qt::AlignLeft); |
| 101 |
|
| 102 |
QPushButton *insertSetButton = new QPushButton(QStringLiteral("Insert a set")); |
| 103 |
connect(insertSetButton, SIGNAL(clicked(bool)), this, SLOT(insertSet())); |
| 104 |
layout->addWidget(insertSetButton, row++, 1, Qt::AlignLeft); |
| 105 |
|
| 106 |
QPushButton *removeSetButton = new QPushButton(QStringLiteral("Remove a set")); |
| 107 |
connect(removeSetButton, SIGNAL(clicked(bool)), this, SLOT(removeSet())); |
| 108 |
layout->addWidget(removeSetButton, row++, 1, Qt::AlignLeft); |
| 109 |
|
| 110 |
QPushButton *removeAllSetsButton = new QPushButton(QStringLiteral("Remove all sets")); |
| 111 |
connect(removeAllSetsButton, SIGNAL(clicked(bool)), this, SLOT(removeAllSets())); |
| 112 |
layout->addWidget(removeAllSetsButton, row++, 1, Qt::AlignLeft); |
| 113 |
|
| 114 |
return layout; |
| 115 |
} |
| 116 |
|
| 117 |
QGridLayout *MainWidget::createCandlestickControlsLayout() |
| 118 |
{ |
| 119 |
QGridLayout *layout = new QGridLayout(); |
| 120 |
int row = 0; |
| 121 |
|
| 122 |
layout->addWidget(new QLabel(QStringLiteral("Maximum column width:")), row, 0, Qt::AlignLeft); |
| 123 |
QDoubleSpinBox *maximumColumnWidthSpinBox = new QDoubleSpinBox(); |
| 124 |
maximumColumnWidthSpinBox->setRange(-1.0, 1024.0); |
| 125 |
maximumColumnWidthSpinBox->setDecimals(0); |
| 126 |
maximumColumnWidthSpinBox->setValue(m_maximumColumnWidth); |
| 127 |
maximumColumnWidthSpinBox->setSingleStep(1.0); |
| 128 |
connect(maximumColumnWidthSpinBox, SIGNAL(valueChanged(double)), |
| 129 |
this, SLOT(changeMaximumColumnWidth(double))); |
| 130 |
layout->addWidget(maximumColumnWidthSpinBox, row++, 1, Qt::AlignLeft); |
| 131 |
|
| 132 |
layout->addWidget(new QLabel(QStringLiteral("Minimum column width:")), row, 0, Qt::AlignLeft); |
| 133 |
QDoubleSpinBox *minimumColumnWidthSpinBox = new QDoubleSpinBox(); |
| 134 |
minimumColumnWidthSpinBox->setRange(-1.0, 1024.0); |
| 135 |
minimumColumnWidthSpinBox->setDecimals(0); |
| 136 |
minimumColumnWidthSpinBox->setValue(m_minimumColumnWidth); |
| 137 |
minimumColumnWidthSpinBox->setSingleStep(1.0); |
| 138 |
connect(minimumColumnWidthSpinBox, SIGNAL(valueChanged(double)), |
| 139 |
this, SLOT(changeMinimumColumnWidth(double))); |
| 140 |
layout->addWidget(minimumColumnWidthSpinBox, row++, 1, Qt::AlignLeft); |
| 141 |
|
| 142 |
QCheckBox *bodyOutlineVisible = new QCheckBox(QStringLiteral("Body outline visible")); |
| 143 |
connect(bodyOutlineVisible, SIGNAL(toggled(bool)), this, SLOT(bodyOutlineVisibleToggled(bool))); |
| 144 |
bodyOutlineVisible->setChecked(m_bodyOutlineVisible); |
| 145 |
layout->addWidget(bodyOutlineVisible, row++, 0, Qt::AlignLeft); |
| 146 |
|
| 147 |
QCheckBox *capsVisible = new QCheckBox(QStringLiteral("Caps visible")); |
| 148 |
connect(capsVisible, SIGNAL(toggled(bool)), this, SLOT(capsVisibleToggled(bool))); |
| 149 |
capsVisible->setChecked(m_capsVisible); |
| 150 |
layout->addWidget(capsVisible, row++, 0, Qt::AlignLeft); |
| 151 |
|
| 152 |
layout->addWidget(new QLabel(QStringLiteral("Candlestick body width:")), row, 0, Qt::AlignLeft); |
| 153 |
QDoubleSpinBox *bodyWidthSpinBox = new QDoubleSpinBox(); |
| 154 |
bodyWidthSpinBox->setRange(-1.0, 2.0); |
| 155 |
bodyWidthSpinBox->setValue(m_bodyWidth); |
| 156 |
bodyWidthSpinBox->setSingleStep(0.1); |
| 157 |
connect(bodyWidthSpinBox, SIGNAL(valueChanged(double)), this, SLOT(changeBodyWidth(double))); |
| 158 |
layout->addWidget(bodyWidthSpinBox, row++, 1, Qt::AlignLeft); |
| 159 |
|
| 160 |
layout->addWidget(new QLabel(QStringLiteral("Candlestick caps width:")), row, 0, Qt::AlignLeft); |
| 161 |
QDoubleSpinBox *capsWidthSpinBox = new QDoubleSpinBox(); |
| 162 |
capsWidthSpinBox->setRange(-1.0, 2.0); |
| 163 |
capsWidthSpinBox->setValue(m_capsWidth); |
| 164 |
capsWidthSpinBox->setSingleStep(0.1); |
| 165 |
connect(capsWidthSpinBox, SIGNAL(valueChanged(double)), this, SLOT(changeCapsWidth(double))); |
| 166 |
layout->addWidget(capsWidthSpinBox, row++, 1, Qt::AlignLeft); |
| 167 |
|
| 168 |
QCheckBox *increasingColor = new QCheckBox(QStringLiteral("Custom increasing color (only S1)")); |
| 169 |
connect(increasingColor, SIGNAL(toggled(bool)), this, SLOT(customIncreasingColorToggled(bool))); |
| 170 |
increasingColor->setChecked(m_customIncreasingColor); |
| 171 |
layout->addWidget(increasingColor, row++, 0, 1, 2, Qt::AlignLeft); |
| 172 |
|
| 173 |
QCheckBox *decreasingColor = new QCheckBox(QStringLiteral("Custom decreasing color (only S1)")); |
| 174 |
connect(decreasingColor, SIGNAL(toggled(bool)), this, SLOT(customDecreasingColorToggled(bool))); |
| 175 |
decreasingColor->setChecked(m_customDecreasingColor); |
| 176 |
layout->addWidget(decreasingColor, row++, 0, 1, 2, Qt::AlignLeft); |
| 177 |
|
| 178 |
return layout; |
| 179 |
} |
| 180 |
|
| 181 |
QGridLayout *MainWidget::createMiscellaneousControlsLayout() |
| 182 |
{ |
| 183 |
QGridLayout *layout = new QGridLayout(); |
| 184 |
int row = 0; |
| 185 |
|
| 186 |
layout->addWidget(new QLabel(QStringLiteral("Miscellaneous:")), row, 0, Qt::AlignLeft); |
| 187 |
|
| 188 |
QCheckBox *antialiasingCheckBox = new QCheckBox(QStringLiteral("Antialiasing")); |
| 189 |
connect(antialiasingCheckBox, SIGNAL(toggled(bool)), this, SLOT(antialiasingToggled(bool))); |
| 190 |
antialiasingCheckBox->setChecked(false); |
| 191 |
layout->addWidget(antialiasingCheckBox, row++, 1, Qt::AlignLeft); |
| 192 |
|
| 193 |
QCheckBox *animationCheckBox = new QCheckBox(QStringLiteral("Animation")); |
| 194 |
connect(animationCheckBox, SIGNAL(toggled(bool)), this, SLOT(animationToggled(bool))); |
| 195 |
animationCheckBox->setChecked(false); |
| 196 |
layout->addWidget(animationCheckBox, row++, 1, Qt::AlignLeft); |
| 197 |
|
| 198 |
QCheckBox *legendCheckBox = new QCheckBox(QStringLiteral("Legend")); |
| 199 |
connect(legendCheckBox, SIGNAL(toggled(bool)), this, SLOT(legendToggled(bool))); |
| 200 |
legendCheckBox->setChecked(true); |
| 201 |
layout->addWidget(legendCheckBox, row++, 1, Qt::AlignLeft); |
| 202 |
|
| 203 |
QCheckBox *titleCheckBox = new QCheckBox(QStringLiteral("Title")); |
| 204 |
connect(titleCheckBox, SIGNAL(toggled(bool)), this, SLOT(titleToggled(bool))); |
| 205 |
titleCheckBox->setChecked(true); |
| 206 |
layout->addWidget(titleCheckBox, row++, 1, Qt::AlignLeft); |
| 207 |
|
| 208 |
layout->addWidget(new QLabel(QStringLiteral("Chart theme:")), row, 0, Qt::AlignLeft); |
| 209 |
QComboBox *chartThemeComboBox = new QComboBox(); |
| 210 |
chartThemeComboBox->addItem(QStringLiteral("Light")); |
| 211 |
chartThemeComboBox->addItem(QStringLiteral("Blue Cerulean")); |
| 212 |
chartThemeComboBox->addItem(QStringLiteral("Dark")); |
| 213 |
chartThemeComboBox->addItem(QStringLiteral("Brown Sand")); |
| 214 |
chartThemeComboBox->addItem(QStringLiteral("Blue Ncs")); |
| 215 |
chartThemeComboBox->addItem(QStringLiteral("High Contrast")); |
| 216 |
chartThemeComboBox->addItem(QStringLiteral("Blue Icy")); |
| 217 |
chartThemeComboBox->addItem(QStringLiteral("Qt")); |
| 218 |
connect(chartThemeComboBox,SIGNAL(currentIndexChanged(int)),this,SLOT(changeChartTheme(int))); |
| 219 |
layout->addWidget(chartThemeComboBox, row++, 1, Qt::AlignLeft); |
| 220 |
|
| 221 |
layout->addWidget(new QLabel(QStringLiteral("Axis X:")), row, 0, Qt::AlignLeft); |
| 222 |
QComboBox *axisXComboBox = new QComboBox(); |
| 223 |
axisXComboBox->addItem(QStringLiteral("BarCategory")); |
| 224 |
axisXComboBox->addItem(QStringLiteral("DateTime")); |
| 225 |
axisXComboBox->addItem(QStringLiteral("Value")); |
| 226 |
connect(axisXComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(changeAxisX(int))); |
| 227 |
layout->addWidget(axisXComboBox, row++, 1, Qt::AlignLeft); |
| 228 |
|
| 229 |
return layout; |
| 230 |
} |
| 231 |
|
| 232 |
QGridLayout *MainWidget::createModelMapperControlsLayout() |
| 233 |
{ |
| 234 |
QGridLayout *layout = new QGridLayout(); |
| 235 |
int row = 0; |
| 236 |
|
| 237 |
layout->addWidget(new QLabel(QStringLiteral("First series:")), row, 0, Qt::AlignLeft); |
| 238 |
|
| 239 |
QPushButton *attachModelMapperButton = new QPushButton(QStringLiteral("Attach model mapper")); |
| 240 |
connect(attachModelMapperButton, SIGNAL(clicked(bool)), this, SLOT(attachModelMapper())); |
| 241 |
layout->addWidget(attachModelMapperButton, row++, 1, Qt::AlignLeft); |
| 242 |
|
| 243 |
QPushButton *detachModelMappeButton = new QPushButton(QStringLiteral("Detach model mapper")); |
| 244 |
connect(detachModelMappeButton, SIGNAL(clicked(bool)), this, SLOT(detachModelMapper())); |
| 245 |
layout->addWidget(detachModelMappeButton, row++, 1, Qt::AlignLeft); |
| 246 |
|
| 247 |
QTableView *tableView = new QTableView(); |
| 248 |
tableView->setMinimumSize(320, 480); |
| 249 |
tableView->setMaximumSize(320, 480); |
| 250 |
tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch); |
| 251 |
tableView->verticalHeader()->setSectionResizeMode(QHeaderView::Stretch); |
| 252 |
Q_ASSERT_X(m_hModelMapper->model(), Q_FUNC_INFO, "Model is not initialized"); |
| 253 |
tableView->setModel(m_hModelMapper->model()); |
| 254 |
layout->addWidget(tableView, row++, 0, 1, 2, Qt::AlignLeft); |
| 255 |
|
| 256 |
layout->addItem(new QSpacerItem(1, 1, QSizePolicy::Fixed, QSizePolicy::Expanding), row++, 0); |
| 257 |
|
| 258 |
return layout; |
| 259 |
} |
| 260 |
|
| 261 |
qreal MainWidget::randomValue(int min, int max) const |
| 262 |
{ |
| 263 |
if (min > max) |
| 264 |
qSwap(min, max); |
| 265 |
return QRandomGenerator::global()->bounded(min, max); |
| 266 |
} |
| 267 |
|
| 268 |
QCandlestickSet *MainWidget::randomSet(qreal timestamp) |
| 269 |
{ |
| 270 |
QCandlestickSet *set = new QCandlestickSet(timestamp); |
| 271 |
set->setOpen(randomValue(4, 11)); |
| 272 |
set->setHigh(randomValue(12, 15)); |
| 273 |
set->setLow(randomValue(0, 3)); |
| 274 |
set->setClose(randomValue(4, 11)); |
| 275 |
|
| 276 |
return set; |
| 277 |
} |
| 278 |
|
| 279 |
void MainWidget::updateAxes() |
| 280 |
{ |
| 281 |
if (m_chart->axes().isEmpty()) |
| 282 |
m_chart->createDefaultAxes(); |
| 283 |
|
| 284 |
QCandlestickSeries *series; |
| 285 |
if (!m_chart->series().isEmpty()) |
| 286 |
series = qobject_cast<QCandlestickSeries *>(m_chart->series().at(0)); |
| 287 |
else |
| 288 |
series = nullptr; |
| 289 |
|
| 290 |
m_axisX = m_chart->axes(Qt::Horizontal).first(); |
| 291 |
if (series && !series->sets().isEmpty()) { |
| 292 |
if (m_axisX->type() == QAbstractAxis::AxisTypeBarCategory) { |
| 293 |
QBarCategoryAxis *axisX = qobject_cast<QBarCategoryAxis *>(m_axisX); |
| 294 |
QStringList categories; |
| 295 |
for (int i = 0; i < series->sets().count(); ++i) |
| 296 |
categories.append(QString::number(i)); |
| 297 |
axisX->setCategories(categories); |
| 298 |
} else { // QAbstractAxis::AxisTypeDateTime || QAbstractAxis::AxisTypeValue |
| 299 |
qreal msInMonth = 31.0 * 24.0 * 60.0 * 60.0 * 1000.0; |
| 300 |
qreal min = series->sets().first()->timestamp() - msInMonth; |
| 301 |
qreal max = series->sets().last()->timestamp() + msInMonth; |
| 302 |
QDateTime minDateTime = QDateTime::fromMSecsSinceEpoch(min); |
| 303 |
QDateTime maxDateTime = QDateTime::fromMSecsSinceEpoch(max); |
| 304 |
|
| 305 |
if (m_axisX->type() == QAbstractAxis::AxisTypeDateTime) |
| 306 |
m_axisX->setRange(minDateTime, maxDateTime); |
| 307 |
else |
| 308 |
m_axisX->setRange(min, max); |
| 309 |
} |
| 310 |
} |
| 311 |
|
| 312 |
m_axisY = m_chart->axes(Qt::Vertical).first(); |
| 313 |
m_axisY->setMax(15); |
| 314 |
m_axisY->setMin(0); |
| 315 |
} |
| 316 |
|
| 317 |
void MainWidget::addSeries() |
| 318 |
{ |
| 319 |
if (m_chart->series().count() > 9) { |
| 320 |
qDebug() << "Maximum series count is 10"; |
| 321 |
return; |
| 322 |
} |
| 323 |
|
| 324 |
QCandlestickSeries *series = new QCandlestickSeries(); |
| 325 |
series->setName(QStringLiteral("S%1").arg(m_chart->series().count() + 1)); |
| 326 |
series->setMaximumColumnWidth(m_maximumColumnWidth); |
| 327 |
series->setMinimumColumnWidth(m_minimumColumnWidth); |
| 328 |
series->setBodyOutlineVisible(m_bodyOutlineVisible); |
| 329 |
series->setBodyWidth(m_bodyWidth); |
| 330 |
series->setCapsVisible(m_capsVisible); |
| 331 |
series->setCapsWidth(m_capsWidth); |
| 332 |
|
| 333 |
if (m_chart->series().isEmpty()) { |
| 334 |
if (m_customIncreasingColor) |
| 335 |
series->setIncreasingColor(QColor(Qt::green)); |
| 336 |
if (m_customDecreasingColor) |
| 337 |
series->setDecreasingColor(QColor(Qt::red)); |
| 338 |
|
| 339 |
for (int month = 1; month <= 12; ++month) { |
| 340 |
QDateTime dateTime; |
| 341 |
dateTime.setDate(QDate(QDateTime::currentDateTime().date().year(), month, 1)); |
| 342 |
dateTime.setTime(QTime(12, 34, 56, 789)); |
| 343 |
|
| 344 |
QCandlestickSet *set = randomSet(dateTime.toMSecsSinceEpoch()); |
| 345 |
series->append(set); |
| 346 |
} |
| 347 |
} else { |
| 348 |
QCandlestickSeries *s = qobject_cast<QCandlestickSeries *>(m_chart->series().at(0)); |
| 349 |
for (int i = 0; i < s->sets().count(); ++i) { |
| 350 |
QCandlestickSet *set = randomSet(s->sets().at(i)->timestamp()); |
| 351 |
series->append(set); |
| 352 |
} |
| 353 |
} |
| 354 |
|
| 355 |
m_chart->addSeries(series); |
| 356 |
|
| 357 |
updateAxes(); |
| 358 |
if (!series->attachedAxes().contains(m_axisX)) |
| 359 |
series->attachAxis(m_axisX); |
| 360 |
if (!series->attachedAxes().contains(m_axisY)) |
| 361 |
series->attachAxis(m_axisY); |
| 362 |
} |
| 363 |
|
| 364 |
void MainWidget::removeSeries() |
| 365 |
{ |
| 366 |
if (m_chart->series().isEmpty()) { |
| 367 |
qDebug() << "Create a series first"; |
| 368 |
return; |
| 369 |
} |
| 370 |
|
| 371 |
if (m_chart->series().count() == 1) |
| 372 |
detachModelMapper(); |
| 373 |
|
| 374 |
QCandlestickSeries *series = qobject_cast<QCandlestickSeries *>(m_chart->series().last()); |
| 375 |
m_chart->removeSeries(series); |
| 376 |
delete series; |
| 377 |
series = nullptr; |
| 378 |
} |
| 379 |
|
| 380 |
void MainWidget::removeAllSeries() |
| 381 |
{ |
| 382 |
if (m_chart->series().isEmpty()) { |
| 383 |
qDebug() << "Create a series first"; |
| 384 |
return; |
| 385 |
} |
| 386 |
|
| 387 |
detachModelMapper(); |
| 388 |
|
| 389 |
m_chart->removeAllSeries(); |
| 390 |
} |
| 391 |
|
| 392 |
void MainWidget::addSet() |
| 393 |
{ |
| 394 |
if (m_chart->series().isEmpty()) { |
| 395 |
qDebug() << "Create a series first"; |
| 396 |
return; |
| 397 |
} |
| 398 |
|
| 399 |
foreach (QAbstractSeries *s, m_chart->series()) { |
| 400 |
QCandlestickSeries *series = qobject_cast<QCandlestickSeries *>(s); |
| 401 |
|
| 402 |
QDateTime dateTime; |
| 403 |
if (series->count()) { |
| 404 |
dateTime.setMSecsSinceEpoch(series->sets().last()->timestamp()); |
| 405 |
dateTime = dateTime.addMonths(1); |
| 406 |
} else { |
| 407 |
dateTime.setDate(QDate(QDateTime::currentDateTime().date().year(), 1, 1)); |
| 408 |
dateTime.setTime(QTime(12, 34, 56, 789)); |
| 409 |
} |
| 410 |
|
| 411 |
QCandlestickSet *set = randomSet(dateTime.toMSecsSinceEpoch()); |
| 412 |
series->append(set); |
| 413 |
} |
| 414 |
|
| 415 |
updateAxes(); |
| 416 |
} |
| 417 |
|
| 418 |
void MainWidget::insertSet() |
| 419 |
{ |
| 420 |
if (m_chart->series().isEmpty()) { |
| 421 |
qDebug() << "Create a series first"; |
| 422 |
return; |
| 423 |
} |
| 424 |
|
| 425 |
foreach (QAbstractSeries *s, m_chart->series()) { |
| 426 |
QCandlestickSeries *series = qobject_cast<QCandlestickSeries *>(s); |
| 427 |
|
| 428 |
QDateTime dateTime; |
| 429 |
if (series->count()) { |
| 430 |
dateTime.setMSecsSinceEpoch(series->sets().first()->timestamp()); |
| 431 |
dateTime = dateTime.addMonths(-1); |
| 432 |
} else { |
| 433 |
dateTime.setDate(QDate(QDateTime::currentDateTime().date().year(), 1, 1)); |
| 434 |
dateTime.setTime(QTime(12, 34, 56, 789)); |
| 435 |
} |
| 436 |
|
| 437 |
QCandlestickSet *set = randomSet(dateTime.toMSecsSinceEpoch()); |
| 438 |
series->insert(0, set); |
| 439 |
} |
| 440 |
|
| 441 |
updateAxes(); |
| 442 |
} |
| 443 |
|
| 444 |
void MainWidget::removeSet() |
| 445 |
{ |
| 446 |
if (m_chart->series().isEmpty()) { |
| 447 |
qDebug() << "Create a series first"; |
| 448 |
return; |
| 449 |
} |
| 450 |
|
| 451 |
foreach (QAbstractSeries *s, m_chart->series()) { |
| 452 |
QCandlestickSeries *series = qobject_cast<QCandlestickSeries *>(s); |
| 453 |
if (series->sets().isEmpty()) |
| 454 |
qDebug() << "Create a set first"; |
| 455 |
else |
| 456 |
series->remove(series->sets().last()); |
| 457 |
} |
| 458 |
|
| 459 |
updateAxes(); |
| 460 |
} |
| 461 |
|
| 462 |
void MainWidget::removeAllSets() |
| 463 |
{ |
| 464 |
if (m_chart->series().isEmpty()) { |
| 465 |
qDebug() << "Create a series first"; |
| 466 |
return; |
| 467 |
} |
| 468 |
|
| 469 |
foreach (QAbstractSeries *s, m_chart->series()) { |
| 470 |
QCandlestickSeries *series = qobject_cast<QCandlestickSeries *>(s); |
| 471 |
if (series->sets().isEmpty()) |
| 472 |
qDebug() << "Create a set first"; |
| 473 |
else |
| 474 |
series->clear(); |
| 475 |
} |
| 476 |
|
| 477 |
updateAxes(); |
| 478 |
} |
| 479 |
|
| 480 |
void MainWidget::changeMaximumColumnWidth(double width) |
| 481 |
{ |
| 482 |
m_maximumColumnWidth = width; |
| 483 |
foreach (QAbstractSeries *s, m_chart->series()) { |
| 484 |
QCandlestickSeries *series = qobject_cast<QCandlestickSeries *>(s); |
| 485 |
series->setMaximumColumnWidth(m_maximumColumnWidth); |
| 486 |
} |
| 487 |
} |
| 488 |
|
| 489 |
void MainWidget::changeMinimumColumnWidth(double width) |
| 490 |
{ |
| 491 |
m_minimumColumnWidth = width; |
| 492 |
foreach (QAbstractSeries *s, m_chart->series()) { |
| 493 |
QCandlestickSeries *series = qobject_cast<QCandlestickSeries *>(s); |
| 494 |
series->setMinimumColumnWidth(m_minimumColumnWidth); |
| 495 |
} |
| 496 |
} |
| 497 |
|
| 498 |
void MainWidget::bodyOutlineVisibleToggled(bool visible) |
| 499 |
{ |
| 500 |
m_bodyOutlineVisible = visible; |
| 501 |
foreach (QAbstractSeries *s, m_chart->series()) { |
| 502 |
QCandlestickSeries *series = qobject_cast<QCandlestickSeries *>(s); |
| 503 |
series->setBodyOutlineVisible(m_bodyOutlineVisible); |
| 504 |
} |
| 505 |
} |
| 506 |
|
| 507 |
void MainWidget::capsVisibleToggled(bool visible) |
| 508 |
{ |
| 509 |
m_capsVisible = visible; |
| 510 |
foreach (QAbstractSeries *s, m_chart->series()) { |
| 511 |
QCandlestickSeries *series = qobject_cast<QCandlestickSeries *>(s); |
| 512 |
series->setCapsVisible(m_capsVisible); |
| 513 |
} |
| 514 |
} |
| 515 |
|
| 516 |
void MainWidget::changeBodyWidth(double width) |
| 517 |
{ |
| 518 |
m_bodyWidth = width; |
| 519 |
foreach (QAbstractSeries *s, m_chart->series()) { |
| 520 |
QCandlestickSeries *series = qobject_cast<QCandlestickSeries *>(s); |
| 521 |
series->setBodyWidth(m_bodyWidth); |
| 522 |
} |
| 523 |
} |
| 524 |
|
| 525 |
void MainWidget::changeCapsWidth(double width) |
| 526 |
{ |
| 527 |
m_capsWidth = width; |
| 528 |
foreach (QAbstractSeries *s, m_chart->series()) { |
| 529 |
QCandlestickSeries *series = qobject_cast<QCandlestickSeries *>(s); |
| 530 |
series->setCapsWidth(m_capsWidth); |
| 531 |
} |
| 532 |
} |
| 533 |
|
| 534 |
void MainWidget::customIncreasingColorToggled(bool custom) |
| 535 |
{ |
| 536 |
m_customIncreasingColor = custom; |
| 537 |
|
| 538 |
if (m_chart->series().isEmpty()) |
| 539 |
return; |
| 540 |
|
| 541 |
QCandlestickSeries *series = qobject_cast<QCandlestickSeries *>(m_chart->series().at(0)); |
| 542 |
if (series) { |
| 543 |
QColor color = m_customIncreasingColor ? QColor(Qt::green) : QColor(); |
| 544 |
series->setIncreasingColor(color); |
| 545 |
} |
| 546 |
} |
| 547 |
|
| 548 |
void MainWidget::customDecreasingColorToggled(bool custom) |
| 549 |
{ |
| 550 |
m_customDecreasingColor = custom; |
| 551 |
|
| 552 |
if (m_chart->series().isEmpty()) |
| 553 |
return; |
| 554 |
|
| 555 |
QCandlestickSeries *series = qobject_cast<QCandlestickSeries *>(m_chart->series().at(0)); |
| 556 |
if (series) { |
| 557 |
QColor color = m_customDecreasingColor ? QColor(Qt::red) : QColor(); |
| 558 |
series->setDecreasingColor(color); |
| 559 |
} |
| 560 |
} |
| 561 |
|
| 562 |
void MainWidget::antialiasingToggled(bool enabled) |
| 563 |
{ |
| 564 |
m_chartView->setRenderHint(QPainter::Antialiasing, enabled); |
| 565 |
} |
| 566 |
|
| 567 |
void MainWidget::animationToggled(bool enabled) |
| 568 |
{ |
| 569 |
if (enabled) |
| 570 |
m_chart->setAnimationOptions(QChart::SeriesAnimations); |
| 571 |
else |
| 572 |
m_chart->setAnimationOptions(QChart::NoAnimation); |
| 573 |
} |
| 574 |
|
| 575 |
void MainWidget::legendToggled(bool visible) |
| 576 |
{ |
| 577 |
m_chart->legend()->setVisible(visible); |
| 578 |
if (visible) |
| 579 |
m_chart->legend()->setAlignment(Qt::AlignBottom); |
| 580 |
} |
| 581 |
|
| 582 |
void MainWidget::titleToggled(bool visible) |
| 583 |
{ |
| 584 |
if (visible) |
| 585 |
m_chart->setTitle(QStringLiteral("Candlestick Chart")); |
| 586 |
else |
| 587 |
m_chart->setTitle(QString()); |
| 588 |
} |
| 589 |
|
| 590 |
void MainWidget::changeChartTheme(int themeIndex) |
| 591 |
{ |
| 592 |
if (themeIndex < QChart::ChartThemeLight || themeIndex > QChart::ChartThemeQt) { |
| 593 |
qDebug() << "Invalid chart theme index:" << themeIndex; |
| 594 |
return; |
| 595 |
} |
| 596 |
|
| 597 |
m_chart->setTheme((QChart::ChartTheme)(themeIndex)); |
| 598 |
} |
| 599 |
|
| 600 |
void MainWidget::changeAxisX(int axisXIndex) |
| 601 |
{ |
| 602 |
if (m_axisX) { |
| 603 |
m_chart->removeAxis(m_axisX); |
| 604 |
delete m_axisX; |
| 605 |
} |
| 606 |
|
| 607 |
switch (axisXIndex) { |
| 608 |
case 0: |
| 609 |
m_axisX = new QBarCategoryAxis(); |
| 610 |
break; |
| 611 |
case 1: |
| 612 |
m_axisX = new QDateTimeAxis(); |
| 613 |
break; |
| 614 |
case 2: |
| 615 |
m_axisX = new QValueAxis(); |
| 616 |
break; |
| 617 |
default: |
| 618 |
qDebug() << "Invalid axis x index:" << axisXIndex; |
| 619 |
return; |
| 620 |
} |
| 621 |
|
| 622 |
m_chart->addAxis(m_axisX, Qt::AlignBottom); |
| 623 |
|
| 624 |
updateAxes(); |
| 625 |
|
| 626 |
foreach (QAbstractSeries *series, m_chart->series()) |
| 627 |
series->attachAxis(m_axisX); |
| 628 |
} |
| 629 |
|
| 630 |
void MainWidget::attachModelMapper() |
| 631 |
{ |
| 632 |
if (m_hModelMapper->series()) { |
| 633 |
qDebug() << "Model mapper is already attached"; |
| 634 |
return; |
| 635 |
} |
| 636 |
|
| 637 |
if (m_chart->series().isEmpty()) |
| 638 |
addSeries(); |
| 639 |
|
| 640 |
QCandlestickSeries *series = qobject_cast<QCandlestickSeries *>(m_chart->series().at(0)); |
| 641 |
Q_ASSERT(series); |
| 642 |
series->setName(QStringLiteral("SWMM")); // Series With Model Mapper |
| 643 |
|
| 644 |
CustomTableModel *model = qobject_cast<CustomTableModel *>(m_hModelMapper->model()); |
| 645 |
foreach (QCandlestickSet *set, series->sets()) |
| 646 |
model->addRow(set); |
| 647 |
|
| 648 |
m_hModelMapper->setFirstSetRow(0); |
| 649 |
m_hModelMapper->setLastSetRow(model->rowCount() - 1); |
| 650 |
m_hModelMapper->setSeries(series); |
| 651 |
} |
| 652 |
|
| 653 |
void MainWidget::detachModelMapper() |
| 654 |
{ |
| 655 |
if (!m_hModelMapper->series()) |
| 656 |
return; |
| 657 |
|
| 658 |
QCandlestickSeries *series = qobject_cast<QCandlestickSeries *>(m_hModelMapper->series()); |
| 659 |
Q_ASSERT(series); |
| 660 |
series->setName(QStringLiteral("S1")); |
| 661 |
|
| 662 |
m_hModelMapper->setSeries(nullptr); |
| 663 |
m_hModelMapper->setFirstSetRow(-1); |
| 664 |
m_hModelMapper->setLastSetRow(-1); |
| 665 |
|
| 666 |
CustomTableModel *model = qobject_cast<CustomTableModel *>(m_hModelMapper->model()); |
| 667 |
model->clearRows(); |
| 668 |
} |