]>
Commit | Line | Data |
---|---|---|
55d3603d 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/>. |
55d3603d JH |
18 | */ |
19 | ||
20 | extern "C" { | |
21 | #include <libsigrokdecode/libsigrokdecode.h> | |
22 | } | |
23 | ||
99ba5f28 | 24 | #include <limits> |
c3a740dd | 25 | #include <mutex> |
5c48ce32 | 26 | #include <tuple> |
c3a740dd | 27 | |
06bb4e6a JH |
28 | #include <extdef.h> |
29 | ||
a855d71e | 30 | #include <boost/functional/hash.hpp> |
b213ef09 | 31 | |
c51482b3 | 32 | #include <QAction> |
d7c0ca4a | 33 | #include <QApplication> |
c764c995 | 34 | #include <QClipboard> |
c6b4e925 | 35 | #include <QCheckBox> |
4e5a4405 | 36 | #include <QComboBox> |
be843692 | 37 | #include <QFileDialog> |
4e5a4405 JH |
38 | #include <QFormLayout> |
39 | #include <QLabel> | |
b213ef09 | 40 | #include <QMenu> |
be843692 | 41 | #include <QMessageBox> |
ce94e4fd | 42 | #include <QPushButton> |
be843692 | 43 | #include <QTextStream> |
e2f90c50 | 44 | #include <QToolTip> |
c51482b3 | 45 | |
2acdb232 | 46 | #include "decodetrace.hpp" |
1573bf16 SA |
47 | #include "view.hpp" |
48 | #include "viewport.hpp" | |
2acdb232 | 49 | |
1cc1c8de | 50 | #include <pv/globalsettings.hpp> |
ad908057 SA |
51 | #include <pv/session.hpp> |
52 | #include <pv/strnatcmp.hpp> | |
53 | #include <pv/data/decodesignal.hpp> | |
aca9aa83 | 54 | #include <pv/data/decode/annotation.hpp> |
2acdb232 JH |
55 | #include <pv/data/decode/decoder.hpp> |
56 | #include <pv/data/logic.hpp> | |
f3d66e52 | 57 | #include <pv/data/logicsegment.hpp> |
2acdb232 JH |
58 | #include <pv/widgets/decodergroupbox.hpp> |
59 | #include <pv/widgets/decodermenu.hpp> | |
119aff65 | 60 | |
5c48ce32 | 61 | using std::abs; |
84002113 SA |
62 | using std::find_if; |
63 | using std::lock_guard; | |
7f8517f6 | 64 | using std::make_pair; |
819f4c25 | 65 | using std::max; |
819f4c25 | 66 | using std::min; |
99ba5f28 | 67 | using std::numeric_limits; |
6f925ba9 | 68 | using std::out_of_range; |
7f8517f6 | 69 | using std::pair; |
f9abf97e | 70 | using std::shared_ptr; |
53e35b2d | 71 | using std::tie; |
819f4c25 | 72 | using std::vector; |
55d3603d | 73 | |
ecd07c20 | 74 | using pv::data::decode::Annotation; |
6a26fc44 | 75 | using pv::data::decode::AnnotationClass; |
ecd07c20 | 76 | using pv::data::decode::Row; |
a82325d1 | 77 | using pv::data::decode::DecodeChannel; |
9f97b357 | 78 | using pv::data::DecodeSignal; |
ecd07c20 | 79 | |
55d3603d | 80 | namespace pv { |
f4e57597 | 81 | namespace views { |
1573bf16 | 82 | namespace trace { |
55d3603d | 83 | |
9ba13f5e SA |
84 | |
85 | #define DECODETRACE_COLOR_SATURATION (180) /* 0-255 */ | |
86 | #define DECODETRACE_COLOR_VALUE (170) /* 0-255 */ | |
06bb4e6a | 87 | |
641574bc SA |
88 | const QColor DecodeTrace::ErrorBgColor = QColor(0xEF, 0x29, 0x29); |
89 | const QColor DecodeTrace::NoDecodeColor = QColor(0x88, 0x8A, 0x85); | |
81dc0221 | 90 | const QColor DecodeTrace::ExpandMarkerWarnColor = QColor(0xFF, 0xA5, 0x00); // QColorConstants::Svg::orange |
c6b4e925 SA |
91 | const uint8_t DecodeTrace::ExpansionAreaHeaderAlpha = 10 * 255 / 100; |
92 | const uint8_t DecodeTrace::ExpansionAreaAlpha = 5 * 255 / 100; | |
ad50ac1a | 93 | |
74bf6666 | 94 | const int DecodeTrace::ArrowSize = 6; |
06e810f2 | 95 | const double DecodeTrace::EndCapWidth = 5; |
74bf6666 | 96 | const int DecodeTrace::RowTitleMargin = 7; |
06e810f2 JH |
97 | const int DecodeTrace::DrawPadding = 100; |
98 | ||
5b6ae103 | 99 | const int DecodeTrace::MaxTraceUpdateRate = 1; // No more than 1 Hz |
74bf6666 | 100 | const unsigned int DecodeTrace::AnimationDurationInTicks = 7; |
5b6ae103 | 101 | |
2b81ae46 | 102 | DecodeTrace::DecodeTrace(pv::Session &session, |
bb7dd726 | 103 | shared_ptr<data::SignalBase> signalbase, int index) : |
bf0edd2b | 104 | Trace(signalbase), |
8dbbc7f0 | 105 | session_(session), |
eee89ff8 | 106 | max_visible_rows_(0), |
8dbbc7f0 | 107 | delete_mapper_(this), |
c6b4e925 SA |
108 | show_hide_mapper_(this), |
109 | row_show_hide_mapper_(this) | |
55d3603d | 110 | { |
ad908057 | 111 | decode_signal_ = dynamic_pointer_cast<data::DecodeSignal>(base_); |
e0fc5810 | 112 | |
ab185f78 SA |
113 | GlobalSettings settings; |
114 | always_show_all_rows_ = settings.value(GlobalSettings::Key_Dec_AlwaysShowAllRows).toBool(); | |
115 | ||
116 | GlobalSettings::add_change_handler(this); | |
117 | ||
752281db SA |
118 | // Determine shortest string we want to see displayed in full |
119 | QFontMetrics m(QApplication::font()); | |
120 | min_useful_label_width_ = m.width("XX"); // e.g. two hex characters | |
121 | ||
84002113 | 122 | default_row_height_ = (ViewItemPaintParams::text_height() * 6) / 4; |
9741d6e0 | 123 | annotation_height_ = (ViewItemPaintParams::text_height() * 5) / 4; |
84002113 | 124 | |
9ba13f5e SA |
125 | // For the base color, we want to start at a very different color for |
126 | // every decoder stack, so multiply the index with a number that is | |
127 | // rather close to 180 degrees of the color circle but not a dividend of 360 | |
128 | // Note: The offset equals the color of the first annotation | |
129 | QColor color; | |
130 | const int h = (120 + 160 * index) % 360; | |
131 | const int s = DECODETRACE_COLOR_SATURATION; | |
132 | const int v = DECODETRACE_COLOR_VALUE; | |
133 | color.setHsv(h, s, v); | |
134 | base_->set_color(color); | |
9cef9567 | 135 | |
ad908057 SA |
136 | connect(decode_signal_.get(), SIGNAL(new_annotations()), |
137 | this, SLOT(on_new_annotations())); | |
eee3eab9 SA |
138 | connect(decode_signal_.get(), SIGNAL(decode_reset()), |
139 | this, SLOT(on_decode_reset())); | |
1b56c646 SA |
140 | connect(decode_signal_.get(), SIGNAL(decode_finished()), |
141 | this, SLOT(on_decode_finished())); | |
9f97b357 SA |
142 | connect(decode_signal_.get(), SIGNAL(channels_updated()), |
143 | this, SLOT(on_channels_updated())); | |
144 | ||
8dbbc7f0 | 145 | connect(&delete_mapper_, SIGNAL(mapped(int)), |
613d097c | 146 | this, SLOT(on_delete_decoder(int))); |
8dbbc7f0 | 147 | connect(&show_hide_mapper_, SIGNAL(mapped(int)), |
dd048a7e | 148 | this, SLOT(on_show_hide_decoder(int))); |
c6b4e925 SA |
149 | connect(&row_show_hide_mapper_, SIGNAL(mapped(int)), |
150 | this, SLOT(on_show_hide_row(int))); | |
6a26fc44 SA |
151 | connect(&class_show_hide_mapper_, SIGNAL(mapped(QWidget*)), |
152 | this, SLOT(on_show_hide_class(QWidget*))); | |
5b6ae103 SA |
153 | |
154 | connect(&delayed_trace_updater_, SIGNAL(timeout()), | |
155 | this, SLOT(on_delayed_trace_update())); | |
156 | delayed_trace_updater_.setSingleShot(true); | |
157 | delayed_trace_updater_.setInterval(1000 / MaxTraceUpdateRate); | |
74bf6666 SA |
158 | |
159 | connect(&animation_timer_, SIGNAL(timeout()), | |
160 | this, SLOT(on_animation_timer())); | |
161 | animation_timer_.setInterval(1000 / 50); | |
162 | ||
163 | default_marker_shape_ << QPoint(0, -ArrowSize); | |
164 | default_marker_shape_ << QPoint(ArrowSize, 0); | |
165 | default_marker_shape_ << QPoint(0, ArrowSize); | |
55d3603d JH |
166 | } |
167 | ||
ab185f78 SA |
168 | DecodeTrace::~DecodeTrace() |
169 | { | |
170 | GlobalSettings::remove_change_handler(this); | |
dca3cbee | 171 | |
6a26fc44 | 172 | for (DecodeTraceRow& r : rows_) { |
c6b4e925 SA |
173 | for (QCheckBox* cb : r.selectors) |
174 | delete cb; | |
175 | ||
176 | delete r.selector_container; | |
177 | delete r.header_container; | |
dca3cbee | 178 | delete r.container; |
c6b4e925 | 179 | } |
ab185f78 SA |
180 | } |
181 | ||
b9329558 | 182 | bool DecodeTrace::enabled() const |
55d3603d JH |
183 | { |
184 | return true; | |
185 | } | |
186 | ||
6f925ba9 | 187 | shared_ptr<data::SignalBase> DecodeTrace::base() const |
b6b267bb | 188 | { |
bb7dd726 | 189 | return base_; |
b6b267bb JH |
190 | } |
191 | ||
a5d93c27 JH |
192 | pair<int, int> DecodeTrace::v_extents() const |
193 | { | |
e40a79cb | 194 | // Make an empty decode trace appear symmetrical |
84002113 SA |
195 | if (max_visible_rows_ == 0) |
196 | return make_pair(-default_row_height_, default_row_height_); | |
197 | ||
198 | unsigned int height = 0; | |
6a26fc44 | 199 | for (const DecodeTraceRow& r : rows_) |
84002113 SA |
200 | if (r.currently_visible) |
201 | height += r.height; | |
e40a79cb | 202 | |
84002113 | 203 | return make_pair(-default_row_height_, height); |
a5d93c27 JH |
204 | } |
205 | ||
60938e04 | 206 | void DecodeTrace::paint_back(QPainter &p, ViewItemPaintParams &pp) |
fe08b6e8 | 207 | { |
3eb29afd | 208 | Trace::paint_back(p, pp); |
97904bf7 | 209 | paint_axis(p, pp, get_visual_y()); |
fe08b6e8 JH |
210 | } |
211 | ||
60938e04 | 212 | void DecodeTrace::paint_mid(QPainter &p, ViewItemPaintParams &pp) |
55d3603d | 213 | { |
84002113 SA |
214 | lock_guard<mutex> lock(row_modification_mutex_); |
215 | ||
cd0c558b SA |
216 | // Set default pen to allow for text width calculation |
217 | p.setPen(Qt::black); | |
218 | ||
67fb15bf | 219 | pair<uint64_t, uint64_t> sample_range = get_view_sample_range(pp.left(), pp.right()); |
b82908ab SA |
220 | |
221 | // Just because the view says we see a certain sample range it | |
222 | // doesn't mean we have this many decoded samples, too, so crop | |
223 | // the range to what has been decoded already | |
224 | sample_range.second = min((int64_t)sample_range.second, | |
225 | decode_signal_->get_decoded_sample_count(current_segment_, false)); | |
5dfeb70f | 226 | |
84002113 SA |
227 | visible_rows_ = 0; |
228 | int y = get_visual_y(); | |
aee9dcf3 | 229 | |
6a26fc44 | 230 | for (DecodeTraceRow& r : rows_) { |
c6b4e925 | 231 | // If the row is hidden, we don't want to fetch annotations |
6a26fc44 SA |
232 | assert(r.decode_row); |
233 | assert(r.decode_row->decoder()); | |
5d3ca591 | 234 | if ((!r.decode_row->decoder()->visible()) || (!r.decode_row->visible())) { |
9741d6e0 SA |
235 | r.currently_visible = false; |
236 | continue; | |
237 | } | |
238 | ||
f9101a91 | 239 | vector<Annotation> annotations; |
84002113 | 240 | decode_signal_->get_annotation_subset(annotations, r.decode_row, |
72435789 | 241 | current_segment_, sample_range.first, sample_range.second); |
f994f496 | 242 | |
41293691 | 243 | // Show row if there are visible annotations, when user wants to see |
f994f496 | 244 | // all rows that have annotations somewhere and this one is one of them |
41293691 | 245 | // or when the row has at least one hidden annotation class |
6a26fc44 SA |
246 | r.currently_visible = !annotations.empty(); |
247 | if (!r.currently_visible) { | |
248 | size_t ann_count = decode_signal_->get_annotation_count(r.decode_row, current_segment_); | |
41293691 SA |
249 | r.currently_visible = (always_show_all_rows_ || r.has_hidden_classes) && |
250 | (ann_count > 0); | |
6a26fc44 | 251 | } |
f994f496 | 252 | |
84002113 | 253 | if (r.currently_visible) { |
9741d6e0 | 254 | draw_annotations(annotations, p, annotation_height_, pp, y, |
6a26fc44 | 255 | get_row_color(r.decode_row->index()), r.title_width); |
84002113 SA |
256 | y += r.height; |
257 | visible_rows_++; | |
f9101a91 | 258 | } |
7e674e43 | 259 | } |
5dfeb70f | 260 | |
9741d6e0 | 261 | draw_unresolved_period(p, annotation_height_, pp.left(), pp.right()); |
eee89ff8 | 262 | |
84002113 SA |
263 | if (visible_rows_ > max_visible_rows_) { |
264 | max_visible_rows_ = visible_rows_; | |
a303c2d8 | 265 | |
6d2802aa SA |
266 | // Call order is important, otherwise the lazy event handler won't work |
267 | owner_->extents_changed(false, true); | |
268 | owner_->row_item_appearance_changed(false, true); | |
269 | } | |
1ae18301 SA |
270 | |
271 | const QString err = decode_signal_->error_message(); | |
272 | if (!err.isEmpty()) | |
273 | draw_error(p, err, pp); | |
55d3603d JH |
274 | } |
275 | ||
60938e04 | 276 | void DecodeTrace::paint_fore(QPainter &p, ViewItemPaintParams &pp) |
88908838 | 277 | { |
84002113 | 278 | unsigned int y = get_visual_y(); |
88908838 | 279 | |
d4c3b059 SA |
280 | update_expanded_rows(); |
281 | ||
6a26fc44 | 282 | for (const DecodeTraceRow& r : rows_) { |
84002113 SA |
283 | if (!r.currently_visible) |
284 | continue; | |
88908838 JH |
285 | |
286 | p.setPen(QPen(Qt::NoPen)); | |
88908838 | 287 | |
84002113 | 288 | if (r.expand_marker_highlighted) |
74bf6666 | 289 | p.setBrush(QApplication::palette().brush(QPalette::Highlight)); |
81dc0221 SA |
290 | else if (r.has_hidden_classes) |
291 | p.setBrush(ExpandMarkerWarnColor); | |
84002113 SA |
292 | else |
293 | p.setBrush(QApplication::palette().brush(QPalette::WindowText)); | |
294 | ||
295 | // Draw expansion marker | |
74bf6666 SA |
296 | QPolygon marker(r.expand_marker_shape); |
297 | marker.translate(pp.left(), y); | |
298 | p.drawPolygon(marker); | |
88908838 | 299 | |
84002113 SA |
300 | p.setBrush(QApplication::palette().brush(QPalette::WindowText)); |
301 | ||
302 | const QRect text_rect(pp.left() + ArrowSize * 2, y - r.height / 2, | |
303 | pp.right() - pp.left(), r.height); | |
6a26fc44 | 304 | const QString h(r.decode_row->title()); |
88908838 JH |
305 | const int f = Qt::AlignLeft | Qt::AlignVCenter | |
306 | Qt::TextDontClip; | |
307 | ||
308 | // Draw the outline | |
309 | p.setPen(QApplication::palette().color(QPalette::Base)); | |
310 | for (int dx = -1; dx <= 1; dx++) | |
311 | for (int dy = -1; dy <= 1; dy++) | |
312 | if (dx != 0 && dy != 0) | |
84002113 | 313 | p.drawText(text_rect.translated(dx, dy), f, h); |
88908838 JH |
314 | |
315 | // Draw the text | |
316 | p.setPen(QApplication::palette().color(QPalette::WindowText)); | |
84002113 SA |
317 | p.drawText(text_rect, f, h); |
318 | ||
319 | y += r.height; | |
88908838 | 320 | } |
1931b5f9 SA |
321 | |
322 | if (show_hover_marker_) | |
323 | paint_hover_marker(p); | |
88908838 JH |
324 | } |
325 | ||
cf0a3c9c SA |
326 | void DecodeTrace::update_stack_button() |
327 | { | |
328 | const vector< shared_ptr<data::decode::Decoder> > &stack = decode_signal_->decoder_stack(); | |
329 | ||
330 | // Only show decoders in the menu that can be stacked onto the last one in the stack | |
331 | if (!stack.empty()) { | |
6a26fc44 | 332 | const srd_decoder* d = stack.back()->get_srd_decoder(); |
cf0a3c9c SA |
333 | |
334 | if (d->outputs) { | |
335 | pv::widgets::DecoderMenu *const decoder_menu = | |
336 | new pv::widgets::DecoderMenu(stack_button_, (const char*)(d->outputs->data)); | |
337 | connect(decoder_menu, SIGNAL(decoder_selected(srd_decoder*)), | |
338 | this, SLOT(on_stack_decoder(srd_decoder*))); | |
339 | ||
15784350 SA |
340 | decoder_menu->setStyleSheet("QMenu { menu-scrollable: 1; }"); |
341 | ||
cf0a3c9c SA |
342 | stack_button_->setMenu(decoder_menu); |
343 | stack_button_->show(); | |
344 | return; | |
345 | } | |
346 | } | |
347 | ||
348 | // No decoders available for stacking | |
349 | stack_button_->setMenu(nullptr); | |
350 | stack_button_->hide(); | |
351 | } | |
352 | ||
b9329558 | 353 | void DecodeTrace::populate_popup_form(QWidget *parent, QFormLayout *form) |
4e5a4405 | 354 | { |
613d097c JH |
355 | using pv::data::decode::Decoder; |
356 | ||
4e5a4405 | 357 | assert(form); |
4e5a4405 | 358 | |
7491a29f | 359 | // Add the standard options |
4e5a4405 JH |
360 | Trace::populate_popup_form(parent, form); |
361 | ||
7491a29f | 362 | // Add the decoder options |
8dbbc7f0 | 363 | bindings_.clear(); |
9f97b357 SA |
364 | channel_id_map_.clear(); |
365 | init_state_map_.clear(); | |
8dbbc7f0 | 366 | decoder_forms_.clear(); |
4e5a4405 | 367 | |
47747218 | 368 | const vector< shared_ptr<Decoder> > &stack = decode_signal_->decoder_stack(); |
4e5a4405 | 369 | |
2ad82c2e | 370 | if (stack.empty()) { |
5069084a JH |
371 | QLabel *const l = new QLabel( |
372 | tr("<p><i>No decoders in the stack</i></p>")); | |
373 | l->setAlignment(Qt::AlignCenter); | |
374 | form->addRow(l); | |
2ad82c2e | 375 | } else { |
f46e495e | 376 | auto iter = stack.cbegin(); |
5069084a JH |
377 | for (int i = 0; i < (int)stack.size(); i++, iter++) { |
378 | shared_ptr<Decoder> dec(*iter); | |
379 | create_decoder_form(i, dec, parent, form); | |
380 | } | |
381 | ||
382 | form->addRow(new QLabel( | |
8bd26d8b | 383 | tr("<i>* Required channels</i>"), parent)); |
5069084a | 384 | } |
4e5a4405 | 385 | |
ce94e4fd | 386 | // Add stacking button |
cf0a3c9c SA |
387 | stack_button_ = new QPushButton(tr("Stack Decoder"), parent); |
388 | stack_button_->setToolTip(tr("Stack a higher-level decoder on top of this one")); | |
389 | update_stack_button(); | |
ce94e4fd JH |
390 | |
391 | QHBoxLayout *stack_button_box = new QHBoxLayout; | |
cf0a3c9c | 392 | stack_button_box->addWidget(stack_button_, 0, Qt::AlignRight); |
ce94e4fd | 393 | form->addRow(stack_button_box); |
4e5a4405 JH |
394 | } |
395 | ||
9e773fec | 396 | QMenu* DecodeTrace::create_header_context_menu(QWidget *parent) |
c51482b3 | 397 | { |
9e773fec | 398 | QMenu *const menu = Trace::create_header_context_menu(parent); |
c51482b3 JH |
399 | |
400 | menu->addSeparator(); | |
401 | ||
402 | QAction *const del = new QAction(tr("Delete"), this); | |
a2d21018 | 403 | del->setShortcuts(QKeySequence::Delete); |
c51482b3 JH |
404 | connect(del, SIGNAL(triggered()), this, SLOT(on_delete())); |
405 | menu->addAction(del); | |
406 | ||
407 | return menu; | |
408 | } | |
409 | ||
be843692 SA |
410 | QMenu* DecodeTrace::create_view_context_menu(QWidget *parent, QPoint &click_pos) |
411 | { | |
79b53a1a SA |
412 | // Get entries from default menu before adding our own |
413 | QMenu *const menu = new QMenu(parent); | |
414 | ||
415 | QMenu* default_menu = Trace::create_view_context_menu(parent, click_pos); | |
416 | if (default_menu) { | |
7a0d99e6 | 417 | for (QAction *action : default_menu->actions()) { // clazy:exclude=range-loop |
79b53a1a SA |
418 | menu->addAction(action); |
419 | if (action->parent() == default_menu) | |
420 | action->setParent(menu); | |
421 | } | |
422 | delete default_menu; | |
423 | ||
424 | // Add separator if needed | |
425 | if (menu->actions().length() > 0) | |
426 | menu->addSeparator(); | |
427 | } | |
428 | ||
84002113 | 429 | selected_row_ = nullptr; |
6a26fc44 | 430 | const DecodeTraceRow* r = get_row_at_point(click_pos); |
84002113 | 431 | if (r) |
6a26fc44 | 432 | selected_row_ = r->decode_row; |
be843692 | 433 | |
c764c995 SA |
434 | const View *const view = owner_->view(); |
435 | assert(view); | |
436 | QPoint pos = view->viewport()->mapFrom(parent, click_pos); | |
437 | ||
99ba5f28 | 438 | // Default sample range is "from here" |
c764c995 | 439 | const pair<uint64_t, uint64_t> sample_range = get_view_sample_range(pos.x(), pos.x() + 1); |
99ba5f28 | 440 | selected_sample_range_ = make_pair(sample_range.first, numeric_limits<uint64_t>::max()); |
be843692 | 441 | |
556259d2 SA |
442 | if (decode_signal_->is_paused()) { |
443 | QAction *const resume = | |
444 | new QAction(tr("Resume decoding"), this); | |
445 | resume->setIcon(QIcon::fromTheme("media-playback-start", | |
446 | QIcon(":/icons/media-playback-start.png"))); | |
447 | connect(resume, SIGNAL(triggered()), this, SLOT(on_pause_decode())); | |
448 | menu->addAction(resume); | |
449 | } else { | |
450 | QAction *const pause = | |
451 | new QAction(tr("Pause decoding"), this); | |
452 | pause->setIcon(QIcon::fromTheme("media-playback-pause", | |
453 | QIcon(":/icons/media-playback-pause.png"))); | |
454 | connect(pause, SIGNAL(triggered()), this, SLOT(on_pause_decode())); | |
455 | menu->addAction(pause); | |
456 | } | |
457 | ||
c764c995 SA |
458 | QAction *const copy_annotation_to_clipboard = |
459 | new QAction(tr("Copy annotation text to clipboard"), this); | |
460 | copy_annotation_to_clipboard->setIcon(QIcon::fromTheme("edit-paste", | |
d01fcb19 | 461 | QIcon(":/icons/edit-paste.svg"))); |
c764c995 SA |
462 | connect(copy_annotation_to_clipboard, SIGNAL(triggered()), this, SLOT(on_copy_annotation_to_clipboard())); |
463 | menu->addAction(copy_annotation_to_clipboard); | |
464 | ||
556259d2 SA |
465 | menu->addSeparator(); |
466 | ||
5a914348 | 467 | QAction *const export_all_rows = |
556259d2 | 468 | new QAction(tr("Export all annotations"), this); |
5a914348 SA |
469 | export_all_rows->setIcon(QIcon::fromTheme("document-save-as", |
470 | QIcon(":/icons/document-save-as.png"))); | |
471 | connect(export_all_rows, SIGNAL(triggered()), this, SLOT(on_export_all_rows())); | |
472 | menu->addAction(export_all_rows); | |
473 | ||
be843692 | 474 | QAction *const export_row = |
556259d2 | 475 | new QAction(tr("Export all annotations for this row"), this); |
be843692 SA |
476 | export_row->setIcon(QIcon::fromTheme("document-save-as", |
477 | QIcon(":/icons/document-save-as.png"))); | |
5a914348 | 478 | connect(export_row, SIGNAL(triggered()), this, SLOT(on_export_row())); |
be843692 SA |
479 | menu->addAction(export_row); |
480 | ||
5a914348 SA |
481 | menu->addSeparator(); |
482 | ||
483 | QAction *const export_all_rows_from_here = | |
484 | new QAction(tr("Export all annotations, starting here"), this); | |
485 | export_all_rows_from_here->setIcon(QIcon::fromTheme("document-save-as", | |
486 | QIcon(":/icons/document-save-as.png"))); | |
487 | connect(export_all_rows_from_here, SIGNAL(triggered()), this, SLOT(on_export_all_rows_from_here())); | |
488 | menu->addAction(export_all_rows_from_here); | |
489 | ||
be843692 SA |
490 | QAction *const export_row_from_here = |
491 | new QAction(tr("Export annotations for this row, starting here"), this); | |
492 | export_row_from_here->setIcon(QIcon::fromTheme("document-save-as", | |
493 | QIcon(":/icons/document-save-as.png"))); | |
5a914348 | 494 | connect(export_row_from_here, SIGNAL(triggered()), this, SLOT(on_export_row_from_here())); |
be843692 SA |
495 | menu->addAction(export_row_from_here); |
496 | ||
99ba5f28 SA |
497 | menu->addSeparator(); |
498 | ||
499 | QAction *const export_all_rows_with_cursor = | |
500 | new QAction(tr("Export all annotations within cursor range"), this); | |
501 | export_all_rows_with_cursor->setIcon(QIcon::fromTheme("document-save-as", | |
502 | QIcon(":/icons/document-save-as.png"))); | |
503 | connect(export_all_rows_with_cursor, SIGNAL(triggered()), this, SLOT(on_export_all_rows_with_cursor())); | |
504 | menu->addAction(export_all_rows_with_cursor); | |
505 | ||
506 | QAction *const export_row_with_cursor = | |
507 | new QAction(tr("Export annotations for this row within cursor range"), this); | |
508 | export_row_with_cursor->setIcon(QIcon::fromTheme("document-save-as", | |
509 | QIcon(":/icons/document-save-as.png"))); | |
510 | connect(export_row_with_cursor, SIGNAL(triggered()), this, SLOT(on_export_row_with_cursor())); | |
511 | menu->addAction(export_row_with_cursor); | |
512 | ||
99ba5f28 SA |
513 | if (!view->cursors()->enabled()) { |
514 | export_all_rows_with_cursor->setEnabled(false); | |
515 | export_row_with_cursor->setEnabled(false); | |
516 | } | |
517 | ||
be843692 SA |
518 | return menu; |
519 | } | |
520 | ||
74bf6666 SA |
521 | void DecodeTrace::delete_pressed() |
522 | { | |
523 | on_delete(); | |
524 | } | |
525 | ||
526 | void DecodeTrace::hover_point_changed(const QPoint &hp) | |
527 | { | |
528 | Trace::hover_point_changed(hp); | |
529 | ||
530 | assert(owner_); | |
531 | ||
6a26fc44 | 532 | DecodeTraceRow* hover_row = get_row_at_point(hp); |
74bf6666 SA |
533 | |
534 | // Row expansion marker handling | |
6a26fc44 | 535 | for (DecodeTraceRow& r : rows_) |
74bf6666 SA |
536 | r.expand_marker_highlighted = false; |
537 | ||
538 | if (hover_row) { | |
539 | int row_y = get_row_y(hover_row); | |
41293691 | 540 | if ((hp.x() > 0) && (hp.x() < (int)(ArrowSize + 3 + hover_row->title_width)) && |
74bf6666 SA |
541 | (hp.y() > (int)(row_y - ArrowSize)) && (hp.y() < (int)(row_y + ArrowSize))) |
542 | hover_row->expand_marker_highlighted = true; | |
543 | } | |
544 | ||
545 | // Tooltip handling | |
546 | if (hp.x() > 0) { | |
547 | QString ann = get_annotation_at_point(hp); | |
548 | ||
549 | if (!ann.isEmpty()) { | |
550 | QFontMetrics m(QToolTip::font()); | |
551 | const QRect text_size = m.boundingRect(QRect(), 0, ann); | |
552 | ||
553 | // This is OS-specific and unfortunately we can't query it, so | |
554 | // use an approximation to at least try to minimize the error. | |
555 | const int padding = default_row_height_ + 8; | |
556 | ||
557 | // Make sure the tool tip doesn't overlap with the mouse cursor. | |
558 | // If it did, the tool tip would constantly hide and re-appear. | |
559 | // We also push it up by one row so that it appears above the | |
560 | // decode trace, not below. | |
561 | QPoint p = hp; | |
562 | p.setX(hp.x() - (text_size.width() / 2) - padding); | |
563 | ||
564 | p.setY(get_row_y(hover_row) - default_row_height_ - | |
565 | text_size.height() - padding); | |
566 | ||
567 | const View *const view = owner_->view(); | |
568 | assert(view); | |
569 | QToolTip::showText(view->viewport()->mapToGlobal(p), ann); | |
570 | ||
571 | } else | |
572 | QToolTip::hideText(); | |
573 | ||
574 | } else | |
575 | QToolTip::hideText(); | |
576 | } | |
577 | ||
578 | void DecodeTrace::mouse_left_press_event(const QMouseEvent* event) | |
579 | { | |
580 | // Handle row expansion marker | |
6a26fc44 | 581 | for (DecodeTraceRow& r : rows_) { |
74bf6666 SA |
582 | if (!r.expand_marker_highlighted) |
583 | continue; | |
584 | ||
585 | unsigned int y = get_row_y(&r); | |
41293691 | 586 | if ((event->x() > 0) && (event->x() <= (int)(ArrowSize + 3 + r.title_width)) && |
74bf6666 SA |
587 | (event->y() > (int)(y - (default_row_height_ / 2))) && |
588 | (event->y() <= (int)(y + (default_row_height_ / 2)))) { | |
589 | ||
590 | if (r.expanded) { | |
591 | r.collapsing = true; | |
592 | r.expanded = false; | |
593 | r.anim_shape = ArrowSize; | |
594 | } else { | |
595 | r.expanding = true; | |
596 | r.anim_shape = 0; | |
dca3cbee | 597 | r.container->setVisible(true); |
c6b4e925 SA |
598 | QApplication::processEvents(); |
599 | r.expanded_height = 5 * default_row_height_ + r.container->size().height(); | |
74bf6666 SA |
600 | } |
601 | ||
602 | r.animation_step = 0; | |
603 | r.anim_height = r.height; | |
dca3cbee | 604 | |
c6b4e925 | 605 | update_expanded_rows(); |
74bf6666 SA |
606 | animation_timer_.start(); |
607 | } | |
608 | } | |
609 | } | |
610 | ||
50631798 SA |
611 | void DecodeTrace::draw_annotations(vector<pv::data::decode::Annotation> annotations, |
612 | QPainter &p, int h, const ViewItemPaintParams &pp, int y, | |
9ba13f5e | 613 | QColor row_color, int row_title_width) |
50631798 SA |
614 | { |
615 | using namespace pv::data::decode; | |
616 | ||
20d90d22 SA |
617 | Annotation::Class block_class = 0; |
618 | bool block_class_uniform = true; | |
5c48ce32 | 619 | qreal block_start = 0; |
20d90d22 SA |
620 | int block_ann_count = 0; |
621 | ||
622 | const Annotation *prev_ann; | |
5c48ce32 | 623 | qreal prev_end = INT_MIN; |
20d90d22 | 624 | |
5c48ce32 | 625 | qreal a_end; |
50631798 SA |
626 | |
627 | double samples_per_pixel, pixels_offset; | |
628 | tie(pixels_offset, samples_per_pixel) = | |
629 | get_pixels_offset_samples_per_pixel(); | |
630 | ||
bdc2a99b SA |
631 | // Sort the annotations by start sample so that decoders |
632 | // can't confuse us by creating annotations out of order | |
633 | stable_sort(annotations.begin(), annotations.end(), | |
634 | [](const Annotation &a, const Annotation &b) { | |
635 | return a.start_sample() < b.start_sample(); }); | |
636 | ||
50631798 SA |
637 | // Gather all annotations that form a visual "block" and draw them as such |
638 | for (const Annotation &a : annotations) { | |
639 | ||
5c48ce32 SA |
640 | const qreal abs_a_start = a.start_sample() / samples_per_pixel; |
641 | const qreal abs_a_end = a.end_sample() / samples_per_pixel; | |
bdc2a99b | 642 | |
5c48ce32 | 643 | const qreal a_start = abs_a_start - pixels_offset; |
20d90d22 SA |
644 | a_end = abs_a_end - pixels_offset; |
645 | ||
5c48ce32 SA |
646 | const qreal a_width = a_end - a_start; |
647 | const qreal delta = a_end - prev_end; | |
bdc2a99b SA |
648 | |
649 | bool a_is_separate = false; | |
650 | ||
651 | // Annotation wider than the threshold for a useful label width? | |
752281db | 652 | if (a_width >= min_useful_label_width_) { |
bdc2a99b | 653 | for (const QString &ann_text : a.annotations()) { |
5c48ce32 | 654 | const qreal w = p.boundingRect(QRectF(), 0, ann_text).width(); |
bdc2a99b SA |
655 | // Annotation wide enough to fit a label? Don't put it in a block then |
656 | if (w <= a_width) { | |
657 | a_is_separate = true; | |
658 | break; | |
659 | } | |
660 | } | |
661 | } | |
50631798 | 662 | |
bdc2a99b SA |
663 | // Were the previous and this annotation more than a pixel apart? |
664 | if ((abs(delta) > 1) || a_is_separate) { | |
665 | // Block was broken, draw annotations that form the current block | |
20d90d22 SA |
666 | if (block_ann_count == 1) |
667 | draw_annotation(*prev_ann, p, h, pp, y, row_color, | |
aee9dcf3 | 668 | row_title_width); |
20d90d22 SA |
669 | else if (block_ann_count > 0) |
670 | draw_annotation_block(block_start, prev_end, block_class, | |
671 | block_class_uniform, p, h, y, row_color); | |
50631798 | 672 | |
20d90d22 | 673 | block_ann_count = 0; |
50631798 SA |
674 | } |
675 | ||
bdc2a99b | 676 | if (a_is_separate) { |
9ba13f5e | 677 | draw_annotation(a, p, h, pp, y, row_color, row_title_width); |
bdc2a99b | 678 | // Next annotation must start a new block. delta will be > 1 |
20d90d22 SA |
679 | // because we set prev_end to INT_MIN but that's okay since |
680 | // block_ann_count will be 0 and nothing will be drawn | |
681 | prev_end = INT_MIN; | |
5c48ce32 | 682 | block_ann_count = 0; |
bdc2a99b | 683 | } else { |
20d90d22 SA |
684 | prev_end = a_end; |
685 | prev_ann = &a; | |
686 | ||
687 | if (block_ann_count == 0) { | |
688 | block_start = a_start; | |
689 | block_class = a.ann_class(); | |
690 | block_class_uniform = true; | |
691 | } else | |
692 | if (a.ann_class() != block_class) | |
693 | block_class_uniform = false; | |
694 | ||
695 | block_ann_count++; | |
bdc2a99b | 696 | } |
50631798 SA |
697 | } |
698 | ||
20d90d22 SA |
699 | if (block_ann_count == 1) |
700 | draw_annotation(*prev_ann, p, h, pp, y, row_color, row_title_width); | |
701 | else if (block_ann_count > 0) | |
702 | draw_annotation_block(block_start, prev_end, block_class, | |
703 | block_class_uniform, p, h, y, row_color); | |
50631798 SA |
704 | } |
705 | ||
287d607f | 706 | void DecodeTrace::draw_annotation(const pv::data::decode::Annotation &a, |
5b5fa4da | 707 | QPainter &p, int h, const ViewItemPaintParams &pp, int y, |
9ba13f5e | 708 | QColor row_color, int row_title_width) const |
06e810f2 | 709 | { |
53e35b2d JH |
710 | double samples_per_pixel, pixels_offset; |
711 | tie(pixels_offset, samples_per_pixel) = | |
712 | get_pixels_offset_samples_per_pixel(); | |
7f8517f6 | 713 | |
06e810f2 JH |
714 | const double start = a.start_sample() / samples_per_pixel - |
715 | pixels_offset; | |
c063290a | 716 | const double end = a.end_sample() / samples_per_pixel - pixels_offset; |
287d607f | 717 | |
f228f00e | 718 | QColor color = get_annotation_color(row_color, a.ann_class()); |
9ba13f5e SA |
719 | p.setPen(color.darker()); |
720 | p.setBrush(color); | |
06e810f2 | 721 | |
3eb29afd | 722 | if (start > pp.right() + DrawPadding || end < pp.left() - DrawPadding) |
06e810f2 JH |
723 | return; |
724 | ||
725 | if (a.start_sample() == a.end_sample()) | |
f765c3db | 726 | draw_instant(a, p, h, start, y); |
06e810f2 | 727 | else |
c063290a | 728 | draw_range(a, p, h, start, end, y, pp, row_title_width); |
06e810f2 JH |
729 | } |
730 | ||
5c48ce32 | 731 | void DecodeTrace::draw_annotation_block(qreal start, qreal end, |
20d90d22 | 732 | Annotation::Class ann_class, bool use_ann_format, QPainter &p, int h, |
9ba13f5e | 733 | int y, QColor row_color) const |
50631798 | 734 | { |
33707990 SA |
735 | const double top = y + .5 - h / 2; |
736 | const double bottom = y + .5 + h / 2; | |
33707990 | 737 | |
3082ee93 JH |
738 | const QRectF rect(start, top, end - start, bottom - top); |
739 | const int r = h / 4; | |
740 | ||
741 | p.setPen(QPen(Qt::NoPen)); | |
742 | p.setBrush(Qt::white); | |
743 | p.drawRoundedRect(rect, r, r); | |
744 | ||
20d90d22 SA |
745 | // If all annotations in this block are of the same type, we can use the |
746 | // one format that all of these annotations have. Otherwise, we should use | |
747 | // a neutral color (i.e. gray) | |
748 | if (use_ann_format) { | |
749 | const QColor color = get_annotation_color(row_color, ann_class); | |
750 | p.setPen(color.darker()); | |
751 | p.setBrush(QBrush(color, Qt::Dense4Pattern)); | |
752 | } else { | |
753 | p.setPen(Qt::gray); | |
754 | p.setBrush(QBrush(Qt::gray, Qt::Dense4Pattern)); | |
755 | } | |
756 | ||
3082ee93 | 757 | p.drawRoundedRect(rect, r, r); |
50631798 SA |
758 | } |
759 | ||
06e810f2 | 760 | void DecodeTrace::draw_instant(const pv::data::decode::Annotation &a, QPainter &p, |
5c48ce32 | 761 | int h, qreal x, int y) const |
06e810f2 JH |
762 | { |
763 | const QString text = a.annotations().empty() ? | |
764 | QString() : a.annotations().back(); | |
5c48ce32 | 765 | const qreal w = min((qreal)p.boundingRect(QRectF(), 0, text).width(), |
06e810f2 JH |
766 | 0.0) + h; |
767 | const QRectF rect(x - w / 2, y - h / 2, w, h); | |
768 | ||
06e810f2 JH |
769 | p.drawRoundedRect(rect, h / 2, h / 2); |
770 | ||
2a56e448 | 771 | p.setPen(Qt::black); |
06e810f2 JH |
772 | p.drawText(rect, Qt::AlignCenter | Qt::AlignVCenter, text); |
773 | } | |
774 | ||
775 | void DecodeTrace::draw_range(const pv::data::decode::Annotation &a, QPainter &p, | |
5c48ce32 | 776 | int h, qreal start, qreal end, int y, const ViewItemPaintParams &pp, |
f765c3db | 777 | int row_title_width) const |
06e810f2 | 778 | { |
5c48ce32 SA |
779 | const qreal top = y + .5 - h / 2; |
780 | const qreal bottom = y + .5 + h / 2; | |
06e810f2 JH |
781 | const vector<QString> annotations = a.annotations(); |
782 | ||
06e810f2 | 783 | // If the two ends are within 1 pixel, draw a vertical line |
2ad82c2e | 784 | if (start + 1.0 > end) { |
06e810f2 JH |
785 | p.drawLine(QPointF(start, top), QPointF(start, bottom)); |
786 | return; | |
787 | } | |
788 | ||
5c48ce32 | 789 | const qreal cap_width = min((end - start) / 4, EndCapWidth); |
06e810f2 JH |
790 | |
791 | QPointF pts[] = { | |
792 | QPointF(start, y + .5f), | |
793 | QPointF(start + cap_width, top), | |
794 | QPointF(end - cap_width, top), | |
795 | QPointF(end, y + .5f), | |
796 | QPointF(end - cap_width, bottom), | |
797 | QPointF(start + cap_width, bottom) | |
798 | }; | |
799 | ||
800 | p.drawConvexPolygon(pts, countof(pts)); | |
801 | ||
802 | if (annotations.empty()) | |
803 | return; | |
804 | ||
7352be72 SA |
805 | const int ann_start = start + cap_width; |
806 | const int ann_end = end - cap_width; | |
807 | ||
74bf6666 | 808 | const int real_start = max(ann_start, pp.left() + ArrowSize + row_title_width); |
6f925ba9 | 809 | const int real_end = min(ann_end, pp.right()); |
7352be72 SA |
810 | const int real_width = real_end - real_start; |
811 | ||
812 | QRectF rect(real_start, y - h / 2, real_width, h); | |
0f290e9b JH |
813 | if (rect.width() <= 4) |
814 | return; | |
815 | ||
2a56e448 | 816 | p.setPen(Qt::black); |
06e810f2 JH |
817 | |
818 | // Try to find an annotation that will fit | |
819 | QString best_annotation; | |
820 | int best_width = 0; | |
821 | ||
d9aecf1f | 822 | for (const QString &a : annotations) { |
06e810f2 JH |
823 | const int w = p.boundingRect(QRectF(), 0, a).width(); |
824 | if (w <= rect.width() && w > best_width) | |
825 | best_annotation = a, best_width = w; | |
826 | } | |
827 | ||
828 | if (best_annotation.isEmpty()) | |
829 | best_annotation = annotations.back(); | |
830 | ||
831 | // If not ellide the last in the list | |
832 | p.drawText(rect, Qt::AlignCenter, p.fontMetrics().elidedText( | |
833 | best_annotation, Qt::ElideRight, rect.width())); | |
834 | } | |
835 | ||
b9329558 | 836 | void DecodeTrace::draw_error(QPainter &p, const QString &message, |
5b5fa4da | 837 | const ViewItemPaintParams &pp) |
ad50ac1a | 838 | { |
be9e7b4b | 839 | const int y = get_visual_y(); |
ad50ac1a | 840 | |
1ae18301 SA |
841 | double samples_per_pixel, pixels_offset; |
842 | tie(pixels_offset, samples_per_pixel) = get_pixels_offset_samples_per_pixel(); | |
843 | ||
641574bc SA |
844 | p.setPen(ErrorBgColor.darker()); |
845 | p.setBrush(ErrorBgColor); | |
ad50ac1a | 846 | |
1ae18301 SA |
847 | const QRectF bounding_rect = QRectF(pp.left(), INT_MIN / 2 + y, pp.right(), INT_MAX); |
848 | ||
849 | const QRectF text_rect = p.boundingRect(bounding_rect, Qt::AlignCenter, message); | |
5c48ce32 | 850 | const qreal r = text_rect.height() / 4; |
ad50ac1a | 851 | |
1ae18301 | 852 | p.drawRoundedRect(text_rect.adjusted(-r, -r, r, r), r, r, Qt::AbsoluteSize); |
ad50ac1a | 853 | |
2a56e448 | 854 | p.setPen(Qt::black); |
ad50ac1a JH |
855 | p.drawText(text_rect, message); |
856 | } | |
857 | ||
ff83d980 | 858 | void DecodeTrace::draw_unresolved_period(QPainter &p, int h, int left, int right) const |
5dfeb70f JH |
859 | { |
860 | using namespace pv::data; | |
861 | using pv::data::decode::Decoder; | |
862 | ||
53e35b2d JH |
863 | double samples_per_pixel, pixels_offset; |
864 | ||
5ecf957f | 865 | const int64_t sample_count = decode_signal_->get_working_sample_count(current_segment_); |
5dfeb70f JH |
866 | if (sample_count == 0) |
867 | return; | |
868 | ||
b82908ab | 869 | const int64_t samples_decoded = decode_signal_->get_decoded_sample_count(current_segment_, true); |
5dfeb70f JH |
870 | if (sample_count == samples_decoded) |
871 | return; | |
872 | ||
be9e7b4b | 873 | const int y = get_visual_y(); |
7f8517f6 | 874 | |
ff83d980 | 875 | tie(pixels_offset, samples_per_pixel) = get_pixels_offset_samples_per_pixel(); |
7f8517f6 | 876 | |
5dfeb70f JH |
877 | const double start = max(samples_decoded / |
878 | samples_per_pixel - pixels_offset, left - 1.0); | |
879 | const double end = min(sample_count / samples_per_pixel - | |
880 | pixels_offset, right + 1.0); | |
e06cf18d | 881 | const QRectF no_decode_rect(start, y - (h / 2) - 0.5, end - start, h); |
5dfeb70f JH |
882 | |
883 | p.setPen(QPen(Qt::NoPen)); | |
884 | p.setBrush(Qt::white); | |
885 | p.drawRect(no_decode_rect); | |
886 | ||
641574bc SA |
887 | p.setPen(NoDecodeColor); |
888 | p.setBrush(QBrush(NoDecodeColor, Qt::Dense6Pattern)); | |
5dfeb70f JH |
889 | p.drawRect(no_decode_rect); |
890 | } | |
891 | ||
53e35b2d | 892 | pair<double, double> DecodeTrace::get_pixels_offset_samples_per_pixel() const |
7f8517f6 | 893 | { |
8dbbc7f0 | 894 | assert(owner_); |
7f8517f6 | 895 | |
8dbbc7f0 | 896 | const View *view = owner_->view(); |
eae6e30a JH |
897 | assert(view); |
898 | ||
899 | const double scale = view->scale(); | |
7f8517f6 SA |
900 | assert(scale > 0); |
901 | ||
53e35b2d | 902 | const double pixels_offset = |
ff83d980 | 903 | ((view->offset() - decode_signal_->start_time()) / scale).convert_to<double>(); |
7f8517f6 | 904 | |
ff83d980 | 905 | double samplerate = decode_signal_->samplerate(); |
7f8517f6 SA |
906 | |
907 | // Show sample rate as 1Hz when it is unknown | |
908 | if (samplerate == 0.0) | |
909 | samplerate = 1.0; | |
910 | ||
53e35b2d | 911 | return make_pair(pixels_offset, samplerate * scale); |
7f8517f6 SA |
912 | } |
913 | ||
67fb15bf | 914 | pair<uint64_t, uint64_t> DecodeTrace::get_view_sample_range( |
db1bf6bf | 915 | int x_start, int x_end) const |
7f8517f6 | 916 | { |
53e35b2d JH |
917 | double samples_per_pixel, pixels_offset; |
918 | tie(pixels_offset, samples_per_pixel) = | |
919 | get_pixels_offset_samples_per_pixel(); | |
7f8517f6 | 920 | |
db1bf6bf JH |
921 | const uint64_t start = (uint64_t)max( |
922 | (x_start + pixels_offset) * samples_per_pixel, 0.0); | |
923 | const uint64_t end = (uint64_t)max( | |
924 | (x_end + pixels_offset) * samples_per_pixel, 0.0); | |
7f8517f6 SA |
925 | |
926 | return make_pair(start, end); | |
927 | } | |
928 | ||
9ba13f5e SA |
929 | QColor DecodeTrace::get_row_color(int row_index) const |
930 | { | |
931 | // For each row color, use the base color hue and add an offset that's | |
932 | // not a dividend of 360 | |
933 | ||
934 | QColor color; | |
935 | const int h = (base_->color().toHsv().hue() + 20 * row_index) % 360; | |
936 | const int s = DECODETRACE_COLOR_SATURATION; | |
937 | const int v = DECODETRACE_COLOR_VALUE; | |
938 | color.setHsl(h, s, v); | |
939 | ||
940 | return color; | |
941 | } | |
942 | ||
943 | QColor DecodeTrace::get_annotation_color(QColor row_color, int annotation_index) const | |
944 | { | |
945 | // For each row color, use the base color hue and add an offset that's | |
946 | // not a dividend of 360 and not a multiple of the row offset | |
947 | ||
948 | QColor color(row_color); | |
949 | const int h = (color.toHsv().hue() + 55 * annotation_index) % 360; | |
950 | const int s = DECODETRACE_COLOR_SATURATION; | |
951 | const int v = DECODETRACE_COLOR_VALUE; | |
952 | color.setHsl(h, s, v); | |
953 | ||
954 | return color; | |
955 | } | |
956 | ||
6a26fc44 | 957 | unsigned int DecodeTrace::get_row_y(const DecodeTraceRow* row) const |
e2f90c50 | 958 | { |
84002113 | 959 | assert(row); |
e2f90c50 | 960 | |
84002113 | 961 | unsigned int y = get_visual_y(); |
99029fda | 962 | |
6a26fc44 | 963 | for (const DecodeTraceRow& r : rows_) { |
9741d6e0 SA |
964 | if (!r.currently_visible) |
965 | continue; | |
966 | ||
967 | if (row->decode_row == r.decode_row) | |
84002113 | 968 | break; |
9741d6e0 SA |
969 | else |
970 | y += r.height; | |
971 | } | |
99029fda | 972 | |
84002113 SA |
973 | return y; |
974 | } | |
99029fda | 975 | |
6a26fc44 | 976 | DecodeTraceRow* DecodeTrace::get_row_at_point(const QPoint &point) |
84002113 | 977 | { |
9741d6e0 | 978 | int y = get_visual_y() - (default_row_height_ / 2); |
84002113 | 979 | |
6a26fc44 | 980 | for (DecodeTraceRow& r : rows_) { |
84002113 SA |
981 | if (!r.currently_visible) |
982 | continue; | |
983 | ||
984 | if ((point.y() >= y) && (point.y() < (int)(y + r.height))) | |
985 | return &r; | |
986 | ||
987 | y += r.height; | |
988 | } | |
e2f90c50 | 989 | |
84002113 | 990 | return nullptr; |
e2f90c50 SA |
991 | } |
992 | ||
117cdea3 | 993 | const QString DecodeTrace::get_annotation_at_point(const QPoint &point) |
e2f90c50 SA |
994 | { |
995 | using namespace pv::data::decode; | |
996 | ||
117cdea3 JH |
997 | if (!enabled()) |
998 | return QString(); | |
e2f90c50 | 999 | |
117cdea3 | 1000 | const pair<uint64_t, uint64_t> sample_range = |
67fb15bf | 1001 | get_view_sample_range(point.x(), point.x() + 1); |
6a26fc44 | 1002 | const DecodeTraceRow* r = get_row_at_point(point); |
84002113 SA |
1003 | |
1004 | if (!r) | |
117cdea3 | 1005 | return QString(); |
e2f90c50 | 1006 | |
9741d6e0 SA |
1007 | if (point.y() > (int)(get_row_y(r) + (annotation_height_ / 2))) |
1008 | return QString(); | |
1009 | ||
be843692 | 1010 | vector<Annotation> annotations; |
e2f90c50 | 1011 | |
84002113 | 1012 | decode_signal_->get_annotation_subset(annotations, r->decode_row, |
72435789 | 1013 | current_segment_, sample_range.first, sample_range.second); |
e2f90c50 SA |
1014 | |
1015 | return (annotations.empty()) ? | |
1016 | QString() : annotations[0].annotations().front(); | |
1017 | } | |
1018 | ||
613d097c JH |
1019 | void DecodeTrace::create_decoder_form(int index, |
1020 | shared_ptr<data::decode::Decoder> &dec, QWidget *parent, | |
1021 | QFormLayout *form) | |
7491a29f | 1022 | { |
1cc1c8de | 1023 | GlobalSettings settings; |
7491a29f JH |
1024 | |
1025 | assert(dec); | |
6a26fc44 | 1026 | const srd_decoder *const decoder = dec->get_srd_decoder(); |
7491a29f JH |
1027 | assert(decoder); |
1028 | ||
ff59fa2c SA |
1029 | const bool decoder_deletable = index > 0; |
1030 | ||
204bae45 | 1031 | pv::widgets::DecoderGroupBox *const group = |
27e8df22 | 1032 | new pv::widgets::DecoderGroupBox( |
580b4f25 UH |
1033 | QString::fromUtf8(decoder->name), |
1034 | tr("%1:\n%2").arg(QString::fromUtf8(decoder->longname), | |
1035 | QString::fromUtf8(decoder->desc)), | |
1036 | nullptr, decoder_deletable); | |
5d3ca591 | 1037 | group->set_decoder_visible(dec->visible()); |
613d097c | 1038 | |
ff59fa2c SA |
1039 | if (decoder_deletable) { |
1040 | delete_mapper_.setMapping(group, index); | |
1041 | connect(group, SIGNAL(delete_decoder()), &delete_mapper_, SLOT(map())); | |
1042 | } | |
613d097c | 1043 | |
8dbbc7f0 | 1044 | show_hide_mapper_.setMapping(group, index); |
dd048a7e | 1045 | connect(group, SIGNAL(show_hide_decoder()), |
8dbbc7f0 | 1046 | &show_hide_mapper_, SLOT(map())); |
dd048a7e | 1047 | |
204bae45 JH |
1048 | QFormLayout *const decoder_form = new QFormLayout; |
1049 | group->add_layout(decoder_form); | |
7491a29f | 1050 | |
47747218 | 1051 | const vector<DecodeChannel> channels = decode_signal_->get_channels(); |
407c9ebe | 1052 | |
9f97b357 | 1053 | // Add the channels |
7a0d99e6 | 1054 | for (const DecodeChannel& ch : channels) { |
9f97b357 SA |
1055 | // Ignore channels not part of the decoder we create the form for |
1056 | if (ch.decoder_ != dec) | |
1057 | continue; | |
407c9ebe | 1058 | |
9f97b357 SA |
1059 | QComboBox *const combo = create_channel_selector(parent, &ch); |
1060 | QComboBox *const combo_init_state = create_channel_selector_init_state(parent, &ch); | |
407c9ebe | 1061 | |
9f97b357 SA |
1062 | channel_id_map_[combo] = ch.id; |
1063 | init_state_map_[combo_init_state] = ch.id; | |
407c9ebe | 1064 | |
7491a29f | 1065 | connect(combo, SIGNAL(currentIndexChanged(int)), |
6ac6242b | 1066 | this, SLOT(on_channel_selected(int))); |
9f97b357 SA |
1067 | connect(combo_init_state, SIGNAL(currentIndexChanged(int)), |
1068 | this, SLOT(on_init_state_changed(int))); | |
407c9ebe UH |
1069 | |
1070 | QHBoxLayout *const hlayout = new QHBoxLayout; | |
1071 | hlayout->addWidget(combo); | |
9f97b357 | 1072 | hlayout->addWidget(combo_init_state); |
407c9ebe | 1073 | |
1cc1c8de | 1074 | if (!settings.value(GlobalSettings::Key_Dec_InitialStateConfigurable).toBool()) |
9f97b357 | 1075 | combo_init_state->hide(); |
7491a29f | 1076 | |
9f97b357 SA |
1077 | const QString required_flag = ch.is_optional ? QString() : QString("*"); |
1078 | decoder_form->addRow(tr("<b>%1</b> (%2) %3") | |
1079 | .arg(ch.name, ch.desc, required_flag), hlayout); | |
7491a29f JH |
1080 | } |
1081 | ||
1082 | // Add the options | |
3cc9ad7b | 1083 | shared_ptr<binding::Decoder> binding( |
946b52e1 | 1084 | new binding::Decoder(decode_signal_, dec)); |
204bae45 | 1085 | binding->add_properties_to_form(decoder_form, true); |
7491a29f | 1086 | |
8dbbc7f0 | 1087 | bindings_.push_back(binding); |
204bae45 JH |
1088 | |
1089 | form->addRow(group); | |
8dbbc7f0 | 1090 | decoder_forms_.push_back(group); |
7491a29f JH |
1091 | } |
1092 | ||
9f97b357 | 1093 | QComboBox* DecodeTrace::create_channel_selector(QWidget *parent, const DecodeChannel *ch) |
4e5a4405 | 1094 | { |
47e9e7bb | 1095 | const auto sigs(session_.signalbases()); |
78b0af3e | 1096 | |
9f97b357 | 1097 | // Sort signals in natural order |
47e9e7bb | 1098 | vector< shared_ptr<data::SignalBase> > sig_list(sigs.begin(), sigs.end()); |
6f925ba9 | 1099 | sort(sig_list.begin(), sig_list.end(), |
47e9e7bb SA |
1100 | [](const shared_ptr<data::SignalBase> &a, |
1101 | const shared_ptr<data::SignalBase> &b) { | |
1102 | return strnatcasecmp(a->name().toStdString(), | |
1103 | b->name().toStdString()) < 0; }); | |
4e5a4405 | 1104 | |
4e5a4405 JH |
1105 | QComboBox *selector = new QComboBox(parent); |
1106 | ||
4c60462b | 1107 | selector->addItem("-", qVariantFromValue((void*)nullptr)); |
4e5a4405 | 1108 | |
9f97b357 | 1109 | if (!ch->assigned_signal) |
4e5a4405 JH |
1110 | selector->setCurrentIndex(0); |
1111 | ||
47e9e7bb SA |
1112 | for (const shared_ptr<data::SignalBase> &b : sig_list) { |
1113 | assert(b); | |
79c4a9c8 | 1114 | if (b->logic_data() && b->enabled()) { |
47e9e7bb SA |
1115 | selector->addItem(b->name(), |
1116 | qVariantFromValue((void*)b.get())); | |
5da5d081 | 1117 | |
9f97b357 SA |
1118 | if (ch->assigned_signal == b.get()) |
1119 | selector->setCurrentIndex(selector->count() - 1); | |
4e5a4405 JH |
1120 | } |
1121 | } | |
1122 | ||
1123 | return selector; | |
1124 | } | |
1125 | ||
9f97b357 SA |
1126 | QComboBox* DecodeTrace::create_channel_selector_init_state(QWidget *parent, |
1127 | const DecodeChannel *ch) | |
407c9ebe UH |
1128 | { |
1129 | QComboBox *selector = new QComboBox(parent); | |
1130 | ||
1131 | selector->addItem("0", qVariantFromValue((int)SRD_INITIAL_PIN_LOW)); | |
1132 | selector->addItem("1", qVariantFromValue((int)SRD_INITIAL_PIN_HIGH)); | |
7df44935 | 1133 | selector->addItem("X", qVariantFromValue((int)SRD_INITIAL_PIN_SAME_AS_SAMPLE0)); |
407c9ebe | 1134 | |
9f97b357 | 1135 | selector->setCurrentIndex(ch->initial_pin_state); |
407c9ebe UH |
1136 | |
1137 | selector->setToolTip("Initial (assumed) pin value before the first sample"); | |
1138 | ||
1139 | return selector; | |
1140 | } | |
1141 | ||
5a914348 SA |
1142 | void DecodeTrace::export_annotations(vector<Annotation> *annotations) const |
1143 | { | |
1144 | using namespace pv::data::decode; | |
1145 | ||
1146 | GlobalSettings settings; | |
1147 | const QString dir = settings.value("MainWindow/SaveDirectory").toString(); | |
1148 | ||
1149 | const QString file_name = QFileDialog::getSaveFileName( | |
1150 | owner_->view(), tr("Export annotations"), dir, tr("Text Files (*.txt);;All Files (*)")); | |
1151 | ||
1152 | if (file_name.isEmpty()) | |
1153 | return; | |
1154 | ||
1155 | QString format = settings.value(GlobalSettings::Key_Dec_ExportFormat).toString(); | |
1156 | const QString quote = format.contains("%q") ? "\"" : ""; | |
1157 | format = format.remove("%q"); | |
1158 | ||
1159 | QFile file(file_name); | |
1160 | if (file.open(QIODevice::WriteOnly | QIODevice::Truncate | QIODevice::Text)) { | |
1161 | QTextStream out_stream(&file); | |
1162 | ||
1163 | for (Annotation &ann : *annotations) { | |
1164 | const QString sample_range = QString("%1-%2") \ | |
1165 | .arg(QString::number(ann.start_sample()), QString::number(ann.end_sample())); | |
1166 | ||
6a26fc44 | 1167 | const QString row_name = quote + ann.row()->description() + quote; |
5a914348 SA |
1168 | |
1169 | QString all_ann_text; | |
1170 | for (const QString &s : ann.annotations()) | |
1171 | all_ann_text = all_ann_text + quote + s + quote + ","; | |
1172 | all_ann_text.chop(1); | |
1173 | ||
1174 | const QString first_ann_text = quote + ann.annotations().front() + quote; | |
1175 | ||
1176 | QString out_text = format; | |
1177 | out_text = out_text.replace("%s", sample_range); | |
1178 | out_text = out_text.replace("%d", | |
cbb2e4da | 1179 | quote + QString::fromUtf8(ann.row()->decoder()->name()) + quote); |
6a26fc44 | 1180 | out_text = out_text.replace("%r", row_name); |
5a914348 SA |
1181 | out_text = out_text.replace("%1", first_ann_text); |
1182 | out_text = out_text.replace("%a", all_ann_text); | |
1183 | out_stream << out_text << '\n'; | |
1184 | } | |
1185 | ||
1186 | if (out_stream.status() == QTextStream::Ok) | |
1187 | return; | |
1188 | } | |
1189 | ||
1190 | QMessageBox msg(owner_->view()); | |
970fca0d | 1191 | msg.setText(tr("Error") + "\n\n" + tr("File %1 could not be written to.").arg(file_name)); |
5a914348 SA |
1192 | msg.setStandardButtons(QMessageBox::Ok); |
1193 | msg.setIcon(QMessageBox::Warning); | |
1194 | msg.exec(); | |
1195 | } | |
1196 | ||
6a26fc44 | 1197 | void DecodeTrace::initialize_row_widgets(DecodeTraceRow* r, unsigned int row_id) |
84002113 | 1198 | { |
84002113 SA |
1199 | QFontMetrics m(QApplication::font()); |
1200 | ||
c6b4e925 SA |
1201 | QPalette header_palette = owner_->view()->palette(); |
1202 | QPalette selector_palette = owner_->view()->palette(); | |
1203 | ||
1204 | if (GlobalSettings::current_theme_is_dark()) { | |
1205 | header_palette.setColor(QPalette::Background, | |
1206 | QColor(255, 255, 255, ExpansionAreaHeaderAlpha)); | |
1207 | selector_palette.setColor(QPalette::Background, | |
1208 | QColor(255, 255, 255, ExpansionAreaAlpha)); | |
1209 | } else { | |
1210 | header_palette.setColor(QPalette::Background, | |
1211 | QColor(0, 0, 0, ExpansionAreaHeaderAlpha)); | |
1212 | selector_palette.setColor(QPalette::Background, | |
1213 | QColor(0, 0, 0, ExpansionAreaAlpha)); | |
1214 | } | |
1215 | ||
6a26fc44 SA |
1216 | const int w = m.boundingRect(r->decode_row->title()).width() + RowTitleMargin; |
1217 | r->title_width = w; | |
1218 | ||
1219 | r->container->resize(owner_->view()->viewport()->width() - r->container->pos().x(), | |
1220 | r->expanded_height - 2 * default_row_height_); | |
1221 | r->container->setVisible(false); | |
1222 | ||
1223 | QVBoxLayout* vlayout = new QVBoxLayout(); | |
1224 | r->container->setLayout(vlayout); | |
1225 | ||
1226 | // Add header container with checkbox for this row | |
1227 | vlayout->addWidget(r->header_container); | |
1228 | vlayout->setContentsMargins(0, 0, 0, 0); | |
1229 | vlayout->setSpacing(0); | |
1230 | r->header_container->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed); | |
1231 | r->header_container->setMinimumSize(0, default_row_height_); | |
1232 | r->header_container->setLayout(new QVBoxLayout()); | |
1233 | r->header_container->layout()->setContentsMargins(10, 2, 0, 2); | |
1234 | ||
1235 | r->header_container->setAutoFillBackground(true); | |
1236 | r->header_container->setPalette(header_palette); | |
1237 | ||
1238 | QCheckBox* cb = new QCheckBox(); | |
1239 | r->header_container->layout()->addWidget(cb); | |
1240 | cb->setText(tr("Show this row")); | |
1241 | cb->setChecked(r->decode_row->visible()); | |
1242 | ||
1243 | row_show_hide_mapper_.setMapping(cb, row_id); | |
1244 | connect(cb, SIGNAL(stateChanged(int)), | |
1245 | &row_show_hide_mapper_, SLOT(map())); | |
1246 | ||
1247 | // Add selector container | |
1248 | vlayout->addWidget(r->selector_container); | |
1249 | r->selector_container->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed); | |
1250 | r->selector_container->setMinimumSize(0, 3 * default_row_height_); // FIXME | |
1251 | r->selector_container->setLayout(new QHBoxLayout()); | |
1252 | ||
1253 | r->selector_container->setAutoFillBackground(true); | |
1254 | r->selector_container->setPalette(selector_palette); | |
1255 | ||
1256 | // Add all classes that can be toggled | |
1257 | vector<AnnotationClass*> ann_classes = r->decode_row->ann_classes(); | |
1258 | ||
1259 | for (const AnnotationClass* ann_class : ann_classes) { | |
1260 | cb = new QCheckBox(); | |
1261 | cb->setText(tr(ann_class->description)); | |
1262 | cb->setChecked(ann_class->visible); | |
1263 | ||
1264 | r->selector_container->layout()->addWidget(cb); | |
1265 | ||
1266 | cb->setProperty("ann_class_ptr", QVariant::fromValue((void*)ann_class)); | |
81dc0221 SA |
1267 | cb->setProperty("decode_trace_row_ptr", QVariant::fromValue((void*)r)); |
1268 | ||
6a26fc44 SA |
1269 | class_show_hide_mapper_.setMapping(cb, cb); |
1270 | connect(cb, SIGNAL(stateChanged(int)), | |
1271 | &class_show_hide_mapper_, SLOT(map())); | |
1272 | } | |
1273 | } | |
1274 | ||
1275 | void DecodeTrace::update_rows() | |
1276 | { | |
1277 | lock_guard<mutex> lock(row_modification_mutex_); | |
1278 | ||
1279 | for (DecodeTraceRow& r : rows_) | |
84002113 SA |
1280 | r.exists = false; |
1281 | ||
c6b4e925 | 1282 | unsigned int row_id = 0; |
6a26fc44 | 1283 | for (Row* decode_row : decode_signal_->get_rows()) { |
84002113 SA |
1284 | // Find row in our list |
1285 | auto r_it = find_if(rows_.begin(), rows_.end(), | |
6a26fc44 | 1286 | [&](DecodeTraceRow& r){ return r.decode_row == decode_row; }); |
84002113 | 1287 | |
6a26fc44 | 1288 | DecodeTraceRow* r = nullptr; |
84002113 SA |
1289 | if (r_it == rows_.end()) { |
1290 | // Row doesn't exist yet, create and append it | |
6a26fc44 | 1291 | DecodeTraceRow nr; |
84002113 SA |
1292 | nr.decode_row = decode_row; |
1293 | nr.height = default_row_height_; | |
c6b4e925 | 1294 | nr.expanded_height = default_row_height_; |
84002113 | 1295 | nr.currently_visible = false; |
81dc0221 | 1296 | nr.has_hidden_classes = decode_row->has_hidden_classes(); |
84002113 | 1297 | nr.expand_marker_highlighted = false; |
74bf6666 | 1298 | nr.expanding = false; |
84002113 | 1299 | nr.expanded = false; |
74bf6666 SA |
1300 | nr.collapsing = false; |
1301 | nr.expand_marker_shape = default_marker_shape_; | |
c6b4e925 SA |
1302 | nr.container = new QWidget(owner_->view()->scrollarea()); |
1303 | nr.header_container = new QWidget(nr.container); | |
1304 | nr.selector_container = new QWidget(nr.container); | |
84002113 SA |
1305 | |
1306 | rows_.push_back(nr); | |
1307 | r = &rows_.back(); | |
6a26fc44 | 1308 | initialize_row_widgets(r, row_id); |
84002113 SA |
1309 | } else |
1310 | r = &(*r_it); | |
1311 | ||
1312 | r->exists = true; | |
c6b4e925 | 1313 | row_id++; |
84002113 SA |
1314 | } |
1315 | ||
1316 | // Remove any rows that no longer exist, obeying that iterators are invalidated | |
1317 | bool any_exists; | |
1318 | do { | |
1319 | any_exists = false; | |
1320 | ||
1321 | for (unsigned int i = 0; i < rows_.size(); i++) | |
1322 | if (!rows_[i].exists) { | |
c6b4e925 SA |
1323 | for (QCheckBox* cb : rows_[i].selectors) |
1324 | delete cb; | |
1325 | ||
1326 | delete rows_[i].selector_container; | |
1327 | delete rows_[i].header_container; | |
dca3cbee | 1328 | delete rows_[i].container; |
c6b4e925 | 1329 | |
84002113 SA |
1330 | rows_.erase(rows_.begin() + i); |
1331 | any_exists = true; | |
1332 | break; | |
1333 | } | |
1334 | } while (any_exists); | |
1335 | } | |
1336 | ||
6a26fc44 | 1337 | void DecodeTrace::set_row_expanded(DecodeTraceRow* r) |
c6b4e925 SA |
1338 | { |
1339 | r->height = r->expanded_height; | |
1340 | r->expanding = false; | |
1341 | r->expanded = true; | |
1342 | ||
1343 | // For details on this, see on_animation_timer() | |
1344 | r->expand_marker_shape.setPoint(0, 0, 0); | |
1345 | r->expand_marker_shape.setPoint(1, ArrowSize, ArrowSize); | |
1346 | r->expand_marker_shape.setPoint(2, 2*ArrowSize, 0); | |
1347 | ||
1348 | r->container->resize(owner_->view()->viewport()->width() - r->container->pos().x(), | |
1349 | r->height - 2 * default_row_height_); | |
6a26fc44 SA |
1350 | |
1351 | max_visible_rows_ = 0; | |
c6b4e925 SA |
1352 | } |
1353 | ||
6a26fc44 | 1354 | void DecodeTrace::set_row_collapsed(DecodeTraceRow* r) |
c6b4e925 SA |
1355 | { |
1356 | r->height = default_row_height_; | |
1357 | r->collapsing = false; | |
1358 | r->expanded = false; | |
1359 | r->expand_marker_shape = default_marker_shape_; | |
1360 | r->container->setVisible(false); | |
1361 | ||
1362 | r->container->resize(owner_->view()->viewport()->width() - r->container->pos().x(), | |
1363 | r->height - 2 * default_row_height_); | |
6a26fc44 SA |
1364 | |
1365 | max_visible_rows_ = 0; | |
c6b4e925 SA |
1366 | } |
1367 | ||
1368 | void DecodeTrace::update_expanded_rows() | |
1369 | { | |
6a26fc44 | 1370 | for (DecodeTraceRow& r : rows_) { |
c6b4e925 SA |
1371 | r.container->move(2 * ArrowSize, |
1372 | get_row_y(&r) + default_row_height_); | |
d4c3b059 SA |
1373 | |
1374 | r.container->resize(owner_->view()->viewport()->width() - r.container->pos().x(), | |
1375 | r.height - 2 * default_row_height_); | |
c6b4e925 SA |
1376 | } |
1377 | } | |
1378 | ||
ab185f78 SA |
1379 | void DecodeTrace::on_setting_changed(const QString &key, const QVariant &value) |
1380 | { | |
945238ca SA |
1381 | Trace::on_setting_changed(key, value); |
1382 | ||
ab185f78 | 1383 | if (key == GlobalSettings::Key_Dec_AlwaysShowAllRows) { |
ab185f78 SA |
1384 | max_visible_rows_ = 0; |
1385 | always_show_all_rows_ = value.toBool(); | |
1386 | } | |
1387 | } | |
1388 | ||
ad908057 | 1389 | void DecodeTrace::on_new_annotations() |
5b6ae103 SA |
1390 | { |
1391 | if (!delayed_trace_updater_.isActive()) | |
1392 | delayed_trace_updater_.start(); | |
1393 | } | |
1394 | ||
1395 | void DecodeTrace::on_delayed_trace_update() | |
9cef9567 | 1396 | { |
8dbbc7f0 | 1397 | if (owner_) |
6e2c3c85 | 1398 | owner_->row_item_appearance_changed(false, true); |
9cef9567 JH |
1399 | } |
1400 | ||
eee3eab9 SA |
1401 | void DecodeTrace::on_decode_reset() |
1402 | { | |
eee3eab9 | 1403 | max_visible_rows_ = 0; |
84002113 | 1404 | update_rows(); |
eee3eab9 SA |
1405 | |
1406 | if (owner_) | |
1407 | owner_->row_item_appearance_changed(false, true); | |
1408 | } | |
1409 | ||
1b56c646 SA |
1410 | void DecodeTrace::on_decode_finished() |
1411 | { | |
1412 | if (owner_) | |
1413 | owner_->row_item_appearance_changed(false, true); | |
1414 | } | |
1415 | ||
556259d2 SA |
1416 | void DecodeTrace::on_pause_decode() |
1417 | { | |
1418 | if (decode_signal_->is_paused()) | |
1419 | decode_signal_->resume_decode(); | |
1420 | else | |
1421 | decode_signal_->pause_decode(); | |
1422 | } | |
1423 | ||
b9329558 | 1424 | void DecodeTrace::on_delete() |
c51482b3 | 1425 | { |
ad908057 | 1426 | session_.remove_decode_signal(decode_signal_); |
c51482b3 JH |
1427 | } |
1428 | ||
6ac6242b | 1429 | void DecodeTrace::on_channel_selected(int) |
4e5a4405 | 1430 | { |
9f97b357 SA |
1431 | QComboBox *cb = qobject_cast<QComboBox*>(QObject::sender()); |
1432 | ||
1433 | // Determine signal that was selected | |
1434 | const data::SignalBase *signal = | |
1435 | (data::SignalBase*)cb->itemData(cb->currentIndex()).value<void*>(); | |
1436 | ||
1437 | // Determine decode channel ID this combo box is the channel selector for | |
1438 | const uint16_t id = channel_id_map_.at(cb); | |
1439 | ||
1440 | decode_signal_->assign_signal(id, signal); | |
1441 | } | |
1442 | ||
1443 | void DecodeTrace::on_channels_updated() | |
1444 | { | |
1445 | if (owner_) | |
1446 | owner_->row_item_appearance_changed(false, true); | |
4e5a4405 JH |
1447 | } |
1448 | ||
9f97b357 | 1449 | void DecodeTrace::on_init_state_changed(int) |
407c9ebe | 1450 | { |
9f97b357 SA |
1451 | QComboBox *cb = qobject_cast<QComboBox*>(QObject::sender()); |
1452 | ||
1453 | // Determine inital pin state that was selected | |
1454 | int init_state = cb->itemData(cb->currentIndex()).value<int>(); | |
1455 | ||
1456 | // Determine decode channel ID this combo box is the channel selector for | |
1457 | const uint16_t id = init_state_map_.at(cb); | |
1458 | ||
1459 | decode_signal_->set_initial_pin_state(id, init_state); | |
407c9ebe UH |
1460 | } |
1461 | ||
7491a29f JH |
1462 | void DecodeTrace::on_stack_decoder(srd_decoder *decoder) |
1463 | { | |
ad908057 | 1464 | decode_signal_->stack_decoder(decoder); |
84002113 | 1465 | update_rows(); |
37fd11b1 JH |
1466 | |
1467 | create_popup_form(); | |
7491a29f JH |
1468 | } |
1469 | ||
613d097c JH |
1470 | void DecodeTrace::on_delete_decoder(int index) |
1471 | { | |
ad908057 | 1472 | decode_signal_->remove_decoder(index); |
84002113 | 1473 | update_rows(); |
613d097c | 1474 | |
84002113 | 1475 | // Force re-calculation of the trace height |
ded43869 SA |
1476 | max_visible_rows_ = 0; |
1477 | owner_->extents_changed(false, true); | |
1478 | ||
c063290a | 1479 | create_popup_form(); |
613d097c JH |
1480 | } |
1481 | ||
dd048a7e JH |
1482 | void DecodeTrace::on_show_hide_decoder(int index) |
1483 | { | |
ad908057 | 1484 | const bool state = decode_signal_->toggle_decoder_visibility(index); |
dd048a7e | 1485 | |
8dbbc7f0 | 1486 | assert(index < (int)decoder_forms_.size()); |
ad908057 | 1487 | decoder_forms_[index]->set_decoder_visible(state); |
dd048a7e | 1488 | |
ded43869 SA |
1489 | if (!state) { |
1490 | // Force re-calculation of the trace height, see paint_mid() | |
1491 | max_visible_rows_ = 0; | |
1492 | owner_->extents_changed(false, true); | |
1493 | } | |
1494 | ||
c6b4e925 SA |
1495 | owner_->row_item_appearance_changed(false, true); |
1496 | } | |
1497 | ||
6a26fc44 | 1498 | void DecodeTrace::on_show_hide_row(int row_id) |
c6b4e925 | 1499 | { |
6a26fc44 | 1500 | if (row_id >= (int)rows_.size()) |
c6b4e925 SA |
1501 | return; |
1502 | ||
6a26fc44 SA |
1503 | set_row_collapsed(&rows_[row_id]); |
1504 | rows_[row_id].decode_row->set_visible(!rows_[row_id].decode_row->visible()); | |
c6b4e925 SA |
1505 | |
1506 | // Force re-calculation of the trace height, see paint_mid() | |
1507 | max_visible_rows_ = 0; | |
1508 | owner_->extents_changed(false, true); | |
1509 | owner_->row_item_appearance_changed(false, true); | |
dd048a7e JH |
1510 | } |
1511 | ||
6a26fc44 SA |
1512 | void DecodeTrace::on_show_hide_class(QWidget* sender) |
1513 | { | |
1514 | void* ann_class_ptr = sender->property("ann_class_ptr").value<void*>(); | |
1515 | assert(ann_class_ptr); | |
6a26fc44 | 1516 | AnnotationClass* ann_class = (AnnotationClass*)ann_class_ptr; |
81dc0221 | 1517 | |
6a26fc44 SA |
1518 | ann_class->visible = !ann_class->visible; |
1519 | ||
81dc0221 SA |
1520 | void* row_ptr = sender->property("decode_trace_row_ptr").value<void*>(); |
1521 | assert(row_ptr); | |
1522 | DecodeTraceRow* row = (DecodeTraceRow*)row_ptr; | |
1523 | ||
1524 | row->has_hidden_classes = row->decode_row->has_hidden_classes(); | |
1525 | ||
6a26fc44 SA |
1526 | owner_->row_item_appearance_changed(false, true); |
1527 | } | |
1528 | ||
c764c995 SA |
1529 | void DecodeTrace::on_copy_annotation_to_clipboard() |
1530 | { | |
1531 | using namespace pv::data::decode; | |
1532 | ||
1533 | if (!selected_row_) | |
1534 | return; | |
1535 | ||
1536 | vector<Annotation> *annotations = new vector<Annotation>(); | |
1537 | ||
6a26fc44 | 1538 | decode_signal_->get_annotation_subset(*annotations, selected_row_, |
c764c995 SA |
1539 | current_segment_, selected_sample_range_.first, selected_sample_range_.first); |
1540 | ||
1541 | if (annotations->empty()) | |
1542 | return; | |
1543 | ||
628b45cc | 1544 | QClipboard *clipboard = QApplication::clipboard(); |
41aaa675 SA |
1545 | clipboard->setText(annotations->front().annotations().front(), QClipboard::Clipboard); |
1546 | ||
1547 | if (clipboard->supportsSelection()) | |
1548 | clipboard->setText(annotations->front().annotations().front(), QClipboard::Selection); | |
c764c995 SA |
1549 | |
1550 | delete annotations; | |
1551 | } | |
1552 | ||
be843692 SA |
1553 | void DecodeTrace::on_export_row() |
1554 | { | |
99ba5f28 | 1555 | selected_sample_range_ = make_pair(0, numeric_limits<uint64_t>::max()); |
be843692 SA |
1556 | on_export_row_from_here(); |
1557 | } | |
1558 | ||
5a914348 SA |
1559 | void DecodeTrace::on_export_all_rows() |
1560 | { | |
99ba5f28 SA |
1561 | selected_sample_range_ = make_pair(0, numeric_limits<uint64_t>::max()); |
1562 | on_export_all_rows_from_here(); | |
1563 | } | |
1564 | ||
1565 | void DecodeTrace::on_export_row_with_cursor() | |
1566 | { | |
1567 | const View *view = owner_->view(); | |
1568 | assert(view); | |
1569 | ||
1570 | if (!view->cursors()->enabled()) | |
1571 | return; | |
1572 | ||
1573 | const double samplerate = session_.get_samplerate(); | |
1574 | ||
1575 | const pv::util::Timestamp& start_time = view->cursors()->first()->time(); | |
1576 | const pv::util::Timestamp& end_time = view->cursors()->second()->time(); | |
1577 | ||
1578 | const uint64_t start_sample = (uint64_t)max( | |
a20c1fcc | 1579 | 0.0, start_time.convert_to<double>() * samplerate); |
99ba5f28 | 1580 | const uint64_t end_sample = (uint64_t)max( |
a20c1fcc | 1581 | 0.0, end_time.convert_to<double>() * samplerate); |
99ba5f28 SA |
1582 | |
1583 | // Are both cursors negative and thus were clamped to 0? | |
1584 | if ((start_sample == 0) && (end_sample == 0)) | |
1585 | return; | |
1586 | ||
1587 | selected_sample_range_ = make_pair(start_sample, end_sample); | |
1588 | on_export_row_from_here(); | |
1589 | } | |
1590 | ||
1591 | void DecodeTrace::on_export_all_rows_with_cursor() | |
1592 | { | |
1593 | const View *view = owner_->view(); | |
1594 | assert(view); | |
1595 | ||
1596 | if (!view->cursors()->enabled()) | |
1597 | return; | |
1598 | ||
1599 | const double samplerate = session_.get_samplerate(); | |
1600 | ||
1601 | const pv::util::Timestamp& start_time = view->cursors()->first()->time(); | |
1602 | const pv::util::Timestamp& end_time = view->cursors()->second()->time(); | |
1603 | ||
1604 | const uint64_t start_sample = (uint64_t)max( | |
a20c1fcc | 1605 | 0.0, start_time.convert_to<double>() * samplerate); |
99ba5f28 | 1606 | const uint64_t end_sample = (uint64_t)max( |
a20c1fcc | 1607 | 0.0, end_time.convert_to<double>() * samplerate); |
99ba5f28 SA |
1608 | |
1609 | // Are both cursors negative and thus were clamped to 0? | |
1610 | if ((start_sample == 0) && (end_sample == 0)) | |
1611 | return; | |
1612 | ||
1613 | selected_sample_range_ = make_pair(start_sample, end_sample); | |
5a914348 SA |
1614 | on_export_all_rows_from_here(); |
1615 | } | |
1616 | ||
be843692 SA |
1617 | void DecodeTrace::on_export_row_from_here() |
1618 | { | |
1619 | using namespace pv::data::decode; | |
1620 | ||
1621 | if (!selected_row_) | |
1622 | return; | |
1623 | ||
5a914348 | 1624 | vector<Annotation> *annotations = new vector<Annotation>(); |
be843692 | 1625 | |
6a26fc44 | 1626 | decode_signal_->get_annotation_subset(*annotations, selected_row_, |
99ba5f28 | 1627 | current_segment_, selected_sample_range_.first, selected_sample_range_.second); |
be843692 | 1628 | |
5a914348 | 1629 | if (annotations->empty()) |
be843692 SA |
1630 | return; |
1631 | ||
5a914348 SA |
1632 | export_annotations(annotations); |
1633 | delete annotations; | |
1634 | } | |
1ed996b4 | 1635 | |
5a914348 SA |
1636 | void DecodeTrace::on_export_all_rows_from_here() |
1637 | { | |
1638 | using namespace pv::data::decode; | |
1ed996b4 | 1639 | |
5a914348 | 1640 | vector<Annotation> *annotations = new vector<Annotation>(); |
1ed996b4 | 1641 | |
5a914348 | 1642 | decode_signal_->get_annotation_subset(*annotations, current_segment_, |
99ba5f28 | 1643 | selected_sample_range_.first, selected_sample_range_.second); |
be843692 | 1644 | |
5a914348 SA |
1645 | if (!annotations->empty()) |
1646 | export_annotations(annotations); | |
be843692 | 1647 | |
5a914348 | 1648 | delete annotations; |
be843692 SA |
1649 | } |
1650 | ||
74bf6666 SA |
1651 | void DecodeTrace::on_animation_timer() |
1652 | { | |
1653 | bool animation_finished = true; | |
1654 | ||
6a26fc44 | 1655 | for (DecodeTraceRow& r : rows_) { |
74bf6666 SA |
1656 | if (!(r.expanding || r.collapsing)) |
1657 | continue; | |
1658 | ||
1659 | unsigned int height_delta = r.expanded_height - default_row_height_; | |
1660 | ||
1661 | if (r.expanding) { | |
1662 | if (r.height < r.expanded_height) { | |
1663 | r.anim_height += height_delta / (float)AnimationDurationInTicks; | |
1664 | r.height = r.anim_height; | |
1665 | r.anim_shape += ArrowSize / (float)AnimationDurationInTicks; | |
1666 | animation_finished = false; | |
c6b4e925 SA |
1667 | } else |
1668 | set_row_expanded(&r); | |
74bf6666 SA |
1669 | } |
1670 | ||
1671 | if (r.collapsing) { | |
1672 | if (r.height > default_row_height_) { | |
1673 | r.anim_height -= height_delta / (float)AnimationDurationInTicks; | |
1674 | r.height = r.anim_height; | |
1675 | r.anim_shape -= ArrowSize / (float)AnimationDurationInTicks; | |
1676 | animation_finished = false; | |
c6b4e925 SA |
1677 | } else |
1678 | set_row_collapsed(&r); | |
74bf6666 SA |
1679 | } |
1680 | ||
1681 | // The expansion marker shape switches between | |
1682 | // 0/-A, A/0, 0/A (default state; anim_shape=0) and | |
1683 | // 0/ 0, A/A, 2A/0 (expanded state; anim_shape=ArrowSize) | |
1684 | ||
1685 | r.expand_marker_shape.setPoint(0, 0, -ArrowSize + r.anim_shape); | |
1686 | r.expand_marker_shape.setPoint(1, ArrowSize, r.anim_shape); | |
1687 | r.expand_marker_shape.setPoint(2, 2*r.anim_shape, ArrowSize - r.anim_shape); | |
1688 | } | |
1689 | ||
1690 | if (animation_finished) | |
1691 | animation_timer_.stop(); | |
1692 | ||
c6b4e925 SA |
1693 | owner_->extents_changed(false, true); |
1694 | owner_->row_item_appearance_changed(false, true); | |
74bf6666 SA |
1695 | } |
1696 | ||
1573bf16 | 1697 | } // namespace trace |
f4e57597 | 1698 | } // namespace views |
55d3603d | 1699 | } // namespace pv |