From 88cd10aa7b3559b092cf5575b0a17d002dc100ae Mon Sep 17 00:00:00 2001 From: Miikka Heikkinen Date: Thu, 13 Feb 2014 09:59:52 +0200 Subject: Fix examples installation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Had to add one folder to the examples structure so installation works correctly. Change-Id: Ic92dfe9997413a6243abcf5eeba12744ba9e938c Reviewed-by: Tomi Korpipää --- .../scatter/doc/images/scatter-example.png | Bin 0 -> 142496 bytes .../datavisualization/scatter/doc/src/scatter.qdoc | 146 +++++++++++++++ examples/datavisualization/scatter/main.cpp | 176 ++++++++++++++++++ examples/datavisualization/scatter/scatter.pro | 8 + .../scatter/scatterdatamodifier.cpp | 203 +++++++++++++++++++++ .../scatter/scatterdatamodifier.h | 69 +++++++ 6 files changed, 602 insertions(+) create mode 100644 examples/datavisualization/scatter/doc/images/scatter-example.png create mode 100644 examples/datavisualization/scatter/doc/src/scatter.qdoc create mode 100644 examples/datavisualization/scatter/main.cpp create mode 100644 examples/datavisualization/scatter/scatter.pro create mode 100644 examples/datavisualization/scatter/scatterdatamodifier.cpp create mode 100644 examples/datavisualization/scatter/scatterdatamodifier.h (limited to 'examples/datavisualization/scatter') diff --git a/examples/datavisualization/scatter/doc/images/scatter-example.png b/examples/datavisualization/scatter/doc/images/scatter-example.png new file mode 100644 index 00000000..82c13855 Binary files /dev/null and b/examples/datavisualization/scatter/doc/images/scatter-example.png differ diff --git a/examples/datavisualization/scatter/doc/src/scatter.qdoc b/examples/datavisualization/scatter/doc/src/scatter.qdoc new file mode 100644 index 00000000..91227184 --- /dev/null +++ b/examples/datavisualization/scatter/doc/src/scatter.qdoc @@ -0,0 +1,146 @@ +/**************************************************************************** +** +** 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 QtDataVisualization module. +** +** 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 +** +****************************************************************************/ + +/*! + \example scatter + \title Scatter Example + \ingroup qtdatavisualization_examples + \brief Using Q3DScatter in a widget application. + + The scatter example shows how to make a simple 3D scatter graph using Q3DScatter and + combining the use of widgets for adjusting several adjustable qualities. The example shows + how to: + + \list + \li Create an application with Q3DScatter and some widgets + \li Use QScatterDataProxy to set data to the graph + \li Adjust some graph properties using widget controls + \endlist + + For instructions about how to interact with the graph, see \l{Qt Data Visualization Interacting with Data}{this page}. + + \image scatter-example.png + + \section1 Creating the application + + First, in main.cpp, we create a QApplication, instantiate Q3DScatter, and a window container + for it: + + \snippet ../examples/scatter/main.cpp 0 + + The call to QWidget::createWindowContainer is required, as all data visualization types + (Q3DBars, Q3DScatter, and Q3DSurface) inherit QWindow. Any class inheriting QWindow cannot be used + as a widget any other way. + + Then we'll create horizontal and vertical layouts. We'll add the graph and the vertical + layout into the horizontal one: + + \snippet ../examples/scatter/main.cpp 1 + + We're not using the vertical layout for anything yet, but we'll get back to it in + \l {Using widgets to control the graph} + + Next, let's create another class to handle the data addition and other interaction with the + graph. Let's call it \c ScatterDataModifier (See \l {Setting up the graph} and + \l {Adding data to the graph} for details): + + \snippet ../examples/scatter/main.cpp 2 + + The application main is done. We can show the graph and start the event loop: + + \snippet ../examples/scatter/main.cpp 3 + + \section1 Setting up the graph + + Let's set up some visual qualities for the graph in the constructor of the \c ScatterDataModifier + class we instantiated in the application main: + + \snippet ../examples/scatter/scatterdatamodifier.cpp 0 + + None of these are required, but are used to override graph defaults. You can try how it looks + with the preset defaults by commenting the block above out. + + Finally we create a QScatterDataProxy and the associated QScatter3DSeries. We set custom label format + and mesh smoothing for the series and add it to the graph: + + \snippet ../examples/scatter/scatterdatamodifier.cpp 2 + + That concludes setting up the graph. + + \section1 Adding data to the graph + + The last thing we do in the \c ScatterDataModifier constructor is to add data to the graph: + + \snippet ../examples/scatter/scatterdatamodifier.cpp 3 + + The actual data addition is done in \c addData() method. First we configure the axes: + + \snippet ../examples/scatter/scatterdatamodifier.cpp 4 + + This could have been done in the constructor of \c {ScatterDataModifier}, but we added it here + to keep the constructor simpler and the axes configuration near the data. + + Next we create a data array: + + \snippet ../examples/scatter/scatterdatamodifier.cpp 5 + + and populate it: + + \snippet ../examples/scatter/scatterdatamodifier.cpp 6 + + Finally we tell the proxy to start using the data we gave it: + + \snippet ../examples/scatter/scatterdatamodifier.cpp 7 + + Now our graph has the data and is ready to be used. There isn't much interaction yet, though, + so let's continue by adding some widgets to play with. + + \section1 Using widgets to control the graph + + First, back in the application main, we'll create some widgets: + + \snippet ../examples/scatter/main.cpp 4 + + And add them to the vertical layout we created earlier: + + \snippet ../examples/scatter/main.cpp 5 + + Now, let's connect them to methods in ScatterDataModifier: + + \snippet ../examples/scatter/main.cpp 6 + + Here are the methods in ScatterDataModifier the signals were connected to: + + \snippet ../examples/scatter/scatterdatamodifier.cpp 8 + + And so we have an application in which we can control: + + \list + \li Label style + \li Camera preset + \li Background visibility + \li Grid visibility + \li Dot shading smoothness + \li Dot style + \li Theme + \li Shadow quality + \li Label font + \endlist + + \section1 Example contents +*/ diff --git a/examples/datavisualization/scatter/main.cpp b/examples/datavisualization/scatter/main.cpp new file mode 100644 index 00000000..d1dad77d --- /dev/null +++ b/examples/datavisualization/scatter/main.cpp @@ -0,0 +1,176 @@ +/**************************************************************************** +** +** 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 QtDataVisualization module. +** +** 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 +** +****************************************************************************/ + +#include "scatterdatamodifier.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +int main(int argc, char **argv) +{ + //! [0] + QApplication app(argc, argv); + Q3DScatter *graph = new Q3DScatter(); + QWidget *container = QWidget::createWindowContainer(graph); + //! [0] + + QSize screenSize = graph->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); + + //! [1] + QWidget *widget = new QWidget; + QHBoxLayout *hLayout = new QHBoxLayout(widget); + QVBoxLayout *vLayout = new QVBoxLayout(); + hLayout->addWidget(container, 1); + hLayout->addLayout(vLayout); + //! [1] + + widget->setWindowTitle(QStringLiteral("A Cosine Wave")); + + //! [4] + 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(6); + + QPushButton *labelButton = new QPushButton(widget); + labelButton->setText(QStringLiteral("Change label style")); + + QCheckBox *smoothCheckBox = new QCheckBox(widget); + smoothCheckBox->setText(QStringLiteral("Smooth dots")); + smoothCheckBox->setChecked(true); + + QComboBox *itemStyleList = new QComboBox(widget); + itemStyleList->addItem(QStringLiteral("Sphere"), int(QAbstract3DSeries::MeshSphere)); + itemStyleList->addItem(QStringLiteral("Cube"), int(QAbstract3DSeries::MeshCube)); + itemStyleList->addItem(QStringLiteral("Minimal"), int(QAbstract3DSeries::MeshMinimal)); + itemStyleList->addItem(QStringLiteral("Point"), int(QAbstract3DSeries::MeshPoint)); + itemStyleList->setCurrentIndex(0); + + QPushButton *cameraButton = new QPushButton(widget); + cameraButton->setText(QStringLiteral("Change camera preset")); + + QPushButton *itemCountButton = new QPushButton(widget); + itemCountButton->setText(QStringLiteral("Toggle item count")); + + QCheckBox *backgroundCheckBox = new QCheckBox(widget); + backgroundCheckBox->setText(QStringLiteral("Show background")); + backgroundCheckBox->setChecked(true); + + QCheckBox *gridCheckBox = new QCheckBox(widget); + gridCheckBox->setText(QStringLiteral("Show grid")); + gridCheckBox->setChecked(true); + + 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(4); + + QFontComboBox *fontList = new QFontComboBox(widget); + fontList->setCurrentFont(QFont("Arial")); + //! [4] + + //! [5] + vLayout->addWidget(labelButton, 0, Qt::AlignTop); + vLayout->addWidget(cameraButton, 0, Qt::AlignTop); + vLayout->addWidget(itemCountButton, 0, Qt::AlignTop); + vLayout->addWidget(backgroundCheckBox); + vLayout->addWidget(gridCheckBox); + vLayout->addWidget(smoothCheckBox, 0, Qt::AlignTop); + vLayout->addWidget(new QLabel(QStringLiteral("Change dot style"))); + vLayout->addWidget(itemStyleList); + 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, 1, Qt::AlignTop); + //! [5] + + //! [2] + ScatterDataModifier *modifier = new ScatterDataModifier(graph); + //! [2] + + //! [6] + QObject::connect(cameraButton, &QPushButton::clicked, modifier, + &ScatterDataModifier::changePresetCamera); + QObject::connect(labelButton, &QPushButton::clicked, modifier, + &ScatterDataModifier::changeLabelStyle); + QObject::connect(itemCountButton, &QPushButton::clicked, modifier, + &ScatterDataModifier::toggleItemCount); + + QObject::connect(backgroundCheckBox, &QCheckBox::stateChanged, modifier, + &ScatterDataModifier::setBackgroundEnabled); + QObject::connect(gridCheckBox, &QCheckBox::stateChanged, modifier, + &ScatterDataModifier::setGridEnabled); + QObject::connect(smoothCheckBox, &QCheckBox::stateChanged, modifier, + &ScatterDataModifier::setSmoothDots); + + QObject::connect(modifier, &ScatterDataModifier::backgroundEnabledChanged, + backgroundCheckBox, &QCheckBox::setChecked); + QObject::connect(modifier, &ScatterDataModifier::gridEnabledChanged, + gridCheckBox, &QCheckBox::setChecked); + QObject::connect(itemStyleList, SIGNAL(currentIndexChanged(int)), modifier, + SLOT(changeStyle(int))); + + QObject::connect(themeList, SIGNAL(currentIndexChanged(int)), modifier, + SLOT(changeTheme(int))); + + QObject::connect(shadowQuality, SIGNAL(currentIndexChanged(int)), modifier, + SLOT(changeShadowQuality(int))); + + QObject::connect(modifier, &ScatterDataModifier::shadowQualityChanged, shadowQuality, + &QComboBox::setCurrentIndex); + QObject::connect(graph, &Q3DScatter::shadowQualityChanged, modifier, + &ScatterDataModifier::shadowQualityUpdatedByVisual); + + QObject::connect(fontList, &QFontComboBox::currentFontChanged, modifier, + &ScatterDataModifier::changeFont); + + QObject::connect(modifier, &ScatterDataModifier::fontChanged, fontList, + &QFontComboBox::setCurrentFont); + //! [6] + + //! [3] + widget->show(); + return app.exec(); + //! [3] +} diff --git a/examples/datavisualization/scatter/scatter.pro b/examples/datavisualization/scatter/scatter.pro new file mode 100644 index 00000000..55baa808 --- /dev/null +++ b/examples/datavisualization/scatter/scatter.pro @@ -0,0 +1,8 @@ +!include( ../examples.pri ) { + error( "Couldn't find the examples.pri file!" ) +} + +SOURCES += main.cpp scatterdatamodifier.cpp +HEADERS += scatterdatamodifier.h + +QT += widgets diff --git a/examples/datavisualization/scatter/scatterdatamodifier.cpp b/examples/datavisualization/scatter/scatterdatamodifier.cpp new file mode 100644 index 00000000..fcba54be --- /dev/null +++ b/examples/datavisualization/scatter/scatterdatamodifier.cpp @@ -0,0 +1,203 @@ +/**************************************************************************** +** +** 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 QtDataVisualization module. +** +** 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 +** +****************************************************************************/ + +#include "scatterdatamodifier.h" +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace QtDataVisualization; + +//#define RANDOM_SCATTER // Uncomment this to switch to random scatter + +const int numberOfItems = 3600; +const float curveDivider = 3.0f; +const int lowerNumberOfItems = 900; +const float lowerCurveDivider = 0.75f; + +ScatterDataModifier::ScatterDataModifier(Q3DScatter *scatter) + : m_graph(scatter), + m_fontSize(40.0f), + m_style(QAbstract3DSeries::MeshSphere), + m_smooth(true), + m_itemCount(lowerNumberOfItems), + m_curveDivider(lowerCurveDivider) +{ + //! [0] + m_graph->activeTheme()->setType(Q3DTheme::ThemeEbony); + QFont font = m_graph->activeTheme()->font(); + font.setPointSize(m_fontSize); + m_graph->activeTheme()->setFont(font); + m_graph->setShadowQuality(QAbstract3DGraph::ShadowQualitySoftLow); + m_graph->scene()->activeCamera()->setCameraPreset(Q3DCamera::CameraPresetFront); + //! [0] + + //! [2] + QScatterDataProxy *proxy = new QScatterDataProxy; + QScatter3DSeries *series = new QScatter3DSeries(proxy); + series->setItemLabelFormat(QStringLiteral("@xTitle: @xLabel @yTitle: @yLabel @zTitle: @zLabel")); + series->setMeshSmooth(m_smooth); + m_graph->addSeries(series); + //! [2] + + //! [3] + addData(); + //! [3] +} + +ScatterDataModifier::~ScatterDataModifier() +{ + delete m_graph; +} + +void ScatterDataModifier::addData() +{ + // Configure the axes according to the data + //! [4] + m_graph->axisX()->setTitle("X"); + m_graph->axisY()->setTitle("Y"); + m_graph->axisZ()->setTitle("Z"); + //! [4] + + //! [5] + QScatterDataArray *dataArray = new QScatterDataArray; + dataArray->resize(m_itemCount); + QScatterDataItem *ptrToDataArray = &dataArray->first(); + //! [5] + +#ifdef RANDOM_SCATTER + for (int i = 0; i < m_itemCount; i++) { + ptrToDataArray->setPosition(randVector()); + ptrToDataArray++; + } +#else + //! [6] + float limit = qSqrt(m_itemCount) / 2.0f; + for (float i = -limit; i < limit; i++) { + for (float j = -limit; j < limit; j++) { + ptrToDataArray->setPosition(QVector3D(i + 0.5f, + qCos(qDegreesToRadians((i * j) / m_curveDivider)), + j + 0.5f)); + ptrToDataArray++; + } + } + //! [6] +#endif + + //! [7] + m_graph->seriesList().at(0)->dataProxy()->resetArray(dataArray); + //! [7] +} + +//! [8] +void ScatterDataModifier::changeStyle(int style) +{ + QComboBox *comboBox = qobject_cast(sender()); + if (comboBox) { + m_style = QAbstract3DSeries::Mesh(comboBox->itemData(style).toInt()); + if (m_graph->seriesList().size()) + m_graph->seriesList().at(0)->setMesh(m_style); + } +} + +void ScatterDataModifier::setSmoothDots(int smooth) +{ + m_smooth = bool(smooth); + QScatter3DSeries *series = m_graph->seriesList().at(0); + series->setMeshSmooth(m_smooth); +} + +void ScatterDataModifier::changeTheme(int theme) +{ + Q3DTheme *currentTheme = m_graph->activeTheme(); + currentTheme->setType(Q3DTheme::Theme(theme)); + emit backgroundEnabledChanged(currentTheme->isBackgroundEnabled()); + emit gridEnabledChanged(currentTheme->isGridEnabled()); + emit fontChanged(currentTheme->font()); +} + +void ScatterDataModifier::changePresetCamera() +{ + static int preset = Q3DCamera::CameraPresetFrontLow; + + m_graph->scene()->activeCamera()->setCameraPreset((Q3DCamera::CameraPreset)preset); + + if (++preset > Q3DCamera::CameraPresetDirectlyBelow) + preset = Q3DCamera::CameraPresetFrontLow; +} + +void ScatterDataModifier::changeLabelStyle() +{ + m_graph->activeTheme()->setLabelBackgroundEnabled(!m_graph->activeTheme()->isLabelBackgroundEnabled()); +} + +void ScatterDataModifier::changeFont(const QFont &font) +{ + QFont newFont = font; + newFont.setPointSizeF(m_fontSize); + m_graph->activeTheme()->setFont(newFont); +} + +void ScatterDataModifier::shadowQualityUpdatedByVisual(QAbstract3DGraph::ShadowQuality sq) +{ + int quality = int(sq); + emit shadowQualityChanged(quality); // connected to a checkbox in main.cpp +} + +void ScatterDataModifier::changeShadowQuality(int quality) +{ + QAbstract3DGraph::ShadowQuality sq = QAbstract3DGraph::ShadowQuality(quality); + m_graph->setShadowQuality(sq); +} + +void ScatterDataModifier::setBackgroundEnabled(int enabled) +{ + m_graph->activeTheme()->setBackgroundEnabled((bool)enabled); +} + +void ScatterDataModifier::setGridEnabled(int enabled) +{ + m_graph->activeTheme()->setGridEnabled((bool)enabled); +} +//! [8] + +void ScatterDataModifier::toggleItemCount() +{ + if (m_itemCount == numberOfItems) { + m_itemCount = lowerNumberOfItems; + m_curveDivider = lowerCurveDivider; + } else { + m_itemCount = numberOfItems; + m_curveDivider = curveDivider; + } + m_graph->seriesList().at(0)->dataProxy()->resetArray(0); + addData(); +} + +QVector3D ScatterDataModifier::randVector() +{ + return QVector3D( + (float)(rand() % 100) / 2.0f - (float)(rand() % 100) / 2.0f, + (float)(rand() % 100) / 100.0f - (float)(rand() % 100) / 100.0f, + (float)(rand() % 100) / 2.0f - (float)(rand() % 100) / 2.0f); +} diff --git a/examples/datavisualization/scatter/scatterdatamodifier.h b/examples/datavisualization/scatter/scatterdatamodifier.h new file mode 100644 index 00000000..7cfe7ff8 --- /dev/null +++ b/examples/datavisualization/scatter/scatterdatamodifier.h @@ -0,0 +1,69 @@ +/**************************************************************************** +** +** 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 QtDataVisualization module. +** +** 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 +** +****************************************************************************/ + +#ifndef SCATTERDATAMODIFIER_H +#define SCATTERDATAMODIFIER_H + +#include +#include +#include + +using namespace QtDataVisualization; + +class ScatterDataModifier : public QObject +{ + Q_OBJECT +public: + explicit ScatterDataModifier(Q3DScatter *scatter); + ~ScatterDataModifier(); + + void addData(); + void changeStyle(); + void changePresetCamera(); + void changeLabelStyle(); + void changeFont(const QFont &font); + void changeFontSize(int fontsize); + void setBackgroundEnabled(int enabled); + void setGridEnabled(int enabled); + void setSmoothDots(int smooth); + void toggleItemCount(); + void start(); + +public slots: + void changeStyle(int style); + void changeTheme(int theme); + void changeShadowQuality(int quality); + void shadowQualityUpdatedByVisual(QAbstract3DGraph::ShadowQuality shadowQuality); + +signals: + void backgroundEnabledChanged(bool enabled); + void gridEnabledChanged(bool enabled); + void shadowQualityChanged(int quality); + void fontChanged(QFont font); + +private: + QVector3D randVector(); + Q3DScatter *m_graph; + int m_fontSize; + QAbstract3DSeries::Mesh m_style; + bool m_smooth; + int m_itemCount; + float m_curveDivider; +}; + +#endif -- cgit v1.2.3