]> git.sesse.net Git - nageru/blob - clip_list.cpp
Split ClipList and PlayList.
[nageru] / clip_list.cpp
1 #include "mainwindow.h"
2
3 #include <math.h>
4 #include <string>
5 #include <vector>
6
7 #include "clip_list.h"
8 #include "ui_mainwindow.h"
9
10 using namespace std;
11
12 string pts_to_string(int64_t pts)
13 {
14         // FIXME: This depends on a fixed timebase.
15         int64_t t = lrint((pts / 12800.0) * 1e3);  // In milliseconds.
16         int ms = t % 1000;
17         t /= 1000;
18         int sec = t % 60;
19         t /= 60;
20         int min = t % 60;
21         t /= 60;
22         int hour = t;
23
24         char buf[256];
25         snprintf(buf, sizeof(buf), "%d:%02d:%02d.%03d", hour, min, sec, ms);
26         return buf;
27 }
28
29 string duration_to_string(int64_t pts_diff)
30 {
31         // FIXME: This depends on a fixed timebase.
32         int64_t t = lrint((pts_diff / 12800.0) * 1e3);  // In milliseconds.
33         int ms = t % 1000;
34         t /= 1000;
35         int sec = t % 60;
36         t /= 60;
37         int min = t;
38
39         char buf[256];
40         snprintf(buf, sizeof(buf), "%d:%02d.%03d", min, sec, ms);
41         return buf;
42 }
43
44 int ClipList::rowCount(const QModelIndex &parent) const {
45         if (parent.isValid()) return 0;
46         return clips.size();
47 }
48
49 int PlayList::rowCount(const QModelIndex &parent) const {
50         if (parent.isValid()) return 0;
51         return clips.size();
52 }
53
54 int ClipList::columnCount(const QModelIndex &parent) const {
55         if (parent.isValid()) return 0;
56         return int(Column::NUM_COLUMNS);
57 }
58
59 int PlayList::columnCount(const QModelIndex &parent) const {
60         if (parent.isValid()) return 0;
61         return int(Column::NUM_COLUMNS);
62 }
63
64 QVariant ClipList::data(const QModelIndex &parent, int role) const {
65         if (!parent.isValid())
66                 return QVariant();
67         const int row = parent.row(), column = parent.column();
68         if (size_t(row) >= clips.size())
69                 return QVariant();
70
71         if (role == Qt::TextAlignmentRole) {
72                 switch (Column(column)) {
73                 case Column::IN:
74                 case Column::OUT:
75                 case Column::DURATION:
76                         return Qt::AlignRight + Qt::AlignVCenter;
77                 default:
78                         return Qt::AlignLeft + Qt::AlignVCenter;
79                 }
80         }
81
82         if (role != Qt::DisplayRole)
83                 return QVariant();
84
85         switch (Column(column)) {
86         case Column::IN:
87                 return QString::fromStdString(pts_to_string(clips[row].pts_in));
88         case Column::OUT:
89                 if (clips[row].pts_out >= 0) {
90                         return QString::fromStdString(pts_to_string(clips[row].pts_out));
91                 } else {
92                         return QVariant();
93                 }
94         case Column::DURATION:
95                 if (clips[row].pts_out >= 0) {
96                         return QString::fromStdString(duration_to_string(clips[row].pts_out - clips[row].pts_in));
97                 } else {
98                         return QVariant();
99                 }
100         default:
101                 return "";
102         }
103 }
104
105 QVariant PlayList::data(const QModelIndex &parent, int role) const {
106         if (!parent.isValid())
107                 return QVariant();
108         const int row = parent.row(), column = parent.column();
109         if (size_t(row) >= clips.size())
110                 return QVariant();
111
112         if (role == Qt::TextAlignmentRole) {
113                 switch (Column(column)) {
114                 case Column::PLAYING:
115                         return Qt::AlignCenter;
116                 case Column::IN:
117                 case Column::OUT:
118                 case Column::DURATION:
119                         return Qt::AlignRight + Qt::AlignVCenter;
120                 case Column::CAMERA:
121                         return Qt::AlignCenter;
122                 default:
123                         return Qt::AlignLeft + Qt::AlignVCenter;
124                 }
125         }
126
127         if (role != Qt::DisplayRole)
128                 return QVariant();
129
130         switch (Column(column)) {
131         case Column::PLAYING:
132                 return (row == currently_playing_index) ? "→" : "";
133         case Column::IN:
134                 return QString::fromStdString(pts_to_string(clips[row].pts_in));
135         case Column::OUT:
136                 if (clips[row].pts_out >= 0) {
137                         return QString::fromStdString(pts_to_string(clips[row].pts_out));
138                 } else {
139                         return QVariant();
140                 }
141         case Column::DURATION:
142                 if (clips[row].pts_out >= 0) {
143                         return QString::fromStdString(duration_to_string(clips[row].pts_out - clips[row].pts_in));
144                 } else {
145                         return QVariant();
146                 }
147         case Column::CAMERA:
148                 return qlonglong(clips[row].stream_idx + 1);
149         default:
150                 return "";
151         }
152 }
153
154 QVariant ClipList::headerData(int section, Qt::Orientation orientation, int role) const {
155         if (role != Qt::DisplayRole)
156                 return QVariant();
157         if (orientation != Qt::Horizontal)
158                 return QVariant();
159
160         switch (Column(section)) {
161         case Column::IN:
162                 return "In";
163         case Column::OUT:
164                 return "Out";
165         case Column::DURATION:
166                 return "Duration";
167         case Column::CAMERA_1:
168                 return "Camera 1";
169         case Column::CAMERA_2:
170                 return "Camera 2";
171         case Column::CAMERA_3:
172                 return "Camera 3";
173         case Column::CAMERA_4:
174                 return "Camera 4";
175         default:
176                 return "";
177         }
178 }
179
180 QVariant PlayList::headerData(int section, Qt::Orientation orientation, int role) const {
181         if (role != Qt::DisplayRole)
182                 return QVariant();
183         if (orientation != Qt::Horizontal)
184                 return QVariant();
185
186         switch (Column(section)) {
187         case Column::PLAYING:
188                 return "";
189         case Column::IN:
190                 return "In";
191         case Column::OUT:
192                 return "Out";
193         case Column::DURATION:
194                 return "Duration";
195         case Column::CAMERA:
196                 return "Camera";
197         case Column::DESCRIPTION:
198                 return "Description";
199         default:
200                 return "";
201         }
202 }
203
204 void ClipList::add_clip(const Clip &clip)
205 {
206         beginInsertRows(QModelIndex(), clips.size(), clips.size());
207         clips.push_back(clip);
208         endInsertRows();
209 }
210
211 void PlayList::add_clip(const Clip &clip)
212 {
213         beginInsertRows(QModelIndex(), clips.size(), clips.size());
214         clips.push_back(clip);
215         endInsertRows();
216 }
217
218 void ClipList::emit_data_changed(size_t row)
219 {
220         emit dataChanged(index(row, 0), index(row, int(Column::NUM_COLUMNS)));
221 }
222
223 void PlayList::emit_data_changed(size_t row)
224 {
225         emit dataChanged(index(row, 0), index(row, int(Column::NUM_COLUMNS)));
226 }
227
228 void PlayList::set_currently_playing(int index)
229 {
230         int old_index = currently_playing_index;
231         if (index != old_index) {
232                 currently_playing_index = index;
233                 if (old_index != -1) {
234                         emit_data_changed(old_index);
235                 }
236                 if (index != -1) {
237                         emit_data_changed(index);
238                 }
239         }
240 }