]> git.sesse.net Git - nageru/blob - futatabi/clip_list.h
Make the UI programmatically dependent on NUM_CAMERAS.
[nageru] / futatabi / 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 ~DataChangedReceiver() {}
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         {
33                 if (clip_list != nullptr) {
34                         clip_list->emit_data_changed(row);
35                 }
36         }
37         Clip *operator->() { return &clip; }
38         Clip &operator*() { return clip; }
39
40 private:
41         Clip &clip;
42         DataChangedReceiver *clip_list;
43         size_t row;
44 };
45
46 class ClipList : public QAbstractTableModel, public DataChangedReceiver {
47         Q_OBJECT
48
49 public:
50         explicit ClipList(const ClipListProto &serialized);
51
52         enum class Column {
53                 IN,
54                 OUT,
55                 DURATION,
56                 CAMERA_1,  // Then CAMERA_2, CAMERA_3, etc. as needed.
57                 NUM_NON_CAMERA_COLUMNS = CAMERA_1
58         };
59
60         int rowCount(const QModelIndex &parent) const override;
61         int columnCount(const QModelIndex &parent) const override;
62         QVariant data(const QModelIndex &parent, int role) const override;
63         QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
64         Qt::ItemFlags flags(const QModelIndex &index) const override;
65         bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override;
66
67         void add_clip(const Clip &clip);
68         size_t size() const { return clips.size(); }
69         bool empty() const { return clips.empty(); }
70
71         ClipProxy mutable_clip(size_t index) { return ClipProxy(clips[index], this, index); }
72         const Clip *clip(size_t index) const { return &clips[index]; }
73
74         ClipProxy mutable_back() { return mutable_clip(size() - 1); }
75         const Clip *back() const { return clip(size() - 1); }
76
77         ClipListProto serialize() const;
78
79         void emit_data_changed(size_t row) override;
80
81         static bool is_camera_column(int column)
82         {
83                 return (column >= int(Column::CAMERA_1) && column < int(Column::CAMERA_1) + NUM_CAMERAS);
84         }
85
86 signals:
87         void any_content_changed();
88
89 private:
90         std::vector<Clip> clips;
91 };
92
93 class PlayList : public QAbstractTableModel, public DataChangedReceiver {
94         Q_OBJECT
95
96 public:
97         explicit PlayList(const ClipListProto &serialized);
98
99         enum class Column {
100                 PLAYING,
101                 IN,
102                 OUT,
103                 DURATION,
104                 CAMERA,
105                 DESCRIPTION,
106                 FADE_TIME,
107                 NUM_COLUMNS
108         };
109
110         int rowCount(const QModelIndex &parent) const override;
111         int columnCount(const QModelIndex &parent) const override;
112         QVariant data(const QModelIndex &parent, int role) const override;
113         QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
114         Qt::ItemFlags flags(const QModelIndex &index) const override;
115         bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override;
116
117         void add_clip(const Clip &clip);
118
119         // <last> is inclusive in all of these.
120         void duplicate_clips(size_t first, size_t last);
121         void erase_clips(size_t first, size_t last);
122         // <delta> is -1 to move upwards, +1 to move downwards.
123         void move_clips(size_t first, size_t last, int delta);
124
125         size_t size() const { return clips.size(); }
126         bool empty() const { return clips.empty(); }
127
128         ClipProxy mutable_clip(size_t index) { return ClipProxy(clips[index], this, index); }
129         const Clip *clip(size_t index) const { return &clips[index]; }
130
131         ClipProxy mutable_back() { return mutable_clip(size() - 1); }
132         const Clip *back() const { return clip(size() - 1); }
133
134         // TODO: Move these out of PlayList.
135         void set_currently_playing(int index, double progress);  // -1 = none.
136         int get_currently_playing() const { return currently_playing_index; }
137
138         void set_progress(const std::map<size_t, double> &progress);
139
140         ClipListProto serialize() const;
141
142         void emit_data_changed(size_t row) override;
143
144 signals:
145         void any_content_changed();
146
147 private:
148         std::vector<Clip> clips;
149         int currently_playing_index = -1;
150         double play_progress = 0.0;
151         std::map<size_t, double> current_progress;
152 };
153
154 #endif  // !defined (_CLIP_LIST_H)