+ vector< shared_ptr<RowItem> > new_top_level_items;
+
+ const auto device = session_.device();
+ if (!device)
+ return;
+
+ shared_ptr<sigrok::Device> sr_dev = device->device();
+ assert(sr_dev);
+
+ // Make a list of traces that are being added, and a list of traces
+ // that are being removed
+ const set<shared_ptr<Trace>> prev_traces = list_by_type<Trace>();
+
+ shared_lock<shared_mutex> lock(session_.signals_mutex());
+ const unordered_set< shared_ptr<Signal> > &sigs(session_.signals());
+
+ set< shared_ptr<Trace> > traces(sigs.begin(), sigs.end());
+
+#ifdef ENABLE_DECODE
+ const vector< shared_ptr<DecodeTrace> > decode_traces(
+ session().get_decode_signals());
+ traces.insert(decode_traces.begin(), decode_traces.end());
+#endif
+
+ set< shared_ptr<Trace> > add_traces;
+ set_difference(traces.begin(), traces.end(),
+ prev_traces.begin(), prev_traces.end(),
+ inserter(add_traces, add_traces.begin()));
+
+ set< shared_ptr<Trace> > remove_traces;
+ set_difference(prev_traces.begin(), prev_traces.end(),
+ traces.begin(), traces.end(),
+ inserter(remove_traces, remove_traces.begin()));
+
+ // Make a look-up table of sigrok Channels to pulseview Signals
+ unordered_map<shared_ptr<sigrok::Channel>, shared_ptr<Signal> >
+ signal_map;
+ for (const shared_ptr<Signal> &sig : sigs)
+ 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
+ RowItemOwner *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 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 channels as free ungrouped traces
+ const vector< shared_ptr<Trace> > new_top_level_signals =
+ extract_new_traces_for_channels(sr_dev->channels(),
+ signal_map, add_traces);
+ new_top_level_items.insert(new_top_level_items.end(),
+ new_top_level_signals.begin(), new_top_level_signals.end());
+
+ // Enqueue any remaining traces i.e. decode traces
+ new_top_level_items.insert(new_top_level_items.end(),
+ add_traces.begin(), add_traces.end());
+
+ // Remove any removed traces
+ for (shared_ptr<Trace> trace : remove_traces) {
+ RowItemOwner *const owner = trace->owner();
+ assert(owner);
+ owner->remove_child_item(trace);