moved timeline interactivity to separate classes
This commit is contained in:
@@ -31,6 +31,8 @@
|
||||
|
||||
TimelineView::TimelineView(QWidget *parent) :
|
||||
QGraphicsView(parent),
|
||||
pointer_tool_(this),
|
||||
import_tool_(this),
|
||||
playhead_(0),
|
||||
dragging_(false)
|
||||
{
|
||||
@@ -102,152 +104,49 @@ void TimelineView::SetTime(const int64_t time)
|
||||
|
||||
void TimelineView::mousePressEvent(QMouseEvent *event)
|
||||
{
|
||||
QGraphicsView::mousePressEvent(event);
|
||||
pointer_tool_.MousePress(event);
|
||||
}
|
||||
|
||||
void TimelineView::mouseMoveEvent(QMouseEvent *event)
|
||||
{
|
||||
QGraphicsView::mouseMoveEvent(event);
|
||||
|
||||
if (!dragging_) {
|
||||
// Let's see if there's anything selected to drag
|
||||
if (itemAt(event->pos()) != nullptr) {
|
||||
QList<QGraphicsItem*> selected_items = scene_.selectedItems();
|
||||
|
||||
foreach (QGraphicsItem* item, selected_items) {
|
||||
TimelineViewGhostItem* ghost = new TimelineViewGhostItem();
|
||||
TimelineViewClipItem* clip_item = static_cast<TimelineViewClipItem*>(item);
|
||||
|
||||
ClipBlock* clip = clip_item->clip();
|
||||
|
||||
ghost->SetIn(clip->in());
|
||||
ghost->SetOut(clip->out());
|
||||
ghost->SetScale(scale_);
|
||||
|
||||
ghost->setPos(clip_item->pos());
|
||||
|
||||
ghost_items_.append(ghost);
|
||||
|
||||
scene_.addItem(ghost);
|
||||
}
|
||||
|
||||
drag_start_ = event->pos();
|
||||
}
|
||||
|
||||
dragging_ = true;
|
||||
} else if (!ghost_items_.isEmpty()) {
|
||||
QPoint movement = event->pos() - drag_start_;
|
||||
|
||||
rational time_movement = ScreenToTime(movement.x());
|
||||
|
||||
// Validate movement
|
||||
foreach (TimelineViewGhostItem* ghost, ghost_items_) {
|
||||
rational validator = ghost->In() + time_movement;
|
||||
if (validator < 0) {
|
||||
time_movement = rational(0) - ghost->In();
|
||||
}
|
||||
}
|
||||
|
||||
// Perform movement
|
||||
foreach (TimelineViewGhostItem* ghost, ghost_items_) {
|
||||
ghost->SetInAdjustment(time_movement);
|
||||
ghost->SetOutAdjustment(time_movement);
|
||||
}
|
||||
}
|
||||
pointer_tool_.MouseMove(event);
|
||||
}
|
||||
|
||||
void TimelineView::mouseReleaseEvent(QMouseEvent *event)
|
||||
{
|
||||
QGraphicsView::mouseReleaseEvent(event);
|
||||
|
||||
ClearGhosts();
|
||||
|
||||
dragging_ = false;
|
||||
pointer_tool_.MouseRelease(event);
|
||||
}
|
||||
|
||||
void TimelineView::dragEnterEvent(QDragEnterEvent *event)
|
||||
{
|
||||
QStringList mime_formats = event->mimeData()->formats();
|
||||
|
||||
// Listen for MIME data from a ProjectViewModel
|
||||
if (mime_formats.contains("application/x-oliveprojectitemdata")) {
|
||||
// FIXME: Implement audio insertion
|
||||
|
||||
// Data is drag/drop data from a ProjectViewModel
|
||||
QByteArray model_data = event->mimeData()->data("application/x-oliveprojectitemdata");
|
||||
|
||||
// Use QDataStream to deserialize the data
|
||||
QDataStream stream(&model_data, QIODevice::ReadOnly);
|
||||
|
||||
// Variables to deserialize into
|
||||
quintptr item_ptr;
|
||||
int r;
|
||||
|
||||
while (!stream.atEnd()) {
|
||||
stream >> r >> item_ptr;
|
||||
|
||||
// Get Item object
|
||||
Item* item = reinterpret_cast<Item*>(item_ptr);
|
||||
|
||||
// Check if Item is Footage
|
||||
if (item->type() == Item::kFootage) {
|
||||
// If the Item is Footage, we can create a Ghost from it
|
||||
Footage* footage = static_cast<Footage*>(item);
|
||||
|
||||
StreamPtr stream = footage->stream(0);
|
||||
|
||||
TimelineViewGhostItem* ghost = new TimelineViewGhostItem();
|
||||
|
||||
rational footage_duration(stream->timebase().numerator() * stream->duration(),
|
||||
stream->timebase().denominator());
|
||||
|
||||
ghost->SetIn(0);
|
||||
ghost->SetOut(footage_duration);
|
||||
ghost->SetScale(scale_);
|
||||
ghost->SetStream(stream);
|
||||
|
||||
ghost_items_.append(ghost);
|
||||
|
||||
scene_.addItem(ghost);
|
||||
}
|
||||
}
|
||||
|
||||
event->accept();
|
||||
} else {
|
||||
// FIXME: Implement dropping from file
|
||||
event->ignore();
|
||||
}
|
||||
import_tool_.DragEnter(event);
|
||||
}
|
||||
|
||||
void TimelineView::dragMoveEvent(QDragMoveEvent *event)
|
||||
{
|
||||
if (ghost_items_.isEmpty()) {
|
||||
event->ignore();
|
||||
} else {
|
||||
event->accept();
|
||||
}
|
||||
import_tool_.DragMove(event);
|
||||
}
|
||||
|
||||
void TimelineView::dragLeaveEvent(QDragLeaveEvent *event)
|
||||
{
|
||||
if (ghost_items_.isEmpty()) {
|
||||
event->ignore();
|
||||
} else {
|
||||
ClearGhosts();
|
||||
|
||||
event->accept();
|
||||
}
|
||||
import_tool_.DragLeave(event);
|
||||
}
|
||||
|
||||
void TimelineView::dropEvent(QDropEvent *event)
|
||||
{
|
||||
if (ghost_items_.isEmpty()) {
|
||||
event->ignore();
|
||||
} else {
|
||||
ClearGhosts();
|
||||
import_tool_.DragDrop(event);
|
||||
}
|
||||
|
||||
event->accept();
|
||||
}
|
||||
void TimelineView::AddGhost(TimelineViewGhostItem *ghost)
|
||||
{
|
||||
ghost->SetScale(scale_);
|
||||
ghost_items_.append(ghost);
|
||||
scene_.addItem(ghost);
|
||||
}
|
||||
|
||||
bool TimelineView::HasGhosts()
|
||||
{
|
||||
return !ghost_items_.isEmpty();
|
||||
}
|
||||
|
||||
rational TimelineView::ScreenToTime(const int &x)
|
||||
@@ -269,3 +168,29 @@ void TimelineView::ClearGhosts()
|
||||
ghost_items_.clear();
|
||||
}
|
||||
}
|
||||
|
||||
TimelineView::Tool::Tool(TimelineView *parent) :
|
||||
parent_(parent)
|
||||
{
|
||||
}
|
||||
|
||||
TimelineView::Tool::~Tool(){}
|
||||
|
||||
void TimelineView::Tool::MousePress(QMouseEvent *){}
|
||||
|
||||
void TimelineView::Tool::MouseMove(QMouseEvent *){}
|
||||
|
||||
void TimelineView::Tool::MouseRelease(QMouseEvent *){}
|
||||
|
||||
void TimelineView::Tool::DragEnter(QDragEnterEvent *){}
|
||||
|
||||
void TimelineView::Tool::DragMove(QDragMoveEvent *){}
|
||||
|
||||
void TimelineView::Tool::DragLeave(QDragLeaveEvent *){}
|
||||
|
||||
void TimelineView::Tool::DragDrop(QDropEvent *){}
|
||||
|
||||
TimelineView *TimelineView::Tool::parent()
|
||||
{
|
||||
return parent_;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user