]> sigrok.org Git - pulseview.git/blobdiff - pv/views/tabular_decoder/model.cpp
TabularDecView-related bug fixes
[pulseview.git] / pv / views / tabular_decoder / model.cpp
index 66fabf5ac1534817781ebe07a453bbdbc2bedaad..0c89cef314f3a8547dfdad3659a90fc1c21676cc 100644 (file)
 
 #include "pv/views/tabular_decoder/view.hpp"
 
+#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 {
@@ -31,6 +40,7 @@ namespace tabular_decoder {
 AnnotationCollectionModel::AnnotationCollectionModel(QObject* parent) :
        QAbstractTableModel(parent),
        all_annotations_(nullptr),
+       signal_(nullptr),
        prev_segment_(0),
        prev_last_row_(0)
 {
@@ -46,31 +56,52 @@ 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())
+       if (!index.isValid() || !signal_)
                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) {
-               if (theme_is_dark_)
-                       return QBrush(ann->dark_color());
-               else
-                       return QBrush(ann->bright_color());
+               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);
+
+               // Only use custom cell background color if column index reached the hierarchy level
+               if (index.column() >= level) {
+                       if (theme_is_dark_)
+                               return QBrush(ann->dark_color());
+                       else
+                               return QBrush(ann->bright_color());
+               }
        }
 
        return QVariant();
@@ -79,9 +110,9 @@ 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;
 }
 
 QVariant AnnotationCollectionModel::headerData(int section, Qt::Orientation orientation,
@@ -135,12 +166,14 @@ void AnnotationCollectionModel::set_signal_and_segment(data::DecodeSignal* signa
 {
        if (!signal) {
                all_annotations_ = nullptr;
+               signal_ = nullptr;
                dataChanged(QModelIndex(), QModelIndex());
                layoutChanged();
                return;
        }
 
        all_annotations_ = signal->get_all_annotations_by_segment(current_segment);
+       signal_ = signal;
 
        if (!all_annotations_ || all_annotations_->empty()) {
                prev_segment_ = current_segment;