aboutsummaryrefslogtreecommitdiff
path: root/colormap.cpp
diff options
context:
space:
mode:
authorSamuel Fadel <samuelfadel@gmail.com>2016-02-28 21:46:31 -0300
committerSamuel Fadel <samuelfadel@gmail.com>2016-02-28 21:46:31 -0300
commitd3d3526bab2ab051656a1b80b4e0f1337037b5de (patch)
tree1df83705676cfac7cc8f755d8025d05b1b1e43cd /colormap.cpp
parent5bd1fec462466d605f974fa27c3b00826a1bab57 (diff)
Added colormap orientation & improved UI.
* Colormap: orientation now enables vertical & horizontal colormap display * Two colormap components, one for each type of point (regular, control) * Improved controls grouping and cohesion
Diffstat (limited to 'colormap.cpp')
-rw-r--r--colormap.cpp77
1 files changed, 70 insertions, 7 deletions
diff --git a/colormap.cpp b/colormap.cpp
index cc57048..3ebf5c8 100644
--- a/colormap.cpp
+++ b/colormap.cpp
@@ -1,5 +1,7 @@
#include "colormap.h"
+#include <algorithm>
+
#include <QOpenGLFunctions>
#include <QSGSimpleTextureNode>
@@ -7,7 +9,8 @@ class ColormapTexture
: public QSGDynamicTexture
{
public:
- ColormapTexture(const std::vector<float> *cmap);
+ ColormapTexture(const std::vector<float> *cmap,
+ Colormap::Orientation orientation = Colormap::Horizontal);
~ColormapTexture();
bool hasAlphaChannel() const { return false; }
@@ -18,26 +21,29 @@ public:
int textureId() const { return m_texture; }
QSize textureSize() const { return m_size; }
+ void setOrientation(Colormap::Orientation orientation);
+
private:
QOpenGLFunctions gl;
+ Colormap::Orientation m_orientation;
QSize m_size;
GLuint m_texture;
const std::vector<float> *m_cmap;
};
-ColormapTexture::ColormapTexture(const std::vector<float> *cmap)
+ColormapTexture::ColormapTexture(const std::vector<float> *cmap,
+ Colormap::Orientation orientation)
: gl(QOpenGLContext::currentContext())
- , m_size(cmap->size() / 3, 1)
, m_cmap(cmap)
{
// Setup OpenGL texture
gl.glGenTextures(1, &m_texture);
gl.glBindTexture(GL_TEXTURE_2D, m_texture);
- gl.glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, m_size.width(), m_size.height(),
- 0, GL_RGB, GL_FLOAT, 0);
gl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
gl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
+
+ setOrientation(orientation);
}
ColormapTexture::~ColormapTexture()
@@ -50,9 +56,28 @@ void ColormapTexture::bind()
gl.glBindTexture(GL_TEXTURE_2D, m_texture);
}
+void ColormapTexture::setOrientation(Colormap::Orientation orientation)
+{
+ if (m_orientation == orientation) {
+ return;
+ }
+
+ m_orientation = orientation;
+ updateTexture();
+}
+
bool ColormapTexture::updateTexture()
{
- m_size.setWidth(m_cmap->size() / 3);
+ switch (m_orientation) {
+ case Colormap::Horizontal:
+ m_size.setWidth(m_cmap->size() / 3);
+ m_size.setHeight(1);
+ break;
+ case Colormap::Vertical:
+ m_size.setWidth(1);
+ m_size.setHeight(m_cmap->size() / 3);
+ break;
+ }
gl.glBindTexture(GL_TEXTURE_2D, m_texture);
gl.glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, m_size.width(), m_size.height(),
@@ -64,6 +89,7 @@ Colormap::Colormap(QQuickItem *parent)
: QQuickItem(parent)
, m_texture(0)
, m_shouldUpdateTexture(false)
+ , m_orientation(Colormap::Horizontal)
{
setFlag(QQuickItem::ItemHasContents);
}
@@ -75,11 +101,42 @@ Colormap::~Colormap()
}
}
+static void reverseCMap(std::vector<float> &cmap)
+{
+ decltype(cmap.size()) i = 0, j = cmap.size() - 3;
+
+ while (i < j) {
+ std::swap(cmap[i++], cmap[j++]);
+ std::swap(cmap[i++], cmap[j++]);
+ std::swap(cmap[i++], cmap[j++]);
+
+ j -= 6;
+ }
+}
+
+void Colormap::setOrientation(Colormap::Orientation orientation)
+{
+ if (m_orientation == orientation) {
+ return;
+ }
+
+ if (!m_cmap.empty()) {
+ reverseCMap(m_cmap);
+ }
+
+ m_orientation = orientation;
+ m_shouldUpdateOrientation = true;
+ update();
+}
void Colormap::setColorScale(const ColorScale &scale)
{
m_cmap.resize(scale.numColors() * 3);
scale.sample(scale.numColors(), m_cmap.data());
+ if (m_orientation == Colormap::Vertical) {
+ reverseCMap(m_cmap);
+ }
+
emit colorScaleChanged(scale);
m_shouldUpdateTexture = true;
@@ -107,8 +164,14 @@ QSGNode *Colormap::updatePaintNode(QSGNode *oldNode, UpdatePaintNodeData *)
QSGSimpleTextureNode *node = static_cast<QSGSimpleTextureNode *>(root);
node->setRect(x(), y(), width(), height());
+ ColormapTexture *texture = static_cast<ColormapTexture *>(m_texture);
+ if (m_shouldUpdateOrientation) {
+ texture->setOrientation(m_orientation);
+ m_shouldUpdateOrientation = false;
+ }
+
if (m_shouldUpdateTexture) {
- m_texture->updateTexture();
+ texture->updateTexture();
m_shouldUpdateTexture = false;
}