X-Git-Url: https://sigrok.org/gitweb/?p=pulseview.git;a=blobdiff_plain;f=pv%2Fviews%2Ftabular_decoder%2Fmodel.cpp;h=66fabf5ac1534817781ebe07a453bbdbc2bedaad;hp=35cdc9583b8c92c86b283c990107f9eda18be87f;hb=88a2597864920ecdbe66cf0cd4b8172bdabb2263;hpb=24d69d27584c7adec70bc0d6db764a3db04fce3c diff --git a/pv/views/tabular_decoder/model.cpp b/pv/views/tabular_decoder/model.cpp index 35cdc958..66fabf5a 100644 --- a/pv/views/tabular_decoder/model.cpp +++ b/pv/views/tabular_decoder/model.cpp @@ -17,6 +17,7 @@ * along with this program; if not, see . */ +#include #include #include "pv/views/tabular_decoder/view.hpp" @@ -28,16 +29,21 @@ namespace views { namespace tabular_decoder { AnnotationCollectionModel::AnnotationCollectionModel(QObject* parent) : - QAbstractItemModel(parent) + QAbstractTableModel(parent), + all_annotations_(nullptr), + prev_segment_(0), + prev_last_row_(0) { - vector header_data; - header_data.emplace_back(tr("ID")); // Column #0 - header_data.emplace_back(tr("Start Time")); // Column #1 - header_data.emplace_back(tr("End Time")); // Column #2 - header_data.emplace_back(tr("Ann Row Name")); // Column #3 - header_data.emplace_back(tr("Class Row Name")); // Column #4 - header_data.emplace_back(tr("Value")); // Column #5 - root_ = make_shared(header_data); + 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 + header_data_.emplace_back(tr("Decoder")); // Column #2 + header_data_.emplace_back(tr("Ann Row")); // Column #3 + header_data_.emplace_back(tr("Ann Class")); // Column #4 + header_data_.emplace_back(tr("Value")); // Column #5 } QVariant AnnotationCollectionModel::data(const QModelIndex& index, int role) const @@ -45,19 +51,26 @@ QVariant AnnotationCollectionModel::data(const QModelIndex& index, int role) con if (!index.isValid()) return QVariant(); - if (role == Qt::DisplayRole) - { - AnnotationCollectionItem* item = - static_cast(index.internalPointer()); - - return item->data(index.column()); + const Annotation* ann = + static_cast(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::FontRole) && (index.parent().isValid()) && (index.column() == 0)) - { - QFont font; - font.setItalic(true); - return QVariant(font); + if (role == Qt::BackgroundRole) { + if (theme_is_dark_) + return QBrush(ann->dark_color()); + else + return QBrush(ann->bright_color()); } return QVariant(); @@ -66,7 +79,7 @@ QVariant AnnotationCollectionModel::data(const QModelIndex& index, int role) con Qt::ItemFlags AnnotationCollectionModel::flags(const QModelIndex& index) const { if (!index.isValid()) - return nullptr; + return 0; return Qt::ItemIsEnabled | Qt::ItemIsSelectable; } @@ -75,7 +88,7 @@ QVariant AnnotationCollectionModel::headerData(int section, Qt::Orientation orie int role) const { if (orientation == Qt::Horizontal && role == Qt::DisplayRole) - return root_->data(section); + return header_data_.at(section); return QVariant(); } @@ -83,55 +96,85 @@ QVariant AnnotationCollectionModel::headerData(int section, Qt::Orientation orie QModelIndex AnnotationCollectionModel::index(int row, int column, const QModelIndex& parent_idx) const { - if (!hasIndex(row, column, parent_idx)) - return QModelIndex(); - - AnnotationCollectionItem* parent = root_.get(); + (void)parent_idx; - if (parent_idx.isValid()) - parent = static_cast(parent_idx.internalPointer()); + if (!all_annotations_) + return QModelIndex(); - AnnotationCollectionItem* subItem = parent->subItem(row).get(); + if ((size_t)row > all_annotations_->size()) + return QModelIndex(); - return subItem ? createIndex(row, column, subItem) : QModelIndex(); + return createIndex(row, column, (void*)(all_annotations_->at(row))); } QModelIndex AnnotationCollectionModel::parent(const QModelIndex& index) const { - if (!index.isValid()) - return QModelIndex(); - - AnnotationCollectionItem* subItem = - static_cast(index.internalPointer()); - - shared_ptr parent = subItem->parent(); + (void)index; - return (parent == root_) ? QModelIndex() : - createIndex(parent->row(), 0, parent.get()); + return QModelIndex(); } int AnnotationCollectionModel::rowCount(const QModelIndex& parent_idx) const { - AnnotationCollectionItem* parent = root_.get(); + (void)parent_idx; - if (parent_idx.column() > 0) + if (!all_annotations_) return 0; - if (parent_idx.isValid()) - parent = static_cast(parent_idx.internalPointer()); - - return parent->subItemCount(); + return all_annotations_->size(); } int AnnotationCollectionModel::columnCount(const QModelIndex& parent_idx) const { - if (parent_idx.isValid()) - return static_cast( - parent_idx.internalPointer())->columnCount(); - else - return root_->columnCount(); + (void)parent_idx; + + return header_data_.size(); } +void AnnotationCollectionModel::set_signal_and_segment(data::DecodeSignal* signal, uint32_t current_segment) +{ + if (!signal) { + all_annotations_ = nullptr; + dataChanged(QModelIndex(), QModelIndex()); + layoutChanged(); + return; + } + + all_annotations_ = signal->get_all_annotations_by_segment(current_segment); + + if (!all_annotations_ || all_annotations_->empty()) { + prev_segment_ = current_segment; + return; + } + + const size_t new_row_count = all_annotations_->size() - 1; + + // Force the view associated with this model to update when the segment changes + if (prev_segment_ != current_segment) { + dataChanged(QModelIndex(), QModelIndex()); + 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())); + layoutChanged(); + } + } + + prev_segment_ = current_segment; + prev_last_row_ = new_row_count; +} + +void AnnotationCollectionModel::on_setting_changed(const QString &key, const QVariant &value) +{ + (void)key; + (void)value; + + // 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(); +} } // namespace tabular_decoder } // namespace views