]> sigrok.org Git - pulseview.git/blobdiff - pv/view/view.cpp
View: Support having no selected device
[pulseview.git] / pv / view / view.cpp
index 4575d05e2b7dd0fc58009a4bc927892dbe52a9a3..695d5f9f094b367a0b6aafc9050df2c9bad5f2ff 100644 (file)
@@ -36,9 +36,8 @@
 #include <QMouseEvent>
 #include <QScrollBar>
 
-#include <libsigrok/libsigrok.hpp>
+#include <libsigrokcxx/libsigrokcxx.hpp>
 
-#include "cursorheader.hpp"
 #include "decodetrace.hpp"
 #include "header.hpp"
 #include "logicsignal.hpp"
@@ -49,6 +48,7 @@
 #include "viewport.hpp"
 
 #include "pv/session.hpp"
+#include "pv/devices/device.hpp"
 #include "pv/data/logic.hpp"
 #include "pv/data/logicsegment.hpp"
 #include "pv/util.hpp"
@@ -60,7 +60,6 @@ using pv::data::SignalData;
 using pv::data::Segment;
 using pv::util::format_time;
 
-using std::back_inserter;
 using std::deque;
 using std::dynamic_pointer_cast;
 using std::list;
@@ -86,31 +85,26 @@ const int View::MaxScrollValue = INT_MAX / 2;
 
 const int View::ScaleUnits[3] = {1, 2, 5};
 
-const QColor View::CursorAreaColour(220, 231, 243);
-
-const QSizeF View::LabelPadding(4, 0);
-
 View::View(Session &session, QWidget *parent) :
        QAbstractScrollArea(parent),
        session_(session),
        viewport_(new Viewport(*this)),
        ruler_(new Ruler(*this)),
-       cursorheader_(new CursorHeader(*this)),
        header_(new Header(*this)),
        scale_(1e-6),
        offset_(0),
-       v_offset_(0),
        updating_scroll_(false),
        tick_period_(0.0),
        tick_prefix_(0),
        show_cursors_(false),
        cursors_(new CursorPair(*this)),
+       next_flag_text_('A'),
        hover_point_(-1, -1)
 {
        connect(horizontalScrollBar(), SIGNAL(valueChanged(int)),
                this, SLOT(h_scroll_value_changed(int)));
        connect(verticalScrollBar(), SIGNAL(valueChanged(int)),
-               this, SLOT(v_scroll_value_changed(int)));
+               this, SLOT(v_scroll_value_changed()));
 
        connect(&session_, SIGNAL(signals_changed()),
                this, SLOT(signals_changed()));
@@ -121,22 +115,14 @@ View::View(Session &session, QWidget *parent) :
        connect(&session_, SIGNAL(frame_ended()),
                this, SLOT(data_updated()));
 
-       connect(cursors_->first().get(), SIGNAL(time_changed()),
-               this, SLOT(marker_time_changed()));
-       connect(cursors_->second().get(), SIGNAL(time_changed()),
-               this, SLOT(marker_time_changed()));
-
-       connect(header_, SIGNAL(signals_moved()),
-               this, SLOT(on_signals_moved()));
-
        connect(header_, SIGNAL(selection_changed()),
-               cursorheader_, SLOT(clear_selection()));
-       connect(cursorheader_, SIGNAL(selection_changed()),
+               ruler_, SLOT(clear_selection()));
+       connect(ruler_, SIGNAL(selection_changed()),
                header_, SLOT(clear_selection()));
 
        connect(header_, SIGNAL(selection_changed()),
                this, SIGNAL(selection_changed()));
-       connect(cursorheader_, SIGNAL(selection_changed()),
+       connect(ruler_, SIGNAL(selection_changed()),
                this, SIGNAL(selection_changed()));
 
        connect(this, SIGNAL(hover_point_changed()),
@@ -150,7 +136,6 @@ View::View(Session &session, QWidget *parent) :
 
        viewport_->installEventFilter(this);
        ruler_->installEventFilter(this);
-       cursorheader_->installEventFilter(this);
        header_->installEventFilter(this);
 
        // Trigger the initial event manually. The default device has signals
@@ -158,7 +143,7 @@ View::View(Session &session, QWidget *parent) :
        signals_changed();
 
        // make sure the transparent widgets are on the top
-       cursorheader_->raise();
+       ruler_->raise();
        header_->raise();
 
        // Update the zoom state
@@ -197,7 +182,9 @@ const Viewport* View::viewport() const
 
 vector< shared_ptr<TimeItem> > View::time_items() const
 {
-       vector< shared_ptr<TimeItem> > items;
+       const vector<shared_ptr<Flag>> f(flags());
+       vector<shared_ptr<TimeItem>> items(f.begin(), f.end());
+       items.push_back(cursors_);
        items.push_back(cursors_->first());
        items.push_back(cursors_->second());
        return items;
@@ -215,7 +202,14 @@ double View::offset() const
 
 int View::owner_visual_v_offset() const
 {
-       return -v_offset_;
+       return -verticalScrollBar()->sliderPosition();
+}
+
+void View::set_v_offset(int offset)
+{
+       verticalScrollBar()->setSliderPosition(offset);
+       header_->update();
+       viewport_->update();
 }
 
 unsigned int View::depth() const
@@ -297,7 +291,6 @@ void View::set_scale_offset(double scale, double offset)
 
        update_scroll();
        ruler_->update();
-       cursorheader_->update();
        viewport_->update();
        scale_offset_changed();
 }
@@ -305,7 +298,7 @@ void View::set_scale_offset(double scale, double offset)
 set< shared_ptr<SignalData> > View::get_visible_data() const
 {
        shared_lock<shared_mutex> lock(session().signals_mutex());
-       const vector< shared_ptr<Signal> > &sigs(session().signals());
+       const unordered_set< shared_ptr<Signal> > &sigs(session().signals());
 
        // Make a set of all the visible data objects
        set< shared_ptr<SignalData> > visible_data;
@@ -331,7 +324,7 @@ pair<double, double> View::get_time_extents() const
                        const double start_time = s->start_time();
                        left_time = min(left_time, start_time);
                        right_time = max(right_time, start_time +
-                               d->get_max_sample_count() / samplerate);
+                               d->max_sample_count() / samplerate);
                }
        }
 
@@ -350,7 +343,7 @@ bool View::cursors_shown() const
 void View::show_cursors(bool show)
 {
        show_cursors_ = show;
-       cursorheader_->update();
+       ruler_->update();
        viewport_->update();
 }
 
@@ -359,7 +352,7 @@ void View::centre_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);
-       cursorheader_->update();
+       ruler_->update();
        viewport_->update();
 }
 
@@ -368,6 +361,32 @@ std::shared_ptr<CursorPair> View::cursors() const
        return cursors_;
 }
 
+void View::add_flag(double time)
+{
+       flags_.push_back(shared_ptr<Flag>(new Flag(*this, time,
+               QString("%1").arg(next_flag_text_))));
+       next_flag_text_ = (next_flag_text_ >= 'Z') ? 'A' :
+               (next_flag_text_ + 1);
+       time_item_appearance_changed(true, true);
+}
+
+void View::remove_flag(std::shared_ptr<Flag> flag)
+{
+       flags_.remove(flag);
+       time_item_appearance_changed(true, true);
+}
+
+vector< std::shared_ptr<Flag> > View::flags() const
+{
+       vector< std::shared_ptr<Flag> > flags(flags_.begin(), flags_.end());
+       stable_sort(flags.begin(), flags.end(),
+               [](const shared_ptr<Flag> &a, const shared_ptr<Flag> &b) {
+                       return a->time() < b->time();
+               });
+
+       return flags;
+}
+
 const QPoint& View::hover_point() const
 {
        return hover_point_;
@@ -482,11 +501,11 @@ void View::update_scroll()
 
        // Set the vertical scrollbar
        verticalScrollBar()->setPageStep(areaSize.height());
+       verticalScrollBar()->setSingleStep(areaSize.height() / 8);
 
        const pair<int, int> extents = v_extents();
-       const int extra_scroll_height = (extents.second - extents.first) / 4;
-       verticalScrollBar()->setRange(extents.first - extra_scroll_height,
-               extents.first + extra_scroll_height);
+       verticalScrollBar()->setRange(extents.first - (areaSize.height() / 2),
+               extents.second - (areaSize.height() / 2));
 }
 
 void View::update_layout()
@@ -495,26 +514,22 @@ void View::update_layout()
                header_->sizeHint().width() - pv::view::Header::BaselineOffset,
                ruler_->sizeHint().height(), 0, 0);
        ruler_->setGeometry(viewport_->x(), 0,
-               viewport_->width(), viewport_->y());
-       cursorheader_->setGeometry(
-               viewport_->x(),
-               ruler_->sizeHint().height() - cursorheader_->sizeHint().height() / 2,
-               viewport_->width(), cursorheader_->sizeHint().height());
+               viewport_->width(), ruler_->extended_size_hint().height());
        header_->setGeometry(0, viewport_->y(),
-               header_->sizeHint().width(), viewport_->height());
+               header_->extended_size_hint().width(), viewport_->height());
        update_scroll();
 }
 
-void View::paint_label(QPainter &p, int right, bool hover)
+void View::paint_label(QPainter &p, const QRect &rect, bool hover)
 {
        (void)p;
-       (void)right;
+       (void)rect;
        (void)hover;
 }
 
-QRectF View::label_rect(int right)
+QRectF View::label_rect(const QRectF &rect)
 {
-       (void)right;
+       (void)rect;
        return QRectF();
 }
 
@@ -565,7 +580,7 @@ bool View::eventFilter(QObject *object, QEvent *event)
                const QMouseEvent *const mouse_event = (QMouseEvent*)event;
                if (object == viewport_)
                        hover_point_ = mouse_event->pos();
-               else if (object == ruler_ || object == cursorheader_)
+               else if (object == ruler_)
                        hover_point_ = QPoint(mouse_event->x(), 0);
                else if (object == header_)
                        hover_point_ = QPoint(0, mouse_event->y());
@@ -606,7 +621,7 @@ void View::resizeEvent(QResizeEvent*)
        update_layout();
 }
 
-void View::appearance_changed(bool label, bool content)
+void View::row_item_appearance_changed(bool label, bool content)
 {
        if (label)
                header_->update();
@@ -614,11 +629,19 @@ void View::appearance_changed(bool label, bool content)
                viewport_->update();
 }
 
+void View::time_item_appearance_changed(bool label, bool content)
+{
+       if (label)
+               ruler_->update();
+       if (content)
+               viewport_->update();
+}
+
 void View::extents_changed(bool horz, bool vert)
 {
        sticky_events_ |=
-               (horz ? SelectableItemHExtentsChanged : 0) |
-               (vert ? SelectableItemVExtentsChanged : 0);
+               (horz ? RowItemHExtentsChanged : 0) |
+               (vert ? RowItemVExtentsChanged : 0);
        lazy_event_handler_.start();
 }
 
@@ -637,13 +660,11 @@ void View::h_scroll_value_changed(int value)
        }
 
        ruler_->update();
-       cursorheader_->update();
        viewport_->update();
 }
 
-void View::v_scroll_value_changed(int value)
+void View::v_scroll_value_changed()
 {
-       v_offset_ = value;
        header_->update();
        viewport_->update();
 }
@@ -655,21 +676,25 @@ void View::signals_changed()
        // Populate the traces
        clear_child_items();
 
-       shared_ptr<sigrok::Device> device = session_.device();
-       assert(device);
+       const auto device = session_.device();
+       if (!device)
+               return;
+
+       shared_ptr<sigrok::Device> sr_dev = device->device();
+       assert(sr_dev);
 
        // Collect a set of signals
        unordered_map<shared_ptr<sigrok::Channel>, shared_ptr<Signal> >
                signal_map;
 
        shared_lock<shared_mutex> lock(session_.signals_mutex());
-       const vector< shared_ptr<Signal> > &sigs(session_.signals());
+       const unordered_set< shared_ptr<Signal> > &sigs(session_.signals());
 
        for (const shared_ptr<Signal> &sig : sigs)
                signal_map[sig->channel()] = sig;
 
        // Populate channel groups
-       for (auto entry : device->channel_groups())
+       for (auto entry : sr_dev->channel_groups())
        {
                const shared_ptr<sigrok::ChannelGroup> &group = entry.second;
 
@@ -690,7 +715,7 @@ void View::signals_changed()
        shared_ptr<TraceGroup> logic_trace_group(new TraceGroup());
        int child_offset = 0;
 
-       if (add_channels_to_owner(device->channels(),
+       if (add_channels_to_owner(sr_dev->channels(),
                logic_trace_group.get(), child_offset, signal_map,
                [](shared_ptr<RowItem> r) -> bool {
                        return dynamic_pointer_cast<LogicSignal>(r) != nullptr;
@@ -702,7 +727,7 @@ void View::signals_changed()
        }
 
        // Add the remaining channels
-       add_channels_to_owner(device->channels(), this, offset, signal_map);
+       add_channels_to_owner(sr_dev->channels(), this, offset, signal_map);
        assert(signal_map.empty());
 
        // Add decode signals
@@ -727,24 +752,14 @@ void View::data_updated()
        viewport_->update();
 }
 
-void View::marker_time_changed()
-{
-       cursorheader_->update();
-       viewport_->update();
-}
-
-void View::on_signals_moved()
-{
-       update_scroll();
-       signals_moved();
-}
-
 void View::process_sticky_events()
 {
-       if (sticky_events_ & SelectableItemHExtentsChanged)
+       if (sticky_events_ & RowItemHExtentsChanged)
                update_layout();
-       if (sticky_events_ & SelectableItemVExtentsChanged)
+       if (sticky_events_ & RowItemVExtentsChanged) {
                restack_all_row_items();
+               update_scroll();
+       }
 
        // Clear the sticky events
        sticky_events_ = 0;