lumiera_/src/gui/widgets/timeline-widget.cpp

452 lines
10 KiB
C++
Raw Normal View History

2008-04-19 21:31:27 +02:00
/*
2008-05-31 14:22:15 +02:00
timeline-widget.cpp - Implementation of the timeline widget
2008-04-19 21:31:27 +02:00
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 "timeline-widget.hpp"
#include <boost/foreach.hpp>
2008-05-22 20:19:04 +02:00
using namespace Gtk;
2008-05-31 14:22:15 +02:00
using namespace std;
using namespace gui::widgets::timeline;
2008-05-22 20:19:04 +02:00
2008-04-19 21:31:27 +02:00
namespace gui {
namespace widgets {
const int TimelineWidget::TrackPadding = 1;
const int TimelineWidget::HeaderWidth = 150;
const double TimelineWidget::ZoomIncrement = 1.25;
const int64_t TimelineWidget::MaxScale = 30000000;
2008-05-22 20:19:04 +02:00
TimelineWidget::TimelineWidget() :
Table(2, 2),
2008-06-23 18:07:57 +02:00
timeOffset(0),
timeScale(1),
2008-05-31 14:22:15 +02:00
totalHeight(0),
horizontalAdjustment(0, 0, 0),
verticalAdjustment(0, 0, 0),
selectionStart(0),
selectionEnd(0),
playbackPeriodStart(0),
playbackPeriodEnd(0),
2008-10-07 22:17:29 +02:00
playbackPoint(GAVL_TIME_UNDEFINED),
hoveringTrack(NULL),
2008-05-22 20:19:04 +02:00
horizontalScroll(horizontalAdjustment),
verticalScroll(verticalAdjustment)
2008-06-19 22:57:53 +02:00
{
body = new TimelineBody(this);
ENSURE(body != NULL);
headerContainer = new TimelineHeaderContainer(this);
ENSURE(headerContainer != NULL);
ruler = new TimelineRuler(this);
ENSURE(ruler != NULL);
2008-06-19 22:57:53 +02:00
horizontalAdjustment.signal_value_changed().connect( sigc::mem_fun(
this, &TimelineWidget::on_scroll) );
verticalAdjustment.signal_value_changed().connect( sigc::mem_fun(
this, &TimelineWidget::on_scroll) );
body->signal_motion_notify_event().connect( sigc::mem_fun(
this, &TimelineWidget::on_motion_in_body_notify_event) );
2008-07-24 00:23:48 +02:00
set_time_scale(GAVL_TIME_SCALE / 200);
set_selection(2000000, 4000000);
2008-10-18 00:36:37 +02:00
tracks.push_back(&video1);
video1.add_child_track(&video1a);
video1.add_child_track(&video1b);
video1b.add_child_track(&video1ba);
2008-10-18 00:36:37 +02:00
tracks.push_back(&video2);
update_tracks();
2008-06-19 22:57:53 +02:00
attach(*body, 1, 2, 1, 2, FILL|EXPAND, FILL|EXPAND);
attach(*ruler, 1, 2, 0, 1, FILL|EXPAND, SHRINK);
2008-06-19 22:57:53 +02:00
attach(*headerContainer, 0, 1, 1, 2, SHRINK, FILL|EXPAND);
attach(horizontalScroll, 1, 2, 2, 3, FILL|EXPAND, SHRINK);
attach(verticalScroll, 2, 3, 1, 2, SHRINK, FILL|EXPAND);
set_tool(timeline::Arrow);
2008-06-19 22:57:53 +02:00
}
2008-05-31 14:22:15 +02:00
TimelineWidget::~TimelineWidget()
2008-06-19 22:57:53 +02:00
{
REQUIRE(body != NULL);
if(body != NULL)
body->unreference();
REQUIRE(headerContainer != NULL);
if(headerContainer != NULL)
headerContainer->unreference();
REQUIRE(ruler != NULL);
if(ruler != NULL)
ruler->unreference();
2008-06-19 22:57:53 +02:00
}
2008-05-31 14:22:15 +02:00
/* ===== Data Access ===== */
gavl_time_t
TimelineWidget::get_time_offset() const
{
2008-06-23 18:07:57 +02:00
return timeOffset;
}
void
TimelineWidget::set_time_offset(gavl_time_t time_offset)
{
REQUIRE(ruler != NULL);
2008-06-23 18:07:57 +02:00
timeOffset = time_offset;
horizontalAdjustment.set_value(time_offset);
viewChangedSignal.emit();
}
int64_t
TimelineWidget::get_time_scale() const
{
return timeScale;
}
void
TimelineWidget::set_time_scale(int64_t time_scale)
{
REQUIRE(ruler != NULL);
timeScale = time_scale;
const int view_width = body->get_allocation().get_width();
horizontalAdjustment.set_page_size(timeScale * view_width);
viewChangedSignal.emit();
}
void
TimelineWidget::zoom_view(int zoom_size)
{
const Allocation allocation = body->get_allocation();
zoom_view(allocation.get_width() / 2, zoom_size);
}
void
TimelineWidget::zoom_view(int point, int zoom_size)
{
int64_t new_time_scale = (double)timeScale * pow(1.25, -zoom_size);
// Limit zooming in too close
if(new_time_scale < 1) new_time_scale = 1;
// Nudge zoom problems caused by integer rounding
if(new_time_scale == timeScale && zoom_size < 0)
new_time_scale++;
// Limit zooming out too far
if(new_time_scale > MaxScale)
new_time_scale = MaxScale;
// The view must be shifted so that the zoom is centred on the cursor
set_time_offset(get_time_offset() +
(timeScale - new_time_scale) * point);
// Apply the new scale
set_time_scale(new_time_scale);
}
void
TimelineWidget::shift_view(int shift_size)
{
const int view_width = body->get_allocation().get_width();
set_time_offset(get_time_offset() +
shift_size * timeScale * view_width / 256);
}
gavl_time_t
TimelineWidget::get_selection_start() const
{
return selectionStart;
}
gavl_time_t
TimelineWidget::get_selection_end() const
{
return selectionEnd;
}
void
TimelineWidget::set_selection(gavl_time_t start, gavl_time_t end,
bool reset_playback_period)
{
REQUIRE(ruler != NULL);
REQUIRE(body != NULL);
if(start < end)
{
selectionStart = start;
selectionEnd = end;
}
else
{
// The selection is back-to-front, flip it round
selectionStart = end;
selectionEnd = start;
}
if(reset_playback_period)
{
playbackPeriodStart = selectionStart;
playbackPeriodEnd = selectionEnd;
}
ruler->queue_draw();
body->queue_draw();
}
gavl_time_t
TimelineWidget::get_playback_period_start() const
{
return playbackPeriodStart;
}
gavl_time_t
TimelineWidget::get_playback_period_end() const
{
return playbackPeriodEnd;
}
void
TimelineWidget::set_playback_period(gavl_time_t start, gavl_time_t end)
{
REQUIRE(ruler != NULL);
REQUIRE(body != NULL);
if(start < end)
{
playbackPeriodStart = start;
playbackPeriodEnd = end;
}
else
{
// The period is back-to-front, flip it round
playbackPeriodStart = end;
playbackPeriodEnd = start;
}
ruler->queue_draw();
body->queue_draw();
}
2008-10-07 22:17:29 +02:00
void
TimelineWidget::set_playback_point(gavl_time_t point)
{
playbackPoint = point;
ruler->queue_draw();
body->queue_draw();
}
gavl_time_t
TimelineWidget::get_playback_point() const
{
return playbackPoint;
}
ToolType
TimelineWidget::get_tool() const
{
REQUIRE(body != NULL);
return body->get_tool();
}
void
TimelineWidget::set_tool(ToolType tool_type)
{
REQUIRE(body != NULL);
body->set_tool(tool_type);
}
timeline::Track*
TimelineWidget::get_hovering_track() const
{
return hoveringTrack;
}
/* ===== Signals ===== */
sigc::signal<void>
TimelineWidget::view_changed_signal() const
{
return viewChangedSignal;
}
2008-08-16 23:06:46 +02:00
sigc::signal<void, gavl_time_t>
TimelineWidget::mouse_hover_signal() const
{
return mouseHoverSignal;
}
2008-10-07 22:17:29 +02:00
sigc::signal<void>
TimelineWidget::playback_period_drag_released_signal() const
{
return playbackPeriodDragReleasedSignal;
}
sigc::signal<void, timeline::Track*>
TimelineWidget::hovering_track_changed_signal() const
{
return hoveringTrackChangedSignal;
}
/* ===== Events ===== */
2008-05-31 14:22:15 +02:00
void
TimelineWidget::on_scroll()
2008-06-19 22:57:53 +02:00
{
2008-06-23 18:07:57 +02:00
timeOffset = horizontalAdjustment.get_value();
viewChangedSignal.emit();
2008-06-19 22:57:53 +02:00
}
void
TimelineWidget::on_size_allocate(Allocation& allocation)
2008-06-19 22:57:53 +02:00
{
Widget::on_size_allocate(allocation);
2008-06-19 22:57:53 +02:00
update_scroll();
}
/* ===== Utilities ===== */
int
TimelineWidget::time_to_x(gavl_time_t time) const
{
return (int)((time - timeOffset) / timeScale);
}
gavl_time_t
TimelineWidget::x_to_time(int x) const
{
return (gavl_time_t)((int64_t)x * timeScale + timeOffset);
}
/* ===== Internals ===== */
2008-05-31 14:22:15 +02:00
void
TimelineWidget::update_tracks()
2008-06-19 22:57:53 +02:00
{
ASSERT(headerContainer != NULL);
headerContainer->update_headers();
// Recalculate the total height of the timeline scrolled area
totalHeight = 0;
BOOST_FOREACH( Track* track, tracks )
{
ASSERT(track != NULL);
totalHeight += measure_branch_height(track);
2008-06-19 22:57:53 +02:00
}
}
2008-05-31 14:22:15 +02:00
void
TimelineWidget::update_scroll()
2008-06-19 22:57:53 +02:00
{
ASSERT(body != NULL);
const Allocation body_allocation = body->get_allocation();
//----- Horizontal Scroll ------//
// TEST CODE
horizontalAdjustment.set_upper(1000 * GAVL_TIME_SCALE / 200);
horizontalAdjustment.set_lower(-1000 * GAVL_TIME_SCALE / 200);
// Set the page size
horizontalAdjustment.set_page_size(
timeScale * body_allocation.get_width());
2008-06-19 22:57:53 +02:00
//----- Vertical Scroll -----//
// Calculate the vertical length that can be scrolled:
// the total height of all the tracks minus one screenful
int y_scroll_length = totalHeight - body_allocation.get_height();
if(y_scroll_length < 0) y_scroll_length = 0;
// If by resizing we're now over-scrolled, scroll back to
// maximum distance
if((int)verticalAdjustment.get_value() > y_scroll_length)
verticalAdjustment.set_value(y_scroll_length);
verticalAdjustment.set_upper(y_scroll_length);
// Hide the scrollbar if no scrolling is possible
2008-07-24 00:23:48 +02:00
#if 0
// Having this code included seems to cause a layout loop as the
// window is shrunk
if(y_scroll_length <= 0 && verticalScroll.is_visible())
2008-06-19 22:57:53 +02:00
verticalScroll.hide();
2008-07-24 00:23:48 +02:00
else if(y_scroll_length > 0 && !verticalScroll.is_visible())
2008-06-19 22:57:53 +02:00
verticalScroll.show();
2008-07-24 00:23:48 +02:00
#endif
2008-10-18 00:36:37 +02:00
2008-06-19 22:57:53 +02:00
}
2008-05-31 14:22:15 +02:00
int
TimelineWidget::measure_branch_height(Track* track)
{
REQUIRE(track != NULL);
int height = track->get_height();
// Recurse through all the children
BOOST_FOREACH( Track* child, track->get_child_tracks() )
height += measure_branch_height(child);
return height;
}
int
TimelineWidget::get_y_scroll_offset() const
2008-06-19 22:57:53 +02:00
{
return (int)verticalAdjustment.get_value();
}
2008-04-19 21:31:27 +02:00
bool
TimelineWidget::on_motion_in_body_notify_event(GdkEventMotion *event)
2008-06-25 21:23:53 +02:00
{
REQUIRE(event != NULL);
ruler->set_mouse_chevron_offset(event->x);
2008-08-16 23:06:46 +02:00
mouseHoverSignal.emit(x_to_time(event->x));
2008-10-18 00:36:37 +02:00
return true;
2008-06-25 21:23:53 +02:00
}
2008-10-07 22:17:29 +02:00
void
TimelineWidget::on_playback_period_drag_released()
{
playbackPeriodDragReleasedSignal.emit();
}
void
TimelineWidget::set_hovering_track(timeline::Track *hovering_track)
{
hoveringTrack = hovering_track;
hoveringTrackChangedSignal.emit(hovering_track);
}
2008-04-19 21:31:27 +02:00
} // namespace widgets
} // namespace gui