]> 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 2283d68ce780d0300f9d4b3a05464ff88f2089b8..91bcaea197bee2a0645d9f314fb77c2f5053df21 100644 (file)
@@ -63,7 +63,64 @@ const char* ViewModeNames[ViewModeCount] = {
        "Show visible in main view"
 };
 
-QSize QCustomTableView::minimumSizeHint() const
+
+CustomFilterProxyModel::CustomFilterProxyModel(QObject* parent) :
+       QSortFilterProxyModel(parent),
+       range_filtering_enabled_(false)
+{
+}
+
+bool CustomFilterProxyModel::filterAcceptsRow(int sourceRow,
+       const QModelIndex &sourceParent) const
+{
+       (void)sourceParent;
+       assert(sourceModel() != nullptr);
+
+       bool result = true;
+
+       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();
+
+               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;
+
+               result = !entirely_outside_of_range;
+       }
+
+       return result;
+}
+
+void CustomFilterProxyModel::set_sample_range(uint64_t start_sample,
+       uint64_t end_sample)
+{
+       range_start_sample_ = start_sample;
+       range_end_sample_ = end_sample;
+
+       invalidateFilter();
+}
+
+void CustomFilterProxyModel::enable_range_filtering(bool value)
+{
+       range_filtering_enabled_ = value;
+
+       invalidateFilter();
+}
+
+
+QSize CustomTableView::minimumSizeHint() const
 {
        QSize size(QTableView::sizeHint());
 
@@ -77,11 +134,19 @@ QSize QCustomTableView::minimumSizeHint() const
        return size;
 }
 
-QSize QCustomTableView::sizeHint() const
+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),
@@ -93,8 +158,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);
@@ -153,13 +219,18 @@ View::View(Session &session, bool is_main_view, QMainWindow *parent) :
        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(false);
+       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);
@@ -176,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&)));
 
@@ -317,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;
@@ -342,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(",", "\\,");
@@ -409,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);
@@ -417,16 +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();
 
-               model_->set_sample_range(max((int64_t)0, start_sample),
+               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));
-       } else {
-               // Reset the model's data range
-               model_->set_sample_range(0, 0);
        }
 
-       if (index == ViewModeLatest)
-               table_view_->scrollTo(model_->index(model_->rowCount() - 1, 0),
+       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)
@@ -461,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())
@@ -530,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();
 
@@ -544,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);
@@ -583,16 +666,29 @@ void View::on_metadata_object_changed(MetadataObject* obj,
                int64_t start_sample = obj->value(MetadataValueStartSample).toLongLong();
                int64_t end_sample = obj->value(MetadataValueEndSample).toLongLong();
 
-               model_->set_sample_range(max((int64_t)0, start_sample),
+               filter_proxy_model_->set_sample_range(max((int64_t)0, start_sample),
                        max((int64_t)0, end_sample));
        }
 
        if (obj->type() == MetadataObjMousePos) {
-               QModelIndex first_visual_idx = table_view_->indexAt(table_view_->rect().topLeft());
-               QModelIndex last_visual_idx = table_view_->indexAt(table_view_->rect().bottomLeft());
+               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);
+                       }
 
-               model_->update_highlighted_rows(first_visual_idx, last_visual_idx,
-                       obj->value(MetadataValueStartSample).toLongLong());
+                       // Force repaint, otherwise the table doesn't immediately update for some reason
+                       table_view_->viewport()->update();
+               }
        }
 }