From ed1f4abfeac9d18e9994686df4d780cd61bcabfe Mon Sep 17 00:00:00 2001 From: Joel Holdsworth Date: Sat, 24 Jan 2009 12:50:49 +0000 Subject: [PATCH] Refactored find_branch_parent --- src/gui/gtk-lumiera.hpp | 1 + src/gui/model/parent-track.cpp | 21 +++++++++++++++ src/gui/model/parent-track.hpp | 7 ++++- src/gui/model/track.cpp | 25 +++--------------- src/gui/model/track.hpp | 26 ++++++++----------- .../timeline/timeline-layout-helper.cpp | 8 +++--- 6 files changed, 46 insertions(+), 42 deletions(-) diff --git a/src/gui/gtk-lumiera.hpp b/src/gui/gtk-lumiera.hpp index 53d146b95..956223de9 100644 --- a/src/gui/gtk-lumiera.hpp +++ b/src/gui/gtk-lumiera.hpp @@ -36,6 +36,7 @@ #include #include #include +#include #include "lib/util.hpp" extern "C" { diff --git a/src/gui/model/parent-track.cpp b/src/gui/model/parent-track.cpp index 4b1a678cf..d46f007f4 100644 --- a/src/gui/model/parent-track.cpp +++ b/src/gui/model/parent-track.cpp @@ -23,6 +23,8 @@ #include "parent-track.hpp" #include +using namespace boost; + namespace gui { namespace model { @@ -70,5 +72,24 @@ ParentTrack::remove_descendant_track(const boost::shared_ptr track) return false; } +boost::shared_ptr +ParentTrack::find_descendant_track_parent( + boost::shared_ptr child) +{ + REQUIRE(child != NULL); + BOOST_FOREACH(shared_ptr track, tracks) + { + if(track == child) + return shared_from_this(); + + shared_ptr result = + track->find_descendant_track_parent(child); + if(result) + return result; + } + + return shared_ptr(); +} + } // namespace model } // namespace gui diff --git a/src/gui/model/parent-track.hpp b/src/gui/model/parent-track.hpp index d3ff1878d..5ef2c0265 100644 --- a/src/gui/model/parent-track.hpp +++ b/src/gui/model/parent-track.hpp @@ -38,7 +38,9 @@ namespace model { * ParentTrack is the abstract base class of all tracks that can parent * children. **/ -class ParentTrack : public Track +class ParentTrack : + public Track, + public boost::enable_shared_from_this { protected: /** @@ -74,6 +76,9 @@ public: * @return Returns true if the track was successfully removed. **/ bool remove_descendant_track(const boost::shared_ptr track); + + boost::shared_ptr + find_descendant_track_parent(boost::shared_ptr child); protected: /** diff --git a/src/gui/model/track.cpp b/src/gui/model/track.cpp index 2cb517447..f2c52b19d 100644 --- a/src/gui/model/track.cpp +++ b/src/gui/model/track.cpp @@ -73,29 +73,10 @@ Track::print_branch() return print_branch_recursive(0); } -shared_ptr -Track::find_parent(shared_ptr root, shared_ptr child) +boost::shared_ptr +Track::find_descendant_track_parent( + boost::shared_ptr /*child*/) { - REQUIRE(root != NULL); - REQUIRE(child != NULL); - const list< shared_ptr > children = root->get_child_tracks(); - BOOST_FOREACH(shared_ptr track, children) - { - if(track == child) - return root; - - shared_ptr parent_track = - dynamic_pointer_cast( - track); - if(parent_track) - { - shared_ptr result = find_parent( - parent_track, child); - if(result) - return result; - } - } - return shared_ptr(); } diff --git a/src/gui/model/track.hpp b/src/gui/model/track.hpp index 3f5c2cdf1..7bc0e7c45 100644 --- a/src/gui/model/track.hpp +++ b/src/gui/model/track.hpp @@ -78,6 +78,16 @@ public: **/ virtual bool remove_descendant_track( const boost::shared_ptr track); + + /** + * A utility function that attempts to find the parent of a track by + * searching through the tree from this track downward. + * @param child The child track to find the parent of. + * @return Returns the parent track if one was found, or an empty + * shared_ptr if none was found. + **/ + virtual boost::shared_ptr + find_descendant_track_parent(boost::shared_ptr child); /** * A debugging helper function that prints this track, and all it's @@ -92,21 +102,7 @@ public: * @return Returns the human readable string. **/ virtual std::string print_track() = 0; - -public: - - /** - * A utility function that attempts to find the parent of a track by - * searching through the tree from a root downward. - * @param root The root track to begin searching down from. - * @param child The child track to find the parent of. - * @return Returns the parent track if one was found, or an empty - * shared_ptr if none was found. - **/ - static boost::shared_ptr - find_parent(boost::shared_ptr root, - boost::shared_ptr child); - + protected: /** * The internal implementation of print_branch. diff --git a/src/gui/widgets/timeline/timeline-layout-helper.cpp b/src/gui/widgets/timeline/timeline-layout-helper.cpp index f05b63155..a67fd94bd 100644 --- a/src/gui/widgets/timeline/timeline-layout-helper.cpp +++ b/src/gui/widgets/timeline/timeline-layout-helper.cpp @@ -563,8 +563,8 @@ TimelineLayoutHelper::apply_drop_to_model_tree( // Detach the track from the old parent shared_ptr old_parent = - model::Track::find_parent( - timelineWidget.sequence, dragging_track); + timelineWidget.sequence->find_descendant_track_parent( + dragging_track); REQUIRE(old_parent); // The track must have a parent old_parent->get_child_track_list().remove(dragging_track); @@ -572,8 +572,8 @@ TimelineLayoutHelper::apply_drop_to_model_tree( { // Find the new parent track shared_ptr new_parent = - model::Track::find_parent( - timelineWidget.sequence, target_track); + timelineWidget.sequence->find_descendant_track_parent( + target_track); REQUIRE(new_parent); // The track must have a parent // Find the destination point