tablewidget.cpp
307 lines
| 10.5 KiB
| text/x-c
|
CppLexer
Jani Honkonen
|
r830 | /**************************************************************************** | ||
** | ||||
** 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$ | ||||
** | ||||
****************************************************************************/ | ||||
Marek Rosa
|
r519 | #include "tablewidget.h" | ||
#include <QGridLayout> | ||||
#include <QTableView> | ||||
#include <QStyledItemDelegate> | ||||
#include "qlineseries.h" | ||||
#include "qsplineseries.h" | ||||
Marek Rosa
|
r545 | #include "qscatterseries.h" | ||
Marek Rosa
|
r519 | #include "customtablemodel.h" | ||
#include "qpieseries.h" | ||||
Marek Rosa
|
r630 | #include "qareaseries.h" | ||
#include "qbarseries.h" | ||||
Marek Rosa
|
r545 | #include <QPushButton> | ||
#include <QRadioButton> | ||||
Marek Rosa
|
r735 | #include <QSpinBox> | ||
Marek Rosa
|
r630 | #include <QTime> | ||
Marek Rosa
|
r519 | |||
TableWidget::TableWidget(QWidget *parent) | ||||
: QWidget(parent) | ||||
{ | ||||
Marek Rosa
|
r546 | setGeometry(100, 100, 1000, 600); | ||
Marek Rosa
|
r630 | qsrand(QTime(0,0,0).secsTo(QTime::currentTime())); | ||
Marek Rosa
|
r519 | // create simple model for storing data | ||
// user's table data model | ||||
Marek Rosa
|
r545 | m_model = new CustomTableModel; | ||
tableView = new QTableView; | ||||
tableView->setModel(m_model); | ||||
Marek Rosa
|
r630 | tableView->setMinimumHeight(240); | ||
// tableView->setMinimumSize(340, 480); | ||||
Marek Rosa
|
r519 | // tableView->setItemDelegate(new QStyledItemDelegate); | ||
Marek Rosa
|
r546 | chartView = new QChartView(this); | ||
Marek Rosa
|
r630 | chartView->setRenderHint(QPainter::Antialiasing); | ||
Marek Rosa
|
r519 | chartView->setMinimumSize(640, 480); | ||
// create | ||||
// QLineSeries* series = new QLineSeries; | ||||
Marek Rosa
|
r545 | // QSplineSeries* series = new QSplineSeries; | ||
// QScatterSeries* series = new QScatterSeries; | ||||
// series->setModel(m_model); | ||||
// series->setModelMapping(0,1, Qt::Vertical); | ||||
Marek Rosa
|
r519 | |||
// QPieSeries* pieSeries = new QPieSeries; | ||||
// pieSeries->setModel(model); | ||||
// pieSeries | ||||
Marek Rosa
|
r545 | // chartView->addSeries(series); | ||
// add, remove data buttons | ||||
Marek Rosa
|
r546 | QPushButton* addRowAboveButton = new QPushButton("Add row above"); | ||
connect(addRowAboveButton, SIGNAL(clicked()), this, SLOT(addRowAbove())); | ||||
QPushButton* addRowBelowButton = new QPushButton("Add row below"); | ||||
connect(addRowBelowButton, SIGNAL(clicked()), this, SLOT(addRowBelow())); | ||||
Marek Rosa
|
r545 | |||
QPushButton* removeRowButton = new QPushButton("Remove row"); | ||||
connect(removeRowButton, SIGNAL(clicked()), this, SLOT(removeRow())); | ||||
Marek Rosa
|
r735 | linesCountSpinBox = new QSpinBox; | ||
linesCountSpinBox->setRange(1, 10); | ||||
linesCountSpinBox->setValue(1); | ||||
Marek Rosa
|
r545 | // buttons layout | ||
QVBoxLayout* buttonsLayout = new QVBoxLayout; | ||||
Marek Rosa
|
r735 | buttonsLayout->addWidget(linesCountSpinBox); | ||
Marek Rosa
|
r546 | buttonsLayout->addWidget(addRowAboveButton); | ||
buttonsLayout->addWidget(addRowBelowButton); | ||||
Marek Rosa
|
r545 | buttonsLayout->addWidget(removeRowButton); | ||
buttonsLayout->addStretch(); | ||||
// chart type radio buttons | ||||
lineRadioButton = new QRadioButton("Line"); | ||||
splineRadioButton = new QRadioButton("Spline"); | ||||
scatterRadioButton = new QRadioButton("Scatter"); | ||||
Marek Rosa
|
r597 | pieRadioButton = new QRadioButton("Pie"); | ||
Marek Rosa
|
r630 | areaRadioButton = new QRadioButton("Area"); | ||
barRadioButton = new QRadioButton("Bar"); | ||||
Marek Rosa
|
r545 | |||
connect(lineRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType())); | ||||
connect(splineRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType())); | ||||
connect(scatterRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType())); | ||||
Marek Rosa
|
r597 | connect(pieRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType())); | ||
Marek Rosa
|
r630 | connect(areaRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType())); | ||
connect(barRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType())); | ||||
Marek Rosa
|
r545 | lineRadioButton->setChecked(true); | ||
// radio buttons layout | ||||
Marek Rosa
|
r630 | QVBoxLayout* radioLayout = new QVBoxLayout; | ||
Marek Rosa
|
r545 | radioLayout->addWidget(lineRadioButton); | ||
radioLayout->addWidget(splineRadioButton); | ||||
radioLayout->addWidget(scatterRadioButton); | ||||
Marek Rosa
|
r597 | radioLayout->addWidget(pieRadioButton); | ||
Marek Rosa
|
r630 | radioLayout->addWidget(areaRadioButton); | ||
radioLayout->addWidget(barRadioButton); | ||||
radioLayout->addStretch(); | ||||
Marek Rosa
|
r519 | |||
// create main layout | ||||
QGridLayout* mainLayout = new QGridLayout; | ||||
Marek Rosa
|
r630 | mainLayout->addLayout(buttonsLayout, 1, 0); | ||
mainLayout->addLayout(radioLayout, 2, 0); | ||||
Marek Rosa
|
r519 | mainLayout->addWidget(tableView, 1, 1); | ||
Marek Rosa
|
r630 | mainLayout->addWidget(chartView, 2, 1); | ||
Marek Rosa
|
r545 | setLayout(mainLayout); | ||
Marek Rosa
|
r630 | lineRadioButton->setFocus(); | ||
Marek Rosa
|
r545 | } | ||
Marek Rosa
|
r546 | void TableWidget::addRowAbove() | ||
{ | ||||
// m_model->insertRow(m_model->rowCount()); | ||||
Marek Rosa
|
r735 | // m_model->insertRow(tableView->currentIndex().row()); | ||
m_model->insertRows(tableView->currentIndex().row(), linesCountSpinBox->value()); | ||||
Marek Rosa
|
r546 | |||
} | ||||
void TableWidget::addRowBelow() | ||||
Marek Rosa
|
r545 | { | ||
// m_model->insertRow(m_model->rowCount()); | ||||
Marek Rosa
|
r735 | // m_model->insertRow(tableView->currentIndex().row() + 1); | ||
m_model->insertRows(tableView->currentIndex().row() + 1, linesCountSpinBox->value()); | ||||
Marek Rosa
|
r545 | |||
} | ||||
void TableWidget::removeRow() | ||||
{ | ||||
// m_model->removeRow(m_model->rowCount() - 1); | ||||
Marek Rosa
|
r735 | // m_model->removeRow(tableView->currentIndex().row()); | ||
m_model->removeRows(tableView->currentIndex().row(), qMin(m_model->rowCount() - tableView->currentIndex().row(), linesCountSpinBox->value())); | ||||
Marek Rosa
|
r545 | } | ||
void TableWidget::updateChartType() | ||||
{ | ||||
chartView->removeAllSeries(); | ||||
if (lineRadioButton->isChecked()) | ||||
Marek Rosa
|
r630 | { | ||
Marek Rosa
|
r735 | QPen pen; | ||
pen.setWidth(2); | ||||
QColor seriesColor("#8FBC8F"); | ||||
Marek Rosa
|
r630 | // series 1 | ||
series = new QLineSeries; | ||||
series->setModel(m_model); | ||||
series->setModelMapping(0,1, Qt::Vertical); | ||||
Marek Rosa
|
r734 | series->setModelMappingShift(1, 4); | ||
Marek Rosa
|
r630 | // series->setModelMapping(0,1, Qt::Horizontal); | ||
Marek Rosa
|
r735 | |||
pen.setColor(seriesColor); | ||||
series->setPen(pen); | ||||
Marek Rosa
|
r630 | chartView->addSeries(series); | ||
Marek Rosa
|
r735 | for (int i = 1; i <=4; i++) | ||
{ | ||||
m_model->setData(m_model->index(i, 0), seriesColor , Qt::BackgroundRole); | ||||
m_model->setData(m_model->index(i, 1), seriesColor , Qt::BackgroundRole); | ||||
} | ||||
// tableView->setsetStyleSheet("QTableView { border: 2px solid red }"); | ||||
seriesColor = QColor("#1E90FF"); | ||||
Marek Rosa
|
r630 | |||
// series 2 | ||||
series = new QLineSeries; | ||||
series->setModel(m_model); | ||||
series->setModelMapping(2,3, Qt::Vertical); | ||||
// series->setModelMapping(2,3, Qt::Horizontal); | ||||
Marek Rosa
|
r735 | pen.setColor(seriesColor); | ||
series->setPen(pen); | ||||
Marek Rosa
|
r630 | chartView->addSeries(series); | ||
Marek Rosa
|
r735 | chartView->axisX()->setRange(0, 500); | ||
for (int i = 0; i < m_model->rowCount(); i++) | ||||
{ | ||||
m_model->setData(m_model->index(i, 2), seriesColor , Qt::BackgroundRole); | ||||
m_model->setData(m_model->index(i, 3), seriesColor , Qt::BackgroundRole); | ||||
} | ||||
Marek Rosa
|
r734 | // // series 3 | ||
// series = new QLineSeries; | ||||
// series->setModel(m_model); | ||||
// series->setModelMapping(4,5, Qt::Vertical); | ||||
//// series->setModelMapping(4,5, Qt::Horizontal); | ||||
// chartView->addSeries(series); | ||||
Marek Rosa
|
r630 | } | ||
Marek Rosa
|
r545 | else if (splineRadioButton->isChecked()) | ||
Marek Rosa
|
r630 | { | ||
// series 1 | ||||
series = new QSplineSeries; | ||||
Marek Rosa
|
r734 | series->setModel(m_model); | ||
series->setModelMapping(0,1, Qt::Vertical); | ||||
series->setModelMappingShift(1, 4); | ||||
Marek Rosa
|
r630 | // series->setModelMapping(0,1, Qt::Horizontal); | ||
chartView->addSeries(series); | ||||
// series 2 | ||||
Marek Rosa
|
r545 | series = new QSplineSeries; | ||
Marek Rosa
|
r630 | series->setModel(m_model); | ||
series->setModelMapping(2,3, Qt::Vertical); | ||||
Marek Rosa
|
r735 | series->setModelMappingShift(0, 0); | ||
Marek Rosa
|
r630 | // series->setModelMapping(2,3, Qt::Horizontal); | ||
chartView->addSeries(series); | ||||
// series 3 | ||||
series = new QSplineSeries; | ||||
series->setModel(m_model); | ||||
series->setModelMapping(4,5, Qt::Vertical); | ||||
Marek Rosa
|
r735 | series->setModelMappingShift(0, 0); | ||
Marek Rosa
|
r630 | // series->setModelMapping(4,5, Qt::Horizontal); | ||
chartView->addSeries(series); | ||||
} | ||||
Marek Rosa
|
r597 | else if (scatterRadioButton->isChecked()) | ||
Marek Rosa
|
r630 | { | ||
// series 1 | ||||
series = new QScatterSeries; | ||||
series->setModel(m_model); | ||||
series->setModelMapping(0,1, Qt::Vertical); | ||||
// series->setModelMapping(0,1, Qt::Horizontal); | ||||
chartView->addSeries(series); | ||||
// series 2 | ||||
series = new QScatterSeries; | ||||
series->setModel(m_model); | ||||
series->setModelMapping(2,3, Qt::Vertical); | ||||
// series->setModelMapping(2,3, Qt::Horizontal); | ||||
chartView->addSeries(series); | ||||
// series 3 | ||||
Marek Rosa
|
r545 | series = new QScatterSeries; | ||
Marek Rosa
|
r630 | series->setModel(m_model); | ||
series->setModelMapping(4,5, Qt::Vertical); | ||||
// series->setModelMapping(4,5, Qt::Horizontal); | ||||
chartView->addSeries(series); | ||||
} | ||||
Marek Rosa
|
r597 | else if (pieRadioButton->isChecked()) | ||
{ | ||||
Marek Rosa
|
r630 | // pie 1 | ||
Marek Rosa
|
r597 | QPieSeries* pieSeries = new QPieSeries; | ||
pieSeries->setModel(m_model); | ||||
Marek Rosa
|
r655 | pieSeries->setModelMapping(0,0, Qt::Vertical); | ||
Marek Rosa
|
r630 | pieSeries->setLabelsVisible(true); | ||
pieSeries->setPieSize(0.4); | ||||
pieSeries->setPiePosition(0.2, 0.35); | ||||
chartView->addSeries(pieSeries); | ||||
// pie 2 | ||||
pieSeries = new QPieSeries; | ||||
pieSeries->setModel(m_model); | ||||
Marek Rosa
|
r655 | pieSeries->setModelMapping(1,1, Qt::Vertical); | ||
Marek Rosa
|
r630 | pieSeries->setLabelsVisible(true); | ||
pieSeries->setPieSize(0.4); | ||||
pieSeries->setPiePosition(0.8, 0.35); | ||||
chartView->addSeries(pieSeries); | ||||
// pie 3 | ||||
pieSeries = new QPieSeries; | ||||
pieSeries->setModel(m_model); | ||||
Marek Rosa
|
r655 | pieSeries->setModelMapping(2,2, Qt::Vertical); | ||
Marek Rosa
|
r597 | pieSeries->setLabelsVisible(true); | ||
Marek Rosa
|
r630 | pieSeries->setPieSize(0.4); | ||
pieSeries->setPiePosition(0.5, 0.65); | ||||
Marek Rosa
|
r597 | chartView->addSeries(pieSeries); | ||
Marek Rosa
|
r630 | } | ||
else if (areaRadioButton->isChecked()) | ||||
{ | ||||
QLineSeries* upperLineSeries = new QLineSeries; | ||||
upperLineSeries->setModel(m_model); | ||||
upperLineSeries->setModelMapping(0, 1, Qt::Vertical); | ||||
QLineSeries* lowerLineSeries = new QLineSeries; | ||||
lowerLineSeries->setModel(m_model); | ||||
lowerLineSeries->setModelMapping(2, 3, Qt::Vertical); | ||||
QAreaSeries* areaSeries = new QAreaSeries(upperLineSeries, lowerLineSeries); | ||||
chartView->addSeries(areaSeries); | ||||
} | ||||
else if (barRadioButton->isChecked()) | ||||
{ | ||||
QBarSeries* barSeries = new QBarSeries(QStringList()); | ||||
barSeries->setModel(m_model); | ||||
barSeries->setModelMapping(5, 2, 4, Qt::Vertical); | ||||
barSeries->setToolTipEnabled(true); | ||||
chartView->addSeries(barSeries); | ||||
Marek Rosa
|
r597 | } | ||
Marek Rosa
|
r545 | |||
Marek Rosa
|
r630 | // series->setModel(m_model); | ||
// series->setModelMapping(0,1, Qt::Vertical); | ||||
// chartView->addSeries(series); | ||||
Marek Rosa
|
r519 | } | ||
TableWidget::~TableWidget() | ||||
{ | ||||
} | ||||