Trace View: Move ruler time conversion from View to Ruler
[pulseview.git] / pv / views / trace / view.cpp
index 625722d4308f9e3128c4f6dd42d2d49e6e8a52b9..4b95a806a490cf492ad33cb6e2b537b5f8744165 100644 (file)
@@ -241,6 +241,7 @@ void View::reset_view_state()
        cursors_ = make_shared<CursorPair>(*this);
        next_flag_text_ = 'A';
        trigger_markers_.clear();
+       hover_widget_ = nullptr;
        hover_point_ = QPoint(-1, -1);
        scroll_needs_defaults_ = true;
        saved_v_offset_ = 0;
@@ -347,6 +348,11 @@ const Viewport* View::viewport() const
        return viewport_;
 }
 
+const Ruler* View::ruler() const
+{
+       return ruler_;
+}
+
 void View::save_settings(QSettings &settings) const
 {
        settings.setValue("scale", scale_);
@@ -356,12 +362,6 @@ void View::save_settings(QSettings &settings) const
        settings.setValue("splitter_state", splitter_->saveState());
        settings.setValue("segment_display_mode", segment_display_mode_);
 
-       {
-               stringstream ss;
-               boost::archive::text_oarchive oa(ss);
-               oa << boost::serialization::make_nvp("ruler_shift", ruler_shift_);
-               settings.setValue("ruler_shift", QString::fromStdString(ss.str()));
-       }
        {
                stringstream ss;
                boost::archive::text_oarchive oa(ss);
@@ -369,7 +369,7 @@ void View::save_settings(QSettings &settings) const
                settings.setValue("offset", QString::fromStdString(ss.str()));
        }
 
-       for (shared_ptr<Signal> signal : signals_) {
+       for (const shared_ptr<Signal>& signal : signals_) {
                settings.beginGroup(signal->base()->internal_name());
                signal->save_settings(settings);
                settings.endGroup();
@@ -384,20 +384,6 @@ void View::restore_settings(QSettings &settings)
        if (settings.contains("scale"))
                set_scale(settings.value("scale").toDouble());
 
-       if (settings.contains("ruler_shift")) {
-               util::Timestamp shift;
-               stringstream ss;
-               ss << settings.value("ruler_shift").toString().toStdString();
-
-               try {
-                       boost::archive::text_iarchive ia(ss);
-                       ia >> boost::serialization::make_nvp("ruler_shift", shift);
-                       ruler_shift_ = shift;
-               } catch (boost::archive::archive_exception&) {
-                       qDebug() << "Could not restore the view ruler shift";
-               }
-       }
-
        if (settings.contains("offset")) {
                util::Timestamp offset;
                stringstream ss;
@@ -451,7 +437,7 @@ vector< shared_ptr<TimeItem> > View::time_items() const
                items.push_back(cursors_->second());
        }
 
-       for (auto trigger_marker : trigger_markers_)
+       for (auto& trigger_marker : trigger_markers_)
                items.push_back(trigger_marker);
 
        return items;
@@ -474,7 +460,7 @@ void View::set_offset(const pv::util::Timestamp& offset, bool force_update)
 {
        if ((offset_ != offset) || force_update) {
                offset_ = offset;
-               ruler_offset_ = offset_ + ruler_shift_;
+               ruler_offset_ = offset_ + zero_offset_;
                offset_changed();
        }
 }
@@ -489,9 +475,9 @@ const Timestamp& View::ruler_offset() const
        return ruler_offset_;
 }
 
-void View::set_zero_position(pv::util::Timestamp& position)
+void View::set_zero_position(const pv::util::Timestamp& position)
 {
-       ruler_shift_ = -position;
+       zero_offset_ = -position;
 
        // Force an immediate update of the offsets
        set_offset(offset_, true);
@@ -500,13 +486,18 @@ void View::set_zero_position(pv::util::Timestamp& position)
 
 void View::reset_zero_position()
 {
-       ruler_shift_ = 0;
+       zero_offset_ = 0;
 
        // Force an immediate update of the offsets
        set_offset(offset_, true);
        ruler_->update();
 }
 
+pv::util::Timestamp View::zero_offset() const
+{
+       return zero_offset_;
+}
+
 int View::owner_visual_v_offset() const
 {
        return -scrollarea_->verticalScrollBar()->sliderPosition();
@@ -590,10 +581,10 @@ void View::set_current_segment(uint32_t segment_id)
 {
        current_segment_ = segment_id;
 
-       for (shared_ptr<Signal> signal : signals_)
+       for (const shared_ptr<Signal>& signal : signals_)
                signal->set_current_segment(current_segment_);
 #ifdef ENABLE_DECODE
-       for (shared_ptr<DecodeTrace> dt : decode_traces_)
+       for (shared_ptr<DecodeTrace>& dt : decode_traces_)
                dt->set_current_segment(current_segment_);
 #endif
 
@@ -629,7 +620,7 @@ void View::set_segment_display_mode(Trace::SegmentDisplayMode mode)
 {
        segment_display_mode_ = mode;
 
-       for (shared_ptr<Signal> signal : signals_)
+       for (const shared_ptr<Signal>& signal : signals_)
                signal->set_segment_display_mode(mode);
 
        uint32_t last_segment = session_.get_segment_count() - 1;
@@ -738,7 +729,7 @@ set< shared_ptr<SignalData> > View::get_visible_data() const
 {
        // Make a set of all the visible data objects
        set< shared_ptr<SignalData> > visible_data;
-       for (const shared_ptr<Signal> sig : signals_)
+       for (const shared_ptr<Signal>& sig : signals_)
                if (sig->enabled())
                        visible_data.insert(sig->data());
 
@@ -749,9 +740,9 @@ 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) {
+       for (const shared_ptr<SignalData>& d : visible_data) {
                const vector< shared_ptr<Segment> > segments = d->segments();
-               for (const shared_ptr<Segment> &s : segments) {
+               for (const shared_ptr<Segment>s : segments) {
                        double samplerate = s->samplerate();
                        samplerate = (samplerate <= 0.0) ? 1.0 : samplerate;
 
@@ -804,22 +795,36 @@ bool View::cursors_shown() const
 
 void View::show_cursors(bool show)
 {
-       show_cursors_ = show;
-       cursor_state_changed(show);
+       if (show_cursors_ != show) {
+               show_cursors_ = show;
+
+               cursor_state_changed(show);
+               ruler_->update();
+               viewport_->update();
+       }
+}
+
+void View::set_cursors(pv::util::Timestamp& first, pv::util::Timestamp& second)
+{
+       assert(cursors_);
+
+       cursors_->first()->set_time(first);
+       cursors_->second()->set_time(second);
+
        ruler_->update();
        viewport_->update();
 }
 
 void View::centre_cursors()
 {
-       if (cursors_) {
-               const double time_width = scale_ * viewport_->width();
-               cursors_->first()->set_time(offset_ + time_width * 0.4);
-               cursors_->second()->set_time(offset_ + time_width * 0.6);
+       assert(cursors_);
 
-               ruler_->update();
-               viewport_->update();
-       }
+       const double time_width = scale_ * viewport_->width();
+       cursors_->first()->set_time(offset_ + time_width * 0.4);
+       cursors_->second()->set_time(offset_ + time_width * 0.6);
+
+       ruler_->update();
+       viewport_->update();
 }
 
 shared_ptr<CursorPair> View::cursors() const
@@ -860,6 +865,11 @@ const QPoint& View::hover_point() const
        return hover_point_;
 }
 
+const QWidget* View::hover_widget() const
+{
+       return hover_widget_;
+}
+
 int64_t View::get_nearest_level_change(const QPoint &p)
 {
        // Is snapping disabled?
@@ -1204,7 +1214,8 @@ void View::set_scroll_default()
 
 void View::determine_if_header_was_shrunk()
 {
-       const int header_pane_width = splitter_->sizes().front();
+       const int header_pane_width =
+               splitter_->sizes().front();  // clazy:exclude=detaching-temporary
 
        // Allow for a slight margin of error so that we also accept
        // slight differences when e.g. a label name change increased
@@ -1223,7 +1234,7 @@ void View::resize_header_to_fit()
        // splitter to the maximum allowed position.
 
        int splitter_area_width = 0;
-       for (int w : splitter_->sizes())
+       for (int w : splitter_->sizes())  // clazy:exclude=range-loop
                splitter_area_width += w;
 
        // Make sure the header has enough horizontal space to show all labels fully
@@ -1249,8 +1260,8 @@ TraceTreeItemOwner* View::find_prevalent_trace_group(
        vector<TraceTreeItemOwner*> owner_list;
 
        // Make a set and a list of all the owners
-       for (const auto &channel : group->channels()) {
-               for (auto entry : signal_map) {
+       for (const autochannel : group->channels()) {
+               for (auto& entry : signal_map) {
                        if (entry.first->channel() == channel) {
                                TraceTreeItemOwner *const o = (entry.second)->owner();
                                owner_list.push_back(o);
@@ -1283,8 +1294,8 @@ vector< shared_ptr<Trace> > View::extract_new_traces_for_channels(
 {
        vector< shared_ptr<Trace> > filtered_traces;
 
-       for (const auto &channel : channels) {
-               for (auto entry : signal_map) {
+       for (const autochannel : channels) {
+               for (auto& entry : signal_map) {
                        if (entry.first->channel() == channel) {
                                shared_ptr<Trace> trace = entry.second;
                                const auto list_iter = add_list.find(trace);
@@ -1305,7 +1316,7 @@ void View::determine_time_unit()
        // Check whether we know the sample rate and hence can use time as the unit
        if (time_unit_ == util::TimeUnit::Samples) {
                // Check all signals but...
-               for (const shared_ptr<Signal> signal : signals_) {
+               for (const shared_ptr<Signal>& signal : signals_) {
                        const shared_ptr<SignalData> data = signal->data();
 
                        // ...only check first segment of each
@@ -1324,14 +1335,14 @@ bool View::eventFilter(QObject *object, QEvent *event)
        const QEvent::Type type = event->type();
        if (type == QEvent::MouseMove) {
 
+               if (object)
+                       hover_widget_ = qobject_cast<QWidget*>(object);
+
                const QMouseEvent *const mouse_event = (QMouseEvent*)event;
                if (object == viewport_)
                        hover_point_ = mouse_event->pos();
                else if (object == ruler_)
-                       // Adjust the hover point's y coordinate so that it's relative to
-                       // the top of the viewport. The result may be negative.
-                       hover_point_ = QPoint(mouse_event->pos().x(),
-                               mouse_event->pos().y() - ruler_->sizeHint().height());
+                       hover_point_ = mouse_event->pos();
                else if (object == header_)
                        hover_point_ = QPoint(0, mouse_event->y());
                else
@@ -1382,7 +1393,7 @@ void View::contextMenuEvent(QContextMenuEvent *event)
 
        QMenu *menu = r->create_view_context_menu(this, pos);
        if (menu)
-               menu->exec(event->globalPos());
+               menu->popup(event->globalPos());
 }
 
 void View::resizeEvent(QResizeEvent* event)
@@ -1398,23 +1409,25 @@ void View::update_hover_point()
 {
        // Determine signal that the mouse cursor is hovering over
        signal_under_mouse_cursor_.reset();
-       for (shared_ptr<Signal> s : signals_) {
-               const pair<int, int> extents = s->v_extents();
-               const int top = s->get_visual_y() + extents.first;
-               const int btm = s->get_visual_y() + extents.second;
-               if ((hover_point_.y() >= top) && (hover_point_.y() <= btm)
-                       && s->base()->enabled())
-                       signal_under_mouse_cursor_ = s;
+       if (hover_widget_ == this) {
+               for (const shared_ptr<Signal>& s : signals_) {
+                       const pair<int, int> extents = s->v_extents();
+                       const int top = s->get_visual_y() + extents.first;
+                       const int btm = s->get_visual_y() + extents.second;
+                       if ((hover_point_.y() >= top) && (hover_point_.y() <= btm)
+                               && s->base()->enabled())
+                               signal_under_mouse_cursor_ = s;
+               }
        }
 
        // Update all trace tree items
        const vector<shared_ptr<TraceTreeItem>> trace_tree_items(
                list_by_type<TraceTreeItem>());
-       for (shared_ptr<TraceTreeItem> r : trace_tree_items)
+       for (const shared_ptr<TraceTreeItem>& r : trace_tree_items)
                r->hover_point_changed(hover_point_);
 
        // Notify any other listeners
-       hover_point_changed(hover_point_);
+       hover_point_changed(hover_widget_, hover_point_);
 }
 
 void View::row_item_appearance_changed(bool label, bool content)
@@ -1545,12 +1558,12 @@ void View::signals_changed()
        // Make a look-up table of sigrok Channels to pulseview Signals
        unordered_map<shared_ptr<data::SignalBase>, shared_ptr<Signal> >
                signal_map;
-       for (const shared_ptr<Signal> &sig : signals_)
+       for (const shared_ptr<Signal>sig : signals_)
                signal_map[sig->base()] = sig;
 
        // Populate channel groups
        if (sr_dev)
-               for (auto entry : sr_dev->channel_groups()) {
+               for (auto& entry : sr_dev->channel_groups()) {
                        const shared_ptr<sigrok::ChannelGroup> &group = entry.second;
 
                        if (group->channels().size() <= 1)
@@ -1576,7 +1589,7 @@ void View::signals_changed()
                        // Add the traces to the group
                        const pair<int, int> prev_v_extents = owner->v_extents();
                        int offset = prev_v_extents.second - prev_v_extents.first;
-                       for (shared_ptr<Trace> trace : new_traces_in_group) {
+                       for (const shared_ptr<Trace>& trace : new_traces_in_group) {
                                assert(trace);
                                owner->add_child_item(trace);
 
@@ -1611,7 +1624,7 @@ void View::signals_changed()
        if (non_grouped_logic_signals.size() > 0) {
                const shared_ptr<TraceGroup> non_grouped_trace_group(
                        make_shared<TraceGroup>());
-               for (shared_ptr<Trace> trace : non_grouped_logic_signals)
+               for (const shared_ptr<Trace>& trace : non_grouped_logic_signals)
                        non_grouped_trace_group->add_child_item(trace);
 
                non_grouped_trace_group->restack_items();
@@ -1629,7 +1642,7 @@ void View::signals_changed()
                add_traces.begin(), add_traces.end());
 
        // Remove any removed traces
-       for (shared_ptr<Trace> trace : remove_traces) {
+       for (const shared_ptr<Trace>& trace : remove_traces) {
                TraceTreeItemOwner *const owner = trace->owner();
                assert(owner);
                owner->remove_child_item(trace);
@@ -1682,6 +1695,7 @@ void View::capture_state_updated(int state)
                set_time_unit(util::TimeUnit::Samples);
 
                trigger_markers_.clear();
+               set_zero_position(0);
 
                scale_at_acq_start_ = scale_;
                offset_at_acq_start_ = offset_;