]>
Commit | Line | Data |
---|---|---|
aba1dd16 JH |
1 | /* |
2 | * This file is part of the PulseView project. | |
3 | * | |
4 | * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk> | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation; either version 2 of the License, or | |
9 | * (at your option) any later version. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
efdec55a | 17 | * along with this program; if not, see <http://www.gnu.org/licenses/>. |
aba1dd16 JH |
18 | */ |
19 | ||
20 | #include <extdef.h> | |
21 | ||
3b68d03d JH |
22 | #include <cassert> |
23 | #include <cmath> | |
8a2fafcc JH |
24 | #include <cstdlib> |
25 | #include <limits> | |
a8fd2759 | 26 | #include <vector> |
aba1dd16 | 27 | |
37b9fed4 | 28 | #include <QApplication> |
73e377fe | 29 | #include <QCheckBox> |
368a37c2 | 30 | #include <QComboBox> |
3782d860 | 31 | #include <QDebug> |
4cffac16 | 32 | #include <QFormLayout> |
368a37c2 SA |
33 | #include <QGridLayout> |
34 | #include <QLabel> | |
03cc651d | 35 | #include <QString> |
37b9fed4 | 36 | |
2acdb232 | 37 | #include "analogsignal.hpp" |
1573bf16 SA |
38 | #include "logicsignal.hpp" |
39 | #include "view.hpp" | |
40 | ||
3782d860 | 41 | #include "pv/util.hpp" |
2acdb232 | 42 | #include "pv/data/analog.hpp" |
f3d66e52 | 43 | #include "pv/data/analogsegment.hpp" |
a8fd2759 SA |
44 | #include "pv/data/logic.hpp" |
45 | #include "pv/data/logicsegment.hpp" | |
bf0edd2b | 46 | #include "pv/data/signalbase.hpp" |
8de1e1b2 | 47 | #include "pv/globalsettings.hpp" |
aba1dd16 | 48 | |
fe3a1c21 | 49 | #include <libsigrokcxx/libsigrokcxx.hpp> |
e8d00928 | 50 | |
6f925ba9 UH |
51 | using std::deque; |
52 | using std::div; | |
53 | using std::div_t; | |
3782d860 UH |
54 | // Note that "using std::isnan;" is _not_ put here since that would break |
55 | // compilation on some platforms. Use "std::isnan()" instead in checks below. | |
819f4c25 | 56 | using std::max; |
a5d93c27 | 57 | using std::make_pair; |
819f4c25 | 58 | using std::min; |
6f925ba9 | 59 | using std::numeric_limits; |
3782d860 | 60 | using std::out_of_range; |
6f925ba9 | 61 | using std::pair; |
f9abf97e | 62 | using std::shared_ptr; |
a8fd2759 | 63 | using std::vector; |
aba1dd16 | 64 | |
3782d860 UH |
65 | using pv::data::LogicSegment; |
66 | using pv::data::SignalBase; | |
67 | using pv::util::SIPrefix; | |
68 | ||
aba1dd16 | 69 | namespace pv { |
f4e57597 | 70 | namespace views { |
1573bf16 | 71 | namespace trace { |
aba1dd16 | 72 | |
3782d860 | 73 | const QColor AnalogSignal::SignalColors[4] = { |
568b90d4 JH |
74 | QColor(0xC4, 0xA0, 0x00), // Yellow |
75 | QColor(0x87, 0x20, 0x7A), // Magenta | |
76 | QColor(0x20, 0x4A, 0x87), // Blue | |
77 | QColor(0x4E, 0x9A, 0x06) // Green | |
78 | }; | |
79 | ||
3782d860 | 80 | const QPen AnalogSignal::AxisPen(QColor(0, 0, 0, 30 * 256 / 100), 2); |
c063290a UH |
81 | const QColor AnalogSignal::GridMajorColor = QColor(0, 0, 0, 40 * 256 / 100); |
82 | const QColor AnalogSignal::GridMinorColor = QColor(0, 0, 0, 20 * 256 / 100); | |
37b9fed4 | 83 | |
3782d860 UH |
84 | const QColor AnalogSignal::SamplingPointColor(0x77, 0x77, 0x77); |
85 | const QColor AnalogSignal::SamplingPointColorLo = QColor(200, 0, 0, 80 * 256 / 100); | |
86 | const QColor AnalogSignal::SamplingPointColorNe = QColor(0, 0, 0, 80 * 256 / 100); | |
87 | const QColor AnalogSignal::SamplingPointColorHi = QColor(0, 200, 0, 80 * 256 / 100); | |
88 | ||
89 | const QColor AnalogSignal::ThresholdColor = QColor(0, 0, 0, 30 * 256 / 100); | |
90 | const QColor AnalogSignal::ThresholdColorLo = QColor(255, 0, 0, 8 * 256 / 100); | |
91 | const QColor AnalogSignal::ThresholdColorNe = QColor(0, 0, 0, 10 * 256 / 100); | |
92 | const QColor AnalogSignal::ThresholdColorHi = QColor(0, 255, 0, 8 * 256 / 100); | |
8de1e1b2 | 93 | |
fdfd5b3e SA |
94 | const int64_t AnalogSignal::TracePaintBlockSize = 1024 * 1024; // 4 MiB (due to float) |
95 | const float AnalogSignal::EnvelopeThreshold = 64.0f; | |
7c68ddae | 96 | |
4cffac16 | 97 | const int AnalogSignal::MaximumVDivs = 10; |
10f5e6f5 UH |
98 | const int AnalogSignal::MinScaleIndex = -6; // 0.01 units/div |
99 | const int AnalogSignal::MaxScaleIndex = 10; // 1000 units/div | |
4cffac16 | 100 | |
03cc651d SA |
101 | const int AnalogSignal::InfoTextMarginRight = 20; |
102 | const int AnalogSignal::InfoTextMarginBottom = 5; | |
103 | ||
b86aa8f4 | 104 | AnalogSignal::AnalogSignal( |
2b81ae46 | 105 | pv::Session &session, |
cbd2a2de | 106 | shared_ptr<data::SignalBase> base) : |
73a25a6e | 107 | Signal(session, base), |
10f5e6f5 | 108 | value_at_hover_pos_(std::numeric_limits<float>::quiet_NaN()), |
834a4f1b | 109 | scale_index_(4), // 20 per div |
459db2c5 SA |
110 | pos_vdivs_(1), |
111 | neg_vdivs_(1), | |
73e377fe | 112 | resolution_(0), |
a970015f | 113 | display_type_(DisplayBoth), |
10f5e6f5 | 114 | autoranging_(true) |
aba1dd16 | 115 | { |
3782d860 UH |
116 | axis_pen_ = AxisPen; |
117 | ||
85715407 SA |
118 | pv::data::Analog* analog_data = |
119 | dynamic_cast<pv::data::Analog*>(data().get()); | |
120 | ||
3782d860 UH |
121 | connect(analog_data, SIGNAL(min_max_changed(float, float)), |
122 | this, SLOT(on_min_max_changed(float, float))); | |
123 | ||
124 | GlobalSettings settings; | |
125 | show_sampling_points_ = | |
126 | settings.value(GlobalSettings::Key_View_ShowSamplingPoints).toBool(); | |
127 | fill_high_areas_ = | |
128 | settings.value(GlobalSettings::Key_View_FillSignalHighAreas).toBool(); | |
129 | high_fill_color_ = QColor::fromRgba(settings.value( | |
130 | GlobalSettings::Key_View_FillSignalHighAreaColor).value<uint32_t>()); | |
131 | show_analog_minor_grid_ = | |
132 | settings.value(GlobalSettings::Key_View_ShowAnalogMinorGrid).toBool(); | |
133 | conversion_threshold_disp_mode_ = | |
134 | settings.value(GlobalSettings::Key_View_ConversionThresholdDispMode).toInt(); | |
135 | div_height_ = settings.value(GlobalSettings::Key_View_DefaultDivHeight).toInt(); | |
85715407 | 136 | |
3782d860 | 137 | base_->set_color(SignalColors[base_->index() % countof(SignalColors)]); |
834a4f1b | 138 | update_scale(); |
aba1dd16 JH |
139 | } |
140 | ||
3009b5b3 JH |
141 | shared_ptr<pv::data::SignalData> AnalogSignal::data() const |
142 | { | |
cbd2a2de | 143 | return base_->analog_data(); |
9a0cd293 JH |
144 | } |
145 | ||
10f5e6f5 | 146 | std::map<QString, QVariant> AnalogSignal::save_settings() const |
3a21afa6 | 147 | { |
10f5e6f5 UH |
148 | std::map<QString, QVariant> result; |
149 | ||
150 | result["pos_vdivs"] = pos_vdivs_; | |
151 | result["neg_vdivs"] = neg_vdivs_; | |
152 | result["scale_index"] = scale_index_; | |
153 | result["display_type"] = display_type_; | |
154 | result["autoranging"] = pos_vdivs_; | |
155 | result["div_height"] = div_height_; | |
156 | ||
157 | return result; | |
3a21afa6 SA |
158 | } |
159 | ||
10f5e6f5 | 160 | void AnalogSignal::restore_settings(std::map<QString, QVariant> settings) |
3a21afa6 | 161 | { |
10f5e6f5 UH |
162 | auto entry = settings.find("pos_vdivs"); |
163 | if (entry != settings.end()) | |
164 | pos_vdivs_ = settings["pos_vdivs"].toInt(); | |
459db2c5 | 165 | |
10f5e6f5 UH |
166 | entry = settings.find("neg_vdivs"); |
167 | if (entry != settings.end()) | |
168 | neg_vdivs_ = settings["neg_vdivs"].toInt(); | |
3a21afa6 | 169 | |
10f5e6f5 UH |
170 | entry = settings.find("scale_index"); |
171 | if (entry != settings.end()) { | |
172 | scale_index_ = settings["scale_index"].toInt(); | |
3a21afa6 SA |
173 | update_scale(); |
174 | } | |
73e377fe | 175 | |
10f5e6f5 UH |
176 | entry = settings.find("display_type"); |
177 | if (entry != settings.end()) | |
178 | display_type_ = (DisplayType)(settings["display_type"].toInt()); | |
a970015f | 179 | |
10f5e6f5 UH |
180 | entry = settings.find("autoranging"); |
181 | if (entry != settings.end()) | |
182 | autoranging_ = settings["autoranging"].toBool(); | |
3782d860 | 183 | |
10f5e6f5 UH |
184 | entry = settings.find("div_height"); |
185 | if (entry != settings.end()) { | |
3782d860 | 186 | const int old_height = div_height_; |
10f5e6f5 | 187 | div_height_ = settings["div_height"].toInt(); |
3782d860 UH |
188 | |
189 | if ((div_height_ != old_height) && owner_) { | |
190 | // Call order is important, otherwise the lazy event handler won't work | |
191 | owner_->extents_changed(false, true); | |
192 | owner_->row_item_appearance_changed(false, true); | |
193 | } | |
194 | } | |
3a21afa6 SA |
195 | } |
196 | ||
6f925ba9 | 197 | pair<int, int> AnalogSignal::v_extents() const |
a5d93c27 | 198 | { |
459db2c5 SA |
199 | const int ph = pos_vdivs_ * div_height_; |
200 | const int nh = neg_vdivs_ * div_height_; | |
201 | return make_pair(-ph, nh); | |
a5d93c27 JH |
202 | } |
203 | ||
3782d860 | 204 | void AnalogSignal::paint_back(QPainter &p, ViewItemPaintParams &pp) |
214470fc | 205 | { |
3782d860 UH |
206 | if (!base_->enabled()) |
207 | return; | |
8a2fafcc | 208 | |
3782d860 UH |
209 | bool paint_thr_bg = |
210 | conversion_threshold_disp_mode_ == GlobalSettings::ConvThrDispMode_Background; | |
211 | ||
212 | const vector<double> thresholds = base_->get_conversion_thresholds(); | |
213 | ||
214 | // Only display thresholds if we have some and we show analog samples | |
215 | if ((thresholds.size() > 0) && paint_thr_bg && | |
216 | ((display_type_ == DisplayAnalog) || (display_type_ == DisplayBoth))) { | |
217 | ||
218 | const int visual_y = get_visual_y(); | |
219 | const pair<int, int> extents = v_extents(); | |
220 | const int top = visual_y + extents.first; | |
221 | const int btm = visual_y + extents.second; | |
222 | ||
223 | // Draw high/neutral/low areas | |
224 | if (thresholds.size() == 2) { | |
225 | int thr_lo = visual_y - thresholds[0] * scale_; | |
226 | int thr_hi = visual_y - thresholds[1] * scale_; | |
227 | thr_lo = min(max(thr_lo, top), btm); | |
228 | thr_hi = min(max(thr_hi, top), btm); | |
229 | ||
230 | p.fillRect(QRectF(pp.left(), top, pp.width(), thr_hi - top), | |
231 | QBrush(ThresholdColorHi)); | |
232 | p.fillRect(QRectF(pp.left(), thr_hi, pp.width(), thr_lo - thr_hi), | |
233 | QBrush(ThresholdColorNe)); | |
234 | p.fillRect(QRectF(pp.left(), thr_lo, pp.width(), btm - thr_lo), | |
235 | QBrush(ThresholdColorLo)); | |
236 | } else { | |
237 | int thr = visual_y - thresholds[0] * scale_; | |
238 | thr = min(max(thr, top), btm); | |
239 | ||
240 | p.fillRect(QRectF(pp.left(), top, pp.width(), thr - top), | |
241 | QBrush(ThresholdColorHi)); | |
242 | p.fillRect(QRectF(pp.left(), thr, pp.width(), btm - thr), | |
243 | QBrush(ThresholdColorLo)); | |
244 | } | |
214470fc | 245 | |
3782d860 UH |
246 | paint_axis(p, pp, get_visual_y()); |
247 | } else { | |
248 | Signal::paint_back(p, pp); | |
97904bf7 | 249 | paint_axis(p, pp, get_visual_y()); |
99af6802 | 250 | } |
fe08b6e8 JH |
251 | } |
252 | ||
60938e04 | 253 | void AnalogSignal::paint_mid(QPainter &p, ViewItemPaintParams &pp) |
aba1dd16 | 254 | { |
cbd2a2de | 255 | assert(base_->analog_data()); |
8dbbc7f0 | 256 | assert(owner_); |
a8acb46e | 257 | |
be9e7b4b | 258 | const int y = get_visual_y(); |
01fd3263 | 259 | |
73a25a6e | 260 | if (!base_->enabled()) |
cec48d16 JH |
261 | return; |
262 | ||
a8fd2759 SA |
263 | if ((display_type_ == DisplayAnalog) || (display_type_ == DisplayBoth)) { |
264 | paint_grid(p, y, pp.left(), pp.right()); | |
265 | ||
3782d860 UH |
266 | shared_ptr<pv::data::AnalogSegment> segment = get_analog_segment_to_paint(); |
267 | if (!segment || (segment->get_sample_count() == 0)) | |
a8fd2759 SA |
268 | return; |
269 | ||
a8fd2759 SA |
270 | const double pixels_offset = pp.pixels_offset(); |
271 | const double samplerate = max(1.0, segment->samplerate()); | |
272 | const pv::util::Timestamp& start_time = segment->start_time(); | |
3782d860 | 273 | const int64_t last_sample = (int64_t)segment->get_sample_count() - 1; |
a8fd2759 SA |
274 | const double samples_per_pixel = samplerate * pp.scale(); |
275 | const pv::util::Timestamp start = samplerate * (pp.offset() - start_time); | |
276 | const pv::util::Timestamp end = start + samples_per_pixel * pp.width(); | |
277 | ||
278 | const int64_t start_sample = min(max(floor(start).convert_to<int64_t>(), | |
279 | (int64_t)0), last_sample); | |
280 | const int64_t end_sample = min(max((ceil(end) + 1).convert_to<int64_t>(), | |
281 | (int64_t)0), last_sample); | |
282 | ||
283 | if (samples_per_pixel < EnvelopeThreshold) | |
3782d860 | 284 | paint_trace(p, segment, y, pp.left(), start_sample, end_sample, |
a8fd2759 SA |
285 | pixels_offset, samples_per_pixel); |
286 | else | |
3782d860 | 287 | paint_envelope(p, segment, y, pp.left(), start_sample, end_sample, |
a8fd2759 SA |
288 | pixels_offset, samples_per_pixel); |
289 | } | |
a8acb46e | 290 | |
3782d860 UH |
291 | if ((display_type_ == DisplayConverted) || (display_type_ == DisplayBoth)) |
292 | paint_logic_mid(p, pp); | |
7c68ddae JH |
293 | } |
294 | ||
60938e04 | 295 | void AnalogSignal::paint_fore(QPainter &p, ViewItemPaintParams &pp) |
03cc651d SA |
296 | { |
297 | if (!enabled()) | |
298 | return; | |
299 | ||
b8c4a95b SA |
300 | if ((display_type_ == DisplayAnalog) || (display_type_ == DisplayBoth)) { |
301 | const int y = get_visual_y(); | |
03cc651d | 302 | |
3782d860 UH |
303 | QString infotext; |
304 | ||
305 | // Show the info section on the right side of the trace, including | |
306 | // the value at the hover point when the hover marker is enabled | |
307 | // and we have corresponding data available | |
308 | if (show_hover_marker_ && !std::isnan(value_at_hover_pos_)) { | |
309 | infotext = QString("[%1] %2 V/div") | |
10f5e6f5 | 310 | .arg(format_value_si(value_at_hover_pos_, SIPrefix::unspecified, 2, "V", false)) |
3782d860 UH |
311 | .arg(resolution_); |
312 | } else | |
313 | infotext = QString("%1 V/div").arg(resolution_); | |
03cc651d | 314 | |
3782d860 | 315 | p.setPen(base_->color()); |
b8c4a95b | 316 | p.setFont(QApplication::font()); |
03cc651d | 317 | |
b8c4a95b SA |
318 | const QRectF bounding_rect = QRectF(pp.left(), |
319 | y + v_extents().first, | |
320 | pp.width() - InfoTextMarginRight, | |
321 | v_extents().second - v_extents().first - InfoTextMarginBottom); | |
03cc651d | 322 | |
b8c4a95b SA |
323 | p.drawText(bounding_rect, Qt::AlignRight | Qt::AlignBottom, infotext); |
324 | } | |
3782d860 UH |
325 | |
326 | if (show_hover_marker_) | |
327 | paint_hover_marker(p); | |
03cc651d SA |
328 | } |
329 | ||
37b9fed4 SA |
330 | void AnalogSignal::paint_grid(QPainter &p, int y, int left, int right) |
331 | { | |
10f5e6f5 | 332 | bool was_antialiased = p.testRenderHint(QPainter::Antialiasing); |
46a0cadc SA |
333 | p.setRenderHint(QPainter::Antialiasing, false); |
334 | ||
459db2c5 SA |
335 | if (pos_vdivs_ > 0) { |
336 | p.setPen(QPen(GridMajorColor, 1, Qt::DashLine)); | |
337 | for (int i = 1; i <= pos_vdivs_; i++) { | |
338 | const float dy = i * div_height_; | |
339 | p.drawLine(QLineF(left, y - dy, right, y - dy)); | |
340 | } | |
8ad61f40 | 341 | } |
459db2c5 | 342 | |
3782d860 | 343 | if ((pos_vdivs_ > 0) && show_analog_minor_grid_) { |
459db2c5 SA |
344 | p.setPen(QPen(GridMinorColor, 1, Qt::DashLine)); |
345 | for (int i = 0; i < pos_vdivs_; i++) { | |
346 | const float dy = i * div_height_; | |
347 | const float dy25 = dy + (0.25 * div_height_); | |
348 | const float dy50 = dy + (0.50 * div_height_); | |
349 | const float dy75 = dy + (0.75 * div_height_); | |
350 | p.drawLine(QLineF(left, y - dy25, right, y - dy25)); | |
351 | p.drawLine(QLineF(left, y - dy50, right, y - dy50)); | |
352 | p.drawLine(QLineF(left, y - dy75, right, y - dy75)); | |
353 | } | |
37b9fed4 SA |
354 | } |
355 | ||
459db2c5 SA |
356 | if (neg_vdivs_ > 0) { |
357 | p.setPen(QPen(GridMajorColor, 1, Qt::DashLine)); | |
358 | for (int i = 1; i <= neg_vdivs_; i++) { | |
359 | const float dy = i * div_height_; | |
360 | p.drawLine(QLineF(left, y + dy, right, y + dy)); | |
361 | } | |
8ad61f40 | 362 | } |
459db2c5 | 363 | |
3782d860 | 364 | if ((pos_vdivs_ > 0) && show_analog_minor_grid_) { |
459db2c5 SA |
365 | p.setPen(QPen(GridMinorColor, 1, Qt::DashLine)); |
366 | for (int i = 0; i < neg_vdivs_; i++) { | |
367 | const float dy = i * div_height_; | |
368 | const float dy25 = dy + (0.25 * div_height_); | |
369 | const float dy50 = dy + (0.50 * div_height_); | |
370 | const float dy75 = dy + (0.75 * div_height_); | |
371 | p.drawLine(QLineF(left, y + dy25, right, y + dy25)); | |
372 | p.drawLine(QLineF(left, y + dy50, right, y + dy50)); | |
373 | p.drawLine(QLineF(left, y + dy75, right, y + dy75)); | |
374 | } | |
37b9fed4 | 375 | } |
46a0cadc | 376 | |
10f5e6f5 | 377 | p.setRenderHint(QPainter::Antialiasing, was_antialiased); |
37b9fed4 SA |
378 | } |
379 | ||
7c68ddae | 380 | void AnalogSignal::paint_trace(QPainter &p, |
f3d66e52 | 381 | const shared_ptr<pv::data::AnalogSegment> &segment, |
7c68ddae JH |
382 | int y, int left, const int64_t start, const int64_t end, |
383 | const double pixels_offset, const double samples_per_pixel) | |
384 | { | |
fdfd5b3e SA |
385 | if (end <= start) |
386 | return; | |
387 | ||
3782d860 UH |
388 | bool paint_thr_dots = |
389 | (base_->get_conversion_type() != data::SignalBase::NoConversion) && | |
390 | (conversion_threshold_disp_mode_ == GlobalSettings::ConvThrDispMode_Dots); | |
391 | ||
392 | vector<double> thresholds; | |
393 | if (paint_thr_dots) | |
394 | thresholds = base_->get_conversion_thresholds(); | |
395 | ||
fdfd5b3e SA |
396 | // Calculate and paint the sampling points if enabled and useful |
397 | GlobalSettings settings; | |
398 | const bool show_sampling_points = | |
3782d860 | 399 | (show_sampling_points_ || paint_thr_dots) && (samples_per_pixel < 0.25); |
fdfd5b3e | 400 | |
3782d860 | 401 | p.setPen(base_->color()); |
7c68ddae | 402 | |
3782d860 | 403 | const int64_t points_count = end - start + 1; |
76ce6c7a UH |
404 | |
405 | QPointF *points = new QPointF[points_count]; | |
a8acb46e JH |
406 | QPointF *point = points; |
407 | ||
3782d860 | 408 | vector<QRectF> sampling_points[3]; |
8de1e1b2 | 409 | |
fdfd5b3e SA |
410 | int64_t sample_count = min(points_count, TracePaintBlockSize); |
411 | int64_t block_sample = 0; | |
3782d860 UH |
412 | float *sample_block = new float[TracePaintBlockSize]; |
413 | segment->get_samples(start, start + sample_count, sample_block); | |
414 | ||
415 | if (show_hover_marker_) | |
416 | reset_pixel_values(); | |
26a883ed | 417 | |
8de1e1b2 | 418 | const int w = 2; |
3782d860 | 419 | for (int64_t sample = start; sample <= end; sample++, block_sample++) { |
fdfd5b3e | 420 | |
3782d860 | 421 | // Fetch next block of samples if we finished the current one |
fdfd5b3e SA |
422 | if (block_sample == TracePaintBlockSize) { |
423 | block_sample = 0; | |
fdfd5b3e | 424 | sample_count = min(points_count - sample, TracePaintBlockSize); |
3782d860 | 425 | segment->get_samples(sample, sample + sample_count, sample_block); |
fdfd5b3e SA |
426 | } |
427 | ||
3782d860 UH |
428 | const float abs_x = sample / samples_per_pixel - pixels_offset; |
429 | const float x = left + abs_x; | |
26a883ed | 430 | |
fdfd5b3e | 431 | *point++ = QPointF(x, y - sample_block[block_sample] * scale_); |
8de1e1b2 | 432 | |
3782d860 UH |
433 | // Generate the pixel<->value lookup table for the mouse hover |
434 | if (show_hover_marker_) | |
435 | process_next_sample_value(abs_x, sample_block[block_sample]); | |
436 | ||
437 | // Create the sampling points if needed | |
438 | if (show_sampling_points) { | |
439 | int idx = 0; // Neutral | |
440 | ||
441 | if (paint_thr_dots) { | |
442 | if (thresholds.size() == 1) | |
443 | idx = (sample_block[block_sample] >= thresholds[0]) ? 2 : 1; | |
444 | else if (thresholds.size() == 2) { | |
445 | if (sample_block[block_sample] > thresholds[1]) | |
446 | idx = 2; // High | |
447 | else if (sample_block[block_sample] < thresholds[0]) | |
448 | idx = 1; // Low | |
449 | } | |
450 | } | |
451 | ||
452 | sampling_points[idx].emplace_back(x - (w / 2), y - sample_block[block_sample] * scale_ - (w / 2), w, w); | |
453 | } | |
a8acb46e | 454 | } |
fdfd5b3e | 455 | delete[] sample_block; |
a8acb46e | 456 | |
10f5e6f5 UH |
457 | // QPainter::drawPolyline() is slow, let's paint the lines ourselves |
458 | for (int64_t i = 1; i < points_count; i++) | |
459 | p.drawLine(points[i - 1], points[i]); | |
a8acb46e | 460 | |
fdfd5b3e | 461 | if (show_sampling_points) { |
3782d860 UH |
462 | if (paint_thr_dots) { |
463 | p.setPen(SamplingPointColorNe); | |
464 | p.drawRects(sampling_points[0].data(), sampling_points[0].size()); | |
465 | p.setPen(SamplingPointColorLo); | |
466 | p.drawRects(sampling_points[1].data(), sampling_points[1].size()); | |
467 | p.setPen(SamplingPointColorHi); | |
468 | p.drawRects(sampling_points[2].data(), sampling_points[2].size()); | |
469 | } else { | |
470 | p.setPen(SamplingPointColor); | |
471 | p.drawRects(sampling_points[0].data(), sampling_points[0].size()); | |
472 | } | |
8de1e1b2 UH |
473 | } |
474 | ||
a8acb46e | 475 | delete[] points; |
aba1dd16 JH |
476 | } |
477 | ||
7c68ddae | 478 | void AnalogSignal::paint_envelope(QPainter &p, |
f3d66e52 | 479 | const shared_ptr<pv::data::AnalogSegment> &segment, |
7c68ddae JH |
480 | int y, int left, const int64_t start, const int64_t end, |
481 | const double pixels_offset, const double samples_per_pixel) | |
482 | { | |
f3d66e52 | 483 | using pv::data::AnalogSegment; |
7c68ddae | 484 | |
3782d860 UH |
485 | // Note: Envelope painting currently doesn't generate a pixel<->value lookup table |
486 | if (show_hover_marker_) | |
487 | reset_pixel_values(); | |
488 | ||
f3d66e52 JH |
489 | AnalogSegment::EnvelopeSection e; |
490 | segment->get_envelope_section(e, start, end, samples_per_pixel); | |
7c68ddae JH |
491 | |
492 | if (e.length < 2) | |
493 | return; | |
494 | ||
819f4c25 | 495 | p.setPen(QPen(Qt::NoPen)); |
3782d860 | 496 | p.setBrush(base_->color()); |
7c68ddae JH |
497 | |
498 | QRectF *const rects = new QRectF[e.length]; | |
499 | QRectF *rect = rects; | |
500 | ||
c063290a | 501 | for (uint64_t sample = 0; sample < e.length - 1; sample++) { |
7c68ddae JH |
502 | const float x = ((e.scale * sample + e.start) / |
503 | samples_per_pixel - pixels_offset) + left; | |
3782d860 UH |
504 | |
505 | const AnalogSegment::EnvelopeSample *const s = e.samples + sample; | |
7c68ddae JH |
506 | |
507 | // We overlap this sample with the next so that vertical | |
508 | // gaps do not appear during steep rising or falling edges | |
c063290a UH |
509 | const float b = y - max(s->max, (s + 1)->min) * scale_; |
510 | const float t = y - min(s->min, (s + 1)->max) * scale_; | |
7c68ddae JH |
511 | |
512 | float h = b - t; | |
f3290553 | 513 | if (h >= 0.0f && h <= 1.0f) |
7c68ddae | 514 | h = 1.0f; |
f3290553 | 515 | if (h <= 0.0f && h >= -1.0f) |
7c68ddae JH |
516 | h = -1.0f; |
517 | ||
518 | *rect++ = QRectF(x, t, 1.0f, h); | |
519 | } | |
520 | ||
521 | p.drawRects(rects, e.length); | |
522 | ||
523 | delete[] rects; | |
524 | delete[] e.samples; | |
525 | } | |
526 | ||
60938e04 | 527 | void AnalogSignal::paint_logic_mid(QPainter &p, ViewItemPaintParams &pp) |
a8fd2759 SA |
528 | { |
529 | QLineF *line; | |
530 | ||
531 | vector< pair<int64_t, bool> > edges; | |
532 | ||
533 | assert(base_); | |
534 | ||
535 | const int y = get_visual_y(); | |
536 | ||
6c0df7c4 | 537 | if (!base_->enabled() || !base_->logic_data()) |
a8fd2759 SA |
538 | return; |
539 | ||
540 | const int signal_margin = | |
541 | QFontMetrics(QApplication::font()).height() / 2; | |
542 | ||
543 | const int ph = min(pos_vdivs_, 1) * div_height_; | |
544 | const int nh = min(neg_vdivs_, 1) * div_height_; | |
545 | const float high_offset = y - ph + signal_margin + 0.5f; | |
546 | const float low_offset = y + nh - signal_margin - 0.5f; | |
3782d860 | 547 | const float signal_height = low_offset - high_offset; |
a8fd2759 | 548 | |
3782d860 UH |
549 | shared_ptr<pv::data::LogicSegment> segment = get_logic_segment_to_paint(); |
550 | if (!segment || (segment->get_sample_count() == 0)) | |
a8fd2759 SA |
551 | return; |
552 | ||
a8fd2759 SA |
553 | double samplerate = segment->samplerate(); |
554 | ||
555 | // Show sample rate as 1Hz when it is unknown | |
556 | if (samplerate == 0.0) | |
557 | samplerate = 1.0; | |
558 | ||
559 | const double pixels_offset = pp.pixels_offset(); | |
560 | const pv::util::Timestamp& start_time = segment->start_time(); | |
3782d860 | 561 | const int64_t last_sample = (int64_t)segment->get_sample_count() - 1; |
a8fd2759 | 562 | const double samples_per_pixel = samplerate * pp.scale(); |
124be547 | 563 | const double pixels_per_sample = 1 / samples_per_pixel; |
a8fd2759 SA |
564 | const pv::util::Timestamp start = samplerate * (pp.offset() - start_time); |
565 | const pv::util::Timestamp end = start + samples_per_pixel * pp.width(); | |
566 | ||
567 | const int64_t start_sample = min(max(floor(start).convert_to<int64_t>(), | |
568 | (int64_t)0), last_sample); | |
569 | const uint64_t end_sample = min(max(ceil(end).convert_to<int64_t>(), | |
570 | (int64_t)0), last_sample); | |
571 | ||
572 | segment->get_subsampled_edges(edges, start_sample, end_sample, | |
573 | samples_per_pixel / LogicSignal::Oversampling, 0); | |
574 | assert(edges.size() >= 2); | |
575 | ||
3782d860 UH |
576 | const float first_sample_x = |
577 | pp.left() + (edges.front().first / samples_per_pixel - pixels_offset); | |
578 | const float last_sample_x = | |
579 | pp.left() + (edges.back().first / samples_per_pixel - pixels_offset); | |
124be547 | 580 | |
3782d860 UH |
581 | // Check whether we need to paint the sampling points |
582 | const bool show_sampling_points = show_sampling_points_ && (samples_per_pixel < 0.25); | |
124be547 | 583 | vector<QRectF> sampling_points; |
3782d860 | 584 | float sampling_point_x = first_sample_x; |
124be547 SA |
585 | int64_t sampling_point_sample = start_sample; |
586 | const int w = 2; | |
587 | ||
3782d860 | 588 | if (show_sampling_points) |
124be547 | 589 | sampling_points.reserve(end_sample - start_sample + 1); |
3782d860 UH |
590 | |
591 | vector<QRectF> high_rects; | |
592 | float rising_edge_x; | |
593 | bool rising_edge_seen = false; | |
124be547 | 594 | |
a8fd2759 SA |
595 | // Paint the edges |
596 | const unsigned int edge_count = edges.size() - 2; | |
597 | QLineF *const edge_lines = new QLineF[edge_count]; | |
598 | line = edge_lines; | |
599 | ||
3782d860 UH |
600 | if (edges.front().second) { |
601 | // Beginning of trace is high | |
602 | rising_edge_x = first_sample_x; | |
603 | rising_edge_seen = true; | |
604 | } | |
605 | ||
a8fd2759 | 606 | for (auto i = edges.cbegin() + 1; i != edges.cend() - 1; i++) { |
3782d860 UH |
607 | // Note: multiple edges occupying a single pixel are represented by an edge |
608 | // with undefined logic level. This means that only the first falling edge | |
609 | // after a rising edge corresponds to said rising edge - and vice versa. If | |
610 | // more edges with the same logic level follow, they denote multiple edges. | |
611 | ||
612 | const float x = pp.left() + ((*i).first / samples_per_pixel - pixels_offset); | |
a8fd2759 | 613 | *line++ = QLineF(x, high_offset, x, low_offset); |
124be547 | 614 | |
3782d860 UH |
615 | if (fill_high_areas_) { |
616 | // Any edge terminates a high area | |
617 | if (rising_edge_seen) { | |
618 | const int width = x - rising_edge_x; | |
619 | if (width > 0) | |
620 | high_rects.emplace_back(rising_edge_x, high_offset, | |
621 | width, signal_height); | |
622 | rising_edge_seen = false; | |
623 | } | |
624 | ||
625 | // Only rising edges start high areas | |
626 | if ((*i).second) { | |
627 | rising_edge_x = x; | |
628 | rising_edge_seen = true; | |
629 | } | |
630 | } | |
631 | ||
124be547 SA |
632 | if (show_sampling_points) |
633 | while (sampling_point_sample < (*i).first) { | |
634 | const float y = (*i).second ? low_offset : high_offset; | |
635 | sampling_points.emplace_back( | |
636 | QRectF(sampling_point_x - (w / 2), y - (w / 2), w, w)); | |
637 | sampling_point_sample++; | |
638 | sampling_point_x += pixels_per_sample; | |
639 | }; | |
a8fd2759 SA |
640 | } |
641 | ||
124be547 SA |
642 | // Calculate the sample points from the last edge to the end of the trace |
643 | if (show_sampling_points) | |
644 | while ((uint64_t)sampling_point_sample <= end_sample) { | |
645 | // Signal changed after the last edge, so the level is inverted | |
646 | const float y = (edges.cend() - 1)->second ? high_offset : low_offset; | |
647 | sampling_points.emplace_back( | |
648 | QRectF(sampling_point_x - (w / 2), y - (w / 2), w, w)); | |
649 | sampling_point_sample++; | |
650 | sampling_point_x += pixels_per_sample; | |
651 | }; | |
652 | ||
3782d860 UH |
653 | if (fill_high_areas_) { |
654 | // Add last high rectangle if the signal is still high at the end of the trace | |
655 | if (rising_edge_seen && (edges.cend() - 1)->second) | |
656 | high_rects.emplace_back(rising_edge_x, high_offset, | |
657 | last_sample_x - rising_edge_x, signal_height); | |
658 | ||
659 | p.setPen(high_fill_color_); | |
660 | p.setBrush(high_fill_color_); | |
661 | p.drawRects((const QRectF*)(high_rects.data()), high_rects.size()); | |
662 | } | |
663 | ||
664 | p.setPen(LogicSignal::EdgeColor); | |
a8fd2759 SA |
665 | p.drawLines(edge_lines, edge_count); |
666 | delete[] edge_lines; | |
667 | ||
668 | // Paint the caps | |
669 | const unsigned int max_cap_line_count = edges.size(); | |
670 | QLineF *const cap_lines = new QLineF[max_cap_line_count]; | |
671 | ||
3782d860 | 672 | p.setPen(LogicSignal::HighColor); |
a8fd2759 SA |
673 | paint_logic_caps(p, cap_lines, edges, true, samples_per_pixel, |
674 | pixels_offset, pp.left(), high_offset); | |
3782d860 | 675 | p.setPen(LogicSignal::LowColor); |
a8fd2759 SA |
676 | paint_logic_caps(p, cap_lines, edges, false, samples_per_pixel, |
677 | pixels_offset, pp.left(), low_offset); | |
678 | ||
679 | delete[] cap_lines; | |
680 | ||
a8fd2759 | 681 | // Paint the sampling points |
124be547 | 682 | if (show_sampling_points) { |
3782d860 | 683 | p.setPen(SamplingPointColor); |
124be547 | 684 | p.drawRects(sampling_points.data(), sampling_points.size()); |
a8fd2759 | 685 | } |
a8fd2759 SA |
686 | } |
687 | ||
688 | void AnalogSignal::paint_logic_caps(QPainter &p, QLineF *const lines, | |
689 | vector< pair<int64_t, bool> > &edges, bool level, | |
690 | double samples_per_pixel, double pixels_offset, float x_offset, | |
691 | float y_offset) | |
692 | { | |
693 | QLineF *line = lines; | |
694 | ||
695 | for (auto i = edges.begin(); i != (edges.end() - 1); i++) | |
696 | if ((*i).second == level) { | |
697 | *line++ = QLineF( | |
698 | ((*i).first / samples_per_pixel - | |
699 | pixels_offset) + x_offset, y_offset, | |
700 | ((*(i+1)).first / samples_per_pixel - | |
701 | pixels_offset) + x_offset, y_offset); | |
702 | } | |
703 | ||
704 | p.drawLines(lines, line - lines); | |
705 | } | |
706 | ||
3782d860 UH |
707 | shared_ptr<pv::data::AnalogSegment> AnalogSignal::get_analog_segment_to_paint() const |
708 | { | |
709 | shared_ptr<pv::data::AnalogSegment> segment; | |
710 | ||
711 | const deque< shared_ptr<pv::data::AnalogSegment> > &segments = | |
712 | base_->analog_data()->analog_segments(); | |
713 | ||
714 | if (!segments.empty()) { | |
715 | if (segment_display_mode_ == ShowLastSegmentOnly) | |
716 | segment = segments.back(); | |
717 | ||
718 | if ((segment_display_mode_ == ShowSingleSegmentOnly) || | |
719 | (segment_display_mode_ == ShowLastCompleteSegmentOnly)) { | |
720 | try { | |
721 | segment = segments.at(current_segment_); | |
722 | } catch (out_of_range&) { | |
723 | qDebug() << "Current analog segment out of range for signal" << base_->name() << ":" << current_segment_; | |
724 | } | |
725 | } | |
726 | } | |
727 | ||
728 | return segment; | |
729 | } | |
730 | ||
731 | shared_ptr<pv::data::LogicSegment> AnalogSignal::get_logic_segment_to_paint() const | |
732 | { | |
733 | shared_ptr<pv::data::LogicSegment> segment; | |
734 | ||
735 | const deque< shared_ptr<pv::data::LogicSegment> > &segments = | |
736 | base_->logic_data()->logic_segments(); | |
737 | ||
738 | if (!segments.empty()) { | |
739 | if (segment_display_mode_ == ShowLastSegmentOnly) | |
740 | segment = segments.back(); | |
741 | ||
742 | if ((segment_display_mode_ == ShowSingleSegmentOnly) || | |
743 | (segment_display_mode_ == ShowLastCompleteSegmentOnly)) { | |
744 | try { | |
745 | segment = segments.at(current_segment_); | |
746 | } catch (out_of_range&) { | |
747 | qDebug() << "Current logic segment out of range for signal" << base_->name() << ":" << current_segment_; | |
748 | } | |
749 | } | |
750 | } | |
751 | ||
752 | return segment; | |
753 | } | |
754 | ||
368a37c2 | 755 | float AnalogSignal::get_resolution(int scale_index) |
8a2fafcc JH |
756 | { |
757 | const float seq[] = {1.0f, 2.0f, 5.0f}; | |
834a4f1b | 758 | |
6f925ba9 UH |
759 | const int offset = numeric_limits<int>::max() / (2 * countof(seq)); |
760 | const div_t d = div((int)(scale_index + countof(seq) * offset), | |
8a2fafcc | 761 | countof(seq)); |
834a4f1b | 762 | |
368a37c2 SA |
763 | return powf(10.0f, d.quot - offset) * seq[d.rem]; |
764 | } | |
765 | ||
766 | void AnalogSignal::update_scale() | |
767 | { | |
768 | resolution_ = get_resolution(scale_index_); | |
834a4f1b | 769 | scale_ = div_height_ / resolution_; |
8a2fafcc JH |
770 | } |
771 | ||
3782d860 | 772 | void AnalogSignal::update_conversion_widgets() |
a8fd2759 | 773 | { |
3782d860 | 774 | SignalBase::ConversionType conv_type = base_->get_conversion_type(); |
a8fd2759 | 775 | |
3782d860 UH |
776 | // Enable or disable widgets depending on conversion state |
777 | conv_threshold_cb_->setEnabled(conv_type != SignalBase::NoConversion); | |
778 | display_type_cb_->setEnabled(conv_type != SignalBase::NoConversion); | |
779 | ||
780 | conv_threshold_cb_->clear(); | |
781 | ||
782 | vector < pair<QString, int> > presets = base_->get_conversion_presets(); | |
783 | ||
784 | // Prevent the combo box from firing the "edit text changed" signal | |
785 | // as that would involuntarily select the first entry | |
786 | conv_threshold_cb_->blockSignals(true); | |
787 | ||
788 | // Set available options depending on chosen conversion | |
789 | for (pair<QString, int>& preset : presets) | |
790 | conv_threshold_cb_->addItem(preset.first, preset.second); | |
791 | ||
792 | map < QString, QVariant > options = base_->get_conversion_options(); | |
793 | ||
794 | if (conv_type == SignalBase::A2LConversionByThreshold) { | |
795 | const vector<double> thresholds = base_->get_conversion_thresholds( | |
796 | SignalBase::A2LConversionByThreshold, true); | |
797 | conv_threshold_cb_->addItem( | |
798 | QString("%1V").arg(QString::number(thresholds[0], 'f', 1)), -1); | |
799 | } | |
800 | ||
801 | if (conv_type == SignalBase::A2LConversionBySchmittTrigger) { | |
802 | const vector<double> thresholds = base_->get_conversion_thresholds( | |
803 | SignalBase::A2LConversionBySchmittTrigger, true); | |
804 | conv_threshold_cb_->addItem(QString("%1V/%2V").arg( | |
805 | QString::number(thresholds[0], 'f', 1), | |
806 | QString::number(thresholds[1], 'f', 1)), -1); | |
807 | } | |
808 | ||
809 | int preset_id = base_->get_current_conversion_preset(); | |
810 | conv_threshold_cb_->setCurrentIndex( | |
811 | conv_threshold_cb_->findData(preset_id)); | |
812 | ||
813 | conv_threshold_cb_->blockSignals(false); | |
814 | } | |
815 | ||
816 | vector<data::LogicSegment::EdgePair> AnalogSignal::get_nearest_level_changes(uint64_t sample_pos) | |
817 | { | |
818 | assert(base_); | |
819 | assert(owner_); | |
820 | ||
821 | // Return if there's no logic data or we're showing only the analog trace | |
822 | if (!base_->logic_data() || (display_type_ == DisplayAnalog)) | |
823 | return vector<data::LogicSegment::EdgePair>(); | |
824 | ||
825 | if (sample_pos == 0) | |
826 | return vector<LogicSegment::EdgePair>(); | |
827 | ||
828 | shared_ptr<LogicSegment> segment = get_logic_segment_to_paint(); | |
829 | if (!segment || (segment->get_sample_count() == 0)) | |
830 | return vector<LogicSegment::EdgePair>(); | |
831 | ||
832 | const View *view = owner_->view(); | |
833 | assert(view); | |
834 | const double samples_per_pixel = base_->get_samplerate() * view->scale(); | |
835 | ||
836 | vector<LogicSegment::EdgePair> edges; | |
837 | ||
838 | segment->get_surrounding_edges(edges, sample_pos, | |
839 | samples_per_pixel / LogicSignal::Oversampling, 0); | |
840 | ||
841 | if (edges.empty()) | |
842 | return vector<LogicSegment::EdgePair>(); | |
843 | ||
844 | return edges; | |
a8fd2759 SA |
845 | } |
846 | ||
430b94aa | 847 | void AnalogSignal::perform_autoranging(bool keep_divs, bool force_update) |
73e377fe SA |
848 | { |
849 | const deque< shared_ptr<pv::data::AnalogSegment> > &segments = | |
850 | base_->analog_data()->analog_segments(); | |
851 | ||
852 | if (segments.empty()) | |
853 | return; | |
854 | ||
10f5e6f5 | 855 | double signal_min_ = 0, signal_max_ = 0; |
73e377fe SA |
856 | double min = 0, max = 0; |
857 | ||
3782d860 | 858 | for (const shared_ptr<pv::data::AnalogSegment>& segment : segments) { |
6f925ba9 | 859 | pair<double, double> mm = segment->get_min_max(); |
73e377fe SA |
860 | min = std::min(min, mm.first); |
861 | max = std::max(max, mm.second); | |
862 | } | |
863 | ||
10f5e6f5 | 864 | if ((min == signal_min_) && (max == signal_max_) && !force_update) |
73e377fe SA |
865 | return; |
866 | ||
10f5e6f5 UH |
867 | signal_min_ = min; |
868 | signal_max_ = max; | |
73e377fe | 869 | |
430b94aa SA |
870 | // If we're allowed to alter the div assignment... |
871 | if (!keep_divs) { | |
872 | // Use all divs for the positive range if there are no negative values | |
873 | if ((min == 0) && (neg_vdivs_ > 0)) { | |
874 | pos_vdivs_ += neg_vdivs_; | |
875 | neg_vdivs_ = 0; | |
876 | } | |
73e377fe | 877 | |
430b94aa SA |
878 | // Split up the divs if there are negative values but no negative divs |
879 | if ((min < 0) && (neg_vdivs_ == 0)) { | |
880 | neg_vdivs_ = pos_vdivs_ / 2; | |
881 | pos_vdivs_ -= neg_vdivs_; | |
882 | } | |
681b6d5a SA |
883 | } |
884 | ||
430b94aa SA |
885 | // If there is still no positive div when we need it, add one |
886 | // (this can happen when pos_vdivs==neg_vdivs==0) | |
17c7f31c | 887 | if ((max > 0) && (pos_vdivs_ == 0)) { |
430b94aa | 888 | pos_vdivs_ = 1; |
17c7f31c SA |
889 | owner_->extents_changed(false, true); |
890 | } | |
430b94aa SA |
891 | |
892 | // If there is still no negative div when we need it, add one | |
893 | // (this can happen when pos_vdivs was 0 or 1 when trying to split) | |
17c7f31c | 894 | if ((min < 0) && (neg_vdivs_ == 0)) { |
430b94aa | 895 | neg_vdivs_ = 1; |
17c7f31c SA |
896 | owner_->extents_changed(false, true); |
897 | } | |
430b94aa | 898 | |
73e377fe SA |
899 | double min_value_per_div; |
900 | if ((pos_vdivs_ > 0) && (neg_vdivs_ > 0)) | |
901 | min_value_per_div = std::max(max / pos_vdivs_, -min / neg_vdivs_); | |
902 | else if (pos_vdivs_ > 0) | |
903 | min_value_per_div = max / pos_vdivs_; | |
904 | else | |
905 | min_value_per_div = -min / neg_vdivs_; | |
906 | ||
907 | // Find first scale value that is bigger than the value we need | |
908 | for (int i = MinScaleIndex; i < MaxScaleIndex; i++) | |
909 | if (get_resolution(i) > min_value_per_div) { | |
910 | scale_index_ = i; | |
911 | break; | |
912 | } | |
913 | ||
914 | update_scale(); | |
915 | } | |
916 | ||
3782d860 UH |
917 | void AnalogSignal::reset_pixel_values() |
918 | { | |
919 | value_at_pixel_pos_.clear(); | |
920 | current_pixel_pos_ = -1; | |
921 | prev_value_at_pixel_ = std::numeric_limits<float>::quiet_NaN(); | |
922 | } | |
923 | ||
924 | void AnalogSignal::process_next_sample_value(float x, float value) | |
925 | { | |
926 | // Note: NAN is used to indicate the non-existance of a value at this pixel | |
927 | ||
928 | if (std::isnan(prev_value_at_pixel_)) { | |
929 | if (x < 0) { | |
930 | min_value_at_pixel_ = value; | |
931 | max_value_at_pixel_ = value; | |
932 | prev_value_at_pixel_ = value; | |
933 | current_pixel_pos_ = x; | |
934 | } else | |
935 | prev_value_at_pixel_ = std::numeric_limits<float>::quiet_NaN(); | |
936 | } | |
937 | ||
938 | const int pixel_pos = (int)(x + 0.5); | |
939 | ||
940 | if (pixel_pos > current_pixel_pos_) { | |
941 | if (pixel_pos - current_pixel_pos_ == 1) { | |
942 | if (std::isnan(prev_value_at_pixel_)) { | |
943 | value_at_pixel_pos_.push_back(prev_value_at_pixel_); | |
944 | } else { | |
945 | // Average the min/max range to create one value for the previous pixel | |
946 | const float avg = (min_value_at_pixel_ + max_value_at_pixel_) / 2; | |
947 | value_at_pixel_pos_.push_back(avg); | |
948 | } | |
949 | } else { | |
950 | // Interpolate values to create values for the intermediate pixels | |
951 | const float start_value = prev_value_at_pixel_; | |
952 | const float end_value = value; | |
953 | const int steps = fabs(pixel_pos - current_pixel_pos_); | |
954 | const double gradient = (end_value - start_value) / steps; | |
955 | for (int i = 0; i < steps; i++) { | |
956 | if (current_pixel_pos_ + i < 0) | |
957 | continue; | |
958 | value_at_pixel_pos_.push_back(start_value + i * gradient); | |
959 | } | |
960 | } | |
961 | ||
962 | min_value_at_pixel_ = value; | |
963 | max_value_at_pixel_ = value; | |
964 | prev_value_at_pixel_ = value; | |
965 | current_pixel_pos_ = pixel_pos; | |
966 | } else { | |
967 | // Another sample for the same pixel | |
968 | if (value < min_value_at_pixel_) | |
969 | min_value_at_pixel_ = value; | |
970 | if (value > max_value_at_pixel_) | |
971 | max_value_at_pixel_ = value; | |
972 | } | |
973 | } | |
974 | ||
4cffac16 SA |
975 | void AnalogSignal::populate_popup_form(QWidget *parent, QFormLayout *form) |
976 | { | |
977 | // Add the standard options | |
978 | Signal::populate_popup_form(parent, form); | |
979 | ||
368a37c2 SA |
980 | QFormLayout *const layout = new QFormLayout; |
981 | ||
3782d860 | 982 | // Add div-related settings |
430b94aa SA |
983 | pvdiv_sb_ = new QSpinBox(parent); |
984 | pvdiv_sb_->setRange(0, MaximumVDivs); | |
985 | pvdiv_sb_->setValue(pos_vdivs_); | |
986 | connect(pvdiv_sb_, SIGNAL(valueChanged(int)), | |
459db2c5 | 987 | this, SLOT(on_pos_vdivs_changed(int))); |
430b94aa | 988 | layout->addRow(tr("Number of pos vertical divs"), pvdiv_sb_); |
459db2c5 | 989 | |
430b94aa SA |
990 | nvdiv_sb_ = new QSpinBox(parent); |
991 | nvdiv_sb_->setRange(0, MaximumVDivs); | |
992 | nvdiv_sb_->setValue(neg_vdivs_); | |
993 | connect(nvdiv_sb_, SIGNAL(valueChanged(int)), | |
459db2c5 | 994 | this, SLOT(on_neg_vdivs_changed(int))); |
430b94aa | 995 | layout->addRow(tr("Number of neg vertical divs"), nvdiv_sb_); |
368a37c2 | 996 | |
3782d860 UH |
997 | div_height_sb_ = new QSpinBox(parent); |
998 | div_height_sb_->setRange(20, 1000); | |
999 | div_height_sb_->setSingleStep(5); | |
1000 | div_height_sb_->setSuffix(tr(" pixels")); | |
1001 | div_height_sb_->setValue(div_height_); | |
1002 | connect(div_height_sb_, SIGNAL(valueChanged(int)), | |
1003 | this, SLOT(on_div_height_changed(int))); | |
1004 | layout->addRow(tr("Div height"), div_height_sb_); | |
1005 | ||
368a37c2 SA |
1006 | // Add the vertical resolution |
1007 | resolution_cb_ = new QComboBox(parent); | |
1008 | ||
1009 | for (int i = MinScaleIndex; i < MaxScaleIndex; i++) { | |
1010 | const QString label = QString("%1").arg(get_resolution(i)); | |
1011 | resolution_cb_->insertItem(0, label, QVariant(i)); | |
1012 | } | |
1013 | ||
a970015f | 1014 | int cur_idx = resolution_cb_->findData(QVariant(scale_index_)); |
368a37c2 SA |
1015 | resolution_cb_->setCurrentIndex(cur_idx); |
1016 | ||
1017 | connect(resolution_cb_, SIGNAL(currentIndexChanged(int)), | |
1018 | this, SLOT(on_resolution_changed(int))); | |
1019 | ||
1020 | QGridLayout *const vdiv_layout = new QGridLayout; | |
1021 | QLabel *const vdiv_unit = new QLabel(tr("V/div")); | |
1022 | vdiv_layout->addWidget(resolution_cb_, 0, 0); | |
1023 | vdiv_layout->addWidget(vdiv_unit, 0, 1); | |
1024 | ||
1025 | layout->addRow(tr("Vertical resolution"), vdiv_layout); | |
1026 | ||
73e377fe SA |
1027 | // Add the autoranging checkbox |
1028 | QCheckBox* autoranging_cb = new QCheckBox(); | |
1029 | autoranging_cb->setCheckState(autoranging_ ? Qt::Checked : Qt::Unchecked); | |
1030 | ||
1031 | connect(autoranging_cb, SIGNAL(stateChanged(int)), | |
1032 | this, SLOT(on_autoranging_changed(int))); | |
1033 | ||
1034 | layout->addRow(tr("Autoranging"), autoranging_cb); | |
1035 | ||
a970015f SA |
1036 | // Add the conversion type dropdown |
1037 | conversion_cb_ = new QComboBox(); | |
1038 | ||
3782d860 UH |
1039 | conversion_cb_->addItem(tr("none"), |
1040 | SignalBase::NoConversion); | |
1041 | conversion_cb_->addItem(tr("to logic via threshold"), | |
1042 | SignalBase::A2LConversionByThreshold); | |
1043 | conversion_cb_->addItem(tr("to logic via schmitt-trigger"), | |
1044 | SignalBase::A2LConversionBySchmittTrigger); | |
a970015f | 1045 | |
3782d860 | 1046 | cur_idx = conversion_cb_->findData(QVariant(base_->get_conversion_type())); |
a970015f SA |
1047 | conversion_cb_->setCurrentIndex(cur_idx); |
1048 | ||
3782d860 | 1049 | layout->addRow(tr("Conversion"), conversion_cb_); |
a970015f SA |
1050 | |
1051 | connect(conversion_cb_, SIGNAL(currentIndexChanged(int)), | |
1052 | this, SLOT(on_conversion_changed(int))); | |
1053 | ||
3782d860 UH |
1054 | // Add the conversion threshold settings |
1055 | conv_threshold_cb_ = new QComboBox(); | |
1056 | conv_threshold_cb_->setEditable(true); | |
1057 | ||
1058 | layout->addRow(tr("Conversion threshold(s)"), conv_threshold_cb_); | |
1059 | ||
1060 | connect(conv_threshold_cb_, SIGNAL(currentIndexChanged(int)), | |
1061 | this, SLOT(on_conv_threshold_changed(int))); | |
1062 | connect(conv_threshold_cb_, SIGNAL(editTextChanged(const QString&)), | |
1063 | this, SLOT(on_conv_threshold_changed())); // index will be -1 | |
1064 | ||
a970015f SA |
1065 | // Add the display type dropdown |
1066 | display_type_cb_ = new QComboBox(); | |
1067 | ||
3782d860 UH |
1068 | display_type_cb_->addItem(tr("analog"), DisplayAnalog); |
1069 | display_type_cb_->addItem(tr("converted"), DisplayConverted); | |
1070 | display_type_cb_->addItem(tr("analog+converted"), DisplayBoth); | |
a970015f SA |
1071 | |
1072 | cur_idx = display_type_cb_->findData(QVariant(display_type_)); | |
1073 | display_type_cb_->setCurrentIndex(cur_idx); | |
1074 | ||
3782d860 | 1075 | layout->addRow(tr("Show traces for"), display_type_cb_); |
a970015f | 1076 | |
b8c4a95b SA |
1077 | connect(display_type_cb_, SIGNAL(currentIndexChanged(int)), |
1078 | this, SLOT(on_display_type_changed(int))); | |
1079 | ||
3782d860 UH |
1080 | // Update the conversion widget contents and states |
1081 | update_conversion_widgets(); | |
1082 | ||
368a37c2 | 1083 | form->addRow(layout); |
4cffac16 SA |
1084 | } |
1085 | ||
3782d860 UH |
1086 | void AnalogSignal::hover_point_changed(const QPoint &hp) |
1087 | { | |
1088 | Signal::hover_point_changed(hp); | |
1089 | ||
1090 | // Note: Even though the view area begins at 0, we exclude 0 because | |
1091 | // that's also the value given when the cursor is over the header to the | |
1092 | // left of the trace paint area | |
1093 | if (hp.x() <= 0) { | |
1094 | value_at_hover_pos_ = std::numeric_limits<float>::quiet_NaN(); | |
1095 | } else { | |
10f5e6f5 | 1096 | if ((size_t)hp.x() < value_at_pixel_pos_.size()) |
3782d860 | 1097 | value_at_hover_pos_ = value_at_pixel_pos_.at(hp.x()); |
10f5e6f5 | 1098 | else |
3782d860 | 1099 | value_at_hover_pos_ = std::numeric_limits<float>::quiet_NaN(); |
3782d860 UH |
1100 | } |
1101 | } | |
1102 | ||
1103 | void AnalogSignal::on_setting_changed(const QString &key, const QVariant &value) | |
85715407 | 1104 | { |
3782d860 UH |
1105 | Signal::on_setting_changed(key, value); |
1106 | ||
1107 | if (key == GlobalSettings::Key_View_ShowSamplingPoints) | |
1108 | show_sampling_points_ = value.toBool(); | |
1109 | ||
1110 | if (key == GlobalSettings::Key_View_FillSignalHighAreas) | |
1111 | fill_high_areas_ = value.toBool(); | |
1112 | ||
1113 | if (key == GlobalSettings::Key_View_FillSignalHighAreaColor) | |
1114 | high_fill_color_ = QColor::fromRgba(value.value<uint32_t>()); | |
1115 | ||
1116 | if (key == GlobalSettings::Key_View_ShowAnalogMinorGrid) | |
1117 | show_analog_minor_grid_ = value.toBool(); | |
1118 | ||
1119 | if (key == GlobalSettings::Key_View_ConversionThresholdDispMode) { | |
1120 | conversion_threshold_disp_mode_ = value.toInt(); | |
1121 | ||
1122 | if (owner_) | |
1123 | owner_->row_item_appearance_changed(false, true); | |
1124 | } | |
1125 | } | |
1126 | ||
1127 | void AnalogSignal::on_min_max_changed(float min, float max) | |
1128 | { | |
1129 | (void)min; | |
1130 | (void)max; | |
1131 | ||
1132 | if (autoranging_) | |
1133 | perform_autoranging(false, false); | |
85715407 SA |
1134 | } |
1135 | ||
459db2c5 SA |
1136 | void AnalogSignal::on_pos_vdivs_changed(int vdivs) |
1137 | { | |
430b94aa SA |
1138 | if (vdivs == pos_vdivs_) |
1139 | return; | |
1140 | ||
459db2c5 SA |
1141 | pos_vdivs_ = vdivs; |
1142 | ||
80067e49 SA |
1143 | // There has to be at least one div, positive or negative |
1144 | if ((neg_vdivs_ == 0) && (pos_vdivs_ == 0)) { | |
1145 | pos_vdivs_ = 1; | |
1146 | if (pvdiv_sb_) | |
1147 | pvdiv_sb_->setValue(pos_vdivs_); | |
1148 | } | |
1149 | ||
430b94aa SA |
1150 | if (autoranging_) { |
1151 | perform_autoranging(true, true); | |
1152 | ||
1153 | // It could be that a positive or negative div was added, so update | |
1154 | if (pvdiv_sb_) { | |
1155 | pvdiv_sb_->setValue(pos_vdivs_); | |
1156 | nvdiv_sb_->setValue(neg_vdivs_); | |
1157 | } | |
1158 | } | |
73e377fe | 1159 | |
459db2c5 SA |
1160 | if (owner_) { |
1161 | // Call order is important, otherwise the lazy event handler won't work | |
1162 | owner_->extents_changed(false, true); | |
1163 | owner_->row_item_appearance_changed(false, true); | |
1164 | } | |
1165 | } | |
1166 | ||
1167 | void AnalogSignal::on_neg_vdivs_changed(int vdivs) | |
4cffac16 | 1168 | { |
430b94aa SA |
1169 | if (vdivs == neg_vdivs_) |
1170 | return; | |
1171 | ||
459db2c5 | 1172 | neg_vdivs_ = vdivs; |
4cffac16 | 1173 | |
80067e49 SA |
1174 | // There has to be at least one div, positive or negative |
1175 | if ((neg_vdivs_ == 0) && (pos_vdivs_ == 0)) { | |
1176 | pos_vdivs_ = 1; | |
1177 | if (pvdiv_sb_) | |
1178 | pvdiv_sb_->setValue(pos_vdivs_); | |
1179 | } | |
1180 | ||
430b94aa SA |
1181 | if (autoranging_) { |
1182 | perform_autoranging(true, true); | |
1183 | ||
1184 | // It could be that a positive or negative div was added, so update | |
1185 | if (pvdiv_sb_) { | |
1186 | pvdiv_sb_->setValue(pos_vdivs_); | |
1187 | nvdiv_sb_->setValue(neg_vdivs_); | |
1188 | } | |
1189 | } | |
73e377fe | 1190 | |
75d0779e SA |
1191 | if (owner_) { |
1192 | // Call order is important, otherwise the lazy event handler won't work | |
4cffac16 | 1193 | owner_->extents_changed(false, true); |
75d0779e SA |
1194 | owner_->row_item_appearance_changed(false, true); |
1195 | } | |
4cffac16 SA |
1196 | } |
1197 | ||
3782d860 UH |
1198 | void AnalogSignal::on_div_height_changed(int height) |
1199 | { | |
1200 | div_height_ = height; | |
1201 | update_scale(); | |
1202 | ||
1203 | if (owner_) { | |
1204 | // Call order is important, otherwise the lazy event handler won't work | |
1205 | owner_->extents_changed(false, true); | |
1206 | owner_->row_item_appearance_changed(false, true); | |
1207 | } | |
1208 | } | |
1209 | ||
368a37c2 SA |
1210 | void AnalogSignal::on_resolution_changed(int index) |
1211 | { | |
1212 | scale_index_ = resolution_cb_->itemData(index).toInt(); | |
1213 | update_scale(); | |
1214 | ||
1215 | if (owner_) | |
1216 | owner_->row_item_appearance_changed(false, true); | |
1217 | } | |
4cffac16 | 1218 | |
73e377fe SA |
1219 | void AnalogSignal::on_autoranging_changed(int state) |
1220 | { | |
1221 | autoranging_ = (state == Qt::Checked); | |
1222 | ||
1223 | if (autoranging_) | |
430b94aa | 1224 | perform_autoranging(false, true); |
73e377fe | 1225 | |
85715407 SA |
1226 | if (owner_) { |
1227 | // Call order is important, otherwise the lazy event handler won't work | |
1228 | owner_->extents_changed(false, true); | |
73e377fe | 1229 | owner_->row_item_appearance_changed(false, true); |
85715407 | 1230 | } |
73e377fe SA |
1231 | } |
1232 | ||
a970015f SA |
1233 | void AnalogSignal::on_conversion_changed(int index) |
1234 | { | |
3782d860 | 1235 | SignalBase::ConversionType old_conv_type = base_->get_conversion_type(); |
a8fd2759 | 1236 | |
3782d860 UH |
1237 | SignalBase::ConversionType conv_type = |
1238 | (SignalBase::ConversionType)(conversion_cb_->itemData(index).toInt()); | |
a8fd2759 | 1239 | |
3782d860 UH |
1240 | if (conv_type != old_conv_type) { |
1241 | base_->set_conversion_type(conv_type); | |
1242 | update_conversion_widgets(); | |
1243 | ||
1244 | if (owner_) | |
1245 | owner_->row_item_appearance_changed(false, true); | |
1246 | } | |
1247 | } | |
1248 | ||
1249 | void AnalogSignal::on_conv_threshold_changed(int index) | |
1250 | { | |
1251 | SignalBase::ConversionType conv_type = base_->get_conversion_type(); | |
1252 | ||
1253 | // Note: index is set to -1 if the text in the combo box matches none of | |
1254 | // the entries in the combo box | |
1255 | ||
1256 | if ((index == -1) && (conv_threshold_cb_->currentText().length() == 0)) | |
1257 | return; | |
1258 | ||
1259 | // The combo box entry with the custom value has user_data set to -1 | |
1260 | const int user_data = conv_threshold_cb_->findText( | |
1261 | conv_threshold_cb_->currentText()); | |
1262 | ||
1263 | const bool use_custom_thr = (index == -1) || (user_data == -1); | |
1264 | ||
1265 | if (conv_type == SignalBase::A2LConversionByThreshold && use_custom_thr) { | |
1266 | // Not one of the preset values, try to parse the combo box text | |
1267 | // Note: Regex loosely based on | |
1268 | // https://txt2re.com/index-c++.php3?s=0.1V&1&-13 | |
1269 | QString re1 = "([+-]?\\d*[\\.,]?\\d*)"; // Float value | |
1270 | QString re2 = "([a-zA-Z]*)"; // SI unit | |
1271 | QRegExp regex(re1 + re2); | |
1272 | ||
1273 | const QString text = conv_threshold_cb_->currentText(); | |
1274 | if (!regex.exactMatch(text)) | |
1275 | return; // String doesn't match the regex | |
1276 | ||
1277 | QStringList tokens = regex.capturedTexts(); | |
1278 | ||
1279 | // For now, we simply assume that the unit is volt without modifiers | |
1280 | const double thr = tokens.at(1).toDouble(); | |
1281 | ||
1282 | // Only restart the conversion if the threshold was updated. | |
1283 | // We're starting a delayed conversion because the user may still be | |
1284 | // typing and the UI would lag if we kept on restarting it immediately | |
1285 | if (base_->set_conversion_option("threshold_value", thr)) | |
1286 | base_->start_conversion(true); | |
1287 | } | |
1288 | ||
1289 | if (conv_type == SignalBase::A2LConversionBySchmittTrigger && use_custom_thr) { | |
1290 | // Not one of the preset values, try to parse the combo box text | |
1291 | // Note: Regex loosely based on | |
1292 | // https://txt2re.com/index-c++.php3?s=0.1V/0.2V&2&14&-22&3&15 | |
1293 | QString re1 = "([+-]?\\d*[\\.,]?\\d*)"; // Float value | |
1294 | QString re2 = "([a-zA-Z]*)"; // SI unit | |
1295 | QString re3 = "\\/"; // Forward slash, not captured | |
1296 | QString re4 = "([+-]?\\d*[\\.,]?\\d*)"; // Float value | |
1297 | QString re5 = "([a-zA-Z]*)"; // SI unit | |
1298 | QRegExp regex(re1 + re2 + re3 + re4 + re5); | |
1299 | ||
1300 | const QString text = conv_threshold_cb_->currentText(); | |
1301 | if (!regex.exactMatch(text)) | |
1302 | return; // String doesn't match the regex | |
1303 | ||
1304 | QStringList tokens = regex.capturedTexts(); | |
1305 | ||
1306 | // For now, we simply assume that the unit is volt without modifiers | |
1307 | const double low_thr = tokens.at(1).toDouble(); | |
1308 | const double high_thr = tokens.at(3).toDouble(); | |
1309 | ||
1310 | // Only restart the conversion if one of the options was updated. | |
1311 | // We're starting a delayed conversion because the user may still be | |
1312 | // typing and the UI would lag if we kept on restarting it immediately | |
1313 | bool o1 = base_->set_conversion_option("threshold_value_low", low_thr); | |
1314 | bool o2 = base_->set_conversion_option("threshold_value_high", high_thr); | |
1315 | if (o1 || o2) | |
1316 | base_->start_conversion(true); // Start delayed conversion | |
a8fd2759 | 1317 | } |
3782d860 UH |
1318 | |
1319 | base_->set_conversion_preset((SignalBase::ConversionPreset)index); | |
1320 | ||
1321 | // Immediately start the conversion if we're not using custom values | |
1322 | // (i.e. we're using one of the presets) | |
1323 | if (!use_custom_thr) | |
1324 | base_->start_conversion(); | |
1325 | } | |
1326 | ||
1327 | void AnalogSignal::on_delayed_conversion_starter() | |
1328 | { | |
1329 | base_->start_conversion(); | |
a970015f SA |
1330 | } |
1331 | ||
b8c4a95b SA |
1332 | void AnalogSignal::on_display_type_changed(int index) |
1333 | { | |
1334 | display_type_ = (DisplayType)(display_type_cb_->itemData(index).toInt()); | |
1335 | ||
1336 | if (owner_) | |
1337 | owner_->row_item_appearance_changed(false, true); | |
1338 | } | |
1339 | ||
1573bf16 | 1340 | } // namespace trace |
f4e57597 | 1341 | } // namespace views |
aba1dd16 | 1342 | } // namespace pv |