From af85378b6361d3252d00c5eaea679d031759a2d2 Mon Sep 17 00:00:00 2001 From: Mika Salmela Date: Fri, 4 Oct 2013 11:59:36 +0300 Subject: Basic example of surface Changes in latest patch: - Sqrt&Sin model on top and default - Digia style default Task-number: QTRD-2347 Change-Id: Id405867f0f1ba243b0032dda670f609810f5837c Reviewed-by: Miikka Heikkinen --- examples/examples.pro | 3 +- examples/surface/Heightmap.png | Bin 0 -> 71764 bytes examples/surface/main.cpp | 208 +++++++++++++++++++++++++++++++++++ examples/surface/surface.pro | 14 +++ examples/surface/surface.qrc | 5 + examples/surface/surfacegraph.cpp | 225 ++++++++++++++++++++++++++++++++++++++ examples/surface/surfacegraph.h | 81 ++++++++++++++ 7 files changed, 535 insertions(+), 1 deletion(-) create mode 100644 examples/surface/Heightmap.png create mode 100644 examples/surface/main.cpp create mode 100644 examples/surface/surface.pro create mode 100644 examples/surface/surface.qrc create mode 100644 examples/surface/surfacegraph.cpp create mode 100644 examples/surface/surfacegraph.h diff --git a/examples/examples.pro b/examples/examples.pro index a76b95a7..e5001049 100644 --- a/examples/examples.pro +++ b/examples/examples.pro @@ -6,7 +6,8 @@ SUBDIRS += qmlbars \ SUBDIRS += bars \ rainfall \ widget \ - scatter + scatter \ + surface } qtHaveModule(multimedia):!android: SUBDIRS += audiolevels diff --git a/examples/surface/Heightmap.png b/examples/surface/Heightmap.png new file mode 100644 index 00000000..2a860111 Binary files /dev/null and b/examples/surface/Heightmap.png differ diff --git a/examples/surface/main.cpp b/examples/surface/main.cpp new file mode 100644 index 00000000..2583b21a --- /dev/null +++ b/examples/surface/main.cpp @@ -0,0 +1,208 @@ +/**************************************************************************** +** +** Copyright (C) 2013 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 "surfacegraph.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +int main(int argc, char **argv) +{ + QApplication app(argc, argv); + + QWidget *widget = new QWidget; + QHBoxLayout *hLayout = new QHBoxLayout(widget); + QVBoxLayout *vLayout = new QVBoxLayout(); + vLayout->setAlignment(Qt::AlignTop); + + Q3DSurface *graph = new Q3DSurface(); + QSize screenSize = graph->screen()->size(); + + QWidget *container = QWidget::createWindowContainer(graph); + container->setMinimumSize(QSize(screenSize.width() / 2, screenSize.height() / 1.6)); + container->setMaximumSize(screenSize); + container->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); + container->setFocusPolicy(Qt::StrongFocus); + + widget->setWindowTitle(QStringLiteral("Example of using axis range and selection")); + + hLayout->addWidget(container, 1); + hLayout->addLayout(vLayout); + + QGroupBox *modelGroupBox = new QGroupBox(QStringLiteral("Model")); + + QRadioButton *sqrtSinModelRB = new QRadioButton(widget); + sqrtSinModelRB->setText(QStringLiteral("Sqrt Sin")); + sqrtSinModelRB->setChecked(false); + + QRadioButton *heightMapModelRB = new QRadioButton(widget); + heightMapModelRB->setText(QStringLiteral("Height Map")); + heightMapModelRB->setChecked(false); + + QVBoxLayout *modelVBox = new QVBoxLayout; + modelVBox->addWidget(sqrtSinModelRB); + modelVBox->addWidget(heightMapModelRB); + modelGroupBox->setLayout(modelVBox); + + QGroupBox *selectionGroupBox = new QGroupBox(QStringLiteral("Selection Mode")); + + QRadioButton *modeNoneRB = new QRadioButton(widget); + modeNoneRB->setText(QStringLiteral("No selection")); + modeNoneRB->setChecked(false); + + QRadioButton *modeItemRB = new QRadioButton(widget); + modeItemRB->setText(QStringLiteral("Item")); + modeItemRB->setChecked(false); + + QRadioButton *modeSliceRowRB = new QRadioButton(widget); + modeSliceRowRB->setText(QStringLiteral("Row Slice")); + modeSliceRowRB->setChecked(false); + + QRadioButton *modeSliceColumnRB = new QRadioButton(widget); + modeSliceColumnRB->setText(QStringLiteral("Column Slice")); + modeSliceColumnRB->setChecked(false); + + QVBoxLayout *selectionVBox = new QVBoxLayout; + selectionVBox->addWidget(modeNoneRB); + selectionVBox->addWidget(modeItemRB); + selectionVBox->addWidget(modeSliceRowRB); + selectionVBox->addWidget(modeSliceColumnRB); + selectionGroupBox->setLayout(selectionVBox); + + QSlider *axisMinSliderX = new QSlider(Qt::Horizontal, widget); + axisMinSliderX->setMinimum(0); + axisMinSliderX->setTickInterval(1); + axisMinSliderX->setEnabled(true); + QSlider *axisMaxSliderX = new QSlider(Qt::Horizontal, widget); + axisMaxSliderX->setMinimum(2); + axisMaxSliderX->setTickInterval(1); + axisMaxSliderX->setEnabled(true); + QSlider *axisMinSliderZ = new QSlider(Qt::Horizontal, widget); + axisMinSliderZ->setMinimum(0); + axisMinSliderZ->setTickInterval(1); + axisMinSliderZ->setEnabled(true); + QSlider *axisMaxSliderZ = new QSlider(Qt::Horizontal, widget); + axisMaxSliderZ->setMinimum(2); + axisMaxSliderZ->setTickInterval(1); + axisMaxSliderZ->setEnabled(true); + + 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")); + + QGroupBox *colorGroupBox = new QGroupBox(QStringLiteral("Custom gradient")); + + QLinearGradient grBtoY(0, 0, 1, 100); + grBtoY.setColorAt(1.0, Qt::black); + grBtoY.setColorAt(0.67, Qt::blue); + grBtoY.setColorAt(0.33, Qt::red); + grBtoY.setColorAt(0.0, Qt::yellow); + QPixmap pm(24, 100); + QPainter pmp(&pm); + pmp.setBrush(QBrush(grBtoY)); + pmp.setPen(Qt::NoPen); + pmp.drawRect(0, 0, 24, 100); + QPushButton *gradientBtoYPB = new QPushButton(widget); + gradientBtoYPB->setIcon(QIcon(pm)); + gradientBtoYPB->setIconSize(QSize(24, 100)); + + QLinearGradient grGtoR(0, 0, 1, 100); + grGtoR.setColorAt(1.0, Qt::darkGreen); + grGtoR.setColorAt(0.5, Qt::yellow); + grGtoR.setColorAt(0.2, Qt::red); + grGtoR.setColorAt(0.0, Qt::darkRed); + pmp.setBrush(QBrush(grGtoR)); + pmp.drawRect(0, 0, 24, 100); + QPushButton *gradientGtoRPB = new QPushButton(widget); + gradientGtoRPB->setIcon(QIcon(pm)); + gradientGtoRPB->setIconSize(QSize(24, 100)); + + QHBoxLayout *colorHBox = new QHBoxLayout; + colorHBox->addWidget(gradientBtoYPB); + colorHBox->addWidget(gradientGtoRPB); + colorGroupBox->setLayout(colorHBox); + + vLayout->addWidget(modelGroupBox); + vLayout->addWidget(selectionGroupBox); + vLayout->addWidget(new QLabel(QStringLiteral("Column range"))); + vLayout->addWidget(axisMinSliderX); + vLayout->addWidget(axisMaxSliderX); + vLayout->addWidget(new QLabel(QStringLiteral("Row range"))); + vLayout->addWidget(axisMinSliderZ); + vLayout->addWidget(axisMaxSliderZ); + vLayout->addWidget(new QLabel(QStringLiteral("Theme"))); + vLayout->addWidget(themeList); + vLayout->addWidget(colorGroupBox); + + widget->show(); + + SurfaceGraph *modifier = new SurfaceGraph(graph); + + QObject::connect(heightMapModelRB, &QRadioButton::toggled, + modifier, &SurfaceGraph::enableHeightMapModel); + QObject::connect(sqrtSinModelRB, &QRadioButton::toggled, + modifier, &SurfaceGraph::enableSqrtSinModel); + QObject::connect(modeNoneRB, &QRadioButton::toggled, + modifier, &SurfaceGraph::toggleModeNone); + QObject::connect(modeItemRB, &QRadioButton::toggled, + modifier, &SurfaceGraph::toggleModeItem); + QObject::connect(modeSliceRowRB, &QRadioButton::toggled, + modifier, &SurfaceGraph::toggleModeSliceRow); + QObject::connect(modeSliceColumnRB, &QRadioButton::toggled, + modifier, &SurfaceGraph::toggleModeSliceColumn); + QObject::connect(axisMinSliderX, &QSlider::valueChanged, + modifier, &SurfaceGraph::adjustXMin); + QObject::connect(axisMaxSliderX, &QSlider::valueChanged, + modifier, &SurfaceGraph::adjustXMax); + QObject::connect(axisMinSliderZ, &QSlider::valueChanged, + modifier, &SurfaceGraph::adjustZMin); + QObject::connect(axisMaxSliderZ, &QSlider::valueChanged, + modifier, &SurfaceGraph::adjustZMax); + QObject::connect(themeList, SIGNAL(currentIndexChanged(int)), + modifier, SLOT(changeTheme(int))); + QObject::connect(gradientBtoYPB, &QPushButton::pressed, + modifier, &SurfaceGraph::setBlackToYellowGradient); + QObject::connect(gradientGtoRPB, &QPushButton::pressed, + modifier, &SurfaceGraph::setGreenToRedGradient); + + modifier->setAxisMinSliderX(axisMinSliderX); + modifier->setAxisMaxSliderX(axisMaxSliderX); + modifier->setAxisMinSliderZ(axisMinSliderZ); + modifier->setAxisMaxSliderZ(axisMaxSliderZ); + + sqrtSinModelRB->setChecked(true); + modeItemRB->setChecked(true); + themeList->setCurrentIndex(2); + + return app.exec(); +} diff --git a/examples/surface/surface.pro b/examples/surface/surface.pro new file mode 100644 index 00000000..54302d6b --- /dev/null +++ b/examples/surface/surface.pro @@ -0,0 +1,14 @@ +!include( ../examples.pri ) { + error( "Couldn't find the examples.pri file!" ) +} + +SOURCES += main.cpp \ + surfacegraph.cpp + +HEADERS += surfacegraph.h + +QT += widgets + +INSTALLS += target + +RESOURCES += surface.qrc diff --git a/examples/surface/surface.qrc b/examples/surface/surface.qrc new file mode 100644 index 00000000..c18da2c4 --- /dev/null +++ b/examples/surface/surface.qrc @@ -0,0 +1,5 @@ + + + Heightmap.png + + diff --git a/examples/surface/surfacegraph.cpp b/examples/surface/surfacegraph.cpp new file mode 100644 index 00000000..251f4643 --- /dev/null +++ b/examples/surface/surfacegraph.cpp @@ -0,0 +1,225 @@ +/**************************************************************************** +** +** Copyright (C) 2013 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 "surfacegraph.h" + +#include +#include +#include + +using namespace QtDataVisualization; + +const int sampleCountX = 50; +const int sampleCountZ = 50; +const int heightMapGridStepX = 6; +const int heightMapGridStepZ = 6; + +SurfaceGraph::SurfaceGraph(Q3DSurface *surface) + : m_graph(surface) +{ + m_graph->setAxisX(new Q3DValueAxis); + m_graph->setAxisY(new Q3DValueAxis); + m_graph->setAxisZ(new Q3DValueAxis); + m_graph->setLabelStyle(QDataVis::LabelStyleFromTheme); + + QImage heightMapImage(":/maps/map"); + m_heightMapProxy = new QHeightMapSurfaceDataProxy(heightMapImage); + m_heightMapProxy->setValueRanges(34.0, 40.0, 18.0, 24.0); + m_heightMapWidth = heightMapImage.width(); + m_heightMapHeight = heightMapImage.height(); + + sqrtSinProxy = new QSurfaceDataProxy(); + fillSqrtSinProxy(); +} + +SurfaceGraph::~SurfaceGraph() +{ + delete m_graph; +} + +void SurfaceGraph::fillSqrtSinProxy() +{ + qreal stepX = 16.0 / qreal(sampleCountX); + qreal stepZ = 16.0 / qreal(sampleCountZ); + + QSurfaceDataArray *dataArray = new QSurfaceDataArray; + dataArray->reserve(sampleCountZ); + for (qreal i = -8.0 + stepZ / 2.0 ; i < 8.0 ; i += stepZ) { + QSurfaceDataRow *newRow = new QSurfaceDataRow(sampleCountX); + int index = 0; + for (qreal j = -8.0 + stepX / 2.0; j < 8.0; j += stepX) { + qreal R = qSqrt(i * i + j * j) + 0.01; + qreal y = (qSin(R) / R + 0.24) * 1.61; + (*newRow)[index++].setPosition(QVector3D(j, y, i)); + } + *dataArray << newRow; + } + + sqrtSinProxy->resetArray(dataArray); +} + +void SurfaceGraph::enableHeightMapModel() +{ + m_graph->setSurfaceGridEnabled(false); + m_graph->setSmoothSurfaceEnabled(true); + + m_graph->axisX()->setLabelFormat("%.1f N"); + m_graph->axisZ()->setLabelFormat("%.1f E"); + m_graph->axisX()->setRange(34.0, 40.0); + m_graph->axisY()->setAutoAdjustRange(true); + m_graph->axisZ()->setRange(18.0, 24.0); + + m_graph->setActiveDataProxy(m_heightMapProxy); + + // Reset range sliders for height map + int mapGridCountX = m_heightMapWidth / heightMapGridStepX; + int mapGridCountZ = m_heightMapHeight / heightMapGridStepZ; + m_rangeMinX = 34.0; + m_rangeMinZ = 18.0; + m_stepX = 6.0 / qreal(mapGridCountX - 1); + m_stepZ = 6.0 / qreal(mapGridCountZ - 1); + m_axisMinSliderX->setMaximum(mapGridCountX - 3); + m_axisMinSliderX->setValue(0); + m_axisMaxSliderX->setMaximum(mapGridCountX - 1); + m_axisMaxSliderX->setValue(mapGridCountX - 1); + m_axisMinSliderZ->setMaximum(mapGridCountZ - 3); + m_axisMinSliderZ->setValue(0); + m_axisMaxSliderZ->setMaximum(mapGridCountZ - 1); + m_axisMaxSliderZ->setValue(mapGridCountZ - 1); +} + +void SurfaceGraph::enableSqrtSinModel() +{ + m_graph->setSurfaceGridEnabled(true); + m_graph->setSmoothSurfaceEnabled(false); + + m_graph->axisX()->setLabelFormat("%.2f"); + m_graph->axisZ()->setLabelFormat("%.2f"); + m_graph->axisX()->setRange(-8.0, 8.0); + m_graph->axisY()->setRange(0.0, 2.0); + m_graph->axisZ()->setRange(-8.0, 8.0); + + m_graph->setActiveDataProxy(sqrtSinProxy); + + // Reset range sliders for Sqrt&Sin + m_rangeMinX = -8.0; + m_rangeMinZ = -8.0; + m_stepX = 16.0 / qreal(sampleCountX - 1); + m_stepZ = 16.0 / qreal(sampleCountZ - 1); + m_axisMinSliderX->setMaximum(sampleCountX - 3); + m_axisMinSliderX->setValue(0); + m_axisMaxSliderX->setMaximum(sampleCountX - 1); + m_axisMaxSliderX->setValue(sampleCountX - 1); + m_axisMinSliderZ->setMaximum(sampleCountZ - 3); + m_axisMinSliderZ->setValue(0); + m_axisMaxSliderZ->setMaximum(sampleCountZ - 1); + m_axisMaxSliderZ->setValue(sampleCountZ - 1); +} + +void SurfaceGraph::adjustXMin(int min) +{ + qreal minX = m_stepX * qreal(min) + m_rangeMinX; + + int max = m_axisMaxSliderX->value(); + if (min >= (max - 1)) { + max = min + 2; + m_axisMaxSliderX->setValue(max); + } + qreal maxX = m_stepX * max + m_rangeMinX; + + setAxisXRange(minX, maxX); +} + +void SurfaceGraph::adjustXMax(int max) +{ + qreal maxX = m_stepX * qreal(max) + m_rangeMinX; + + int min = m_axisMinSliderX->value(); + if (max <= (min + 1)) { + min = max - 2; + m_axisMinSliderX->setValue(min); + } + qreal minX = m_stepX * min + m_rangeMinX; + + setAxisXRange(minX, maxX); +} + +void SurfaceGraph::adjustZMin(int min) +{ + qreal minZ = m_stepZ * qreal(min) + m_rangeMinZ; + + int max = m_axisMaxSliderZ->value(); + if (min >= (max - 1)) { + max = min + 2; + m_axisMaxSliderZ->setValue(max); + } + qreal maxZ = m_stepZ * max + m_rangeMinZ; + + setAxisZRange(minZ, maxZ); +} + +void SurfaceGraph::adjustZMax(int max) +{ + qreal maxX = m_stepZ * qreal(max) + m_rangeMinZ; + + int min = m_axisMinSliderZ->value(); + if (max <= (min + 1)) { + min = max - 2; + m_axisMinSliderZ->setValue(min); + } + qreal minX = m_stepZ * min + m_rangeMinZ; + + setAxisZRange(minX, maxX); +} + +void SurfaceGraph::setAxisXRange(qreal min, qreal max) +{ + m_graph->axisX()->setRange(min, max); +} + +void SurfaceGraph::setAxisZRange(qreal min, qreal max) +{ + m_graph->axisZ()->setRange(min, max); +} + +void SurfaceGraph::setBlackToYellowGradient() +{ + QLinearGradient gr; + gr.setColorAt(0.0, Qt::black); + gr.setColorAt(0.33, Qt::blue); + gr.setColorAt(0.67, Qt::red); + gr.setColorAt(1.0, Qt::yellow); + + m_graph->setGradient(gr); +} + +void SurfaceGraph::setGreenToRedGradient() +{ + QLinearGradient gr; + gr.setColorAt(0.0, Qt::darkGreen); + gr.setColorAt(0.5, Qt::yellow); + gr.setColorAt(0.8, Qt::red); + gr.setColorAt(1.0, Qt::darkRed); + + m_graph->setGradient(gr); +} + +void SurfaceGraph::changeTheme(int theme) +{ + m_graph->setTheme((QDataVis::Theme)theme); +} diff --git a/examples/surface/surfacegraph.h b/examples/surface/surfacegraph.h new file mode 100644 index 00000000..8da84e38 --- /dev/null +++ b/examples/surface/surfacegraph.h @@ -0,0 +1,81 @@ +/**************************************************************************** +** +** Copyright (C) 2013 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 SURFACEGRAPH_H +#define SURFACEGRAPH_H + +#include +#include +#include +#include + +using namespace QtDataVisualization; + +class SurfaceGraph : public QObject +{ + Q_OBJECT +public: + explicit SurfaceGraph(Q3DSurface *surface); + ~SurfaceGraph(); + + void enableHeightMapModel(); + void enableSqrtSinModel(); + + void toggleModeNone() { m_graph->setSelectionMode(QDataVis::SelectionModeNone); } + void toggleModeItem() { m_graph->setSelectionMode(QDataVis::SelectionModeItem); } + void toggleModeSliceRow() { m_graph->setSelectionMode(QDataVis::SelectionModeSliceRow); } + void toggleModeSliceColumn() { m_graph->setSelectionMode(QDataVis::SelectionModeSliceColumn); } + + void setBlackToYellowGradient(); + void setGreenToRedGradient(); + + void setAxisMinSliderX(QSlider *slider) { m_axisMinSliderX = slider; } + void setAxisMaxSliderX(QSlider *slider) { m_axisMaxSliderX = slider; } + void setAxisMinSliderZ(QSlider *slider) { m_axisMinSliderZ = slider; } + void setAxisMaxSliderZ(QSlider *slider) { m_axisMaxSliderZ = slider; } + + void adjustXMin(int min); + void adjustXMax(int max); + void adjustZMin(int min); + void adjustZMax(int max); + +public slots: + void changeTheme(int theme); + +private: + Q3DSurface *m_graph; + QHeightMapSurfaceDataProxy *m_heightMapProxy; + QSurfaceDataProxy *sqrtSinProxy; + + QSlider *m_axisMinSliderX; + QSlider *m_axisMaxSliderX; + QSlider *m_axisMinSliderZ; + QSlider *m_axisMaxSliderZ; + qreal m_rangeMinX; + qreal m_rangeMinZ; + qreal m_stepX; + qreal m_stepZ; + int m_heightMapWidth; + int m_heightMapHeight; + + void setAxisXRange(qreal min, qreal max); + void setAxisZRange(qreal min, qreal max); + void fillSqrtSinProxy(); +}; + +#endif // SURFACEGRAPH_H -- cgit v1.2.3