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, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 #include <libsigrokdecode/libsigrokdecode.h>
31 #include <unordered_set>
33 #include <QApplication>
35 #include <QFontMetrics>
36 #include <QMouseEvent>
39 #include <libsigrok/libsigrok.hpp>
41 #include "cursorheader.hpp"
42 #include "decodetrace.hpp"
44 #include "logicsignal.hpp"
47 #include "tracegroup.hpp"
49 #include "viewport.hpp"
51 #include "pv/session.hpp"
52 #include "pv/data/logic.hpp"
53 #include "pv/data/logicsegment.hpp"
54 #include "pv/util.hpp"
56 using boost::shared_lock;
57 using boost::shared_mutex;
59 using pv::data::SignalData;
60 using pv::data::Segment;
61 using pv::util::format_time;
63 using std::back_inserter;
65 using std::dynamic_pointer_cast;
67 using std::lock_guard;
73 using std::shared_ptr;
74 using std::unordered_map;
75 using std::unordered_set;
82 const double View::MaxScale = 1e9;
83 const double View::MinScale = 1e-15;
85 const int View::MaxScrollValue = INT_MAX / 2;
87 const int View::ScaleUnits[3] = {1, 2, 5};
89 const QColor View::CursorAreaColour(220, 231, 243);
91 const QSizeF View::LabelPadding(4, 0);
93 View::View(Session &session, QWidget *parent) :
94 QAbstractScrollArea(parent),
96 viewport_(new Viewport(*this)),
97 ruler_(new Ruler(*this)),
98 cursorheader_(new CursorHeader(*this)),
99 header_(new Header(*this)),
103 updating_scroll_(false),
106 show_cursors_(false),
110 connect(horizontalScrollBar(), SIGNAL(valueChanged(int)),
111 this, SLOT(h_scroll_value_changed(int)));
112 connect(verticalScrollBar(), SIGNAL(valueChanged(int)),
113 this, SLOT(v_scroll_value_changed(int)));
115 connect(&session_, SIGNAL(signals_changed()),
116 this, SLOT(signals_changed()));
117 connect(&session_, SIGNAL(capture_state_changed(int)),
118 this, SLOT(data_updated()));
119 connect(&session_, SIGNAL(data_received()),
120 this, SLOT(data_updated()));
121 connect(&session_, SIGNAL(frame_ended()),
122 this, SLOT(data_updated()));
124 connect(cursors_.first().get(), SIGNAL(time_changed()),
125 this, SLOT(marker_time_changed()));
126 connect(cursors_.second().get(), SIGNAL(time_changed()),
127 this, SLOT(marker_time_changed()));
129 connect(header_, SIGNAL(signals_moved()),
130 this, SLOT(on_signals_moved()));
132 connect(header_, SIGNAL(selection_changed()),
133 cursorheader_, SLOT(clear_selection()));
134 connect(cursorheader_, SIGNAL(selection_changed()),
135 header_, SLOT(clear_selection()));
137 connect(header_, SIGNAL(selection_changed()),
138 this, SIGNAL(selection_changed()));
139 connect(cursorheader_, SIGNAL(selection_changed()),
140 this, SIGNAL(selection_changed()));
142 connect(this, SIGNAL(hover_point_changed()),
143 this, SLOT(on_hover_point_changed()));
145 connect(&lazy_event_handler_, SIGNAL(timeout()),
146 this, SLOT(process_sticky_events()));
147 lazy_event_handler_.setSingleShot(true);
149 setViewport(viewport_);
151 viewport_->installEventFilter(this);
152 ruler_->installEventFilter(this);
153 cursorheader_->installEventFilter(this);
154 header_->installEventFilter(this);
156 // Trigger the initial event manually. The default device has signals
157 // which were created before this object came into being
160 // make sure the transparent widgets are on the top
161 cursorheader_->raise();
164 // Update the zoom state
165 calculate_tick_spacing();
168 Session& View::session()
173 const Session& View::session() const
183 const View* View::view() const
188 Viewport* View::viewport()
193 const Viewport* View::viewport() const
198 double View::scale() const
203 double View::offset() const
208 int View::owner_visual_v_offset() const
213 unsigned int View::depth() const
218 unsigned int View::tick_prefix() const
223 double View::tick_period() const
228 void View::zoom(double steps)
230 zoom(steps, viewport_->width() / 2);
233 void View::zoom(double steps, int offset)
235 set_zoom(scale_ * pow(3.0/2.0, -steps), offset);
238 void View::zoom_fit()
240 const pair<double, double> extents = get_time_extents();
241 const double delta = extents.second - extents.first;
246 const int w = viewport_->width();
250 const double scale = max(min(delta / w, MaxScale), MinScale);
251 set_scale_offset(scale, extents.first);
254 void View::zoom_one_to_one()
256 using pv::data::SignalData;
258 // Make a set of all the visible data objects
259 set< shared_ptr<SignalData> > visible_data = get_visible_data();
260 if (visible_data.empty())
263 double samplerate = 0.0;
264 for (const shared_ptr<SignalData> d : visible_data) {
266 const vector< shared_ptr<Segment> > segments =
268 for (const shared_ptr<Segment> &s : segments)
269 samplerate = max(samplerate, s->samplerate());
272 if (samplerate == 0.0)
276 const int w = viewport_->width();
280 set_zoom(1.0 / samplerate, w / 2);
283 void View::set_scale_offset(double scale, double offset)
288 calculate_tick_spacing();
292 cursorheader_->update();
294 scale_offset_changed();
297 set< shared_ptr<SignalData> > View::get_visible_data() const
299 shared_lock<shared_mutex> lock(session().signals_mutex());
300 const vector< shared_ptr<Signal> > &sigs(session().signals());
302 // Make a set of all the visible data objects
303 set< shared_ptr<SignalData> > visible_data;
304 for (const shared_ptr<Signal> sig : sigs)
306 visible_data.insert(sig->data());
311 pair<double, double> View::get_time_extents() const
313 double left_time = DBL_MAX, right_time = DBL_MIN;
314 const set< shared_ptr<SignalData> > visible_data = get_visible_data();
315 for (const shared_ptr<SignalData> d : visible_data)
317 const vector< shared_ptr<Segment> > segments =
319 for (const shared_ptr<Segment> &s : segments) {
320 double samplerate = s->samplerate();
321 samplerate = (samplerate <= 0.0) ? 1.0 : samplerate;
323 const double start_time = s->start_time();
324 left_time = min(left_time, start_time);
325 right_time = max(right_time, start_time +
326 d->get_max_sample_count() / samplerate);
330 if (left_time == DBL_MAX && right_time == DBL_MIN)
331 return make_pair(0.0, 0.0);
333 assert(left_time < right_time);
334 return make_pair(left_time, right_time);
337 bool View::cursors_shown() const
339 return show_cursors_;
342 void View::show_cursors(bool show)
344 show_cursors_ = show;
345 cursorheader_->update();
349 void View::centre_cursors()
351 const double time_width = scale_ * viewport_->width();
352 cursors_.first()->set_time(offset_ + time_width * 0.4);
353 cursors_.second()->set_time(offset_ + time_width * 0.6);
354 cursorheader_->update();
358 CursorPair& View::cursors()
363 const CursorPair& View::cursors() const
368 const QPoint& View::hover_point() const
373 void View::update_viewport()
380 void View::restack_all_row_items()
382 // Make a set of owners
383 unordered_set< RowItemOwner* > owners;
384 for (const auto &r : *this)
385 owners.insert(r->owner());
387 // Make a list that is sorted from deepest first
388 vector< RowItemOwner* > sorted_owners(owners.begin(), owners.end());
389 sort(sorted_owners.begin(), sorted_owners.end(),
390 [](const RowItemOwner* a, const RowItemOwner *b) {
391 return a->depth() > b->depth(); });
393 // Restack the items recursively
394 for (auto &o : sorted_owners)
397 // Animate the items to their destination
398 for (const auto &r : *this)
399 r->animate_to_layout_v_offset();
402 void View::get_scroll_layout(double &length, double &offset) const
404 const pair<double, double> extents = get_time_extents();
405 length = (extents.second - extents.first) / scale_;
406 offset = offset_ / scale_;
409 void View::set_zoom(double scale, int offset)
411 const double cursor_offset = offset_ + scale_ * offset;
412 const double new_scale = max(min(scale, MaxScale), MinScale);
413 const double new_offset = cursor_offset - new_scale * offset;
414 set_scale_offset(new_scale, new_offset);
417 void View::calculate_tick_spacing()
419 const double SpacingIncrement = 32.0f;
420 const double MinValueSpacing = 32.0f;
422 double min_width = SpacingIncrement, typical_width;
424 QFontMetrics m(QApplication::font());
427 const double min_period = scale_ * min_width;
429 const int order = (int)floorf(log10f(min_period));
430 const double order_decimal = pow(10.0, order);
432 unsigned int unit = 0;
435 tick_period_ = order_decimal * ScaleUnits[unit++];
436 } while (tick_period_ < min_period &&
437 unit < countof(ScaleUnits));
439 tick_prefix_ = (order - pv::util::FirstSIPrefixPower) / 3;
441 typical_width = m.boundingRect(0, 0, INT_MAX, INT_MAX,
442 Qt::AlignLeft | Qt::AlignTop,
443 format_time(offset_, tick_prefix_)).width() +
446 min_width += SpacingIncrement;
448 } while(typical_width > tick_period_ / scale_);
451 void View::update_scroll()
455 const QSize areaSize = viewport_->size();
457 // Set the horizontal scroll bar
458 double length = 0, offset = 0;
459 get_scroll_layout(length, offset);
460 length = max(length - areaSize.width(), 0.0);
462 int major_tick_distance = tick_period_ / scale_;
464 horizontalScrollBar()->setPageStep(areaSize.width() / 2);
465 horizontalScrollBar()->setSingleStep(major_tick_distance);
467 updating_scroll_ = true;
469 if (length < MaxScrollValue) {
470 horizontalScrollBar()->setRange(0, length);
471 horizontalScrollBar()->setSliderPosition(offset);
473 horizontalScrollBar()->setRange(0, MaxScrollValue);
474 horizontalScrollBar()->setSliderPosition(
475 offset_ * MaxScrollValue / (scale_ * length));
478 updating_scroll_ = false;
480 // Set the vertical scrollbar
481 verticalScrollBar()->setPageStep(areaSize.height());
483 const pair<int, int> extents = v_extents();
484 const int extra_scroll_height = (extents.second - extents.first) / 4;
485 verticalScrollBar()->setRange(extents.first - extra_scroll_height,
486 extents.first + extra_scroll_height);
489 void View::update_layout()
492 header_->sizeHint().width() - pv::view::Header::BaselineOffset,
493 ruler_->sizeHint().height(), 0, 0);
494 ruler_->setGeometry(viewport_->x(), 0,
495 viewport_->width(), viewport_->y());
496 cursorheader_->setGeometry(
498 ruler_->sizeHint().height() - cursorheader_->sizeHint().height() / 2,
499 viewport_->width(), cursorheader_->sizeHint().height());
500 header_->setGeometry(0, viewport_->y(),
501 header_->sizeHint().width(), viewport_->height());
505 void View::paint_label(QPainter &p, int right, bool hover)
512 QRectF View::label_rect(int right)
518 bool View::add_channels_to_owner(
519 const vector< shared_ptr<sigrok::Channel> > &channels,
520 RowItemOwner *owner, int &offset,
521 unordered_map<shared_ptr<sigrok::Channel>, shared_ptr<Signal> >
523 std::function<bool (shared_ptr<RowItem>)> filter_func)
525 bool any_added = false;
529 for (const auto &channel : channels)
531 const auto iter = signal_map.find(channel);
532 if (iter == signal_map.end() ||
533 (filter_func && !filter_func((*iter).second)))
536 shared_ptr<RowItem> row_item = (*iter).second;
537 owner->add_child_item(row_item);
538 apply_offset(row_item, offset);
539 signal_map.erase(iter);
547 void View::apply_offset(shared_ptr<RowItem> row_item, int &offset) {
549 const pair<int, int> extents = row_item->v_extents();
550 if (row_item->enabled())
551 offset += -extents.first;
552 row_item->force_to_v_offset(offset);
553 if (row_item->enabled())
554 offset += extents.second;
557 bool View::eventFilter(QObject *object, QEvent *event)
559 const QEvent::Type type = event->type();
560 if (type == QEvent::MouseMove) {
562 const QMouseEvent *const mouse_event = (QMouseEvent*)event;
563 if (object == viewport_)
564 hover_point_ = mouse_event->pos();
565 else if (object == ruler_ || object == cursorheader_)
566 hover_point_ = QPoint(mouse_event->x(), 0);
567 else if (object == header_)
568 hover_point_ = QPoint(0, mouse_event->y());
570 hover_point_ = QPoint(-1, -1);
572 hover_point_changed();
574 } else if (type == QEvent::Leave) {
575 hover_point_ = QPoint(-1, -1);
576 hover_point_changed();
579 return QObject::eventFilter(object, event);
582 bool View::viewportEvent(QEvent *e)
586 case QEvent::MouseButtonPress:
587 case QEvent::MouseButtonRelease:
588 case QEvent::MouseButtonDblClick:
589 case QEvent::MouseMove:
591 case QEvent::TouchBegin:
592 case QEvent::TouchUpdate:
593 case QEvent::TouchEnd:
597 return QAbstractScrollArea::viewportEvent(e);
601 void View::resizeEvent(QResizeEvent*)
606 void View::appearance_changed(bool label, bool content)
614 void View::extents_changed(bool horz, bool vert)
617 (horz ? SelectableItemHExtentsChanged : 0) |
618 (vert ? SelectableItemVExtentsChanged : 0);
619 lazy_event_handler_.start();
622 void View::h_scroll_value_changed(int value)
624 if (updating_scroll_)
627 const int range = horizontalScrollBar()->maximum();
628 if (range < MaxScrollValue)
629 offset_ = scale_ * value;
631 double length = 0, offset;
632 get_scroll_layout(length, offset);
633 offset_ = scale_ * length * value / MaxScrollValue;
637 cursorheader_->update();
641 void View::v_scroll_value_changed(int value)
648 void View::signals_changed()
652 // Populate the traces
655 shared_ptr<sigrok::Device> device = session_.device();
658 // Collect a set of signals
659 unordered_map<shared_ptr<sigrok::Channel>, shared_ptr<Signal> >
662 shared_lock<shared_mutex> lock(session_.signals_mutex());
663 const vector< shared_ptr<Signal> > &sigs(session_.signals());
665 for (const shared_ptr<Signal> &sig : sigs)
666 signal_map[sig->channel()] = sig;
668 // Populate channel groups
669 for (auto entry : device->channel_groups())
671 const shared_ptr<sigrok::ChannelGroup> &group = entry.second;
673 if (group->channels().size() <= 1)
676 shared_ptr<TraceGroup> trace_group(new TraceGroup());
677 int child_offset = 0;
678 if (add_channels_to_owner(group->channels(),
679 trace_group.get(), child_offset, signal_map))
681 add_child_item(trace_group);
682 apply_offset(trace_group, offset);
686 // Add the remaining logic channels
687 shared_ptr<TraceGroup> logic_trace_group(new TraceGroup());
688 int child_offset = 0;
690 if (add_channels_to_owner(device->channels(),
691 logic_trace_group.get(), child_offset, signal_map,
692 [](shared_ptr<RowItem> r) -> bool {
693 return dynamic_pointer_cast<LogicSignal>(r) != nullptr;
697 add_child_item(logic_trace_group);
698 apply_offset(logic_trace_group, offset);
701 // Add the remaining channels
702 add_channels_to_owner(device->channels(), this, offset, signal_map);
703 assert(signal_map.empty());
705 // Add decode signals
707 const vector< shared_ptr<DecodeTrace> > decode_sigs(
708 session().get_decode_signals());
709 for (auto s : decode_sigs) {
711 apply_offset(s, offset);
718 void View::data_updated()
720 // Update the scroll bars
727 void View::marker_time_changed()
729 cursorheader_->update();
733 void View::on_signals_moved()
739 void View::process_sticky_events()
741 if (sticky_events_ & SelectableItemHExtentsChanged)
743 if (sticky_events_ & SelectableItemVExtentsChanged)
744 restack_all_row_items();
746 // Clear the sticky events
750 void View::on_hover_point_changed()
752 for (shared_ptr<RowItem> r : *this)
753 r->hover_point_changed();