From: Joel Holdsworth Date: Mon, 17 Aug 2015 01:14:47 +0000 (-0600) Subject: Added ViewItemOwner X-Git-Tag: pulseview-0.3.0~84 X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=c373f82810ad9c5376a7370118de9dd587ee0e43;p=pulseview.git Added ViewItemOwner --- diff --git a/CMakeLists.txt b/CMakeLists.txt index c80a834d..51170dee 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -219,6 +219,7 @@ set(pulseview_SOURCES pv/view/tracetreeitemowner.cpp pv/view/view.cpp pv/view/viewitem.cpp + pv/view/viewitemowner.cpp pv/view/viewitempaintparams.cpp pv/view/viewport.cpp pv/view/viewwidget.cpp diff --git a/pv/view/rowitemiterator.hpp b/pv/view/rowitemiterator.hpp deleted file mode 100644 index 9857e8a5..00000000 --- a/pv/view/rowitemiterator.hpp +++ /dev/null @@ -1,132 +0,0 @@ -/* - * This file is part of the PulseView project. - * - * Copyright (C) 2014 Joel Holdsworth - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA - */ - -#ifndef PULSEVIEW_PV_VIEW_ROWITEMITERATOR_HPP -#define PULSEVIEW_PV_VIEW_ROWITEMITERATOR_HPP - -#include -#include -#include -#include -#include -#include -#include - -#include - -namespace pv { -namespace view { - -template class RowItemIterator -{ -public: - typedef typename std::conditional::value, - typename Owner::item_list::const_iterator, - typename Owner::item_list::iterator>::type child_iterator; - - typedef std::shared_ptr value_type; - typedef ptrdiff_t difference_type; - typedef value_type pointer; - typedef value_type& reference; - typedef std::forward_iterator_tag iterator_category; - -public: - RowItemIterator(Owner *owner) : - owner_stack_({owner}) {} - - RowItemIterator(Owner *owner, child_iterator iter) : - owner_stack_({owner}) { - assert(owner); - if (iter != owner->child_items().end()) - iter_stack_.push(iter); - } - - RowItemIterator(const RowItemIterator &o) : - owner_stack_(o.owner_stack_), - iter_stack_(o.iter_stack_) {} - - reference operator*() const { - return *iter_stack_.top(); - } - - reference operator->() const { - return *this; - } - - RowItemIterator& operator++() { - using std::dynamic_pointer_cast; - using std::shared_ptr; - - assert(!owner_stack_.empty()); - assert(!iter_stack_.empty()); - - shared_ptr owner(dynamic_pointer_cast( - *iter_stack_.top())); - if (owner && !owner->child_items().empty()) { - owner_stack_.push(owner.get()); - iter_stack_.push(owner->child_items().begin()); - } else { - while (!iter_stack_.empty() && (++iter_stack_.top()) == - owner_stack_.top()->child_items().end()) { - owner_stack_.pop(); - iter_stack_.pop(); - } - } - - return *this; - } - - RowItemIterator operator++(int) { - RowItemIterator pre = *this; - ++*this; - return pre; - } - - bool operator==(const RowItemIterator &o) const { - return (iter_stack_.empty() && o.iter_stack_.empty()) || ( - iter_stack_.size() == o.iter_stack_.size() && - owner_stack_.top() == o.owner_stack_.top() && - iter_stack_.top() == o.iter_stack_.top()); - } - - bool operator!=(const RowItemIterator &o) const { - return !((const RowItemIterator&)*this == o); - } - - void swap(RowItemIterator& other) { - swap(owner_stack_, other.owner_stack_); - swap(iter_stack_, other.iter_stack_); - } - -private: - std::stack owner_stack_; - std::stack iter_stack_; -}; - -template -void swap(RowItemIterator& a, RowItemIterator& b) -{ - a.swap(b); -} - -} // namespace view -} // namespace pv - -#endif // PULSEVIEW_PV_VIEW_ROWITEMITERATOR_HPP diff --git a/pv/view/tracegroup.cpp b/pv/view/tracegroup.cpp index 87f1ff3d..140b8518 100644 --- a/pv/view/tracegroup.cpp +++ b/pv/view/tracegroup.cpp @@ -49,7 +49,7 @@ TraceGroup::~TraceGroup() bool TraceGroup::enabled() const { return std::any_of(child_items().begin(), child_items().end(), - [](const shared_ptr &r) { return r->enabled(); }); + [](const shared_ptr &r) { return r->enabled(); }); } pv::Session& TraceGroup::session() @@ -115,7 +115,7 @@ void TraceGroup::paint_label(QPainter &p, const QRect &rect, bool hover) QRectF TraceGroup::label_rect(const QRectF &rect) const { QRectF child_rect; - for (const shared_ptr r : child_items()) + for (const shared_ptr r : child_items()) if (r && r->enabled()) child_rect = child_rect.united(r->label_rect(rect)); @@ -157,8 +157,7 @@ int TraceGroup::owner_visual_v_offset() const void TraceGroup::restack_items() { - vector< shared_ptr > items( - child_items().begin(), child_items().end()); + vector> items(trace_tree_child_items()); // Sort by the centre line of the extents stable_sort(items.begin(), items.end(), @@ -197,8 +196,7 @@ unsigned int TraceGroup::depth() const void TraceGroup::ungroup() { - const vector< shared_ptr > items( - child_items().begin(), child_items().end()); + const vector> items(trace_tree_child_items()); clear_child_items(); for (shared_ptr r : items) diff --git a/pv/view/tracetreeitem.hpp b/pv/view/tracetreeitem.hpp index 5e60ffcb..8a72f6eb 100644 --- a/pv/view/tracetreeitem.hpp +++ b/pv/view/tracetreeitem.hpp @@ -46,6 +46,11 @@ public: */ TraceTreeItem(); + /** + * Gets the owner this item in the view item hierachy. + */ + TraceTreeItemOwner* owner() const; + /** * Gets the vertical layout offset of this signal. */ @@ -77,11 +82,6 @@ public: */ void animate_to_layout_v_offset(); - /** - * Gets the owner this trace in the view trace hierachy. - */ - pv::view::TraceTreeItemOwner* owner() const; - /** * Sets the owner this trace in the view trace hierachy. * @param The new owner of the trace. @@ -112,7 +112,7 @@ public: virtual std::pair v_extents() const = 0; protected: - pv::view::TraceTreeItemOwner *owner_; + TraceTreeItemOwner *owner_; int layout_v_offset_; int visual_v_offset_; diff --git a/pv/view/tracetreeitemowner.cpp b/pv/view/tracetreeitemowner.cpp index 93b966b4..af11ad26 100644 --- a/pv/view/tracetreeitemowner.cpp +++ b/pv/view/tracetreeitemowner.cpp @@ -31,26 +31,41 @@ using std::min; using std::pair; using std::set; using std::shared_ptr; +using std::static_pointer_cast; using std::vector; namespace pv { namespace view { -vector< shared_ptr >& TraceTreeItemOwner::child_items() +vector< shared_ptr >& TraceTreeItemOwner::child_items() { return items_; } -const vector< shared_ptr >& TraceTreeItemOwner::child_items() const +const vector< shared_ptr >& TraceTreeItemOwner::child_items() const { return items_; } -void TraceTreeItemOwner::clear_child_items() +vector< std::shared_ptr > +TraceTreeItemOwner::trace_tree_child_items() const { + vector< shared_ptr > items; for (auto &i : items_) { - assert(i->owner() == this); - i->set_owner(nullptr); + assert(dynamic_pointer_cast(i)); + const shared_ptr t( + static_pointer_cast(i)); + items.push_back(t); + } + + return items; +} + +void TraceTreeItemOwner::clear_child_items() +{ + for (auto &t : trace_tree_child_items()) { + assert(t->owner() == this); + t->set_owner(nullptr); } items_.clear(); } @@ -75,37 +90,17 @@ void TraceTreeItemOwner::remove_child_item(std::shared_ptr item) extents_changed(true, true); } -TraceTreeItemOwner::iterator TraceTreeItemOwner::begin() -{ - return iterator(this, items_.begin()); -} - -TraceTreeItemOwner::iterator TraceTreeItemOwner::end() -{ - return iterator(this); -} - -TraceTreeItemOwner::const_iterator TraceTreeItemOwner::begin() const -{ - return const_iterator(this, items_.cbegin()); -} - -TraceTreeItemOwner::const_iterator TraceTreeItemOwner::end() const -{ - return const_iterator(this); -} - pair TraceTreeItemOwner::v_extents() const { pair extents(INT_MAX, INT_MIN); - for (const shared_ptr r : child_items()) { - assert(r); - if (!r->enabled()) + for (const shared_ptr t : trace_tree_child_items()) { + assert(t); + if (!t->enabled()) continue; - const int child_offset = r->layout_v_offset(); - const pair child_extents = r->v_extents(); + const int child_offset = t->layout_v_offset(); + const pair child_extents = t->v_extents(); extents.first = min(child_extents.first + child_offset, extents.first); extents.second = max(child_extents.second + child_offset, diff --git a/pv/view/tracetreeitemowner.hpp b/pv/view/tracetreeitemowner.hpp index b26b1e7e..025c34ed 100644 --- a/pv/view/tracetreeitemowner.hpp +++ b/pv/view/tracetreeitemowner.hpp @@ -21,10 +21,8 @@ #ifndef PULSEVIEW_PV_VIEW_TRACETREEITEMOWNER_HPP #define PULSEVIEW_PV_VIEW_TRACETREEITEMOWNER_HPP -#include -#include - -#include "rowitemiterator.hpp" +#include "viewitemowner.hpp" +#include "tracetreeitem.hpp" namespace pv { @@ -35,24 +33,9 @@ namespace view { class TraceTreeItem; class View; -class TraceTreeItemOwner +class TraceTreeItemOwner : public ViewItemOwner { public: - typedef std::vector< std::shared_ptr > item_list; - typedef RowItemIterator iterator; - typedef RowItemIterator const_iterator; - -public: - /** - * Returns the session of the onwer. - */ - virtual pv::Session& session() = 0; - - /** - * Returns the session of the owner. - */ - virtual const pv::Session& session() const = 0; - /** * Returns the view of the owner. */ @@ -65,6 +48,16 @@ public: virtual int owner_visual_v_offset() const = 0; + /** + * Returns the session of the onwer. + */ + virtual pv::Session& session() = 0; + + /** + * Returns the session of the owner. + */ + virtual const pv::Session& session() const = 0; + /** * Returns the number of nested parents that this row item owner has. */ @@ -80,6 +73,12 @@ public: */ virtual const item_list& child_items() const; + /** + * Returns a list of row items owned by this object. + */ + std::vector< std::shared_ptr > + trace_tree_child_items() const; + /** * Clears the list of child items. */ @@ -95,43 +94,7 @@ public: */ void remove_child_item(std::shared_ptr item); - /** - * Returns a depth-first iterator at the beginning of the child TraceTreeItem - * tree. - */ - iterator begin(); - - /** - * Returns a depth-first iterator at the end of the child TraceTreeItem tree. - */ - iterator end(); - - /** - * Returns a constant depth-first iterator at the beginning of the - * child TraceTreeItem tree. - */ - const_iterator begin() const; - - /** - * Returns a constant depth-first iterator at the end of the child - * TraceTreeItem tree. - */ - const_iterator end() const; - - /** - * Creates a list of decendant signals filtered by type. - */ - template - std::vector< std::shared_ptr > list_by_type() { - std::vector< std::shared_ptr > items; - for (const auto &r : *this) { - std::shared_ptr p = std::dynamic_pointer_cast(r); - if (p) - items.push_back(p); - } - - return items; - } + virtual void restack_items(); /** * Computes the vertical extents of the contents of this row item owner. @@ -139,15 +102,10 @@ public: */ std::pair v_extents() const; - virtual void restack_items(); - public: virtual void row_item_appearance_changed(bool label, bool content) = 0; virtual void extents_changed(bool horz, bool vert) = 0; - -private: - item_list items_; }; } // view diff --git a/pv/view/viewitem.hpp b/pv/view/viewitem.hpp index 5b80c4e8..dd3d2aa0 100644 --- a/pv/view/viewitem.hpp +++ b/pv/view/viewitem.hpp @@ -39,6 +39,8 @@ class Popup; namespace view { +class ViewItemOwner; + class ViewItem : public QObject { Q_OBJECT diff --git a/pv/view/viewitemiterator.hpp b/pv/view/viewitemiterator.hpp new file mode 100644 index 00000000..179de383 --- /dev/null +++ b/pv/view/viewitemiterator.hpp @@ -0,0 +1,132 @@ +/* + * This file is part of the PulseView project. + * + * Copyright (C) 2014 Joel Holdsworth + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef PULSEVIEW_PV_VIEW_VIEWITEMITERATOR_HPP +#define PULSEVIEW_PV_VIEW_VIEWITEMITERATOR_HPP + +#include +#include +#include +#include +#include +#include +#include + +#include + +namespace pv { +namespace view { + +template class ViewItemIterator +{ +public: + typedef typename std::conditional::value, + typename Owner::item_list::const_iterator, + typename Owner::item_list::iterator>::type child_iterator; + + typedef std::shared_ptr value_type; + typedef ptrdiff_t difference_type; + typedef value_type pointer; + typedef value_type& reference; + typedef std::forward_iterator_tag iterator_category; + +public: + ViewItemIterator(Owner *owner) : + owner_stack_({owner}) {} + + ViewItemIterator(Owner *owner, child_iterator iter) : + owner_stack_({owner}) { + assert(owner); + if (iter != owner->child_items().end()) + iter_stack_.push(iter); + } + + ViewItemIterator(const ViewItemIterator &o) : + owner_stack_(o.owner_stack_), + iter_stack_(o.iter_stack_) {} + + reference operator*() const { + return *iter_stack_.top(); + } + + reference operator->() const { + return *this; + } + + ViewItemIterator& operator++() { + using std::dynamic_pointer_cast; + using std::shared_ptr; + + assert(!owner_stack_.empty()); + assert(!iter_stack_.empty()); + + shared_ptr owner(dynamic_pointer_cast( + *iter_stack_.top())); + if (owner && !owner->child_items().empty()) { + owner_stack_.push(owner.get()); + iter_stack_.push(owner->child_items().begin()); + } else { + while (!iter_stack_.empty() && (++iter_stack_.top()) == + owner_stack_.top()->child_items().end()) { + owner_stack_.pop(); + iter_stack_.pop(); + } + } + + return *this; + } + + ViewItemIterator operator++(int) { + ViewItemIterator pre = *this; + ++*this; + return pre; + } + + bool operator==(const ViewItemIterator &o) const { + return (iter_stack_.empty() && o.iter_stack_.empty()) || ( + iter_stack_.size() == o.iter_stack_.size() && + owner_stack_.top() == o.owner_stack_.top() && + iter_stack_.top() == o.iter_stack_.top()); + } + + bool operator!=(const ViewItemIterator &o) const { + return !((const ViewItemIterator&)*this == o); + } + + void swap(ViewItemIterator& other) { + swap(owner_stack_, other.owner_stack_); + swap(iter_stack_, other.iter_stack_); + } + +private: + std::stack owner_stack_; + std::stack iter_stack_; +}; + +template +void swap(ViewItemIterator& a, ViewItemIterator& b) +{ + a.swap(b); +} + +} // namespace view +} // namespace pv + +#endif // PULSEVIEW_PV_VIEW_VIEWITEMITERATOR_HPP diff --git a/pv/view/viewitemowner.cpp b/pv/view/viewitemowner.cpp new file mode 100644 index 00000000..8a704787 --- /dev/null +++ b/pv/view/viewitemowner.cpp @@ -0,0 +1,60 @@ +/* + * This file is part of the PulseView project. + * + * Copyright (C) 2014 Joel Holdsworth + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include + +#include "tracetreeitem.hpp" +#include "tracetreeitemowner.hpp" +#include "trace.hpp" + +using std::dynamic_pointer_cast; +using std::max; +using std::make_pair; +using std::min; +using std::pair; +using std::set; +using std::shared_ptr; +using std::vector; + +namespace pv { +namespace view { + +ViewItemOwner::iterator ViewItemOwner::begin() +{ + return iterator(this, items_.begin()); +} + +ViewItemOwner::iterator ViewItemOwner::end() +{ + return iterator(this); +} + +ViewItemOwner::const_iterator ViewItemOwner::begin() const +{ + return const_iterator(this, items_.cbegin()); +} + +ViewItemOwner::const_iterator ViewItemOwner::end() const +{ + return const_iterator(this); +} + +} // view +} // pv diff --git a/pv/view/viewitemowner.hpp b/pv/view/viewitemowner.hpp new file mode 100644 index 00000000..60c85076 --- /dev/null +++ b/pv/view/viewitemowner.hpp @@ -0,0 +1,101 @@ +/* + * This file is part of the PulseView project. + * + * Copyright (C) 2014 Joel Holdsworth + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef PULSEVIEW_PV_VIEW_VIEWITEMOWNER_HPP +#define PULSEVIEW_PV_VIEW_VIEWITEMOWNER_HPP + +#include +#include + +#include "viewitemiterator.hpp" + +namespace pv { + +class Session; + +namespace view { + +class ViewItem; +class View; + +class ViewItemOwner +{ +public: + typedef std::vector< std::shared_ptr > item_list; + typedef ViewItemIterator iterator; + typedef ViewItemIterator const_iterator; + +public: + /** + * Returns a list of row items owned by this object. + */ + virtual item_list& child_items() = 0; + + /** + * Returns a list of row items owned by this object. + */ + virtual const item_list& child_items() const = 0; + + /** + * Returns a depth-first iterator at the beginning of the child ViewItem + * tree. + */ + iterator begin(); + + /** + * Returns a depth-first iterator at the end of the child ViewItem tree. + */ + iterator end(); + + /** + * Returns a constant depth-first iterator at the beginning of the + * child ViewItem tree. + */ + const_iterator begin() const; + + /** + * Returns a constant depth-first iterator at the end of the child + * ViewItem tree. + */ + const_iterator end() const; + + /** + * Creates a list of decendant signals filtered by type. + */ + template + std::vector< std::shared_ptr > list_by_type() { + std::vector< std::shared_ptr > items; + for (const auto &r : *this) { + std::shared_ptr p = std::dynamic_pointer_cast(r); + if (p) + items.push_back(p); + } + + return items; + } + +protected: + item_list items_; +}; + +} // view +} // pv + +#endif // PULSEVIEW_PV_VIEW_VIEWITEMOWNER_HPP