-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparamview.cpp
More file actions
212 lines (152 loc) · 4.77 KB
/
paramview.cpp
File metadata and controls
212 lines (152 loc) · 4.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#include "paramview.h"
#include <QPaintEvent>
#include <QResizeEvent>
#include <QMouseEvent>
#include <QToolTip>
#include <QPainter>
#include <QPalette>
#include <QPen>
#include <QBrush>
#include <QRect>
#include <QSize>
#include <algorithm>
#include <utility>
#include "utils.h"
#include <QDebug>
using namespace utils::paint;
ParamView::ParamView(QWidget *parent) : QWidget(parent)
{
setMouseTracking(true);
param_color = palette().mid().color();
main_pixmap = new QPixmap();
param = nullptr;
buffer = new QQueue<float>();
buffer_min = 0.0f;
buffer_max = 0.0f;
}
ParamView::~ParamView()
{
delete main_pixmap;
delete buffer;
}
void ParamView::viewParam(const QString &name, Parameter *p)
{
param_name = name;
param = p;
}
Parameter *ParamView::parameter()
{
return param;
}
void ParamView::setColor(const QColor& c)
{
param_color = c;
updatePixmap();
update();
}
void ParamView::updated()
{
buffer->append(param->toFloat());
while(buffer->length() > width()) buffer->removeFirst();
auto buf_minmax = std::minmax_element(buffer->begin(), buffer->end());
buffer_min = *buf_minmax.first;
buffer_max = *buf_minmax.second;
updatePixmap();
update();
}
void ParamView::paintEvent(QPaintEvent *)
{
QPainter painter(this);
painter.drawPixmap(0, 0, *main_pixmap);
}
void ParamView::resizeEvent(QResizeEvent* event)
{
QWidget::resizeEvent(event);
delete main_pixmap;
main_pixmap = new QPixmap(event->size());
updatePixmap();
}
void ParamView::mouseMoveEvent(QMouseEvent *event)
{
QWidget::mouseMoveEvent(event);
int index = event->x();
if(buffer->size() < width()) index -= width() - buffer->size();
if(index < buffer->size() && index > 0){
QToolTip::showText(event->globalPos(),Parameter::number(param->type(), buffer->at(index)), this);
}else{
QToolTip::hideText();
}
}
void ParamView::updatePixmap()
{
if(main_pixmap->width() == 0 || main_pixmap->height() == 0) return;
QPainter painter(main_pixmap);
painter.setRenderHint(QPainter::TextAntialiasing, true);
painter.fillRect(main_pixmap->rect(), palette().dark());
float top_value = getNearestMax(buffer_max);
float bottom_value = (buffer_min >= 0.0f) ? 0.0f : getNearestMin(buffer_min);
if(top_value == bottom_value){
if(top_value == 0.0f){
top_value = 1.0f;
}else{
top_value += 0.5f;
bottom_value -= 0.5f;
}
}
QFont name_font(font());
scaleFont(name_font, height()/5, 8, 12);
QFontMetrics fm(name_font);
int delta = fm.height() / 2;
painter.setPen(param_color);
if(!buffer->isEmpty()){
auto calc_y = [top_value, bottom_value, this](float value) -> int{
return static_cast<int>((1.0f - (value - bottom_value) / (top_value - bottom_value)) * height());
};
int x = width() - buffer->size(), y = 0;
auto iter = buffer->begin();
++ iter;
for(; iter != buffer->end(); ++ iter){
x = x + 1;
y = calc_y(*iter);
painter.drawLine(x, y, x, height() - 1);
}
}
QString value_str = param ? param->toString() : QString();
QString top_str = param ? Parameter::number(param->type(), top_value) : QString();
QString bottom_str = param ? Parameter::number(param->type(), bottom_value) : QString();
painter.setPen(palette().light().color());
painter.setFont(name_font);
drawTextAt(&painter, param_name, delta, delta, TEXT_POS_BOTTOM_RIGHT);
drawTextAt(&painter, value_str, delta, height() - delta, TEXT_POS_TOP_RIGHT);
drawTextAt(&painter, top_str, width() - delta, delta, TEXT_POS_BOTTOM_LEFT);
drawTextAt(&painter, bottom_str, width() - delta, height() - delta, TEXT_POS_TOP_LEFT);
}
void ParamView::scaleFont(QFont& scale_font, int needed_height, int min_size, int max_size)
{
int dpi = logicalDpiY();
if(dpi == 0) return;
float font_size = static_cast<float>(needed_height) * 72 / dpi;
font_size = std::max<float>(std::min<float>(font_size, max_size), min_size);
//qDebug() << font_size;
scale_font.setPointSizeF(font_size);
}
float ParamView::getNearestMax(float value)
{
if(value == 0.0f) return 0.0f;
float cur_base = pow(10.0f, floor(log10(fabs(value))));
float n = ceil(value / cur_base);
float res = cur_base * n;
//qDebug() << value << cur_base << n << res;
if(value >= res) res += cur_base;
return res;
}
float ParamView::getNearestMin(float value)
{
if(value == 0.0f) return 0.0f;
float cur_base = pow(10.0f, floor(log10(fabs(value))));
float n = floor(value / cur_base);
float res = cur_base * n;
//qDebug() << value << cur_base << n << res;
if(value <= res) res -= cur_base;
return res;
}