]> sigrok.org Git - pulseview.git/commitdiff
View: Provide a clear setup path for the initial v_offset value
authorSoeren Apel <redacted>
Sun, 3 Apr 2016 18:37:53 +0000 (20:37 +0200)
committerSoeren Apel <redacted>
Sun, 3 Apr 2016 18:37:53 +0000 (20:37 +0200)
- View::update_scroll():
  The range for the vertical scrollbar is only updated if there
  are traces in the view.

- View::set_scroll_default():
  This new function adjusts the vertical offset of the view so
  that all traces are visible. Needs to be executed once after a
  new device was selected.

- View::signals_changed():
  We want to be able to clear stale items in-between devices, so
  we no longer want to simply return when no device is selected.
  This means we need to handle a missing device more gracefully.

  Also, we now reset the vertical scroll bar when no device is
  selected and call set_scroll_default() when needed.

pv/view/view.cpp
pv/view/view.hpp

index 8e48ae444582ffd96b9f3d5af407dcd28b3abe17..b7ee331f165c175bdaaf1080561b2ae31650644e 100644 (file)
@@ -689,8 +689,47 @@ void View::update_scroll()
        verticalScrollBar()->setSingleStep(areaSize.height() / 8);
 
        const pair<int, int> extents = v_extents();
-       verticalScrollBar()->setRange(extents.first - (areaSize.height() / 2),
-               extents.second - (areaSize.height() / 2));
+
+       // Don't change the scrollbar range if there are no traces
+       if (extents.first != extents.second)
+               verticalScrollBar()->setRange(extents.first - areaSize.height(),
+                       extents.second);
+
+       if (scroll_needs_defaults)
+               set_scroll_default();
+}
+
+void View::reset_scroll()
+{
+       verticalScrollBar()->setRange(0, 0);
+}
+
+void View::set_scroll_default()
+{
+       assert(viewport_);
+
+       const QSize areaSize = viewport_->size();
+
+       // Special case: when starting up and the window isn't visible yet,
+       // areaSize is [0, 0]. In this case we want to be called again later
+       if (areaSize.height() == 0) {
+               scroll_needs_defaults = true;
+               return;
+       } else {
+               scroll_needs_defaults = false;
+       }
+
+       const pair<int, int> extents = v_extents();
+       const int trace_height = extents.second - extents.first;
+
+       // Do all traces fit in the view?
+       if (areaSize.height() >= trace_height)
+               // Center all traces vertically
+               set_v_offset(extents.first -
+                       ((areaSize.height() - trace_height) / 2));
+       else
+               // Put the first trace at the top, letting the bottom ones overflow
+               set_v_offset(extents.first);
 }
 
 void View::update_layout()
@@ -909,15 +948,23 @@ void View::signals_changed()
 {
        using sigrok::Channel;
 
-       vector< shared_ptr<TraceTreeItem> > new_top_level_items;
+       vector< shared_ptr<Channel> > channels;
+       shared_ptr<sigrok::Device> sr_dev;
 
-       if (!session_.device())
-               return;
+       // Do we need to set the vertical scrollbar to its default position later?
+       // We do if there are no traces, i.e. the scroll bar has no range set
+       bool reset_scrollbar =
+               verticalScrollBar()->minimum() == verticalScrollBar()->maximum();
 
-       shared_ptr<sigrok::Device> sr_dev = session_.device()->device();
-       assert(sr_dev);
-       const vector< shared_ptr<Channel> > channels(
-               sr_dev->channels());
+       if (!session_.device()) {
+               reset_scroll();
+       } else {
+               assert(sr_dev);
+               sr_dev = session_.device()->device();
+               channels = sr_dev->channels();
+       }
+
+       vector< shared_ptr<TraceTreeItem> > new_top_level_items;
 
        // Make a list of traces that are being added, and a list of traces
        // that are being removed
@@ -952,53 +999,54 @@ void View::signals_changed()
                signal_map[sig->channel()] = sig;
 
        // Populate channel groups
-       for (auto entry : sr_dev->channel_groups()) {
-               const shared_ptr<sigrok::ChannelGroup> &group = entry.second;
-
-               if (group->channels().size() <= 1)
-                       continue;
-
-               // Find best trace group to add to
-               TraceTreeItemOwner *owner = find_prevalent_trace_group(
-                       group, signal_map);
-
-               // If there is no trace group, create one
-               shared_ptr<TraceGroup> new_trace_group;
-               if (!owner) {
-                       new_trace_group.reset(new TraceGroup());
-                       owner = new_trace_group.get();
-               }
-
-               // Extract traces for the trace group, removing them from
-               // the add list
-               const vector< shared_ptr<Trace> > new_traces_in_group =
-                       extract_new_traces_for_channels(group->channels(),
-                               signal_map, add_traces);
-
-               // Add the traces to the group
-               const pair<int, int> prev_v_extents = owner->v_extents();
-               int offset = prev_v_extents.second - prev_v_extents.first;
-               for (shared_ptr<Trace> trace : new_traces_in_group) {
-                       assert(trace);
-                       owner->add_child_item(trace);
-
-                       const pair<int, int> extents = trace->v_extents();
-                       if (trace->enabled())
-                               offset += -extents.first;
-                       trace->force_to_v_offset(offset);
-                       if (trace->enabled())
-                               offset += extents.second;
+       if (sr_dev)
+               for (auto entry : sr_dev->channel_groups()) {
+                       const shared_ptr<sigrok::ChannelGroup> &group = entry.second;
+
+                       if (group->channels().size() <= 1)
+                               continue;
+
+                       // Find best trace group to add to
+                       TraceTreeItemOwner *owner = find_prevalent_trace_group(
+                               group, signal_map);
+
+                       // If there is no trace group, create one
+                       shared_ptr<TraceGroup> new_trace_group;
+                       if (!owner) {
+                               new_trace_group.reset(new TraceGroup());
+                               owner = new_trace_group.get();
+                       }
+
+                       // Extract traces for the trace group, removing them from
+                       // the add list
+                       const vector< shared_ptr<Trace> > new_traces_in_group =
+                               extract_new_traces_for_channels(group->channels(),
+                                       signal_map, add_traces);
+
+                       // Add the traces to the group
+                       const pair<int, int> prev_v_extents = owner->v_extents();
+                       int offset = prev_v_extents.second - prev_v_extents.first;
+                       for (shared_ptr<Trace> trace : new_traces_in_group) {
+                               assert(trace);
+                               owner->add_child_item(trace);
+
+                               const pair<int, int> extents = trace->v_extents();
+                               if (trace->enabled())
+                                       offset += -extents.first;
+                               trace->force_to_v_offset(offset);
+                               if (trace->enabled())
+                                       offset += extents.second;
+                       }
+
+                       // Assign proper vertical offsets to each channel in the group
+                       new_trace_group->restack_items();
+
+                       // If this is a new group, enqueue it in the new top level
+                       // items list
+                       if (!new_traces_in_group.empty() && new_trace_group)
+                               new_top_level_items.push_back(new_trace_group);
                }
 
-               // Assign proper vertical offsets to each channel in the group
-               new_trace_group->restack_items();
-
-               // If this is a new group, enqueue it in the new top level
-               // items list
-               if (!new_traces_in_group.empty() && new_trace_group)
-                       new_top_level_items.push_back(new_trace_group);
-       }
-
        // Enqueue the remaining logic channels in a group
        vector< shared_ptr<Channel> > logic_channels;
        copy_if(channels.begin(), channels.end(), back_inserter(logic_channels),
@@ -1052,6 +1100,9 @@ void View::signals_changed()
 
        header_->update();
        viewport_->update();
+
+       if (reset_scrollbar)
+               set_scroll_default();
 }
 
 void View::capture_state_updated(int state)
index 25d1dbf78b1b8afb2134d0ceb3aec2f7759b1ea2..42fa322cc50b7e47f42ee51e1145a60ecf97c19b 100644 (file)
@@ -267,6 +267,10 @@ private:
 
        void update_scroll();
 
+       void reset_scroll();
+
+       void set_scroll_default();
+
        void update_layout();
 
        /**
@@ -396,6 +400,9 @@ private:
 
        unsigned int sticky_events_;
        QTimer lazy_event_handler_;
+
+       // This is true when the defaults couldn't be set due to insufficient info
+       bool scroll_needs_defaults;
 };
 
 } // namespace view