]> git.sesse.net Git - nageru/blob - clip_list.cpp
Make the grid display a bit better formatted.
[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 ClipList::columnCount(const QModelIndex &parent) const {
50         if (parent.isValid()) return 0;
51         if (display_type == ListDisplay::CLIP_LIST) {
52                 return int(ClipListColumn::NUM_COLUMNS);
53         } else {
54                 return int(PlayListColumn::NUM_COLUMNS);
55         }
56 }
57
58 QVariant ClipList::data(const QModelIndex &parent, int role) const {
59         if (!parent.isValid())
60                 return QVariant();
61         const int row = parent.row(), column = parent.column();
62         if (size_t(row) >= clips.size())
63                 return QVariant();
64
65         if (role == Qt::TextAlignmentRole) {
66                 if (display_type == ListDisplay::CLIP_LIST) {
67                         switch (ClipListColumn(column)) {
68                         case ClipListColumn::IN:
69                         case ClipListColumn::OUT:
70                         case ClipListColumn::DURATION:
71                                 return Qt::AlignRight + Qt::AlignVCenter;
72                         default:
73                                 return Qt::AlignLeft + Qt::AlignVCenter;
74                         }
75                 } else {
76                         switch (PlayListColumn(column)) {
77                         case PlayListColumn::PLAYING:
78                                 return Qt::AlignCenter;
79                         case PlayListColumn::IN:
80                         case PlayListColumn::OUT:
81                         case PlayListColumn::DURATION:
82                                 return Qt::AlignRight + Qt::AlignVCenter;
83                         case PlayListColumn::CAMERA:
84                                 return Qt::AlignCenter;
85                         default:
86                                 return Qt::AlignLeft + Qt::AlignVCenter;
87                         }
88                 }
89         }
90
91         if (role != Qt::DisplayRole)
92                 return QVariant();
93
94         if (display_type == ListDisplay::CLIP_LIST) {
95                 switch (ClipListColumn(column)) {
96                 case ClipListColumn::IN:
97                         return QString::fromStdString(pts_to_string(clips[row].pts_in));
98                 case ClipListColumn::OUT:
99                         if (clips[row].pts_out >= 0) {
100                                 return QString::fromStdString(pts_to_string(clips[row].pts_out));
101                         } else {
102                                 return QVariant();
103                         }
104                 case ClipListColumn::DURATION:
105                         if (clips[row].pts_out >= 0) {
106                                 return QString::fromStdString(duration_to_string(clips[row].pts_out - clips[row].pts_in));
107                         } else {
108                                 return QVariant();
109                         }
110                 default:
111                         return "";
112                 }
113         } else {
114                 switch (PlayListColumn(column)) {
115                 case PlayListColumn::PLAYING:
116                         return (row == currently_playing_index) ? "→" : "";
117                 case PlayListColumn::IN:
118                         return QString::fromStdString(pts_to_string(clips[row].pts_in));
119                 case PlayListColumn::OUT:
120                         if (clips[row].pts_out >= 0) {
121                                 return QString::fromStdString(pts_to_string(clips[row].pts_out));
122                         } else {
123                                 return QVariant();
124                         }
125                 case PlayListColumn::DURATION:
126                         if (clips[row].pts_out >= 0) {
127                                 return QString::fromStdString(duration_to_string(clips[row].pts_out - clips[row].pts_in));
128                         } else {
129                                 return QVariant();
130                         }
131                 case PlayListColumn::CAMERA:
132                         return qlonglong(clips[row].stream_idx + 1);
133                 default:
134                         return "";
135                 }
136         }
137 }
138
139 QVariant ClipList::headerData(int section, Qt::Orientation orientation, int role) const {
140         if (role != Qt::DisplayRole)
141                 return QVariant();
142         if (orientation != Qt::Horizontal)
143                 return QVariant();
144
145         if (display_type == ListDisplay::CLIP_LIST) {
146                 switch (ClipListColumn(section)) {
147                 case ClipListColumn::IN:
148                         return "In";
149                 case ClipListColumn::OUT:
150                         return "Out";
151                 case ClipListColumn::DURATION:
152                         return "Duration";
153                 case ClipListColumn::CAMERA_1:
154                         return "Camera 1";
155                 case ClipListColumn::CAMERA_2:
156                         return "Camera 2";
157                 case ClipListColumn::CAMERA_3:
158                         return "Camera 3";
159                 case ClipListColumn::CAMERA_4:
160                         return "Camera 4";
161                 default:
162                         return "";
163                 }
164         } else {
165                 switch (PlayListColumn(section)) {
166                 case PlayListColumn::PLAYING:
167                         return "";
168                 case PlayListColumn::IN:
169                         return "In";
170                 case PlayListColumn::OUT:
171                         return "Out";
172                 case PlayListColumn::DURATION:
173                         return "Duration";
174                 case PlayListColumn::CAMERA:
175                         return "Camera";
176                 case PlayListColumn::DESCRIPTION:
177                         return "Description";
178                 default:
179                         return "";
180                 }
181         }
182 }
183
184 void ClipList::add_clip(const Clip &clip)
185 {
186         beginInsertRows(QModelIndex(), clips.size(), clips.size());
187         clips.push_back(clip);
188         endInsertRows();
189 }
190
191 void ClipList::emit_data_changed(size_t row)
192 {
193         if (display_type == ListDisplay::CLIP_LIST) {
194                 emit dataChanged(index(row, 0), index(row, int(ClipListColumn::NUM_COLUMNS)));
195         } else {
196                 emit dataChanged(index(row, 0), index(row, int(PlayListColumn::NUM_COLUMNS)));
197         }
198 }
199
200 void ClipList::set_currently_playing(int index)
201 {
202         int old_index = currently_playing_index;
203         if (index != old_index) {
204                 currently_playing_index = index;
205                 if (old_index != -1) {
206                         emit_data_changed(old_index);
207                 }
208                 if (index != -1) {
209                         emit_data_changed(index);
210                 }
211         }
212 }