]> git.sesse.net Git - nageru/blobdiff - clip_list.cpp
Allow symlinked frame files. Useful for testing.
[nageru] / clip_list.cpp
index 69a6334cd473984d1b730998464c61cc5af422cc..14f083e91268984c043ac652cd74ff19b39cccc9 100644 (file)
@@ -1,13 +1,13 @@
+#include "clip_list.h"
+
 #include "mainwindow.h"
+#include "timebase.h"
+#include "ui_mainwindow.h"
 
 #include <math.h>
 #include <string>
 #include <vector>
 
-#include "clip_list.h"
-#include "timebase.h"
-#include "ui_mainwindow.h"
-
 using namespace std;
 
 string pts_to_string(int64_t pts)
@@ -40,27 +40,36 @@ string duration_to_string(int64_t pts_diff)
        return buf;
 }
 
-int ClipList::rowCount(const QModelIndex &parent) const {
-       if (parent.isValid()) return 0;
+int ClipList::rowCount(const QModelIndex &parent) const
+{
+       if (parent.isValid())
+               return 0;
        return clips.size();
 }
 
-int PlayList::rowCount(const QModelIndex &parent) const {
-       if (parent.isValid()) return 0;
+int PlayList::rowCount(const QModelIndex &parent) const
+{
+       if (parent.isValid())
+               return 0;
        return clips.size();
 }
 
-int ClipList::columnCount(const QModelIndex &parent) const {
-       if (parent.isValid()) return 0;
+int ClipList::columnCount(const QModelIndex &parent) const
+{
+       if (parent.isValid())
+               return 0;
        return int(Column::NUM_COLUMNS);
 }
 
-int PlayList::columnCount(const QModelIndex &parent) const {
-       if (parent.isValid()) return 0;
+int PlayList::columnCount(const QModelIndex &parent) const
+{
+       if (parent.isValid())
+               return 0;
        return int(Column::NUM_COLUMNS);
 }
 
-QVariant ClipList::data(const QModelIndex &parent, int role) const {
+QVariant ClipList::data(const QModelIndex &parent, int role) const
+{
        if (!parent.isValid())
                return QVariant();
        const int row = parent.row(), column = parent.column();
@@ -108,7 +117,8 @@ QVariant ClipList::data(const QModelIndex &parent, int role) const {
        }
 }
 
-QVariant PlayList::data(const QModelIndex &parent, int role) const {
+QVariant PlayList::data(const QModelIndex &parent, int role) const
+{
        if (!parent.isValid())
                return QVariant();
        const int row = parent.row(), column = parent.column();
@@ -122,6 +132,7 @@ QVariant PlayList::data(const QModelIndex &parent, int role) const {
                case Column::IN:
                case Column::OUT:
                case Column::DURATION:
+               case Column::FADE_TIME:
                        return Qt::AlignRight + Qt::AlignVCenter;
                case Column::CAMERA:
                        return Qt::AlignCenter;
@@ -131,7 +142,10 @@ QVariant PlayList::data(const QModelIndex &parent, int role) const {
        }
        if (role == Qt::BackgroundRole) {
                if (Column(column) == Column::PLAYING) {
-                       if (row == currently_playing_index) {
+                       auto it = current_progress.find(row);
+                       if (it != current_progress.end()) {
+                               double play_progress = it->second;
+
                                // This only really works well for the first column, for whatever odd Qt reason.
                                QLinearGradient grad(QPointF(0, 0), QPointF(1, 0));
                                grad.setCoordinateMode(grad.QGradient::ObjectBoundingMode);
@@ -154,7 +168,7 @@ QVariant PlayList::data(const QModelIndex &parent, int role) const {
 
        switch (Column(column)) {
        case Column::PLAYING:
-               return (row == currently_playing_index) ? "→" : "";
+               return current_progress.count(row) ? "→" : "";
        case Column::IN:
                return QString::fromStdString(pts_to_string(clips[row].pts_in));
        case Column::OUT:
@@ -173,12 +187,20 @@ QVariant PlayList::data(const QModelIndex &parent, int role) const {
                return qlonglong(clips[row].stream_idx + 1);
        case Column::DESCRIPTION:
                return QString::fromStdString(clips[row].descriptions[clips[row].stream_idx]);
+       case Column::FADE_TIME: {
+               stringstream ss;
+               ss.imbue(locale("C"));
+               ss.precision(3);
+               ss << fixed << clips[row].fade_time_seconds;
+               return QString::fromStdString(ss.str());
+       }
        default:
                return "";
        }
 }
 
-QVariant ClipList::headerData(int section, Qt::Orientation orientation, int role) const {
+QVariant ClipList::headerData(int section, Qt::Orientation orientation, int role) const
+{
        if (role != Qt::DisplayRole)
                return QVariant();
        if (orientation != Qt::Horizontal)
@@ -204,7 +226,8 @@ QVariant ClipList::headerData(int section, Qt::Orientation orientation, int role
        }
 }
 
-QVariant PlayList::headerData(int section, Qt::Orientation orientation, int role) const {
+QVariant PlayList::headerData(int section, Qt::Orientation orientation, int role) const
+{
        if (role != Qt::DisplayRole)
                return QVariant();
        if (orientation != Qt::Horizontal)
@@ -223,6 +246,8 @@ QVariant PlayList::headerData(int section, Qt::Orientation orientation, int role
                return "Camera";
        case Column::DESCRIPTION:
                return "Description";
+       case Column::FADE_TIME:
+               return "Fade time";
        default:
                return "";
        }
@@ -258,6 +283,7 @@ Qt::ItemFlags PlayList::flags(const QModelIndex &index) const
        switch (Column(column)) {
        case Column::DESCRIPTION:
        case Column::CAMERA:
+       case Column::FADE_TIME:
                return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable;
                return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable;
        default:
@@ -315,6 +341,16 @@ bool PlayList::setData(const QModelIndex &index, const QVariant &value, int role
                emit_data_changed(row);
                return true;
        }
+       case Column::FADE_TIME: {
+               bool ok;
+               double val = value.toDouble(&ok);
+               if (!ok || !(val >= 0.0)) {
+                       return false;
+               }
+               clips[row].fade_time_seconds = val;
+               emit_data_changed(row);
+               return true;
+       }
        default:
                return false;
        }
@@ -325,6 +361,7 @@ void ClipList::add_clip(const Clip &clip)
        beginInsertRows(QModelIndex(), clips.size(), clips.size());
        clips.push_back(clip);
        endInsertRows();
+       emit any_content_changed();
 }
 
 void PlayList::add_clip(const Clip &clip)
@@ -332,6 +369,7 @@ void PlayList::add_clip(const Clip &clip)
        beginInsertRows(QModelIndex(), clips.size(), clips.size());
        clips.push_back(clip);
        endInsertRows();
+       emit any_content_changed();
 }
 
 void PlayList::duplicate_clips(size_t first, size_t last)
@@ -339,6 +377,7 @@ void PlayList::duplicate_clips(size_t first, size_t last)
        beginInsertRows(QModelIndex(), first, last);
        clips.insert(clips.begin() + first, clips.begin() + first, clips.begin() + last + 1);
        endInsertRows();
+       emit any_content_changed();
 }
 
 void PlayList::erase_clips(size_t first, size_t last)
@@ -346,6 +385,7 @@ void PlayList::erase_clips(size_t first, size_t last)
        beginRemoveRows(QModelIndex(), first, last);
        clips.erase(clips.begin() + first, clips.begin() + last + 1);
        endRemoveRows();
+       emit any_content_changed();
 }
 
 void PlayList::move_clips(size_t first, size_t last, int delta)
@@ -354,22 +394,25 @@ void PlayList::move_clips(size_t first, size_t last, int delta)
                beginMoveRows(QModelIndex(), first, last, QModelIndex(), first - 1);
                rotate(clips.begin() + first - 1, clips.begin() + first, clips.begin() + last + 1);
        } else {
-               beginMoveRows(QModelIndex(), first, last, QModelIndex(), first + (last-first+1) + 1);
+               beginMoveRows(QModelIndex(), first, last, QModelIndex(), first + (last - first + 1) + 1);
                first = clips.size() - first - 1;
                last = clips.size() - last - 1;
                rotate(clips.rbegin() + last - 1, clips.rbegin() + last, clips.rbegin() + first + 1);
        }
        endMoveRows();
+       emit any_content_changed();
 }
 
 void ClipList::emit_data_changed(size_t row)
 {
        emit dataChanged(index(row, 0), index(row, int(Column::NUM_COLUMNS)));
+       emit any_content_changed();
 }
 
 void PlayList::emit_data_changed(size_t row)
 {
        emit dataChanged(index(row, 0), index(row, int(Column::NUM_COLUMNS)));
+       emit any_content_changed();
 }
 
 void PlayList::set_currently_playing(int index, double progress)
@@ -390,3 +433,81 @@ void PlayList::set_currently_playing(int index, double progress)
                emit dataChanged(this->index(index, column), this->index(index, column));
        }
 }
+
+void PlayList::set_progress(const map<size_t, double> &progress)
+{
+       const int column = int(Column::PLAYING);
+       map<size_t, double> old_progress = move(this->current_progress);
+       this->current_progress = progress;
+
+       for (auto it : old_progress) {
+               size_t index = it.first;
+               if (current_progress.count(index) == 0) {
+                       emit dataChanged(this->index(index, column), this->index(index, column));
+               }
+       }
+       for (auto it : current_progress) {
+               size_t index = it.first;
+               emit dataChanged(this->index(index, column), this->index(index, column));
+       }
+}
+
+namespace {
+
+Clip deserialize_clip(const ClipProto &clip_proto)
+{
+       Clip clip;
+       clip.pts_in = clip_proto.pts_in();
+       clip.pts_out = clip_proto.pts_out();
+       for (int camera_idx = 0; camera_idx < min(clip_proto.description_size(), NUM_CAMERAS); ++camera_idx) {
+               clip.descriptions[camera_idx] = clip_proto.description(camera_idx);
+       }
+       clip.stream_idx = clip_proto.stream_idx();
+       clip.fade_time_seconds = clip_proto.fade_time_seconds();
+       return clip;
+}
+
+void serialize_clip(const Clip &clip, ClipProto *clip_proto)
+{
+       clip_proto->set_pts_in(clip.pts_in);
+       clip_proto->set_pts_out(clip.pts_out);
+       for (int camera_idx = 0; camera_idx < NUM_CAMERAS; ++camera_idx) {
+               *clip_proto->add_description() = clip.descriptions[camera_idx];
+       }
+       clip_proto->set_stream_idx(clip.stream_idx);
+       clip_proto->set_fade_time_seconds(clip.fade_time_seconds);
+}
+
+}  // namespace
+
+ClipList::ClipList(const ClipListProto &serialized)
+{
+       for (const ClipProto &clip_proto : serialized.clip()) {
+               clips.push_back(deserialize_clip(clip_proto));
+       }
+}
+
+ClipListProto ClipList::serialize() const
+{
+       ClipListProto ret;
+       for (const Clip &clip : clips) {
+               serialize_clip(clip, ret.add_clip());
+       }
+       return ret;
+}
+
+PlayList::PlayList(const ClipListProto &serialized)
+{
+       for (const ClipProto &clip_proto : serialized.clip()) {
+               clips.push_back(deserialize_clip(clip_proto));
+       }
+}
+
+ClipListProto PlayList::serialize() const
+{
+       ClipListProto ret;
+       for (const Clip &clip : clips) {
+               serialize_clip(clip, ret.add_clip());
+       }
+       return ret;
+}