]> sigrok.org Git - pulseview.git/blobdiff - pv/views/tabular_decoder/view.cpp
TabularDecView: Allow return/enter press and don't change scale
[pulseview.git] / pv / views / tabular_decoder / view.cpp
index 1ed6f95b18b1a0daedf3903291febfab099f8ed2..91bcaea197bee2a0645d9f314fb77c2f5053df21 100644 (file)
@@ -65,7 +65,8 @@ const char* ViewModeNames[ViewModeCount] = {
 
 
 CustomFilterProxyModel::CustomFilterProxyModel(QObject* parent) :
-       QSortFilterProxyModel(parent)
+       QSortFilterProxyModel(parent),
+       range_filtering_enabled_(false)
 {
 }
 
@@ -75,25 +76,31 @@ bool CustomFilterProxyModel::filterAcceptsRow(int sourceRow,
        (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();
+       bool result = true;
 
-       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();
+       if (range_filtering_enabled_) {
+               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();
 
-       // 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 QModelIndex ann_end_sample_idx = sourceModel()->index(sourceRow, 6);
+               const uint64_t ann_end_sample =
+                       sourceModel()->data(ann_end_sample_idx, Qt::DisplayRole).toULongLong();
 
-       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;
+               // 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"
 
-       return !entirely_outside_of_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;
+
+               result = !entirely_outside_of_range;
+       }
+
+       return result;
 }
 
 void CustomFilterProxyModel::set_sample_range(uint64_t start_sample,
@@ -105,6 +112,13 @@ void CustomFilterProxyModel::set_sample_range(uint64_t start_sample,
        invalidateFilter();
 }
 
+void CustomFilterProxyModel::enable_range_filtering(bool value)
+{
+       range_filtering_enabled_ = value;
+
+       invalidateFilter();
+}
+
 
 QSize CustomTableView::minimumSizeHint() const
 {
@@ -125,6 +139,14 @@ QSize CustomTableView::sizeHint() const
        return minimumSizeHint();
 }
 
+void CustomTableView::keyPressEvent(QKeyEvent *event)
+{
+       if ((event->key() == Qt::Key_Return) || (event->key() == Qt::Key_Enter))
+               activatedByKey(currentIndex());
+       else
+               QTableView::keyPressEvent(event);
+}
+
 
 View::View(Session &session, bool is_main_view, QMainWindow *parent) :
        ViewBase(session, is_main_view, parent),
@@ -225,6 +247,8 @@ View::View(Session &session, bool is_main_view, QMainWindow *parent) :
                this, SLOT(on_table_item_clicked(const QModelIndex&)));
        connect(table_view_, SIGNAL(doubleClicked(const QModelIndex&)),
                this, SLOT(on_table_item_double_clicked(const QModelIndex&)));
+       connect(table_view_, SIGNAL(activatedByKey(const QModelIndex&)),
+               this, SLOT(on_table_item_double_clicked(const QModelIndex&)));
        connect(table_view_->horizontalHeader(), SIGNAL(customContextMenuRequested(const QPoint&)),
                this, SLOT(on_table_header_requested(const QPoint&)));
 
@@ -366,7 +390,7 @@ void View::save_data_as_csv(unsigned int save_type) const
                        if (table_view_->horizontalHeader()->isSectionHidden(column))
                                continue;
 
-                       const QString title = model_->headerData(column, Qt::Horizontal, Qt::DisplayRole).toString();
+                       const QString title = filter_proxy_model_->headerData(column, Qt::Horizontal, Qt::DisplayRole).toString();
 
                        if (save_type == SaveTypeCSVEscaped)
                                out_stream << title;
@@ -391,8 +415,8 @@ void View::save_data_as_csv(unsigned int save_type) const
                                if (table_view_->horizontalHeader()->isSectionHidden(column))
                                        continue;
 
-                               const QModelIndex idx = model_->index(row, column);
-                               QString s = model_->data(idx, Qt::DisplayRole).toString();
+                               const QModelIndex idx = filter_proxy_model_->index(row, column);
+                               QString s = filter_proxy_model_->data(idx, Qt::DisplayRole).toString();
 
                                if (save_type == SaveTypeCSVEscaped)
                                        out_stream << s.replace(",", "\\,");
@@ -458,6 +482,9 @@ void View::on_hide_hidden_changed(bool checked)
 
 void View::on_view_mode_changed(int index)
 {
+       if (index == ViewModeAll)
+               filter_proxy_model_->enable_range_filtering(false);
+
        if (index == ViewModeVisible) {
                MetadataObject *md_obj =
                        session_.metadata_obj_manager()->find_object_by_type(MetadataObjMainViewRange);
@@ -466,20 +493,18 @@ void View::on_view_mode_changed(int index)
                int64_t start_sample = md_obj->value(MetadataValueStartSample).toLongLong();
                int64_t end_sample = md_obj->value(MetadataValueEndSample).toLongLong();
 
+               filter_proxy_model_->enable_range_filtering(true);
                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)
+       if (index == ViewModeLatest) {
+               filter_proxy_model_->enable_range_filtering(false);
+
                table_view_->scrollTo(
                        filter_proxy_model_->mapFromSource(model_->index(model_->rowCount() - 1, 0)),
                        QAbstractItemView::PositionAtBottom);
+       }
 }
 
 void View::on_signal_name_changed(const QString &name)
@@ -514,7 +539,8 @@ void View::on_new_annotations()
 {
        if (view_mode_selector_->currentIndex() == ViewModeLatest) {
                update_data();
-               table_view_->scrollTo(model_->index(model_->rowCount() - 1, 0),
+               table_view_->scrollTo(
+                       filter_proxy_model_->index(filter_proxy_model_->rowCount() - 1, 0),
                        QAbstractItemView::PositionAtBottom);
        } else {
                if (!delayed_view_updater_.isActive())
@@ -583,7 +609,10 @@ void View::on_table_item_clicked(const QModelIndex& index)
 
 void View::on_table_item_double_clicked(const QModelIndex& index)
 {
-       const Annotation* ann = static_cast<const Annotation*>(index.internalPointer());
+       const QModelIndex src_idx = filter_proxy_model_->mapToSource(index);
+
+       const Annotation* ann = static_cast<const Annotation*>(src_idx.internalPointer());
+       assert(ann);
 
        shared_ptr<views::ViewBase> main_view = session_.main_view();
 
@@ -597,7 +626,8 @@ void View::on_table_header_requested(const QPoint& pos)
        for (int i = 0; i < table_view_->horizontalHeader()->count(); i++) {
                int column = table_view_->horizontalHeader()->logicalIndex(i);
 
-               const QString title = model_->headerData(column, Qt::Horizontal, Qt::DisplayRole).toString();
+               const QString title =
+                       filter_proxy_model_->headerData(column, Qt::Horizontal, Qt::DisplayRole).toString();
                QAction* action = new QAction(title, this);
 
                action->setCheckable(true);
@@ -655,6 +685,9 @@ void View::on_metadata_object_changed(MetadataObject* obj,
                                const QModelIndex idx = filter_proxy_model_->mapFromSource(first_highlighted_idx);
                                table_view_->scrollTo(idx, QAbstractItemView::EnsureVisible);
                        }
+
+                       // Force repaint, otherwise the table doesn't immediately update for some reason
+                       table_view_->viewport()->update();
                }
        }
 }