X-Git-Url: https://sigrok.org/gitweb/?p=pulseview.git;a=blobdiff_plain;f=pv%2Fview%2Fview.cpp;h=c2d97f070a83bb46c1d6b389bbd628048c5064b3;hp=6af03c9c0f7a78fec96a341c57c7e38b1d951bae;hb=d7c0ca4a965c5f9cb2ae9aea584bb2547f4baca1;hpb=03ce95a9bb81c05bae0da9de189d280214db67c3 diff --git a/pv/view/view.cpp b/pv/view/view.cpp index 6af03c9c..c2d97f07 100644 --- a/pv/view/view.cpp +++ b/pv/view/view.cpp @@ -18,17 +18,21 @@ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ +#include + #include #include #include +#include + #include #include #include #include -#include "decodesignal.h" +#include "decodetrace.h" #include "header.h" #include "ruler.h" #include "signal.h" @@ -48,7 +52,6 @@ namespace view { const double View::MaxScale = 1e9; const double View::MinScale = 1e-15; -const int View::LabelMarginWidth = 70; const int View::RulerHeight = 30; const int View::MaxScrollValue = INT_MAX / 2; @@ -91,6 +94,8 @@ View::View(SigSession &session, QWidget *parent) : connect(_cursors.second().get(), SIGNAL(time_changed()), this, SLOT(marker_time_changed())); + connect(_header, SIGNAL(geometry_updated()), + this, SLOT(on_geometry_updated())); connect(_header, SIGNAL(signals_moved()), this, SLOT(on_signals_moved())); @@ -104,7 +109,6 @@ View::View(SigSession &session, QWidget *parent) : connect(_ruler, SIGNAL(selection_changed()), this, SIGNAL(selection_changed())); - setViewportMargins(LabelMarginWidth, RulerHeight, 0, 0); setViewport(_viewport); _viewport->installEventFilter(this); @@ -139,16 +143,84 @@ int View::v_offset() const void View::zoom(double steps) { - zoom(steps, (width() - LabelMarginWidth) / 2); + zoom(steps, _viewport->width() / 2); } void View::zoom(double steps, int offset) { - const double cursor_offset = _offset + _scale * offset; const double new_scale = max(min(_scale * pow(3.0/2.0, -steps), MaxScale), MinScale); - const double new_offset = cursor_offset - new_scale * offset; - set_scale_offset(new_scale, new_offset); + set_zoom(new_scale, offset); +} + +void View::zoom_fit() +{ + using pv::data::SignalData; + + const vector< shared_ptr > sigs( + session().get_signals()); + + // Make a set of all the visible data objects + set< shared_ptr > visible_data; + BOOST_FOREACH(const shared_ptr sig, sigs) + if (sig->enabled()) + visible_data.insert(sig->data()); + + if (visible_data.empty()) + return; + + double left_time = DBL_MAX, right_time = DBL_MIN; + BOOST_FOREACH(const shared_ptr d, visible_data) + { + const double start_time = d->get_start_time(); + left_time = min(left_time, start_time); + right_time = max(right_time, start_time + + d->get_max_sample_count() / d->samplerate()); + } + + assert(left_time < right_time); + if (right_time - left_time < 1e-12) + return; + + assert(_viewport); + const int w = _viewport->width(); + if (w <= 0) + return; + + set_scale_offset((right_time - left_time) / w, left_time); +} + +void View::zoom_one_to_one() +{ + using pv::data::SignalData; + + const vector< shared_ptr > sigs( + session().get_signals()); + + // Make a set of all the visible data objects + set< shared_ptr > visible_data; + BOOST_FOREACH(const shared_ptr sig, sigs) + if (sig->enabled()) + visible_data.insert(sig->data()); + + if (visible_data.empty()) + return; + + double samplerate = 0.0; + BOOST_FOREACH(const shared_ptr d, visible_data) { + assert(d); + samplerate = max(samplerate, d->samplerate()); + } + + if (samplerate == 0.0) + return; + + assert(_viewport); + const int w = _viewport->width(); + if (w <= 0) + return; + + set_zoom(1.0 / samplerate, w / 2); } void View::set_scale_offset(double scale, double offset) @@ -166,7 +238,7 @@ vector< shared_ptr > View::get_traces() const { const vector< shared_ptr > sigs( session().get_signals()); - const vector< shared_ptr > decode_sigs( + const vector< shared_ptr > decode_sigs( session().get_decode_signals()); vector< shared_ptr > traces( sigs.size() + decode_sigs.size()); @@ -175,7 +247,7 @@ vector< shared_ptr > View::get_traces() const i = copy(sigs.begin(), sigs.end(), i); i = copy(decode_sigs.begin(), decode_sigs.end(), i); - sort(traces.begin(), traces.end(), compare_trace_v_offsets); + stable_sort(traces.begin(), traces.end(), compare_trace_v_offsets); return traces; } @@ -264,10 +336,18 @@ void View::get_scroll_layout(double &length, double &offset) const if (!sig_data) return; - length = _data_length / (sig_data->get_samplerate() * _scale); + length = _data_length / (sig_data->samplerate() * _scale); offset = _offset / _scale; } +void View::set_zoom(double scale, int offset) +{ + const double cursor_offset = _offset + _scale * offset; + const double new_scale = max(min(scale, MaxScale), MinScale); + const double new_offset = cursor_offset - new_scale * offset; + set_scale_offset(new_scale, new_offset); +} + void View::update_scroll() { assert(_viewport); @@ -301,6 +381,16 @@ void View::update_scroll() areaSize.height()); } +void View::update_layout() +{ + setViewportMargins(_header->sizeHint().width(), RulerHeight, 0, 0); + _ruler->setGeometry(_viewport->x(), 0, + _viewport->width(), _viewport->y()); + _header->setGeometry(0, _viewport->y(), + _viewport->x(), _viewport->height()); + update_scroll(); +} + bool View::compare_trace_v_offsets(const shared_ptr &a, const shared_ptr &b) { @@ -352,11 +442,7 @@ bool View::viewportEvent(QEvent *e) void View::resizeEvent(QResizeEvent*) { - _ruler->setGeometry(_viewport->x(), 0, - _viewport->width(), _viewport->y()); - _header->setGeometry(0, _viewport->y(), - _viewport->x(), _viewport->height()); - update_scroll(); + update_layout(); } void View::h_scroll_value_changed(int value) @@ -394,6 +480,7 @@ void View::signals_changed() offset += SignalHeight + 2 * SignalMargin; } + setViewportMargins(_header->sizeHint().width(), RulerHeight, 0, 0); normalize_layout(); } @@ -430,5 +517,10 @@ void View::on_signals_moved() signals_moved(); } +void View::on_geometry_updated() +{ + update_layout(); +} + } // namespace view } // namespace pv