From: Joel Holdsworth Date: Mon, 1 Sep 2014 21:46:34 +0000 (+0100) Subject: View: Keep a list of owned traces in RowItemOwner X-Git-Tag: pulseview-0.3.0~476 X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=68b21a71797051fb48ed272bc2a6b4893bdbf517;p=pulseview.git View: Keep a list of owned traces in RowItemOwner --- diff --git a/pv/view/header.cpp b/pv/view/header.cpp index 361a60c1..5e978a70 100644 --- a/pv/view/header.cpp +++ b/pv/view/header.cpp @@ -24,7 +24,8 @@ #include "signal.h" #include "../sigsession.h" -#include +#include +#include #include #include @@ -38,6 +39,7 @@ using std::max; using std::make_pair; using std::pair; using std::shared_ptr; +using std::stable_sort; using std::vector; namespace pv { @@ -112,7 +114,11 @@ void Header::paintEvent(QPaintEvent*) // left edge of the widget, because then the selection shadow // would be clipped away. const int w = width() - BaselineOffset; - const vector< shared_ptr > row_items(_view.child_items()); + + vector< shared_ptr > row_items(_view.child_items()); + stable_sort(row_items.begin(), row_items.end(), + [](const shared_ptr &a, const shared_ptr &b) { + return a->v_offset() < b->v_offset(); }); QPainter painter(this); painter.setRenderHint(QPainter::Antialiasing); diff --git a/pv/view/rowitemowner.cpp b/pv/view/rowitemowner.cpp index 3ef2782e..d6150f89 100644 --- a/pv/view/rowitemowner.cpp +++ b/pv/view/rowitemowner.cpp @@ -18,4 +18,46 @@ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ +#include + +#include "rowitem.h" #include "rowitemowner.h" + +using std::shared_ptr; +using std::vector; + +namespace pv { +namespace view { + +const vector< shared_ptr >& RowItemOwner::child_items() const +{ + return _items; +} + +void RowItemOwner::clear_child_items() +{ + for (auto &i : _items) { + assert(i->owner() == this); + i->set_owner(nullptr); + } + _items.clear(); +} + +void RowItemOwner::add_child_item(std::shared_ptr item) +{ + assert(!item->owner()); + item->set_owner(this); + _items.push_back(item); +} + +void RowItemOwner::remove_child_item(std::shared_ptr item) +{ + assert(item->owner() == this); + item->set_owner(nullptr); + auto iter = std::find(_items.begin(), _items.end(), item); + assert(iter != _items.end()); + _items.erase(iter); +} + +} // view +} // pv diff --git a/pv/view/rowitemowner.h b/pv/view/rowitemowner.h index 53e2e14f..4b538e6d 100644 --- a/pv/view/rowitemowner.h +++ b/pv/view/rowitemowner.h @@ -61,9 +61,28 @@ public: /** * Returns a list of row items owned by this object. */ - virtual std::vector< std::shared_ptr > child_items() const = 0; + virtual const std::vector< std::shared_ptr >& + child_items() const; + + /** + * Clears the list of child items. + */ + void clear_child_items(); + + /** + * Adds a child item to this object. + */ + void add_child_item(std::shared_ptr item); + + /** + * Removes a child item from this object. + */ + void remove_child_item(std::shared_ptr item); virtual void update_viewport() = 0; + +private: + std::vector< std::shared_ptr > _items; }; } // view diff --git a/pv/view/view.cpp b/pv/view/view.cpp index 7a55bbb3..fae74f8f 100644 --- a/pv/view/view.cpp +++ b/pv/view/view.cpp @@ -251,27 +251,6 @@ void View::set_scale_offset(double scale, double offset) scale_offset_changed(); } -vector< shared_ptr > View::child_items() const -{ - vector< shared_ptr > row_items; - - const vector< shared_ptr > sigs( - session().get_signals()); - copy(sigs.begin(), sigs.end(), back_inserter(row_items)); - -#ifdef ENABLE_DECODE - const vector< shared_ptr > decode_sigs( - session().get_decode_signals()); - copy(decode_sigs.begin(), decode_sigs.end(), back_inserter(row_items)); -#endif - - stable_sort(row_items.begin(), row_items.end(), - [](const shared_ptr &a, const shared_ptr &b) { - return a->v_offset() < b->v_offset(); }); - - return row_items; -} - list > View::selected_items() const { list > items; @@ -279,8 +258,7 @@ list > View::selected_items() const // List the selected signals const vector< shared_ptr > row_items(child_items()); for (shared_ptr r : row_items) { - assert(r); - if (r->selected()) + if (r && r->selected()) items.push_back(r); } @@ -541,10 +519,24 @@ void View::v_scroll_value_changed(int value) void View::signals_changed() { + // Populate the traces + clear_child_items(); + + const vector< shared_ptr > sigs( + session().get_signals()); + for (auto s : sigs) + add_child_item(s); + +#ifdef ENABLE_DECODE + const vector< shared_ptr > decode_sigs( + session().get_decode_signals()); + for (auto s : decode_sigs) + add_child_item(s); +#endif + + // Create the initial layout int offset = SignalMargin + SignalHeight; - const vector< shared_ptr > row_items(child_items()); - for (shared_ptr r : row_items) { - r->set_owner(this); + for (shared_ptr r : child_items()) { r->set_v_offset(offset); offset += SignalHeight + 2 * SignalMargin; } diff --git a/pv/view/view.h b/pv/view/view.h index 5fbc0ade..50fbcf1c 100644 --- a/pv/view/view.h +++ b/pv/view/view.h @@ -110,11 +110,6 @@ public: */ void set_scale_offset(double scale, double offset); - /** - * Returns a list of traces owned by this object. - */ - std::vector< std::shared_ptr > child_items() const; - std::list > selected_items() const; std::set< std::shared_ptr > diff --git a/pv/view/viewport.cpp b/pv/view/viewport.cpp index c987c368..43da4728 100644 --- a/pv/view/viewport.cpp +++ b/pv/view/viewport.cpp @@ -20,6 +20,7 @@ #include #include +#include #include "view.h" #include "viewport.h" @@ -33,6 +34,7 @@ using std::abs; using std::max; using std::min; using std::shared_ptr; +using std::stable_sort; using std::vector; namespace pv { @@ -75,7 +77,10 @@ int Viewport::get_total_height() const void Viewport::paintEvent(QPaintEvent*) { - const vector< shared_ptr > row_items(_view.child_items()); + vector< shared_ptr > row_items(_view.child_items()); + stable_sort(row_items.begin(), row_items.end(), + [](const shared_ptr &a, const shared_ptr &b) { + return a->v_offset() < b->v_offset(); }); QPainter p(this); p.setRenderHint(QPainter::Antialiasing);