1 #include "mainwindow.h"
9 #include "ui_mainwindow.h"
13 string pts_to_string(int64_t pts)
15 int64_t t = lrint((pts / double(TIMEBASE)) * 1e3); // In milliseconds.
25 snprintf(buf, sizeof(buf), "%d:%02d:%02d.%03d", hour, min, sec, ms);
29 string duration_to_string(int64_t pts_diff)
31 int64_t t = lrint((pts_diff / double(TIMEBASE)) * 1e3); // In milliseconds.
39 snprintf(buf, sizeof(buf), "%d:%02d.%03d", min, sec, ms);
43 int ClipList::rowCount(const QModelIndex &parent) const {
44 if (parent.isValid()) return 0;
48 int PlayList::rowCount(const QModelIndex &parent) const {
49 if (parent.isValid()) return 0;
53 int ClipList::columnCount(const QModelIndex &parent) const {
54 if (parent.isValid()) return 0;
55 return int(Column::NUM_COLUMNS);
58 int PlayList::columnCount(const QModelIndex &parent) const {
59 if (parent.isValid()) return 0;
60 return int(Column::NUM_COLUMNS);
63 QVariant ClipList::data(const QModelIndex &parent, int role) const {
64 if (!parent.isValid())
66 const int row = parent.row(), column = parent.column();
67 if (size_t(row) >= clips.size())
70 if (role == Qt::TextAlignmentRole) {
71 switch (Column(column)) {
74 case Column::DURATION:
75 return Qt::AlignRight + Qt::AlignVCenter;
77 return Qt::AlignLeft + Qt::AlignVCenter;
81 if (role != Qt::DisplayRole && role != Qt::EditRole)
84 switch (Column(column)) {
86 return QString::fromStdString(pts_to_string(clips[row].pts_in));
88 if (clips[row].pts_out >= 0) {
89 return QString::fromStdString(pts_to_string(clips[row].pts_out));
93 case Column::DURATION:
94 if (clips[row].pts_out >= 0) {
95 return QString::fromStdString(duration_to_string(clips[row].pts_out - clips[row].pts_in));
99 case Column::CAMERA_1:
100 case Column::CAMERA_2:
101 case Column::CAMERA_3:
102 case Column::CAMERA_4: {
103 unsigned stream_idx = column - int(Column::CAMERA_1);
104 return QString::fromStdString(clips[row].descriptions[stream_idx]);
111 QVariant PlayList::data(const QModelIndex &parent, int role) const {
112 if (!parent.isValid())
114 const int row = parent.row(), column = parent.column();
115 if (size_t(row) >= clips.size())
118 if (role == Qt::TextAlignmentRole) {
119 switch (Column(column)) {
120 case Column::PLAYING:
121 return Qt::AlignCenter;
124 case Column::DURATION:
125 return Qt::AlignRight + Qt::AlignVCenter;
127 return Qt::AlignCenter;
129 return Qt::AlignLeft + Qt::AlignVCenter;
132 if (role == Qt::BackgroundRole) {
133 if (Column(column) == Column::PLAYING) {
134 if (row == currently_playing_index) {
135 // This only really works well for the first column, for whatever odd Qt reason.
136 QLinearGradient grad(QPointF(0, 0), QPointF(1, 0));
137 grad.setCoordinateMode(grad.QGradient::ObjectBoundingMode);
138 grad.setColorAt(0.0f, QColor::fromRgbF(0.0f, 0.0f, 1.0f, 0.2f));
139 grad.setColorAt(play_progress, QColor::fromRgbF(0.0f, 0.0f, 1.0f, 0.2f));
140 if (play_progress + 0.01f <= 1.0f) {
141 grad.setColorAt(play_progress + 0.01f, QColor::fromRgbF(0.0f, 0.0f, 1.0f, 0.0f));
152 if (role != Qt::DisplayRole && role != Qt::EditRole)
155 switch (Column(column)) {
156 case Column::PLAYING:
157 return (row == currently_playing_index) ? "→" : "";
159 return QString::fromStdString(pts_to_string(clips[row].pts_in));
161 if (clips[row].pts_out >= 0) {
162 return QString::fromStdString(pts_to_string(clips[row].pts_out));
166 case Column::DURATION:
167 if (clips[row].pts_out >= 0) {
168 return QString::fromStdString(duration_to_string(clips[row].pts_out - clips[row].pts_in));
173 return qlonglong(clips[row].stream_idx + 1);
174 case Column::DESCRIPTION:
175 return QString::fromStdString(clips[row].descriptions[clips[row].stream_idx]);
181 QVariant ClipList::headerData(int section, Qt::Orientation orientation, int role) const {
182 if (role != Qt::DisplayRole)
184 if (orientation != Qt::Horizontal)
187 switch (Column(section)) {
192 case Column::DURATION:
194 case Column::CAMERA_1:
196 case Column::CAMERA_2:
198 case Column::CAMERA_3:
200 case Column::CAMERA_4:
207 QVariant PlayList::headerData(int section, Qt::Orientation orientation, int role) const {
208 if (role != Qt::DisplayRole)
210 if (orientation != Qt::Horizontal)
213 switch (Column(section)) {
214 case Column::PLAYING:
220 case Column::DURATION:
224 case Column::DESCRIPTION:
225 return "Description";
231 Qt::ItemFlags ClipList::flags(const QModelIndex &index) const
233 if (!index.isValid())
234 return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
235 const int row = index.row(), column = index.column();
236 if (size_t(row) >= clips.size())
237 return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
239 switch (Column(column)) {
240 case Column::CAMERA_1:
241 case Column::CAMERA_2:
242 case Column::CAMERA_3:
243 case Column::CAMERA_4:
244 return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsDragEnabled;
246 return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
250 Qt::ItemFlags PlayList::flags(const QModelIndex &index) const
252 if (!index.isValid())
253 return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
254 const int row = index.row(), column = index.column();
255 if (size_t(row) >= clips.size())
256 return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
258 switch (Column(column)) {
259 case Column::DESCRIPTION:
261 return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable;
262 return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable;
264 return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
268 bool ClipList::setData(const QModelIndex &index, const QVariant &value, int role)
270 if (!index.isValid() || role != Qt::EditRole) {
274 const int row = index.row(), column = index.column();
275 if (size_t(row) >= clips.size())
278 switch (Column(column)) {
279 case Column::CAMERA_1:
280 case Column::CAMERA_2:
281 case Column::CAMERA_3:
282 case Column::CAMERA_4: {
283 unsigned stream_idx = column - int(Column::CAMERA_1);
284 clips[row].descriptions[stream_idx] = value.toString().toStdString();
285 emit_data_changed(row);
293 bool PlayList::setData(const QModelIndex &index, const QVariant &value, int role)
295 if (!index.isValid() || role != Qt::EditRole) {
299 const int row = index.row(), column = index.column();
300 if (size_t(row) >= clips.size())
303 switch (Column(column)) {
304 case Column::DESCRIPTION:
305 clips[row].descriptions[clips[row].stream_idx] = value.toString().toStdString();
306 emit_data_changed(row);
308 case Column::CAMERA: {
310 int camera_idx = value.toInt(&ok);
311 if (!ok || camera_idx < 1 || camera_idx > NUM_CAMERAS) {
314 clips[row].stream_idx = camera_idx - 1;
315 emit_data_changed(row);
323 void ClipList::add_clip(const Clip &clip)
325 beginInsertRows(QModelIndex(), clips.size(), clips.size());
326 clips.push_back(clip);
328 emit any_content_changed();
331 void PlayList::add_clip(const Clip &clip)
333 beginInsertRows(QModelIndex(), clips.size(), clips.size());
334 clips.push_back(clip);
336 emit any_content_changed();
339 void PlayList::duplicate_clips(size_t first, size_t last)
341 beginInsertRows(QModelIndex(), first, last);
342 clips.insert(clips.begin() + first, clips.begin() + first, clips.begin() + last + 1);
344 emit any_content_changed();
347 void PlayList::erase_clips(size_t first, size_t last)
349 beginRemoveRows(QModelIndex(), first, last);
350 clips.erase(clips.begin() + first, clips.begin() + last + 1);
352 emit any_content_changed();
355 void PlayList::move_clips(size_t first, size_t last, int delta)
358 beginMoveRows(QModelIndex(), first, last, QModelIndex(), first - 1);
359 rotate(clips.begin() + first - 1, clips.begin() + first, clips.begin() + last + 1);
361 beginMoveRows(QModelIndex(), first, last, QModelIndex(), first + (last-first+1) + 1);
362 first = clips.size() - first - 1;
363 last = clips.size() - last - 1;
364 rotate(clips.rbegin() + last - 1, clips.rbegin() + last, clips.rbegin() + first + 1);
367 emit any_content_changed();
370 void ClipList::emit_data_changed(size_t row)
372 emit dataChanged(index(row, 0), index(row, int(Column::NUM_COLUMNS)));
373 emit any_content_changed();
376 void PlayList::emit_data_changed(size_t row)
378 emit dataChanged(index(row, 0), index(row, int(Column::NUM_COLUMNS)));
379 emit any_content_changed();
382 void PlayList::set_currently_playing(int index, double progress)
384 int old_index = currently_playing_index;
385 int column = int(Column::PLAYING);
386 if (index != old_index) {
387 currently_playing_index = index;
388 play_progress = progress;
389 if (old_index != -1) {
390 emit dataChanged(this->index(old_index, column), this->index(old_index, column));
393 emit dataChanged(this->index(index, column), this->index(index, column));
395 } else if (index != -1 && fabs(progress - play_progress) > 1e-3) {
396 play_progress = progress;
397 emit dataChanged(this->index(index, column), this->index(index, column));
403 Clip deserialize_clip(const ClipProto &clip_proto)
406 clip.pts_in = clip_proto.pts_in();
407 clip.pts_out = clip_proto.pts_out();
408 for (int camera_idx = 0; camera_idx < min(clip_proto.description_size(), NUM_CAMERAS); ++camera_idx) {
409 clip.descriptions[camera_idx] = clip_proto.description(camera_idx);
411 clip.stream_idx = clip_proto.stream_idx();
415 void serialize_clip(const Clip &clip, ClipProto *clip_proto)
417 clip_proto->set_pts_in(clip.pts_in);
418 clip_proto->set_pts_out(clip.pts_out);
419 for (int camera_idx = 0; camera_idx < NUM_CAMERAS; ++camera_idx) {
420 *clip_proto->add_description() = clip.descriptions[camera_idx];
422 clip_proto->set_stream_idx(clip.stream_idx);
427 ClipList::ClipList(const ClipListProto &serialized)
429 for (const ClipProto &clip_proto : serialized.clip()) {
430 clips.push_back(deserialize_clip(clip_proto));
434 ClipListProto ClipList::serialize() const
437 for (const Clip &clip : clips) {
438 serialize_clip(clip, ret.add_clip());
443 PlayList::PlayList(const ClipListProto &serialized)
445 for (const ClipProto &clip_proto : serialized.clip()) {
446 clips.push_back(deserialize_clip(clip_proto));
450 ClipListProto PlayList::serialize() const
453 for (const Clip &clip : clips) {
454 serialize_clip(clip, ret.add_clip());