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 grad.setColorAt(play_progress + 0.01f, QColor::fromRgbF(0.0f, 0.0f, 1.0f, 0.0f));
150 if (role != Qt::DisplayRole && role != Qt::EditRole)
153 switch (Column(column)) {
154 case Column::PLAYING:
155 return (row == currently_playing_index) ? "→" : "";
157 return QString::fromStdString(pts_to_string(clips[row].pts_in));
159 if (clips[row].pts_out >= 0) {
160 return QString::fromStdString(pts_to_string(clips[row].pts_out));
164 case Column::DURATION:
165 if (clips[row].pts_out >= 0) {
166 return QString::fromStdString(duration_to_string(clips[row].pts_out - clips[row].pts_in));
171 return qlonglong(clips[row].stream_idx + 1);
172 case Column::DESCRIPTION:
173 return QString::fromStdString(clips[row].descriptions[clips[row].stream_idx]);
179 QVariant ClipList::headerData(int section, Qt::Orientation orientation, int role) const {
180 if (role != Qt::DisplayRole)
182 if (orientation != Qt::Horizontal)
185 switch (Column(section)) {
190 case Column::DURATION:
192 case Column::CAMERA_1:
194 case Column::CAMERA_2:
196 case Column::CAMERA_3:
198 case Column::CAMERA_4:
205 QVariant PlayList::headerData(int section, Qt::Orientation orientation, int role) const {
206 if (role != Qt::DisplayRole)
208 if (orientation != Qt::Horizontal)
211 switch (Column(section)) {
212 case Column::PLAYING:
218 case Column::DURATION:
222 case Column::DESCRIPTION:
223 return "Description";
229 Qt::ItemFlags ClipList::flags(const QModelIndex &index) const
231 if (!index.isValid())
232 return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
233 const int row = index.row(), column = index.column();
234 if (size_t(row) >= clips.size())
235 return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
237 switch (Column(column)) {
238 case Column::CAMERA_1:
239 case Column::CAMERA_2:
240 case Column::CAMERA_3:
241 case Column::CAMERA_4:
242 return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsDragEnabled;
244 return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
248 Qt::ItemFlags PlayList::flags(const QModelIndex &index) const
250 if (!index.isValid())
251 return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
252 const int row = index.row(), column = index.column();
253 if (size_t(row) >= clips.size())
254 return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
256 switch (Column(column)) {
257 case Column::DESCRIPTION:
259 return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable;
260 return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable;
262 return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
266 bool ClipList::setData(const QModelIndex &index, const QVariant &value, int role)
268 if (!index.isValid() || role != Qt::EditRole) {
272 const int row = index.row(), column = index.column();
273 if (size_t(row) >= clips.size())
276 switch (Column(column)) {
277 case Column::CAMERA_1:
278 case Column::CAMERA_2:
279 case Column::CAMERA_3:
280 case Column::CAMERA_4: {
281 unsigned stream_idx = column - int(Column::CAMERA_1);
282 clips[row].descriptions[stream_idx] = value.toString().toStdString();
283 emit_data_changed(row);
291 bool PlayList::setData(const QModelIndex &index, const QVariant &value, int role)
293 if (!index.isValid() || role != Qt::EditRole) {
297 const int row = index.row(), column = index.column();
298 if (size_t(row) >= clips.size())
301 switch (Column(column)) {
302 case Column::DESCRIPTION:
303 clips[row].descriptions[clips[row].stream_idx] = value.toString().toStdString();
304 emit_data_changed(row);
306 case Column::CAMERA: {
308 int camera_idx = value.toInt(&ok);
309 if (!ok || camera_idx < 1 || camera_idx > NUM_CAMERAS) {
312 clips[row].stream_idx = camera_idx - 1;
313 emit_data_changed(row);
321 void ClipList::add_clip(const Clip &clip)
323 beginInsertRows(QModelIndex(), clips.size(), clips.size());
324 clips.push_back(clip);
328 void PlayList::add_clip(const Clip &clip)
330 beginInsertRows(QModelIndex(), clips.size(), clips.size());
331 clips.push_back(clip);
335 void PlayList::duplicate_clips(size_t first, size_t last)
337 beginInsertRows(QModelIndex(), first, last);
338 clips.insert(clips.begin() + first, clips.begin() + first, clips.begin() + last + 1);
342 void PlayList::erase_clips(size_t first, size_t last)
344 beginRemoveRows(QModelIndex(), first, last);
345 clips.erase(clips.begin() + first, clips.begin() + last + 1);
349 void PlayList::move_clips(size_t first, size_t last, int delta)
352 beginMoveRows(QModelIndex(), first, last, QModelIndex(), first - 1);
353 rotate(clips.begin() + first - 1, clips.begin() + first, clips.begin() + last + 1);
355 beginMoveRows(QModelIndex(), first, last, QModelIndex(), first + (last-first+1) + 1);
356 first = clips.size() - first - 1;
357 last = clips.size() - last - 1;
358 rotate(clips.rbegin() + last - 1, clips.rbegin() + last, clips.rbegin() + first + 1);
363 void ClipList::emit_data_changed(size_t row)
365 emit dataChanged(index(row, 0), index(row, int(Column::NUM_COLUMNS)));
368 void PlayList::emit_data_changed(size_t row)
370 emit dataChanged(index(row, 0), index(row, int(Column::NUM_COLUMNS)));
373 void PlayList::set_currently_playing(int index, double progress)
375 int old_index = currently_playing_index;
376 int column = int(Column::PLAYING);
377 if (index != old_index) {
378 currently_playing_index = index;
379 play_progress = progress;
380 if (old_index != -1) {
381 emit dataChanged(this->index(old_index, column), this->index(old_index, column));
384 emit dataChanged(this->index(index, column), this->index(index, column));
386 } else if (index != -1 && fabs(progress - play_progress) > 1e-3) {
387 play_progress = progress;
388 emit dataChanged(this->index(index, column), this->index(index, column));