]> git.sesse.net Git - nageru/blob - clip_list.h
Implement fades.
[nageru] / clip_list.h
1 #ifndef _CLIP_LIST_H
2 #define _CLIP_LIST_H 1
3
4 #include <QAbstractTableModel>
5
6 #include <stdint.h>
7
8 #include <vector>
9 #include <string>
10
11 #include "defs.h"
12 #include "state.pb.h"
13
14 struct Clip {
15         int64_t pts_in = -1, pts_out = -1;  // pts_in is inclusive, pts_out is exclusive.
16         std::string descriptions[NUM_CAMERAS];
17         unsigned stream_idx = 0;  // For the playlist only.
18         double fade_time_seconds = 0.5;  // For the playlist only.
19 };
20
21 class DataChangedReceiver {
22 public:
23         virtual void emit_data_changed(size_t row) = 0;
24 };
25
26 // Like a smart pointer to a Clip, but emits dataChanged when it goes out of scope.
27 struct ClipProxy {
28 public:
29         ClipProxy(Clip &clip, DataChangedReceiver *clip_list, size_t row)
30                 : clip(clip), clip_list(clip_list), row(row) {}
31         ~ClipProxy() {
32                 if (clip_list != nullptr) {
33                         clip_list->emit_data_changed(row);
34                 }
35         }
36         Clip *operator->() { return &clip; }
37         Clip &operator*() { return clip; }
38
39 private:
40         Clip &clip;
41         DataChangedReceiver *clip_list;
42         size_t row;
43 };
44
45 class ClipList : public QAbstractTableModel, public DataChangedReceiver {
46         Q_OBJECT
47
48 public:
49         explicit ClipList(const ClipListProto &serialized);
50
51         enum class Column {
52                 IN,
53                 OUT,
54                 DURATION,
55                 CAMERA_1,
56                 CAMERA_2,
57                 CAMERA_3,
58                 CAMERA_4,
59                 NUM_COLUMNS
60         };
61
62         int rowCount(const QModelIndex &parent) const override;
63         int columnCount(const QModelIndex &parent) const override;
64         QVariant data(const QModelIndex &parent, int role) const override;
65         QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
66         Qt::ItemFlags flags(const QModelIndex &index) const override;
67         bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override;
68
69         void add_clip(const Clip &clip);
70         size_t size() const { return clips.size(); }
71         bool empty() const { return clips.empty(); }
72
73         ClipProxy mutable_clip(size_t index) { return ClipProxy(clips[index], this, index); }
74         const Clip *clip(size_t index) const { return &clips[index]; }
75
76         ClipProxy mutable_back() { return mutable_clip(size() - 1); }
77         const Clip *back() const { return clip(size() - 1); }
78
79         ClipListProto serialize() const;
80
81         void emit_data_changed(size_t row) override;
82
83 signals:
84         void any_content_changed();
85
86 private:
87         std::vector<Clip> clips;
88 };
89
90 class PlayList : public QAbstractTableModel, public DataChangedReceiver {
91         Q_OBJECT
92
93 public:
94         explicit PlayList(const ClipListProto &serialized);
95
96         enum class Column {
97                 PLAYING,
98                 IN,
99                 OUT,
100                 DURATION,
101                 CAMERA,
102                 DESCRIPTION,
103                 FADE_TIME,
104                 NUM_COLUMNS
105         };
106
107         int rowCount(const QModelIndex &parent) const override;
108         int columnCount(const QModelIndex &parent) const override;
109         QVariant data(const QModelIndex &parent, int role) const override;
110         QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
111         Qt::ItemFlags flags(const QModelIndex &index) const override;
112         bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override;
113
114         void add_clip(const Clip &clip);
115
116         // <last> is inclusive in all of these.
117         void duplicate_clips(size_t first, size_t last);
118         void erase_clips(size_t first, size_t last);
119         // <delta> is -1 to move upwards, +1 to move downwards.
120         void move_clips(size_t first, size_t last, int delta);
121
122         size_t size() const { return clips.size(); }
123         bool empty() const { return clips.empty(); }
124
125         ClipProxy mutable_clip(size_t index) { return ClipProxy(clips[index], this, index); }
126         const Clip *clip(size_t index) const { return &clips[index]; }
127
128         ClipProxy mutable_back() { return mutable_clip(size() - 1); }
129         const Clip *back() const { return clip(size() - 1); }
130
131         // TODO: Move these out of PlayList.
132         void set_currently_playing(int index, double progress);  // -1 = none.
133         int get_currently_playing() const { return currently_playing_index; }
134
135         ClipListProto serialize() const;
136
137         void emit_data_changed(size_t row) override;
138
139 signals:
140         void any_content_changed();
141
142 private:
143         std::vector<Clip> clips;
144         int currently_playing_index = -1;
145         double play_progress = 0.0;
146 };
147
148 #endif  // !defined (_CLIP_LIST_H)