]>
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 | ||
20c99cfc SA |
142 | void CustomTableView::keyPressEvent(QKeyEvent *event) |
143 | { | |
144 | if ((event->key() == Qt::Key_Return) || (event->key() == Qt::Key_Enter)) | |
145 | activatedByKey(currentIndex()); | |
146 | else | |
147 | QTableView::keyPressEvent(event); | |
148 | } | |
149 | ||
24d69d27 SA |
150 | |
151 | View::View(Session &session, bool is_main_view, QMainWindow *parent) : | |
152 | ViewBase(session, is_main_view, parent), | |
153 | ||
154 | // Note: Place defaults in View::reset_view_state(), not here | |
155 | parent_(parent), | |
156 | decoder_selector_(new QComboBox()), | |
86d4b8e3 SA |
157 | hide_hidden_cb_(new QCheckBox()), |
158 | view_mode_selector_(new QComboBox()), | |
24d69d27 SA |
159 | save_button_(new QToolButton()), |
160 | save_action_(new QAction(this)), | |
6f43db70 SA |
161 | table_view_(new CustomTableView()), |
162 | model_(new AnnotationCollectionModel(this)), | |
163 | filter_proxy_model_(new CustomFilterProxyModel(this)), | |
6d46525f | 164 | signal_(nullptr) |
24d69d27 SA |
165 | { |
166 | QVBoxLayout *root_layout = new QVBoxLayout(this); | |
167 | root_layout->setContentsMargins(0, 0, 0, 0); | |
168 | root_layout->addWidget(table_view_); | |
169 | ||
170 | // Create toolbar | |
171 | QToolBar* toolbar = new QToolBar(); | |
172 | toolbar->setContextMenuPolicy(Qt::PreventContextMenu); | |
173 | parent->addToolBar(toolbar); | |
174 | ||
175 | // Populate toolbar | |
176 | toolbar->addWidget(new QLabel(tr("Decoder:"))); | |
177 | toolbar->addWidget(decoder_selector_); | |
178 | toolbar->addSeparator(); | |
179 | toolbar->addWidget(save_button_); | |
86d4b8e3 SA |
180 | toolbar->addSeparator(); |
181 | toolbar->addWidget(view_mode_selector_); | |
182 | toolbar->addSeparator(); | |
183 | toolbar->addWidget(hide_hidden_cb_); | |
24d69d27 SA |
184 | |
185 | connect(decoder_selector_, SIGNAL(currentIndexChanged(int)), | |
186 | this, SLOT(on_selected_decoder_changed(int))); | |
86d4b8e3 SA |
187 | connect(view_mode_selector_, SIGNAL(currentIndexChanged(int)), |
188 | this, SLOT(on_view_mode_changed(int))); | |
189 | connect(hide_hidden_cb_, SIGNAL(toggled(bool)), | |
190 | this, SLOT(on_hide_hidden_changed(bool))); | |
24d69d27 SA |
191 | |
192 | // Configure widgets | |
193 | decoder_selector_->setSizeAdjustPolicy(QComboBox::AdjustToContents); | |
194 | ||
86d4b8e3 SA |
195 | for (int i = 0; i < ViewModeCount; i++) |
196 | view_mode_selector_->addItem(ViewModeNames[i], QVariant::fromValue(i)); | |
197 | ||
198 | hide_hidden_cb_->setText(tr("Hide Hidden Rows/Classes")); | |
199 | hide_hidden_cb_->setChecked(true); | |
200 | ||
24d69d27 SA |
201 | // Configure actions |
202 | save_action_->setText(tr("&Save...")); | |
203 | save_action_->setIcon(QIcon::fromTheme("document-save-as", | |
204 | QIcon(":/icons/document-save-as.png"))); | |
1ed73ebd VPP |
205 | #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) |
206 | save_action_->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_S)); | |
207 | #else | |
24d69d27 | 208 | save_action_->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_S)); |
1ed73ebd | 209 | #endif |
24d69d27 SA |
210 | connect(save_action_, SIGNAL(triggered(bool)), |
211 | this, SLOT(on_actionSave_triggered())); | |
212 | ||
213 | QMenu *save_menu = new QMenu(); | |
214 | connect(save_menu, SIGNAL(triggered(QAction*)), | |
215 | this, SLOT(on_actionSave_triggered(QAction*))); | |
216 | ||
be0f5903 SA |
217 | for (int i = 0; i < SaveTypeCount; i++) { |
218 | QAction *const action = save_menu->addAction(tr(SaveTypeNames[i])); | |
009fc9ae | 219 | action->setData(QVariant::fromValue(i)); |
be0f5903 SA |
220 | } |
221 | ||
24d69d27 SA |
222 | save_button_->setMenu(save_menu); |
223 | save_button_->setDefaultAction(save_action_); | |
224 | save_button_->setPopupMode(QToolButton::MenuButtonPopup); | |
225 | ||
6f43db70 SA |
226 | // Set up the models and the table view |
227 | filter_proxy_model_->setSourceModel(model_); | |
228 | table_view_->setModel(filter_proxy_model_); | |
229 | ||
9a35b05d | 230 | table_view_->setSelectionBehavior(QAbstractItemView::SelectRows); |
be0f5903 | 231 | table_view_->setSelectionMode(QAbstractItemView::ContiguousSelection); |
6f43db70 | 232 | table_view_->setSortingEnabled(true); |
24d69d27 SA |
233 | table_view_->sortByColumn(0, Qt::AscendingOrder); |
234 | ||
6f43db70 SA |
235 | for (uint8_t i = model_->first_hidden_column(); i < model_->columnCount(); i++) |
236 | table_view_->setColumnHidden(i, true); | |
237 | ||
f54e68b0 SA |
238 | const int font_height = QFontMetrics(QApplication::font()).height(); |
239 | table_view_->verticalHeader()->setDefaultSectionSize((font_height * 5) / 4); | |
2a89c44b | 240 | table_view_->verticalHeader()->setVisible(false); |
f54e68b0 | 241 | |
88a25978 SA |
242 | table_view_->horizontalHeader()->setStretchLastSection(true); |
243 | table_view_->horizontalHeader()->setCascadingSectionResizes(true); | |
244 | table_view_->horizontalHeader()->setSectionsMovable(true); | |
9a35b05d | 245 | table_view_->horizontalHeader()->setContextMenuPolicy(Qt::CustomContextMenu); |
f54e68b0 SA |
246 | |
247 | table_view_->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); | |
248 | parent->setSizePolicy(table_view_->sizePolicy()); | |
249 | ||
9a35b05d SA |
250 | connect(table_view_, SIGNAL(clicked(const QModelIndex&)), |
251 | this, SLOT(on_table_item_clicked(const QModelIndex&))); | |
252 | connect(table_view_, SIGNAL(doubleClicked(const QModelIndex&)), | |
253 | this, SLOT(on_table_item_double_clicked(const QModelIndex&))); | |
20c99cfc SA |
254 | connect(table_view_, SIGNAL(activatedByKey(const QModelIndex&)), |
255 | this, SLOT(on_table_item_double_clicked(const QModelIndex&))); | |
9a35b05d SA |
256 | connect(table_view_->horizontalHeader(), SIGNAL(customContextMenuRequested(const QPoint&)), |
257 | this, SLOT(on_table_header_requested(const QPoint&))); | |
258 | ||
8997f62a SA |
259 | // Set up metadata event handler |
260 | session_.metadata_obj_manager()->add_observer(this); | |
261 | ||
24d69d27 SA |
262 | reset_view_state(); |
263 | } | |
264 | ||
8997f62a SA |
265 | View::~View() |
266 | { | |
267 | session_.metadata_obj_manager()->remove_observer(this); | |
268 | } | |
269 | ||
24d69d27 SA |
270 | ViewType View::get_type() const |
271 | { | |
272 | return ViewTypeTabularDecoder; | |
273 | } | |
274 | ||
275 | void View::reset_view_state() | |
276 | { | |
277 | ViewBase::reset_view_state(); | |
278 | ||
279 | decoder_selector_->clear(); | |
280 | } | |
281 | ||
282 | void View::clear_decode_signals() | |
283 | { | |
284 | ViewBase::clear_decode_signals(); | |
285 | ||
286 | reset_data(); | |
287 | reset_view_state(); | |
288 | } | |
289 | ||
290 | void View::add_decode_signal(shared_ptr<data::DecodeSignal> signal) | |
291 | { | |
292 | ViewBase::add_decode_signal(signal); | |
293 | ||
294 | connect(signal.get(), SIGNAL(name_changed(const QString&)), | |
295 | this, SLOT(on_signal_name_changed(const QString&))); | |
88a25978 SA |
296 | |
297 | // Note: At time of initial creation, decode signals have no decoders so we | |
298 | // need to watch for decoder stacking events | |
299 | ||
24d69d27 SA |
300 | connect(signal.get(), SIGNAL(decoder_stacked(void*)), |
301 | this, SLOT(on_decoder_stacked(void*))); | |
302 | connect(signal.get(), SIGNAL(decoder_removed(void*)), | |
303 | this, SLOT(on_decoder_removed(void*))); | |
304 | ||
88a25978 | 305 | // Add the top-level decoder provided by an already-existing signal |
24d69d27 | 306 | auto stack = signal->decoder_stack(); |
f54e68b0 SA |
307 | if (!stack.empty()) { |
308 | shared_ptr<Decoder>& dec = stack.at(0); | |
309 | decoder_selector_->addItem(signal->name(), QVariant::fromValue((void*)dec.get())); | |
310 | } | |
24d69d27 SA |
311 | } |
312 | ||
313 | void View::remove_decode_signal(shared_ptr<data::DecodeSignal> signal) | |
314 | { | |
315 | // Remove all decoders provided by this signal | |
316 | for (const shared_ptr<Decoder>& dec : signal->decoder_stack()) { | |
317 | int index = decoder_selector_->findData(QVariant::fromValue((void*)dec.get())); | |
318 | ||
319 | if (index != -1) | |
320 | decoder_selector_->removeItem(index); | |
321 | } | |
322 | ||
323 | ViewBase::remove_decode_signal(signal); | |
324 | ||
325 | if (signal.get() == signal_) { | |
326 | reset_data(); | |
327 | update_data(); | |
328 | reset_view_state(); | |
329 | } | |
330 | } | |
331 | ||
332 | void View::save_settings(QSettings &settings) const | |
333 | { | |
334 | ViewBase::save_settings(settings); | |
86d4b8e3 SA |
335 | |
336 | settings.setValue("view_mode", view_mode_selector_->currentIndex()); | |
337 | settings.setValue("hide_hidden", hide_hidden_cb_->isChecked()); | |
24d69d27 SA |
338 | } |
339 | ||
340 | void View::restore_settings(QSettings &settings) | |
341 | { | |
24d69d27 | 342 | ViewBase::restore_settings(settings); |
86d4b8e3 SA |
343 | |
344 | if (settings.contains("view_mode")) | |
345 | view_mode_selector_->setCurrentIndex(settings.value("view_mode").toInt()); | |
346 | ||
347 | if (settings.contains("hide_hidden")) | |
348 | hide_hidden_cb_->setChecked(settings.value("hide_hidden").toBool()); | |
24d69d27 SA |
349 | } |
350 | ||
351 | void View::reset_data() | |
352 | { | |
353 | signal_ = nullptr; | |
354 | decoder_ = nullptr; | |
355 | } | |
356 | ||
357 | void View::update_data() | |
358 | { | |
f54e68b0 | 359 | model_->set_signal_and_segment(signal_, current_segment_); |
24d69d27 SA |
360 | } |
361 | ||
be0f5903 | 362 | void View::save_data_as_csv(unsigned int save_type) const |
24d69d27 | 363 | { |
be0f5903 SA |
364 | // Note: We try to follow RFC 4180 (https://tools.ietf.org/html/rfc4180) |
365 | ||
24d69d27 SA |
366 | assert(decoder_); |
367 | assert(signal_); | |
368 | ||
369 | if (!signal_) | |
370 | return; | |
371 | ||
be0f5903 SA |
372 | const bool save_all = !table_view_->selectionModel()->hasSelection(); |
373 | ||
374 | GlobalSettings settings; | |
24d69d27 SA |
375 | const QString dir = settings.value("MainWindow/SaveDirectory").toString(); |
376 | ||
377 | const QString file_name = QFileDialog::getSaveFileName( | |
be0f5903 | 378 | parent_, tr("Save Annotations as CSV"), dir, tr("CSV Files (*.csv);;Text Files (*.txt);;All Files (*)")); |
24d69d27 SA |
379 | |
380 | if (file_name.isEmpty()) | |
381 | return; | |
382 | ||
383 | QFile file(file_name); | |
384 | if (file.open(QIODevice::WriteOnly | QIODevice::Truncate)) { | |
be0f5903 SA |
385 | QTextStream out_stream(&file); |
386 | ||
387 | if (save_all) | |
388 | table_view_->selectAll(); | |
389 | ||
390 | // Write out header columns in visual order, not logical order | |
391 | for (int i = 0; i < table_view_->horizontalHeader()->count(); i++) { | |
392 | int column = table_view_->horizontalHeader()->logicalIndex(i); | |
393 | ||
394 | if (table_view_->horizontalHeader()->isSectionHidden(column)) | |
395 | continue; | |
396 | ||
939d25cb | 397 | const QString title = filter_proxy_model_->headerData(column, Qt::Horizontal, Qt::DisplayRole).toString(); |
be0f5903 SA |
398 | |
399 | if (save_type == SaveTypeCSVEscaped) | |
400 | out_stream << title; | |
401 | else | |
402 | out_stream << '"' << title << '"'; | |
403 | ||
404 | if (i < (table_view_->horizontalHeader()->count() - 1)) | |
405 | out_stream << ","; | |
406 | } | |
407 | out_stream << '\r' << '\n'; | |
24d69d27 | 408 | |
24d69d27 | 409 | |
be0f5903 | 410 | QModelIndexList selected_rows = table_view_->selectionModel()->selectedRows(); |
24d69d27 | 411 | |
be0f5903 SA |
412 | for (int i = 0; i < selected_rows.size(); i++) { |
413 | const int row = selected_rows.at(i).row(); | |
414 | ||
415 | // Write out columns in visual order, not logical order | |
416 | for (int c = 0; c < table_view_->horizontalHeader()->count(); c++) { | |
417 | const int column = table_view_->horizontalHeader()->logicalIndex(c); | |
418 | ||
419 | if (table_view_->horizontalHeader()->isSectionHidden(column)) | |
420 | continue; | |
421 | ||
939d25cb SA |
422 | const QModelIndex idx = filter_proxy_model_->index(row, column); |
423 | QString s = filter_proxy_model_->data(idx, Qt::DisplayRole).toString(); | |
be0f5903 SA |
424 | |
425 | if (save_type == SaveTypeCSVEscaped) | |
426 | out_stream << s.replace(",", "\\,"); | |
427 | else | |
428 | out_stream << '"' << s.replace("\"", "\"\"") << '"'; | |
429 | ||
430 | if (c < (table_view_->horizontalHeader()->count() - 1)) | |
431 | out_stream << ","; | |
432 | } | |
433 | ||
434 | out_stream << '\r' << '\n'; | |
435 | } | |
436 | ||
437 | if (out_stream.status() == QTextStream::Ok) { | |
438 | if (save_all) | |
439 | table_view_->clearSelection(); | |
24d69d27 | 440 | |
24d69d27 SA |
441 | return; |
442 | } | |
be0f5903 SA |
443 | } |
444 | ||
445 | QMessageBox msg(parent_); | |
446 | msg.setText(tr("Error") + "\n\n" + tr("File %1 could not be written to.").arg(file_name)); | |
447 | msg.setStandardButtons(QMessageBox::Ok); | |
448 | msg.setIcon(QMessageBox::Warning); | |
449 | msg.exec(); | |
24d69d27 SA |
450 | } |
451 | ||
452 | void View::on_selected_decoder_changed(int index) | |
453 | { | |
02c87df7 | 454 | if (signal_) { |
85125b0f | 455 | disconnect(signal_, SIGNAL(color_changed(QColor))); |
24d69d27 | 456 | disconnect(signal_, SIGNAL(new_annotations())); |
02c87df7 SA |
457 | disconnect(signal_, SIGNAL(decode_reset())); |
458 | } | |
24d69d27 SA |
459 | |
460 | reset_data(); | |
461 | ||
462 | decoder_ = (Decoder*)decoder_selector_->itemData(index).value<void*>(); | |
463 | ||
464 | // Find the signal that contains the selected decoder | |
465 | for (const shared_ptr<DecodeSignal>& ds : decode_signals_) | |
466 | for (const shared_ptr<Decoder>& dec : ds->decoder_stack()) | |
467 | if (decoder_ == dec.get()) | |
468 | signal_ = ds.get(); | |
469 | ||
02c87df7 | 470 | if (signal_) { |
88a25978 | 471 | connect(signal_, SIGNAL(color_changed(QColor)), this, SLOT(on_signal_color_changed(QColor))); |
24d69d27 | 472 | connect(signal_, SIGNAL(new_annotations()), this, SLOT(on_new_annotations())); |
02c87df7 SA |
473 | connect(signal_, SIGNAL(decode_reset()), this, SLOT(on_decoder_reset())); |
474 | } | |
24d69d27 SA |
475 | |
476 | update_data(); | |
b36ba611 SA |
477 | |
478 | // Force repaint, otherwise the new selection isn't shown for some reason | |
479 | table_view_->viewport()->update(); | |
24d69d27 SA |
480 | } |
481 | ||
86d4b8e3 SA |
482 | void View::on_hide_hidden_changed(bool checked) |
483 | { | |
484 | model_->set_hide_hidden(checked); | |
485 | ||
486 | // Force repaint, otherwise the new selection isn't shown for some reason | |
487 | table_view_->viewport()->update(); | |
488 | } | |
489 | ||
490 | void View::on_view_mode_changed(int index) | |
491 | { | |
939d25cb SA |
492 | if (index == ViewModeAll) |
493 | filter_proxy_model_->enable_range_filtering(false); | |
494 | ||
8997f62a SA |
495 | if (index == ViewModeVisible) { |
496 | MetadataObject *md_obj = | |
497 | session_.metadata_obj_manager()->find_object_by_type(MetadataObjMainViewRange); | |
498 | assert(md_obj); | |
499 | ||
500 | int64_t start_sample = md_obj->value(MetadataValueStartSample).toLongLong(); | |
501 | int64_t end_sample = md_obj->value(MetadataValueEndSample).toLongLong(); | |
502 | ||
939d25cb | 503 | filter_proxy_model_->enable_range_filtering(true); |
6f43db70 | 504 | filter_proxy_model_->set_sample_range(max((int64_t)0, start_sample), |
8997f62a | 505 | max((int64_t)0, end_sample)); |
8997f62a SA |
506 | } |
507 | ||
939d25cb SA |
508 | if (index == ViewModeLatest) { |
509 | filter_proxy_model_->enable_range_filtering(false); | |
510 | ||
6f43db70 SA |
511 | table_view_->scrollTo( |
512 | filter_proxy_model_->mapFromSource(model_->index(model_->rowCount() - 1, 0)), | |
8997f62a | 513 | QAbstractItemView::PositionAtBottom); |
939d25cb | 514 | } |
86d4b8e3 SA |
515 | } |
516 | ||
24d69d27 SA |
517 | void View::on_signal_name_changed(const QString &name) |
518 | { | |
519 | (void)name; | |
520 | ||
521 | SignalBase* sb = qobject_cast<SignalBase*>(QObject::sender()); | |
522 | assert(sb); | |
523 | ||
524 | DecodeSignal* signal = dynamic_cast<DecodeSignal*>(sb); | |
525 | assert(signal); | |
526 | ||
f54e68b0 | 527 | // Update the top-level decoder provided by this signal |
24d69d27 | 528 | auto stack = signal->decoder_stack(); |
f54e68b0 SA |
529 | if (!stack.empty()) { |
530 | shared_ptr<Decoder>& dec = stack.at(0); | |
531 | int index = decoder_selector_->findData(QVariant::fromValue((void*)dec.get())); | |
24d69d27 | 532 | |
f54e68b0 SA |
533 | if (index != -1) |
534 | decoder_selector_->setItemText(index, signal->name()); | |
535 | } | |
24d69d27 SA |
536 | } |
537 | ||
88a25978 SA |
538 | void View::on_signal_color_changed(const QColor &color) |
539 | { | |
540 | (void)color; | |
541 | ||
b36ba611 SA |
542 | // Force immediate repaint, otherwise it's updated after the header popup is closed |
543 | table_view_->viewport()->update(); | |
88a25978 SA |
544 | } |
545 | ||
24d69d27 SA |
546 | void View::on_new_annotations() |
547 | { | |
c84afcfd SA |
548 | if (view_mode_selector_->currentIndex() == ViewModeLatest) { |
549 | update_data(); | |
5a5d3b1d SA |
550 | table_view_->scrollTo( |
551 | filter_proxy_model_->index(filter_proxy_model_->rowCount() - 1, 0), | |
c84afcfd SA |
552 | QAbstractItemView::PositionAtBottom); |
553 | } else { | |
554 | if (!delayed_view_updater_.isActive()) | |
555 | delayed_view_updater_.start(); | |
556 | } | |
24d69d27 SA |
557 | } |
558 | ||
02c87df7 SA |
559 | void View::on_decoder_reset() |
560 | { | |
561 | // Invalidate the model's data connection immediately - otherwise we | |
562 | // will use a stale pointer in model_->index() when called from the table view | |
563 | model_->set_signal_and_segment(signal_, current_segment_); | |
564 | } | |
565 | ||
24d69d27 SA |
566 | void View::on_decoder_stacked(void* decoder) |
567 | { | |
24d69d27 SA |
568 | Decoder* d = static_cast<Decoder*>(decoder); |
569 | ||
570 | // Find the signal that contains the selected decoder | |
571 | DecodeSignal* signal = nullptr; | |
572 | ||
573 | for (const shared_ptr<DecodeSignal>& ds : decode_signals_) | |
574 | for (const shared_ptr<Decoder>& dec : ds->decoder_stack()) | |
575 | if (d == dec.get()) | |
576 | signal = ds.get(); | |
577 | ||
578 | assert(signal); | |
579 | ||
88a25978 SA |
580 | const shared_ptr<Decoder>& dec = signal->decoder_stack().at(0); |
581 | int index = decoder_selector_->findData(QVariant::fromValue((void*)dec.get())); | |
582 | ||
583 | if (index == -1) { | |
584 | // Add the decoder to the list | |
85125b0f | 585 | decoder_selector_->addItem(signal->name(), QVariant::fromValue((void*)d)); |
88a25978 | 586 | } |
24d69d27 SA |
587 | } |
588 | ||
589 | void View::on_decoder_removed(void* decoder) | |
590 | { | |
591 | Decoder* d = static_cast<Decoder*>(decoder); | |
592 | ||
593 | // Remove the decoder from the list | |
594 | int index = decoder_selector_->findData(QVariant::fromValue((void*)d)); | |
595 | ||
596 | if (index != -1) | |
597 | decoder_selector_->removeItem(index); | |
598 | } | |
599 | ||
600 | void View::on_actionSave_triggered(QAction* action) | |
601 | { | |
be0f5903 SA |
602 | int save_type = SaveTypeCSVQuoted; |
603 | ||
604 | if (action) | |
605 | save_type = action->data().toInt(); | |
24d69d27 | 606 | |
be0f5903 | 607 | save_data_as_csv(save_type); |
24d69d27 SA |
608 | } |
609 | ||
9a35b05d SA |
610 | void View::on_table_item_clicked(const QModelIndex& index) |
611 | { | |
612 | (void)index; | |
613 | ||
614 | // Force repaint, otherwise the new selection isn't shown for some reason | |
615 | table_view_->viewport()->update(); | |
616 | } | |
617 | ||
618 | void View::on_table_item_double_clicked(const QModelIndex& index) | |
619 | { | |
49a0a403 SA |
620 | const QModelIndex src_idx = filter_proxy_model_->mapToSource(index); |
621 | ||
622 | const Annotation* ann = static_cast<const Annotation*>(src_idx.internalPointer()); | |
623 | assert(ann); | |
9a35b05d SA |
624 | |
625 | shared_ptr<views::ViewBase> main_view = session_.main_view(); | |
626 | ||
627 | main_view->focus_on_range(ann->start_sample(), ann->end_sample()); | |
628 | } | |
629 | ||
630 | void View::on_table_header_requested(const QPoint& pos) | |
631 | { | |
632 | QMenu* menu = new QMenu(this); | |
633 | ||
634 | for (int i = 0; i < table_view_->horizontalHeader()->count(); i++) { | |
635 | int column = table_view_->horizontalHeader()->logicalIndex(i); | |
636 | ||
49a0a403 SA |
637 | const QString title = |
638 | filter_proxy_model_->headerData(column, Qt::Horizontal, Qt::DisplayRole).toString(); | |
9a35b05d SA |
639 | QAction* action = new QAction(title, this); |
640 | ||
641 | action->setCheckable(true); | |
642 | action->setChecked(!table_view_->horizontalHeader()->isSectionHidden(column)); | |
643 | action->setData(column); | |
644 | ||
645 | connect(action, SIGNAL(toggled(bool)), this, SLOT(on_table_header_toggled(bool))); | |
646 | ||
647 | menu->addAction(action); | |
648 | } | |
649 | ||
650 | menu->popup(table_view_->horizontalHeader()->viewport()->mapToGlobal(pos)); | |
651 | } | |
652 | ||
653 | void View::on_table_header_toggled(bool checked) | |
654 | { | |
655 | QAction* action = qobject_cast<QAction*>(QObject::sender()); | |
656 | assert(action); | |
657 | ||
658 | const int column = action->data().toInt(); | |
659 | ||
660 | table_view_->horizontalHeader()->setSectionHidden(column, !checked); | |
661 | } | |
662 | ||
8997f62a SA |
663 | void View::on_metadata_object_changed(MetadataObject* obj, |
664 | MetadataValueType value_type) | |
665 | { | |
666 | // Check if we need to update the model's data range. We only work on the | |
667 | // end sample value because the start sample value is updated first and | |
668 | // we don't want to update the model twice | |
8997f62a SA |
669 | if ((view_mode_selector_->currentIndex() == ViewModeVisible) && |
670 | (obj->type() == MetadataObjMainViewRange) && | |
671 | (value_type == MetadataValueEndSample)) { | |
672 | ||
673 | int64_t start_sample = obj->value(MetadataValueStartSample).toLongLong(); | |
674 | int64_t end_sample = obj->value(MetadataValueEndSample).toLongLong(); | |
675 | ||
6f43db70 | 676 | filter_proxy_model_->set_sample_range(max((int64_t)0, start_sample), |
8997f62a SA |
677 | max((int64_t)0, end_sample)); |
678 | } | |
1c521100 SA |
679 | |
680 | if (obj->type() == MetadataObjMousePos) { | |
6f43db70 SA |
681 | QModelIndex first_visible_idx = |
682 | filter_proxy_model_->mapToSource(filter_proxy_model_->index(0, 0)); | |
683 | QModelIndex last_visible_idx = | |
684 | filter_proxy_model_->mapToSource(filter_proxy_model_->index(filter_proxy_model_->rowCount() - 1, 0)); | |
685 | ||
686 | if (first_visible_idx.isValid()) { | |
687 | const QModelIndex first_highlighted_idx = | |
688 | model_->update_highlighted_rows(first_visible_idx, last_visible_idx, | |
689 | obj->value(MetadataValueStartSample).toLongLong()); | |
690 | ||
691 | if (view_mode_selector_->currentIndex() == ViewModeVisible) { | |
692 | const QModelIndex idx = filter_proxy_model_->mapFromSource(first_highlighted_idx); | |
693 | table_view_->scrollTo(idx, QAbstractItemView::EnsureVisible); | |
694 | } | |
49a0a403 SA |
695 | |
696 | // Force repaint, otherwise the table doesn't immediately update for some reason | |
697 | table_view_->viewport()->update(); | |
6f43db70 | 698 | } |
1c521100 | 699 | } |
8997f62a SA |
700 | } |
701 | ||
24d69d27 SA |
702 | void View::perform_delayed_view_update() |
703 | { | |
704 | update_data(); | |
705 | } | |
706 | ||
707 | ||
708 | } // namespace tabular_decoder | |
709 | } // namespace views | |
710 | } // namespace pv |