]> git.sesse.net Git - nageru/blob - clip_list.h
Fix displaying of progress bars and time remaining in the presence of fades.
[nageru] / clip_list.h
1 #ifndef _CLIP_LIST_H
2 #define _CLIP_LIST_H 1
3
4 #include "defs.h"
5 #include "state.pb.h"
6
7 #include <QAbstractTableModel>
8 #include <stdint.h>
9 #include <map>
10 #include <string>
11 #include <vector>
12
13 struct Clip {
14         int64_t pts_in = -1, pts_out = -1;  // pts_in is inclusive, pts_out is exclusive.
15         std::string descriptions[NUM_CAMERAS];
16         unsigned stream_idx = 0;  // For the playlist only.
17         double fade_time_seconds = 0.5;  // For the playlist only.
18 };
19
20 class DataChangedReceiver {
21 public:
22         virtual void emit_data_changed(size_t row) = 0;
23 };
24
25 // Like a smart pointer to a Clip, but emits dataChanged when it goes out of scope.
26 struct ClipProxy {
27 public:
28         ClipProxy(Clip &clip, DataChangedReceiver *clip_list, size_t row)
29                 : clip(clip), clip_list(clip_list), row(row) {}
30         ~ClipProxy()
31         {
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         void set_progress(const std::map<size_t, double> &progress);
136
137         ClipListProto serialize() const;
138
139         void emit_data_changed(size_t row) override;
140
141 signals:
142         void any_content_changed();
143
144 private:
145         std::vector<Clip> clips;
146         int currently_playing_index = -1;
147         double play_progress = 0.0;
148         std::map<size_t, double> current_progress;
149 };
150
151 #endif  // !defined (_CLIP_LIST_H)