VAPOR3 3.9.4
TFOpacityWidget.h
Go to the documentation of this file.
1#pragma once
2
3#include "TFMapWidget.h"
4#include <vapor/VAssert.h>
5#include <glm/glm.hpp>
6
8public:
10 ControlPointList *list;
11 int i;
12
13 PointIterator(ControlPointList *list, int i) : list(list), i(i) {}
14
15 public:
18 {
19 ++i;
20 return *this;
21 }
23 {
24 --i;
25 return *this;
26 }
27 PointIterator operator+(int x) const { return PointIterator(list, i + x); }
28 PointIterator operator-(int x) const { return PointIterator(list, i - x); }
29 glm::vec2 & operator*() { return (*list)[i]; }
30 bool operator!=(const PointIterator &other) { return !(*this == other); }
31 bool operator==(const PointIterator &other) { return other.list == this->list && other.i == this->i; }
32
33 bool IsFirst() const { return i == 0; }
34 bool IsLast() const { return i == list->Size() - 1; }
35 int Index() const { return i; }
36
37 friend class ControlPointList;
38 };
39
40 class LineIterator : public PointIterator {
42
43 public:
44 glm::vec2 &operator*() = delete;
45 // bool IsFirst() const = delete;
46 // bool IsLast() const = delete;
47 bool IsLast() const { return i == list->Size(); }
48 LineIterator operator+(int x) const { return LineIterator(list, i + x); }
49 LineIterator operator-(int x) const { return LineIterator(list, i - x); }
50
51 const glm::vec2 a()
52 {
53 if (i == 0) return glm::vec2(0, (*list)[0].y);
54 return (*list)[i - 1];
55 }
56 const glm::vec2 b()
57 {
58 if (i == list->SizeLines() - 1) return glm::vec2(1, (*list)[list->Size() - 1].y);
59 return (*list)[i];
60 }
61 void setA(const glm::vec2 &v)
62 {
63 if (i != 0) (*list)[i - 1] = v;
64 }
65 void setB(const glm::vec2 &v)
66 {
67 if (i != list->SizeLines() - 1) (*list)[i] = v;
68 }
69
70 friend class ControlPointList;
71 };
72
73 std::vector<glm::vec2> _points;
74
75public:
76 glm::vec2 &operator[](const int i)
77 {
78 VAssert(i >= 0 && i < _points.size());
79 return _points[i];
80 }
81
82 int Add(const glm::vec2 &v)
83 {
84 for (int i = 0; i < _points.size(); i++)
85 if (_points[i].x > v.x) return Add(v, i);
86
87 return Add(v, Size());
88 }
89
90 int Add(const glm::vec2 &v, const int i)
91 {
92 VAssert(i >= 0 && i <= _points.size());
93 glm::vec2 vClamped = glm::clamp(v, glm::vec2(0, 0), glm::vec2(1, 1));
94 _points.insert(_points.begin() + i, vClamped);
95 return i;
96 }
97
98 int Add(const glm::vec2 &v, const LineIterator &line)
99 {
100 VAssert(line.i >= 0 && line.i <= _points.size());
101 _points.insert(_points.begin() + line.i, v);
102 return line.i;
103 }
104
105 void Remove(const PointIterator &point)
106 {
107 VAssert(point.i >= 0 && point.i < _points.size());
108 if (Size() > 1) _points.erase(_points.begin() + point.i);
109 }
110
111 int Size() const { return _points.size(); }
112 int SizeLines() const { return Size() + 1; }
113 void Resize(int n) { _points.resize(n); }
114
117 LineIterator BeginLines() { return LineIterator(this, 0); }
119};
120
121class TFOpacityMap : public TFMap {
122 Q_OBJECT
123
124public:
125 TFOpacityMap(const std::string &variableNameTag, TFMapWidget *parent = nullptr);
126
127 QSize minimumSizeHint() const override;
128 void LostFocus() override;
129 void PopulateContextMenu(QMenu *menu, const glm::vec2 &p) override;
130 void PopulateSettingsMenu(QMenu *menu) const override;
131
132protected:
133 void paramsUpdate() override;
135 void paintEvent(QPainter &p) override;
136 void mousePressEvent(QMouseEvent *event) override;
137 void mouseReleaseEvent(QMouseEvent *event) override;
138 void mouseMoveEvent(QMouseEvent *event) override;
139 void mouseDoubleClickEvent(QMouseEvent *event) override;
140
141private:
142 ControlPointList _controlPoints;
143 bool _isDraggingControl = false;
144 bool _isDraggingLine = false;
145 ControlPointList::PointIterator _draggedControl;
147 glm::vec2 _dragOffset;
148 glm::vec2 _dragOffsetB;
149 glm::vec2 m;
150 int _selectedControl = -1;
151
152 void opacityChanged();
153
154 bool controlPointContainsPixel(const glm::vec2 &cp, const glm::vec2 &pixel) const;
155 ControlPointList::PointIterator findSelectedControlPoint(const glm::vec2 &mouse);
156 ControlPointList::LineIterator findSelectedControlLine(const glm::vec2 &mouse);
157
158 void selectControlPoint(ControlPointList::PointIterator it);
159 void deleteControlPoint(ControlPointList::PointIterator it);
160 void addControlPoint(const glm::vec2 &ndc);
161
162signals:
164 void UpdateInfo(float value, float opacity);
165
166public slots:
168 void UpdateFromInfo(float value, float opacity);
169
170private slots:
171 void menuDeleteSelectedControlPoint();
172 void menuAddControlPoint();
173 void menuLoad();
174 void menuSave();
175};
176
178 Q_OBJECT
179public:
180 TFOpacityWidget(const std::string &variableNameTag);
181};
#define VAssert(expr)
Definition: VAssert.h:9
glm::vec2 & operator*()=delete
LineIterator operator-(int x) const
void setB(const glm::vec2 &v)
void setA(const glm::vec2 &v)
LineIterator operator+(int x) const
bool operator==(const PointIterator &other)
bool operator!=(const PointIterator &other)
PointIterator operator-(int x) const
PointIterator operator+(int x) const
std::vector< glm::vec2 > _points
PointIterator EndPoints()
int Add(const glm::vec2 &v, const LineIterator &line)
PointIterator BeginPoints()
void Resize(int n)
glm::vec2 & operator[](const int i)
LineIterator BeginLines()
int SizeLines() const
int Add(const glm::vec2 &v)
int Add(const glm::vec2 &v, const int i)
void Remove(const PointIterator &point)
LineIterator EndLines()
void LostFocus() override
void paintEvent(QPainter &p) override
These map to the QWidget counterparts.
void mouseMoveEvent(QMouseEvent *event) override
TFOpacityMap(const std::string &variableNameTag, TFMapWidget *parent=nullptr)
void UpdateInfo(float value, float opacity)
void paramsUpdate() override
void mouseDoubleClickEvent(QMouseEvent *event) override
QSize minimumSizeHint() const override
void mouseReleaseEvent(QMouseEvent *event) override
TFInfoWidget * createInfoWidget() override
void UpdateFromInfo(float value, float opacity)
void ControlPointDeselected()
void PopulateContextMenu(QMenu *menu, const glm::vec2 &p) override
(Right-click menu)
void mousePressEvent(QMouseEvent *event) override
void DeselectControlPoint()
void PopulateSettingsMenu(QMenu *menu) const override
TFOpacityWidget(const std::string &variableNameTag)