X-Git-Url: https://sigrok.org/gitweb/?p=pulseview.git;a=blobdiff_plain;f=pv%2Fviews%2Ftrace%2Fview.cpp;h=a470715b48595d94ec89eff74a3f12ddd34a3a52;hp=40ecc195cea44e9cd5d204b525c2386c8c618baa;hb=f9a0fd83226d97af7458d8c9dac0b88c83a54d29;hpb=e887fe9e6ede73b9fe9ace9c6bfd4f6a096eb3b2 diff --git a/pv/views/trace/view.cpp b/pv/views/trace/view.cpp index 40ecc195..a470715b 100644 --- a/pv/views/trace/view.cpp +++ b/pv/views/trace/view.cpp @@ -29,7 +29,6 @@ #include #include #include -#include #include #include @@ -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" @@ -72,6 +72,7 @@ using std::back_inserter; using std::copy_if; using std::count_if; using std::inserter; +using std::lock_guard; using std::max; using std::make_pair; using std::make_shared; @@ -81,8 +82,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 { @@ -94,8 +93,10 @@ const Timestamp View::MinScale("1e-12"); const int View::MaxScrollValue = INT_MAX / 2; -const int View::ScaleUnits[3] = {1, 2, 5}; +/* Area at the top and bottom of the view that can't be scrolled out of sight */ +const int View::ViewScrollMargin = 50; +const int View::ScaleUnits[3] = {1, 2, 5}; CustomScrollArea::CustomScrollArea(QWidget *parent) : QAbstractScrollArea(parent) @@ -180,6 +181,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)), @@ -201,6 +209,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, @@ -315,7 +324,7 @@ const Session& View::session() const return session_; } -unordered_set< shared_ptr > View::signals() const +vector< shared_ptr > View::signals() const { return signals_; } @@ -333,24 +342,60 @@ shared_ptr View::get_signal_by_signalbase(shared_ptr b return ret_val; } -void View::clear_signals() +void View::clear_signalbases() { - ViewBase::clear_signals(); + ViewBase::clear_signalbases(); signals_.clear(); } -void View::add_signal(const shared_ptr signal) +void View::add_signalbase(const shared_ptr signalbase) { - ViewBase::add_signalbase(signal->base()); - signals_.insert(signal); + ViewBase::add_signalbase(signalbase); + + shared_ptr signal; + + switch (signalbase->type()) { + case SignalBase::LogicChannel: + signal = shared_ptr(new LogicSignal(session_, session_.device(), signalbase)); + break; + + case SignalBase::AnalogChannel: + signal = shared_ptr(new AnalogSignal(session_, signalbase)); + break; + + default: + qDebug() << "Unknown signalbase type:" << signalbase->type(); + assert(false); + break; + } + + signals_.push_back(signal); signal->set_segment_display_mode(segment_display_mode_); signal->set_current_segment(current_segment_); + // Secondary views use the signal's settings in the main view + if (!is_main_view()) { + shared_ptr main_tv = dynamic_pointer_cast(session_.main_view()); + shared_ptr main_signal = main_tv->get_signal_by_signalbase(signalbase); + if (main_signal) + signal->restore_settings(main_signal->save_settings()); + } + connect(signal->base().get(), SIGNAL(name_changed(const QString&)), this, SLOT(on_signal_name_changed())); } +void View::remove_signalbase(const shared_ptr signalbase) +{ + ViewBase::remove_signalbase(signalbase); + + shared_ptr signal = get_signal_by_signalbase(signalbase); + + if (signal) + remove_trace(signal); +} + #ifdef ENABLE_DECODE void View::clear_decode_signals() { @@ -378,14 +423,34 @@ void View::remove_decode_signal(shared_ptr signal) for (auto i = decode_traces_.begin(); i != decode_traces_.end(); i++) if ((*i)->base() == signal) { decode_traces_.erase(i); - signals_changed(); - return; + break; } ViewBase::remove_decode_signal(signal); } #endif +void View::remove_trace(shared_ptr trace) +{ + TraceTreeItemOwner *const owner = trace->owner(); + assert(owner); + owner->remove_child_item(trace); + + for (auto i = signals_.begin(); i != signals_.end(); i++) + if ((*i) == trace) { + signals_.erase(i); + break; + } + + if (!header_was_shrunk_) + resize_header_to_fit(); + + update_layout(); + + header_->update(); + viewport_->update(); +} + shared_ptr View::get_signal_under_mouse_cursor() const { return signal_under_mouse_cursor_; @@ -522,6 +587,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 end_sample = (offset_ * samplerate).convert_to() + + (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(); } } @@ -780,6 +861,26 @@ void View::zoom_fit(bool gui_state) set_scale_offset(scale.convert_to(), 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(), offset); +} + void View::set_scale_offset(double scale, const Timestamp& offset) { // Disable sticky scrolling / always zoom to fit when acquisition runs @@ -808,13 +909,13 @@ void View::set_scale_offset(double scale, const Timestamp& offset) viewport_->update(); } -set< shared_ptr > View::get_visible_data() const +vector< shared_ptr > View::get_visible_data() const { // Make a set of all the visible data objects - set< shared_ptr > visible_data; + vector< shared_ptr > visible_data; for (const shared_ptr& sig : signals_) if (sig->enabled()) - visible_data.insert(sig->data()); + visible_data.push_back(sig->data()); return visible_data; } @@ -822,8 +923,14 @@ set< shared_ptr > View::get_visible_data() const pair View::get_time_extents() const { boost::optional left_time, right_time; - const set< shared_ptr > visible_data = get_visible_data(); - for (const shared_ptr& d : visible_data) { + + vector< shared_ptr > data; + if (signals_.size() == 0) + return make_pair(0, 0); + + data.push_back(signals_.front()->data()); + + for (const shared_ptr& d : data) { const vector< shared_ptr > segments = d->segments(); for (const shared_ptr& s : segments) { double samplerate = s->samplerate(); @@ -842,7 +949,7 @@ pair View::get_time_extents() const if (!left_time || !right_time) return make_pair(0, 0); - assert(*left_time < *right_time); + assert(*left_time <= *right_time); return make_pair(*left_time, *right_time); } @@ -1245,9 +1352,13 @@ void View::update_scroll() const pair extents = v_extents(); // Don't change the scrollbar range if there are no traces - if (extents.first != extents.second) - vscrollbar->setRange(extents.first - areaSize.height(), - extents.second); + if (extents.first != extents.second) { + int top_margin = ViewScrollMargin; + int btm_margin = ViewScrollMargin; + + vscrollbar->setRange(extents.first - areaSize.height() + top_margin, + extents.second - btm_margin); + } if (scroll_needs_defaults_) { set_scroll_default(); @@ -1318,12 +1429,11 @@ void View::update_layout() TraceTreeItemOwner* View::find_prevalent_trace_group( const shared_ptr &group, - const unordered_map, shared_ptr > - &signal_map) + const map, shared_ptr > &signal_map) { assert(group); - unordered_set owners; + set owners; vector owner_list; // Make a set and a list of all the owners @@ -1355,8 +1465,7 @@ TraceTreeItemOwner* View::find_prevalent_trace_group( vector< shared_ptr > View::extract_new_traces_for_channels( const vector< shared_ptr > &channels, - const unordered_map, shared_ptr > - &signal_map, + const map, shared_ptr > &signal_map, set< shared_ptr > &add_list) { vector< shared_ptr > filtered_traces; @@ -1516,8 +1625,19 @@ void View::update_hover_point() for (const shared_ptr& 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(); + + 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) @@ -1547,8 +1667,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() @@ -1642,6 +1762,8 @@ void View::signals_changed() { using sigrok::Channel; + lock_guard lock(signal_mutex_); + vector< shared_ptr > channels; shared_ptr sr_dev; bool signals_added_or_removed = false; @@ -1664,7 +1786,9 @@ void View::signals_changed() vector< shared_ptr > new_top_level_items; // Make a list of traces that are being added, and a list of traces - // that are being removed + // that are being removed. The set_difference() algorithms require + // both sets to be in the exact same order, which means that PD signals + // must always be last as they interrupt the sort order otherwise const vector> prev_trace_list = list_by_type(); const set> prev_traces( prev_trace_list.begin(), prev_trace_list.end()); @@ -1674,7 +1798,6 @@ void View::signals_changed() #ifdef ENABLE_DECODE traces.insert(decode_traces_.begin(), decode_traces_.end()); #endif - set< shared_ptr > add_traces; set_difference(traces.begin(), traces.end(), prev_traces.begin(), prev_traces.end(), @@ -1686,8 +1809,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 > - signal_map; + map, shared_ptr > signal_map; for (const shared_ptr& sig : signals_) signal_map[sig->base()] = sig; @@ -1788,7 +1910,11 @@ 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 item : new_top_level_items) { + // items may already have gained an owner when they were added to a group above + if (item->owner()) + continue; + add_child_item(item); // Position the item after the last item or at the top if there is none @@ -1909,6 +2035,8 @@ void View::on_segment_changed(int segment) void View::on_settingViewTriggerIsZeroTime_changed(const QVariant new_value) { + (void)new_value; + if (!custom_zero_offset_set_) reset_zero_position(); }