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_NON_CAMERA_COLUMNS) + num_cameras;
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));
109 if (is_camera_column(column)) {
110 unsigned stream_idx = column - int(Column::CAMERA_1);
111 return QString::fromStdString(clips[row].descriptions[stream_idx]);
118 QVariant PlayList::data(const QModelIndex &parent, int role) const
120 if (!parent.isValid())
122 const int row = parent.row(), column = parent.column();
123 if (size_t(row) >= clips.size())
126 if (role == Qt::TextAlignmentRole) {
127 switch (Column(column)) {
128 case Column::PLAYING:
129 return Qt::AlignCenter;
132 case Column::DURATION:
133 case Column::FADE_TIME:
135 return Qt::AlignRight + Qt::AlignVCenter;
137 return Qt::AlignCenter;
139 return Qt::AlignLeft + Qt::AlignVCenter;
142 if (role == Qt::BackgroundRole) {
143 if (Column(column) == Column::PLAYING) {
144 auto it = current_progress.find(row);
145 if (it != current_progress.end()) {
146 double play_progress = it->second;
148 // This only really works well for the first column, for whatever odd Qt reason.
149 QLinearGradient grad(QPointF(0, 0), QPointF(1, 0));
150 grad.setCoordinateMode(grad.QGradient::ObjectBoundingMode);
151 grad.setColorAt(0.0f, QColor::fromRgbF(0.0f, 0.0f, 1.0f, 0.2f));
152 grad.setColorAt(play_progress, QColor::fromRgbF(0.0f, 0.0f, 1.0f, 0.2f));
153 if (play_progress + 0.01f <= 1.0f) {
154 grad.setColorAt(play_progress + 0.01f, QColor::fromRgbF(0.0f, 0.0f, 1.0f, 0.0f));
165 if (role != Qt::DisplayRole && role != Qt::EditRole)
168 switch (Column(column)) {
169 case Column::PLAYING:
170 return current_progress.count(row) ? "→" : "";
172 return QString::fromStdString(pts_to_string(clips[row].pts_in));
174 if (clips[row].pts_out >= 0) {
175 return QString::fromStdString(pts_to_string(clips[row].pts_out));
179 case Column::DURATION:
180 if (clips[row].pts_out >= 0) {
181 return QString::fromStdString(duration_to_string(clips[row].pts_out - clips[row].pts_in));
186 return qlonglong(clips[row].stream_idx + 1);
187 case Column::DESCRIPTION:
188 return QString::fromStdString(clips[row].descriptions[clips[row].stream_idx]);
189 case Column::FADE_TIME: {
191 ss.imbue(locale("C"));
193 ss << fixed << clips[row].fade_time_seconds;
194 return QString::fromStdString(ss.str());
196 case Column::SPEED: {
198 ss.imbue(locale("C"));
200 ss << fixed << clips[row].speed;
201 return QString::fromStdString(ss.str());
208 QVariant ClipList::headerData(int section, Qt::Orientation orientation, int role) const
210 if (role != Qt::DisplayRole)
212 if (orientation != Qt::Horizontal)
215 switch (Column(section)) {
220 case Column::DURATION:
223 if (is_camera_column(section)) {
224 return QString::fromStdString("Camera " + to_string(section - int(Column::CAMERA_1) + 1));
231 QVariant PlayList::headerData(int section, Qt::Orientation orientation, int role) const
233 if (role != Qt::DisplayRole)
235 if (orientation != Qt::Horizontal)
238 switch (Column(section)) {
239 case Column::PLAYING:
245 case Column::DURATION:
249 case Column::DESCRIPTION:
250 return "Description";
251 case Column::FADE_TIME:
260 Qt::ItemFlags ClipList::flags(const QModelIndex &index) const
262 if (!index.isValid())
263 return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
264 const int row = index.row(), column = index.column();
265 if (size_t(row) >= clips.size())
266 return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
268 if (is_camera_column(column)) {
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:
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 if (is_camera_column(column)) {
305 unsigned stream_idx = column - int(Column::CAMERA_1);
306 clips[row].descriptions[stream_idx] = value.toString().toStdString();
307 emit_data_changed(row);
314 bool PlayList::setData(const QModelIndex &index, const QVariant &value, int role)
316 if (!index.isValid() || role != Qt::EditRole) {
320 const int row = index.row(), column = index.column();
321 if (size_t(row) >= clips.size())
324 switch (Column(column)) {
325 case Column::DESCRIPTION:
326 clips[row].descriptions[clips[row].stream_idx] = value.toString().toStdString();
327 emit_data_changed(row);
329 case Column::CAMERA: {
331 int camera_idx = value.toInt(&ok);
332 if (!ok || camera_idx < 1 || camera_idx > int(num_cameras)) {
335 clips[row].stream_idx = camera_idx - 1;
336 emit_data_changed(row);
339 case Column::FADE_TIME: {
341 double val = value.toDouble(&ok);
342 if (!ok || !(val >= 0.0)) {
345 clips[row].fade_time_seconds = val;
346 emit_data_changed(row);
349 case Column::SPEED: {
351 double val = value.toDouble(&ok);
352 if (!ok || !(val >= 0.001)) {
355 clips[row].speed = val;
356 emit_data_changed(row);
364 void ClipList::add_clip(const Clip &clip)
366 beginInsertRows(QModelIndex(), clips.size(), clips.size());
367 clips.push_back(clip);
369 emit any_content_changed();
372 void PlayList::add_clip(const Clip &clip)
374 beginInsertRows(QModelIndex(), clips.size(), clips.size());
375 clips.push_back(clip);
377 emit any_content_changed();
380 void PlayList::duplicate_clips(size_t first, size_t last)
382 beginInsertRows(QModelIndex(), first, last);
383 clips.insert(clips.begin() + first, clips.begin() + first, clips.begin() + last + 1);
385 emit any_content_changed();
388 void PlayList::erase_clips(size_t first, size_t last)
390 beginRemoveRows(QModelIndex(), first, last);
391 clips.erase(clips.begin() + first, clips.begin() + last + 1);
393 emit any_content_changed();
396 void PlayList::move_clips(size_t first, size_t last, int delta)
399 beginMoveRows(QModelIndex(), first, last, QModelIndex(), first - 1);
400 rotate(clips.begin() + first - 1, clips.begin() + first, clips.begin() + last + 1);
402 beginMoveRows(QModelIndex(), first, last, QModelIndex(), first + (last - first + 1) + 1);
403 first = clips.size() - first - 1;
404 last = clips.size() - last - 1;
405 rotate(clips.rbegin() + last - 1, clips.rbegin() + last, clips.rbegin() + first + 1);
408 emit any_content_changed();
411 void ClipList::emit_data_changed(size_t row)
413 emit dataChanged(index(row, 0), index(row, int(Column::NUM_NON_CAMERA_COLUMNS) + num_cameras));
414 emit any_content_changed();
417 void PlayList::emit_data_changed(size_t row)
419 emit dataChanged(index(row, 0), index(row, int(Column::NUM_COLUMNS)));
420 emit any_content_changed();
423 void ClipList::change_num_cameras(size_t num_cameras)
425 assert(num_cameras >= this->num_cameras);
426 if (num_cameras == this->num_cameras) {
430 beginInsertColumns(QModelIndex(), int(Column::NUM_NON_CAMERA_COLUMNS) + this->num_cameras, int(Column::NUM_NON_CAMERA_COLUMNS) + num_cameras - 1);
431 this->num_cameras = num_cameras;
433 emit any_content_changed();
436 void PlayList::set_currently_playing(int index, double progress)
438 int old_index = currently_playing_index;
439 int column = int(Column::PLAYING);
440 if (index != old_index) {
441 currently_playing_index = index;
442 play_progress = progress;
443 if (old_index != -1) {
444 emit dataChanged(this->index(old_index, column), this->index(old_index, column));
447 emit dataChanged(this->index(index, column), this->index(index, column));
449 } else if (index != -1 && fabs(progress - play_progress) > 1e-3) {
450 play_progress = progress;
451 emit dataChanged(this->index(index, column), this->index(index, column));
455 void PlayList::set_progress(const map<size_t, double> &progress)
457 const int column = int(Column::PLAYING);
458 map<size_t, double> old_progress = move(this->current_progress);
459 this->current_progress = progress;
461 for (auto it : old_progress) {
462 size_t index = it.first;
463 if (current_progress.count(index) == 0) {
464 emit dataChanged(this->index(index, column), this->index(index, column));
467 for (auto it : current_progress) {
468 size_t index = it.first;
469 emit dataChanged(this->index(index, column), this->index(index, column));
475 Clip deserialize_clip(const ClipProto &clip_proto)
478 clip.pts_in = clip_proto.pts_in();
479 clip.pts_out = clip_proto.pts_out();
480 for (int camera_idx = 0; camera_idx < min(clip_proto.description_size(), MAX_STREAMS); ++camera_idx) {
481 clip.descriptions[camera_idx] = clip_proto.description(camera_idx);
483 clip.stream_idx = clip_proto.stream_idx();
484 clip.fade_time_seconds = clip_proto.fade_time_seconds();
485 if (clip_proto.speed() < 0.001) {
486 clip.speed = 0.5; // Default.
488 clip.speed = clip_proto.speed();
493 void serialize_clip(const Clip &clip, ClipProto *clip_proto)
495 clip_proto->set_pts_in(clip.pts_in);
496 clip_proto->set_pts_out(clip.pts_out);
497 for (int camera_idx = 0; camera_idx < MAX_STREAMS; ++camera_idx) {
498 *clip_proto->add_description() = clip.descriptions[camera_idx];
500 clip_proto->set_stream_idx(clip.stream_idx);
501 clip_proto->set_fade_time_seconds(clip.fade_time_seconds);
502 clip_proto->set_speed(clip.speed);
507 ClipList::ClipList(const ClipListProto &serialized)
509 for (const ClipProto &clip_proto : serialized.clip()) {
510 clips.push_back(deserialize_clip(clip_proto));
514 ClipListProto ClipList::serialize() const
517 for (const Clip &clip : clips) {
518 serialize_clip(clip, ret.add_clip());
523 PlayList::PlayList(const ClipListProto &serialized)
525 for (const ClipProto &clip_proto : serialized.clip()) {
526 clips.push_back(deserialize_clip(clip_proto));
530 ClipListProto PlayList::serialize() const
533 for (const Clip &clip : clips) {
534 serialize_clip(clip, ret.add_clip());