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