Refactored find_branch_parent

This commit is contained in:
Joel Holdsworth 2009-01-24 12:50:49 +00:00
parent e2992c62ef
commit ed1f4abfea
6 changed files with 46 additions and 42 deletions

View file

@ -36,6 +36,7 @@
#include <boost/shared_ptr.hpp> #include <boost/shared_ptr.hpp>
#include <boost/weak_ptr.hpp> #include <boost/weak_ptr.hpp>
#include <boost/scoped_ptr.hpp> #include <boost/scoped_ptr.hpp>
#include <boost/enable_shared_from_this.hpp>
#include "lib/util.hpp" #include "lib/util.hpp"
extern "C" { extern "C" {

View file

@ -23,6 +23,8 @@
#include "parent-track.hpp" #include "parent-track.hpp"
#include <boost/foreach.hpp> #include <boost/foreach.hpp>
using namespace boost;
namespace gui { namespace gui {
namespace model { namespace model {
@ -70,5 +72,24 @@ ParentTrack::remove_descendant_track(const boost::shared_ptr<Track> track)
return false; return false;
} }
boost::shared_ptr<ParentTrack>
ParentTrack::find_descendant_track_parent(
boost::shared_ptr<Track> child)
{
REQUIRE(child != NULL);
BOOST_FOREACH(shared_ptr<Track> track, tracks)
{
if(track == child)
return shared_from_this();
shared_ptr<ParentTrack> result =
track->find_descendant_track_parent(child);
if(result)
return result;
}
return shared_ptr<ParentTrack>();
}
} // namespace model } // namespace model
} // namespace gui } // namespace gui

View file

@ -38,7 +38,9 @@ namespace model {
* ParentTrack is the abstract base class of all tracks that can parent * ParentTrack is the abstract base class of all tracks that can parent
* children. * children.
**/ **/
class ParentTrack : public Track class ParentTrack :
public Track,
public boost::enable_shared_from_this<ParentTrack>
{ {
protected: protected:
/** /**
@ -74,6 +76,9 @@ public:
* @return Returns true if the track was successfully removed. * @return Returns true if the track was successfully removed.
**/ **/
bool remove_descendant_track(const boost::shared_ptr<Track> track); bool remove_descendant_track(const boost::shared_ptr<Track> track);
boost::shared_ptr<ParentTrack>
find_descendant_track_parent(boost::shared_ptr<Track> child);
protected: protected:
/** /**

View file

@ -73,29 +73,10 @@ Track::print_branch()
return print_branch_recursive(0); return print_branch_recursive(0);
} }
shared_ptr<ParentTrack> boost::shared_ptr<ParentTrack>
Track::find_parent(shared_ptr<ParentTrack> root, shared_ptr<Track> child) Track::find_descendant_track_parent(
boost::shared_ptr<Track> /*child*/)
{ {
REQUIRE(root != NULL);
REQUIRE(child != NULL);
const list< shared_ptr<Track> > children = root->get_child_tracks();
BOOST_FOREACH(shared_ptr<Track> track, children)
{
if(track == child)
return root;
shared_ptr<ParentTrack> parent_track =
dynamic_pointer_cast<model::ParentTrack, model::Track>(
track);
if(parent_track)
{
shared_ptr<ParentTrack> result = find_parent(
parent_track, child);
if(result)
return result;
}
}
return shared_ptr<ParentTrack>(); return shared_ptr<ParentTrack>();
} }

View file

@ -78,6 +78,16 @@ public:
**/ **/
virtual bool remove_descendant_track( virtual bool remove_descendant_track(
const boost::shared_ptr<Track> track); const boost::shared_ptr<Track> 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<ParentTrack>
find_descendant_track_parent(boost::shared_ptr<Track> child);
/** /**
* A debugging helper function that prints this track, and all it's * A debugging helper function that prints this track, and all it's
@ -92,21 +102,7 @@ public:
* @return Returns the human readable string. * @return Returns the human readable string.
**/ **/
virtual std::string print_track() = 0; 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<ParentTrack>
find_parent(boost::shared_ptr<ParentTrack> root,
boost::shared_ptr<Track> child);
protected: protected:
/** /**
* The internal implementation of print_branch. * The internal implementation of print_branch.

View file

@ -563,8 +563,8 @@ TimelineLayoutHelper::apply_drop_to_model_tree(
// Detach the track from the old parent // Detach the track from the old parent
shared_ptr<model::ParentTrack> old_parent = shared_ptr<model::ParentTrack> old_parent =
model::Track::find_parent( timelineWidget.sequence->find_descendant_track_parent(
timelineWidget.sequence, dragging_track); dragging_track);
REQUIRE(old_parent); // The track must have a parent REQUIRE(old_parent); // The track must have a parent
old_parent->get_child_track_list().remove(dragging_track); old_parent->get_child_track_list().remove(dragging_track);
@ -572,8 +572,8 @@ TimelineLayoutHelper::apply_drop_to_model_tree(
{ {
// Find the new parent track // Find the new parent track
shared_ptr<model::ParentTrack> new_parent = shared_ptr<model::ParentTrack> new_parent =
model::Track::find_parent( timelineWidget.sequence->find_descendant_track_parent(
timelineWidget.sequence, target_track); target_track);
REQUIRE(new_parent); // The track must have a parent REQUIRE(new_parent); // The track must have a parent
// Find the destination point // Find the destination point