2 * This file is part of the PulseView project.
4 * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
21 #include <libsigrokdecode/libsigrokdecode.h>
32 #include <unordered_set>
34 #include <boost/archive/text_iarchive.hpp>
35 #include <boost/archive/text_oarchive.hpp>
36 #include <boost/serialization/serialization.hpp>
38 #include <QApplication>
39 #include <QHBoxLayout>
41 #include <QFontMetrics>
42 #include <QMouseEvent>
45 #include <libsigrokcxx/libsigrokcxx.hpp>
47 #include "analogsignal.hpp"
49 #include "logicsignal.hpp"
52 #include "tracegroup.hpp"
53 #include "triggermarker.hpp"
55 #include "viewport.hpp"
57 #include "pv/session.hpp"
58 #include "pv/globalsettings.hpp"
59 #include "pv/devices/device.hpp"
60 #include "pv/data/logic.hpp"
61 #include "pv/data/logicsegment.hpp"
62 #include "pv/util.hpp"
65 #include "decodetrace.hpp"
69 using pv::data::SignalData;
70 using pv::data::Segment;
71 using pv::util::TimeUnit;
72 using pv::util::Timestamp;
74 using std::back_inserter;
77 using std::dynamic_pointer_cast;
81 using std::make_shared;
85 using std::set_difference;
86 using std::shared_ptr;
87 using std::stringstream;
88 using std::unordered_map;
89 using std::unordered_set;
96 const Timestamp View::MaxScale("1e9");
97 const Timestamp View::MinScale("1e-12");
99 const int View::MaxScrollValue = INT_MAX / 2;
100 const int View::MaxViewAutoUpdateRate = 25; // No more than 25 Hz with sticky scrolling
102 const int View::ScaleUnits[3] = {1, 2, 5};
105 CustomAbstractScrollArea::CustomAbstractScrollArea(QWidget *parent) :
106 QAbstractScrollArea(parent)
110 void CustomAbstractScrollArea::setViewportMargins(int left, int top, int right, int bottom)
112 QAbstractScrollArea::setViewportMargins(left, top, right, bottom);
115 bool CustomAbstractScrollArea::viewportEvent(QEvent *event)
117 switch (event->type()) {
119 case QEvent::MouseButtonPress:
120 case QEvent::MouseButtonRelease:
121 case QEvent::MouseButtonDblClick:
122 case QEvent::MouseMove:
124 case QEvent::TouchBegin:
125 case QEvent::TouchUpdate:
126 case QEvent::TouchEnd:
129 return QAbstractScrollArea::viewportEvent(event);
133 View::View(Session &session, bool is_main_view, QWidget *parent) :
134 ViewBase(session, is_main_view, parent),
135 viewport_(new Viewport(*this)),
136 ruler_(new Ruler(*this)),
137 header_(new Header(*this)),
141 updating_scroll_(false),
142 sticky_scrolling_(false), // Default setting is set in MainWindow::setup_ui()
143 always_zoom_to_fit_(false),
145 tick_prefix_(pv::util::SIPrefix::yocto),
147 time_unit_(util::TimeUnit::Time),
148 show_cursors_(false),
149 cursors_(new CursorPair(*this)),
150 next_flag_text_('A'),
152 hover_point_(-1, -1),
153 scroll_needs_defaults_(false),
155 size_finalized_(false)
157 GlobalSettings settings;
158 coloured_bg_ = settings.value(GlobalSettings::Key_View_ColouredBG).toBool();
160 connect(scrollarea_.horizontalScrollBar(), SIGNAL(valueChanged(int)),
161 this, SLOT(h_scroll_value_changed(int)));
162 connect(scrollarea_.verticalScrollBar(), SIGNAL(valueChanged(int)),
163 this, SLOT(v_scroll_value_changed()));
165 connect(header_, SIGNAL(selection_changed()),
166 ruler_, SLOT(clear_selection()));
167 connect(ruler_, SIGNAL(selection_changed()),
168 header_, SLOT(clear_selection()));
170 connect(header_, SIGNAL(selection_changed()),
171 this, SIGNAL(selection_changed()));
172 connect(ruler_, SIGNAL(selection_changed()),
173 this, SIGNAL(selection_changed()));
175 connect(this, SIGNAL(hover_point_changed()),
176 this, SLOT(on_hover_point_changed()));
178 connect(&lazy_event_handler_, SIGNAL(timeout()),
179 this, SLOT(process_sticky_events()));
180 lazy_event_handler_.setSingleShot(true);
182 connect(&delayed_view_updater_, SIGNAL(timeout()),
183 this, SLOT(perform_delayed_view_update()));
184 delayed_view_updater_.setSingleShot(true);
185 delayed_view_updater_.setInterval(1000 / MaxViewAutoUpdateRate);
187 /* To let the scroll area fill up the parent QWidget (this), we need a layout */
188 QHBoxLayout *layout = new QHBoxLayout(this);
190 layout->setContentsMargins(0, 0, 0, 0);
191 layout->addWidget(&scrollarea_);
193 scrollarea_.setViewport(viewport_);
195 viewport_->installEventFilter(this);
196 ruler_->installEventFilter(this);
197 header_->installEventFilter(this);
199 // Trigger the initial event manually. The default device has signals
200 // which were created before this object came into being
203 // make sure the transparent widgets are on the top
207 // Update the zoom state
208 calculate_tick_spacing();
211 Session& View::session()
216 const Session& View::session() const
221 unordered_set< shared_ptr<Signal> > View::signals() const
226 void View::clear_signals()
231 void View::add_signal(const shared_ptr<Signal> signal)
233 signals_.insert(signal);
237 void View::clear_decode_signals()
239 decode_traces_.clear();
242 void View::add_decode_signal(shared_ptr<data::SignalBase> signalbase)
244 shared_ptr<DecodeTrace> d(
245 new DecodeTrace(session_, signalbase, decode_traces_.size()));
246 decode_traces_.push_back(d);
249 void View::remove_decode_signal(shared_ptr<data::SignalBase> signalbase)
251 for (auto i = decode_traces_.begin(); i != decode_traces_.end(); i++)
252 if ((*i)->base() == signalbase) {
253 decode_traces_.erase(i);
265 const View* View::view() const
270 Viewport* View::viewport()
275 const Viewport* View::viewport() const
280 void View::save_settings(QSettings &settings) const
282 settings.setValue("scale", scale_);
283 settings.setValue("v_offset",
284 scrollarea_.verticalScrollBar()->sliderPosition());
287 boost::archive::text_oarchive oa(ss);
288 oa << boost::serialization::make_nvp("offset", offset_);
289 settings.setValue("offset", QString::fromStdString(ss.str()));
291 for (shared_ptr<Signal> signal : signals_) {
292 settings.beginGroup(signal->base()->internal_name());
293 signal->save_settings(settings);
298 void View::restore_settings(QSettings &settings)
300 // Note: It is assumed that this function is only called once,
301 // immediately after restoring a previous session.
303 if (settings.contains("scale"))
304 set_scale(settings.value("scale").toDouble());
306 if (settings.contains("offset")) {
307 util::Timestamp offset;
309 ss << settings.value("offset").toString().toStdString();
311 boost::archive::text_iarchive ia(ss);
312 ia >> boost::serialization::make_nvp("offset", offset);
317 for (shared_ptr<Signal> signal : signals_) {
318 settings.beginGroup(signal->base()->internal_name());
319 signal->restore_settings(settings);
323 if (settings.contains("v_offset")) {
324 saved_v_offset_ = settings.value("v_offset").toInt();
325 set_v_offset(saved_v_offset_);
326 scroll_needs_defaults_ = false;
327 // Note: see resizeEvent() for additional information
331 vector< shared_ptr<TimeItem> > View::time_items() const
333 const vector<shared_ptr<Flag>> f(flags());
334 vector<shared_ptr<TimeItem>> items(f.begin(), f.end());
335 items.push_back(cursors_);
336 items.push_back(cursors_->first());
337 items.push_back(cursors_->second());
339 for (auto trigger_marker : trigger_markers_)
340 items.push_back(trigger_marker);
345 double View::scale() const
350 void View::set_scale(double scale)
352 if (scale_ != scale) {
354 Q_EMIT scale_changed();
358 const Timestamp& View::offset() const
363 void View::set_offset(const pv::util::Timestamp& offset)
365 if (offset_ != offset) {
367 Q_EMIT offset_changed();
371 int View::owner_visual_v_offset() const
373 return -scrollarea_.verticalScrollBar()->sliderPosition();
376 void View::set_v_offset(int offset)
378 scrollarea_.verticalScrollBar()->setSliderPosition(offset);
383 unsigned int View::depth() const
388 pv::util::SIPrefix View::tick_prefix() const
393 void View::set_tick_prefix(pv::util::SIPrefix tick_prefix)
395 if (tick_prefix_ != tick_prefix) {
396 tick_prefix_ = tick_prefix;
397 Q_EMIT tick_prefix_changed();
401 unsigned int View::tick_precision() const
403 return tick_precision_;
406 void View::set_tick_precision(unsigned tick_precision)
408 if (tick_precision_ != tick_precision) {
409 tick_precision_ = tick_precision;
410 Q_EMIT tick_precision_changed();
414 const pv::util::Timestamp& View::tick_period() const
419 void View::set_tick_period(const pv::util::Timestamp& tick_period)
421 if (tick_period_ != tick_period) {
422 tick_period_ = tick_period;
423 Q_EMIT tick_period_changed();
427 TimeUnit View::time_unit() const
432 void View::set_time_unit(pv::util::TimeUnit time_unit)
434 if (time_unit_ != time_unit) {
435 time_unit_ = time_unit;
436 Q_EMIT time_unit_changed();
440 void View::zoom(double steps)
442 zoom(steps, viewport_->width() / 2);
445 void View::zoom(double steps, int offset)
447 set_zoom(scale_ * pow(3.0/2.0, -steps), offset);
450 void View::zoom_fit(bool gui_state)
452 // Act as one-shot when stopped, toggle along with the GUI otherwise
453 if (session_.get_capture_state() == Session::Stopped) {
454 always_zoom_to_fit_ = false;
455 always_zoom_to_fit_changed(false);
457 always_zoom_to_fit_ = gui_state;
458 always_zoom_to_fit_changed(gui_state);
461 const pair<Timestamp, Timestamp> extents = get_time_extents();
462 const Timestamp delta = extents.second - extents.first;
463 if (delta < Timestamp("1e-12"))
467 const int w = viewport_->width();
471 const Timestamp scale = max(min(delta / w, MaxScale), MinScale);
472 set_scale_offset(scale.convert_to<double>(), extents.first);
475 void View::zoom_one_to_one()
477 using pv::data::SignalData;
479 // Make a set of all the visible data objects
480 set< shared_ptr<SignalData> > visible_data = get_visible_data();
481 if (visible_data.empty())
485 const int w = viewport_->width();
489 set_zoom(1.0 / session_.get_samplerate(), w / 2);
492 void View::set_scale_offset(double scale, const Timestamp& offset)
494 // Disable sticky scrolling / always zoom to fit when acquisition runs
495 // and user drags the viewport
496 if ((scale_ == scale) && (offset_ != offset) &&
497 (session_.get_capture_state() == Session::Running)) {
499 if (sticky_scrolling_) {
500 sticky_scrolling_ = false;
501 sticky_scrolling_changed(false);
504 if (always_zoom_to_fit_) {
505 always_zoom_to_fit_ = false;
506 always_zoom_to_fit_changed(false);
513 calculate_tick_spacing();
520 set< shared_ptr<SignalData> > View::get_visible_data() const
522 // Make a set of all the visible data objects
523 set< shared_ptr<SignalData> > visible_data;
524 for (const shared_ptr<Signal> sig : signals_)
526 visible_data.insert(sig->data());
531 pair<Timestamp, Timestamp> View::get_time_extents() const
533 boost::optional<Timestamp> left_time, right_time;
534 const set< shared_ptr<SignalData> > visible_data = get_visible_data();
535 for (const shared_ptr<SignalData> d : visible_data) {
536 const vector< shared_ptr<Segment> > segments =
538 for (const shared_ptr<Segment> &s : segments) {
539 double samplerate = s->samplerate();
540 samplerate = (samplerate <= 0.0) ? 1.0 : samplerate;
542 const Timestamp start_time = s->start_time();
543 left_time = left_time ?
544 min(*left_time, start_time) :
546 right_time = right_time ?
547 max(*right_time, start_time + d->max_sample_count() / samplerate) :
548 start_time + d->max_sample_count() / samplerate;
552 if (!left_time || !right_time)
553 return make_pair(0, 0);
555 assert(*left_time < *right_time);
556 return make_pair(*left_time, *right_time);
559 void View::enable_show_sampling_points(bool state)
566 void View::enable_coloured_bg(bool state)
568 const vector<shared_ptr<TraceTreeItem>> items(
569 list_by_type<TraceTreeItem>());
571 for (shared_ptr<TraceTreeItem> i : items) {
572 // Can't cast to Trace because it's abstract, so we need to
573 // check for any derived classes individually
575 shared_ptr<AnalogSignal> a = dynamic_pointer_cast<AnalogSignal>(i);
577 a->set_coloured_bg(state);
579 shared_ptr<LogicSignal> l = dynamic_pointer_cast<LogicSignal>(i);
581 l->set_coloured_bg(state);
584 shared_ptr<DecodeTrace> d = dynamic_pointer_cast<DecodeTrace>(i);
586 d->set_coloured_bg(state);
593 bool View::cursors_shown() const
595 return show_cursors_;
598 void View::show_cursors(bool show)
600 show_cursors_ = show;
605 void View::centre_cursors()
607 const double time_width = scale_ * viewport_->width();
608 cursors_->first()->set_time(offset_ + time_width * 0.4);
609 cursors_->second()->set_time(offset_ + time_width * 0.6);
614 shared_ptr<CursorPair> View::cursors() const
619 void View::add_flag(const Timestamp& time)
621 flags_.push_back(make_shared<Flag>(*this, time,
622 QString("%1").arg(next_flag_text_)));
624 next_flag_text_ = (next_flag_text_ >= 'Z') ? 'A' :
625 (next_flag_text_ + 1);
627 time_item_appearance_changed(true, true);
630 void View::remove_flag(shared_ptr<Flag> flag)
633 time_item_appearance_changed(true, true);
636 vector< shared_ptr<Flag> > View::flags() const
638 vector< shared_ptr<Flag> > flags(flags_.begin(), flags_.end());
639 stable_sort(flags.begin(), flags.end(),
640 [](const shared_ptr<Flag> &a, const shared_ptr<Flag> &b) {
641 return a->time() < b->time();
647 const QPoint& View::hover_point() const
652 void View::restack_all_trace_tree_items()
654 // Make a list of owners that is sorted from deepest first
655 const vector<shared_ptr<TraceTreeItem>> items(
656 list_by_type<TraceTreeItem>());
657 set< TraceTreeItemOwner* > owners;
658 for (const auto &r : items)
659 owners.insert(r->owner());
660 vector< TraceTreeItemOwner* > sorted_owners(owners.begin(), owners.end());
661 sort(sorted_owners.begin(), sorted_owners.end(),
662 [](const TraceTreeItemOwner* a, const TraceTreeItemOwner *b) {
663 return a->depth() > b->depth(); });
665 // Restack the items recursively
666 for (auto &o : sorted_owners)
669 // Re-assign background colors
670 bool next_bgcolour_state = false;
672 for (auto &o : sorted_owners)
673 next_bgcolour_state = o->reassign_bgcolour_states(next_bgcolour_state);
675 // Animate the items to their destination
676 for (const auto &i : items)
677 i->animate_to_layout_v_offset();
680 void View::trigger_event(util::Timestamp location)
682 trigger_markers_.push_back(make_shared<TriggerMarker>(*this, location));
685 void View::get_scroll_layout(double &length, Timestamp &offset) const
687 const pair<Timestamp, Timestamp> extents = get_time_extents();
688 length = ((extents.second - extents.first) / scale_).convert_to<double>();
689 offset = offset_ / scale_;
692 void View::set_zoom(double scale, int offset)
694 // Reset the "always zoom to fit" feature as the user changed the zoom
695 always_zoom_to_fit_ = false;
696 always_zoom_to_fit_changed(false);
698 const Timestamp cursor_offset = offset_ + scale_ * offset;
699 const Timestamp new_scale = max(min(Timestamp(scale), MaxScale), MinScale);
700 const Timestamp new_offset = cursor_offset - new_scale * offset;
701 set_scale_offset(new_scale.convert_to<double>(), new_offset);
704 void View::calculate_tick_spacing()
706 const double SpacingIncrement = 10.0f;
707 const double MinValueSpacing = 40.0f;
709 // Figure out the highest numeric value visible on a label
710 const QSize areaSize = viewport_->size();
711 const Timestamp max_time = max(fabs(offset_),
712 fabs(offset_ + scale_ * areaSize.width()));
714 double min_width = SpacingIncrement;
715 double label_width, tick_period_width;
717 QFontMetrics m(QApplication::font());
719 // Copies of the member variables with the same name, used in the calculation
720 // and written back afterwards, so that we don't emit signals all the time
721 // during the calculation.
722 pv::util::Timestamp tick_period = tick_period_;
723 pv::util::SIPrefix tick_prefix = tick_prefix_;
724 unsigned tick_precision = tick_precision_;
727 const double min_period = scale_ * min_width;
729 const int order = (int)floorf(log10f(min_period));
730 const pv::util::Timestamp order_decimal =
731 pow(pv::util::Timestamp(10), order);
733 // Allow for a margin of error so that a scale unit of 1 can be used.
734 // Otherwise, for a SU of 1 the tick period will almost always be below
735 // the min_period by a small amount - and thus skipped in favor of 2.
736 // Note: margin assumes that SU[0] and SU[1] contain the smallest values
737 double tp_margin = (ScaleUnits[0] + ScaleUnits[1]) / 2.0;
738 double tp_with_margin;
739 unsigned int unit = 0;
742 tp_with_margin = order_decimal.convert_to<double>() *
743 (ScaleUnits[unit++] + tp_margin);
744 } while (tp_with_margin < min_period && unit < countof(ScaleUnits));
746 tick_period = order_decimal * ScaleUnits[unit - 1];
747 tick_prefix = static_cast<pv::util::SIPrefix>(
748 (order - pv::util::exponent(pv::util::SIPrefix::yocto)) / 3);
750 // Precision is the number of fractional digits required, not
751 // taking the prefix into account (and it must never be negative)
752 tick_precision = max(ceil(log10(1 / tick_period)).convert_to<int>(), 0);
754 tick_period_width = (tick_period / scale_).convert_to<double>();
756 const QString label_text = Ruler::format_time_with_distance(
757 tick_period, max_time, tick_prefix, time_unit_, tick_precision);
759 label_width = m.boundingRect(0, 0, INT_MAX, INT_MAX,
760 Qt::AlignLeft | Qt::AlignTop, label_text).width() +
763 min_width += SpacingIncrement;
764 } while (tick_period_width < label_width);
766 set_tick_period(tick_period);
767 set_tick_prefix(tick_prefix);
768 set_tick_precision(tick_precision);
771 void View::update_scroll()
774 QScrollBar *hscrollbar = scrollarea_.horizontalScrollBar();
775 QScrollBar *vscrollbar = scrollarea_.verticalScrollBar();
777 const QSize areaSize = viewport_->size();
779 // Set the horizontal scroll bar
782 get_scroll_layout(length, offset);
783 length = max(length - areaSize.width(), 0.0);
785 int major_tick_distance = (tick_period_ / scale_).convert_to<int>();
787 hscrollbar->setPageStep(areaSize.width() / 2);
788 hscrollbar->setSingleStep(major_tick_distance);
790 updating_scroll_ = true;
792 if (length < MaxScrollValue) {
793 hscrollbar->setRange(0, length);
794 hscrollbar->setSliderPosition(offset.convert_to<double>());
796 hscrollbar->setRange(0, MaxScrollValue);
797 hscrollbar->setSliderPosition(
798 (offset_ * MaxScrollValue / (scale_ * length)).convert_to<double>());
801 updating_scroll_ = false;
803 // Set the vertical scrollbar
804 vscrollbar->setPageStep(areaSize.height());
805 vscrollbar->setSingleStep(areaSize.height() / 8);
807 const pair<int, int> extents = v_extents();
809 // Don't change the scrollbar range if there are no traces
810 if (extents.first != extents.second)
811 vscrollbar->setRange(extents.first - areaSize.height(),
814 if (scroll_needs_defaults_)
815 set_scroll_default();
818 void View::reset_scroll()
820 scrollarea_.verticalScrollBar()->setRange(0, 0);
823 void View::set_scroll_default()
827 const QSize areaSize = viewport_->size();
829 const pair<int, int> extents = v_extents();
830 const int trace_height = extents.second - extents.first;
832 // Do all traces fit in the view?
833 if (areaSize.height() >= trace_height)
834 // Center all traces vertically
835 set_v_offset(extents.first -
836 ((areaSize.height() - trace_height) / 2));
838 // Put the first trace at the top, letting the bottom ones overflow
839 set_v_offset(extents.first);
841 // If we're not sure whether setting the defaults worked as
842 // the window wasn't set up entirely yet, we want to be called
843 // again later to make sure
844 scroll_needs_defaults_ = !size_finalized_;
847 void View::update_layout()
849 scrollarea_.setViewportMargins(
850 header_->sizeHint().width() - Header::BaselineOffset,
851 ruler_->sizeHint().height(), 0, 0);
852 ruler_->setGeometry(viewport_->x(), 0,
853 viewport_->width(), ruler_->extended_size_hint().height());
854 header_->setGeometry(0, viewport_->y(),
855 header_->extended_size_hint().width(), viewport_->height());
859 TraceTreeItemOwner* View::find_prevalent_trace_group(
860 const shared_ptr<sigrok::ChannelGroup> &group,
861 const unordered_map<shared_ptr<data::SignalBase>, shared_ptr<Signal> >
866 unordered_set<TraceTreeItemOwner*> owners;
867 vector<TraceTreeItemOwner*> owner_list;
869 // Make a set and a list of all the owners
870 for (const auto &channel : group->channels()) {
871 for (auto entry : signal_map) {
872 if (entry.first->channel() == channel) {
873 TraceTreeItemOwner *const o = (entry.second)->owner();
874 owner_list.push_back(o);
880 // Iterate through the list of owners, and find the most prevalent
881 size_t max_prevalence = 0;
882 TraceTreeItemOwner *prevalent_owner = nullptr;
883 for (TraceTreeItemOwner *owner : owners) {
884 const size_t prevalence = count_if(
885 owner_list.begin(), owner_list.end(),
886 [&](TraceTreeItemOwner *o) { return o == owner; });
887 if (prevalence > max_prevalence) {
888 max_prevalence = prevalence;
889 prevalent_owner = owner;
893 return prevalent_owner;
896 vector< shared_ptr<Trace> > View::extract_new_traces_for_channels(
897 const vector< shared_ptr<sigrok::Channel> > &channels,
898 const unordered_map<shared_ptr<data::SignalBase>, shared_ptr<Signal> >
900 set< shared_ptr<Trace> > &add_list)
902 vector< shared_ptr<Trace> > filtered_traces;
904 for (const auto &channel : channels) {
905 for (auto entry : signal_map) {
906 if (entry.first->channel() == channel) {
907 shared_ptr<Trace> trace = entry.second;
908 const auto list_iter = add_list.find(trace);
909 if (list_iter == add_list.end())
912 filtered_traces.push_back(trace);
913 add_list.erase(list_iter);
918 return filtered_traces;
921 void View::determine_time_unit()
923 // Check whether we know the sample rate and hence can use time as the unit
924 if (time_unit_ == util::TimeUnit::Samples) {
925 // Check all signals but...
926 for (const shared_ptr<Signal> signal : signals_) {
927 const shared_ptr<SignalData> data = signal->data();
929 // ...only check first segment of each
930 const vector< shared_ptr<Segment> > segments = data->segments();
931 if (!segments.empty())
932 if (segments[0]->samplerate()) {
933 set_time_unit(util::TimeUnit::Time);
940 bool View::eventFilter(QObject *object, QEvent *event)
942 const QEvent::Type type = event->type();
943 if (type == QEvent::MouseMove) {
945 const QMouseEvent *const mouse_event = (QMouseEvent*)event;
946 if (object == viewport_)
947 hover_point_ = mouse_event->pos();
948 else if (object == ruler_)
949 hover_point_ = QPoint(mouse_event->x(), 0);
950 else if (object == header_)
951 hover_point_ = QPoint(0, mouse_event->y());
953 hover_point_ = QPoint(-1, -1);
955 hover_point_changed();
957 } else if (type == QEvent::Leave) {
958 hover_point_ = QPoint(-1, -1);
959 hover_point_changed();
962 return QObject::eventFilter(object, event);
965 void View::resizeEvent(QResizeEvent*)
969 // This is somewhat of a hack, unfortunately. We cannot use
970 // set_v_offset() from within restore_settings() as the view
971 // at that point is neither visible nor properly sized.
972 // This is the least intrusive workaround I could come up
973 // with: set the vertical offset (or scroll defaults) when
974 // the view is visible and resized to its final size.
975 // Resize events that are sent when the view is not visible
976 // must be ignored as they have wrong sizes, potentially
977 // preventing the v offset from being set successfully.
980 size_finalized_ = true;
982 if (size_finalized_ && saved_v_offset_) {
983 set_v_offset(saved_v_offset_);
987 if (size_finalized_ && scroll_needs_defaults_)
988 set_scroll_default();
991 void View::row_item_appearance_changed(bool label, bool content)
999 void View::time_item_appearance_changed(bool label, bool content)
1004 viewport_->update();
1007 void View::extents_changed(bool horz, bool vert)
1010 (horz ? TraceTreeItemHExtentsChanged : 0) |
1011 (vert ? TraceTreeItemVExtentsChanged : 0);
1012 lazy_event_handler_.start();
1015 void View::h_scroll_value_changed(int value)
1017 if (updating_scroll_)
1020 // Disable sticky scrolling when user moves the horizontal scroll bar
1021 // during a running acquisition
1022 if (sticky_scrolling_ && (session_.get_capture_state() == Session::Running)) {
1023 sticky_scrolling_ = false;
1024 sticky_scrolling_changed(false);
1027 const int range = scrollarea_.horizontalScrollBar()->maximum();
1028 if (range < MaxScrollValue)
1029 set_offset(scale_ * value);
1033 get_scroll_layout(length, offset);
1034 set_offset(scale_ * length * value / MaxScrollValue);
1038 viewport_->update();
1041 void View::v_scroll_value_changed()
1044 viewport_->update();
1047 void View::signals_changed()
1049 using sigrok::Channel;
1051 vector< shared_ptr<Channel> > channels;
1052 shared_ptr<sigrok::Device> sr_dev;
1054 // Do we need to set the vertical scrollbar to its default position later?
1055 // We do if there are no traces, i.e. the scroll bar has no range set
1056 bool reset_scrollbar =
1057 (scrollarea_.verticalScrollBar()->minimum() ==
1058 scrollarea_.verticalScrollBar()->maximum());
1060 if (!session_.device()) {
1064 sr_dev = session_.device()->device();
1066 channels = sr_dev->channels();
1069 vector< shared_ptr<TraceTreeItem> > new_top_level_items;
1071 // Make a list of traces that are being added, and a list of traces
1072 // that are being removed
1073 const vector<shared_ptr<Trace>> prev_trace_list = list_by_type<Trace>();
1074 const set<shared_ptr<Trace>> prev_traces(
1075 prev_trace_list.begin(), prev_trace_list.end());
1077 set< shared_ptr<Trace> > traces(signals_.begin(), signals_.end());
1079 #ifdef ENABLE_DECODE
1080 traces.insert(decode_traces_.begin(), decode_traces_.end());
1083 set< shared_ptr<Trace> > add_traces;
1084 set_difference(traces.begin(), traces.end(),
1085 prev_traces.begin(), prev_traces.end(),
1086 inserter(add_traces, add_traces.begin()));
1088 set< shared_ptr<Trace> > remove_traces;
1089 set_difference(prev_traces.begin(), prev_traces.end(),
1090 traces.begin(), traces.end(),
1091 inserter(remove_traces, remove_traces.begin()));
1093 // Make a look-up table of sigrok Channels to pulseview Signals
1094 unordered_map<shared_ptr<data::SignalBase>, shared_ptr<Signal> >
1096 for (const shared_ptr<Signal> &sig : signals_)
1097 signal_map[sig->base()] = sig;
1099 // Populate channel groups
1101 for (auto entry : sr_dev->channel_groups()) {
1102 const shared_ptr<sigrok::ChannelGroup> &group = entry.second;
1104 if (group->channels().size() <= 1)
1107 // Find best trace group to add to
1108 TraceTreeItemOwner *owner = find_prevalent_trace_group(
1111 // If there is no trace group, create one
1112 shared_ptr<TraceGroup> new_trace_group;
1114 new_trace_group.reset(new TraceGroup());
1115 owner = new_trace_group.get();
1118 // Extract traces for the trace group, removing them from
1120 const vector< shared_ptr<Trace> > new_traces_in_group =
1121 extract_new_traces_for_channels(group->channels(),
1122 signal_map, add_traces);
1124 // Add the traces to the group
1125 const pair<int, int> prev_v_extents = owner->v_extents();
1126 int offset = prev_v_extents.second - prev_v_extents.first;
1127 for (shared_ptr<Trace> trace : new_traces_in_group) {
1129 owner->add_child_item(trace);
1131 const pair<int, int> extents = trace->v_extents();
1132 if (trace->enabled())
1133 offset += -extents.first;
1134 trace->force_to_v_offset(offset);
1135 if (trace->enabled())
1136 offset += extents.second;
1139 if (new_trace_group) {
1140 // Assign proper vertical offsets to each channel in the group
1141 new_trace_group->restack_items();
1143 // If this is a new group, enqueue it in the new top level
1145 if (!new_traces_in_group.empty())
1146 new_top_level_items.push_back(new_trace_group);
1150 // Enqueue the remaining logic channels in a group
1151 vector< shared_ptr<Channel> > logic_channels;
1152 copy_if(channels.begin(), channels.end(), back_inserter(logic_channels),
1153 [](const shared_ptr<Channel>& c) {
1154 return c->type() == sigrok::ChannelType::LOGIC; });
1156 const vector< shared_ptr<Trace> > non_grouped_logic_signals =
1157 extract_new_traces_for_channels(logic_channels, signal_map, add_traces);
1159 if (non_grouped_logic_signals.size() > 0) {
1160 const shared_ptr<TraceGroup> non_grouped_trace_group(
1161 make_shared<TraceGroup>());
1162 for (shared_ptr<Trace> trace : non_grouped_logic_signals)
1163 non_grouped_trace_group->add_child_item(trace);
1165 non_grouped_trace_group->restack_items();
1166 new_top_level_items.push_back(non_grouped_trace_group);
1169 // Enqueue the remaining channels as free ungrouped traces
1170 const vector< shared_ptr<Trace> > new_top_level_signals =
1171 extract_new_traces_for_channels(channels, signal_map, add_traces);
1172 new_top_level_items.insert(new_top_level_items.end(),
1173 new_top_level_signals.begin(), new_top_level_signals.end());
1175 // Enqueue any remaining traces i.e. decode traces
1176 new_top_level_items.insert(new_top_level_items.end(),
1177 add_traces.begin(), add_traces.end());
1179 // Remove any removed traces
1180 for (shared_ptr<Trace> trace : remove_traces) {
1181 TraceTreeItemOwner *const owner = trace->owner();
1183 owner->remove_child_item(trace);
1186 // Remove any empty trace groups
1187 for (shared_ptr<TraceGroup> group : list_by_type<TraceGroup>())
1188 if (group->child_items().size() == 0) {
1189 remove_child_item(group);
1193 // Add and position the pending top levels items
1194 int offset = v_extents().second;
1195 for (auto item : new_top_level_items) {
1196 add_child_item(item);
1198 // Position the item after the last item or at the top if there is none
1199 const pair<int, int> extents = item->v_extents();
1201 if (item->enabled())
1202 offset += -extents.first;
1204 item->force_to_v_offset(offset);
1206 if (item->enabled())
1207 offset += extents.second;
1213 viewport_->update();
1215 if (reset_scrollbar)
1216 set_scroll_default();
1219 void View::capture_state_updated(int state)
1221 if (state == Session::Running) {
1222 set_time_unit(util::TimeUnit::Samples);
1224 trigger_markers_.clear();
1226 // Activate "always zoom to fit" if the setting is enabled and we're
1227 // the main view of this session (other trace views may be used for
1228 // zooming and we don't want to mess them up)
1229 GlobalSettings settings;
1230 bool state = settings.value(GlobalSettings::Key_View_AlwaysZoomToFit).toBool();
1231 if (is_main_view_ && state) {
1232 always_zoom_to_fit_ = true;
1233 always_zoom_to_fit_changed(always_zoom_to_fit_);
1236 // Enable sticky scrolling if the setting is enabled
1237 sticky_scrolling_ = settings.value(GlobalSettings::Key_View_StickyScrolling).toBool();
1240 if (state == Session::Stopped) {
1241 // After acquisition has stopped we need to re-calculate the ticks once
1242 // as it's otherwise done when the user pans or zooms, which is too late
1243 calculate_tick_spacing();
1245 // Reset "always zoom to fit", the acquisition has stopped
1246 if (always_zoom_to_fit_) {
1247 // Perform a final zoom-to-fit before disabling
1248 zoom_fit(always_zoom_to_fit_);
1249 always_zoom_to_fit_ = false;
1250 always_zoom_to_fit_changed(always_zoom_to_fit_);
1255 void View::data_updated()
1257 if (always_zoom_to_fit_ || sticky_scrolling_) {
1258 if (!delayed_view_updater_.isActive())
1259 delayed_view_updater_.start();
1261 determine_time_unit();
1264 viewport_->update();
1268 void View::perform_delayed_view_update()
1270 if (always_zoom_to_fit_) {
1272 } else if (sticky_scrolling_) {
1273 // Make right side of the view sticky
1276 get_scroll_layout(length, offset);
1278 const QSize areaSize = viewport_->size();
1279 length = max(length - areaSize.width(), 0.0);
1281 set_offset(scale_ * length);
1284 determine_time_unit();
1287 viewport_->update();
1290 void View::process_sticky_events()
1292 if (sticky_events_ & TraceTreeItemHExtentsChanged)
1294 if (sticky_events_ & TraceTreeItemVExtentsChanged) {
1295 restack_all_trace_tree_items();
1299 // Clear the sticky events
1303 void View::on_hover_point_changed()
1305 const vector<shared_ptr<TraceTreeItem>> trace_tree_items(
1306 list_by_type<TraceTreeItem>());
1307 for (shared_ptr<TraceTreeItem> r : trace_tree_items)
1308 r->hover_point_changed();
1311 } // namespace TraceView
1312 } // namespace views