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
|
/**
* Copyright (C) 2004 Scott Wheeler <wheeler@kde.org>
* Copyright (C) 2009 Michael Pyne <mpyne@kde.org>
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation; either version 2 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "nowplaying.h"
#include <KIO/StoredTransferJob>
#include <KJobWidgets>
#include <KLocalizedString>
#include <kiconloader.h>
#include <QApplication>
#include <QDrag>
#include <QDragEnterEvent>
#include <QDropEvent>
#include <QFontDatabase>
#include <QFontMetrics>
#include <QImage>
#include <QLabel>
#include <QLayout>
#include <QList>
#include <QMouseEvent>
#include <QPoint>
#include <QTimer>
#include <QUrl>
#include <QVBoxLayout>
#include "playlistcollection.h"
#include "playlistitem.h"
#include "coverinfo.h"
#include "juktag.h"
#include "collectionlist.h"
#include "juk_debug.h"
////////////////////////////////////////////////////////////////////////////////
// NowPlaying
////////////////////////////////////////////////////////////////////////////////
NowPlaying::NowPlaying(QWidget *parent, PlaylistCollection *collection) :
QWidget(parent),
m_observer(this, collection),
// Also watch the collection
m_collectionListObserver(this, CollectionList::instance()),
m_collection(collection)
{
setObjectName(QLatin1String("NowPlaying"));
QHBoxLayout *layout = new QHBoxLayout(this);
setLayout(layout);
layout->setContentsMargins(0, 0, 0, 0);
layout->setSpacing(3);
// With HiDPI the text might actually be bigger... try to account for
// that.
const QFont defaultLargeFont(QFontDatabase::systemFont(QFontDatabase::TitleFont));
const QFontMetrics fm(defaultLargeFont, this);
const int coverIconHeight = qMax(64, fm.lineSpacing() * 2);
setFixedHeight(coverIconHeight + 4);
layout->addWidget(new CoverItem(this), 0);
layout->addWidget(new TrackItem(this), 2);
hide();
}
void NowPlaying::addItem(NowPlayingItem *item)
{
m_items.append(item);
}
PlaylistCollection *NowPlaying::collection() const
{
return m_collection;
}
void NowPlaying::slotUpdate(const FileHandle &file)
{
m_file = file;
if(file.isNull()) {
hide();
emit nowPlayingHidden();
return;
}
else
show();
for(NowPlayingItem *item : m_items) {
item->update(file);
}
}
void NowPlaying::slotReloadCurrentItem()
{
for(NowPlayingItem *item : m_items) {
item->update(m_file);
}
}
////////////////////////////////////////////////////////////////////////////////
// CoverItem
////////////////////////////////////////////////////////////////////////////////
CoverItem::CoverItem(NowPlaying *parent) :
QLabel(parent),
NowPlayingItem(parent)
{
setObjectName(QLatin1String("CoverItem"));
const QMargins margins = parent->layout()->contentsMargins();
setFixedHeight(parent->height() - (margins.top() + margins.bottom()));
setContentsMargins(1, 1, 1, 1);
setAcceptDrops(true);
}
void CoverItem::update(const FileHandle &file)
{
m_file = file;
if(!file.isNull() && file.coverInfo()->hasCover()) {
show();
const auto pixRatio = this->devicePixelRatioF();
const QSizeF logicalSize = QSizeF(this->height(), this->height());
const QSizeF scaledSize = logicalSize * pixRatio;
QPixmap pix =
file.coverInfo()->pixmap(CoverInfo::FullSize)
.scaled(scaledSize.toSize(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
if (!qFuzzyCompare(pixRatio, 1.0)) {
pix.setDevicePixelRatio(pixRatio);
}
setPixmap(pix);
}
else
hide();
}
void CoverItem::mouseReleaseEvent(QMouseEvent *event)
{
if(m_dragging) {
m_dragging = false;
return;
}
if(rect().contains(event->position().toPoint()) &&
event->button() == Qt::LeftButton &&
m_file.coverInfo()->hasCover())
{
m_file.coverInfo()->popup();
}
QLabel::mousePressEvent(event);
}
void CoverItem::mousePressEvent(QMouseEvent *e)
{
m_dragging = false;
m_dragStart = e->globalPosition().toPoint();
}
void CoverItem::mouseMoveEvent(QMouseEvent *e)
{
if(m_dragging)
return;
QPoint diff = m_dragStart - e->globalPosition().toPoint();
if(diff.manhattanLength() > QApplication::startDragDistance()) {
// Start a drag.
m_dragging = true;
// TODO: Restore CoverDrag and include ability to DnD image data
// rather than a cover ID.
#if 0
QDrag *drag = new QDrag(this);
CoverDrag *data = new CoverDrag(m_file.coverInfo()->coverId());
drag->setMimeData(data);
drag->exec(Qt::CopyAction);
#endif
}
}
void CoverItem::dragEnterEvent(QDragEnterEvent *e)
{
e->setAccepted(false);
}
void CoverItem::dropEvent(QDropEvent *e)
{
qCWarning(JUK_LOG) << "Dropping covers is not currently supported.";
Q_UNUSED(e);
#if 0
QImage image;
QList<QUrl> urls;
coverKey key;
if(e->source() == this)
return;
if(e->mimeData()->hasImage()) {
m_file.coverInfo()->setCover(qvariant_cast<QImage>(e->mimeData()->imageData()));
update(m_file);
}
else {
urls = e->mimeData()->urls();
if(urls.isEmpty())
return;
auto getJob = KIO::storedGet(urls.front());
KJobWidgets::setWindow(getJob, this);
if(getJob->exec()) {
if(image.loadFromData(getJob->data())) {
m_file.coverInfo()->setCover(image);
update(m_file);
}
else
qCCritical(JUK_LOG) << "Unable to load image from " << urls.front();
}
else
qCCritical(JUK_LOG) << "Unable to download " << urls.front();
}
#endif
}
////////////////////////////////////////////////////////////////////////////////
// TrackItem
////////////////////////////////////////////////////////////////////////////////
TrackItem::TrackItem(NowPlaying *parent) :
QWidget(parent),
NowPlayingItem(parent)
{
setObjectName(QLatin1String("TrackItem"));
const QMargins margins = parent->layout()->contentsMargins();
setFixedHeight(parent->height() - (margins.top() + margins.bottom()));
setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
QVBoxLayout *layout = new QVBoxLayout(this);
m_label = new QLabel(this);
m_label->setWordWrap(true);
m_label->setTextInteractionFlags(Qt::LinksAccessibleByMouse|Qt::LinksAccessibleByKeyboard|Qt::TextSelectableByMouse);
layout->addStretch();
layout->addWidget(m_label, 1);
layout->addStretch();
connect(m_label, SIGNAL(linkActivated(QString)), this,
SLOT(slotOpenLink(QString)));
// Ensure that if we're filtering results, that the filtering is cleared if we
// hide the now playing bar so that the user can select tracks normally.
connect(parent, SIGNAL(nowPlayingHidden()), SLOT(slotClearShowMore()));
}
void TrackItem::update(const FileHandle &file)
{
m_file = file;
QTimer::singleShot(0, this, SLOT(slotUpdate()));
}
void TrackItem::slotOpenLink(const QString &link)
{
PlaylistCollection *collection = parentManager()->collection();
if(link == "artist")
collection->showMore(m_file.tag()->artist());
else if(link == "album")
collection->showMore(QString(), m_file.tag()->album());
else if(link == "clear")
collection->clearShowMore();
update(m_file);
}
void TrackItem::slotUpdate()
{
if(m_file.isNull()) {
m_label->setText(QString());
return;
}
const QString title = m_file.tag()->title().toHtmlEscaped();
const QString artist = m_file.tag()->artist().toHtmlEscaped();
const QString album = m_file.tag()->album().toHtmlEscaped();
const QString separator =
(artist.isEmpty() || album.isEmpty())
? QString() : QString(" - ");
// This block-o-nastiness makes the font smaller and smaller until it actually fits.
int size = 4;
QString format =
"<font size=\"+%1\"><b>%2</b></font>"
"<br />"
"<font size=\"+%3\"><b><a href=\"artist\">%4</a>%5<a href=\"album\">%6</a></b>";
if(parentManager()->collection()->showMoreActive())
format.append(QString(" (<a href=\"clear\">%1</a>)").arg(i18n("back to playlist")));
format.append("</font>");
int parentHeight = parentManager()->contentsRect().height();
int neededHeight = 0;
do {
m_label->setText(format.arg(size).arg(title).arg(size - 2)
.arg(artist, separator, album));
--size;
neededHeight = m_label->heightForWidth(m_label->width());
} while(neededHeight > parentHeight && size >= -1);
m_label->setFixedHeight(qMin(neededHeight, parentHeight));
}
void TrackItem::slotClearShowMore()
{
PlaylistCollection *collection = parentManager()->collection();
Q_ASSERT(collection);
collection->clearShowMore();
}
// vim: set et sw=4 tw=0 sta:
|