2008-12-30 23:27:09 +01:00
|
|
|
/*
|
|
|
|
|
timeline-layout-helper.cpp - Implementation of the timeline
|
|
|
|
|
layout helper class
|
|
|
|
|
|
|
|
|
|
Copyright (C) Lumiera.org
|
|
|
|
|
2008, Joel Holdsworth <joel@airwebreathe.org.uk>
|
|
|
|
|
|
|
|
|
|
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., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
|
|
|
|
|
|
* *****************************************************/
|
|
|
|
|
|
|
|
|
|
#include <boost/foreach.hpp>
|
|
|
|
|
|
|
|
|
|
#include "timeline-layout-helper.hpp"
|
2008-12-31 18:05:32 +01:00
|
|
|
#include "../timeline-widget.hpp"
|
|
|
|
|
#include "../../model/sequence.hpp"
|
2008-12-30 23:27:09 +01:00
|
|
|
|
|
|
|
|
using namespace Gtk;
|
|
|
|
|
using namespace std;
|
|
|
|
|
using namespace boost;
|
2008-12-31 18:05:32 +01:00
|
|
|
using namespace lumiera;
|
2009-01-01 19:43:46 +01:00
|
|
|
using namespace util;
|
2008-12-30 23:27:09 +01:00
|
|
|
|
|
|
|
|
namespace gui {
|
|
|
|
|
namespace widgets {
|
|
|
|
|
namespace timeline {
|
2008-12-31 18:05:32 +01:00
|
|
|
|
2009-01-03 17:06:30 +01:00
|
|
|
const int TimelineLayoutHelper::AnimationTimeout = 20; // 20ms
|
|
|
|
|
|
2009-01-01 19:43:46 +01:00
|
|
|
TimelineLayoutHelper::TimelineLayoutHelper(TimelineWidget &owner) :
|
|
|
|
|
timelineWidget(owner),
|
2009-01-03 17:06:30 +01:00
|
|
|
totalHeight(0),
|
|
|
|
|
animation_state(Track::NoAnimationState)
|
2008-12-31 18:05:32 +01:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
TimelineLayoutHelper::clone_tree_from_sequence()
|
|
|
|
|
{
|
|
|
|
|
const shared_ptr<model::Sequence> &sequence = timelineWidget.sequence;
|
|
|
|
|
REQUIRE(sequence);
|
|
|
|
|
|
|
|
|
|
layoutTree.clear();
|
2009-01-01 19:43:46 +01:00
|
|
|
TrackTree::iterator_base iterator = layoutTree.set_head(sequence);
|
2008-12-31 18:05:32 +01:00
|
|
|
add_branch(iterator, sequence);
|
|
|
|
|
}
|
|
|
|
|
|
2009-01-01 19:43:46 +01:00
|
|
|
TimelineLayoutHelper::TrackTree&
|
|
|
|
|
TimelineLayoutHelper::get_layout_tree()
|
|
|
|
|
{
|
|
|
|
|
return layoutTree;
|
|
|
|
|
}
|
|
|
|
|
|
2008-12-31 18:05:32 +01:00
|
|
|
void
|
|
|
|
|
TimelineLayoutHelper::add_branch(
|
2009-01-01 14:31:34 +01:00
|
|
|
TrackTree::iterator_base parent_iterator,
|
2008-12-31 18:05:32 +01:00
|
|
|
shared_ptr<model::Track> parent)
|
|
|
|
|
{
|
|
|
|
|
BOOST_FOREACH(shared_ptr<model::Track> child,
|
|
|
|
|
parent->get_child_tracks())
|
|
|
|
|
{
|
2009-01-01 14:31:34 +01:00
|
|
|
TrackTree::iterator_base child_iterator =
|
2008-12-31 18:05:32 +01:00
|
|
|
layoutTree.append_child(parent_iterator, child);
|
|
|
|
|
add_branch(child_iterator, child);
|
|
|
|
|
}
|
|
|
|
|
}
|
2008-12-30 23:27:09 +01:00
|
|
|
|
2009-01-01 19:43:46 +01:00
|
|
|
optional<Gdk::Rectangle>
|
|
|
|
|
TimelineLayoutHelper::get_track_header_rect(
|
|
|
|
|
boost::weak_ptr<timeline::Track> track)
|
|
|
|
|
{
|
|
|
|
|
if(contains(headerBoxes, track))
|
|
|
|
|
{
|
|
|
|
|
Gdk::Rectangle rect(headerBoxes[track]);
|
|
|
|
|
rect.set_y(rect.get_y() - timelineWidget.get_y_scroll_offset());
|
|
|
|
|
return optional<Gdk::Rectangle>(rect);
|
|
|
|
|
}
|
|
|
|
|
return optional<Gdk::Rectangle>();
|
|
|
|
|
}
|
|
|
|
|
|
2009-01-02 13:43:21 +01:00
|
|
|
shared_ptr<timeline::Track>
|
2009-01-02 16:25:45 +01:00
|
|
|
TimelineLayoutHelper::header_from_point(Gdk::Point point)
|
2009-01-01 19:43:46 +01:00
|
|
|
{
|
2009-01-02 16:25:45 +01:00
|
|
|
// Apply the scroll offset
|
|
|
|
|
point.set_y(point.get_y() + timelineWidget.get_y_scroll_offset());
|
|
|
|
|
|
|
|
|
|
// Search the headers
|
2009-01-01 19:43:46 +01:00
|
|
|
std::pair<weak_ptr<timeline::Track>, Gdk::Rectangle> pair;
|
|
|
|
|
BOOST_FOREACH( pair, headerBoxes )
|
|
|
|
|
{
|
|
|
|
|
// Hit test the rectangle
|
|
|
|
|
const Gdk::Rectangle &rect = pair.second;
|
|
|
|
|
|
|
|
|
|
if(point.get_x() >= rect.get_x() &&
|
|
|
|
|
point.get_x() < rect.get_x() + rect.get_width() &&
|
|
|
|
|
point.get_y() >= rect.get_y() &&
|
|
|
|
|
point.get_y() < rect.get_y() + rect.get_height())
|
2009-01-02 13:43:21 +01:00
|
|
|
return shared_ptr<timeline::Track>(pair.first);
|
2009-01-01 19:43:46 +01:00
|
|
|
}
|
2009-01-02 12:39:45 +01:00
|
|
|
|
|
|
|
|
// No track was found - return an empty pointer
|
2009-01-01 19:43:46 +01:00
|
|
|
return shared_ptr<timeline::Track>();
|
|
|
|
|
}
|
|
|
|
|
|
2009-01-02 13:41:30 +01:00
|
|
|
boost::shared_ptr<timeline::Track>
|
2009-01-02 16:25:45 +01:00
|
|
|
TimelineLayoutHelper::track_from_y(int y)
|
2009-01-02 13:17:19 +01:00
|
|
|
{
|
2009-01-02 16:25:45 +01:00
|
|
|
// Apply the scroll offset
|
|
|
|
|
y += timelineWidget.get_y_scroll_offset();
|
|
|
|
|
|
|
|
|
|
// Search the tracks
|
2009-01-02 13:17:19 +01:00
|
|
|
std::pair<weak_ptr<timeline::Track>, Gdk::Rectangle> pair;
|
|
|
|
|
BOOST_FOREACH( pair, headerBoxes )
|
|
|
|
|
{
|
|
|
|
|
// Hit test the rectangle
|
|
|
|
|
const Gdk::Rectangle &rect = pair.second;
|
|
|
|
|
if(y >= rect.get_y() && y < rect.get_y() + rect.get_height())
|
2009-01-02 13:41:30 +01:00
|
|
|
return shared_ptr<timeline::Track>(pair.first);
|
2009-01-02 13:17:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// No track was found - return an empty pointer
|
|
|
|
|
return shared_ptr<timeline::Track>();
|
|
|
|
|
}
|
|
|
|
|
|
2009-01-01 19:43:46 +01:00
|
|
|
int
|
|
|
|
|
TimelineLayoutHelper::get_total_height() const
|
|
|
|
|
{
|
|
|
|
|
ENSURE(totalHeight >= 0);
|
|
|
|
|
return totalHeight;
|
|
|
|
|
}
|
|
|
|
|
|
2009-01-01 14:31:34 +01:00
|
|
|
void
|
|
|
|
|
TimelineLayoutHelper::update_layout()
|
2009-01-03 17:06:30 +01:00
|
|
|
{
|
|
|
|
|
// Reset the animation state value, before it gets recalculated
|
|
|
|
|
animation_state = Track::NoAnimationState;
|
2009-01-01 14:31:34 +01:00
|
|
|
|
|
|
|
|
// Clear previously cached layout
|
|
|
|
|
headerBoxes.clear();
|
2009-01-01 19:43:46 +01:00
|
|
|
|
2009-01-02 14:18:09 +01:00
|
|
|
// Do the layout
|
|
|
|
|
const int header_width = TimelineWidget::HeaderWidth;
|
|
|
|
|
const int indent_width = TimelineWidget::HeaderIndentWidth;
|
2009-01-04 21:29:05 +01:00
|
|
|
totalHeight = layout_headers_recursive(layoutTree.begin(),
|
|
|
|
|
0, header_width, indent_width, 0, true);
|
2009-01-02 14:03:00 +01:00
|
|
|
|
2009-01-02 14:18:09 +01:00
|
|
|
// Signal that the layout has changed
|
2009-01-02 14:03:00 +01:00
|
|
|
timelineWidget.on_layout_changed();
|
2009-01-03 17:06:30 +01:00
|
|
|
|
|
|
|
|
// Begin animating as necessary
|
|
|
|
|
if(animation_state != Track::NoAnimationState && !animationTimer)
|
|
|
|
|
begin_animation();
|
2009-01-01 14:31:34 +01:00
|
|
|
}
|
|
|
|
|
|
2009-01-04 21:29:05 +01:00
|
|
|
int
|
2009-01-01 14:31:34 +01:00
|
|
|
TimelineLayoutHelper::layout_headers_recursive(
|
|
|
|
|
TrackTree::iterator_base parent_iterator,
|
2009-01-04 21:29:05 +01:00
|
|
|
const int branch_offset, const int header_width,
|
|
|
|
|
const int indent_width, const int depth, const bool parent_expanded)
|
2009-01-01 14:31:34 +01:00
|
|
|
{
|
|
|
|
|
REQUIRE(depth >= 0);
|
|
|
|
|
|
2009-01-04 21:29:05 +01:00
|
|
|
int child_offset = 0;
|
|
|
|
|
|
2009-01-01 14:31:34 +01:00
|
|
|
TrackTree::sibling_iterator iterator;
|
|
|
|
|
for(iterator = layoutTree.begin(parent_iterator);
|
|
|
|
|
iterator != layoutTree.end(parent_iterator);
|
|
|
|
|
iterator++)
|
|
|
|
|
{
|
|
|
|
|
const shared_ptr<model::Track> &model_track = *iterator;
|
|
|
|
|
REQUIRE(model_track);
|
|
|
|
|
|
|
|
|
|
shared_ptr<timeline::Track> timeline_track =
|
2009-01-03 17:06:30 +01:00
|
|
|
lookup_timeline_track(model_track);
|
|
|
|
|
|
|
|
|
|
// Is the track animating?
|
2009-01-04 21:29:05 +01:00
|
|
|
const int track_animation_state =
|
2009-01-03 17:06:30 +01:00
|
|
|
timeline_track->get_expand_animation_state();
|
|
|
|
|
|
|
|
|
|
// Is the track going to be shown?
|
2009-01-01 14:31:34 +01:00
|
|
|
if(parent_expanded)
|
|
|
|
|
{
|
|
|
|
|
// Calculate and store the box of the header
|
|
|
|
|
const int track_height = timeline_track->get_height();
|
|
|
|
|
const int indent = depth * indent_width;
|
|
|
|
|
|
|
|
|
|
headerBoxes[timeline_track] = Gdk::Rectangle(
|
|
|
|
|
indent, // x
|
2009-01-04 21:29:05 +01:00
|
|
|
branch_offset + child_offset, // y
|
2009-01-01 14:31:34 +01:00
|
|
|
max( header_width - indent, 0 ), // width
|
|
|
|
|
track_height); // height
|
|
|
|
|
|
|
|
|
|
// Offset for the next header
|
2009-01-04 21:29:05 +01:00
|
|
|
child_offset += track_height + TimelineWidget::TrackPadding;
|
2009-01-01 14:31:34 +01:00
|
|
|
}
|
2009-01-04 21:29:05 +01:00
|
|
|
|
2009-01-03 17:06:30 +01:00
|
|
|
// Recurse to children
|
|
|
|
|
const bool expand_child =
|
|
|
|
|
((animation_state != Track::NoAnimationState) ||
|
|
|
|
|
timeline_track->get_expanded())
|
|
|
|
|
&& parent_expanded;
|
2009-01-04 21:29:05 +01:00
|
|
|
|
|
|
|
|
const int branch_height = layout_headers_recursive(
|
|
|
|
|
iterator, branch_offset + child_offset,
|
2009-01-03 17:06:30 +01:00
|
|
|
header_width, indent_width, depth + 1, expand_child);
|
|
|
|
|
|
|
|
|
|
// Do collapse animation as necessary
|
2009-01-04 21:29:05 +01:00
|
|
|
if(track_animation_state != Track::NoAnimationState)
|
2009-01-03 17:06:30 +01:00
|
|
|
{
|
2009-01-04 21:29:05 +01:00
|
|
|
// Tick the track expand animation
|
2009-01-03 17:06:30 +01:00
|
|
|
timeline_track->tick_expand_animation();
|
2009-01-04 21:29:05 +01:00
|
|
|
|
|
|
|
|
// Calculate the height of te area which will be
|
|
|
|
|
// shown as expanded
|
|
|
|
|
const float a = ((float)track_animation_state /
|
|
|
|
|
(float)Track::MaxExpandAnimation);
|
|
|
|
|
child_offset += branch_height * a * a;
|
|
|
|
|
const int y_limit = branch_offset + child_offset;
|
2009-01-03 17:06:30 +01:00
|
|
|
|
2009-01-04 21:29:05 +01:00
|
|
|
// Obscure tracks according to the animation state
|
2009-01-03 17:06:30 +01:00
|
|
|
TrackTree::pre_order_iterator descendant_iterator(iterator);
|
|
|
|
|
descendant_iterator++;
|
2009-01-04 21:29:05 +01:00
|
|
|
TrackTree::sibling_iterator end(iterator);
|
|
|
|
|
end++;
|
2009-01-03 17:06:30 +01:00
|
|
|
|
|
|
|
|
for(descendant_iterator = layoutTree.begin(parent_iterator);
|
2009-01-04 21:29:05 +01:00
|
|
|
descendant_iterator != end;
|
2009-01-03 17:06:30 +01:00
|
|
|
descendant_iterator++)
|
|
|
|
|
{
|
2009-01-04 21:29:05 +01:00
|
|
|
const Gdk::Rectangle &rect = headerBoxes[
|
|
|
|
|
lookup_timeline_track(*descendant_iterator)];
|
|
|
|
|
|
|
|
|
|
if(rect.get_y() + rect.get_height() > y_limit)
|
2009-01-03 17:06:30 +01:00
|
|
|
headerBoxes.erase(track);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Make sure the global animation state includes this branch's
|
|
|
|
|
// animation state
|
2009-01-04 21:29:05 +01:00
|
|
|
animation_state = max(
|
|
|
|
|
animation_state, track_animation_state);
|
2009-01-03 17:06:30 +01:00
|
|
|
}
|
2009-01-04 21:29:05 +01:00
|
|
|
else // If no animation, just append the normal length
|
|
|
|
|
child_offset += branch_height;
|
2009-01-01 14:31:34 +01:00
|
|
|
}
|
2009-01-04 21:29:05 +01:00
|
|
|
|
|
|
|
|
return child_offset;
|
2009-01-01 14:31:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
shared_ptr<timeline::Track>
|
|
|
|
|
TimelineLayoutHelper::lookup_timeline_track(
|
|
|
|
|
shared_ptr<model::Track> model_track)
|
|
|
|
|
{
|
|
|
|
|
REQUIRE(model_track != NULL);
|
|
|
|
|
shared_ptr<timeline::Track> timeline_track =
|
|
|
|
|
timelineWidget.lookup_timeline_track(model_track);
|
|
|
|
|
ENSURE(timeline_track);
|
|
|
|
|
|
|
|
|
|
return timeline_track;
|
|
|
|
|
}
|
|
|
|
|
|
2009-01-03 17:06:30 +01:00
|
|
|
void
|
|
|
|
|
TimelineLayoutHelper::begin_animation()
|
|
|
|
|
{
|
|
|
|
|
animationTimer = Glib::signal_timeout().connect(
|
|
|
|
|
sigc::mem_fun(this, &TimelineLayoutHelper::on_animation_tick),
|
|
|
|
|
AnimationTimeout);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
|
TimelineLayoutHelper::on_animation_tick()
|
|
|
|
|
{
|
|
|
|
|
update_layout();
|
|
|
|
|
return animation_state != Track::NoAnimationState;
|
|
|
|
|
}
|
|
|
|
|
|
2008-12-30 23:27:09 +01:00
|
|
|
} // namespace timeline
|
|
|
|
|
} // namespace widgets
|
|
|
|
|
} // namespace gui
|