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