]> git.sesse.net Git - nageru/blob - futatabi/clip_list.h
Some clang-formatting of Futatabi.
[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 <map>
9 #include <stdint.h>
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[MAX_STREAMS];
16
17         // These are for the playlist only.
18         unsigned stream_idx = 0;
19         double fade_time_seconds = 0.5;
20         double speed = 0.5;
21 };
22
23 class DataChangedReceiver {
24 public:
25         virtual ~DataChangedReceiver() {}
26         virtual void emit_data_changed(size_t row) = 0;
27 };
28
29 // Like a smart pointer to a Clip, but emits dataChanged when it goes out of scope.
30 struct ClipProxy {
31 public:
32         ClipProxy(Clip &clip, DataChangedReceiver *clip_list, size_t row)
33                 : clip(clip), clip_list(clip_list), row(row) {}
34         ~ClipProxy()
35         {
36                 if (clip_list != nullptr) {
37                         clip_list->emit_data_changed(row);
38                 }
39         }
40         Clip *operator->() { return &clip; }
41         Clip &operator*() { return clip; }
42
43 private:
44         Clip &clip;
45         DataChangedReceiver *clip_list;
46         size_t row;
47 };
48
49 class ClipList : public QAbstractTableModel, public DataChangedReceiver {
50         Q_OBJECT
51
52 public:
53         ClipList(const ClipListProto &serialized);
54
55         enum class Column {
56                 IN,
57                 OUT,
58                 DURATION,
59                 CAMERA_1,  // Then CAMERA_2, CAMERA_3, etc. as needed.
60                 NUM_NON_CAMERA_COLUMNS = CAMERA_1
61         };
62
63         int rowCount(const QModelIndex &parent) const override;
64         int columnCount(const QModelIndex &parent) const override;
65         QVariant data(const QModelIndex &parent, int role) const override;
66         QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
67         Qt::ItemFlags flags(const QModelIndex &index) const override;
68         bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override;
69
70         void add_clip(const Clip &clip);
71         size_t size() const { return clips.size(); }
72         bool empty() const { return clips.empty(); }
73
74         ClipProxy mutable_clip(size_t index) { return ClipProxy(clips[index], this, index); }
75         const Clip *clip(size_t index) const { return &clips[index]; }
76
77         ClipProxy mutable_back() { return mutable_clip(size() - 1); }
78         const Clip *back() const { return clip(size() - 1); }
79
80         ClipListProto serialize() const;
81
82         void change_num_cameras(size_t num_cameras);  // Defaults to 2. Cannot decrease.
83         void emit_data_changed(size_t row) override;
84
85         bool is_camera_column(int column) const
86         {
87                 return (column >= int(Column::CAMERA_1) && column < int(Column::CAMERA_1) + int(num_cameras));
88         }
89
90 signals:
91         void any_content_changed();
92
93 private:
94         std::vector<Clip> clips;
95         size_t num_cameras = 2;
96 };
97
98 class PlayList : public QAbstractTableModel, public DataChangedReceiver {
99         Q_OBJECT
100
101 public:
102         explicit PlayList(const ClipListProto &serialized);
103
104         enum class Column {
105                 PLAYING,
106                 IN,
107                 OUT,
108                 DURATION,
109                 CAMERA,
110                 DESCRIPTION,
111                 FADE_TIME,
112                 SPEED,
113                 NUM_COLUMNS
114         };
115
116         int rowCount(const QModelIndex &parent) const override;
117         int columnCount(const QModelIndex &parent) const override;
118         QVariant data(const QModelIndex &parent, int role) const override;
119         QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
120         Qt::ItemFlags flags(const QModelIndex &index) const override;
121         bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override;
122
123         void add_clip(const Clip &clip);
124
125         // <last> is inclusive in all of these.
126         void duplicate_clips(size_t first, size_t last);
127         void erase_clips(size_t first, size_t last);
128         // <delta> is -1 to move upwards, +1 to move downwards.
129         void move_clips(size_t first, size_t last, int delta);
130
131         size_t size() const { return clips.size(); }
132         bool empty() const { return clips.empty(); }
133
134         ClipProxy mutable_clip(size_t index) { return ClipProxy(clips[index], this, index); }
135         const Clip *clip(size_t index) const { return &clips[index]; }
136
137         ClipProxy mutable_back() { return mutable_clip(size() - 1); }
138         const Clip *back() const { return clip(size() - 1); }
139
140         // TODO: Move these out of PlayList.
141         void set_currently_playing(int index, double progress);  // -1 = none.
142         int get_currently_playing() const { return currently_playing_index; }
143
144         void set_progress(const std::map<size_t, double> &progress);
145
146         ClipListProto serialize() const;
147
148         void change_num_cameras(size_t num_cameras)  // Defaults to 2. Cannot decrease.
149         {
150                 this->num_cameras = num_cameras;
151         }
152
153         void emit_data_changed(size_t row) override;
154
155 signals:
156         void any_content_changed();
157
158 private:
159         std::vector<Clip> clips;
160         int currently_playing_index = -1;
161         double play_progress = 0.0;
162         std::map<size_t, double> current_progress;
163         size_t num_cameras = 2;
164 };
165
166 #endif  // !defined (_CLIP_LIST_H)