-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoperator_const.cpp
More file actions
206 lines (177 loc) · 5.54 KB
/
operator_const.cpp
File metadata and controls
206 lines (177 loc) · 5.54 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
// SPDX-License-Identifier: GPL-2.0
#include "operator_const.hpp"
#include "document.hpp"
#include "aligned_buf.hpp" // For assume_aligned
#include <QGraphicsSceneMouseEvent>
#include <QMenu>
#include <cassert>
OperatorConst::OperatorConst(MainWindow &w)
: OperatorTemplate(w)
, imagebuf(size * size)
, current_color_type((ColorType)-1)
{
}
void OperatorConst::init()
{
// Paint handle above buttons
handle = new Handle("Click and drag to change value", this);
handle->setZValue(2.0);
dont_accumulate_undo = true;
// Make image buffer black
uint32_t *out = assume_aligned(imagebuf.get());
for (size_t i = 0; i < size * size; ++i)
*out++ = qRgb(0.0, 0.0, 0.0);
// Paint image before adding ruler, so that correct size is set.
paint_image();
scroller_scale = new Scroller(0.01, 100.0, true, [this](double v) { set_scale(v); }, this);
add_button_new_line();
new Button(":/icons/reset.svg", "Reset to 0", [this](){clear();}, Side::left, this);
new TextButton(("1"), "Set to 1", [this]() { set({ 1.0, 0.0 }); }, Side::left, this);
new TextButton(("-1"), "Set to -1", [this]() { set({ -1.0, 0.0 }); }, Side::left, this);
new TextButton(("i"), "Set to i", [this]() { set({ 0.0, 1.0 }); }, Side::left, this);
new TextButton(("-i"), "Set to -i", [this]() { set({ 0.0, -1.0 }); }, Side::left, this);
mode_menu = make_color_menu(this, [this](ColorType t) { switch_mode(t); }, Side::left);
text = add_text_line();
text->setPlainText("1");
}
QJsonObject OperatorConstState::to_json() const
{
QJsonObject res;
res["color_type"] = static_cast<int>(color_type);
res["v_real"] = v.real();
res["v_imag"] = v.imag();
return res;
}
void OperatorConstState::from_json(const QJsonObject &desc)
{
color_type = static_cast<ColorType>(desc["color_type"].toInt());
v = std::complex<double>(desc["v_real"].toDouble(), desc["v_imag"].toDouble());
}
void OperatorConst::state_reset()
{
if (current_color_type != state.color_type)
paint_image();
calculate();
place_handle();
set_scroller();
}
void OperatorConst::paint_image()
{
current_color_type = state.color_type; // save old color type to avoid useless repainting of the image
make_color_wheel(imagebuf, size, scale, state.color_type);
uint32_t *out = imagebuf.get();
QImage image(reinterpret_cast<unsigned char *>(out),
size, size, QImage::Format_RGB32);
setPixmap(QPixmap::fromImage(image));
}
void OperatorConst::placed()
{
make_output_complex(0);
calculate();
place_handle();
set_scroller();
}
static double round_to_3(double v)
{
return std::round(v * 1000.0) / 1000.0;
}
void OperatorConst::calculate()
{
size_t fft_size = get_fft_size();
size_t n = fft_size * fft_size;
std::complex<double> *buf = assume_aligned(output_buffers[0].get_complex_data());
std::complex<double> v = state.v * state.scale;
for (size_t i = 0; i < n; ++i)
*buf++ = v;
output_buffers[0].set_extremes(state.scale);
// Format string
double real_disp = round_to_3(v.real());
double imag_disp = round_to_3(v.imag());
bool has_real = std::abs(real_disp) > 0.0005;
bool has_imag = std::abs(imag_disp) > 0.0005;
QString str;
if (has_real && has_imag) {
if (std::abs(imag_disp - 1.0) < 0.0005) {
str = QStringLiteral("%1+i").arg(real_disp);
} else if (std::abs(imag_disp + 1.0) < 0.0005) {
str = QStringLiteral("%1-i").arg(real_disp);
} else if (v.imag() >= 0.0) {
str = QStringLiteral("%1+%2i").arg(real_disp)
.arg(imag_disp);
} else {
str = QStringLiteral("%1%2i").arg(real_disp)
.arg(imag_disp);
}
} else if (has_real) {
str = QStringLiteral("%1").arg(real_disp);
} else if (has_imag) {
if (std::abs(imag_disp - 1.0) < 0.0005)
str = QStringLiteral("i");
else if (std::abs(imag_disp + 1.0) < 0.0005)
str = QStringLiteral("-i");
else
str = QStringLiteral("%1i").arg(imag_disp);
} else {
str = "0";
}
text->setPlainText(str);
// Execute children
execute_topo();
}
void OperatorConst::place_handle()
{
double pos_x = state.v.real() * size / scale / 2.0 + size / 2.0;
double pos_y = -state.v.imag() * size / scale / 2.0 + size / 2.0;
handle->set_pos({ pos_x, pos_y });
}
bool OperatorConst::handle_click(QGraphicsSceneMouseEvent *event)
{
if (event->button() != Qt::LeftButton)
return false;
enter_drag_mode();
drag_handle(mapFromScene(event->scenePos()), event->modifiers());
return true;
}
void OperatorConst::drag_handle(const QPointF &p, Qt::KeyboardModifiers)
{
auto new_state = clone_state();
double x_rel = (p.x() - size / 2.0) / size * 2.0 * scale;
double y_rel = -(p.y() - size / 2.0) / size * 2.0 * scale;
std::complex<double> v(x_rel, y_rel);
if (std::norm(v) > 1.0)
v = std::polar(1.0, std::arg(v));
set(v);
dont_accumulate_undo = false;
}
void OperatorConst::set(std::complex<double> v)
{
auto new_state = clone_state();
new_state->v = v;
place_set_state_command("Set constant", std::move(new_state), !dont_accumulate_undo);
}
void OperatorConst::restore_handles()
{
dont_accumulate_undo = true;
}
void OperatorConst::switch_mode(ColorType type)
{
auto new_state = clone_state();
new_state->color_type = type;
place_set_state_command("Change constant mode", std::move(new_state), false);
}
void OperatorConst::set_scale(double scale)
{
auto new_state = clone_state();
new_state->scale = scale;
place_set_state_command("Set constant magnitude", std::move(new_state), !dont_accumulate_undo);
dont_accumulate_undo = false;
}
void OperatorConst::set_scroller()
{
scroller_scale->set_val(state.scale);
}
void OperatorConst::clear()
{
auto new_state = std::make_unique<OperatorConstState>();
place_set_state_command("Reset constant", std::move(new_state), false);
}