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