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