Debian Codegraph @

au4/src/playback/view/toolbars/components/timecodemodel.cpp

raw
1 /*
2 * Audacity: A Digital Audio Editor
3 */
4 #include "timecodemodel.h"
5
6 #include "uicomponents/view/menuitem.h"
7 #include "ui/uiaction.h"
8
9 #include "numericformatter.h"
10 #include "beatsformatter.h"
11
12 #include "translation.h"
13 #include "log.h"
14
15 using namespace au::playback;
16 using namespace muse::uicomponents;
17 using namespace muse::ui;
18
19 static bool isFieldEditable(const QChar& fieldSymbol)
20 {
21 return fieldSymbol.isDigit();
22 }
23
24 TimecodeModel::TimecodeModel(QObject* parent)
25 : QAbstractListModel(parent)
26 {
27 // translate all
28 m_availableViewFormats = {
29 { ViewFormatType::Seconds, muse::qtrc("playback", "Seconds"), "01000,01000s" },
30 { ViewFormatType::SecondsMilliseconds, muse::qtrc("playback", "Seconds + milliseconds"), "01000,01000>01000 s" },
31 { ViewFormatType::HHMMSS, muse::qtrc("playback", "hh:mm:ss"), "0100 h 060 m 060 s" },
32 { ViewFormatType::DDHHMMSS, muse::qtrc("playback", "dd:hh:mm:ss"), "0100 d 024 h 060 m 060 s" },
33
34 { ViewFormatType::HHMMSSHundredths, muse::qtrc("playback", "hh:mm:ss + hundredths"), "0100 h 060 m 060>0100 s" },
35 { ViewFormatType::HHMMSSMilliseconds, muse::qtrc("playback", "hh:mm:ss + milliseconds"), "0100 h 060 m 060>01000 s" },
36
37 { ViewFormatType::HHMMSSSamples, muse::qtrc("playback", "hh:mm:ss + samples"), "0100 h 060 m 060 s+># samples" },
38 { ViewFormatType::Samples, muse::qtrc("playback", "Samples"), "01000,01000,01000 samples|#" },
39
40 { ViewFormatType::HHMMSSFilmFrames, muse::qtrc("playback", "hh:mm:ss + film frames (24 fps)"), "0100 h 060 m 060 s+>24 frames" },
41 { ViewFormatType::FilmFrames, muse::qtrc("playback", "Film frames (24 fps)"), "01000,01000 frames|24" },
42
43 { ViewFormatType::HHMMSSNTSCDropFrames, muse::qtrc("playback", "hh:mm:ss + NTSC drop frames"), "0100 h 060 m 060 s+>30 frames|N" },
44 { ViewFormatType::HHMMSSNTSCNonDropFrames, muse::qtrc("playback", "hh:mm:ss + NTSC non-drop frames"),
45 "0100 h 060 m 060 s+>030 frames| .999000999" },
46 { ViewFormatType::NTSCFrames, muse::qtrc("playback", "NTSC frames"), "01000,01000 frames|29.97002997" },
47
48 { ViewFormatType::HHMMSSPALFrames, muse::qtrc("playback", "hh:mm:ss + PAL frames (25 fps)"), "0100 h 060 m 060 s+>25 frames" },
49 { ViewFormatType::PALFrames, muse::qtrc("playback", "PAL frames (25 fps)"), "01000,01000 frames|25" },
50
51 { ViewFormatType::HHMMSSCDDAFrames, muse::qtrc("playback", "hh:mm:ss + CDDA frames (25 fps)"), "0100 h 060 m 060 s+>75 frames" },
52 { ViewFormatType::CDDAFrames, muse::qtrc("playback", "CDDA frames (75 fps)"), "01000,01000 frames|75" },
53
54 { ViewFormatType::BarBeat, muse::qtrc("playback", "bar:beat"), "bar:beat" },
55 { ViewFormatType::BarBeatTick, muse::qtrc("playback", "bar:beat:tick"), "bar:beat:tick" },
56 };
57
58 initFieldInteractionController();
59
60 reloadFormatter();
61
62 setValue(0.0);
63 }
64
65 QVariantList TimecodeModel::availableFormats()
66 {
67 muse::uicomponents::MenuItemList result;
68
69 for (const ViewFormat& viewFormat : m_availableViewFormats) {
70 MenuItem* item = new MenuItem(this);
71
72 int id = static_cast<int>(viewFormat.type);
73
74 item->setId(QString::number(id));
75
76 UiAction action;
77 action.title = muse::TranslatableString::untranslatable(viewFormat.title);
78 action.checkable = Checkable::Yes;
79 item->setAction(action);
80
81 UiActionState state;
82 state.enabled = true;
83 state.checked = m_currentFormat == id;
84
85 item->setState(state);
86
87 result << item;
88 }
89
90 return menuItemListToVariantList(result);
91 }
92
93 int TimecodeModel::rowCount(const QModelIndex& parent) const
94 {
95 UNUSED(parent);
96 return m_valueString.size();
97 }
98
99 QVariant TimecodeModel::data(const QModelIndex& index, int role) const
100 {
101 if (!index.isValid()) {
102 return QVariant();
103 }
104
105 QChar ch = m_valueString[index.row()];
106
107 switch (role) {
108 case rSymbol: return QVariant::fromValue(ch);
109 case rIsEditable: return isFieldEditable(ch);
110 }
111
112 return QVariant();
113 }
114
115 QHash<int, QByteArray> TimecodeModel::roleNames() const
116 {
117 static const QHash<int, QByteArray> roles = {
118 { rSymbol, "symbol" },
119 { rIsEditable, "editable" }
120 };
121
122 return roles;
123 }
124
125 double TimecodeModel::value() const
126 {
127 return m_value;
128 }
129
130 void TimecodeModel::setValue(double value)
131 {
132 if (qFuzzyCompare(m_value, value)) {
133 return;
134 }
135
136 m_value = value;
137
138 updateValueString();
139
140 emit valueChanged();
141 }
142
143 int TimecodeModel::currentFormat() const
144 {
145 return m_currentFormat;
146 }
147
148 void TimecodeModel::setCurrentFormat(int format)
149 {
150 if (m_currentFormat == format) {
151 return;
152 }
153
154 m_currentFormat = format;
155
156 reloadFormatter();
157 updateValueString();
158
159 emit currentFormatChanged();
160 emit availableFormatsChanged();
161 emit valueChanged();
162 }
163
164 int TimecodeModel::currentEditedFieldIndex() const
165 {
166 return m_fieldsInteractionController->currentEditedFieldIndex();
167 }
168
169 void TimecodeModel::setCurrentEditedFieldIndex(int index)
170 {
171 m_fieldsInteractionController->setCurrentEditedFieldIndex(index);
172 }
173
174 QQuickItem* TimecodeModel::visualItem() const
175 {
176 return m_fieldsInteractionController->visualItem();
177 }
178
179 void TimecodeModel::setVisualItem(QQuickItem* item)
180 {
181 m_fieldsInteractionController->setVisualItem(item);
182 }
183
184 void TimecodeModel::reloadFormatter()
185 {
186 ViewFormatType format = static_cast<ViewFormatType>(m_currentFormat);
187 if (format == ViewFormatType::BarBeat || format == ViewFormatType::BarBeatTick) {
188 int fracPart = format == ViewFormatType::BarBeat ? 0 : 16;
189 m_formatter = std::make_shared<BeatsFormatter>(m_availableViewFormats[m_currentFormat].formatStr, fracPart);
190 } else {
191 m_formatter = std::make_shared<NumericFormatter>(m_availableViewFormats[m_currentFormat].formatStr);
192 }
193
194 initFormatter();
195
196 m_fieldsInteractionController->setFormatter(m_formatter);
197 }
198
199 void TimecodeModel::initFormatter()
200 {
201 m_formatter->setSampleRate(sampleRate());
202 m_formatter->setTempo(m_tempo);
203 m_formatter->setUpperTimeSignature(m_upperTimeSignature);
204 m_formatter->setLowerTimeSignature(m_lowerTimeSignature);
205
206 m_formatter->init();
207 }
208
209 void TimecodeModel::initFieldInteractionController()
210 {
211 m_fieldsInteractionController = std::make_shared<FieldsInteractionController>(this);
212
213 connect(m_fieldsInteractionController.get(), &FieldsInteractionController::currentEditedFieldIndexChanged,
214 this, &TimecodeModel::currentEditedFieldIndexChanged);
215
216 connect(m_fieldsInteractionController.get(), &FieldsInteractionController::valueChanged,
217 this, &TimecodeModel::setValue);
218 }
219
220 void TimecodeModel::updateValueString()
221 {
222 QString newValueString = m_formatter->valueToString(m_value, false).valueString;
223
224 if (newValueString.size() != m_valueString.size()) {
225 beginResetModel();
226 m_valueString = newValueString;
227 endResetModel();
228 } else {
229 for (int i = 0; i < newValueString.size(); ++i) {
230 if (newValueString[i] != m_valueString[i]) {
231 m_valueString[i] = newValueString[i];
232
233 QModelIndex index = createIndex(i, 0);
234 emit dataChanged(index, index, { rSymbol });
235 }
236 }
237 }
238
239 m_fieldsInteractionController->setValueString(m_valueString);
240 }
241
242 double TimecodeModel::sampleRate() const
243 {
244 return m_sampleRate;
245 }
246
247 void TimecodeModel::setSampleRate(double sampleRate)
248 {
249 if (qFuzzyCompare(m_sampleRate, sampleRate)) {
250 return;
251 }
252
253 m_sampleRate = sampleRate;
254
255 initFormatter();
256 updateValueString();
257
258 emit sampleRateChanged();
259 }
260
261 double TimecodeModel::tempo() const
262 {
263 return m_tempo;
264 }
265
266 void TimecodeModel::setTempo(double tempo)
267 {
268 if (qFuzzyCompare(m_tempo, tempo)) {
269 return;
270 }
271
272 m_tempo = tempo;
273
274 initFormatter();
275 updateValueString();
276
277 emit tempoChanged();
278 }
279
280 int TimecodeModel::upperTimeSignature() const
281 {
282 return m_upperTimeSignature;
283 }
284
285 void TimecodeModel::setUpperTimeSignature(int timeSignature)
286 {
287 if (m_upperTimeSignature == timeSignature) {
288 return;
289 }
290
291 m_upperTimeSignature = timeSignature;
292
293 initFormatter();
294 updateValueString();
295
296 emit upperTimeSignatureChanged();
297 }
298
299 int TimecodeModel::lowerTimeSignature() const
300 {
301 return m_lowerTimeSignature;
302 }
303
304 void TimecodeModel::setLowerTimeSignature(int timeSignature)
305 {
306 if (m_lowerTimeSignature == timeSignature) {
307 return;
308 }
309
310 m_lowerTimeSignature = timeSignature;
311
312 initFormatter();
313 updateValueString();
314
315 emit lowerTimeSignatureChanged();
316 }