]>
Commit | Line | Data |
---|---|---|
24d69d27 SA |
1 | /* |
2 | * This file is part of the PulseView project. | |
3 | * | |
4 | * Copyright (C) 2020 Soeren Apel <soeren@apelpie.net> | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation; either version 2 of the License, or | |
9 | * (at your option) any later version. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * along with this program; if not, see <http://www.gnu.org/licenses/>. | |
18 | */ | |
19 | ||
20 | #include <climits> | |
21 | ||
f54e68b0 | 22 | #include <QApplication> |
24d69d27 SA |
23 | #include <QDebug> |
24 | #include <QFileDialog> | |
f54e68b0 SA |
25 | #include <QFontMetrics> |
26 | #include <QHeaderView> | |
24d69d27 SA |
27 | #include <QLabel> |
28 | #include <QMenu> | |
29 | #include <QMessageBox> | |
30 | #include <QToolBar> | |
31 | #include <QVBoxLayout> | |
32 | ||
33 | #include <libsigrokdecode/libsigrokdecode.h> | |
34 | ||
35 | #include "view.hpp" | |
36 | ||
37 | #include "pv/globalsettings.hpp" | |
9a35b05d | 38 | #include "pv/session.hpp" |
24d69d27 SA |
39 | #include "pv/util.hpp" |
40 | #include "pv/data/decode/decoder.hpp" | |
41 | ||
42 | using pv::data::DecodeSignal; | |
43 | using pv::data::SignalBase; | |
44 | using pv::data::decode::Decoder; | |
45 | using pv::util::Timestamp; | |
46 | ||
f54e68b0 | 47 | using std::make_shared; |
8997f62a | 48 | using std::max; |
24d69d27 SA |
49 | using std::shared_ptr; |
50 | ||
51 | namespace pv { | |
52 | namespace views { | |
53 | namespace tabular_decoder { | |
54 | ||
be0f5903 SA |
55 | const char* SaveTypeNames[SaveTypeCount] = { |
56 | "CSV, commas escaped", | |
57 | "CSV, fields quoted" | |
58 | }; | |
59 | ||
86d4b8e3 SA |
60 | const char* ViewModeNames[ViewModeCount] = { |
61 | "Show all", | |
8997f62a SA |
62 | "Show all and focus on newest", |
63 | "Show visible in main view" | |
86d4b8e3 SA |
64 | }; |
65 | ||
6f43db70 SA |
66 | |
67 | CustomFilterProxyModel::CustomFilterProxyModel(QObject* parent) : | |
939d25cb SA |
68 | QSortFilterProxyModel(parent), |
69 | range_filtering_enabled_(false) | |
6f43db70 SA |
70 | { |
71 | } | |
72 | ||
73 | bool CustomFilterProxyModel::filterAcceptsRow(int sourceRow, | |
74 | const QModelIndex &sourceParent) const | |
75 | { | |
76 | (void)sourceParent; | |
77 | assert(sourceModel() != nullptr); | |
78 | ||
939d25cb | 79 | bool result = true; |
6f43db70 | 80 | |
939d25cb SA |
81 | if (range_filtering_enabled_) { |
82 | const QModelIndex ann_start_sample_idx = sourceModel()->index(sourceRow, 0); | |
83 | const uint64_t ann_start_sample = | |
84 | sourceModel()->data(ann_start_sample_idx, Qt::DisplayRole).toULongLong(); | |
6f43db70 | 85 | |
939d25cb SA |
86 | const QModelIndex ann_end_sample_idx = sourceModel()->index(sourceRow, 6); |
87 | const uint64_t ann_end_sample = | |
88 | sourceModel()->data(ann_end_sample_idx, Qt::DisplayRole).toULongLong(); | |
6f43db70 | 89 | |
939d25cb SA |
90 | // We consider all annotations as visible that either |
91 | // a) begin to the left of the range and end within the range or | |
92 | // b) begin and end within the range or | |
93 | // c) begin within the range and end to the right of the range | |
94 | // ...which is equivalent to the negation of "begins and ends outside the range" | |
6f43db70 | 95 | |
939d25cb SA |
96 | const bool left_of_range = (ann_end_sample < range_start_sample_); |
97 | const bool right_of_range = (ann_start_sample > range_end_sample_); | |
98 | const bool entirely_outside_of_range = left_of_range || right_of_range; | |
99 | ||
100 | result = !entirely_outside_of_range; | |
101 | } | |
102 | ||
103 | return result; | |
6f43db70 SA |
104 | } |
105 | ||
106 | void CustomFilterProxyModel::set_sample_range(uint64_t start_sample, | |
107 | uint64_t end_sample) | |
108 | { | |
109 | range_start_sample_ = start_sample; | |
110 | range_end_sample_ = end_sample; | |
111 | ||
112 | invalidateFilter(); | |
113 | } | |
114 | ||
939d25cb SA |
115 | void CustomFilterProxyModel::enable_range_filtering(bool value) |
116 | { | |
117 | range_filtering_enabled_ = value; | |
118 | ||
119 | invalidateFilter(); | |
120 | } | |
121 | ||
6f43db70 SA |
122 | |
123 | QSize CustomTableView::minimumSizeHint() const | |
f54e68b0 SA |
124 | { |
125 | QSize size(QTableView::sizeHint()); | |
126 | ||
127 | int width = 0; | |
128 | for (int i = 0; i < horizontalHeader()->count(); i++) | |
129 | if (!horizontalHeader()->isSectionHidden(i)) | |
88a25978 | 130 | width += horizontalHeader()->sectionSize(i); |
f54e68b0 SA |
131 | |
132 | size.setWidth(width + (horizontalHeader()->count() * 1)); | |
133 | ||
134 | return size; | |
135 | } | |
136 | ||
6f43db70 | 137 | QSize CustomTableView::sizeHint() const |
f54e68b0 SA |
138 | { |
139 | return minimumSizeHint(); | |
140 | } | |
141 | ||
24d69d27 SA |
142 | |
143 | View::View(Session &session, bool is_main_view, QMainWindow *parent) : | |
144 | ViewBase(session, is_main_view, parent), | |
145 | ||
146 | // Note: Place defaults in View::reset_view_state(), not here | |
147 | parent_(parent), | |
148 | decoder_selector_(new QComboBox()), | |
86d4b8e3 SA |
149 | hide_hidden_cb_(new QCheckBox()), |
150 | view_mode_selector_(new QComboBox()), | |
24d69d27 SA |
151 | save_button_(new QToolButton()), |
152 | save_action_(new QAction(this)), | |
6f43db70 SA |
153 | table_view_(new CustomTableView()), |
154 | model_(new AnnotationCollectionModel(this)), | |
155 | filter_proxy_model_(new CustomFilterProxyModel(this)), | |
6d46525f | 156 | signal_(nullptr) |
24d69d27 SA |
157 | { |
158 | QVBoxLayout *root_layout = new QVBoxLayout(this); | |
159 | root_layout->setContentsMargins(0, 0, 0, 0); | |
160 | root_layout->addWidget(table_view_); | |
161 | ||
162 | // Create toolbar | |
163 | QToolBar* toolbar = new QToolBar(); | |
164 | toolbar->setContextMenuPolicy(Qt::PreventContextMenu); | |
165 | parent->addToolBar(toolbar); | |
166 | ||
167 | // Populate toolbar | |
168 | toolbar->addWidget(new QLabel(tr("Decoder:"))); | |
169 | toolbar->addWidget(decoder_selector_); | |
170 | toolbar->addSeparator(); | |
171 | toolbar->addWidget(save_button_); | |
86d4b8e3 SA |
172 | toolbar->addSeparator(); |
173 | toolbar->addWidget(view_mode_selector_); | |
174 | toolbar->addSeparator(); | |
175 | toolbar->addWidget(hide_hidden_cb_); | |
24d69d27 SA |
176 | |
177 | connect(decoder_selector_, SIGNAL(currentIndexChanged(int)), | |
178 | this, SLOT(on_selected_decoder_changed(int))); | |
86d4b8e3 SA |
179 | connect(view_mode_selector_, SIGNAL(currentIndexChanged(int)), |
180 | this, SLOT(on_view_mode_changed(int))); | |
181 | connect(hide_hidden_cb_, SIGNAL(toggled(bool)), | |
182 | this, SLOT(on_hide_hidden_changed(bool))); | |
24d69d27 SA |
183 | |
184 | // Configure widgets | |
185 | decoder_selector_->setSizeAdjustPolicy(QComboBox::AdjustToContents); | |
186 | ||
86d4b8e3 SA |
187 | for (int i = 0; i < ViewModeCount; i++) |
188 | view_mode_selector_->addItem(ViewModeNames[i], QVariant::fromValue(i)); | |
189 | ||
190 | hide_hidden_cb_->setText(tr("Hide Hidden Rows/Classes")); | |
191 | hide_hidden_cb_->setChecked(true); | |
192 | ||
24d69d27 SA |
193 | // Configure actions |
194 | save_action_->setText(tr("&Save...")); | |
195 | save_action_->setIcon(QIcon::fromTheme("document-save-as", | |
196 | QIcon(":/icons/document-save-as.png"))); | |
197 | save_action_->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_S)); | |
198 | connect(save_action_, SIGNAL(triggered(bool)), | |
199 | this, SLOT(on_actionSave_triggered())); | |
200 | ||
201 | QMenu *save_menu = new QMenu(); | |
202 | connect(save_menu, SIGNAL(triggered(QAction*)), | |
203 | this, SLOT(on_actionSave_triggered(QAction*))); | |
204 | ||
be0f5903 SA |
205 | for (int i = 0; i < SaveTypeCount; i++) { |
206 | QAction *const action = save_menu->addAction(tr(SaveTypeNames[i])); | |
009fc9ae | 207 | action->setData(QVariant::fromValue(i)); |
be0f5903 SA |
208 | } |
209 | ||
24d69d27 SA |
210 | save_button_->setMenu(save_menu); |
211 | save_button_->setDefaultAction(save_action_); | |
212 | save_button_->setPopupMode(QToolButton::MenuButtonPopup); | |
213 | ||
6f43db70 SA |
214 | // Set up the models and the table view |
215 | filter_proxy_model_->setSourceModel(model_); | |
216 | table_view_->setModel(filter_proxy_model_); | |
217 | ||
9a35b05d | 218 | table_view_->setSelectionBehavior(QAbstractItemView::SelectRows); |
be0f5903 | 219 | table_view_->setSelectionMode(QAbstractItemView::ContiguousSelection); |
6f43db70 | 220 | table_view_->setSortingEnabled(true); |
24d69d27 SA |
221 | table_view_->sortByColumn(0, Qt::AscendingOrder); |
222 | ||
6f43db70 SA |
223 | for (uint8_t i = model_->first_hidden_column(); i < model_->columnCount(); i++) |
224 | table_view_->setColumnHidden(i, true); | |
225 | ||
f54e68b0 SA |
226 | const int font_height = QFontMetrics(QApplication::font()).height(); |
227 | table_view_->verticalHeader()->setDefaultSectionSize((font_height * 5) / 4); | |
2a89c44b | 228 | table_view_->verticalHeader()->setVisible(false); |
f54e68b0 | 229 | |
88a25978 SA |
230 | table_view_->horizontalHeader()->setStretchLastSection(true); |
231 | table_view_->horizontalHeader()->setCascadingSectionResizes(true); | |
232 | table_view_->horizontalHeader()->setSectionsMovable(true); | |
9a35b05d | 233 | table_view_->horizontalHeader()->setContextMenuPolicy(Qt::CustomContextMenu); |
f54e68b0 SA |
234 | |
235 | table_view_->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); | |
236 | parent->setSizePolicy(table_view_->sizePolicy()); | |
237 | ||
9a35b05d SA |
238 | connect(table_view_, SIGNAL(clicked(const QModelIndex&)), |
239 | this, SLOT(on_table_item_clicked(const QModelIndex&))); | |
240 | connect(table_view_, SIGNAL(doubleClicked(const QModelIndex&)), | |
241 | this, SLOT(on_table_item_double_clicked(const QModelIndex&))); | |
242 | connect(table_view_->horizontalHeader(), SIGNAL(customContextMenuRequested(const QPoint&)), | |
243 | this, SLOT(on_table_header_requested(const QPoint&))); | |
244 | ||
8997f62a SA |
245 | // Set up metadata event handler |
246 | session_.metadata_obj_manager()->add_observer(this); | |
247 | ||
24d69d27 SA |
248 | reset_view_state(); |
249 | } | |
250 | ||
8997f62a SA |
251 | View::~View() |
252 | { | |
253 | session_.metadata_obj_manager()->remove_observer(this); | |
254 | } | |
255 | ||
24d69d27 SA |
256 | ViewType View::get_type() const |
257 | { | |
258 | return ViewTypeTabularDecoder; | |
259 | } | |
260 | ||
261 | void View::reset_view_state() | |
262 | { | |
263 | ViewBase::reset_view_state(); | |
264 | ||
265 | decoder_selector_->clear(); | |
266 | } | |
267 | ||
268 | void View::clear_decode_signals() | |
269 | { | |
270 | ViewBase::clear_decode_signals(); | |
271 | ||
272 | reset_data(); | |
273 | reset_view_state(); | |
274 | } | |
275 | ||
276 | void View::add_decode_signal(shared_ptr<data::DecodeSignal> signal) | |
277 | { | |
278 | ViewBase::add_decode_signal(signal); | |
279 | ||
280 | connect(signal.get(), SIGNAL(name_changed(const QString&)), | |
281 | this, SLOT(on_signal_name_changed(const QString&))); | |
88a25978 SA |
282 | |
283 | // Note: At time of initial creation, decode signals have no decoders so we | |
284 | // need to watch for decoder stacking events | |
285 | ||
24d69d27 SA |
286 | connect(signal.get(), SIGNAL(decoder_stacked(void*)), |
287 | this, SLOT(on_decoder_stacked(void*))); | |
288 | connect(signal.get(), SIGNAL(decoder_removed(void*)), | |
289 | this, SLOT(on_decoder_removed(void*))); | |
290 | ||
88a25978 | 291 | // Add the top-level decoder provided by an already-existing signal |
24d69d27 | 292 | auto stack = signal->decoder_stack(); |
f54e68b0 SA |
293 | if (!stack.empty()) { |
294 | shared_ptr<Decoder>& dec = stack.at(0); | |
295 | decoder_selector_->addItem(signal->name(), QVariant::fromValue((void*)dec.get())); | |
296 | } | |
24d69d27 SA |
297 | } |
298 | ||
299 | void View::remove_decode_signal(shared_ptr<data::DecodeSignal> signal) | |
300 | { | |
301 | // Remove all decoders provided by this signal | |
302 | for (const shared_ptr<Decoder>& dec : signal->decoder_stack()) { | |
303 | int index = decoder_selector_->findData(QVariant::fromValue((void*)dec.get())); | |
304 | ||
305 | if (index != -1) | |
306 | decoder_selector_->removeItem(index); | |
307 | } | |
308 | ||
309 | ViewBase::remove_decode_signal(signal); | |
310 | ||
311 | if (signal.get() == signal_) { | |
312 | reset_data(); | |
313 | update_data(); | |
314 | reset_view_state(); | |
315 | } | |
316 | } | |
317 | ||
318 | void View::save_settings(QSettings &settings) const | |
319 | { | |
320 | ViewBase::save_settings(settings); | |
86d4b8e3 SA |
321 | |
322 | settings.setValue("view_mode", view_mode_selector_->currentIndex()); | |
323 | settings.setValue("hide_hidden", hide_hidden_cb_->isChecked()); | |
24d69d27 SA |
324 | } |
325 | ||
326 | void View::restore_settings(QSettings &settings) | |
327 | { | |
24d69d27 | 328 | ViewBase::restore_settings(settings); |
86d4b8e3 SA |
329 | |
330 | if (settings.contains("view_mode")) | |
331 | view_mode_selector_->setCurrentIndex(settings.value("view_mode").toInt()); | |
332 | ||
333 | if (settings.contains("hide_hidden")) | |
334 | hide_hidden_cb_->setChecked(settings.value("hide_hidden").toBool()); | |
24d69d27 SA |
335 | } |
336 | ||
337 | void View::reset_data() | |
338 | { | |
339 | signal_ = nullptr; | |
340 | decoder_ = nullptr; | |
341 | } | |
342 | ||
343 | void View::update_data() | |
344 | { | |
f54e68b0 | 345 | model_->set_signal_and_segment(signal_, current_segment_); |
24d69d27 SA |
346 | } |
347 | ||
be0f5903 | 348 | void View::save_data_as_csv(unsigned int save_type) const |
24d69d27 | 349 | { |
be0f5903 SA |
350 | // Note: We try to follow RFC 4180 (https://tools.ietf.org/html/rfc4180) |
351 | ||
24d69d27 SA |
352 | assert(decoder_); |
353 | assert(signal_); | |
354 | ||
355 | if (!signal_) | |
356 | return; | |
357 | ||
be0f5903 SA |
358 | const bool save_all = !table_view_->selectionModel()->hasSelection(); |
359 | ||
360 | GlobalSettings settings; | |
24d69d27 SA |
361 | const QString dir = settings.value("MainWindow/SaveDirectory").toString(); |
362 | ||
363 | const QString file_name = QFileDialog::getSaveFileName( | |
be0f5903 | 364 | parent_, tr("Save Annotations as CSV"), dir, tr("CSV Files (*.csv);;Text Files (*.txt);;All Files (*)")); |
24d69d27 SA |
365 | |
366 | if (file_name.isEmpty()) | |
367 | return; | |
368 | ||
369 | QFile file(file_name); | |
370 | if (file.open(QIODevice::WriteOnly | QIODevice::Truncate)) { | |
be0f5903 SA |
371 | QTextStream out_stream(&file); |
372 | ||
373 | if (save_all) | |
374 | table_view_->selectAll(); | |
375 | ||
376 | // Write out header columns in visual order, not logical order | |
377 | for (int i = 0; i < table_view_->horizontalHeader()->count(); i++) { | |
378 | int column = table_view_->horizontalHeader()->logicalIndex(i); | |
379 | ||
380 | if (table_view_->horizontalHeader()->isSectionHidden(column)) | |
381 | continue; | |
382 | ||
939d25cb | 383 | const QString title = filter_proxy_model_->headerData(column, Qt::Horizontal, Qt::DisplayRole).toString(); |
be0f5903 SA |
384 | |
385 | if (save_type == SaveTypeCSVEscaped) | |
386 | out_stream << title; | |
387 | else | |
388 | out_stream << '"' << title << '"'; | |
389 | ||
390 | if (i < (table_view_->horizontalHeader()->count() - 1)) | |
391 | out_stream << ","; | |
392 | } | |
393 | out_stream << '\r' << '\n'; | |
24d69d27 | 394 | |
24d69d27 | 395 | |
be0f5903 | 396 | QModelIndexList selected_rows = table_view_->selectionModel()->selectedRows(); |
24d69d27 | 397 | |
be0f5903 SA |
398 | for (int i = 0; i < selected_rows.size(); i++) { |
399 | const int row = selected_rows.at(i).row(); | |
400 | ||
401 | // Write out columns in visual order, not logical order | |
402 | for (int c = 0; c < table_view_->horizontalHeader()->count(); c++) { | |
403 | const int column = table_view_->horizontalHeader()->logicalIndex(c); | |
404 | ||
405 | if (table_view_->horizontalHeader()->isSectionHidden(column)) | |
406 | continue; | |
407 | ||
939d25cb SA |
408 | const QModelIndex idx = filter_proxy_model_->index(row, column); |
409 | QString s = filter_proxy_model_->data(idx, Qt::DisplayRole).toString(); | |
be0f5903 SA |
410 | |
411 | if (save_type == SaveTypeCSVEscaped) | |
412 | out_stream << s.replace(",", "\\,"); | |
413 | else | |
414 | out_stream << '"' << s.replace("\"", "\"\"") << '"'; | |
415 | ||
416 | if (c < (table_view_->horizontalHeader()->count() - 1)) | |
417 | out_stream << ","; | |
418 | } | |
419 | ||
420 | out_stream << '\r' << '\n'; | |
421 | } | |
422 | ||
423 | if (out_stream.status() == QTextStream::Ok) { | |
424 | if (save_all) | |
425 | table_view_->clearSelection(); | |
24d69d27 | 426 | |
24d69d27 SA |
427 | return; |
428 | } | |
be0f5903 SA |
429 | } |
430 | ||
431 | QMessageBox msg(parent_); | |
432 | msg.setText(tr("Error") + "\n\n" + tr("File %1 could not be written to.").arg(file_name)); | |
433 | msg.setStandardButtons(QMessageBox::Ok); | |
434 | msg.setIcon(QMessageBox::Warning); | |
435 | msg.exec(); | |
24d69d27 SA |
436 | } |
437 | ||
438 | void View::on_selected_decoder_changed(int index) | |
439 | { | |
02c87df7 | 440 | if (signal_) { |
85125b0f | 441 | disconnect(signal_, SIGNAL(color_changed(QColor))); |
24d69d27 | 442 | disconnect(signal_, SIGNAL(new_annotations())); |
02c87df7 SA |
443 | disconnect(signal_, SIGNAL(decode_reset())); |
444 | } | |
24d69d27 SA |
445 | |
446 | reset_data(); | |
447 | ||
448 | decoder_ = (Decoder*)decoder_selector_->itemData(index).value<void*>(); | |
449 | ||
450 | // Find the signal that contains the selected decoder | |
451 | for (const shared_ptr<DecodeSignal>& ds : decode_signals_) | |
452 | for (const shared_ptr<Decoder>& dec : ds->decoder_stack()) | |
453 | if (decoder_ == dec.get()) | |
454 | signal_ = ds.get(); | |
455 | ||
02c87df7 | 456 | if (signal_) { |
88a25978 | 457 | connect(signal_, SIGNAL(color_changed(QColor)), this, SLOT(on_signal_color_changed(QColor))); |
24d69d27 | 458 | connect(signal_, SIGNAL(new_annotations()), this, SLOT(on_new_annotations())); |
02c87df7 SA |
459 | connect(signal_, SIGNAL(decode_reset()), this, SLOT(on_decoder_reset())); |
460 | } | |
24d69d27 SA |
461 | |
462 | update_data(); | |
463 | } | |
464 | ||
86d4b8e3 SA |
465 | void View::on_hide_hidden_changed(bool checked) |
466 | { | |
467 | model_->set_hide_hidden(checked); | |
468 | ||
469 | // Force repaint, otherwise the new selection isn't shown for some reason | |
470 | table_view_->viewport()->update(); | |
471 | } | |
472 | ||
473 | void View::on_view_mode_changed(int index) | |
474 | { | |
939d25cb SA |
475 | if (index == ViewModeAll) |
476 | filter_proxy_model_->enable_range_filtering(false); | |
477 | ||
8997f62a SA |
478 | if (index == ViewModeVisible) { |
479 | MetadataObject *md_obj = | |
480 | session_.metadata_obj_manager()->find_object_by_type(MetadataObjMainViewRange); | |
481 | assert(md_obj); | |
482 | ||
483 | int64_t start_sample = md_obj->value(MetadataValueStartSample).toLongLong(); | |
484 | int64_t end_sample = md_obj->value(MetadataValueEndSample).toLongLong(); | |
485 | ||
939d25cb | 486 | filter_proxy_model_->enable_range_filtering(true); |
6f43db70 | 487 | filter_proxy_model_->set_sample_range(max((int64_t)0, start_sample), |
8997f62a | 488 | max((int64_t)0, end_sample)); |
8997f62a SA |
489 | } |
490 | ||
939d25cb SA |
491 | if (index == ViewModeLatest) { |
492 | filter_proxy_model_->enable_range_filtering(false); | |
493 | ||
6f43db70 SA |
494 | table_view_->scrollTo( |
495 | filter_proxy_model_->mapFromSource(model_->index(model_->rowCount() - 1, 0)), | |
8997f62a | 496 | QAbstractItemView::PositionAtBottom); |
939d25cb | 497 | } |
86d4b8e3 SA |
498 | } |
499 | ||
24d69d27 SA |
500 | void View::on_signal_name_changed(const QString &name) |
501 | { | |
502 | (void)name; | |
503 | ||
504 | SignalBase* sb = qobject_cast<SignalBase*>(QObject::sender()); | |
505 | assert(sb); | |
506 | ||
507 | DecodeSignal* signal = dynamic_cast<DecodeSignal*>(sb); | |
508 | assert(signal); | |
509 | ||
f54e68b0 | 510 | // Update the top-level decoder provided by this signal |
24d69d27 | 511 | auto stack = signal->decoder_stack(); |
f54e68b0 SA |
512 | if (!stack.empty()) { |
513 | shared_ptr<Decoder>& dec = stack.at(0); | |
514 | int index = decoder_selector_->findData(QVariant::fromValue((void*)dec.get())); | |
24d69d27 | 515 | |
f54e68b0 SA |
516 | if (index != -1) |
517 | decoder_selector_->setItemText(index, signal->name()); | |
518 | } | |
24d69d27 SA |
519 | } |
520 | ||
88a25978 SA |
521 | void View::on_signal_color_changed(const QColor &color) |
522 | { | |
523 | (void)color; | |
524 | ||
525 | table_view_->update(); | |
526 | } | |
527 | ||
24d69d27 SA |
528 | void View::on_new_annotations() |
529 | { | |
c84afcfd SA |
530 | if (view_mode_selector_->currentIndex() == ViewModeLatest) { |
531 | update_data(); | |
5a5d3b1d SA |
532 | table_view_->scrollTo( |
533 | filter_proxy_model_->index(filter_proxy_model_->rowCount() - 1, 0), | |
c84afcfd SA |
534 | QAbstractItemView::PositionAtBottom); |
535 | } else { | |
536 | if (!delayed_view_updater_.isActive()) | |
537 | delayed_view_updater_.start(); | |
538 | } | |
24d69d27 SA |
539 | } |
540 | ||
02c87df7 SA |
541 | void View::on_decoder_reset() |
542 | { | |
543 | // Invalidate the model's data connection immediately - otherwise we | |
544 | // will use a stale pointer in model_->index() when called from the table view | |
545 | model_->set_signal_and_segment(signal_, current_segment_); | |
546 | } | |
547 | ||
24d69d27 SA |
548 | void View::on_decoder_stacked(void* decoder) |
549 | { | |
24d69d27 SA |
550 | Decoder* d = static_cast<Decoder*>(decoder); |
551 | ||
552 | // Find the signal that contains the selected decoder | |
553 | DecodeSignal* signal = nullptr; | |
554 | ||
555 | for (const shared_ptr<DecodeSignal>& ds : decode_signals_) | |
556 | for (const shared_ptr<Decoder>& dec : ds->decoder_stack()) | |
557 | if (d == dec.get()) | |
558 | signal = ds.get(); | |
559 | ||
560 | assert(signal); | |
561 | ||
88a25978 SA |
562 | const shared_ptr<Decoder>& dec = signal->decoder_stack().at(0); |
563 | int index = decoder_selector_->findData(QVariant::fromValue((void*)dec.get())); | |
564 | ||
565 | if (index == -1) { | |
566 | // Add the decoder to the list | |
85125b0f | 567 | decoder_selector_->addItem(signal->name(), QVariant::fromValue((void*)d)); |
88a25978 | 568 | } |
24d69d27 SA |
569 | } |
570 | ||
571 | void View::on_decoder_removed(void* decoder) | |
572 | { | |
573 | Decoder* d = static_cast<Decoder*>(decoder); | |
574 | ||
575 | // Remove the decoder from the list | |
576 | int index = decoder_selector_->findData(QVariant::fromValue((void*)d)); | |
577 | ||
578 | if (index != -1) | |
579 | decoder_selector_->removeItem(index); | |
580 | } | |
581 | ||
582 | void View::on_actionSave_triggered(QAction* action) | |
583 | { | |
be0f5903 SA |
584 | int save_type = SaveTypeCSVQuoted; |
585 | ||
586 | if (action) | |
587 | save_type = action->data().toInt(); | |
24d69d27 | 588 | |
be0f5903 | 589 | save_data_as_csv(save_type); |
24d69d27 SA |
590 | } |
591 | ||
9a35b05d SA |
592 | void View::on_table_item_clicked(const QModelIndex& index) |
593 | { | |
594 | (void)index; | |
595 | ||
596 | // Force repaint, otherwise the new selection isn't shown for some reason | |
597 | table_view_->viewport()->update(); | |
598 | } | |
599 | ||
600 | void View::on_table_item_double_clicked(const QModelIndex& index) | |
601 | { | |
49a0a403 SA |
602 | const QModelIndex src_idx = filter_proxy_model_->mapToSource(index); |
603 | ||
604 | const Annotation* ann = static_cast<const Annotation*>(src_idx.internalPointer()); | |
605 | assert(ann); | |
9a35b05d SA |
606 | |
607 | shared_ptr<views::ViewBase> main_view = session_.main_view(); | |
608 | ||
609 | main_view->focus_on_range(ann->start_sample(), ann->end_sample()); | |
610 | } | |
611 | ||
612 | void View::on_table_header_requested(const QPoint& pos) | |
613 | { | |
614 | QMenu* menu = new QMenu(this); | |
615 | ||
616 | for (int i = 0; i < table_view_->horizontalHeader()->count(); i++) { | |
617 | int column = table_view_->horizontalHeader()->logicalIndex(i); | |
618 | ||
49a0a403 SA |
619 | const QString title = |
620 | filter_proxy_model_->headerData(column, Qt::Horizontal, Qt::DisplayRole).toString(); | |
9a35b05d SA |
621 | QAction* action = new QAction(title, this); |
622 | ||
623 | action->setCheckable(true); | |
624 | action->setChecked(!table_view_->horizontalHeader()->isSectionHidden(column)); | |
625 | action->setData(column); | |
626 | ||
627 | connect(action, SIGNAL(toggled(bool)), this, SLOT(on_table_header_toggled(bool))); | |
628 | ||
629 | menu->addAction(action); | |
630 | } | |
631 | ||
632 | menu->popup(table_view_->horizontalHeader()->viewport()->mapToGlobal(pos)); | |
633 | } | |
634 | ||
635 | void View::on_table_header_toggled(bool checked) | |
636 | { | |
637 | QAction* action = qobject_cast<QAction*>(QObject::sender()); | |
638 | assert(action); | |
639 | ||
640 | const int column = action->data().toInt(); | |
641 | ||
642 | table_view_->horizontalHeader()->setSectionHidden(column, !checked); | |
643 | } | |
644 | ||
8997f62a SA |
645 | void View::on_metadata_object_changed(MetadataObject* obj, |
646 | MetadataValueType value_type) | |
647 | { | |
648 | // Check if we need to update the model's data range. We only work on the | |
649 | // end sample value because the start sample value is updated first and | |
650 | // we don't want to update the model twice | |
651 | ||
652 | if ((view_mode_selector_->currentIndex() == ViewModeVisible) && | |
653 | (obj->type() == MetadataObjMainViewRange) && | |
654 | (value_type == MetadataValueEndSample)) { | |
655 | ||
656 | int64_t start_sample = obj->value(MetadataValueStartSample).toLongLong(); | |
657 | int64_t end_sample = obj->value(MetadataValueEndSample).toLongLong(); | |
658 | ||
6f43db70 | 659 | filter_proxy_model_->set_sample_range(max((int64_t)0, start_sample), |
8997f62a SA |
660 | max((int64_t)0, end_sample)); |
661 | } | |
1c521100 SA |
662 | |
663 | if (obj->type() == MetadataObjMousePos) { | |
6f43db70 SA |
664 | QModelIndex first_visible_idx = |
665 | filter_proxy_model_->mapToSource(filter_proxy_model_->index(0, 0)); | |
666 | QModelIndex last_visible_idx = | |
667 | filter_proxy_model_->mapToSource(filter_proxy_model_->index(filter_proxy_model_->rowCount() - 1, 0)); | |
668 | ||
669 | if (first_visible_idx.isValid()) { | |
670 | const QModelIndex first_highlighted_idx = | |
671 | model_->update_highlighted_rows(first_visible_idx, last_visible_idx, | |
672 | obj->value(MetadataValueStartSample).toLongLong()); | |
673 | ||
674 | if (view_mode_selector_->currentIndex() == ViewModeVisible) { | |
675 | const QModelIndex idx = filter_proxy_model_->mapFromSource(first_highlighted_idx); | |
676 | table_view_->scrollTo(idx, QAbstractItemView::EnsureVisible); | |
677 | } | |
49a0a403 SA |
678 | |
679 | // Force repaint, otherwise the table doesn't immediately update for some reason | |
680 | table_view_->viewport()->update(); | |
6f43db70 | 681 | } |
1c521100 | 682 | } |
8997f62a SA |
683 | } |
684 | ||
24d69d27 SA |
685 | void View::perform_delayed_view_update() |
686 | { | |
687 | update_data(); | |
688 | } | |
689 | ||
690 | ||
691 | } // namespace tabular_decoder | |
692 | } // namespace views | |
693 | } // namespace pv |