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_);
}
+65 -2
View File
@@ -21,6 +21,7 @@
#ifndef TIMELINEMARKER_H
#define TIMELINEMARKER_H
#include <QPainter>
#include <QString>
#include <QXmlStreamReader>
#include <QXmlStreamWriter>
@@ -36,8 +37,12 @@ class TimelineMarker : public QObject
public:
TimelineMarker(int color, const TimeRange& time, const QString& name = QString(), QObject* parent = nullptr);
const TimeRange &time() const { return time_; }
const rational &time() const { return time_.in(); }
const TimeRange &time_range() const { return time_; }
void set_time(const TimeRange& time);
void set_time(const rational& time);
bool has_sibling_at_time(const rational &t) const;
const QString& name() const { return name_; }
void set_name(const QString& name);
@@ -45,6 +50,9 @@ public:
int color() const { return color_; }
void set_color(int c);
static int GetMarkerHeight(const QFontMetrics &fm);
QRect Draw(QPainter *p, const QPoint &pt, double scale, bool selected);
signals:
void TimeChanged(const TimeRange& time);
@@ -70,24 +78,79 @@ public:
{
}
const std::vector<TimelineMarker *> &list() const;
inline bool empty() const { return markers_.empty(); }
inline std::vector<TimelineMarker*>::iterator begin() { return markers_.begin(); }
inline std::vector<TimelineMarker*>::iterator end() { return markers_.end(); }
inline std::vector<TimelineMarker*>::const_iterator cbegin() const { return markers_.cbegin(); }
inline std::vector<TimelineMarker*>::const_iterator cend() const { return markers_.cend(); }
inline TimelineMarker *back() const { return markers_.back(); }
inline TimelineMarker *front() const { return markers_.front(); }
inline size_t size() const { return markers_.size(); }
TimelineMarker *GetMarkerAtTime(const rational &t) const
{
for (auto it=markers_.cbegin(); it!=markers_.cend(); it++) {
TimelineMarker *m = *it;
if (m->time() == t) {
return m;
}
}
return nullptr;
}
TimelineMarker *GetClosestMarkerToTime(const rational &t) const
{
TimelineMarker *closest = nullptr;
for (auto it=markers_.cbegin(); it!=markers_.cend(); it++) {
TimelineMarker *m = *it;
rational this_diff = qAbs(m->time() - t);
if (closest) {
rational stored_diff = qAbs(closest->time() - t);
if (this_diff > stored_diff) {
// Since the list is organized by time, if the diff increases, assume we are only going
// to move further away from here and there's no need to check
break;
}
}
closest = m;
}
return closest;
}
signals:
void MarkerAdded(TimelineMarker* marker);
void MarkerRemoved(TimelineMarker* marker);
void MarkerModified(TimelineMarker* marker);
protected:
virtual void childEvent(QChildEvent *e) override;
private:
void InsertIntoList(TimelineMarker *m);
bool RemoveFromList(TimelineMarker *m);
std::vector<TimelineMarker*> markers_;
private slots:
void HandleMarkerModification();
void HandleMarkerTimeChange();
};
class MarkerAddCommand : public UndoCommand {
public:
MarkerAddCommand(TimelineMarkerList* marker_list, const TimeRange& range, const QString& name, int color);
MarkerAddCommand(TimelineMarkerList* marker_list, TimelineMarker *marker);
virtual Project* GetRelevantProject() const override;