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