diff options
Diffstat (limited to 'tests/manual/boxplottester')
-rw-r--r-- | tests/manual/boxplottester/boxplottester.pro | 18 | ||||
-rw-r--r-- | tests/manual/boxplottester/customtablemodel.cpp | 147 | ||||
-rw-r--r-- | tests/manual/boxplottester/customtablemodel.h | 52 | ||||
-rw-r--r-- | tests/manual/boxplottester/main.cpp | 34 | ||||
-rw-r--r-- | tests/manual/boxplottester/mainwidget.cpp | 482 | ||||
-rw-r--r-- | tests/manual/boxplottester/mainwidget.h | 90 | ||||
-rw-r--r-- | tests/manual/boxplottester/pentool.cpp | 141 | ||||
-rw-r--r-- | tests/manual/boxplottester/pentool.h | 60 |
8 files changed, 1024 insertions, 0 deletions
diff --git a/tests/manual/boxplottester/boxplottester.pro b/tests/manual/boxplottester/boxplottester.pro new file mode 100644 index 00000000..1a8681fe --- /dev/null +++ b/tests/manual/boxplottester/boxplottester.pro @@ -0,0 +1,18 @@ +!include( ../../tests.pri ) { + error( "Couldn't find the test.pri file!" ) +} + +TEMPLATE = app + +QT += charts +QT += core gui widgets + +SOURCES += main.cpp \ + mainwidget.cpp \ + customtablemodel.cpp \ + pentool.cpp + +HEADERS += \ + mainwidget.h \ + customtablemodel.h \ + pentool.h diff --git a/tests/manual/boxplottester/customtablemodel.cpp b/tests/manual/boxplottester/customtablemodel.cpp new file mode 100644 index 00000000..8724dd56 --- /dev/null +++ b/tests/manual/boxplottester/customtablemodel.cpp @@ -0,0 +1,147 @@ +/**************************************************************************** +** +** Copyright (C) 2014 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 Enterprise Charts Add-on. +** +** $QT_BEGIN_LICENSE$ +** Licensees holding valid Qt Enterprise licenses may use this file in +** accordance with the Qt Enterprise 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$ +** +****************************************************************************/ + +#include "customtablemodel.h" +#include <QtCore/QVector> +#include <QtCore/QTime> +#include <QtCore/QRect> +#include <QtGui/QColor> + +const QString categories[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Nov", "Dec"}; + +CustomTableModel::CustomTableModel(QObject *parent) : + QAbstractTableModel(parent) +{ + m_columnCount = 6; + m_rowCount = 5; + QVector<qreal>* dataVec_Jan = new QVector<qreal>(m_rowCount); + dataVec_Jan->insert(0, 3.0); + dataVec_Jan->insert(1, 4.0); + dataVec_Jan->insert(2, 4.4); + dataVec_Jan->insert(3, 6.0); + dataVec_Jan->insert(4, 7.0); + m_data.append(dataVec_Jan); + + QVector<qreal>* dataVec_Feb = new QVector<qreal>(m_rowCount); + dataVec_Feb->insert(0, 5.0); + dataVec_Feb->insert(1, 6.0); + dataVec_Feb->insert(2, 7.5); + dataVec_Feb->insert(3, 8.0); + dataVec_Feb->insert(4, 12.0); + m_data.append(dataVec_Feb); + + QVector<qreal>* dataVec_Mar = new QVector<qreal>(m_rowCount); + dataVec_Mar->insert(0, 3.0); + dataVec_Mar->insert(1, 4.0); + dataVec_Mar->insert(2, 5.7); + dataVec_Mar->insert(3, 8.0); + dataVec_Mar->insert(4, 9.0); + m_data.append(dataVec_Mar); + + QVector<qreal>* dataVec_Apr = new QVector<qreal>(m_rowCount); + dataVec_Apr->insert(0, 5.0); + dataVec_Apr->insert(1, 6.0); + dataVec_Apr->insert(2, 6.8); + dataVec_Apr->insert(3, 7.0); + dataVec_Apr->insert(4, 8.0); + m_data.append(dataVec_Apr); + + QVector<qreal>* dataVec_May = new QVector<qreal>(m_rowCount); + dataVec_May->insert(0, 4.0); + dataVec_May->insert(1, 5.0); + dataVec_May->insert(2, 5.2); + dataVec_May->insert(3, 6.0); + dataVec_May->insert(4, 7.0); + m_data.append(dataVec_May); + + QVector<qreal>* dataVec_Jun = new QVector<qreal>(m_rowCount); + dataVec_Jun->insert(0, 4.0); + dataVec_Jun->insert(1, 7.0); + dataVec_Jun->insert(2, 8.2); + dataVec_Jun->insert(3, 9.0); + dataVec_Jun->insert(4, 10.0); + m_data.append(dataVec_Jun); +} + +CustomTableModel::~CustomTableModel() +{ + qDeleteAll(m_data); +} + +int CustomTableModel::rowCount(const QModelIndex &parent) const +{ + Q_UNUSED(parent) + return m_rowCount; +} + +int CustomTableModel::columnCount(const QModelIndex &parent) const +{ + Q_UNUSED(parent) + return m_data.count(); +} + +QVariant CustomTableModel::headerData(int section, Qt::Orientation orientation, int role) const +{ + if (role != Qt::DisplayRole) + return QVariant(); + + if (orientation == Qt::Horizontal) + return categories[section]; + else + return QString("%1").arg(section + 1); +} + +QVariant CustomTableModel::data(const QModelIndex &index, int role) const +{ + if (role == Qt::DisplayRole) { + return m_data[index.column()]->at(index.row()); + } else if (role == Qt::EditRole) { + return m_data[index.column()]->at(index.row()); + } else if (role == Qt::BackgroundRole) { + QRect rect; + foreach (rect, m_mapping) + if (rect.contains(index.column(), index.row())) + return QColor(m_mapping.key(rect)); + + // cell not mapped return white color + return QColor(Qt::white); + } + return QVariant(); +} + +bool CustomTableModel::setData(const QModelIndex &index, const QVariant &value, int role) +{ + if (index.isValid() && role == Qt::EditRole) { + m_data[index.column()]->replace(index.row(), value.toDouble()); + emit dataChanged(index, index); + return true; + } + return false; +} + +Qt::ItemFlags CustomTableModel::flags(const QModelIndex &index) const +{ + return QAbstractItemModel::flags(index) | Qt::ItemIsEditable; +} + +void CustomTableModel::addMapping(QString color, QRect area) +{ + m_mapping.insertMulti(color, area); +} diff --git a/tests/manual/boxplottester/customtablemodel.h b/tests/manual/boxplottester/customtablemodel.h new file mode 100644 index 00000000..0f1fb9df --- /dev/null +++ b/tests/manual/boxplottester/customtablemodel.h @@ -0,0 +1,52 @@ +/**************************************************************************** +** +** Copyright (C) 2014 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 Enterprise Charts Add-on. +** +** $QT_BEGIN_LICENSE$ +** Licensees holding valid Qt Enterprise licenses may use this file in +** accordance with the Qt Enterprise 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$ +** +****************************************************************************/ + +#ifndef CUSTOMTABLEMODEL_H +#define CUSTOMTABLEMODEL_H + +#include <QtCore/QAbstractTableModel> +#include <QtCore/QHash> +#include <QtCore/QRect> + +class CustomTableModel : public QAbstractTableModel +{ + Q_OBJECT +public: + explicit CustomTableModel(QObject *parent = 0); + virtual ~CustomTableModel(); + + int rowCount(const QModelIndex &parent = QModelIndex()) const; + int columnCount(const QModelIndex &parent = QModelIndex()) const; + QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const; + QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const; + bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole); + Qt::ItemFlags flags(const QModelIndex &index) const; + + void addMapping(QString color, QRect area); + void clearMapping() { m_mapping.clear(); } + +private: + QList<QVector<qreal> *> m_data; + QHash<QString, QRect> m_mapping; + int m_columnCount; + int m_rowCount; +}; + +#endif // CUSTOMTABLEMODEL_H diff --git a/tests/manual/boxplottester/main.cpp b/tests/manual/boxplottester/main.cpp new file mode 100644 index 00000000..6d136669 --- /dev/null +++ b/tests/manual/boxplottester/main.cpp @@ -0,0 +1,34 @@ +/**************************************************************************** +** +** Copyright (C) 2014 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 Enterprise Charts Add-on. +** +** $QT_BEGIN_LICENSE$ +** Licensees holding valid Qt Enterprise licenses may use this file in +** accordance with the Qt Enterprise 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$ +** +****************************************************************************/ + +#include <QtCore/QtGlobal> +#include <QtWidgets/QApplication> +#include "mainwidget.h" + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + + MainWidget w; + w.resize(1000,600); + w.show(); + + return a.exec(); +} diff --git a/tests/manual/boxplottester/mainwidget.cpp b/tests/manual/boxplottester/mainwidget.cpp new file mode 100644 index 00000000..784512d3 --- /dev/null +++ b/tests/manual/boxplottester/mainwidget.cpp @@ -0,0 +1,482 @@ +/**************************************************************************** +** +** Copyright (C) 2014 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 Enterprise Charts Add-on. +** +** $QT_BEGIN_LICENSE$ +** Licensees holding valid Qt Enterprise licenses may use this file in +** accordance with the Qt Enterprise 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$ +** +****************************************************************************/ + +#include "mainwidget.h" +#include "customtablemodel.h" +#include "pentool.h" +#include <QtCharts/QVBoxPlotModelMapper> +#include <QtWidgets/QTableView> +#include <QtWidgets/QHeaderView> +#include <QtCharts/QChartView> +#include <QtCharts/QBoxPlotSeries> +#include <QtCharts/QBoxSet> +#include <QtCharts/QLegend> +#include <QtCharts/QBarCategoryAxis> +#include <QtGui/QBrush> +#include <QtGui/QColor> +#include <QtWidgets/QPushButton> +#include <QtWidgets/QComboBox> +#include <QtWidgets/QSpinBox> +#include <QtWidgets/QCheckBox> +#include <QtWidgets/QGridLayout> +#include <QtWidgets/QHBoxLayout> +#include <QtWidgets/QLabel> +#include <QtWidgets/QSpacerItem> +#include <QtWidgets/QMessageBox> +#include <cmath> +#include <QtCore/QDebug> +#include <QtGui/QStandardItemModel> +#include <QtCharts/QBarCategoryAxis> +#include <QtCharts/QLogValueAxis> + +QT_CHARTS_USE_NAMESPACE + +static const QString allCategories[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; +static const int maxCategories = 12; + +MainWidget::MainWidget(QWidget *parent) : + QWidget(parent), + m_chart(0), + m_axis(0), + m_rowPos(0), + m_seriesCount(0) +{ + m_chart = new QChart(); + + m_penTool = new PenTool("Whiskers pen", this); + + // Grid layout for the controls for configuring the chart widget + QGridLayout *grid = new QGridLayout(); + + // Create add a series button + QPushButton *addSeriesButton = new QPushButton("Add a series"); + connect(addSeriesButton, SIGNAL(clicked()), this, SLOT(addSeries())); + grid->addWidget(addSeriesButton, m_rowPos++, 1); + + // Create remove a series button + QPushButton *removeSeriesButton = new QPushButton("Remove a series"); + connect(removeSeriesButton, SIGNAL(clicked()), this, SLOT(removeSeries())); + grid->addWidget(removeSeriesButton, m_rowPos++, 1); + + // Create add a single box button + QPushButton *addBoxButton = new QPushButton("Add a box"); + connect(addBoxButton, SIGNAL(clicked()), this, SLOT(addBox())); + grid->addWidget(addBoxButton, m_rowPos++, 1); + + // Create insert a box button + QPushButton *insertBoxButton = new QPushButton("Insert a box"); + connect(insertBoxButton, SIGNAL(clicked()), this, SLOT(insertBox())); + grid->addWidget(insertBoxButton, m_rowPos++, 1); + + // Create add a single box button + QPushButton *removeBoxButton = new QPushButton("Remove a box"); + connect(removeBoxButton, SIGNAL(clicked()), this, SLOT(removeBox())); + grid->addWidget(removeBoxButton, m_rowPos++, 1); + + // Create clear button + QPushButton *clearButton = new QPushButton("Clear"); + connect(clearButton, SIGNAL(clicked()), this, SLOT(clear())); + grid->addWidget(clearButton, m_rowPos++, 1); + + // Create clear button + QPushButton *clearBoxButton = new QPushButton("ClearBox"); + connect(clearBoxButton, SIGNAL(clicked()), this, SLOT(clearBox())); + grid->addWidget(clearBoxButton, m_rowPos++, 1); + + // Create set brush button + QPushButton *setBrushButton = new QPushButton("Set brush"); + connect(setBrushButton, SIGNAL(clicked()), this, SLOT(setBrush())); + grid->addWidget(setBrushButton, m_rowPos++, 1); + + // Create set whiskers pen button + QPushButton *setWhiskersButton = new QPushButton("Whiskers pen"); + connect(setWhiskersButton, SIGNAL(clicked()), m_penTool, SLOT(show())); + connect(m_penTool, SIGNAL(changed()), this, SLOT(changePen())); + grid->addWidget(setWhiskersButton, m_rowPos++, 1); + + // Box width setting + m_boxWidthSB = new QDoubleSpinBox(); + m_boxWidthSB->setMinimum(-1.0); + m_boxWidthSB->setMaximum(2.0); + m_boxWidthSB->setSingleStep(0.1); + m_boxWidthSB->setValue(0.5); + grid->addWidget(new QLabel("Box width:"), m_rowPos, 0); + grid->addWidget(m_boxWidthSB, m_rowPos++, 1); + connect(m_boxWidthSB, SIGNAL(valueChanged(double)), this, SLOT(setBoxWidth(double))); + + initThemeCombo(grid); + initCheckboxes(grid); + + QTableView *tableView = new QTableView; + m_model = new CustomTableModel(tableView); + tableView->setModel(m_model); + tableView->setMaximumWidth(200); + grid->addWidget(tableView, m_rowPos++, 0, 3, 2, Qt::AlignLeft); + tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch); + tableView->verticalHeader()->setSectionResizeMode(QHeaderView::Stretch); + + // add row with empty label to make all the other rows static + grid->addWidget(new QLabel(""), grid->rowCount(), 0); + grid->setRowStretch(grid->rowCount() - 1, 1); + + // Create chart view with the chart + m_chartView = new QChartView(m_chart, this); + + // As a default antialiasing is off + m_chartView->setRenderHint(QPainter::Antialiasing, false); + + // Another grid layout as a main layout + QGridLayout *mainLayout = new QGridLayout(); + mainLayout->addLayout(grid, 0, 0); + mainLayout->addWidget(m_chartView, 0, 1, 3, 1); + setLayout(mainLayout); + + legendToggled(false); + animationToggled(false); +} + +// Combo box for selecting theme +void MainWidget::initThemeCombo(QGridLayout *grid) +{ + QComboBox *chartTheme = new QComboBox(); + chartTheme->addItem("Default"); + chartTheme->addItem("Light"); + chartTheme->addItem("Blue Cerulean"); + chartTheme->addItem("Dark"); + chartTheme->addItem("Brown Sand"); + chartTheme->addItem("Blue NCS"); + chartTheme->addItem("High Contrast"); + chartTheme->addItem("Blue Icy"); + chartTheme->addItem("Qt"); + connect(chartTheme, SIGNAL(currentIndexChanged(int)), + this, SLOT(changeChartTheme(int))); + grid->addWidget(new QLabel("Chart theme:"), m_rowPos, 0); + grid->addWidget(chartTheme, m_rowPos++, 1); +} + +// Different check boxes for customizing chart +void MainWidget::initCheckboxes(QGridLayout *grid) +{ + QCheckBox *animationCheckBox = new QCheckBox("Animation"); + connect(animationCheckBox, SIGNAL(toggled(bool)), this, SLOT(animationToggled(bool))); + animationCheckBox->setChecked(false); + grid->addWidget(animationCheckBox, m_rowPos++, 0); + + QCheckBox *legendCheckBox = new QCheckBox("Legend"); + connect(legendCheckBox, SIGNAL(toggled(bool)), this, SLOT(legendToggled(bool))); + legendCheckBox->setChecked(false); + grid->addWidget(legendCheckBox, m_rowPos++, 0); + + QCheckBox *titleCheckBox = new QCheckBox("Title"); + connect(titleCheckBox, SIGNAL(toggled(bool)), this, SLOT(titleToggled(bool))); + titleCheckBox->setChecked(false); + grid->addWidget(titleCheckBox, m_rowPos++, 0); + + QCheckBox *antialiasingCheckBox = new QCheckBox("Antialiasing"); + connect(antialiasingCheckBox, SIGNAL(toggled(bool)), this, SLOT(antialiasingToggled(bool))); + antialiasingCheckBox->setChecked(false); + grid->addWidget(antialiasingCheckBox, m_rowPos++, 0); + + QCheckBox *modelMapperCheckBox = new QCheckBox("Use model mapper"); + connect(modelMapperCheckBox, SIGNAL(toggled(bool)), this, SLOT(modelMapperToggled(bool))); + modelMapperCheckBox->setChecked(false); + grid->addWidget(modelMapperCheckBox, m_rowPos++, 0); + + m_boxOutlined = new QCheckBox("Box outlined"); + connect(m_boxOutlined, SIGNAL(toggled(bool)), this, SLOT(boxOutlineToggled(bool))); + m_boxOutlined->setChecked(true); + grid->addWidget(m_boxOutlined, m_rowPos++, 0); +} + +void MainWidget::updateAxis(int categoryCount) +{ + if (!m_axis) { + m_chart->createDefaultAxes(); + m_axis = new QBarCategoryAxis(); + } + QStringList categories; + for (int i = 0; i < categoryCount; i++) + categories << allCategories[i]; + m_axis->setCategories(categories); +} + +void MainWidget::addSeries() +{ + qDebug() << "BoxPlotTester::MainWidget::addSeries()"; + + if (m_seriesCount > 9) + return; + + // Initial data + QBoxSet *set0 = new QBoxSet(); + QBoxSet *set1 = new QBoxSet(); + QBoxSet *set2 = new QBoxSet(); + QBoxSet *set3 = new QBoxSet(); + QBoxSet *set4 = new QBoxSet(); + QBoxSet *set5 = new QBoxSet(); + + // low bot med top upp + *set0 << -1 << 2 << 4 << 13 << 15; + *set1 << 5 << 6 << 7.5 << 8 << 12; + *set2 << 3 << 5 << 5.7 << 8 << 9; + *set3 << 5 << 6 << 6.8 << 7 << 8; + *set4 << 4 << 5 << 5.2 << 6 << 7; + *set5 << 4 << 7 << 8.2 << 9 << 10; + + m_series[m_seriesCount] = new QBoxPlotSeries(); + m_series[m_seriesCount]->append(set0); + m_series[m_seriesCount]->append(set1); + m_series[m_seriesCount]->append(set2); + m_series[m_seriesCount]->append(set3); + m_series[m_seriesCount]->append(set4); + m_series[m_seriesCount]->append(set5); + m_series[m_seriesCount]->setName("Box & Whiskers"); + + connect(m_series[m_seriesCount], SIGNAL(clicked(QBoxSet*)), this, SLOT(boxClicked(QBoxSet*))); + connect(m_series[m_seriesCount], SIGNAL(hovered(bool, QBoxSet*)), this, SLOT(boxHovered(bool, QBoxSet*))); + connect(set1, SIGNAL(clicked()), this, SLOT(singleBoxClicked())); + connect(set2, SIGNAL(hovered(bool)), this, SLOT(singleBoxHovered(bool))); + + m_series[m_seriesCount]->setBoxOutlineVisible(m_boxOutlined->checkState()); + m_series[m_seriesCount]->setBoxWidth(m_boxWidthSB->value()); + + m_chart->addSeries(m_series[m_seriesCount]); + + updateAxis(m_series[0]->count()); + m_chart->setAxisX(m_axis, m_series[m_seriesCount]); + + m_seriesCount++; +} + +void MainWidget::removeSeries() +{ + qDebug() << "BoxPlotTester::MainWidget::removeSeries()"; + + if (m_seriesCount > 0) { + m_seriesCount--; + m_chart->removeSeries(m_series[m_seriesCount]); + delete m_series[m_seriesCount]; + m_series[m_seriesCount] = 0; + } else { + qDebug() << "Create a series first"; + } +} + +void MainWidget::addBox() +{ + qDebug() << "BoxPlotTester::MainWidget::addBox()"; + + if (m_seriesCount > 0 && m_series[0]->count() < maxCategories) { + QBoxSet *newSet = new QBoxSet(); + newSet->setValue(QBoxSet::LowerExtreme, 5.0); + newSet->setValue(QBoxSet::LowerQuartile, 6.0); + newSet->setValue(QBoxSet::Median, 6.8); + newSet->setValue(QBoxSet::UpperQuartile, 7.0); + newSet->setValue(QBoxSet::UpperExtreme, 8.0); + + updateAxis(m_series[0]->count() + 1); + + m_series[0]->append(newSet); + } +} + +void MainWidget::insertBox() +{ + qDebug() << "BoxPlotTester::MainWidget::insertBox()"; + + if (m_seriesCount > 0 && m_series[0]->count() < maxCategories) { + updateAxis(m_series[0]->count() + 1); + for (int i = 0; i < m_seriesCount; i++) { + QBoxSet *newSet = new QBoxSet(); + *newSet << 2 << 6 << 6.8 << 7 << 10; + m_series[i]->insert(1, newSet); + } + } +} + +void MainWidget::removeBox() +{ + qDebug() << "BoxPlotTester::MainWidget::removeBox"; + + if (m_seriesCount > 0) { + for (int i = 0; i < m_seriesCount; i++) { + qDebug() << "m_series[i]->count() = " << m_series[i]->count(); + if (m_series[i]->count()) { + QList<QBoxSet *> sets = m_series[i]->boxSets(); + m_series[i]->remove(sets.at(m_series[i]->count() - 1)); + } + } + + updateAxis(m_series[0]->count()); + } else { + qDebug() << "Create a series first"; + } +} + +void MainWidget::clear() +{ + qDebug() << "BoxPlotTester::MainWidget::clear"; + + if (m_seriesCount > 0) + m_series[0]->clear(); + else + qDebug() << "Create a series first"; +} + +void MainWidget::clearBox() +{ + qDebug() << "BoxPlotTester::MainWidget::clearBox"; + + if (m_seriesCount > 0) { + QList<QBoxSet *> sets = m_series[0]->boxSets(); + if (sets.count() > 1) + sets.at(1)->clear(); + else + qDebug() << "Create a series with at least two items first"; + } else { + qDebug() << "Create a series first"; + } +} + +void MainWidget::setBrush() +{ + qDebug() << "BoxPlotTester::MainWidget::setBrush"; + + if (m_seriesCount > 0) { + QList<QBoxSet *> sets = m_series[0]->boxSets(); + if (sets.count() > 1) + sets.at(1)->setBrush(QBrush(QColor(Qt::yellow))); + else + qDebug() << "Create a series with at least two items first"; + } else { + qDebug() << "Create a series first"; + } +} + +void MainWidget::animationToggled(bool enabled) +{ + qDebug() << "BoxPlotTester::Animation toggled to " << enabled; + if (enabled) + m_chart->setAnimationOptions(QChart::SeriesAnimations); + else + m_chart->setAnimationOptions(QChart::NoAnimation); +} + +void MainWidget::legendToggled(bool enabled) +{ + qDebug() << "BoxPlotTester::Legend toggled to " << enabled; + m_chart->legend()->setVisible(enabled); + if (enabled) + m_chart->legend()->setAlignment(Qt::AlignBottom); +} + +void MainWidget::titleToggled(bool enabled) +{ + qDebug() << "BoxPlotTester::Title toggled to " << enabled; + if (enabled) + m_chart->setTitle("Simple boxplotchart example"); + else + m_chart->setTitle(""); +} + +void MainWidget::antialiasingToggled(bool enabled) +{ + qDebug() << "BoxPlotTester::antialiasingToggled toggled to " << enabled; + m_chartView->setRenderHint(QPainter::Antialiasing, enabled); +} + +void MainWidget::boxOutlineToggled(bool visible) +{ + qDebug() << "BoxPlotTester::boxOutlineToggled toggled to " << visible; + for (int i = 0; i < m_seriesCount; i++) + m_series[i]->setBoxOutlineVisible(visible); +} + +void MainWidget::modelMapperToggled(bool enabled) +{ + if (enabled) { + m_series[m_seriesCount] = new QBoxPlotSeries(); + + int first = 0; + int count = 5; + QVBoxPlotModelMapper *mapper = new QVBoxPlotModelMapper(this); + mapper->setFirstBoxSetColumn(0); + mapper->setLastBoxSetColumn(5); + mapper->setFirstRow(first); + mapper->setRowCount(count); + mapper->setSeries(m_series[m_seriesCount]); + mapper->setModel(m_model); + m_chart->addSeries(m_series[m_seriesCount]); + + m_seriesCount++; + } else { + removeSeries(); + } +} + +void MainWidget::changeChartTheme(int themeIndex) +{ + qDebug() << "BoxPlotTester::changeChartTheme: " << themeIndex; + if (themeIndex == 0) + m_chart->setTheme(QChart::ChartThemeLight); + else + m_chart->setTheme((QChart::ChartTheme) (themeIndex - 1)); +} + +void MainWidget::boxClicked(QBoxSet *set) +{ + qDebug() << "boxClicked, median = " << set->at(QBoxSet::Median); +} + +void MainWidget::boxHovered(bool state, QBoxSet *set) +{ + if (state) + qDebug() << "box median " << set->at(QBoxSet::Median) << " hover started"; + else + qDebug() << "box median " << set->at(QBoxSet::Median) << " hover ended"; +} + +void MainWidget::singleBoxClicked() +{ + qDebug() << "singleBoxClicked"; +} + +void MainWidget::singleBoxHovered(bool state) +{ + if (state) + qDebug() << "single box hover started"; + else + qDebug() << "single box hover ended"; +} + +void MainWidget::changePen() +{ + qDebug() << "changePen() = " << m_penTool->pen(); + for (int i = 0; i < m_seriesCount; i++) + m_series[i]->setPen(m_penTool->pen()); +} + +void MainWidget::setBoxWidth(double width) +{ + qDebug() << "setBoxWidth to " << width; + + for (int i = 0; i < m_seriesCount; i++) + m_series[i]->setBoxWidth(qreal(width)); +} diff --git a/tests/manual/boxplottester/mainwidget.h b/tests/manual/boxplottester/mainwidget.h new file mode 100644 index 00000000..cbb2cd38 --- /dev/null +++ b/tests/manual/boxplottester/mainwidget.h @@ -0,0 +1,90 @@ +/**************************************************************************** +** +** Copyright (C) 2014 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 Enterprise Charts Add-on. +** +** $QT_BEGIN_LICENSE$ +** Licensees holding valid Qt Enterprise licenses may use this file in +** accordance with the Qt Enterprise 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$ +** +****************************************************************************/ + +#ifndef MAINWIDGET_H +#define MAINWIDGET_H + +#include <QtCharts/QChartGlobal> +#include <QtCharts/QChart> +#include <QtCharts/QChartView> +#include <QtWidgets/QWidget> +#include <QtCharts/QBoxPlotSeries> +#include <QtCharts/QBarCategoryAxis> +#include <QtCharts/QBoxSet> +#include <QtWidgets/QCheckBox> +#include <QtWidgets/QDoubleSpinBox> + +class QGridLayout; +class CustomTableModel; +class PenTool; + +QT_CHARTS_USE_NAMESPACE + +class MainWidget : public QWidget +{ + Q_OBJECT +public: + explicit MainWidget(QWidget *parent = 0); + +signals: + +private: + void initThemeCombo(QGridLayout *grid); + void initCheckboxes(QGridLayout *grid); + void updateAxis(int categoryCount); + +private slots: + void addSeries(); + void removeSeries(); + void addBox(); + void insertBox(); + void removeBox(); + void clear(); + void clearBox(); + void setBrush(); + void animationToggled(bool enabled); + void legendToggled(bool enabled); + void titleToggled(bool enabled); + void modelMapperToggled(bool enabled); + void changeChartTheme(int themeIndex); + void boxClicked(QBoxSet *set); + void boxHovered(bool state, QBoxSet *set); + void singleBoxClicked(); + void singleBoxHovered(bool state); + void changePen(); + void antialiasingToggled(bool); + void boxOutlineToggled(bool); + void setBoxWidth(double width); + +private: + QChart *m_chart; + QChartView *m_chartView; + QGridLayout *m_scatterLayout; + QBarCategoryAxis *m_axis; + CustomTableModel *m_model; + PenTool *m_penTool; + int m_rowPos; + int m_seriesCount; + QBoxPlotSeries *m_series[10]; + QCheckBox *m_boxOutlined; + QDoubleSpinBox *m_boxWidthSB; +}; + +#endif // MAINWIDGET_H diff --git a/tests/manual/boxplottester/pentool.cpp b/tests/manual/boxplottester/pentool.cpp new file mode 100644 index 00000000..b827039f --- /dev/null +++ b/tests/manual/boxplottester/pentool.cpp @@ -0,0 +1,141 @@ +/**************************************************************************** +** +** Copyright (C) 2014 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 Enterprise Charts Add-on. +** +** $QT_BEGIN_LICENSE$ +** Licensees holding valid Qt Enterprise licenses may use this file in +** accordance with the Qt Enterprise 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$ +** +****************************************************************************/ + +#include "pentool.h" +#include <QtWidgets/QPushButton> +#include <QtWidgets/QDoubleSpinBox> +#include <QtWidgets/QComboBox> +#include <QtWidgets/QFormLayout> +#include <QtWidgets/QColorDialog> + +PenTool::PenTool(QString title, QWidget *parent) + : QWidget(parent) +{ + setWindowTitle(title); + setWindowFlags(Qt::Tool); + + m_colorButton = new QPushButton(); + + m_widthSpinBox = new QDoubleSpinBox(); + + m_styleCombo = new QComboBox(); + m_styleCombo->addItem("NoPen"); + m_styleCombo->addItem("SolidLine"); + m_styleCombo->addItem("DashLine"); + m_styleCombo->addItem("DotLine"); + m_styleCombo->addItem("DashDotLine"); + m_styleCombo->addItem("DashDotDotLine"); + + m_capStyleCombo = new QComboBox(); + m_capStyleCombo->addItem("FlatCap", Qt::FlatCap); + m_capStyleCombo->addItem("SquareCap", Qt::SquareCap); + m_capStyleCombo->addItem("RoundCap", Qt::RoundCap); + + m_joinStyleCombo = new QComboBox(); + m_joinStyleCombo->addItem("MiterJoin", Qt::MiterJoin); + m_joinStyleCombo->addItem("BevelJoin", Qt::BevelJoin); + m_joinStyleCombo->addItem("RoundJoin", Qt::RoundJoin); + m_joinStyleCombo->addItem("SvgMiterJoin", Qt::SvgMiterJoin); + + QFormLayout *layout = new QFormLayout(); + layout->addRow("Color", m_colorButton); + layout->addRow("Width", m_widthSpinBox); + layout->addRow("Style", m_styleCombo); + layout->addRow("Cap style", m_capStyleCombo); + layout->addRow("Join style", m_joinStyleCombo); + setLayout(layout); + + connect(m_colorButton, SIGNAL(clicked()), this, SLOT(showColorDialog())); + connect(m_widthSpinBox, SIGNAL(valueChanged(double)), this, SLOT(updateWidth(double))); + connect(m_styleCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(updateStyle(int))); + connect(m_capStyleCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(updateCapStyle(int))); + connect(m_joinStyleCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(updateJoinStyle(int))); +} + +void PenTool::setPen(const QPen &pen) +{ + m_pen = pen; + m_colorButton->setText(m_pen.color().name()); + m_widthSpinBox->setValue(m_pen.widthF()); + m_styleCombo->setCurrentIndex(m_pen.style()); // index matches the enum + m_capStyleCombo->setCurrentIndex(m_capStyleCombo->findData(m_pen.capStyle())); + m_joinStyleCombo->setCurrentIndex(m_joinStyleCombo->findData(m_pen.joinStyle())); +} + +QPen PenTool::pen() const +{ + return m_pen; +} + +QString PenTool::name() +{ + return name(m_pen); +} + +QString PenTool::name(const QPen &pen) +{ + return pen.color().name() + ":" + QString::number(pen.widthF()); +} + +void PenTool::showColorDialog() +{ + QColorDialog dialog(m_pen.color()); + dialog.show(); + dialog.exec(); + m_pen.setColor(dialog.selectedColor()); + m_colorButton->setText(m_pen.color().name()); + emit changed(); +} + +void PenTool::updateWidth(double width) +{ + if (!qFuzzyCompare((qreal) width, m_pen.widthF())) { + m_pen.setWidthF(width); + emit changed(); + } +} + +void PenTool::updateStyle(int style) +{ + if (m_pen.style() != style) { + m_pen.setStyle((Qt::PenStyle) style); + emit changed(); + } +} + +void PenTool::updateCapStyle(int index) +{ + Qt::PenCapStyle capStyle = (Qt::PenCapStyle) m_capStyleCombo->itemData(index).toInt(); + if (m_pen.capStyle() != capStyle) { + m_pen.setCapStyle(capStyle); + emit changed(); + } +} + +void PenTool::updateJoinStyle(int index) +{ + Qt::PenJoinStyle joinStyle = (Qt::PenJoinStyle) m_joinStyleCombo->itemData(index).toInt(); + if (m_pen.joinStyle() != joinStyle) { + m_pen.setJoinStyle(joinStyle); + emit changed(); + } +} + +#include "moc_pentool.cpp" diff --git a/tests/manual/boxplottester/pentool.h b/tests/manual/boxplottester/pentool.h new file mode 100644 index 00000000..13b4064d --- /dev/null +++ b/tests/manual/boxplottester/pentool.h @@ -0,0 +1,60 @@ +/**************************************************************************** +** +** Copyright (C) 2014 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 Enterprise Charts Add-on. +** +** $QT_BEGIN_LICENSE$ +** Licensees holding valid Qt Enterprise licenses may use this file in +** accordance with the Qt Enterprise 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$ +** +****************************************************************************/ +#ifndef PENTOOL_H +#define PENTOOL_H + +#include <QtWidgets/QWidget> +#include <QtGui/QPen> + +class QPushButton; +class QDoubleSpinBox; +class QComboBox; + +class PenTool : public QWidget +{ + Q_OBJECT + +public: + explicit PenTool(QString title, QWidget *parent = 0); + void setPen(const QPen &pen); + QPen pen() const; + QString name(); + static QString name(const QPen &pen); + +Q_SIGNALS: + void changed(); + +public Q_SLOTS: + void showColorDialog(); + void updateWidth(double width); + void updateStyle(int style); + void updateCapStyle(int index); + void updateJoinStyle(int index); + +private: + QPen m_pen; + QPushButton *m_colorButton; + QDoubleSpinBox *m_widthSpinBox; + QComboBox *m_styleCombo; + QComboBox *m_capStyleCombo; + QComboBox *m_joinStyleCombo; +}; + +#endif // PENTOOL_H |