2011-08-28 23:20:22 +02:00
|
|
|
/* This file is part of Clementine.
|
|
|
|
Copyright 2010, David Sansome <me@davidsansome.com>
|
|
|
|
|
|
|
|
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 "globalsearchitemdelegate.h"
|
|
|
|
#include "globalsearchwidget.h"
|
2011-09-17 16:28:26 +02:00
|
|
|
#include "searchprovider.h"
|
|
|
|
#include "core/logging.h"
|
2011-08-28 23:20:22 +02:00
|
|
|
|
|
|
|
#include <QApplication>
|
|
|
|
#include <QPainter>
|
|
|
|
|
|
|
|
|
|
|
|
const int GlobalSearchItemDelegate::kHeight = SearchProvider::kArtHeight;
|
2011-09-17 16:24:46 +02:00
|
|
|
const int GlobalSearchItemDelegate::kMargin = 2;
|
2011-08-28 23:20:22 +02:00
|
|
|
const int GlobalSearchItemDelegate::kArtMargin = 6;
|
2011-09-17 17:21:08 +02:00
|
|
|
const int GlobalSearchItemDelegate::kProviderIconSize = 16;
|
2011-08-28 23:20:22 +02:00
|
|
|
|
|
|
|
GlobalSearchItemDelegate::GlobalSearchItemDelegate(GlobalSearchWidget* widget)
|
|
|
|
: QStyledItemDelegate(widget),
|
|
|
|
widget_(widget)
|
|
|
|
{
|
2011-09-17 16:28:26 +02:00
|
|
|
no_cover_ = QPixmap::fromImage(SearchProvider::ScaleAndPad(QImage(":nocover.png")));
|
2011-08-28 23:20:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
QSize GlobalSearchItemDelegate::sizeHint(const QStyleOptionViewItem& option,
|
|
|
|
const QModelIndex& index) const {
|
|
|
|
QSize size = QStyledItemDelegate::sizeHint(option, index);
|
2011-08-28 23:54:51 +02:00
|
|
|
size.setHeight(kHeight + kMargin*2);
|
2011-08-28 23:20:22 +02:00
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GlobalSearchItemDelegate::paint(QPainter* p,
|
|
|
|
const QStyleOptionViewItem& option,
|
|
|
|
const QModelIndex& index) const {
|
|
|
|
const SearchProvider::Result result =
|
2011-09-17 18:42:14 +02:00
|
|
|
index.data(GlobalSearchWidget::Role_PrimaryResult).value<SearchProvider::Result>();
|
|
|
|
const SearchProvider::ResultList all_results =
|
|
|
|
index.data(GlobalSearchWidget::Role_AllResults).value<SearchProvider::ResultList>();
|
2011-08-28 23:20:22 +02:00
|
|
|
const Song& m = result.metadata_;
|
2011-09-17 16:03:56 +02:00
|
|
|
const bool selected = option.state & QStyle::State_Selected;
|
2011-08-28 23:20:22 +02:00
|
|
|
|
|
|
|
widget_->LazyLoadArt(index);
|
|
|
|
|
|
|
|
QFont bold_font = option.font;
|
|
|
|
bold_font.setBold(true);
|
|
|
|
|
2011-09-17 17:21:08 +02:00
|
|
|
QFont big_font(bold_font);
|
|
|
|
big_font.setPointSizeF(big_font.pointSizeF() + 2);
|
|
|
|
|
2011-09-17 16:03:56 +02:00
|
|
|
QColor pen = option.palette.color(selected ? QPalette::HighlightedText : QPalette::Text);
|
2011-08-28 23:20:22 +02:00
|
|
|
QColor light_pen = pen;
|
|
|
|
pen.setAlpha(200);
|
|
|
|
light_pen.setAlpha(128);
|
|
|
|
|
|
|
|
// Draw the background
|
|
|
|
const QStyleOptionViewItemV3* vopt = qstyleoption_cast<const QStyleOptionViewItemV3*>(&option);
|
|
|
|
const QWidget* widget = vopt->widget;
|
|
|
|
QStyle* style = widget->style() ? widget->style() : QApplication::style();
|
|
|
|
style->drawPrimitive(QStyle::PE_PanelItemViewItem, &option, p, widget);
|
|
|
|
|
|
|
|
// Draw the album art. This will already be the correct size.
|
|
|
|
const QRect rect = option.rect;
|
2011-08-28 23:54:51 +02:00
|
|
|
const QRect art_rect(rect.left() + kMargin, rect.top() + kMargin, kHeight, kHeight);
|
2011-08-28 23:20:22 +02:00
|
|
|
|
|
|
|
QPixmap art = index.data(Qt::DecorationRole).value<QPixmap>();
|
|
|
|
if (art.isNull())
|
|
|
|
art = no_cover_;
|
|
|
|
|
|
|
|
p->drawPixmap(art_rect, art);
|
|
|
|
|
2011-09-17 17:21:08 +02:00
|
|
|
// Draw a track count indicator next to the art.
|
|
|
|
QRect count_rect(art_rect.right() + kArtMargin, art_rect.top(),
|
|
|
|
kHeight, kHeight);
|
|
|
|
|
|
|
|
QString count;
|
|
|
|
switch (result.type_) {
|
2011-10-16 22:57:53 +02:00
|
|
|
case globalsearch::Type_Track:
|
2011-09-17 17:21:08 +02:00
|
|
|
break;
|
|
|
|
|
2011-10-16 22:57:53 +02:00
|
|
|
case globalsearch::Type_Stream:
|
2011-09-24 18:01:18 +02:00
|
|
|
count = QString::fromUtf8("∞");
|
|
|
|
break;
|
|
|
|
|
2011-10-16 22:57:53 +02:00
|
|
|
case globalsearch::Type_Album:
|
2011-09-17 17:21:08 +02:00
|
|
|
if (result.album_size_ <= 0)
|
|
|
|
count = "-";
|
|
|
|
else
|
|
|
|
count = QString::number(result.album_size_);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
p->setPen(light_pen);
|
|
|
|
p->setFont(big_font);
|
|
|
|
p->drawText(count_rect, Qt::TextSingleLine | Qt::AlignCenter, count);
|
|
|
|
|
2011-09-17 18:42:14 +02:00
|
|
|
// Draw provider icons on the right.
|
|
|
|
const int icons_width = (kProviderIconSize + kArtMargin) * all_results.count();
|
|
|
|
QRect icons_rect(rect.right() - icons_width,
|
2011-09-17 17:21:08 +02:00
|
|
|
rect.top() + (rect.height() - kProviderIconSize) / 2,
|
2011-09-17 18:42:14 +02:00
|
|
|
icons_width, kProviderIconSize);
|
|
|
|
|
|
|
|
QRect icon_rect(icons_rect.topLeft(), QSize(kProviderIconSize, kProviderIconSize));
|
|
|
|
foreach (const SearchProvider::Result& result, all_results) {
|
|
|
|
p->drawPixmap(icon_rect, result.provider_->icon().pixmap(kProviderIconSize));
|
|
|
|
icon_rect.translate(kProviderIconSize + kArtMargin, 0);
|
|
|
|
}
|
2011-09-17 17:21:08 +02:00
|
|
|
|
2011-08-28 23:20:22 +02:00
|
|
|
// Position text
|
2011-09-17 17:21:08 +02:00
|
|
|
QRect text_rect(count_rect.right() + kArtMargin, count_rect.top(),
|
2011-09-17 18:42:14 +02:00
|
|
|
icons_rect.left() - count_rect.right() - kArtMargin*2, kHeight);
|
2011-08-28 23:20:22 +02:00
|
|
|
QRect text_rect_1(text_rect.adjusted(0, 0, 0, -kHeight/2));
|
|
|
|
QRect text_rect_2(text_rect.adjusted(0, kHeight/2, 0, 0));
|
|
|
|
|
2011-09-17 16:24:46 +02:00
|
|
|
QString line_1;
|
|
|
|
QString line_2;
|
|
|
|
|
2011-08-28 23:20:22 +02:00
|
|
|
// The text we draw depends on the type of result.
|
|
|
|
switch (result.type_) {
|
2011-10-16 22:57:53 +02:00
|
|
|
case globalsearch::Type_Track:
|
|
|
|
case globalsearch::Type_Stream: {
|
2011-08-28 23:20:22 +02:00
|
|
|
// Title
|
2011-11-11 23:11:25 +01:00
|
|
|
line_1 += m.PrettyTitle() + " ";
|
2011-08-28 23:20:22 +02:00
|
|
|
|
2011-09-17 16:24:46 +02:00
|
|
|
// Artist - Album - Track n
|
2011-08-28 23:20:22 +02:00
|
|
|
if (!m.artist().isEmpty()) {
|
2011-09-17 16:24:46 +02:00
|
|
|
line_2 += m.artist();
|
2011-08-28 23:20:22 +02:00
|
|
|
} else if (!m.albumartist().isEmpty()) {
|
2011-09-17 16:24:46 +02:00
|
|
|
line_2 += m.albumartist();
|
2011-08-28 23:20:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!m.album().isEmpty()) {
|
2011-09-17 16:24:46 +02:00
|
|
|
line_2 += " - " + m.album();
|
2011-08-28 23:20:22 +02:00
|
|
|
}
|
|
|
|
|
2011-08-29 00:24:54 +02:00
|
|
|
if (m.track() > 0) {
|
2011-09-17 16:24:46 +02:00
|
|
|
line_2 += " - " + tr("track %1").arg(m.track());
|
2011-08-29 00:24:54 +02:00
|
|
|
}
|
|
|
|
|
2011-08-28 23:20:22 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-10-16 22:57:53 +02:00
|
|
|
case globalsearch::Type_Album: {
|
2011-08-28 23:20:22 +02:00
|
|
|
// Line 1 is Artist - Album
|
|
|
|
// Artist
|
|
|
|
if (!m.albumartist().isEmpty())
|
2011-09-17 16:24:46 +02:00
|
|
|
line_1 += m.albumartist();
|
2011-08-28 23:20:22 +02:00
|
|
|
else if (m.is_compilation())
|
2011-10-30 17:53:39 +01:00
|
|
|
line_1 += tr("Various artists");
|
2011-08-28 23:20:22 +02:00
|
|
|
else if (!m.artist().isEmpty())
|
2011-09-17 16:24:46 +02:00
|
|
|
line_1 += m.artist();
|
2011-08-28 23:20:22 +02:00
|
|
|
else
|
2011-09-17 16:24:46 +02:00
|
|
|
line_1 += tr("Unknown");
|
2011-08-28 23:20:22 +02:00
|
|
|
|
|
|
|
// Dash
|
2011-09-17 16:24:46 +02:00
|
|
|
line_1 += " - ";
|
2011-08-28 23:20:22 +02:00
|
|
|
|
|
|
|
// Album
|
|
|
|
if (m.album().isEmpty())
|
2011-09-17 16:24:46 +02:00
|
|
|
line_1 += tr("Unknown");
|
2011-08-28 23:20:22 +02:00
|
|
|
else
|
2011-09-17 16:24:46 +02:00
|
|
|
line_1 += m.album();
|
2011-08-28 23:20:22 +02:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2011-09-17 16:24:46 +02:00
|
|
|
|
2011-09-17 17:21:08 +02:00
|
|
|
if (line_2.isEmpty()) {
|
|
|
|
text_rect_1 = text_rect;
|
|
|
|
}
|
|
|
|
|
2011-09-17 16:24:46 +02:00
|
|
|
p->setFont(bold_font);
|
|
|
|
p->setPen(pen);
|
|
|
|
p->drawText(text_rect_1, Qt::TextSingleLine | Qt::AlignVCenter, line_1);
|
|
|
|
|
|
|
|
p->setFont(option.font);
|
|
|
|
p->setPen(light_pen);
|
|
|
|
p->drawText(text_rect_2, Qt::TextSingleLine | Qt::AlignVCenter, line_2);
|
2011-08-28 23:20:22 +02:00
|
|
|
}
|