]> sigrok.org Git - pulseview.git/blobdiff - pv/views/tabular_decoder/model.cpp
TabularDecView: Implement "hide hidden rows/classes" checkbox
[pulseview.git] / pv / views / tabular_decoder / model.cpp
index bd9e174c26ba482237c7bdc750e002c1f52880c7..1d4d839e4757be2ad20169c09b621f04044013b1 100644 (file)
 
 #include "view.hpp"
 
+#include "pv/util.hpp"
+
 using std::make_shared;
 
+using pv::util::Timestamp;
+using pv::util::format_time_si;
+using pv::util::format_time_minutes;
+using pv::util::SIPrefix;
+
 namespace pv {
 namespace views {
 namespace tabular_decoder {
@@ -33,9 +40,11 @@ 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),
+       hide_hidden_(false)
 {
        GlobalSettings::add_change_handler(this);
        theme_is_dark_ = GlobalSettings::current_theme_is_dark();
@@ -49,25 +58,38 @@ AnnotationCollectionModel::AnnotationCollectionModel(QObject* parent) :
        header_data_.emplace_back(tr("Value"));      // Column #5
 }
 
+QVariant AnnotationCollectionModel::data_from_ann(const Annotation* ann, int index) const
+{
+       switch (index) {
+       case 0: return QVariant((qulonglong)ann->start_sample());  // Column #0, Start Sample
+       case 1: {                                                  // Column #1, Start Time
+                       Timestamp t = ann->start_sample() / signal_->get_samplerate();
+                       QString unit = signal_->get_samplerate() ? tr("s") : tr("sa");
+                       QString s;
+                       if ((t < 60) || (signal_->get_samplerate() == 0))  // i.e. if unit is sa
+                               s = format_time_si(t, SIPrefix::unspecified, 3, unit, false);
+                       else
+                               s = format_time_minutes(t, 3, false);
+                       return QVariant(s);
+               }
+       case 2: return QVariant(ann->row()->decoder()->name());    // Column #2, Decoder
+       case 3: return QVariant(ann->row()->description());        // Column #3, Ann Row
+       case 4: return QVariant(ann->ann_class_description());     // Column #4, Ann Class
+       case 5: return QVariant(ann->longest_annotation());        // Column #5, Value
+       default: return QVariant();
+       }
+}
+
 QVariant AnnotationCollectionModel::data(const QModelIndex& index, int role) const
 {
-       if (!index.isValid() || !signal_)
+       if (!signal_ || !index.isValid() || !index.internalPointer())
                return QVariant();
 
        const Annotation* ann =
                static_cast<const Annotation*>(index.internalPointer());
 
-       if (role == Qt::DisplayRole) {
-               switch (index.column()) {
-               case 0: return QVariant((qulonglong)ann->start_sample());  // Column #0, Start Sample
-               case 1: return QVariant(0/*(qulonglong)ann->start_sample()*/);  // Column #1, Start Time
-               case 2: return QVariant(ann->row()->decoder()->name());    // Column #2, Decoder
-               case 3: return QVariant(ann->row()->description());        // Column #3, Ann Row
-               case 4: return QVariant(ann->ann_class_description());     // Column #4, Ann Class
-               case 5: return QVariant(ann->longest_annotation());        // Column #5, Value
-               default: return QVariant();
-               }
-       }
+       if ((role == Qt::DisplayRole) || (role == Qt::ToolTipRole))
+               return data_from_ann(ann, index.column());
 
        if (role == Qt::BackgroundRole) {
                int level = 0;
@@ -98,7 +120,7 @@ Qt::ItemFlags AnnotationCollectionModel::flags(const QModelIndex& index) const
 QVariant AnnotationCollectionModel::headerData(int section, Qt::Orientation orientation,
        int role) const
 {
-       if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
+       if ((orientation == Qt::Horizontal) && (role == Qt::DisplayRole))
                return header_data_.at(section);
 
        return QVariant();
@@ -108,14 +130,18 @@ 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_)
                return QModelIndex();
 
-       if ((size_t)row > all_annotations_->size())
-               return QModelIndex();
+       QModelIndex idx;
 
-       return createIndex(row, column, (void*)(all_annotations_->at(row)));
+       if ((size_t)row < dataset_->size())
+               idx = createIndex(row, column, (void*)dataset_->at(row));
+
+       return idx;
 }
 
 QModelIndex AnnotationCollectionModel::parent(const QModelIndex& index) const
@@ -129,10 +155,10 @@ int AnnotationCollectionModel::rowCount(const QModelIndex& parent_idx) const
 {
        (void)parent_idx;
 
-       if (!all_annotations_)
+       if (!dataset_)
                return 0;
 
-       return all_annotations_->size();
+       return dataset_->size();
 }
 
 int AnnotationCollectionModel::columnCount(const QModelIndex& parent_idx) const
@@ -146,7 +172,9 @@ void AnnotationCollectionModel::set_signal_and_segment(data::DecodeSignal* signa
 {
        if (!signal) {
                all_annotations_ = nullptr;
+               dataset_ = nullptr;
                signal_ = nullptr;
+
                dataChanged(QModelIndex(), QModelIndex());
                layoutChanged();
                return;
@@ -155,12 +183,17 @@ void AnnotationCollectionModel::set_signal_and_segment(data::DecodeSignal* signa
        all_annotations_ = signal->get_all_annotations_by_segment(current_segment);
        signal_ = signal;
 
-       if (!all_annotations_ || all_annotations_->empty()) {
+       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;
+       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) {
@@ -179,6 +212,44 @@ void AnnotationCollectionModel::set_signal_and_segment(data::DecodeSignal* signa
        prev_last_row_ = new_row_count;
 }
 
+void AnnotationCollectionModel::set_hide_hidden(bool hide_hidden)
+{
+       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
+       }
+}
+
+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);
+
+       dataChanged(index(0, 0, QModelIndex()), index(count, 0, QModelIndex()));
+       layoutChanged();
+}
+
 void AnnotationCollectionModel::on_setting_changed(const QString &key, const QVariant &value)
 {
        (void)key;