diff options
Diffstat (limited to 'examples/bars/main.cpp')
-rw-r--r-- | examples/bars/main.cpp | 406 |
1 files changed, 165 insertions, 241 deletions
diff --git a/examples/bars/main.cpp b/examples/bars/main.cpp index 6ab685ed..158244b4 100644 --- a/examples/bars/main.cpp +++ b/examples/bars/main.cpp @@ -16,272 +16,196 @@ ** ****************************************************************************/ -#include <QtDataVisualization/q3dbars.h> -#include <QtDataVisualization/q3dcategoryaxis.h> -#include <QtDataVisualization/qitemmodelbardataproxy.h> -#include <QtDataVisualization/q3dvalueaxis.h> -#include <QtDataVisualization/q3dscene.h> -#include <QtDataVisualization/q3dcamera.h> +#include "graphmodifier.h" #include <QApplication> +#include <QWidget> +#include <QHBoxLayout> #include <QVBoxLayout> -#include <QTableWidget> +#include <QPushButton> +#include <QCheckBox> +#include <QSlider> +#include <QFontComboBox> +#include <QLabel> #include <QScreen> -#include <QTimer> -#include <QFont> -#include <QDebug> -#include <QHeaderView> +#include <QFontDatabase> -#define USE_STATIC_DATA - -using namespace QtDataVisualization; - -class GraphDataGenerator : public QObject +int main(int argc, char **argv) { -public: - explicit GraphDataGenerator(Q3DBars *bargraph, QTableWidget *tableWidget); - ~GraphDataGenerator(); - - void setupModel(); - void addRow(); - void changeStyle(); - void changePresetCamera(); - void changeTheme(); - void start(); - void selectFromTable(const QPoint &selection); - void selectedFromTable(int currentRow, int currentColumn, int previousRow, int previousColumn); - void fixTableSize(); + //! [0] + QApplication app(argc, argv); + Q3DBars *widgetgraph = new Q3DBars(); + QWidget *container = QWidget::createWindowContainer(widgetgraph); + //! [0] -private: - Q3DBars *m_graph; - QTimer *m_dataTimer; - QTimer *m_styleTimer; - QTimer *m_presetTimer; - QTimer *m_themeTimer; - int m_columnCount; - int m_rowCount; - QTableWidget *m_tableWidget; // not owned -}; + QSize screenSize = widgetgraph->screen()->size(); + container->setMinimumSize(QSize(screenSize.width() / 2, screenSize.height() / 1.5)); + container->setMaximumSize(screenSize); + container->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); + container->setFocusPolicy(Qt::StrongFocus); -GraphDataGenerator::GraphDataGenerator(Q3DBars *bargraph, QTableWidget *tableWidget) - : m_graph(bargraph), - m_dataTimer(0), - m_styleTimer(0), - m_presetTimer(0), - m_themeTimer(0), - m_columnCount(100), - m_rowCount(50), - m_tableWidget(tableWidget) -{ - //! [5] - // Set up bar specifications; make the bars as wide as they are deep, - // and add a small space between them - m_graph->setBarThickness(1.0); - m_graph->setBarSpacing(QSizeF(0.2, 0.2)); + //! [1] + QWidget *widget = new QWidget; + QHBoxLayout *hLayout = new QHBoxLayout(widget); + QVBoxLayout *vLayout = new QVBoxLayout(); + hLayout->addWidget(container, 1); + hLayout->addLayout(vLayout); + //! [1] - // Set bar type to flat pyramids - m_graph->setBarType(QDataVis::MeshStylePyramids, false); + widget->setWindowTitle(QStringLiteral("Average temperatures in Oulu, Finland (2006-2012)")); + + QComboBox *themeList = new QComboBox(widget); + themeList->addItem(QStringLiteral("Qt")); + themeList->addItem(QStringLiteral("Primary Colors")); + themeList->addItem(QStringLiteral("Digia")); + themeList->addItem(QStringLiteral("Stone Moss")); + themeList->addItem(QStringLiteral("Army Blue")); + themeList->addItem(QStringLiteral("Retro")); + themeList->addItem(QStringLiteral("Ebony")); + themeList->addItem(QStringLiteral("Isabelle")); + themeList->setCurrentIndex(0); + + QPushButton *labelButton = new QPushButton(widget); + labelButton->setText(QStringLiteral("Change label style")); + + QCheckBox *smoothCheckBox = new QCheckBox(widget); + smoothCheckBox->setText(QStringLiteral("Smooth bars")); + smoothCheckBox->setChecked(false); + + QComboBox *barStyleList = new QComboBox(widget); + barStyleList->addItem(QStringLiteral("Bars")); + barStyleList->addItem(QStringLiteral("Pyramids")); + barStyleList->addItem(QStringLiteral("Cones")); + barStyleList->addItem(QStringLiteral("Cylinders")); + barStyleList->addItem(QStringLiteral("Beveled Bars")); + barStyleList->setCurrentIndex(4); + + QPushButton *cameraButton = new QPushButton(widget); + cameraButton->setText(QStringLiteral("Change camera preset")); + + QComboBox *selectionModeList = new QComboBox(widget); + selectionModeList->addItem(QStringLiteral("None")); + selectionModeList->addItem(QStringLiteral("Bar")); + selectionModeList->addItem(QStringLiteral("Bar and Row")); + selectionModeList->addItem(QStringLiteral("Bar and Column")); + selectionModeList->addItem(QStringLiteral("Bar, Row and Column")); + selectionModeList->addItem(QStringLiteral("Slice into Row")); + selectionModeList->addItem(QStringLiteral("Slice into Column")); + selectionModeList->setCurrentIndex(1); + + QCheckBox *backgroundCheckBox = new QCheckBox(widget); + backgroundCheckBox->setText(QStringLiteral("Show background")); + backgroundCheckBox->setChecked(false); + + QCheckBox *gridCheckBox = new QCheckBox(widget); + gridCheckBox->setText(QStringLiteral("Show grid")); + gridCheckBox->setChecked(true); - //! [5] + //! [4] + QSlider *rotationSliderX = new QSlider(Qt::Horizontal, widget); + rotationSliderX->setTickInterval(30); + rotationSliderX->setTickPosition(QSlider::TicksBelow); + rotationSliderX->setMinimum(-180); + rotationSliderX->setValue(0); + rotationSliderX->setMaximum(180); + QSlider *rotationSliderY = new QSlider(Qt::Horizontal, widget); + rotationSliderY->setTickInterval(15); + rotationSliderY->setTickPosition(QSlider::TicksAbove); + rotationSliderY->setMinimum(-90); + rotationSliderY->setValue(0); + rotationSliderY->setMaximum(90); + //! [4] -#ifndef USE_STATIC_DATA - // Set up sample space; make it as deep as it's wide - m_graph->setDataWindow(m_rowCount, m_columnCount); - m_tableWidget->setColumnCount(m_columnCount); + QSlider *fontSizeSlider = new QSlider(Qt::Horizontal, widget); + fontSizeSlider->setTickInterval(10); + fontSizeSlider->setTickPosition(QSlider::TicksBelow); + fontSizeSlider->setMinimum(1); + fontSizeSlider->setValue(30); + fontSizeSlider->setMaximum(100); + + QFontComboBox *fontList = new QFontComboBox(widget); + fontList->setCurrentFont(QFont("Times New Roman")); + + QComboBox *shadowQuality = new QComboBox(widget); + shadowQuality->addItem(QStringLiteral("None")); + shadowQuality->addItem(QStringLiteral("Low")); + shadowQuality->addItem(QStringLiteral("Medium")); + shadowQuality->addItem(QStringLiteral("High")); + shadowQuality->addItem(QStringLiteral("Low Soft")); + shadowQuality->addItem(QStringLiteral("Medium Soft")); + shadowQuality->addItem(QStringLiteral("High Soft")); + shadowQuality->setCurrentIndex(5); - // Set selection mode to full - m_graph->setSelectionMode(QDataVis::SelectionModeItemRowAndColumn); + //! [5] + vLayout->addWidget(new QLabel(QStringLiteral("Rotate horizontally"))); + vLayout->addWidget(rotationSliderX, 0, Qt::AlignTop); + vLayout->addWidget(new QLabel(QStringLiteral("Rotate vertically"))); + vLayout->addWidget(rotationSliderY, 0, Qt::AlignTop); + //! [5] + vLayout->addWidget(labelButton, 0, Qt::AlignTop); + vLayout->addWidget(cameraButton, 0, Qt::AlignTop); + vLayout->addWidget(backgroundCheckBox); + vLayout->addWidget(gridCheckBox); + vLayout->addWidget(smoothCheckBox, 0, Qt::AlignTop); + vLayout->addWidget(new QLabel(QStringLiteral("Change bar style"))); + vLayout->addWidget(barStyleList); + vLayout->addWidget(new QLabel(QStringLiteral("Change selection mode"))); + vLayout->addWidget(selectionModeList); + vLayout->addWidget(new QLabel(QStringLiteral("Change theme"))); + vLayout->addWidget(themeList); + vLayout->addWidget(new QLabel(QStringLiteral("Adjust shadow quality"))); + vLayout->addWidget(shadowQuality); + vLayout->addWidget(new QLabel(QStringLiteral("Change font"))); + vLayout->addWidget(fontList); + vLayout->addWidget(new QLabel(QStringLiteral("Adjust font size"))); + vLayout->addWidget(fontSizeSlider, 1, Qt::AlignTop); - // Hide axis labels by explicitly setting one empty string as label list - m_graph->rowAxis()->setCategoryLabels(QStringList(QString())); - m_graph->columnAxis()->setCategoryLabels(QStringList(QString())); + //! [2] + GraphModifier *modifier = new GraphModifier(widgetgraph); + //! [2] - m_graph->activeDataProxy()->setItemLabelFormat(QStringLiteral("@valueLabel")); -#else //! [6] - - // Set selection mode to slice row - m_graph->setSelectionMode(QDataVis::SelectionModeSliceRow); - - // Set font - m_graph->setFont(QFont("Impact", 20)); - + QObject::connect(rotationSliderX, &QSlider::valueChanged, modifier, &GraphModifier::rotateX); + QObject::connect(rotationSliderY, &QSlider::valueChanged, modifier, &GraphModifier::rotateY); //! [6] -#endif - //! [7] + QObject::connect(labelButton, &QPushButton::clicked, modifier, + &GraphModifier::changeLabelStyle); + QObject::connect(cameraButton, &QPushButton::clicked, modifier, + &GraphModifier::changePresetCamera); - // Set theme - m_graph->setTheme(QDataVis::ThemeDigia); + QObject::connect(backgroundCheckBox, &QCheckBox::stateChanged, modifier, + &GraphModifier::setBackgroundEnabled); + QObject::connect(gridCheckBox, &QCheckBox::stateChanged, modifier, + &GraphModifier::setGridEnabled); + QObject::connect(smoothCheckBox, &QCheckBox::stateChanged, modifier, + &GraphModifier::setSmoothBars); - // Set preset camera position - m_graph->scene()->activeCamera()->setCameraPreset(QDataVis::CameraPresetFront); - //! [7] -} + QObject::connect(barStyleList, SIGNAL(currentIndexChanged(int)), modifier, + SLOT(changeStyle(int))); -GraphDataGenerator::~GraphDataGenerator() -{ - if (m_dataTimer) { - m_dataTimer->stop(); - delete m_dataTimer; - } - delete m_graph; -} - -void GraphDataGenerator::start() -{ -#ifndef USE_STATIC_DATA - m_dataTimer = new QTimer(); - m_dataTimer->setTimerType(Qt::CoarseTimer); - QObject::connect(m_dataTimer, &QTimer::timeout, this, &GraphDataGenerator::addRow); - m_dataTimer->start(0); - m_tableWidget->setFixedWidth(m_graph->width()); -#else - //! [8] - setupModel(); - // Table needs to be shown before the size of its headers can be accurately obtained, - // so we postpone it a bit - m_dataTimer = new QTimer(); - m_dataTimer->setSingleShot(true); - QObject::connect(m_dataTimer, &QTimer::timeout, this, &GraphDataGenerator::fixTableSize); - m_dataTimer->start(0); - //! [8] -#endif -} + QObject::connect(selectionModeList, SIGNAL(currentIndexChanged(int)), modifier, + SLOT(changeSelectionMode(int))); -void GraphDataGenerator::setupModel() -{ - //! [9] - // Set up row and column names - QStringList days; - days << "Monday" << "Tuesday" << "Wednesday" << "Thursday" << "Friday" << "Saturday" << "Sunday"; - QStringList weeks; - weeks << "week 1" << "week 2" << "week 3" << "week 4" << "week 5"; - - // Set up data Mon Tue Wed Thu Fri Sat Sun - float hours[5][7] = {{2.0, 1.0, 3.0, 0.2, 1.0, 5.0, 10.0}, // week 1 - {0.5, 1.0, 3.0, 1.0, 2.0, 2.0, 3.0}, // week 2 - {1.0, 1.0, 2.0, 1.0, 4.0, 4.0, 4.0}, // week 3 - {0.0, 1.0, 0.0, 0.0, 2.0, 2.0, 0.3}, // week 4 - {3.0, 3.0, 6.0, 2.0, 2.0, 1.0, 1.0}}; // week 5 - //! [9] + QObject::connect(themeList, SIGNAL(currentIndexChanged(int)), modifier, + SLOT(changeTheme(int))); - // Add labels - //! [10] - m_graph->rowAxis()->setTitle("Week of year"); - m_graph->columnAxis()->setTitle("Day of week"); - m_graph->valueAxis()->setTitle("Hours spent on the Internet"); - m_graph->valueAxis()->setSegmentCount(5); - m_graph->valueAxis()->setLabelFormat("%.1f h"); - //! [10] + QObject::connect(shadowQuality, SIGNAL(currentIndexChanged(int)), modifier, + SLOT(changeShadowQuality(int))); - //! [11] - m_tableWidget->setRowCount(5); - m_tableWidget->setColumnCount(7); - m_tableWidget->setHorizontalHeaderLabels(days); - m_tableWidget->setVerticalHeaderLabels(weeks); - m_tableWidget->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); - m_tableWidget->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); - m_tableWidget->setCurrentCell(-1, -1); - //! [11] + QObject::connect(modifier, &GraphModifier::shadowQualityChanged, shadowQuality, + &QComboBox::setCurrentIndex); + QObject::connect(widgetgraph, &Q3DBars::shadowQualityChanged, modifier, + &GraphModifier::shadowQualityUpdatedByVisual); - //! [12] - for (int week = 0; week < weeks.size(); week++) { - for (int day = 0; day < days.size(); day++) { - QModelIndex index = m_tableWidget->model()->index(week, day); - m_tableWidget->model()->setData(index, hours[week][day]); - } - } - //! [12] -} + QObject::connect(fontSizeSlider, &QSlider::valueChanged, modifier, + &GraphModifier::changeFontSize); + QObject::connect(fontList, &QFontComboBox::currentFontChanged, modifier, + &GraphModifier::changeFont); -void GraphDataGenerator::addRow() -{ - m_tableWidget->model()->insertRow(0); - if (m_tableWidget->model()->rowCount() > m_rowCount) - m_tableWidget->model()->removeRow(m_rowCount); - for (int i = 0; i < m_columnCount; i++) { - QModelIndex index = m_tableWidget->model()->index(0, i); - m_tableWidget->model()->setData(index, - ((qreal)i / (qreal)m_columnCount) / 2.0 + (qreal)(rand() % 30) / 100.0); - } - m_tableWidget->resizeColumnsToContents(); -} - -//! [13] -void GraphDataGenerator::selectFromTable(const QPoint &selection) -{ - m_tableWidget->setFocus(); - m_tableWidget->setCurrentCell(selection.x(), selection.y()); -} -//! [13] - -//! [14] -void GraphDataGenerator::selectedFromTable(int currentRow, int currentColumn, - int previousRow, int previousColumn) -{ - Q_UNUSED(previousRow) - Q_UNUSED(previousColumn) - m_graph->setSelectedBarPos(QPoint(currentRow, currentColumn)); -} -//! [14] - -void GraphDataGenerator::fixTableSize() -{ - int width = m_tableWidget->horizontalHeader()->length(); - width += m_tableWidget->verticalHeader()->width(); - m_tableWidget->setFixedWidth(width + 2); - int height = m_tableWidget->verticalHeader()->length(); - height += m_tableWidget->horizontalHeader()->height(); - m_tableWidget->setFixedHeight(height + 2); -} - -int main(int argc, char **argv) -{ - //! [0] - QApplication app(argc, argv); - Q3DBars *graph = new Q3DBars(); - QWidget *container = QWidget::createWindowContainer(graph); - //! [0] - - QSize screenSize = graph->screen()->size(); - container->setMinimumSize(QSize(screenSize.width() / 2, screenSize.height() / 2)); - container->setMaximumSize(screenSize); - container->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); - container->setFocusPolicy(Qt::StrongFocus); - - //! [1] - QWidget widget; - QVBoxLayout *layout = new QVBoxLayout(&widget); - QTableWidget *tableWidget = new QTableWidget(&widget); - layout->addWidget(container, 1); - layout->addWidget(tableWidget, 1, Qt::AlignHCenter); - //! [1] - - tableWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed); - tableWidget->setAlternatingRowColors(true); - widget.setWindowTitle(QStringLiteral("Hours spent on the Internet")); - - //! [2] - // Since we are dealing with QTableWidget, the model will already have data sorted properly - // in rows and columns, so create a mapping to utilize this. - QItemModelBarDataMapping *mapping = new QItemModelBarDataMapping; - mapping->setUseModelCategories(true); - QItemModelBarDataProxy *proxy = new QItemModelBarDataProxy(tableWidget->model(), mapping); - graph->setActiveDataProxy(proxy); - //! [2] - - //! [3] - GraphDataGenerator generator(graph, tableWidget); - QObject::connect(graph, &Q3DBars::selectedBarPosChanged, &generator, - &GraphDataGenerator::selectFromTable); - QObject::connect(tableWidget, &QTableWidget::currentCellChanged, &generator, - &GraphDataGenerator::selectedFromTable); //! [3] - - //! [4] - widget.show(); - generator.start(); + widget->show(); + modifier->start(); return app.exec(); - //! [4] + //! [3] } |