various: extended marker and timeline functionality

This commit is contained in:
itsmattkc
2022-05-01 21:17:22 -07:00
parent cdcd83c09a
commit 6a0b5856c9
23 changed files with 781 additions and 240 deletions
+152 -14
View File
@@ -20,18 +20,20 @@
#include "timelinemarker.h"
#include "common/qtutils.h"
#include "common/xmlutils.h"
#include "config/config.h"
#include "core.h"
#include "ui/colorcoding.h"
namespace olive {
TimelineMarker::TimelineMarker(int color, const TimeRange &time, const QString &name, QObject *parent) :
QObject(parent),
time_(time),
name_(name),
color_(color)
{
setParent(parent);
}
void TimelineMarker::set_time(const TimeRange &time)
@@ -40,6 +42,17 @@ void TimelineMarker::set_time(const TimeRange &time)
emit TimeChanged(time_);
}
void TimelineMarker::set_time(const rational &time)
{
set_time(TimeRange(time, time + time_.length()));
}
bool TimelineMarker::has_sibling_at_time(const rational &t) const
{
TimelineMarker *m = static_cast<TimelineMarkerList*>(parent())->GetMarkerAtTime(t);
return m && m != this;
}
void TimelineMarker::set_name(const QString &name)
{
name_ = name;
@@ -49,13 +62,65 @@ void TimelineMarker::set_name(const QString &name)
void TimelineMarker::set_color(int c)
{
color_ = c;
emit ColorChanged(color_);
}
const std::vector<TimelineMarker*> &TimelineMarkerList::list() const
int TimelineMarker::GetMarkerHeight(const QFontMetrics &fm)
{
return markers_;
return fm.height();
}
QRect TimelineMarker::Draw(QPainter *p, const QPoint &pt, double scale, bool selected)
{
QFontMetrics fm = p->fontMetrics();
int marker_height = GetMarkerHeight(fm);
int marker_width = QtUtils::QFontMetricsWidth(fm, QStringLiteral("H"));
int half_width = marker_width / 2;
QColor c = ColorCoding::GetColor(color()).toQColor();
if (selected) {
p->setPen(Qt::white);
p->setBrush(c.lighter());
} else {
p->setPen(Qt::black);
p->setBrush(c);
}
int top = pt.y() - marker_height;
if (time_.out() != time_.in()) {
QRect marker_rect(pt.x(), top, time_.length().toDouble() * scale, marker_height);
p->drawRect(marker_rect);
if (!name_.isEmpty()) {
p->setPen(ColorCoding::GetUISelectorColor(ColorCoding::GetColor(color_)));
p->drawText(marker_rect.adjusted(marker_width/4, 0, 0, 0), name_, Qt::AlignLeft | Qt::AlignVCenter);
}
return marker_rect;
} else {
int half_marker_height = marker_height / 3;
int left = pt.x() - half_width;
int right = pt.x() + half_width;
int center_y = pt.y() - half_marker_height;
QPoint points[] = {
pt,
QPoint(left, center_y),
QPoint(left, top),
QPoint(right, top),
QPoint(right, center_y),
pt,
};
p->setRenderHint(QPainter::Antialiasing);
p->drawPolygon(points, 6);
return QRect(left, top, marker_width, marker_height);
}
}
void TimelineMarkerList::childEvent(QChildEvent *e)
@@ -64,20 +129,93 @@ void TimelineMarkerList::childEvent(QChildEvent *e)
if (TimelineMarker *marker = dynamic_cast<TimelineMarker *>(e->child())) {
if (e->type() == QChildEvent::ChildAdded) {
markers_.push_back(marker);
connect(marker, &TimelineMarker::TimeChanged, this, &TimelineMarkerList::HandleMarkerTimeChange);
connect(marker, &TimelineMarker::TimeChanged, this, &TimelineMarkerList::HandleMarkerModification);
connect(marker, &TimelineMarker::NameChanged, this, &TimelineMarkerList::HandleMarkerModification);
connect(marker, &TimelineMarker::ColorChanged, this, &TimelineMarkerList::HandleMarkerModification);
InsertIntoList(marker);
emit MarkerAdded(marker);
} else if (e->type() == QChildEvent::ChildRemoved) {
auto it = std::find(markers_.begin(), markers_.end(), marker);
if (it != markers_.end()) {
markers_.erase(it);
}
RemoveFromList(marker);
disconnect(marker, &TimelineMarker::TimeChanged, this, &TimelineMarkerList::HandleMarkerTimeChange);
disconnect(marker, &TimelineMarker::TimeChanged, this, &TimelineMarkerList::HandleMarkerModification);
disconnect(marker, &TimelineMarker::NameChanged, this, &TimelineMarkerList::HandleMarkerModification);
disconnect(marker, &TimelineMarker::ColorChanged, this, &TimelineMarkerList::HandleMarkerModification);
emit MarkerRemoved(marker);
}
}
}
MarkerAddCommand::MarkerAddCommand(TimelineMarkerList *marker_list, const TimeRange &range, const QString &name, int color) :
marker_list_(marker_list)
void TimelineMarkerList::InsertIntoList(TimelineMarker *marker)
{
added_marker_ = new TimelineMarker(color, range, name, &memory_manager_);
// Insertion sort by time to allow some loop optimizations
bool found = false;
for (auto it=markers_.begin(); it!=markers_.end(); it++) {
TimelineMarker *m = *it;
Q_ASSERT(m->time() != marker->time());
if (m->time() > marker->time()) {
markers_.insert(it, marker);
found = true;
break;
}
}
if (!found) {
markers_.push_back(marker);
}
}
bool TimelineMarkerList::RemoveFromList(TimelineMarker *marker)
{
auto it = std::find(markers_.begin(), markers_.end(), marker);
if (it != markers_.end()) {
markers_.erase(it);
return true;
}
return false;
}
void TimelineMarkerList::HandleMarkerModification()
{
emit MarkerModified(static_cast<TimelineMarker*>(sender()));
}
void TimelineMarkerList::HandleMarkerTimeChange()
{
TimelineMarker *m = static_cast<TimelineMarker*>(sender());
auto it = std::find(markers_.begin(), markers_.end(), m);
if ((it+1 != markers_.end() && (*(it+1))->time() < m->time())
|| (it != markers_.begin() && (*(it-1))->time() > m->time())) {
// Re-sort into list
markers_.erase(it);
InsertIntoList(m);
}
}
MarkerAddCommand::MarkerAddCommand(TimelineMarkerList *marker_list, const TimeRange &range, const QString &name, int color) :
MarkerAddCommand(marker_list, new TimelineMarker(color, range, name, &memory_manager_))
{
}
MarkerAddCommand::MarkerAddCommand(TimelineMarkerList *marker_list, TimelineMarker *marker) :
marker_list_(marker_list),
added_marker_(marker)
{
added_marker_->setParent(&memory_manager_);
}
Project* MarkerAddCommand::GetRelevantProject() const
@@ -102,7 +240,7 @@ MarkerRemoveCommand::MarkerRemoveCommand(TimelineMarker *marker) :
Project* MarkerRemoveCommand::GetRelevantProject() const
{
return Project::GetProjectFromObject(marker_list_);
return Project::GetProjectFromObject(marker_);
}
void MarkerRemoveCommand::redo()
@@ -173,7 +311,7 @@ Project* MarkerChangeTimeCommand::GetRelevantProject() const
void MarkerChangeTimeCommand::redo()
{
old_time_ = marker_->time();
old_time_ = marker_->time_range();
marker_->set_time(new_time_);
}