X-Git-Url: https://sigrok.org/gitweb/?p=pulseview.git;a=blobdiff_plain;f=pv%2Fviews%2Ftabular_decoder%2Fmodel.cpp;h=b87bf05869ef1f118ad69853ff3899ec4e1d097a;hp=e54fda8c82f096617e681ab4ecddea904fa34ae1;hb=4d0f333acdcd747ffc542d77e679ce08af6031ae;hpb=f54e68b03d5d24c7787962fcc701d8d52b0ec8ab diff --git a/pv/views/tabular_decoder/model.cpp b/pv/views/tabular_decoder/model.cpp index e54fda8c..b87bf058 100644 --- a/pv/views/tabular_decoder/model.cpp +++ b/pv/views/tabular_decoder/model.cpp @@ -17,13 +17,24 @@ * along with this program; if not, see . */ +#include #include #include #include "pv/views/tabular_decoder/view.hpp" +#include "view.hpp" + +#include "pv/util.hpp" +#include "pv/globalsettings.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 { @@ -31,33 +42,106 @@ namespace tabular_decoder { AnnotationCollectionModel::AnnotationCollectionModel(QObject* parent) : QAbstractTableModel(parent), all_annotations_(nullptr), + dataset_(nullptr), + signal_(nullptr), + first_hidden_column_(0), prev_segment_(0), - prev_last_row_(0) + prev_last_row_(0), + had_highlight_before_(false), + hide_hidden_(false) +{ + // Note: when adding entries, consider ViewVisibleFilterProxyModel::filterAcceptsRow() + + uint8_t i = 0; + header_data_.emplace_back(tr("Sample")); i++; // Column #0 + header_data_.emplace_back(tr("Time")); i++; // Column #1 + header_data_.emplace_back(tr("Decoder")); i++; // Column #2 + header_data_.emplace_back(tr("Ann Row")); i++; // Column #3 + header_data_.emplace_back(tr("Ann Class")); i++; // Column #4 + header_data_.emplace_back(tr("Value")); i++; // Column #5 + + first_hidden_column_ = i; + header_data_.emplace_back("End Sample"); // Column #6, hidden +} + +int AnnotationCollectionModel::get_hierarchy_level(const Annotation* ann) const +{ + int level = 0; + + const unsigned int ann_stack_level = ann->row_data()->row()->decoder()->get_stack_level(); + level = (signal_->decoder_stack().size() - 1 - ann_stack_level); + + return level; +} + +QVariant AnnotationCollectionModel::data_from_ann(const Annotation* ann, int index) const { - // TBD Maybe use empty columns as indentation levels to indicate stacked decoders - header_data_.emplace_back(tr("Start Sample")); // Column #0 - header_data_.emplace_back(tr("Start Time")); // Column #1 - header_data_.emplace_back(tr("Ann Row Name")); // Column #2 - header_data_.emplace_back(tr("Ann Class Name")); // Column #3 - header_data_.emplace_back(tr("Value")); // Column #4 + 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 + case 6: return QVariant((qulonglong)ann->end_sample()); // Column #6, End Sample + default: return QVariant(); + } } QVariant AnnotationCollectionModel::data(const QModelIndex& index, int role) const { - if (!index.isValid()) + if (!signal_ || !index.isValid() || !index.internalPointer()) return QVariant(); - if (role == Qt::DisplayRole) { - const Annotation* ann = - static_cast(index.internalPointer()); - - 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()->title()); // Column #2, Ann Row Name - case 3: return QVariant(ann->ann_class_name()); // Column #3, Ann Class Name - case 4: return QVariant(ann->longest_annotation()); // Column #4, Value - default: return QVariant(); + const Annotation* ann = + static_cast(index.internalPointer()); + + if ((role == Qt::DisplayRole) || (role == Qt::ToolTipRole)) + return data_from_ann(ann, index.column()); + + if (role == Qt::ForegroundRole) { + if (index.column() >= get_hierarchy_level(ann)) { + // Invert the text color if this cell is highlighted + const bool must_highlight = (highlight_sample_num_ > 0) && + ((int64_t)ann->start_sample() <= highlight_sample_num_) && + ((int64_t)ann->end_sample() >= highlight_sample_num_); + + if (must_highlight) { + if (GlobalSettings::current_theme_is_dark()) + return QApplication::palette().brush(QPalette::Window); + else + return QApplication::palette().brush(QPalette::WindowText); + } + } + + return QApplication::palette().brush(QPalette::WindowText); + } + + if (role == Qt::BackgroundRole) { + // Only use custom cell background color if column index reached the hierarchy level + if (index.column() >= get_hierarchy_level(ann)) { + + QColor color; + const bool must_highlight = (highlight_sample_num_ > 0) && + ((int64_t)ann->start_sample() <= highlight_sample_num_) && + ((int64_t)ann->end_sample() >= highlight_sample_num_); + + if (must_highlight) + color = ann->color(); + else + color = GlobalSettings::current_theme_is_dark() ? + ann->dark_color() : ann->bright_color(); + + return QBrush(color); } } @@ -67,15 +151,20 @@ QVariant AnnotationCollectionModel::data(const QModelIndex& index, int role) con Qt::ItemFlags AnnotationCollectionModel::flags(const QModelIndex& index) const { if (!index.isValid()) - return 0; + return Qt::NoItemFlags; - return Qt::ItemIsEnabled | Qt::ItemIsSelectable; + return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemNeverHasChildren; +} + +uint8_t AnnotationCollectionModel::first_hidden_column() const +{ + return first_hidden_column_; } 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(); @@ -85,14 +174,17 @@ QModelIndex AnnotationCollectionModel::index(int row, int column, const QModelIndex& parent_idx) const { (void)parent_idx; + 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 ((size_t)row < dataset_->size()) + idx = createIndex(row, column, (void*)dataset_->at(row)); + + return idx; } QModelIndex AnnotationCollectionModel::parent(const QModelIndex& index) const @@ -106,10 +198,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 @@ -121,24 +213,47 @@ int AnnotationCollectionModel::columnCount(const QModelIndex& parent_idx) const void AnnotationCollectionModel::set_signal_and_segment(data::DecodeSignal* signal, uint32_t current_segment) { + layoutAboutToBeChanged(); + + 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& 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; + 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) { - dataChanged(QModelIndex(), QModelIndex()); + dataChanged(index(0, 0), index(new_row_count, 0)); layoutChanged(); } 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(); } } @@ -147,6 +262,108 @@ void AnnotationCollectionModel::set_signal_and_segment(data::DecodeSignal* signa prev_last_row_ = new_row_count; } +void AnnotationCollectionModel::set_hide_hidden(bool hide_hidden) +{ + layoutAboutToBeChanged(); + + 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 + } + + if (dataset_) + dataChanged(index(0, 0), index(dataset_->size() - 1, 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); +} + +QModelIndex AnnotationCollectionModel::update_highlighted_rows(QModelIndex first, + QModelIndex last, int64_t sample_num) +{ + bool has_highlight = false; + QModelIndex result; + + highlight_sample_num_ = sample_num; + + if (!dataset_ || dataset_->empty()) + return result; + + if (sample_num >= 0) { + last = last.sibling(last.row() + 1, 0); + + // Check if there are any annotations visible in the table view that + // we would need to highlight - only then do we do so + QModelIndex index = first; + do { + const Annotation* ann = static_cast(index.internalPointer()); + if (!ann) // Can happen if the table is being modified at this exact time + return result; + + if (((int64_t)ann->start_sample() <= sample_num) && + ((int64_t)ann->end_sample() >= sample_num)) { + result = index; + has_highlight = true; + break; + } + + index = index.sibling(index.row() + 1, 0); + } while (index != last); + } + + if (has_highlight || had_highlight_before_) + dataChanged(first, last); + + had_highlight_before_ = has_highlight; + + return result; +} + +void AnnotationCollectionModel::on_annotation_visibility_changed() +{ + if (!hide_hidden_) + return; + + layoutAboutToBeChanged(); + + update_annotations_without_hidden(); + + if (dataset_) + dataChanged(index(0, 0), index(dataset_->size() - 1, 0)); + else + dataChanged(QModelIndex(), QModelIndex()); + + layoutChanged(); +} + } // namespace tabular_decoder } // namespace views } // namespace pv