]> sigrok.org Git - pulseview.git/blobdiff - pv/views/trace/view.cpp
TraceView: Don't use unordered sets and maps
[pulseview.git] / pv / views / trace / view.cpp
index e5cf980a79223b1a84412fb1c845070194d4458d..93bcb52072f01ea2e7fe61b433e5849606013323 100644 (file)
@@ -29,7 +29,6 @@
 #include <cmath>
 #include <iostream>
 #include <iterator>
-#include <unordered_set>
 
 #include <QApplication>
 #include <QDebug>
@@ -52,6 +51,7 @@
 #include "view.hpp"
 #include "viewport.hpp"
 
+#include "pv/metadata_obj.hpp"
 #include "pv/data/logic.hpp"
 #include "pv/data/logicsegment.hpp"
 #include "pv/devices/device.hpp"
@@ -81,8 +81,6 @@ using std::pair;
 using std::set;
 using std::set_difference;
 using std::shared_ptr;
-using std::unordered_map;
-using std::unordered_set;
 using std::vector;
 
 namespace pv {
@@ -182,6 +180,13 @@ View::View(Session &session, bool is_main_view, QMainWindow *parent) :
 
        GlobalSettings::add_change_handler(this);
 
+       // Set up metadata objects and event handlers
+       if (is_main_view) {
+               session_.metadata_obj_manager()->create_object(MetadataObjMainViewRange);
+               session_.metadata_obj_manager()->create_object(MetadataObjMousePos);
+       }
+
+       // Set up UI event handlers
        connect(scrollarea_->horizontalScrollBar(), SIGNAL(valueChanged(int)),
                this, SLOT(h_scroll_value_changed(int)));
        connect(scrollarea_->verticalScrollBar(), SIGNAL(valueChanged(int)),
@@ -203,6 +208,7 @@ View::View(Session &session, bool is_main_view, QMainWindow *parent) :
        connect(&lazy_event_handler_, SIGNAL(timeout()),
                this, SLOT(process_sticky_events()));
        lazy_event_handler_.setSingleShot(true);
+       lazy_event_handler_.setInterval(1000 / ViewBase::MaxViewAutoUpdateRate);
 
        // Set up local keyboard shortcuts
        zoom_in_shortcut_ = new QShortcut(QKeySequence(Qt::Key_Plus), this,
@@ -317,7 +323,7 @@ const Session& View::session() const
        return session_;
 }
 
-unordered_set< shared_ptr<Signal> > View::signals() const
+vector< shared_ptr<Signal> > View::signals() const
 {
        return signals_;
 }
@@ -344,7 +350,7 @@ void View::clear_signals()
 void View::add_signal(const shared_ptr<Signal> signal)
 {
        ViewBase::add_signalbase(signal->base());
-       signals_.insert(signal);
+       signals_.push_back(signal);
 
        signal->set_segment_display_mode(segment_display_mode_);
        signal->set_current_segment(current_segment_);
@@ -524,6 +530,22 @@ void View::set_offset(const pv::util::Timestamp& offset, bool force_update)
        if ((offset_ != offset) || force_update) {
                offset_ = offset;
                ruler_offset_ = offset_ + zero_offset_;
+
+               const int w = viewport_->width();
+               if (w > 0) {
+                       const double samplerate = session_.get_samplerate();
+                       // Note: sample_num = time * samplerate
+                       // Note: samples_per_pixel = samplerate * scale
+                       int64_t start_sample = (offset_ * samplerate).convert_to<int64_t>();
+                       int64_t end_sample = (offset_ * samplerate).convert_to<int64_t>() +
+                               (w * session_.get_samplerate() * scale_);
+
+                       MetadataObject* md_obj =
+                               session_.metadata_obj_manager()->find_object_by_type(MetadataObjMainViewRange);
+                       md_obj->set_value(MetadataValueStartSample, QVariant((qlonglong)start_sample));
+                       md_obj->set_value(MetadataValueEndSample, QVariant((qlonglong)end_sample));
+               }
+
                offset_changed();
        }
 }
@@ -782,6 +804,26 @@ void View::zoom_fit(bool gui_state)
        set_scale_offset(scale.convert_to<double>(), extents.first);
 }
 
+void View::focus_on_range(uint64_t start_sample, uint64_t end_sample)
+{
+       assert(viewport_);
+       const int w = viewport_->width();
+       if (w <= 0)
+               return;
+
+       const double samplerate = session_.get_samplerate();
+
+       const uint64_t sample_delta = (end_sample - start_sample);
+
+       // Note: We add 20% margin on the left and 5% on the right
+       const Timestamp delta = (sample_delta * 1.25) / samplerate;
+
+       const Timestamp scale = max(min(delta / w, MaxScale), MinScale);
+       const Timestamp offset = (start_sample - sample_delta * 0.20) / samplerate;
+
+       set_scale_offset(scale.convert_to<double>(), offset);
+}
+
 void View::set_scale_offset(double scale, const Timestamp& offset)
 {
        // Disable sticky scrolling / always zoom to fit when acquisition runs
@@ -810,13 +852,13 @@ void View::set_scale_offset(double scale, const Timestamp& offset)
        viewport_->update();
 }
 
-set< shared_ptr<SignalData> > View::get_visible_data() const
+vector< shared_ptr<SignalData> > View::get_visible_data() const
 {
        // Make a set of all the visible data objects
-       set< shared_ptr<SignalData> > visible_data;
+       vector< shared_ptr<SignalData> > visible_data;
        for (const shared_ptr<Signal>& sig : signals_)
                if (sig->enabled())
-                       visible_data.insert(sig->data());
+                       visible_data.push_back(sig->data());
 
        return visible_data;
 }
@@ -824,8 +866,14 @@ set< shared_ptr<SignalData> > View::get_visible_data() const
 pair<Timestamp, Timestamp> View::get_time_extents() const
 {
        boost::optional<Timestamp> left_time, right_time;
-       const set< shared_ptr<SignalData> > visible_data = get_visible_data();
-       for (const shared_ptr<SignalData>& d : visible_data) {
+
+       vector< shared_ptr<SignalData> > data;
+       if (signals_.size() == 0)
+               return make_pair(0, 0);
+
+       data.push_back(signals_.front()->data());
+
+       for (const shared_ptr<SignalData>& d : data) {
                const vector< shared_ptr<Segment> > segments = d->segments();
                for (const shared_ptr<Segment>& s : segments) {
                        double samplerate = s->samplerate();
@@ -1324,12 +1372,11 @@ void View::update_layout()
 
 TraceTreeItemOwner* View::find_prevalent_trace_group(
        const shared_ptr<sigrok::ChannelGroup> &group,
-       const unordered_map<shared_ptr<data::SignalBase>, shared_ptr<Signal> >
-               &signal_map)
+       const map<shared_ptr<data::SignalBase>, shared_ptr<Signal> > &signal_map)
 {
        assert(group);
 
-       unordered_set<TraceTreeItemOwner*> owners;
+       set<TraceTreeItemOwner*> owners;
        vector<TraceTreeItemOwner*> owner_list;
 
        // Make a set and a list of all the owners
@@ -1361,8 +1408,7 @@ TraceTreeItemOwner* View::find_prevalent_trace_group(
 
 vector< shared_ptr<Trace> > View::extract_new_traces_for_channels(
        const vector< shared_ptr<sigrok::Channel> > &channels,
-       const unordered_map<shared_ptr<data::SignalBase>, shared_ptr<Signal> >
-               &signal_map,
+       const map<shared_ptr<data::SignalBase>, shared_ptr<Signal> > &signal_map,
        set< shared_ptr<Trace> > &add_list)
 {
        vector< shared_ptr<Trace> > filtered_traces;
@@ -1522,8 +1568,19 @@ void View::update_hover_point()
        for (const shared_ptr<TraceTreeItem>& r : trace_tree_items)
                r->hover_point_changed(hover_point_);
 
-       // Notify any other listeners
+       // Notify this view's listeners
        hover_point_changed(hover_widget_, hover_point_);
+
+       // Hover point is -1 when invalid and 0 for the header
+       if (hover_point_.x() > 0) {
+               // Notify global listeners
+               pv::util::Timestamp mouse_time = offset_ + hover_point_.x() * scale_;
+               int64_t sample_num = (mouse_time * session_.get_samplerate()).convert_to<int64_t>();
+
+               MetadataObject* md_obj =
+                       session_.metadata_obj_manager()->find_object_by_type(MetadataObjMousePos);
+               md_obj->set_value(MetadataValueStartSample, QVariant((qlonglong)sample_num));
+       }
 }
 
 void View::row_item_appearance_changed(bool label, bool content)
@@ -1553,8 +1610,8 @@ void View::extents_changed(bool horz, bool vert)
                (horz ? TraceTreeItemHExtentsChanged : 0) |
                (vert ? TraceTreeItemVExtentsChanged : 0);
 
-       lazy_event_handler_.stop();
-       lazy_event_handler_.start();
+       if (!lazy_event_handler_.isActive())
+               lazy_event_handler_.start();
 }
 
 void View::on_signal_name_changed()
@@ -1692,8 +1749,7 @@ void View::signals_changed()
                inserter(remove_traces, remove_traces.begin()));
 
        // Make a look-up table of sigrok Channels to pulseview Signals
-       unordered_map<shared_ptr<data::SignalBase>, shared_ptr<Signal> >
-               signal_map;
+       map<shared_ptr<data::SignalBase>, shared_ptr<Signal> > signal_map;
        for (const shared_ptr<Signal>& sig : signals_)
                signal_map[sig->base()] = sig;
 
@@ -1794,7 +1850,7 @@ void View::signals_changed()
 
        // Add and position the pending top levels items
        int offset = v_extents().second;
-       for (auto item : new_top_level_items) {
+       for (shared_ptr<TraceTreeItem> item : new_top_level_items) {
                add_child_item(item);
 
                // Position the item after the last item or at the top if there is none