X-Git-Url: https://sigrok.org/gitweb/?a=blobdiff_plain;f=pv%2Fviews%2Ftabular_decoder%2Fview.cpp;h=1ed6f95b18b1a0daedf3903291febfab099f8ed2;hb=8845be3c9c7d5aca02fb2efc4038f4735a5242d6;hp=6ee3e2ac13b1871b632bfa229c44ef0f4d4b4ca4;hpb=c84afcfd68ab1fef53e579c8c04d4e83ef9597a0;p=pulseview.git diff --git a/pv/views/tabular_decoder/view.cpp b/pv/views/tabular_decoder/view.cpp index 6ee3e2ac..1ed6f95b 100644 --- a/pv/views/tabular_decoder/view.cpp +++ b/pv/views/tabular_decoder/view.cpp @@ -45,6 +45,7 @@ using pv::data::decode::Decoder; using pv::util::Timestamp; using std::make_shared; +using std::max; using std::shared_ptr; namespace pv { @@ -58,11 +59,54 @@ const char* SaveTypeNames[SaveTypeCount] = { const char* ViewModeNames[ViewModeCount] = { "Show all", - "Show all and focus on newest" -// "Show visible in main view" + "Show all and focus on newest", + "Show visible in main view" }; -QSize QCustomTableView::minimumSizeHint() const + +CustomFilterProxyModel::CustomFilterProxyModel(QObject* parent) : + QSortFilterProxyModel(parent) +{ +} + +bool CustomFilterProxyModel::filterAcceptsRow(int sourceRow, + const QModelIndex &sourceParent) const +{ + (void)sourceParent; + assert(sourceModel() != nullptr); + + const QModelIndex ann_start_sample_idx = sourceModel()->index(sourceRow, 0); + const uint64_t ann_start_sample = + sourceModel()->data(ann_start_sample_idx, Qt::DisplayRole).toULongLong(); + + const QModelIndex ann_end_sample_idx = sourceModel()->index(sourceRow, 6); + const uint64_t ann_end_sample = + sourceModel()->data(ann_end_sample_idx, Qt::DisplayRole).toULongLong(); + + // We consider all annotations as visible that either + // a) begin to the left of the range and end within the range or + // b) begin and end within the range or + // c) begin within the range and end to the right of the range + // ...which is equivalent to the negation of "begins and ends outside the range" + + const bool left_of_range = (ann_end_sample < range_start_sample_); + const bool right_of_range = (ann_start_sample > range_end_sample_); + const bool entirely_outside_of_range = left_of_range || right_of_range; + + return !entirely_outside_of_range; +} + +void CustomFilterProxyModel::set_sample_range(uint64_t start_sample, + uint64_t end_sample) +{ + range_start_sample_ = start_sample; + range_end_sample_ = end_sample; + + invalidateFilter(); +} + + +QSize CustomTableView::minimumSizeHint() const { QSize size(QTableView::sizeHint()); @@ -76,7 +120,7 @@ QSize QCustomTableView::minimumSizeHint() const return size; } -QSize QCustomTableView::sizeHint() const +QSize CustomTableView::sizeHint() const { return minimumSizeHint(); } @@ -92,8 +136,9 @@ View::View(Session &session, bool is_main_view, QMainWindow *parent) : view_mode_selector_(new QComboBox()), save_button_(new QToolButton()), save_action_(new QAction(this)), - table_view_(new QCustomTableView()), - model_(new AnnotationCollectionModel()), + table_view_(new CustomTableView()), + model_(new AnnotationCollectionModel(this)), + filter_proxy_model_(new CustomFilterProxyModel(this)), signal_(nullptr) { QVBoxLayout *root_layout = new QVBoxLayout(this); @@ -145,20 +190,25 @@ View::View(Session &session, bool is_main_view, QMainWindow *parent) : for (int i = 0; i < SaveTypeCount; i++) { QAction *const action = save_menu->addAction(tr(SaveTypeNames[i])); - action->setData(qVariantFromValue(i)); + action->setData(QVariant::fromValue(i)); } save_button_->setMenu(save_menu); save_button_->setDefaultAction(save_action_); save_button_->setPopupMode(QToolButton::MenuButtonPopup); - // Set up the table view - table_view_->setModel(model_); + // Set up the models and the table view + filter_proxy_model_->setSourceModel(model_); + table_view_->setModel(filter_proxy_model_); + table_view_->setSelectionBehavior(QAbstractItemView::SelectRows); table_view_->setSelectionMode(QAbstractItemView::ContiguousSelection); table_view_->setSortingEnabled(true); table_view_->sortByColumn(0, Qt::AscendingOrder); + for (uint8_t i = model_->first_hidden_column(); i < model_->columnCount(); i++) + table_view_->setColumnHidden(i, true); + const int font_height = QFontMetrics(QApplication::font()).height(); table_view_->verticalHeader()->setDefaultSectionSize((font_height * 5) / 4); table_view_->verticalHeader()->setVisible(false); @@ -178,9 +228,17 @@ View::View(Session &session, bool is_main_view, QMainWindow *parent) : connect(table_view_->horizontalHeader(), SIGNAL(customContextMenuRequested(const QPoint&)), this, SLOT(on_table_header_requested(const QPoint&))); + // Set up metadata event handler + session_.metadata_obj_manager()->add_observer(this); + reset_view_state(); } +View::~View() +{ + session_.metadata_obj_manager()->remove_observer(this); +} + ViewType View::get_type() const { return ViewTypeTabularDecoder; @@ -400,7 +458,28 @@ void View::on_hide_hidden_changed(bool checked) void View::on_view_mode_changed(int index) { - (void)index; + if (index == ViewModeVisible) { + MetadataObject *md_obj = + session_.metadata_obj_manager()->find_object_by_type(MetadataObjMainViewRange); + assert(md_obj); + + int64_t start_sample = md_obj->value(MetadataValueStartSample).toLongLong(); + int64_t end_sample = md_obj->value(MetadataValueEndSample).toLongLong(); + + filter_proxy_model_->set_sample_range(max((int64_t)0, start_sample), + max((int64_t)0, end_sample)); + + // Force repaint, otherwise the new selection may not show immediately +// table_view_->viewport()->update(); + } else { + // Use the data model directly + table_view_->setModel(model_); + } + + if (index == ViewModeLatest) + table_view_->scrollTo( + filter_proxy_model_->mapFromSource(model_->index(model_->rowCount() - 1, 0)), + QAbstractItemView::PositionAtBottom); } void View::on_signal_name_changed(const QString &name) @@ -543,6 +622,43 @@ void View::on_table_header_toggled(bool checked) table_view_->horizontalHeader()->setSectionHidden(column, !checked); } +void View::on_metadata_object_changed(MetadataObject* obj, + MetadataValueType value_type) +{ + // Check if we need to update the model's data range. We only work on the + // end sample value because the start sample value is updated first and + // we don't want to update the model twice + + if ((view_mode_selector_->currentIndex() == ViewModeVisible) && + (obj->type() == MetadataObjMainViewRange) && + (value_type == MetadataValueEndSample)) { + + int64_t start_sample = obj->value(MetadataValueStartSample).toLongLong(); + int64_t end_sample = obj->value(MetadataValueEndSample).toLongLong(); + + filter_proxy_model_->set_sample_range(max((int64_t)0, start_sample), + max((int64_t)0, end_sample)); + } + + if (obj->type() == MetadataObjMousePos) { + QModelIndex first_visible_idx = + filter_proxy_model_->mapToSource(filter_proxy_model_->index(0, 0)); + QModelIndex last_visible_idx = + filter_proxy_model_->mapToSource(filter_proxy_model_->index(filter_proxy_model_->rowCount() - 1, 0)); + + if (first_visible_idx.isValid()) { + const QModelIndex first_highlighted_idx = + model_->update_highlighted_rows(first_visible_idx, last_visible_idx, + obj->value(MetadataValueStartSample).toLongLong()); + + if (view_mode_selector_->currentIndex() == ViewModeVisible) { + const QModelIndex idx = filter_proxy_model_->mapFromSource(first_highlighted_idx); + table_view_->scrollTo(idx, QAbstractItemView::EnsureVisible); + } + } + } +} + void View::perform_delayed_view_update() { update_data();