]> sigrok.org Git - pulseview.git/blobdiff - pv/views/tabular_decoder/model.cpp
TabularDecView: Remove unnecessary stuff
[pulseview.git] / pv / views / tabular_decoder / model.cpp
index ffd151a5300c4278a297a45fe040b84f22e4e246..c8692efb17d7125003b8989a2311b3b275a00b3d 100644 (file)
@@ -25,6 +25,7 @@
 #include "view.hpp"
 
 #include "pv/util.hpp"
+#include "pv/globalsettings.hpp"
 
 using std::make_shared;
 
@@ -40,13 +41,14 @@ namespace tabular_decoder {
 AnnotationCollectionModel::AnnotationCollectionModel(QObject* parent) :
        QAbstractTableModel(parent),
        all_annotations_(nullptr),
+       dataset_(nullptr),
        signal_(nullptr),
        prev_segment_(0),
-       prev_last_row_(0)
+       prev_last_row_(0),
+       start_index_(0),
+       end_index_(0),
+       hide_hidden_(false)
 {
-       GlobalSettings::add_change_handler(this);
-       theme_is_dark_ = GlobalSettings::current_theme_is_dark();
-
        // TBD Maybe use empty columns as indentation levels to indicate stacked decoders
        header_data_.emplace_back(tr("Sample"));     // Column #0
        header_data_.emplace_back(tr("Time"));       // Column #1
@@ -97,7 +99,7 @@ QVariant AnnotationCollectionModel::data(const QModelIndex& index, int role) con
 
                // Only use custom cell background color if column index reached the hierarchy level
                if (index.column() >= level) {
-                       if (theme_is_dark_)
+                       if (GlobalSettings::current_theme_is_dark())
                                return QBrush(ann->dark_color());
                        else
                                return QBrush(ann->bright_color());
@@ -128,16 +130,22 @@ QModelIndex AnnotationCollectionModel::index(int row, int column,
        const QModelIndex& parent_idx) const
 {
        (void)parent_idx;
-       assert(row >= 0);
        assert(column >= 0);
 
-       if (!all_annotations_)
+       if (!dataset_ || (row < 0))
                return QModelIndex();
 
-       if ((size_t)row > all_annotations_->size())
-               return QModelIndex();
+       QModelIndex idx;
 
-       return createIndex(row, column, (void*)(all_annotations_->at(row)));
+       if (start_index_ == end_index_) {
+               if ((size_t)row < dataset_->size())
+                       idx = createIndex(row, column, (void*)dataset_->at(row));
+       } else {
+               if ((size_t)row < (end_index_ - start_index_))
+                       idx = createIndex(row, column, (void*)dataset_->at(start_index_ + row));
+       }
+
+       return idx;
 }
 
 QModelIndex AnnotationCollectionModel::parent(const QModelIndex& index) const
@@ -151,10 +159,13 @@ int AnnotationCollectionModel::rowCount(const QModelIndex& parent_idx) const
 {
        (void)parent_idx;
 
-       if (!all_annotations_)
+       if (!dataset_)
                return 0;
 
-       return all_annotations_->size();
+       if (start_index_ == end_index_)
+               return dataset_->size();
+       else
+               return (end_index_ - start_index_);
 }
 
 int AnnotationCollectionModel::columnCount(const QModelIndex& parent_idx) const
@@ -168,21 +179,37 @@ void AnnotationCollectionModel::set_signal_and_segment(data::DecodeSignal* signa
 {
        if (!signal) {
                all_annotations_ = nullptr;
+               dataset_ = nullptr;
                signal_ = nullptr;
+
                dataChanged(QModelIndex(), QModelIndex());
                layoutChanged();
                return;
        }
 
+       disconnect(this, SLOT(on_annotation_visibility_changed()));
+
        all_annotations_ = signal->get_all_annotations_by_segment(current_segment);
        signal_ = signal;
 
-       if (!all_annotations_ || all_annotations_->empty()) {
+       for (const shared_ptr<Decoder>& dec : signal_->decoder_stack())
+               connect(dec.get(), SIGNAL(annotation_visibility_changed()),
+                       this, SLOT(on_annotation_visibility_changed()));
+
+       if (hide_hidden_)
+               update_annotations_without_hidden();
+       else
+               dataset_ = all_annotations_;
+
+       if (!dataset_ || dataset_->empty()) {
                prev_segment_ = current_segment;
                return;
        }
 
-       const size_t new_row_count = all_annotations_->size() - 1;
+       // Re-apply the requested sample range
+       set_sample_range(start_sample_, end_sample_);
+
+       const size_t new_row_count = dataset_->size() - 1;
 
        // Force the view associated with this model to update when the segment changes
        if (prev_segment_ != current_segment) {
@@ -191,8 +218,7 @@ void AnnotationCollectionModel::set_signal_and_segment(data::DecodeSignal* signa
        } else {
                // Force the view associated with this model to update when we have more annotations
                if (prev_last_row_ < new_row_count) {
-                       dataChanged(index(prev_last_row_, 0, QModelIndex()),
-                               index(new_row_count, 0, QModelIndex()));
+                       dataChanged(index(prev_last_row_, 0), index(new_row_count, 0));
                        layoutChanged();
                }
        }
@@ -201,14 +227,123 @@ void AnnotationCollectionModel::set_signal_and_segment(data::DecodeSignal* signa
        prev_last_row_ = new_row_count;
 }
 
-void AnnotationCollectionModel::on_setting_changed(const QString &key, const QVariant &value)
+void AnnotationCollectionModel::set_sample_range(uint64_t start_sample, uint64_t end_sample)
+{
+       // Check if there's even anything to reset
+       if ((start_sample == end_sample) && (start_index_ == end_index_))
+               return;
+
+       if (!dataset_ || dataset_->empty() || (end_sample == 0)) {
+               start_index_ = 0;
+               end_index_ = 0;
+               start_sample_ = 0;
+               end_sample_ = 0;
+
+               dataChanged(QModelIndex(), QModelIndex());
+               layoutChanged();
+               return;
+       }
+
+       start_sample_ = start_sample;
+       end_sample_ = end_sample;
+
+       // Determine first and last indices into the annotation list
+       int64_t i = -1;
+       bool ann_outside_range;
+       do {
+               i++;
+
+               if (i == (int64_t)dataset_->size()) {
+                       start_index_ = 0;
+                       end_index_ = 0;
+
+                       dataChanged(QModelIndex(), QModelIndex());
+                       layoutChanged();
+                       return;
+               }
+               const Annotation* ann = (*dataset_)[i];
+               ann_outside_range =
+                       ((ann->start_sample() < start_sample) && (ann->end_sample() < start_sample));
+       } while (ann_outside_range);
+       start_index_ = i;
+
+       // Ideally, we would be able to set end_index_ to the last annotation that
+       // is within range. However, as annotations in the list are sorted by
+       // start sample and hierarchy level, we may encounter this scenario:
+       //   [long annotation that spans across view]
+       //   [short annotations that aren't seen]
+       //   [short annotations that are seen]
+       // ..in which our output would only show the first long annotations.
+       // For this reason, we simply show everything after the first visible
+       // annotation for now.
+
+       end_index_ = dataset_->size();
+
+       dataChanged(index(0, 0), index((end_index_ - start_index_), 0));
+       layoutChanged();
+}
+
+void AnnotationCollectionModel::set_hide_hidden(bool hide_hidden)
 {
-       (void)key;
-       (void)value;
+       hide_hidden_ = hide_hidden;
+
+       if (hide_hidden_) {
+               dataset_ = &all_annotations_without_hidden_;
+               update_annotations_without_hidden();
+       } else {
+               dataset_ = all_annotations_;
+               all_annotations_without_hidden_.clear();  // To conserve memory
+       }
+
+       // Re-apply the requested sample range
+       set_sample_range(start_sample_, end_sample_);
+
+       if (dataset_)
+               dataChanged(index(0, 0), index(dataset_->size(), 0));
+       else
+               dataChanged(QModelIndex(), QModelIndex());
+
+       layoutChanged();
+}
+
+void AnnotationCollectionModel::update_annotations_without_hidden()
+{
+       uint64_t count = 0;
+
+       if (!all_annotations_ || all_annotations_->empty()) {
+               all_annotations_without_hidden_.clear();
+               return;
+       }
+
+       for (const Annotation* ann : *all_annotations_) {
+               if (!ann->visible())
+                       continue;
+
+               if (all_annotations_without_hidden_.size() < (count + 100))
+                       all_annotations_without_hidden_.resize(count + 100);
+
+               all_annotations_without_hidden_[count++] = ann;
+       }
+
+       all_annotations_without_hidden_.resize(count);
+}
+
+void AnnotationCollectionModel::on_annotation_visibility_changed()
+{
+       if (!hide_hidden_)
+               return;
+
+       update_annotations_without_hidden();
+
+       // Re-apply the requested sample range
+       set_sample_range(start_sample_, end_sample_);
+
+       if (dataset_)
+               dataChanged(index(0, 0), index(dataset_->size(), 0));
+       else
+               dataChanged(QModelIndex(), QModelIndex());
 
-       // We don't really care about the actual setting, we just update the
-       // flag that indicates whether we are using a bright or dark color theme
-       theme_is_dark_ = GlobalSettings::current_theme_is_dark();
+       layoutChanged();
 }
 
 } // namespace tabular_decoder