-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathdlgtrackinfo.cpp
More file actions
1082 lines (943 loc) · 35.9 KB
/
Copy pathdlgtrackinfo.cpp
File metadata and controls
1082 lines (943 loc) · 35.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "library/dlgtrackinfo.h"
#include <QSignalBlocker>
#include <QStyleFactory>
#include <QtDebug>
#include <cmath>
#include "defs_urls.h"
#include "library/coverartcache.h"
#include "library/coverartutils.h"
#include "library/dlgtagfetcher.h"
#include "library/library_prefs.h"
#include "library/trackmodel.h"
#include "moc_dlgtrackinfo.cpp"
#include "preferences/colorpalettesettings.h"
#include "sources/soundsourceproxy.h"
#include "track/beatutils.h"
#include "track/keyfactory.h"
#include "track/track.h"
#include "util/color/color.h"
#include "util/datetime.h"
#include "util/desktophelper.h"
#include "util/duration.h"
#include "widget/wcoverartlabel.h"
#include "widget/wcoverartmenu.h"
#include "widget/wstarrating.h"
namespace {
constexpr double kBpmTabRounding = 1 / 12.0;
constexpr int kFilterLength = 80;
constexpr int kMinBpm = 30;
// Maximum allowed interval between beats (calculated from kMinBpm).
const mixxx::Duration kMaxInterval = mixxx::Duration::fromMillis(
static_cast<qint64>(1000.0 * (60.0 / kMinBpm)));
const QString kBpmPropertyName = QStringLiteral("bpm");
constexpr double kStandardTuningHz = 440.0;
constexpr double kCentsPerOctave = 1200.0;
} // namespace
DlgTrackInfo::DlgTrackInfo(
UserSettingsPointer pUserSettings,
const TrackModel* trackModel)
// No parent because otherwise it inherits the style parent's
// style which can make it unreadable. Bug #673411
: QDialog(nullptr),
m_pUserSettings(std::move(pUserSettings)),
m_pTrackModel(trackModel),
m_tapFilter(this, kFilterLength, kMaxInterval),
m_pWCoverArtMenu(make_parented<WCoverArtMenu>(this)),
m_pWCoverArtLabel(make_parented<WCoverArtLabel>(this, m_pWCoverArtMenu)),
m_pWStarRating(make_parented<WStarRating>(this)),
m_pColorPicker(make_parented<WColorPickerAction>(
WColorPicker::Option::AllowNoColor |
// TODO(xxx) remove this once the preferences are themed via QSS
WColorPicker::Option::NoExtStyleSheet,
ColorPaletteSettings(m_pUserSettings).getTrackColorPalette(),
this)),
m_widgetSizesFixed(false) {
init();
}
void DlgTrackInfo::init() {
setupUi(this);
setWindowIcon(QIcon(MIXXX_ICON_PATH));
// Store tag edit widget pointers to allow focusing a specific widgets when
// this is opened by double-clicking a WTrackProperty label.
// Associate with property strings taken from library/dao/trackdao.h
m_propertyWidgets.insert("artist", txtArtist);
m_propertyWidgets.insert("title", txtTitle);
m_propertyWidgets.insert("titleInfo", txtTitle);
m_propertyWidgets.insert("album", txtAlbum);
m_propertyWidgets.insert("album_artist", txtAlbumArtist);
m_propertyWidgets.insert("record_label", txtRecordLabel);
m_propertyWidgets.insert("composer", txtComposer);
m_propertyWidgets.insert("genre", txtGenre);
m_propertyWidgets.insert("year", txtYear);
m_propertyWidgets.insert(kBpmPropertyName, spinBpm);
m_propertyWidgets.insert("tracknumber", txtTrackNumber);
m_propertyWidgets.insert("key", txtKey);
m_propertyWidgets.insert("grouping", txtGrouping);
m_propertyWidgets.insert("comment", txtComment);
m_propertyWidgets.insert("color", btnColorPicker);
coverLayout->insertWidget(0, m_pWCoverArtLabel.get());
starsLayout->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
starsLayout->setSpacing(0);
starsLayout->setContentsMargins(0, 0, 0, 0);
starsLayout->insertWidget(0, m_pWStarRating.get());
// This is necessary to pass on mouseMove events to WStarRating
m_pWStarRating->setMouseTracking(true);
if (m_pTrackModel) {
connect(btnNext,
&QPushButton::clicked,
this,
&DlgTrackInfo::slotNextButton);
connect(btnPrev,
&QPushButton::clicked,
this,
&DlgTrackInfo::slotPrevButton);
} else {
btnNext->hide();
btnPrev->hide();
}
// QDialog buttons
connect(btnApply,
&QPushButton::clicked,
this,
&DlgTrackInfo::slotApply);
connect(btnOK,
&QPushButton::clicked,
this,
&DlgTrackInfo::slotOk);
connect(btnCancel,
&QPushButton::clicked,
this,
&DlgTrackInfo::slotCancel);
// BPM edit buttons
connect(bpmHalve, &QPushButton::clicked, this, [this] {
slotBpmScale(mixxx::Beats::BpmScale::Halve);
});
connect(bpmTwoThirds, &QPushButton::clicked, this, [this] {
slotBpmScale(mixxx::Beats::BpmScale::TwoThirds);
});
connect(bpmThreeFourths, &QPushButton::clicked, this, [this] {
slotBpmScale(mixxx::Beats::BpmScale::ThreeFourths);
});
connect(bpmFourFifths, &QPushButton::clicked, this, [this] {
slotBpmScale(mixxx::Beats::BpmScale::FourFifths);
});
connect(bpmFiveFourths, &QPushButton::clicked, this, [this] {
slotBpmScale(mixxx::Beats::BpmScale::FiveFourths);
});
connect(bpmFourThirds, &QPushButton::clicked, this, [this] {
slotBpmScale(mixxx::Beats::BpmScale::FourThirds);
});
connect(bpmThreeHalves, &QPushButton::clicked, this, [this] {
slotBpmScale(mixxx::Beats::BpmScale::ThreeHalves);
});
connect(bpmDouble, &QPushButton::clicked, this, [this] {
slotBpmScale(mixxx::Beats::BpmScale::Double);
});
connect(bpmClear,
&QPushButton::clicked,
this,
&DlgTrackInfo::slotBpmClear);
connect(bpmLock,
&QPushButton::clicked,
this,
&DlgTrackInfo::slotBpmLockClicked);
connect(bpmConst,
#if QT_VERSION >= QT_VERSION_CHECK(6, 7, 0)
&QCheckBox::checkStateChanged,
#else
&QCheckBox::stateChanged,
#endif
this,
&DlgTrackInfo::slotBpmConstChanged);
connect(spinBpm,
QOverload<double>::of(&QDoubleSpinBox::valueChanged),
this,
&DlgTrackInfo::slotSpinBpmValueChanged);
connect(txtKey,
&QLineEdit::editingFinished,
this,
&DlgTrackInfo::slotKeyTextChanged);
connect(spinTuning,
QOverload<double>::of(&QDoubleSpinBox::valueChanged),
this,
&DlgTrackInfo::slotTuningValueChanged);
connect(bpmTap,
&QPushButton::pressed,
&m_tapFilter,
&TapFilter::tap);
connect(&m_tapFilter,
&TapFilter::tapped,
this,
&DlgTrackInfo::slotBpmTap);
// Metadata fields
connect(txtTitle,
&QLineEdit::editingFinished,
this,
[this]() {
txtTitle->setText(txtTitle->text().trimmed());
m_trackRecord.refMetadata().refTrackInfo().setTitle(
txtTitle->text());
});
connect(txtArtist,
&QLineEdit::editingFinished,
this,
[this]() {
txtArtist->setText(txtArtist->text().trimmed());
m_trackRecord.refMetadata().refTrackInfo().setArtist(
txtArtist->text());
});
connect(txtAlbum,
&QLineEdit::editingFinished,
this,
[this]() {
txtAlbum->setText(txtAlbum->text().trimmed());
m_trackRecord.refMetadata().refAlbumInfo().setTitle(
txtAlbum->text());
});
connect(txtAlbumArtist,
&QLineEdit::editingFinished,
this,
[this]() {
txtAlbumArtist->setText(txtAlbumArtist->text().trimmed());
m_trackRecord.refMetadata().refAlbumInfo().setArtist(
txtAlbumArtist->text());
});
connect(txtRecordLabel,
&QLineEdit::editingFinished,
this,
[this]() {
txtRecordLabel->setText(
txtRecordLabel->text().trimmed());
m_trackRecord
.refMetadata()
.refAlbumInfo()
.setRecordLabel(
txtRecordLabel->text());
});
connect(txtGenre,
&QLineEdit::editingFinished,
this,
[this]() {
txtGenre->setText(txtGenre->text().trimmed());
m_trackRecord.refMetadata().refTrackInfo().setGenre(
txtGenre->text());
});
connect(txtComposer,
&QLineEdit::editingFinished,
this,
[this]() {
txtComposer->setText(txtComposer->text().trimmed());
m_trackRecord.refMetadata().refTrackInfo().setComposer(
txtComposer->text());
});
connect(txtGrouping,
&QLineEdit::editingFinished,
this,
[this]() {
txtGrouping->setText(txtGrouping->text().trimmed());
m_trackRecord.refMetadata().refTrackInfo().setGrouping(
txtGrouping->text());
});
connect(txtYear,
&QLineEdit::editingFinished,
this,
[this]() {
txtYear->setText(txtYear->text().trimmed());
m_trackRecord.refMetadata().refTrackInfo().setYear(
txtYear->text());
});
connect(txtTrackNumber,
&QLineEdit::editingFinished,
this,
[this]() {
txtTrackNumber->setText(txtTrackNumber->text().trimmed());
m_trackRecord.refMetadata().refTrackInfo().setTrackNumber(
txtTrackNumber->text());
});
// Import and file browser buttons
connect(btnImportMetadataFromFile,
&QPushButton::clicked,
this,
&DlgTrackInfo::slotImportMetadataFromFile);
connect(btnImportMetadataFromMusicBrainz,
&QPushButton::clicked,
this,
&DlgTrackInfo::slotImportMetadataFromMusicBrainz);
connect(btnOpenFileBrowser,
&QPushButton::clicked,
this,
&DlgTrackInfo::slotOpenInFileBrowser);
// Cover art
CoverArtCache* pCache = CoverArtCache::instance();
if (pCache) {
connect(pCache,
&CoverArtCache::coverFound,
this,
&DlgTrackInfo::slotCoverFound);
}
connect(m_pWCoverArtMenu,
&WCoverArtMenu::coverInfoSelected,
this,
&DlgTrackInfo::slotCoverInfoSelected);
connect(m_pWCoverArtMenu,
&WCoverArtMenu::reloadCoverArt,
this,
&DlgTrackInfo::slotReloadCoverArt);
connect(m_pWStarRating,
&WStarRating::ratingChangeRequest,
this,
&DlgTrackInfo::slotRatingChanged);
btnColorPicker->setStyle(QStyleFactory::create(QStringLiteral("fusion")));
QMenu* pColorPickerMenu = new QMenu(this);
pColorPickerMenu->addAction(m_pColorPicker);
btnColorPicker->setMenu(pColorPickerMenu);
connect(btnColorPicker,
&QPushButton::clicked,
this,
&DlgTrackInfo::slotColorButtonClicked);
connect(m_pColorPicker.get(),
&WColorPickerAction::colorPicked,
this,
[this](const mixxx::RgbColor::optional_t& newColor) {
trackColorDialogSetColor(newColor);
m_trackRecord.setColor(newColor);
});
}
void DlgTrackInfo::slotApply() {
saveTrack();
}
void DlgTrackInfo::slotOk() {
saveTrack();
accept();
}
void DlgTrackInfo::slotCancel() {
reject();
}
void DlgTrackInfo::slotNextButton() {
loadNextTrack();
}
void DlgTrackInfo::slotPrevButton() {
loadPrevTrack();
}
void DlgTrackInfo::slotNextDlgTagFetcher() {
loadNextTrack();
// Do not load track back into DlgTagFetcher since
// it will cause a reload of the same track.
}
void DlgTrackInfo::slotPrevDlgTagFetcher() {
loadPrevTrack();
}
void DlgTrackInfo::loadNextTrack() {
auto nextRow = m_currentTrackIndex.sibling(
m_currentTrackIndex.row() + 1, m_currentTrackIndex.column());
if (nextRow.isValid()) {
loadTrack(nextRow);
emit next();
}
}
void DlgTrackInfo::loadPrevTrack() {
QModelIndex prevRow = m_currentTrackIndex.sibling(
m_currentTrackIndex.row() - 1, m_currentTrackIndex.column());
if (prevRow.isValid()) {
loadTrack(prevRow);
emit previous();
}
}
void DlgTrackInfo::updateFromTrack(const Track& track) {
const QSignalBlocker signalBlocker(this);
setWindowTitle(track.getInfo());
// Cover art, file type and 'date added'
replaceTrackRecord(
track.getRecord(),
track.getLocation());
// paint the color selector and check the respective color picker button
trackColorDialogSetColor(track.getColor());
txtLocation->setText(QDir::toNativeSeparators(track.getLocation()));
reloadTrackBeats(track);
m_pWStarRating->slotSetRating(m_pLoadedTrack->getRating());
}
void DlgTrackInfo::replaceTrackRecord(
mixxx::TrackRecord trackRecord,
const QString& trackLocation) {
// Signals are already blocked
m_trackRecord = std::move(trackRecord);
const auto coverInfo = CoverInfo(
m_trackRecord.getCoverInfo(),
trackLocation);
m_pWCoverArtLabel->setCoverInfoAndPixmap(coverInfo, QPixmap());
// Executed concurrently
CoverArtCache::requestCover(this, coverInfo);
// Non-editable fields
txtType->setText(
m_trackRecord.getFileType());
txtDateAdded->setText(
mixxx::displayLocalDateTime(
mixxx::localDateTimeFromUtc(
m_trackRecord.getDateAdded())));
QFileInfo info(trackLocation);
if (info.exists() && info.isFile()) {
int size = info.size();
QString sizeStr = QLocale().formattedDataSize(size, 1, QLocale::DataSizeSIFormat);
txtFileSize->setText(sizeStr);
}
updateTrackMetadataFields();
}
void DlgTrackInfo::updateTrackMetadataFields() {
const auto metadata = m_trackRecord.getMetadata();
const auto trackInfo = metadata.getTrackInfo();
const auto albumInfo = metadata.getAlbumInfo();
const auto signalInfo = metadata.getStreamInfo().getSignalInfo();
// Editable fields
txtTitle->setText(trackInfo.getTitle());
txtArtist->setText(trackInfo.getArtist());
txtAlbum->setText(albumInfo.getTitle());
txtAlbumArtist->setText(albumInfo.getArtist());
txtGenre->setText(trackInfo.getGenre());
txtComposer->setText(trackInfo.getComposer());
txtGrouping->setText(trackInfo.getGrouping());
txtYear->setText(trackInfo.getYear());
txtTrackNumber->setText(trackInfo.getTrackNumber());
txtComment->setPlainText(trackInfo.getComment());
txtBpm->setText(trackInfo.getBpmText());
displayKeyText();
displayTuningFields();
// Non-editable fields
txtDuration->setText(
metadata.getDurationText(mixxx::Duration::Precision::SECONDS));
QString bitrate = metadata.getBitrateText();
if (bitrate.isEmpty()) {
txtBitrate->clear();
} else {
txtBitrate->setText(bitrate + QChar(' ') + mixxx::audio::Bitrate::unit());
}
txtReplayGain->setText(
mixxx::ReplayGain::ratioToString(
trackInfo.getReplayGain().getRatio()));
auto samplerate = signalInfo.getSampleRate();
if (samplerate.isValid()) {
txtSamplerate->setText(QString::number(samplerate.value()) + " Hz");
} else {
txtSamplerate->clear();
}
}
void DlgTrackInfo::updateSpinBpmFromBeats() {
auto bpmValue = mixxx::Bpm::kValueUndefined;
if (m_pLoadedTrack && m_pBeatsClone) {
const auto trackEndPosition = mixxx::audio::FramePos{
m_pLoadedTrack->getDuration() * m_pBeatsClone->getSampleRate()};
bpmValue = m_pBeatsClone
->getBpmInRange(mixxx::audio::kStartFramePos,
trackEndPosition)
.valueOr(mixxx::Bpm::kValueUndefined);
}
spinBpm->setValue(bpmValue);
}
/// Updates Lock button text and enables/disables all BPM editing controls based on m_bpmLocked.
void DlgTrackInfo::updateBpmEditControls() {
bpmLock->setText(m_bpmLocked ? tr("Unlock BPM") : tr("Lock BPM"));
bpmConst->setEnabled(!m_bpmLocked && m_trackHasBeatMap);
spinBpm->setEnabled(!m_bpmLocked && !m_trackHasBeatMap);
bpmTap->setEnabled(!m_bpmLocked && !m_trackHasBeatMap);
bpmHalve->setEnabled(!m_bpmLocked);
bpmTwoThirds->setEnabled(!m_bpmLocked);
bpmThreeFourths->setEnabled(!m_bpmLocked);
bpmFourFifths->setEnabled(!m_bpmLocked);
bpmFiveFourths->setEnabled(!m_bpmLocked);
bpmFourThirds->setEnabled(!m_bpmLocked);
bpmThreeHalves->setEnabled(!m_bpmLocked);
bpmDouble->setEnabled(!m_bpmLocked);
bpmClear->setEnabled(!m_bpmLocked);
}
void DlgTrackInfo::reloadTrackBeats(const Track& track) {
m_pBeatsClone = track.getBeats();
updateSpinBpmFromBeats();
updateBpmScaleButtonLabels();
m_trackHasBeatMap = m_pBeatsClone && !m_pBeatsClone->hasConstantTempo();
bpmConst->setChecked(!m_trackHasBeatMap);
// Store the lock state from the track into the local staging variable.
// This will only be written back to the track on Apply/OK.
m_bpmLocked = track.isBpmLocked();
updateBpmEditControls();
}
void DlgTrackInfo::loadTrackInternal(const TrackPointer& pTrack) {
clear();
if (!pTrack) {
return;
}
m_pLoadedTrack = pTrack;
updateFromTrack(*m_pLoadedTrack);
m_pWCoverArtLabel->loadTrack(m_pLoadedTrack);
// Listen to changed() so we don't need to listen to individual
// signals such as cuesUpdates, coverArtUpdated(), etc.
connect(pTrack.get(),
&Track::changed,
this,
&DlgTrackInfo::slotTrackChanged);
}
void DlgTrackInfo::loadTrack(TrackPointer pTrack) {
VERIFY_OR_DEBUG_ASSERT(!m_pTrackModel) {
return;
}
loadTrackInternal(pTrack);
if (m_pDlgTagFetcher && m_pDlgTagFetcher->isVisible()) {
m_pDlgTagFetcher->loadTrack(m_pLoadedTrack);
}
}
void DlgTrackInfo::loadTrack(const QModelIndex& index) {
VERIFY_OR_DEBUG_ASSERT(m_pTrackModel) {
return;
}
TrackPointer pTrack = m_pTrackModel->getTrack(index);
VERIFY_OR_DEBUG_ASSERT(pTrack) {
return;
}
m_currentTrackIndex = index;
loadTrackInternal(pTrack);
if (m_pDlgTagFetcher && m_pDlgTagFetcher->isVisible()) {
m_pDlgTagFetcher->loadTrack(m_currentTrackIndex);
}
}
void DlgTrackInfo::focusField(const QString& property) {
if (property.isEmpty()) {
return;
}
auto it = m_propertyWidgets.constFind(property);
if (it != m_propertyWidgets.constEnd()) {
if (property == kBpmPropertyName) {
// If we shall focus the BPM spinbox, switch to BPM tab
tabWidget->setCurrentIndex(tabWidget->indexOf(tabBPM));
}
it.value()->setFocus();
}
}
void DlgTrackInfo::slotCoverFound(
const QObject* pRequester,
const CoverInfo& coverInfo,
const QPixmap& pixmap) {
if (pRequester == this &&
m_pLoadedTrack &&
m_pLoadedTrack->getLocation() == coverInfo.trackLocation) {
m_trackRecord.setCoverInfo(coverInfo);
m_pWCoverArtLabel->setCoverInfoAndPixmap(coverInfo, pixmap);
}
}
void DlgTrackInfo::slotReloadCoverArt() {
VERIFY_OR_DEBUG_ASSERT(m_pLoadedTrack) {
return;
}
slotCoverInfoSelected(
CoverInfoGuesser().guessCoverInfoForTrack(
m_pLoadedTrack));
}
void DlgTrackInfo::slotCoverInfoSelected(const CoverInfoRelative& coverInfo) {
qDebug() << "DlgTrackInfo::slotCoverInfoSelected" << coverInfo;
VERIFY_OR_DEBUG_ASSERT(m_pLoadedTrack) {
return;
}
m_trackRecord.setCoverInfo(coverInfo);
CoverArtCache::requestCover(this, CoverInfo(coverInfo, m_pLoadedTrack->getLocation()));
}
void DlgTrackInfo::slotOpenInFileBrowser() {
if (!m_pLoadedTrack) {
return;
}
mixxx::DesktopHelper::openInFileBrowser(QStringList(m_pLoadedTrack->getLocation()));
}
void DlgTrackInfo::slotColorButtonClicked() {
if (!m_pLoadedTrack) {
return;
}
btnColorPicker->showMenu();
}
void DlgTrackInfo::trackColorDialogSetColor(const mixxx::RgbColor::optional_t& newColor) {
m_pColorPicker->setSelectedColor(newColor);
btnColorPicker->menu()->close();
if (newColor) {
btnColorPicker->setText("");
const QColor ccolor = mixxx::RgbColor::toQColor(newColor);
const QString styleSheet =
QStringLiteral(
"QPushButton { background-color: %1; color: %2; }")
.arg(ccolor.name(QColor::HexRgb),
Color::isDimColor(ccolor)
? "white"
: "black");
btnColorPicker->setStyleSheet(styleSheet);
} else { // no color
btnColorPicker->setText(tr("(no color)"));
// clear custom stylesheet, i.e. restore Fusion style,
btnColorPicker->setStyleSheet("");
}
}
void DlgTrackInfo::saveTrack() {
qDebug() << "DlgTrackInfo::saveTrack() called";
if (!m_pLoadedTrack) {
return;
}
// In case Apply is triggered by hotkey AND a QLineEdit with pending changes
// is focused AND the user did not hit Enter to finish editing,
// the content of that focused line edit would be reset to the last confirmed state.
// This hack makes a focused QLineEdit emit editingFinished() (clearFocus()
// implicitly emits a focusOutEvent()
if (this == QApplication::activeWindow()) {
auto* pFocusWidget = QApplication::focusWidget();
if (pFocusWidget) {
pFocusWidget->clearFocus();
pFocusWidget->setFocus();
}
}
// Special case handling for the comment field that is not
// updated by the editingFinished signal.
m_trackRecord.refMetadata().refTrackInfo().setComment(txtComment->toPlainText());
m_trackRecord
.refMetadata()
.refAlbumInfo()
.setRecordLabel(
txtRecordLabel->text().trimmed());
// First, disconnect the track changed signal. Otherwise we signal ourselves
// and repopulate all these fields.
const QSignalBlocker signalBlocker(this);
// If the user is editing the bpm or key and hits enter to close DlgTrackInfo,
// the editingFinished signal will not fire in time. Invoke the connected
// handlers manually to capture any changes. If the bpm or key was unchanged
// or invalid then the change will be ignored/rejected.
slotSpinBpmValueChanged(spinBpm->value());
updateKeyText();
slotTuningValueChanged(spinTuning->value());
m_trackRecord.setBpmLocked(m_bpmLocked);
// Update the cached track
//
// If replaceRecord() returns true then both m_trackRecord and m_pBeatsClone
// will be updated by the subsequent Track::changed() signal to keep them
// synchronized with the track. Otherwise the track has not been modified and
// both members must remain valid. Do not use std::move() for passing arguments!
// Else triggering apply twice in quick succession might clear the metadata.
m_pLoadedTrack->replaceRecord(m_trackRecord, m_pBeatsClone);
}
void DlgTrackInfo::clear() {
const QSignalBlocker signalBlocker(this);
setWindowTitle(QString());
if (m_pLoadedTrack) {
disconnect(m_pLoadedTrack.get(),
&Track::changed,
this,
&DlgTrackInfo::slotTrackChanged);
m_pLoadedTrack.reset();
}
resetTrackRecord();
m_pBeatsClone.reset();
m_bpmLocked = false;
updateSpinBpmFromBeats();
txtLocation->setText("");
m_pWStarRating->slotSetRating(0);
}
void DlgTrackInfo::slotBpmScale(mixxx::Beats::BpmScale bpmScale) {
if (!m_pBeatsClone) {
return;
}
const auto scaledBeats = m_pBeatsClone->tryScale(bpmScale);
if (scaledBeats) {
m_pBeatsClone = *scaledBeats;
updateSpinBpmFromBeats();
updateBpmScaleButtonLabels();
}
}
void DlgTrackInfo::updateBpmScaleButtonLabels() {
// Get current BPM from the spinbox
const double bpm = spinBpm->value();
auto formatLabel = [bpm](const QString& baseLabel, double scale) -> QString {
if (bpm <= 0) {
return baseLabel;
}
const double scaledBpm = bpm * scale;
QLocale loc;
QString scaledBpmStr = loc.toString(scaledBpm, 'f', 2);
while (scaledBpmStr.endsWith('0')) {
scaledBpmStr.chop(1);
}
if (scaledBpmStr.endsWith(loc.decimalPoint())) {
scaledBpmStr.chop(1);
}
return QStringLiteral("%1 | %2 BPM").arg(baseLabel, scaledBpmStr);
};
bpmHalve->setText(formatLabel(tr("1/2 BPM"), 0.5));
bpmTwoThirds->setText(formatLabel(tr("2/3 BPM"), 2.0 / 3.0));
bpmThreeFourths->setText(formatLabel(tr("3/4 BPM"), 3.0 / 4.0));
bpmFourFifths->setText(formatLabel(tr("4/5 BPM"), 4.0 / 5.0));
bpmFiveFourths->setText(formatLabel(tr("5/4 BPM"), 5.0 / 4.0));
bpmFourThirds->setText(formatLabel(tr("4/3 BPM"), 4.0 / 3.0));
bpmThreeHalves->setText(formatLabel(tr("3/2 BPM"), 3.0 / 2.0));
bpmDouble->setText(formatLabel(tr("2x BPM"), 2.0));
}
void DlgTrackInfo::slotBpmClear() {
m_pBeatsClone.reset();
updateSpinBpmFromBeats();
updateBpmScaleButtonLabels();
bpmConst->setChecked(true);
bpmConst->setEnabled(m_trackHasBeatMap);
spinBpm->setEnabled(true);
bpmTap->setEnabled(true);
}
void DlgTrackInfo::slotBpmLockClicked() {
if (!m_pLoadedTrack) {
return;
}
m_bpmLocked = !m_bpmLocked;
updateBpmEditControls();
}
#if QT_VERSION >= QT_VERSION_CHECK(6, 7, 0)
void DlgTrackInfo::slotBpmConstChanged(Qt::CheckState state) {
#else
void DlgTrackInfo::slotBpmConstChanged(int state) {
#endif
if (state == Qt::Unchecked) {
// try to reload BeatMap from the Track
reloadTrackBeats(*m_pLoadedTrack);
return;
}
spinBpm->setEnabled(true);
bpmTap->setEnabled(true);
slotSpinBpmValueChanged(spinBpm->value());
}
void DlgTrackInfo::slotBpmTap(double averageLength, int numSamples) {
Q_UNUSED(numSamples);
if (averageLength == 0) {
return;
}
auto averageBpm = mixxx::Bpm(60.0 * 1000.0 / averageLength);
averageBpm = BeatUtils::roundBpmWithinRange(averageBpm - kBpmTabRounding,
averageBpm,
averageBpm + kBpmTabRounding);
if (averageBpm != m_lastTapedBpm) {
m_lastTapedBpm = averageBpm;
spinBpm->setValue(averageBpm.valueOr(mixxx::Bpm::kValueUndefined));
}
}
void DlgTrackInfo::slotSpinBpmValueChanged(double value) {
const auto bpm = mixxx::Bpm(value);
if (!bpm.isValid()) {
m_pBeatsClone.reset();
return;
}
if (m_pLoadedTrack) {
if (m_pBeatsClone) {
const auto trackEndPosition = mixxx::audio::FramePos{
m_pLoadedTrack->getDuration() * m_pBeatsClone->getSampleRate()};
const mixxx::Bpm oldBpm = m_pBeatsClone->getBpmInRange(
mixxx::audio::kStartFramePos, trackEndPosition);
if (oldBpm == bpm) {
return;
}
m_pBeatsClone = m_pBeatsClone->trySetBpm(bpm).value_or(m_pBeatsClone);
} else {
mixxx::audio::FramePos cuePosition = m_pLoadedTrack->getMainCuePosition();
// This should never happen, but we cannot be sure
VERIFY_OR_DEBUG_ASSERT(cuePosition.isValid()) {
cuePosition = mixxx::audio::kStartFramePos;
}
m_pBeatsClone = mixxx::Beats::fromConstTempo(
m_pLoadedTrack->getSampleRate(),
// Cue positions might be fractional, i.e. not on frame boundaries!
cuePosition.toNearestFrameBoundary(),
bpm);
}
}
updateSpinBpmFromBeats();
updateBpmScaleButtonLabels();
}
void DlgTrackInfo::updateKeyText() {
const auto keyText = txtKey->text();
m_trackRecord.updateGlobalKeyNormalizeText(
keyText,
mixxx::track::io::key::USER);
displayKeyText();
}
void DlgTrackInfo::displayKeyText() {
const QString keyText = KeyUtils::keyToString(m_trackRecord.getKeys().getGlobalKey());
txtKey->setText(keyText);
}
void DlgTrackInfo::displayTuningFields() {
const double tuningHz =
m_trackRecord.getKeys().getGlobalTuningFrequencyHz();
const QSignalBlocker blocker(spinTuning);
if (tuningHz > 0.0) {
// Block signals to avoid triggering slotTuningValueChanged
// while we are just loading data into the widget.
spinTuning->setValue(tuningHz);
const double cents = kCentsPerOctave *
std::log2(tuningHz / kStandardTuningHz);
const int centsRounded = static_cast<int>(std::lround(cents));
const QString offsetText = centsRounded >= 0
? QStringLiteral("+%1 ct").arg(centsRounded)
: QStringLiteral("%1 ct").arg(centsRounded);
txtTuningCents->setText(offsetText);
} else {
// No tuning data: set to minimum (triggers specialValueText = blank)
spinTuning->setValue(spinTuning->minimum());
txtTuningCents->clear();
}
}
void DlgTrackInfo::slotTuningValueChanged(double value) {
// Store the user-entered Hz value in the Keys protobuf
Keys keys = m_trackRecord.getKeys();
if (value <= spinTuning->minimum()) {
// Special value (minimum) means "no tuning set" — store 0 Hz
keys.setGlobalTuningFrequencyHz(0.0);
m_trackRecord.setKeys(std::move(keys));
txtTuningCents->clear();
return;
}
keys.setGlobalTuningFrequencyHz(value);
m_trackRecord.setKeys(std::move(keys));
// Update the cents offset label so the user gets immediate feedback
const double cents = kCentsPerOctave *
std::log2(value / kStandardTuningHz);
const int centsRounded = static_cast<int>(std::lround(cents));
const QString offsetText = centsRounded >= 0
? QStringLiteral("+%1 ct").arg(centsRounded)
: QStringLiteral("%1 ct").arg(centsRounded);
txtTuningCents->setText(offsetText);
}
void DlgTrackInfo::slotKeyTextChanged() {
updateKeyText();
}
void DlgTrackInfo::slotRatingChanged(int rating) {
if (!m_pLoadedTrack) {
return;
}
if (m_trackRecord.isValidRating(rating) &&
rating != m_trackRecord.getRating()) {
m_pWStarRating->slotSetRating(rating);
m_trackRecord.setRating(rating);
}
}
void DlgTrackInfo::slotImportMetadataFromFile() {
if (!m_pLoadedTrack) {
return;
}
// Initialize the metadata with the current metadata to avoid
// losing existing metadata or to lose the beat grid by replacing
// it with a default grid created from an imprecise BPM.
// See also: https://github.com/mixxxdj/mixxx/issues/10420
// In addition we need to preserve all other track properties
// that are stored in TrackRecord, which serves as the underlying
// model for this dialog.
mixxx::TrackRecord trackRecord = m_pLoadedTrack->getRecord();
mixxx::TrackMetadata trackMetadata = trackRecord.getMetadata();
const auto resetMissingTagMetadata =
m_pUserSettings->getValue<bool>(
mixxx::library::prefs::
kResetMissingTagMetadataOnImportConfigKey);
constexpr QImage* pNoCoverImport = nullptr;
const auto importTrackMetadata = [&](mixxx::TrackMetadata* metadata) {
return SoundSourceProxy(m_pLoadedTrack)
.importTrackMetadataAndCoverImage(
metadata,
pNoCoverImport,
resetMissingTagMetadata);
};
const auto [importResult, sourceSynchronizedAt] =
importTrackMetadata(&trackMetadata);
if (importResult != mixxx::MetadataSource::ImportResult::Succeeded) {
return;
}
const mixxx::FileInfo fileInfo = m_pLoadedTrack->getFileInfo();
trackRecord.replaceMetadataFromSource(
std::move(trackMetadata),
sourceSynchronizedAt);
QString importedKeyText =
trackRecord.getMetadata().getTrackInfo().getKeyText();
{
Keys newKeys = KeyFactory::makeBasicKeysKeepText(
importedKeyText,
mixxx::track::io::key::FILE_METADATA);
if (newKeys.getGlobalKey() != mixxx::track::io::key::INVALID &&
trackRecord.getKeys().getGlobalKeyText() !=
importedKeyText) {
// Only replace the keys with a single new key if valid and different.
// Otherwise preserve existing array of keys for different positions.
trackRecord.setKeys(std::move(newKeys));
}
}
replaceTrackRecord(std::move(trackRecord), fileInfo.location());
}
void DlgTrackInfo::slotTrackChanged(TrackId trackId) {
if (m_pLoadedTrack &&
m_pLoadedTrack->getId() == trackId) {
updateFromTrack(*m_pLoadedTrack);
}
}
void DlgTrackInfo::slotImportMetadataFromMusicBrainz() {
if (!m_pDlgTagFetcher) {