summaryrefslogtreecommitdiffstats
path: root/src/widget/wcoverart.cpp
blob: 5aaff9fed2fca5a50ae8d44cdc2b5e8ebd65de1a (plain)
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
#include <QAction>
#include <QApplication>
#include <QBitmap>
#include <QLabel>
#include <QIcon>
#include <QPainter>

#include "dlgcoverartfullsize.h"
#include "wcoverart.h"
#include "wskincolor.h"
#include "library/coverartcache.h"

WCoverArt::WCoverArt(QWidget* parent,
                     ConfigObject<ConfigValue>* pConfig)
        : QWidget(parent),
          WBaseWidget(this),
          m_pConfig(pConfig),
          m_bEnableWidget(true),
          m_bCoverIsHovered(false),
          m_bCoverIsVisible(false),
          m_pMenu(new WCoverArtMenu(this)),
          m_loadedCover(CoverArtCache::instance()->getDefaultCoverArt()),
          m_lastRequestedTrackId(-1) {
    // load icon to hide cover
    m_iconHide = QPixmap(":/images/library/ic_library_cover_hide.png");
    m_iconHide = m_iconHide.scaled(20,
                                   20,
                                   Qt::KeepAspectRatioByExpanding,
                                   Qt::SmoothTransformation);

    // load icon to show cover
    m_iconShow = QPixmap(":/images/library/ic_library_cover_show.png");
    m_iconShow = m_iconShow.scaled(17,
                                   17,
                                   Qt::KeepAspectRatioByExpanding,
                                   Qt::SmoothTransformation);

    connect(CoverArtCache::instance(), SIGNAL(pixmapFound(int, QPixmap)),
            this, SLOT(slotPixmapFound(int, QPixmap)), Qt::DirectConnection);
}

WCoverArt::~WCoverArt() {
    delete m_pMenu;
}

void WCoverArt::setup(QDomNode node, const SkinContext& context) {
    Q_UNUSED(node);
    setMouseTracking(TRUE);

    // Background color
    QColor bgc(255,255,255);
    if (context.hasNode(node, "BgColor")) {
        bgc.setNamedColor(context.selectString(node, "BgColor"));
        setAutoFillBackground(true);
    }
    QPalette pal = palette();
    pal.setBrush(backgroundRole(), WSkinColor::getCorrectColor(bgc));

    // Foreground color
    QColor m_fgc(0,0,0);
    if (context.hasNode(node, "FgColor")) {
        m_fgc.setNamedColor(context.selectString(node, "FgColor"));
    }
    bgc = WSkinColor::getCorrectColor(bgc);
    m_fgc = QColor(255 - bgc.red(), 255 - bgc.green(), 255 - bgc.blue());
    pal.setBrush(foregroundRole(), m_fgc);
    setPalette(pal);
}

void WCoverArt::slotEnableWidget(bool enable) {
    m_bEnableWidget = enable;
    setMinimumSize(0, 0);
    update();
}

void WCoverArt::slotResetWidget() {
    m_lastRequestedTrackId = -1;
    m_lastRequestedCover = qMakePair(QString(), QString());
    m_bCoverIsVisible = false;
    m_bCoverIsHovered = false;
    m_loadedCover = CoverArtCache::instance()->getDefaultCoverArt();
    m_loadedCover = scaledCoverArt(m_loadedCover);
    setMinimumSize(0, 20);
    update();
}

void WCoverArt::slotPixmapFound(int trackId, QPixmap pixmap) {
    if (!m_bEnableWidget) {
        return;
    }
    if (m_lastRequestedTrackId == trackId) {
        m_loadedCover = scaledCoverArt(pixmap);
        update();
    }
}

void WCoverArt::slotLoadCoverArt(const QString& coverLocation,
                                 const QString& md5Hash,
                                 int trackId, bool cachedOnly) {
    if (!m_bEnableWidget) {
        return;
    }

    m_lastRequestedTrackId = trackId;
    m_lastRequestedCover = qMakePair(coverLocation, md5Hash);

    if (!m_bCoverIsVisible) {
        return;
    }

    m_loadedCover = CoverArtCache::instance()->requestPixmap(trackId,
                                                             coverLocation,
                                                             md5Hash);
    if (m_loadedCover.isNull()) {
        m_loadedCover = CoverArtCache::instance()->getDefaultCoverLocation();
    }
    m_loadedCover = scaledCoverArt(m_loadedCover);
    update();
}

QPixmap WCoverArt::scaledCoverArt(QPixmap normal) {
    int height = parentWidget()->height() / 3;
    return normal.scaled(QSize(height - 10, height - 10),
                         Qt::KeepAspectRatio,
                         Qt::SmoothTransformation);
}

void WCoverArt::paintEvent(QPaintEvent*) {
    if (!m_bEnableWidget) {
        return;
    }

    QPainter painter(this);
    painter.drawLine(0, 0, width(), 0);

    if (m_bCoverIsVisible) {
        int x = width() / 2 - height() / 2 + 4;
        int y = 6;
        painter.drawPixmap(x, y, m_loadedCover);
    } else {
        painter.drawPixmap(1, 2 ,m_iconShow);
        painter.drawText(25, 15, tr("Show Cover Art"));
    }

    if (m_bCoverIsVisible && m_bCoverIsHovered) {
        painter.drawPixmap(width() - 21, 6, m_iconHide);
    }
}

void WCoverArt::resizeEvent(QResizeEvent*) {
    if (!m_bEnableWidget) {
        setMinimumSize(0, 0);
        return;
    }

    if (m_bCoverIsVisible) {
        setMinimumSize(0, parentWidget()->height() / 3);
        slotLoadCoverArt(m_lastRequestedCover.first,
                         m_lastRequestedCover.second,
                         m_lastRequestedTrackId, true);
     } else {
        m_loadedCover = CoverArtCache::instance()->getDefaultCoverArt();
        setMinimumSize(0, 20);
        DlgCoverArtFullSize::instance()->close();
    }
}

void WCoverArt::mousePressEvent(QMouseEvent* event) {
    if (!m_bEnableWidget) {
        return;
    }

    if (!m_bCoverIsVisible) { // show widget
        m_bCoverIsVisible = true;
        resize(sizeHint());
        return;
    }

    QPoint lastPoint(event->pos());
    if(lastPoint.x() > width() - (height() / 5)
            && lastPoint.y() < (height() / 5) + 5) { // hide widget
        m_bCoverIsVisible = false;
        resize(sizeHint());
    } else if (event->button() == Qt::RightButton) { // show context-menu
        m_pMenu->show(event->globalPos(),
                      m_lastRequestedCover,
                      m_lastRequestedTrackId);
    }
}

void WCoverArt::mouseMoveEvent(QMouseEvent* event) {
    m_bCoverIsHovered  = event->HoverEnter;
    update();
}

void WCoverArt::leaveEvent(QEvent*) {
    m_bCoverIsHovered = false;
    update();
}