]> sigrok.org Git - pulseview.git/blobdiff - pv/data/decodesignal.cpp
Add row colors and some fixes
[pulseview.git] / pv / data / decodesignal.cpp
index ce38fbe3df15b90abec65378851ad20cbcae758f..77784de07f84dcdce9431ef03fc634de56aed02e 100644 (file)
@@ -33,7 +33,6 @@
 #include <pv/globalsettings.hpp>
 #include <pv/session.hpp>
 
-using std::forward_list;
 using std::lock_guard;
 using std::make_shared;
 using std::min;
@@ -84,7 +83,7 @@ void DecodeSignal::stack_decoder(const srd_decoder *decoder, bool restart_decode
        if ((stack_.empty()) || ((stack_.size() > 0) && (name() == prev_dec_name)))
                set_name(QString::fromUtf8(decoder->name));
 
-       const shared_ptr<Decoder> dec = make_shared<Decoder>(decoder);
+       const shared_ptr<Decoder> dec = make_shared<Decoder>(decoder, stack_.size());
        stack_.push_back(dec);
 
        // Include the newly created decode channels in the channel lists
@@ -141,6 +140,8 @@ bool DecodeSignal::toggle_decoder_visibility(int index)
 
 void DecodeSignal::reset_decode(bool shutting_down)
 {
+       resume_decode();  // Make sure the decode thread isn't blocked by pausing
+
        if (stack_config_changed_ || shutting_down)
                stop_srd_session();
        else
@@ -158,8 +159,6 @@ void DecodeSignal::reset_decode(bool shutting_down)
                logic_mux_thread_.join();
        }
 
-       resume_decode();  // Make sure the decode thread isn't blocked by pausing
-
        current_segment_id_ = 0;
        segments_.clear();
 
@@ -478,7 +477,7 @@ uint64_t DecodeSignal::get_annotation_count(const Row* row, uint32_t segment_id)
        return rd->get_annotation_count();
 }
 
-void DecodeSignal::get_annotation_subset(vector<const Annotation*> &dest,
+void DecodeSignal::get_annotation_subset(deque<const Annotation*> &dest,
        const Row* row, uint32_t segment_id, uint64_t start_sample,
        uint64_t end_sample) const
 {
@@ -500,27 +499,11 @@ void DecodeSignal::get_annotation_subset(vector<const Annotation*> &dest,
        rd->get_annotation_subset(dest, start_sample, end_sample);
 }
 
-void DecodeSignal::get_annotation_subset(vector<const Annotation*> &dest,
+void DecodeSignal::get_annotation_subset(deque<const Annotation*> &dest,
        uint32_t segment_id, uint64_t start_sample, uint64_t end_sample) const
 {
-       // Use forward_lists for faster merging
-       forward_list<const Annotation*> *all_ann_list = new forward_list<const Annotation*>();
-
-       vector<const Row*> rows = get_rows();
-       for (const Row* row : rows) {
-               vector<const Annotation*> *ann_vector = new vector<const Annotation*>();
-               get_annotation_subset(*ann_vector, row, segment_id, start_sample, end_sample);
-
-               forward_list<const Annotation*> *ann_list =
-                       new forward_list<const Annotation*>(ann_vector->begin(), ann_vector->end());
-               delete ann_vector;
-
-               all_ann_list->merge(*ann_list);
-               delete ann_list;
-       }
-
-       move(all_ann_list->begin(), all_ann_list->end(), back_inserter(dest));
-       delete all_ann_list;
+       for (const Row* row : get_rows())
+               get_annotation_subset(dest, row, segment_id, start_sample, end_sample);
 }
 
 uint32_t DecodeSignal::get_binary_data_chunk_count(uint32_t segment_id,
@@ -659,6 +642,19 @@ const DecodeBinaryClass* DecodeSignal::get_binary_data_class(uint32_t segment_id
        return nullptr;
 }
 
+const deque<const Annotation*>* DecodeSignal::get_all_annotations_by_segment(
+       uint32_t segment_id) const
+{
+       try {
+               const DecodeSegment *segment = &(segments_.at(segment_id));
+               return &(segment->all_annotations);
+       } catch (out_of_range&) {
+               // Do nothing
+       }
+
+       return nullptr;
+}
+
 void DecodeSignal::save_settings(QSettings &settings) const
 {
        SignalBase::save_settings(settings);
@@ -689,6 +685,24 @@ void DecodeSignal::save_settings(QSettings &settings) const
                        i++;
                }
 
+               // Save row properties
+               i = 0;
+               for (const Row* row : decoder->get_rows()) {
+                       settings.beginGroup("row" + QString::number(i));
+                       settings.setValue("visible", row->visible());
+                       settings.endGroup();
+                       i++;
+               }
+
+               // Save class properties
+               i = 0;
+               for (const AnnotationClass* ann_class : decoder->ann_classes()) {
+                       settings.beginGroup("ann_class" + QString::number(i));
+                       settings.setValue("visible", ann_class->visible);
+                       settings.endGroup();
+                       i++;
+               }
+
                settings.endGroup();
        }
 
@@ -736,7 +750,7 @@ void DecodeSignal::restore_settings(QSettings &settings)
                                continue;
 
                        if (QString::fromUtf8(dec->id) == id) {
-                               shared_ptr<Decoder> decoder = make_shared<Decoder>(dec);
+                               shared_ptr<Decoder> decoder = make_shared<Decoder>(dec, stack_.size());
 
                                stack_.push_back(decoder);
                                decoder->set_visible(settings.value("visible", true).toBool());
@@ -754,6 +768,25 @@ void DecodeSignal::restore_settings(QSettings &settings)
 
                                // Include the newly created decode channels in the channel lists
                                update_channel_list();
+
+                               // Restore row properties
+                               int i = 0;
+                               for (Row* row : decoder->get_rows()) {
+                                       settings.beginGroup("row" + QString::number(i));
+                                       row->set_visible(settings.value("visible", true).toBool());
+                                       settings.endGroup();
+                                       i++;
+                               }
+
+                               // Restore class properties
+                               i = 0;
+                               for (AnnotationClass* ann_class : decoder->ann_classes()) {
+                                       settings.beginGroup("ann_class" + QString::number(i));
+                                       ann_class->visible = settings.value("visible", true).toBool();
+                                       settings.endGroup();
+                                       i++;
+                               }
+
                                break;
                        }
                }
@@ -765,7 +798,7 @@ void DecodeSignal::restore_settings(QSettings &settings)
        // Restore channel mapping
        unsigned int channels = settings.value("channels").toInt();
 
-       const unordered_set< shared_ptr<data::SignalBase> > signalbases =
+       const vector< shared_ptr<data::SignalBase> > signalbases =
                session_.signalbases();
 
        for (unsigned int channel_id = 0; channel_id < channels; channel_id++) {
@@ -1359,7 +1392,7 @@ void DecodeSignal::connect_input_notifiers()
 void DecodeSignal::create_decode_segment()
 {
        // Create annotation segment
-       segments_.emplace_back(DecodeSegment());
+       segments_.emplace_back();
 
        // Add annotation classes
        for (const shared_ptr<Decoder>& dec : stack_)
@@ -1387,6 +1420,9 @@ void DecodeSignal::annotation_callback(srd_proto_data *pdata, void *decode_signa
        if (ds->decode_interrupt_)
                return;
 
+       if (ds->segments_.empty())
+               return;
+
        lock_guard<mutex> lock(ds->output_mutex_);
 
        // Get the decoder and the annotation data
@@ -1415,8 +1451,38 @@ void DecodeSignal::annotation_callback(srd_proto_data *pdata, void *decode_signa
        if (!row)
                row = dec->get_row_by_id(0);
 
-       // Add the annotation
-       ds->segments_[ds->current_segment_id_].annotation_rows.at(row).emplace_annotation(pdata);
+       RowData& row_data = ds->segments_[ds->current_segment_id_].annotation_rows.at(row);
+
+       // Add the annotation to the row
+       const Annotation* ann = row_data.emplace_annotation(pdata);
+
+       // Add the annotation to the global annotation list
+       deque<const Annotation*>& all_annotations =
+               ds->segments_[ds->current_segment_id_].all_annotations;
+       all_annotations.emplace_back(ann);
+
+       // When emplace_annotation() inserts instead of appends an annotation,
+       // the pointers in all_annotations that follow the inserted annotation and
+       // point to annotations for this row are off by one and must be updated
+       if (&(row_data.annotations().back()) != ann) {
+               // Search backwards until we find the annotation we just added
+               auto row_it = row_data.annotations().end();
+               auto all_it = all_annotations.end();
+               do {
+                       all_it--;
+                       if ((*all_it)->row_data() == &row_data)
+                               row_it--;
+               } while (&(*row_it) != ann);
+
+               // Update the annotation addresses for this row's annotations until the end
+               do {
+                       if ((*all_it)->row_data() == &row_data) {
+                               *all_it = &(*row_it);
+                               row_it++;
+                       }
+                       all_it++;
+               } while (all_it != all_annotations.end());
+       }
 }
 
 void DecodeSignal::binary_callback(srd_proto_data *pdata, void *decode_signal)