3 #include "mainwindow.h"
4 #include "shared/timebase.h"
5 #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
50 int PlayList::rowCount(const QModelIndex &parent) const
57 int ClipList::columnCount(const QModelIndex &parent) const
61 return int(Column::NUM_COLUMNS);
64 int PlayList::columnCount(const QModelIndex &parent) const
68 return int(Column::NUM_COLUMNS);
71 QVariant ClipList::data(const QModelIndex &parent, int role) const
73 if (!parent.isValid())
75 const int row = parent.row(), column = parent.column();
76 if (size_t(row) >= clips.size())
79 if (role == Qt::TextAlignmentRole) {
80 switch (Column(column)) {
83 case Column::DURATION:
84 return Qt::AlignRight + Qt::AlignVCenter;
86 return Qt::AlignLeft + Qt::AlignVCenter;
90 if (role != Qt::DisplayRole && role != Qt::EditRole)
93 switch (Column(column)) {
95 return QString::fromStdString(pts_to_string(clips[row].pts_in));
97 if (clips[row].pts_out >= 0) {
98 return QString::fromStdString(pts_to_string(clips[row].pts_out));
102 case Column::DURATION:
103 if (clips[row].pts_out >= 0) {
104 return QString::fromStdString(duration_to_string(clips[row].pts_out - clips[row].pts_in));
108 case Column::CAMERA_1:
109 case Column::CAMERA_2:
110 case Column::CAMERA_3:
111 case Column::CAMERA_4: {
112 unsigned stream_idx = column - int(Column::CAMERA_1);
113 return QString::fromStdString(clips[row].descriptions[stream_idx]);
120 QVariant PlayList::data(const QModelIndex &parent, int role) const
122 if (!parent.isValid())
124 const int row = parent.row(), column = parent.column();
125 if (size_t(row) >= clips.size())
128 if (role == Qt::TextAlignmentRole) {
129 switch (Column(column)) {
130 case Column::PLAYING:
131 return Qt::AlignCenter;
134 case Column::DURATION:
135 case Column::FADE_TIME:
136 return Qt::AlignRight + Qt::AlignVCenter;
138 return Qt::AlignCenter;
140 return Qt::AlignLeft + Qt::AlignVCenter;
143 if (role == Qt::BackgroundRole) {
144 if (Column(column) == Column::PLAYING) {
145 auto it = current_progress.find(row);
146 if (it != current_progress.end()) {
147 double play_progress = it->second;
149 // This only really works well for the first column, for whatever odd Qt reason.
150 QLinearGradient grad(QPointF(0, 0), QPointF(1, 0));
151 grad.setCoordinateMode(grad.QGradient::ObjectBoundingMode);
152 grad.setColorAt(0.0f, QColor::fromRgbF(0.0f, 0.0f, 1.0f, 0.2f));
153 grad.setColorAt(play_progress, QColor::fromRgbF(0.0f, 0.0f, 1.0f, 0.2f));
154 if (play_progress + 0.01f <= 1.0f) {
155 grad.setColorAt(play_progress + 0.01f, QColor::fromRgbF(0.0f, 0.0f, 1.0f, 0.0f));
166 if (role != Qt::DisplayRole && role != Qt::EditRole)
169 switch (Column(column)) {
170 case Column::PLAYING:
171 return current_progress.count(row) ? "→" : "";
173 return QString::fromStdString(pts_to_string(clips[row].pts_in));
175 if (clips[row].pts_out >= 0) {
176 return QString::fromStdString(pts_to_string(clips[row].pts_out));
180 case Column::DURATION:
181 if (clips[row].pts_out >= 0) {
182 return QString::fromStdString(duration_to_string(clips[row].pts_out - clips[row].pts_in));
187 return qlonglong(clips[row].stream_idx + 1);
188 case Column::DESCRIPTION:
189 return QString::fromStdString(clips[row].descriptions[clips[row].stream_idx]);
190 case Column::FADE_TIME: {
192 ss.imbue(locale("C"));
194 ss << fixed << clips[row].fade_time_seconds;
195 return QString::fromStdString(ss.str());
202 QVariant ClipList::headerData(int section, Qt::Orientation orientation, int role) const
204 if (role != Qt::DisplayRole)
206 if (orientation != Qt::Horizontal)
209 switch (Column(section)) {
214 case Column::DURATION:
216 case Column::CAMERA_1:
218 case Column::CAMERA_2:
220 case Column::CAMERA_3:
222 case Column::CAMERA_4:
229 QVariant PlayList::headerData(int section, Qt::Orientation orientation, int role) const
231 if (role != Qt::DisplayRole)
233 if (orientation != Qt::Horizontal)
236 switch (Column(section)) {
237 case Column::PLAYING:
243 case Column::DURATION:
247 case Column::DESCRIPTION:
248 return "Description";
249 case Column::FADE_TIME:
256 Qt::ItemFlags ClipList::flags(const QModelIndex &index) const
258 if (!index.isValid())
259 return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
260 const int row = index.row(), column = index.column();
261 if (size_t(row) >= clips.size())
262 return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
264 switch (Column(column)) {
265 case Column::CAMERA_1:
266 case Column::CAMERA_2:
267 case Column::CAMERA_3:
268 case Column::CAMERA_4:
269 return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsDragEnabled;
271 return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
275 Qt::ItemFlags PlayList::flags(const QModelIndex &index) const
277 if (!index.isValid())
278 return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
279 const int row = index.row(), column = index.column();
280 if (size_t(row) >= clips.size())
281 return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
283 switch (Column(column)) {
284 case Column::DESCRIPTION:
286 case Column::FADE_TIME:
287 return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable;
288 return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable;
290 return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
294 bool ClipList::setData(const QModelIndex &index, const QVariant &value, int role)
296 if (!index.isValid() || role != Qt::EditRole) {
300 const int row = index.row(), column = index.column();
301 if (size_t(row) >= clips.size())
304 switch (Column(column)) {
305 case Column::CAMERA_1:
306 case Column::CAMERA_2:
307 case Column::CAMERA_3:
308 case Column::CAMERA_4: {
309 unsigned stream_idx = column - int(Column::CAMERA_1);
310 clips[row].descriptions[stream_idx] = value.toString().toStdString();
311 emit_data_changed(row);
319 bool PlayList::setData(const QModelIndex &index, const QVariant &value, int role)
321 if (!index.isValid() || role != Qt::EditRole) {
325 const int row = index.row(), column = index.column();
326 if (size_t(row) >= clips.size())
329 switch (Column(column)) {
330 case Column::DESCRIPTION:
331 clips[row].descriptions[clips[row].stream_idx] = value.toString().toStdString();
332 emit_data_changed(row);
334 case Column::CAMERA: {
336 int camera_idx = value.toInt(&ok);
337 if (!ok || camera_idx < 1 || camera_idx > NUM_CAMERAS) {
340 clips[row].stream_idx = camera_idx - 1;
341 emit_data_changed(row);
344 case Column::FADE_TIME: {
346 double val = value.toDouble(&ok);
347 if (!ok || !(val >= 0.0)) {
350 clips[row].fade_time_seconds = val;
351 emit_data_changed(row);
359 void ClipList::add_clip(const Clip &clip)
361 beginInsertRows(QModelIndex(), clips.size(), clips.size());
362 clips.push_back(clip);
364 emit any_content_changed();
367 void PlayList::add_clip(const Clip &clip)
369 beginInsertRows(QModelIndex(), clips.size(), clips.size());
370 clips.push_back(clip);
372 emit any_content_changed();
375 void PlayList::duplicate_clips(size_t first, size_t last)
377 beginInsertRows(QModelIndex(), first, last);
378 clips.insert(clips.begin() + first, clips.begin() + first, clips.begin() + last + 1);
380 emit any_content_changed();
383 void PlayList::erase_clips(size_t first, size_t last)
385 beginRemoveRows(QModelIndex(), first, last);
386 clips.erase(clips.begin() + first, clips.begin() + last + 1);
388 emit any_content_changed();
391 void PlayList::move_clips(size_t first, size_t last, int delta)
394 beginMoveRows(QModelIndex(), first, last, QModelIndex(), first - 1);
395 rotate(clips.begin() + first - 1, clips.begin() + first, clips.begin() + last + 1);
397 beginMoveRows(QModelIndex(), first, last, QModelIndex(), first + (last - first + 1) + 1);
398 first = clips.size() - first - 1;
399 last = clips.size() - last - 1;
400 rotate(clips.rbegin() + last - 1, clips.rbegin() + last, clips.rbegin() + first + 1);
403 emit any_content_changed();
406 void ClipList::emit_data_changed(size_t row)
408 emit dataChanged(index(row, 0), index(row, int(Column::NUM_COLUMNS)));
409 emit any_content_changed();
412 void PlayList::emit_data_changed(size_t row)
414 emit dataChanged(index(row, 0), index(row, int(Column::NUM_COLUMNS)));
415 emit any_content_changed();
418 void PlayList::set_currently_playing(int index, double progress)
420 int old_index = currently_playing_index;
421 int column = int(Column::PLAYING);
422 if (index != old_index) {
423 currently_playing_index = index;
424 play_progress = progress;
425 if (old_index != -1) {
426 emit dataChanged(this->index(old_index, column), this->index(old_index, column));
429 emit dataChanged(this->index(index, column), this->index(index, column));
431 } else if (index != -1 && fabs(progress - play_progress) > 1e-3) {
432 play_progress = progress;
433 emit dataChanged(this->index(index, column), this->index(index, column));
437 void PlayList::set_progress(const map<size_t, double> &progress)
439 const int column = int(Column::PLAYING);
440 map<size_t, double> old_progress = move(this->current_progress);
441 this->current_progress = progress;
443 for (auto it : old_progress) {
444 size_t index = it.first;
445 if (current_progress.count(index) == 0) {
446 emit dataChanged(this->index(index, column), this->index(index, column));
449 for (auto it : current_progress) {
450 size_t index = it.first;
451 emit dataChanged(this->index(index, column), this->index(index, column));
457 Clip deserialize_clip(const ClipProto &clip_proto)
460 clip.pts_in = clip_proto.pts_in();
461 clip.pts_out = clip_proto.pts_out();
462 for (int camera_idx = 0; camera_idx < min(clip_proto.description_size(), NUM_CAMERAS); ++camera_idx) {
463 clip.descriptions[camera_idx] = clip_proto.description(camera_idx);
465 clip.stream_idx = clip_proto.stream_idx();
466 clip.fade_time_seconds = clip_proto.fade_time_seconds();
470 void serialize_clip(const Clip &clip, ClipProto *clip_proto)
472 clip_proto->set_pts_in(clip.pts_in);
473 clip_proto->set_pts_out(clip.pts_out);
474 for (int camera_idx = 0; camera_idx < NUM_CAMERAS; ++camera_idx) {
475 *clip_proto->add_description() = clip.descriptions[camera_idx];
477 clip_proto->set_stream_idx(clip.stream_idx);
478 clip_proto->set_fade_time_seconds(clip.fade_time_seconds);
483 ClipList::ClipList(const ClipListProto &serialized)
485 for (const ClipProto &clip_proto : serialized.clip()) {
486 clips.push_back(deserialize_clip(clip_proto));
490 ClipListProto ClipList::serialize() const
493 for (const Clip &clip : clips) {
494 serialize_clip(clip, ret.add_clip());
499 PlayList::PlayList(const ClipListProto &serialized)
501 for (const ClipProto &clip_proto : serialized.clip()) {
502 clips.push_back(deserialize_clip(clip_proto));
506 ClipListProto PlayList::serialize() const
509 for (const Clip &clip : clips) {
510 serialize_clip(clip, ret.add_clip());