1 #include "widget/wtrackwidgetgroup.h"
2 
3 #include <QDebug>
4 #include <QStylePainter>
5 #include <QUrl>
6 
7 #include "control/controlobject.h"
8 #include "moc_wtrackwidgetgroup.cpp"
9 #include "track/track.h"
10 #include "util/dnd.h"
11 #include "widget/wtrackmenu.h"
12 
13 namespace {
14 
15 constexpr int kDefaultTrackColorAlpha = 255;
16 
17 const WTrackMenu::Features kTrackMenuFeatures =
18         WTrackMenu::Feature::Playlist |
19         WTrackMenu::Feature::Crate |
20         WTrackMenu::Feature::Metadata |
21         WTrackMenu::Feature::Reset |
22         WTrackMenu::Feature::BPM |
23         WTrackMenu::Feature::Color |
24         WTrackMenu::Feature::FileBrowser |
25         WTrackMenu::Feature::Properties;
26 
27 } // anonymous namespace
28 
WTrackWidgetGroup(QWidget * pParent,UserSettingsPointer pConfig,TrackCollectionManager * pTrackCollectionManager,const QString & group)29 WTrackWidgetGroup::WTrackWidgetGroup(QWidget* pParent,
30         UserSettingsPointer pConfig,
31         TrackCollectionManager* pTrackCollectionManager,
32         const QString& group)
33         : WWidgetGroup(pParent),
34           m_group(group),
35           m_pConfig(pConfig),
36           m_trackColorAlpha(kDefaultTrackColorAlpha),
37           m_pTrackMenu(make_parented<WTrackMenu>(
38                   this, pConfig, pTrackCollectionManager, kTrackMenuFeatures)) {
39     setAcceptDrops(true);
40 }
41 
42 WTrackWidgetGroup::~WTrackWidgetGroup() = default;
43 
setup(const QDomNode & node,const SkinContext & context)44 void WTrackWidgetGroup::setup(const QDomNode& node, const SkinContext& context) {
45     WWidgetGroup::setup(node, context);
46 
47     bool ok = false;
48     int trackColorAlpha = context.selectInt(
49             node,
50             QStringLiteral("TrackColorAlpha"),
51             &ok);
52     if (ok && trackColorAlpha >= 0 && trackColorAlpha <= 255) {
53         m_trackColorAlpha = trackColorAlpha;
54     }
55 }
56 
slotTrackLoaded(TrackPointer pTrack)57 void WTrackWidgetGroup::slotTrackLoaded(TrackPointer pTrack) {
58     if (!pTrack) {
59         return;
60     }
61     m_pCurrentTrack = pTrack;
62     connect(pTrack.get(),
63             &Track::changed,
64             this,
65             &WTrackWidgetGroup::slotTrackChanged);
66     updateColor();
67 }
68 
slotLoadingTrack(TrackPointer pNewTrack,TrackPointer pOldTrack)69 void WTrackWidgetGroup::slotLoadingTrack(TrackPointer pNewTrack, TrackPointer pOldTrack) {
70     Q_UNUSED(pNewTrack);
71     Q_UNUSED(pOldTrack);
72     if (m_pCurrentTrack) {
73         disconnect(m_pCurrentTrack.get(), nullptr, this, nullptr);
74     }
75     m_pCurrentTrack.reset();
76     updateColor();
77 }
78 
slotTrackChanged(TrackId trackId)79 void WTrackWidgetGroup::slotTrackChanged(TrackId trackId) {
80     Q_UNUSED(trackId);
81     updateColor();
82 }
83 
updateColor()84 void WTrackWidgetGroup::updateColor() {
85     if (m_pCurrentTrack) {
86         m_trackColor = mixxx::RgbColor::toQColor(m_pCurrentTrack->getColor());
87         if (m_trackColor.isValid()) {
88             m_trackColor.setAlpha(m_trackColorAlpha);
89         }
90     } else {
91         m_trackColor = QColor();
92     }
93     update();
94 }
95 
paintEvent(QPaintEvent * pe)96 void WTrackWidgetGroup::paintEvent(QPaintEvent* pe) {
97     WWidgetGroup::paintEvent(pe);
98 
99     if (m_trackColor.isValid()) {
100         QStylePainter p(this);
101 
102         p.fillRect(rect(), QBrush(m_trackColor));
103     }
104 }
105 
mouseMoveEvent(QMouseEvent * event)106 void WTrackWidgetGroup::mouseMoveEvent(QMouseEvent* event) {
107     if (event->buttons().testFlag(Qt::LeftButton) && m_pCurrentTrack) {
108         DragAndDropHelper::dragTrack(m_pCurrentTrack, this, m_group);
109     }
110 }
111 
dragEnterEvent(QDragEnterEvent * event)112 void WTrackWidgetGroup::dragEnterEvent(QDragEnterEvent* event) {
113     DragAndDropHelper::handleTrackDragEnterEvent(event, m_group, m_pConfig);
114 }
115 
dropEvent(QDropEvent * event)116 void WTrackWidgetGroup::dropEvent(QDropEvent* event) {
117     DragAndDropHelper::handleTrackDropEvent(event, *this, m_group, m_pConfig);
118 }
119 
contextMenuEvent(QContextMenuEvent * event)120 void WTrackWidgetGroup::contextMenuEvent(QContextMenuEvent* event) {
121     event->accept();
122     if (m_pCurrentTrack) {
123         m_pTrackMenu->loadTrack(m_pCurrentTrack);
124         // Create the right-click menu
125         m_pTrackMenu->popup(event->globalPos());
126     }
127 }
128