2010-03-29 18:59:01 +02:00
|
|
|
/* This file is part of Clementine.
|
2010-11-20 14:27:10 +01:00
|
|
|
Copyright 2010, David Sansome <me@davidsansome.com>
|
2010-03-29 18:59:01 +02:00
|
|
|
|
|
|
|
Clementine 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 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Clementine 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 Clementine. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "playlistdelegates.h"
|
|
|
|
|
|
|
|
#include <QDateTime>
|
2010-08-28 21:07:23 +02:00
|
|
|
#include <QDir>
|
2010-12-25 14:11:09 +01:00
|
|
|
#include <QFuture>
|
|
|
|
#include <QFutureWatcher>
|
2010-12-04 17:43:09 +01:00
|
|
|
#include <QHeaderView>
|
|
|
|
#include <QHelpEvent>
|
|
|
|
#include <QLinearGradient>
|
2010-03-29 18:59:01 +02:00
|
|
|
#include <QLineEdit>
|
|
|
|
#include <QPainter>
|
2010-12-04 17:43:09 +01:00
|
|
|
#include <QScrollBar>
|
|
|
|
#include <QTextDocument>
|
2010-04-30 17:23:52 +02:00
|
|
|
#include <QToolTip>
|
|
|
|
#include <QWhatsThis>
|
2010-12-25 14:11:09 +01:00
|
|
|
#include <QtConcurrentRun>
|
2010-03-29 18:59:01 +02:00
|
|
|
|
2014-01-10 16:57:32 +01:00
|
|
|
#include "queue.h"
|
|
|
|
#include "core/logging.h"
|
|
|
|
#include "core/player.h"
|
|
|
|
#include "core/utilities.h"
|
|
|
|
#include "library/librarybackend.h"
|
|
|
|
#include "widgets/trackslider.h"
|
|
|
|
#include "ui/iconloader.h"
|
|
|
|
|
|
|
|
#ifdef Q_OS_DARWIN
|
|
|
|
#include "core/mac_utilities.h"
|
|
|
|
#endif // Q_OS_DARWIN
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
const int QueuedItemDelegate::kQueueBoxBorder = 1;
|
|
|
|
const int QueuedItemDelegate::kQueueBoxCornerRadius = 3;
|
|
|
|
const int QueuedItemDelegate::kQueueBoxLength = 30;
|
|
|
|
const QRgb QueuedItemDelegate::kQueueBoxGradientColor1 = qRgb(102, 150, 227);
|
|
|
|
const QRgb QueuedItemDelegate::kQueueBoxGradientColor2 = qRgb(77, 121, 200);
|
|
|
|
const int QueuedItemDelegate::kQueueOpacitySteps = 10;
|
2010-07-11 20:37:42 +02:00
|
|
|
const float QueuedItemDelegate::kQueueOpacityLowerBound = 0.4;
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
const int PlaylistDelegateBase::kMinHeight = 19;
|
2010-10-17 19:10:19 +02:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
QueuedItemDelegate::QueuedItemDelegate(QObject* parent, int indicator_column)
|
|
|
|
: QStyledItemDelegate(parent), indicator_column_(indicator_column) {}
|
2010-07-11 18:17:38 +02:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
void QueuedItemDelegate::paint(QPainter* painter,
|
|
|
|
const QStyleOptionViewItem& option,
|
|
|
|
const QModelIndex& index) const {
|
2010-07-11 18:17:38 +02:00
|
|
|
QStyledItemDelegate::paint(painter, option, index);
|
|
|
|
|
2010-07-11 23:27:36 +02:00
|
|
|
if (index.column() == indicator_column_) {
|
2010-08-28 21:07:23 +02:00
|
|
|
bool ok = false;
|
|
|
|
const int queue_pos = index.data(Playlist::Role_QueuePosition).toInt(&ok);
|
|
|
|
if (ok && queue_pos != -1) {
|
2010-07-11 18:17:38 +02:00
|
|
|
float opacity = kQueueOpacitySteps - qMin(kQueueOpacitySteps, queue_pos);
|
|
|
|
opacity /= kQueueOpacitySteps;
|
|
|
|
opacity *= 1.0 - kQueueOpacityLowerBound;
|
|
|
|
opacity += kQueueOpacityLowerBound;
|
|
|
|
painter->setOpacity(opacity);
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
DrawBox(painter, option.rect, option.font, QString::number(queue_pos + 1),
|
2010-07-11 20:37:42 +02:00
|
|
|
kQueueBoxLength);
|
2010-07-11 18:17:38 +02:00
|
|
|
|
|
|
|
painter->setOpacity(1.0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
void QueuedItemDelegate::DrawBox(QPainter* painter, const QRect& line_rect,
|
|
|
|
const QFont& font, const QString& text,
|
|
|
|
int width) const {
|
2010-07-11 20:37:42 +02:00
|
|
|
QFont smaller = font;
|
2010-08-28 21:07:23 +02:00
|
|
|
smaller.setPointSize(smaller.pointSize() - 1);
|
2010-07-11 20:37:42 +02:00
|
|
|
smaller.setBold(true);
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
if (width == -1) width = QFontMetrics(font).width(text + " ");
|
2010-07-11 20:37:42 +02:00
|
|
|
|
|
|
|
QRect rect(line_rect);
|
|
|
|
rect.setLeft(rect.right() - width - kQueueBoxBorder);
|
|
|
|
rect.setWidth(width);
|
|
|
|
rect.setTop(rect.top() + kQueueBoxBorder);
|
|
|
|
rect.setBottom(rect.bottom() - kQueueBoxBorder - 1);
|
|
|
|
|
|
|
|
QRect text_rect(rect);
|
|
|
|
text_rect.setBottom(text_rect.bottom() + 1);
|
|
|
|
|
|
|
|
QLinearGradient gradient(rect.topLeft(), rect.bottomLeft());
|
|
|
|
gradient.setColorAt(0.0, kQueueBoxGradientColor1);
|
|
|
|
gradient.setColorAt(1.0, kQueueBoxGradientColor2);
|
|
|
|
|
|
|
|
// Turn on antialiasing
|
|
|
|
painter->setRenderHint(QPainter::Antialiasing);
|
|
|
|
|
|
|
|
// Draw the box
|
2012-01-08 00:52:31 +01:00
|
|
|
painter->translate(0.5, 0.5);
|
2010-07-11 20:37:42 +02:00
|
|
|
painter->setPen(QPen(Qt::white, 1));
|
|
|
|
painter->setBrush(gradient);
|
|
|
|
painter->drawRoundedRect(rect, kQueueBoxCornerRadius, kQueueBoxCornerRadius);
|
|
|
|
|
|
|
|
// Draw the text
|
|
|
|
painter->setFont(smaller);
|
2012-01-08 00:52:31 +01:00
|
|
|
painter->drawText(rect.translated(-1, -1), Qt::AlignCenter, text);
|
|
|
|
painter->translate(-0.5, -0.5);
|
2010-07-11 20:37:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int QueuedItemDelegate::queue_indicator_size(const QModelIndex& index) const {
|
2010-07-11 23:27:36 +02:00
|
|
|
if (index.column() == indicator_column_) {
|
2010-07-11 20:37:42 +02:00
|
|
|
const int queue_pos = index.data(Playlist::Role_QueuePosition).toInt();
|
|
|
|
if (queue_pos != -1) {
|
2014-02-07 16:34:20 +01:00
|
|
|
return kQueueBoxLength + kQueueBoxBorder * 2;
|
2010-07-11 20:37:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
PlaylistDelegateBase::PlaylistDelegateBase(QObject* parent,
|
|
|
|
const QString& suffix)
|
|
|
|
: QueuedItemDelegate(parent),
|
|
|
|
view_(qobject_cast<QTreeView*>(parent)),
|
|
|
|
suffix_(suffix) {}
|
2010-07-11 20:37:42 +02:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
QString PlaylistDelegateBase::displayText(const QVariant& value,
|
|
|
|
const QLocale&) const {
|
2010-06-10 19:04:40 +02:00
|
|
|
QString text;
|
|
|
|
|
2010-11-03 22:19:26 +01:00
|
|
|
switch (static_cast<QMetaType::Type>(value.type())) {
|
|
|
|
case QMetaType::Int: {
|
2010-03-29 18:59:01 +02:00
|
|
|
int v = value.toInt();
|
2014-02-07 16:34:20 +01:00
|
|
|
if (v > 0) text = QString::number(v);
|
2010-06-10 19:04:40 +02:00
|
|
|
break;
|
2010-03-29 18:59:01 +02:00
|
|
|
}
|
|
|
|
|
2010-06-10 18:54:07 +02:00
|
|
|
case QMetaType::Float:
|
2010-11-03 22:19:26 +01:00
|
|
|
case QMetaType::Double: {
|
2010-03-29 18:59:01 +02:00
|
|
|
double v = value.toDouble();
|
2014-02-07 16:34:20 +01:00
|
|
|
if (v > 0) text = QString::number(v);
|
2010-06-10 19:04:40 +02:00
|
|
|
break;
|
2010-03-29 18:59:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
2010-06-10 19:04:40 +02:00
|
|
|
text = value.toString();
|
|
|
|
break;
|
2010-03-29 18:59:01 +02:00
|
|
|
}
|
2010-06-10 19:04:40 +02:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
if (!text.isNull() && !suffix_.isNull()) text += " " + suffix_;
|
2010-06-10 19:04:40 +02:00
|
|
|
return text;
|
2010-03-29 18:59:01 +02:00
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
QSize PlaylistDelegateBase::sizeHint(const QStyleOptionViewItem& option,
|
|
|
|
const QModelIndex& index) const {
|
2010-07-11 18:17:38 +02:00
|
|
|
QSize size = QueuedItemDelegate::sizeHint(option, index);
|
2014-02-07 16:34:20 +01:00
|
|
|
if (size.height() < kMinHeight) size.setHeight(kMinHeight);
|
2010-04-12 20:07:07 +02:00
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
void PlaylistDelegateBase::paint(QPainter* painter,
|
|
|
|
const QStyleOptionViewItem& option,
|
|
|
|
const QModelIndex& index) const {
|
2010-07-11 18:17:38 +02:00
|
|
|
QueuedItemDelegate::paint(painter, Adjusted(option, index), index);
|
2010-03-29 18:59:01 +02:00
|
|
|
|
2010-07-11 18:17:38 +02:00
|
|
|
// Stop after indicator
|
2010-07-11 20:37:42 +02:00
|
|
|
if (index.column() == Playlist::Column_Title) {
|
2010-03-29 18:59:01 +02:00
|
|
|
if (index.data(Playlist::Role_StopAfter).toBool()) {
|
2010-07-11 20:37:42 +02:00
|
|
|
QRect rect(option.rect);
|
|
|
|
rect.setRight(rect.right() - queue_indicator_size(index));
|
|
|
|
|
|
|
|
DrawBox(painter, rect, option.font, tr("stop"));
|
2010-03-29 18:59:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
QStyleOptionViewItemV4 PlaylistDelegateBase::Adjusted(
|
|
|
|
const QStyleOptionViewItem& option, const QModelIndex& index) const {
|
|
|
|
if (!view_) return option;
|
2010-08-28 21:07:23 +02:00
|
|
|
|
2010-06-09 17:37:54 +02:00
|
|
|
QPoint top_left(-view_->horizontalScrollBar()->value(),
|
|
|
|
-view_->verticalScrollBar()->value());
|
|
|
|
|
|
|
|
if (view_->header()->logicalIndexAt(top_left) != index.column())
|
2010-03-29 18:59:01 +02:00
|
|
|
return option;
|
|
|
|
|
|
|
|
QStyleOptionViewItemV4 ret(option);
|
|
|
|
|
|
|
|
if (index.data(Playlist::Role_IsCurrent).toBool()) {
|
|
|
|
// Move the text in a bit on the first column for the song that's currently
|
|
|
|
// playing
|
|
|
|
ret.rect.setLeft(ret.rect.left() + 20);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
bool PlaylistDelegateBase::helpEvent(QHelpEvent* event, QAbstractItemView* view,
|
|
|
|
const QStyleOptionViewItem& option,
|
|
|
|
const QModelIndex& index) {
|
2010-04-30 17:23:52 +02:00
|
|
|
// This function is copied from QAbstractItemDelegate, and changed to show
|
|
|
|
// displayText() in the tooltip, rather than the index's naked
|
|
|
|
// Qt::ToolTipRole text.
|
|
|
|
|
|
|
|
Q_UNUSED(option);
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
if (!event || !view) return false;
|
2010-04-30 17:23:52 +02:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
QHelpEvent* he = static_cast<QHelpEvent*>(event);
|
2010-04-30 17:23:52 +02:00
|
|
|
QString text = displayText(index.data(), QLocale::system());
|
|
|
|
|
2010-12-04 17:43:09 +01:00
|
|
|
// Special case: we want newlines in the comment tooltip
|
|
|
|
if (index.column() == Playlist::Column_Comment) {
|
|
|
|
text = Qt::escape(index.data(Qt::ToolTipRole).toString());
|
2010-12-05 14:41:40 +01:00
|
|
|
text.replace("\\r\\n", "<br />");
|
|
|
|
text.replace("\\n", "<br />");
|
|
|
|
text.replace("\r\n", "<br />");
|
2010-12-04 17:43:09 +01:00
|
|
|
text.replace("\n", "<br />");
|
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
if (text.isEmpty() || !he) return false;
|
2010-04-30 17:23:52 +02:00
|
|
|
|
|
|
|
switch (event->type()) {
|
2011-02-03 00:27:55 +01:00
|
|
|
case QEvent::ToolTip: {
|
|
|
|
QRect displayed_text;
|
|
|
|
QSize real_text;
|
|
|
|
bool is_elided = false;
|
|
|
|
|
2011-02-02 23:58:19 +01:00
|
|
|
real_text = sizeHint(option, index);
|
|
|
|
displayed_text = view->visualRect(index);
|
|
|
|
is_elided = displayed_text.width() < real_text.width();
|
2014-02-07 16:34:20 +01:00
|
|
|
if (is_elided) {
|
2011-02-02 23:58:19 +01:00
|
|
|
QToolTip::showText(he->globalPos(), text, view);
|
2014-02-07 16:34:20 +01:00
|
|
|
} else { // in case that another text was previously displayed
|
2011-02-02 23:58:19 +01:00
|
|
|
QToolTip::hideText();
|
|
|
|
}
|
2010-04-30 17:23:52 +02:00
|
|
|
return true;
|
2011-02-03 00:27:55 +01:00
|
|
|
}
|
2010-04-30 17:23:52 +02:00
|
|
|
|
|
|
|
case QEvent::QueryWhatsThis:
|
|
|
|
return true;
|
|
|
|
|
|
|
|
case QEvent::WhatsThis:
|
|
|
|
QWhatsThis::showText(he->globalPos(), text, view);
|
|
|
|
return true;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
QString LengthItemDelegate::displayText(const QVariant& value,
|
|
|
|
const QLocale&) const {
|
2010-03-29 18:59:01 +02:00
|
|
|
bool ok = false;
|
2011-02-13 19:34:30 +01:00
|
|
|
qint64 nanoseconds = value.toLongLong(&ok);
|
2010-03-29 18:59:01 +02:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
if (ok && nanoseconds > 0) return Utilities::PrettyTimeNanosec(nanoseconds);
|
2010-03-29 18:59:01 +02:00
|
|
|
return QString::null;
|
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
QString SizeItemDelegate::displayText(const QVariant& value,
|
|
|
|
const QLocale&) const {
|
2010-03-29 18:59:01 +02:00
|
|
|
bool ok = false;
|
|
|
|
int bytes = value.toInt(&ok);
|
2010-07-03 23:05:55 +02:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
if (ok) return Utilities::PrettySize(bytes);
|
2010-07-03 23:05:55 +02:00
|
|
|
return QString();
|
2010-03-29 18:59:01 +02:00
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
QString DateItemDelegate::displayText(const QVariant& value,
|
|
|
|
const QLocale& locale) const {
|
2010-03-29 18:59:01 +02:00
|
|
|
bool ok = false;
|
|
|
|
int time = value.toInt(&ok);
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
if (!ok || time == -1) return QString::null;
|
2010-03-29 18:59:01 +02:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
return QDateTime::fromTime_t(time)
|
|
|
|
.toString(QLocale::system().dateTimeFormat(QLocale::ShortFormat));
|
2010-03-29 18:59:01 +02:00
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
QString LastPlayedItemDelegate::displayText(const QVariant& value,
|
|
|
|
const QLocale& locale) const {
|
2010-10-17 22:53:15 +02:00
|
|
|
bool ok = false;
|
|
|
|
const int time = value.toInt(&ok);
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
if (!ok || time == -1) return tr("Never");
|
2010-10-17 22:53:15 +02:00
|
|
|
|
|
|
|
return Utilities::Ago(time, locale);
|
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
QString FileTypeItemDelegate::displayText(const QVariant& value,
|
|
|
|
const QLocale& locale) const {
|
2010-03-29 18:59:01 +02:00
|
|
|
bool ok = false;
|
|
|
|
Song::FileType type = Song::FileType(value.toInt(&ok));
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
if (!ok) return tr("Unknown");
|
2010-03-29 18:59:01 +02:00
|
|
|
|
2010-07-31 16:14:03 +02:00
|
|
|
return Song::TextForFiletype(type);
|
2010-03-29 18:59:01 +02:00
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
QWidget* TextItemDelegate::createEditor(QWidget* parent,
|
|
|
|
const QStyleOptionViewItem& option,
|
|
|
|
const QModelIndex& index) const {
|
2010-03-29 18:59:01 +02:00
|
|
|
return new QLineEdit(parent);
|
|
|
|
}
|
|
|
|
|
2010-10-17 19:10:19 +02:00
|
|
|
RatingItemDelegate::RatingItemDelegate(QObject* parent)
|
2014-02-07 16:34:20 +01:00
|
|
|
: PlaylistDelegateBase(parent) {}
|
2010-10-17 19:10:19 +02:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
void RatingItemDelegate::paint(QPainter* painter,
|
|
|
|
const QStyleOptionViewItem& option,
|
|
|
|
const QModelIndex& index) const {
|
2010-10-17 20:21:30 +02:00
|
|
|
// Draw the background
|
|
|
|
const QStyleOptionViewItemV3* vopt =
|
|
|
|
qstyleoption_cast<const QStyleOptionViewItemV3*>(&option);
|
2014-02-07 16:34:20 +01:00
|
|
|
vopt->widget->style()->drawPrimitive(QStyle::PE_PanelItemViewItem, vopt,
|
|
|
|
painter, vopt->widget);
|
2010-10-17 20:21:30 +02:00
|
|
|
|
2010-10-17 23:56:19 +02:00
|
|
|
// Don't draw anything else if the user can't set the rating of this item
|
2014-02-07 16:34:20 +01:00
|
|
|
if (!index.data(Playlist::Role_CanSetRating).toBool()) return;
|
2010-10-17 23:56:19 +02:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
const bool hover = mouse_over_index_.isValid() &&
|
|
|
|
(mouse_over_index_ == index ||
|
|
|
|
(selected_indexes_.contains(mouse_over_index_) &&
|
|
|
|
selected_indexes_.contains(index)));
|
2010-12-11 15:53:43 +01:00
|
|
|
|
2010-10-26 21:59:55 +02:00
|
|
|
const double rating =
|
|
|
|
(hover ? RatingPainter::RatingForPos(mouse_over_pos_, option.rect)
|
|
|
|
: index.data().toDouble());
|
2010-10-17 23:56:19 +02:00
|
|
|
|
2010-10-26 21:59:55 +02:00
|
|
|
painter_.Paint(painter, option.rect, rating);
|
2010-10-17 19:10:19 +02:00
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
QSize RatingItemDelegate::sizeHint(const QStyleOptionViewItem& option,
|
|
|
|
const QModelIndex& index) const {
|
2010-10-17 19:10:19 +02:00
|
|
|
QSize size = PlaylistDelegateBase::sizeHint(option, index);
|
2010-10-26 21:59:55 +02:00
|
|
|
size.setWidth(size.height() * RatingPainter::kStarCount);
|
2010-10-17 19:10:19 +02:00
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
QString RatingItemDelegate::displayText(const QVariant& value,
|
|
|
|
const QLocale&) const {
|
|
|
|
if (value.isNull() || value.toDouble() <= 0) return QString();
|
2010-10-17 22:53:15 +02:00
|
|
|
|
2010-10-17 23:56:19 +02:00
|
|
|
// Round to the nearest 0.5
|
2014-02-07 16:34:20 +01:00
|
|
|
const double rating =
|
|
|
|
float(int(value.toDouble() * RatingPainter::kStarCount * 2 + 0.5)) / 2;
|
2010-10-17 23:56:19 +02:00
|
|
|
|
2010-10-17 19:10:19 +02:00
|
|
|
return QString::number(rating, 'f', 1);
|
|
|
|
}
|
2010-03-29 18:59:01 +02:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
TagCompletionModel::TagCompletionModel(LibraryBackend* backend,
|
|
|
|
Playlist::Column column)
|
|
|
|
: QStringListModel() {
|
2010-12-25 14:11:09 +01:00
|
|
|
QString col = database_column(column);
|
|
|
|
if (!col.isEmpty()) {
|
|
|
|
setStringList(backend->GetAll(col));
|
|
|
|
}
|
|
|
|
}
|
2010-03-29 18:59:01 +02:00
|
|
|
|
2010-12-25 14:11:09 +01:00
|
|
|
QString TagCompletionModel::database_column(Playlist::Column column) {
|
|
|
|
switch (column) {
|
2014-02-07 16:34:20 +01:00
|
|
|
case Playlist::Column_Artist:
|
|
|
|
return "artist";
|
|
|
|
case Playlist::Column_Album:
|
|
|
|
return "album";
|
|
|
|
case Playlist::Column_AlbumArtist:
|
|
|
|
return "albumartist";
|
|
|
|
case Playlist::Column_Composer:
|
|
|
|
return "composer";
|
|
|
|
case Playlist::Column_Performer:
|
|
|
|
return "performer";
|
|
|
|
case Playlist::Column_Grouping:
|
|
|
|
return "grouping";
|
|
|
|
case Playlist::Column_Genre:
|
|
|
|
return "genre";
|
2010-03-29 18:59:01 +02:00
|
|
|
default:
|
2011-04-22 18:50:29 +02:00
|
|
|
qLog(Warning) << "Unknown column" << column;
|
2010-12-25 14:11:09 +01:00
|
|
|
return QString();
|
2010-03-29 18:59:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-25 14:11:09 +01:00
|
|
|
static TagCompletionModel* InitCompletionModel(LibraryBackend* backend,
|
|
|
|
Playlist::Column column) {
|
|
|
|
return new TagCompletionModel(backend, column);
|
|
|
|
}
|
|
|
|
|
|
|
|
TagCompleter::TagCompleter(LibraryBackend* backend, Playlist::Column column,
|
|
|
|
QLineEdit* editor)
|
2014-02-07 16:34:20 +01:00
|
|
|
: QCompleter(editor), editor_(editor) {
|
|
|
|
QFuture<TagCompletionModel*> future =
|
|
|
|
QtConcurrent::run(&InitCompletionModel, backend, column);
|
2010-12-25 14:11:09 +01:00
|
|
|
QFutureWatcher<TagCompletionModel*>* watcher =
|
|
|
|
new QFutureWatcher<TagCompletionModel*>(this);
|
|
|
|
watcher->setFuture(future);
|
|
|
|
|
|
|
|
connect(watcher, SIGNAL(finished()), SLOT(ModelReady()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void TagCompleter::ModelReady() {
|
|
|
|
QFutureWatcher<TagCompletionModel*>* watcher =
|
|
|
|
dynamic_cast<QFutureWatcher<TagCompletionModel*>*>(sender());
|
2014-02-07 16:34:20 +01:00
|
|
|
if (!watcher) return;
|
2010-03-29 18:59:01 +02:00
|
|
|
|
2010-12-25 14:11:09 +01:00
|
|
|
TagCompletionModel* model = watcher->result();
|
|
|
|
setModel(model);
|
2010-03-29 18:59:01 +02:00
|
|
|
setCaseSensitivity(Qt::CaseInsensitive);
|
2010-12-25 14:11:09 +01:00
|
|
|
editor_->setCompleter(this);
|
2010-03-29 18:59:01 +02:00
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
QWidget* TagCompletionItemDelegate::createEditor(QWidget* parent,
|
|
|
|
const QStyleOptionViewItem&,
|
|
|
|
const QModelIndex&) const {
|
2010-03-29 18:59:01 +02:00
|
|
|
|
|
|
|
QLineEdit* editor = new QLineEdit(parent);
|
2010-05-09 02:10:26 +02:00
|
|
|
new TagCompleter(backend_, column_, editor);
|
2010-03-29 18:59:01 +02:00
|
|
|
|
|
|
|
return editor;
|
|
|
|
}
|
2010-08-28 21:07:23 +02:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
QString NativeSeparatorsDelegate::displayText(const QVariant& value,
|
|
|
|
const QLocale&) const {
|
2012-08-26 13:23:39 +02:00
|
|
|
const QString string_value = value.toString();
|
|
|
|
|
|
|
|
QUrl url;
|
|
|
|
if (value.type() == QVariant::Url) {
|
|
|
|
url = value.toUrl();
|
|
|
|
} else if (string_value.contains("://")) {
|
|
|
|
url = QUrl::fromEncoded(string_value.toAscii());
|
|
|
|
} else {
|
|
|
|
return QDir::toNativeSeparators(string_value);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (url.scheme() == "file") {
|
|
|
|
return QDir::toNativeSeparators(url.toLocalFile());
|
2011-11-12 20:57:21 +01:00
|
|
|
}
|
2012-08-26 13:23:39 +02:00
|
|
|
return string_value;
|
2010-08-28 21:07:23 +02:00
|
|
|
}
|
2012-01-04 12:50:19 +01:00
|
|
|
|
|
|
|
SongSourceDelegate::SongSourceDelegate(QObject* parent, Player* player)
|
2014-02-07 16:34:20 +01:00
|
|
|
: PlaylistDelegateBase(parent), player_(player) {}
|
2012-01-04 12:50:19 +01:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
QString SongSourceDelegate::displayText(const QVariant& value,
|
|
|
|
const QLocale&) const {
|
2012-01-04 16:43:28 +01:00
|
|
|
return QString();
|
2012-01-04 12:50:19 +01:00
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
QPixmap SongSourceDelegate::LookupPixmap(const QUrl& url,
|
|
|
|
const QSize& size) const {
|
2012-01-04 13:04:17 +01:00
|
|
|
QPixmap pixmap;
|
|
|
|
if (cache_.find(url.scheme(), &pixmap)) {
|
|
|
|
return pixmap;
|
|
|
|
}
|
|
|
|
|
2012-01-04 12:50:19 +01:00
|
|
|
QIcon icon;
|
|
|
|
const UrlHandler* handler = player_->HandlerForUrl(url);
|
|
|
|
if (handler) {
|
|
|
|
icon = handler->icon();
|
|
|
|
} else {
|
|
|
|
if (url.scheme() == "spotify") {
|
2012-01-08 16:16:36 +01:00
|
|
|
icon = QIcon(":icons/22x22/spotify.png");
|
2012-01-04 12:50:19 +01:00
|
|
|
} else if (url.scheme() == "file") {
|
|
|
|
icon = IconLoader::Load("folder-sound");
|
2012-03-05 01:13:11 +01:00
|
|
|
} else if (url.host() == "api.jamendo.com") {
|
|
|
|
icon = QIcon(":/providers/jamendo.png");
|
2012-08-09 22:36:39 +02:00
|
|
|
} else if (url.host() == "api.soundcloud.com") {
|
|
|
|
icon = QIcon(":/providers/soundcloud.png");
|
2012-01-04 12:50:19 +01:00
|
|
|
}
|
|
|
|
}
|
2012-01-04 13:04:17 +01:00
|
|
|
pixmap = icon.pixmap(size.height());
|
|
|
|
cache_.insert(url.scheme(), pixmap);
|
|
|
|
return pixmap;
|
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
void SongSourceDelegate::paint(QPainter* painter,
|
|
|
|
const QStyleOptionViewItem& option,
|
|
|
|
const QModelIndex& index) const {
|
2012-01-08 16:16:36 +01:00
|
|
|
// Draw the background
|
2012-01-04 13:04:17 +01:00
|
|
|
PlaylistDelegateBase::paint(painter, option, index);
|
2012-01-08 16:16:36 +01:00
|
|
|
|
2012-01-04 13:04:17 +01:00
|
|
|
QStyleOptionViewItem option_copy(option);
|
|
|
|
initStyleOption(&option_copy, index);
|
2012-01-08 16:16:36 +01:00
|
|
|
|
|
|
|
// Find the pixmap to use for this URL
|
2012-01-04 13:04:17 +01:00
|
|
|
const QUrl& url = index.data().toUrl();
|
|
|
|
QPixmap pixmap = LookupPixmap(url, option_copy.decorationSize);
|
2012-01-08 16:16:36 +01:00
|
|
|
|
2014-01-10 16:57:32 +01:00
|
|
|
float device_pixel_ratio = 1.0f;
|
|
|
|
#ifdef Q_OS_DARWIN
|
|
|
|
QWidget* parent_widget = reinterpret_cast<QWidget*>(parent());
|
|
|
|
device_pixel_ratio = mac::GetDevicePixelRatio(parent_widget);
|
|
|
|
#endif
|
|
|
|
|
2012-01-08 16:16:36 +01:00
|
|
|
// Draw the pixmap in the middle of the rectangle
|
2014-02-07 16:34:20 +01:00
|
|
|
QRect draw_rect(QPoint(0, 0),
|
|
|
|
option_copy.decorationSize / device_pixel_ratio);
|
2012-01-08 16:16:36 +01:00
|
|
|
draw_rect.moveCenter(option_copy.rect.center());
|
|
|
|
|
2012-01-04 12:50:19 +01:00
|
|
|
painter->drawPixmap(draw_rect, pixmap);
|
|
|
|
}
|