2010-03-29 18:59:01 +02:00
|
|
|
/* This file is part of Clementine.
|
|
|
|
|
|
|
|
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"
|
2010-07-11 18:17:38 +02:00
|
|
|
#include "queue.h"
|
2010-05-10 23:50:31 +02:00
|
|
|
#include "core/utilities.h"
|
|
|
|
#include "library/librarybackend.h"
|
|
|
|
#include "widgets/trackslider.h"
|
2010-03-29 18:59:01 +02:00
|
|
|
|
|
|
|
#include <QDateTime>
|
|
|
|
#include <QLineEdit>
|
|
|
|
#include <QPainter>
|
2010-04-30 17:23:52 +02:00
|
|
|
#include <QToolTip>
|
|
|
|
#include <QWhatsThis>
|
|
|
|
#include <QHelpEvent>
|
2010-05-10 23:50:31 +02:00
|
|
|
#include <QHeaderView>
|
2010-06-09 17:37:54 +02:00
|
|
|
#include <QScrollBar>
|
2010-07-11 18:17:38 +02:00
|
|
|
#include <QLinearGradient>
|
2010-03-29 18:59:01 +02:00
|
|
|
|
2010-07-11 20:37:42 +02: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;
|
|
|
|
const float QueuedItemDelegate::kQueueOpacityLowerBound = 0.4;
|
|
|
|
|
2010-04-12 20:07:07 +02:00
|
|
|
const int PlaylistDelegateBase::kMinHeight = 19;
|
|
|
|
|
2010-07-11 18:17:38 +02:00
|
|
|
QueuedItemDelegate::QueuedItemDelegate(QObject *parent)
|
|
|
|
: QStyledItemDelegate(parent)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void QueuedItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const {
|
|
|
|
QStyledItemDelegate::paint(painter, option, index);
|
|
|
|
|
|
|
|
if (index.column() == Playlist::Column_Title ||
|
|
|
|
index.column() == Queue::Column_CombinedArtistTitle) {
|
|
|
|
const int queue_pos = index.data(Playlist::Role_QueuePosition).toInt();
|
|
|
|
if (queue_pos != -1) {
|
|
|
|
float opacity = kQueueOpacitySteps - qMin(kQueueOpacitySteps, queue_pos);
|
|
|
|
opacity /= kQueueOpacitySteps;
|
|
|
|
opacity *= 1.0 - kQueueOpacityLowerBound;
|
|
|
|
opacity += kQueueOpacityLowerBound;
|
|
|
|
painter->setOpacity(opacity);
|
|
|
|
|
2010-07-11 20:37:42 +02:00
|
|
|
DrawBox(painter, option.rect, option.font, QString::number(queue_pos+1),
|
|
|
|
kQueueBoxLength);
|
2010-07-11 18:17:38 +02:00
|
|
|
|
|
|
|
painter->setOpacity(1.0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-11 20:37:42 +02:00
|
|
|
void QueuedItemDelegate::DrawBox(
|
|
|
|
QPainter* painter, const QRect& line_rect, const QFont& font,
|
|
|
|
const QString& text, int width) const {
|
|
|
|
QFont smaller = font;
|
|
|
|
smaller.setPointSize(smaller.pointSize() - 2);
|
|
|
|
smaller.setBold(true);
|
|
|
|
|
|
|
|
if (width == -1)
|
|
|
|
width = QFontMetrics(font).width(text + " ");
|
|
|
|
|
|
|
|
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
|
|
|
|
painter->setPen(QPen(Qt::white, 1));
|
|
|
|
painter->setBrush(gradient);
|
|
|
|
painter->drawRoundedRect(rect, kQueueBoxCornerRadius, kQueueBoxCornerRadius);
|
|
|
|
|
|
|
|
// Draw the text
|
|
|
|
painter->setFont(smaller);
|
|
|
|
painter->drawText(rect, Qt::AlignCenter, text);
|
|
|
|
}
|
|
|
|
|
|
|
|
int QueuedItemDelegate::queue_indicator_size(const QModelIndex& index) const {
|
|
|
|
if (index.column() == Playlist::Column_Title ||
|
|
|
|
index.column() == Queue::Column_CombinedArtistTitle) {
|
|
|
|
const int queue_pos = index.data(Playlist::Role_QueuePosition).toInt();
|
|
|
|
if (queue_pos != -1) {
|
|
|
|
return kQueueBoxLength + kQueueBoxBorder*2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-06-10 19:04:40 +02:00
|
|
|
PlaylistDelegateBase::PlaylistDelegateBase(QTreeView* view, const QString& suffix)
|
2010-07-11 18:17:38 +02:00
|
|
|
: QueuedItemDelegate(view),
|
2010-06-10 19:04:40 +02:00
|
|
|
view_(view),
|
|
|
|
suffix_(suffix)
|
2010-03-29 18:59:01 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
QString PlaylistDelegateBase::displayText(const QVariant& value, const QLocale&) const {
|
2010-06-10 19:04:40 +02:00
|
|
|
QString text;
|
|
|
|
|
2010-03-29 18:59:01 +02:00
|
|
|
switch (value.type()) {
|
|
|
|
case QVariant::Int: {
|
|
|
|
int v = value.toInt();
|
2010-06-10 19:04:40 +02:00
|
|
|
if (v > 0)
|
|
|
|
text = QString::number(v);
|
|
|
|
break;
|
2010-03-29 18:59:01 +02:00
|
|
|
}
|
|
|
|
|
2010-06-10 18:54:07 +02:00
|
|
|
case QMetaType::Float:
|
2010-03-29 18:59:01 +02:00
|
|
|
case QVariant::Double: {
|
|
|
|
double v = value.toDouble();
|
2010-06-10 19:04:40 +02:00
|
|
|
if (v > 0)
|
|
|
|
text = QString::number(v);
|
|
|
|
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
|
|
|
|
|
|
|
if (!text.isNull() && !suffix_.isNull())
|
|
|
|
text += " " + suffix_;
|
|
|
|
return text;
|
2010-03-29 18:59:01 +02:00
|
|
|
}
|
|
|
|
|
2010-04-12 20:07:07 +02: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);
|
2010-04-12 20:07:07 +02:00
|
|
|
if (size.height() < kMinHeight)
|
|
|
|
size.setHeight(kMinHeight);
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2010-03-29 18:59:01 +02: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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QStyleOptionViewItemV4 PlaylistDelegateBase::Adjusted(const QStyleOptionViewItem& option, const QModelIndex& index) const {
|
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;
|
|
|
|
}
|
|
|
|
|
2010-04-30 17:23:52 +02:00
|
|
|
bool PlaylistDelegateBase::helpEvent(QHelpEvent *event, QAbstractItemView *view,
|
|
|
|
const QStyleOptionViewItem &option,
|
|
|
|
const QModelIndex &index) {
|
|
|
|
// 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);
|
|
|
|
|
|
|
|
if (!event || !view)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
QHelpEvent *he = static_cast<QHelpEvent*>(event);
|
|
|
|
QString text = displayText(index.data(), QLocale::system());
|
|
|
|
|
|
|
|
if (text.isEmpty() || !he)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
switch (event->type()) {
|
|
|
|
case QEvent::ToolTip:
|
|
|
|
QToolTip::showText(he->globalPos(), text, view);
|
|
|
|
return true;
|
|
|
|
|
|
|
|
case QEvent::QueryWhatsThis:
|
|
|
|
return true;
|
|
|
|
|
|
|
|
case QEvent::WhatsThis:
|
|
|
|
QWhatsThis::showText(he->globalPos(), text, view);
|
|
|
|
return true;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-03-29 18:59:01 +02:00
|
|
|
|
|
|
|
QString LengthItemDelegate::displayText(const QVariant& value, const QLocale&) const {
|
|
|
|
bool ok = false;
|
|
|
|
int seconds = value.toInt(&ok);
|
|
|
|
|
|
|
|
if (ok && seconds > 0)
|
2010-05-10 23:50:31 +02:00
|
|
|
return Utilities::PrettyTime(seconds);
|
2010-03-29 18:59:01 +02:00
|
|
|
return QString::null;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
QString SizeItemDelegate::displayText(const QVariant& value, const QLocale&) const {
|
|
|
|
bool ok = false;
|
|
|
|
int bytes = value.toInt(&ok);
|
2010-07-03 23:05:55 +02:00
|
|
|
|
|
|
|
if (ok)
|
|
|
|
return Utilities::PrettySize(bytes);
|
|
|
|
return QString();
|
2010-03-29 18:59:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
QString DateItemDelegate::displayText(const QVariant &value, const QLocale &locale) const {
|
|
|
|
bool ok = false;
|
|
|
|
int time = value.toInt(&ok);
|
|
|
|
|
|
|
|
if (!ok || time == -1)
|
|
|
|
return QString::null;
|
|
|
|
|
|
|
|
return QDateTime::fromTime_t(time).toString(
|
|
|
|
QLocale::system().dateTimeFormat(QLocale::ShortFormat));
|
|
|
|
}
|
|
|
|
|
|
|
|
QString FileTypeItemDelegate::displayText(const QVariant &value, const QLocale &locale) const {
|
|
|
|
bool ok = false;
|
|
|
|
Song::FileType type = Song::FileType(value.toInt(&ok));
|
|
|
|
|
|
|
|
if (!ok)
|
|
|
|
return tr("Unknown");
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case Song::Type_Asf: return tr("ASF");
|
|
|
|
case Song::Type_Flac: return tr("FLAC");
|
|
|
|
case Song::Type_Mp4: return tr("MP4");
|
|
|
|
case Song::Type_Mpc: return tr("MPC");
|
|
|
|
case Song::Type_Mpeg: return tr("MP3"); // Not technically correct
|
|
|
|
case Song::Type_OggFlac: return tr("Ogg FLAC");
|
|
|
|
case Song::Type_OggSpeex: return tr("Ogg Speex");
|
|
|
|
case Song::Type_OggVorbis: return tr("Ogg Vorbis");
|
|
|
|
case Song::Type_Aiff: return tr("AIFF");
|
|
|
|
case Song::Type_Wav: return tr("WAV");
|
|
|
|
case Song::Type_TrueAudio: return tr("TrueAudio");
|
|
|
|
|
|
|
|
case Song::Type_Stream: return tr("Stream");
|
|
|
|
|
|
|
|
case Song::Type_Unknown:
|
|
|
|
default:
|
|
|
|
return tr("Unknown");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QWidget* TextItemDelegate::createEditor(
|
|
|
|
QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const {
|
|
|
|
return new QLineEdit(parent);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-05-09 02:10:26 +02:00
|
|
|
TagCompletionModel::TagCompletionModel(LibraryBackend* backend, Playlist::Column column) :
|
2010-03-29 18:59:01 +02:00
|
|
|
QStringListModel() {
|
|
|
|
|
|
|
|
switch(column) {
|
|
|
|
case Playlist::Column_Artist: {
|
2010-05-09 02:10:26 +02:00
|
|
|
setStringList(backend->GetAllArtists());
|
2010-03-29 18:59:01 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Playlist::Column_Album: {
|
|
|
|
QStringList album_names;
|
2010-05-09 02:10:26 +02:00
|
|
|
LibraryBackend::AlbumList albums = backend->GetAllAlbums();
|
2010-03-29 18:59:01 +02:00
|
|
|
foreach(const LibraryBackend::Album& album, albums)
|
|
|
|
album_names << album.album_name;
|
|
|
|
setStringList(album_names);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Playlist::Column_AlbumArtist: {
|
|
|
|
// TODO: get all albumartists?
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-09 02:10:26 +02:00
|
|
|
TagCompleter::TagCompleter(LibraryBackend* backend, Playlist::Column column, QLineEdit* editor) :
|
2010-03-29 18:59:01 +02:00
|
|
|
QCompleter(editor) {
|
|
|
|
|
2010-05-09 02:10:26 +02:00
|
|
|
setModel(new TagCompletionModel(backend, column));
|
2010-03-29 18:59:01 +02:00
|
|
|
setCaseSensitivity(Qt::CaseInsensitive);
|
|
|
|
editor->setCompleter(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
QWidget* TagCompletionItemDelegate::createEditor(
|
2010-05-09 02:10:26 +02:00
|
|
|
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;
|
|
|
|
}
|