From a96f9f1a2688c215c478cfbee5748b4bb2043a43 Mon Sep 17 00:00:00 2001 From: Samuel Fadel Date: Mon, 18 May 2015 18:33:50 -0300 Subject: Updated UI. - Removed unnecessary UI elements from QML file; - Added the ColorScale class and implemented glyph color mapping from class labels; - Mark geometry nodes of individual glyphs as dirty when updating the scene graph. --- scatterplot.cpp | 45 +++++++++++++++++++++++---------------------- 1 file changed, 23 insertions(+), 22 deletions(-) (limited to 'scatterplot.cpp') diff --git a/scatterplot.cpp b/scatterplot.cpp index b5e8261..a3a0dfc 100644 --- a/scatterplot.cpp +++ b/scatterplot.cpp @@ -1,18 +1,22 @@ #include "scatterplot.h" +#include + #include -#include #include #include #include #include #include +#include -const int GLYPH_SIZE = 4; -const float PADDING = 5; +const int GLYPH_SIZE = 5; +const float PADDING = 10; const float PI = 3.1415f; -Scatterplot::Scatterplot() +Scatterplot::Scatterplot(QQuickItem *parent) + : QQuickItem(parent) + , m_colorScale{QColor("red"), QColor("green"), QColor("blue")} { setFlag(QQuickItem::ItemHasContents); } @@ -23,10 +27,11 @@ Scatterplot::~Scatterplot() void Scatterplot::setData(const arma::mat &data) { - if (data.n_cols != 2) + if (data.n_cols != 3) return; m_data = data; + m_colorScale.setExtents(m_data.col(2).min(), m_data.col(2).max()); update(); } @@ -68,10 +73,10 @@ void updateSquareGeometry(QSGGeometry *geometry, float size, float cx, float cy) QSGNode *Scatterplot::updatePaintNode(QSGNode *oldNode, UpdatePaintNodeData *) { + if (m_data.n_rows < 1) + return 0; + QSGNode *node = 0; - QSGGeometryNode *childNode = 0; - QSGGeometry *geometry = 0; - QSGFlatColorMaterial *material = 0; int vertexCount = calculateCircleVertexCount(GLYPH_SIZE / 2); qreal xmin = m_data.col(0).min(), xmax = m_data.col(0).max(), @@ -82,19 +87,15 @@ QSGNode *Scatterplot::updatePaintNode(QSGNode *oldNode, UpdatePaintNodeData *) if (!oldNode) { node = new QSGNode; for (arma::uword i = 0; i < m_data.n_rows; i++) { - arma::rowvec row = m_data.row(i); - x = (row[0] - xmin) / (xmax - xmin) * width(); - y = (row[1] - ymin) / (ymax - ymin) * height(); - - childNode = new QSGGeometryNode; + QSGGeometryNode *childNode = new QSGGeometryNode; - geometry = new QSGGeometry(QSGGeometry::defaultAttributes_Point2D(), vertexCount); + QSGGeometry *geometry = new QSGGeometry(QSGGeometry::defaultAttributes_Point2D(), vertexCount); geometry->setDrawingMode(GL_POLYGON); childNode->setGeometry(geometry); childNode->setFlag(QSGNode::OwnsGeometry); - material = new QSGFlatColorMaterial; - material->setColor(QColor()); + QSGFlatColorMaterial *material = new QSGFlatColorMaterial; + material->setColor(m_colorScale.color(m_data(i, 2))); childNode->setMaterial(material); childNode->setFlag(QSGNode::OwnsMaterial); @@ -104,17 +105,17 @@ QSGNode *Scatterplot::updatePaintNode(QSGNode *oldNode, UpdatePaintNodeData *) node = oldNode; } - childNode = static_cast(node->firstChild()); + QSGNode *childNode = node->firstChild(); for (arma::uword i = 0; i < m_data.n_rows; i++) { arma::rowvec row = m_data.row(i); - x = (row[0] - xmin) / (xmax - xmin) * width(); - y = (row[1] - ymin) / (ymax - ymin) * height(); + x = PADDING + (row[0] - xmin) / (xmax - xmin) * (width() - 2*PADDING); + y = PADDING + (row[1] - ymin) / (ymax - ymin) * (height() - 2*PADDING); - geometry = childNode->geometry(); + QSGGeometry *geometry = static_cast(childNode)->geometry(); updateCircleGeometry(geometry, GLYPH_SIZE, x, y); - childNode = static_cast(childNode->nextSibling()); + childNode->markDirty(QSGNode::DirtyGeometry); + childNode = childNode->nextSibling(); } - node->markDirty(QSGNode::DirtyGeometry); return node; } -- cgit v1.2.3