2019-05-27 21:10:37 +02:00
|
|
|
/*
|
|
|
|
* Strawberry Music Player
|
|
|
|
* This file was part of Clementine.
|
|
|
|
* Copyright 2010, David Sansome <me@davidsansome.com>
|
2021-03-20 21:14:47 +01:00
|
|
|
* Copyright 2018-2021, Jonas Kvinge <jonas@jkvinge.net>
|
2019-05-27 21:10:37 +02:00
|
|
|
*
|
|
|
|
* Strawberry 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.
|
|
|
|
*
|
|
|
|
* Strawberry 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 Strawberry. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include <QtGlobal>
|
2020-02-08 03:40:30 +01:00
|
|
|
#include <QAbstractItemModel>
|
2019-05-27 21:10:37 +02:00
|
|
|
#include <QAbstractItemView>
|
|
|
|
#include <QStyleOptionViewItem>
|
|
|
|
#include <QVariant>
|
|
|
|
#include <QString>
|
|
|
|
#include <QLocale>
|
2020-02-08 03:40:30 +01:00
|
|
|
#include <QPixmap>
|
|
|
|
#include <QIcon>
|
2019-05-27 21:10:37 +02:00
|
|
|
#include <QPainter>
|
|
|
|
#include <QPalette>
|
|
|
|
#include <QPen>
|
|
|
|
#include <QRect>
|
|
|
|
#include <QSize>
|
|
|
|
#include <QBrush>
|
|
|
|
#include <QColor>
|
|
|
|
#include <QFont>
|
2020-02-08 03:40:30 +01:00
|
|
|
#include <QPoint>
|
2019-05-27 21:10:37 +02:00
|
|
|
#include <QLinearGradient>
|
|
|
|
#include <QToolTip>
|
|
|
|
#include <QWhatsThis>
|
|
|
|
#include <QtEvents>
|
|
|
|
|
|
|
|
#include "collectionitemdelegate.h"
|
|
|
|
#include "collectionmodel.h"
|
|
|
|
|
|
|
|
CollectionItemDelegate::CollectionItemDelegate(QObject *parent) : QStyledItemDelegate(parent) {}
|
|
|
|
|
2021-01-26 16:48:04 +01:00
|
|
|
void CollectionItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &opt, const QModelIndex &idx) const {
|
2019-05-27 21:10:37 +02:00
|
|
|
|
2021-01-26 16:48:04 +01:00
|
|
|
const bool is_divider = idx.data(CollectionModel::Role_IsDivider).toBool();
|
2019-05-27 21:10:37 +02:00
|
|
|
|
|
|
|
if (is_divider) {
|
2021-01-26 16:48:04 +01:00
|
|
|
QString text(idx.data().toString());
|
2019-05-27 21:10:37 +02:00
|
|
|
|
|
|
|
painter->save();
|
|
|
|
|
|
|
|
QRect text_rect(opt.rect);
|
|
|
|
|
|
|
|
// Does this item have an icon?
|
|
|
|
QPixmap pixmap;
|
2021-01-26 16:48:04 +01:00
|
|
|
QVariant decoration = idx.data(Qt::DecorationRole);
|
2019-05-27 21:10:37 +02:00
|
|
|
if (!decoration.isNull()) {
|
|
|
|
if (decoration.canConvert<QPixmap>()) {
|
|
|
|
pixmap = decoration.value<QPixmap>();
|
|
|
|
}
|
|
|
|
else if (decoration.canConvert<QIcon>()) {
|
|
|
|
pixmap = decoration.value<QIcon>().pixmap(opt.decorationSize);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Draw the icon at the left of the text rectangle
|
|
|
|
if (!pixmap.isNull()) {
|
|
|
|
QRect icon_rect(text_rect.topLeft(), opt.decorationSize);
|
|
|
|
const int padding = (text_rect.height() - icon_rect.height()) / 2;
|
|
|
|
icon_rect.adjust(padding, padding, padding, padding);
|
|
|
|
text_rect.moveLeft(icon_rect.right() + padding + 6);
|
|
|
|
|
|
|
|
if (pixmap.size() != opt.decorationSize) {
|
|
|
|
pixmap = pixmap.scaled(opt.decorationSize, Qt::KeepAspectRatio);
|
|
|
|
}
|
|
|
|
|
|
|
|
painter->drawPixmap(icon_rect, pixmap);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
text_rect.setLeft(text_rect.left() + 30);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Draw the text
|
|
|
|
QFont bold_font(opt.font);
|
|
|
|
bold_font.setBold(true);
|
|
|
|
|
|
|
|
painter->setPen(opt.palette.color(QPalette::Text));
|
|
|
|
painter->setFont(bold_font);
|
|
|
|
painter->drawText(text_rect, text);
|
|
|
|
|
|
|
|
// Draw the line under the item
|
|
|
|
QColor line_color = opt.palette.color(QPalette::Text);
|
|
|
|
QLinearGradient grad_color(opt.rect.bottomLeft(), opt.rect.bottomRight());
|
2022-03-22 21:09:05 +01:00
|
|
|
const double fade_start_end = (opt.rect.width() / 3.0) / opt.rect.width();
|
2019-05-27 21:10:37 +02:00
|
|
|
line_color.setAlphaF(0.0);
|
|
|
|
grad_color.setColorAt(0, line_color);
|
|
|
|
line_color.setAlphaF(0.5);
|
|
|
|
grad_color.setColorAt(fade_start_end, line_color);
|
|
|
|
grad_color.setColorAt(1.0 - fade_start_end, line_color);
|
|
|
|
line_color.setAlphaF(0.0);
|
|
|
|
grad_color.setColorAt(1, line_color);
|
|
|
|
painter->setPen(QPen(grad_color, 1));
|
|
|
|
painter->drawLine(opt.rect.bottomLeft(), opt.rect.bottomRight());
|
|
|
|
|
|
|
|
painter->restore();
|
|
|
|
}
|
|
|
|
else {
|
2021-01-26 16:48:04 +01:00
|
|
|
QStyledItemDelegate::paint(painter, opt, idx);
|
2019-05-27 21:10:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-01-26 16:48:04 +01:00
|
|
|
bool CollectionItemDelegate::helpEvent(QHelpEvent *event, QAbstractItemView *view, const QStyleOptionViewItem &option, const QModelIndex &idx) {
|
2019-05-27 21:10:37 +02:00
|
|
|
|
|
|
|
Q_UNUSED(option);
|
|
|
|
|
|
|
|
if (!event || !view) return false;
|
|
|
|
|
2021-01-26 16:48:04 +01:00
|
|
|
QString text = displayText(idx.data(), QLocale::system());
|
2019-05-27 21:10:37 +02:00
|
|
|
|
2023-07-21 07:11:21 +02:00
|
|
|
if (text.isEmpty()) return false;
|
2019-05-27 21:10:37 +02:00
|
|
|
|
|
|
|
switch (event->type()) {
|
2024-04-11 02:56:01 +02:00
|
|
|
case QEvent::ToolTip:{
|
2019-05-27 21:10:37 +02:00
|
|
|
|
2021-01-26 16:48:04 +01:00
|
|
|
QSize real_text = sizeHint(option, idx);
|
|
|
|
QRect displayed_text = view->visualRect(idx);
|
2019-05-27 21:10:37 +02:00
|
|
|
bool is_elided = displayed_text.width() < real_text.width();
|
|
|
|
|
|
|
|
if (is_elided) {
|
2021-06-20 19:04:08 +02:00
|
|
|
QToolTip::showText(event->globalPos(), text, view);
|
2019-05-27 21:10:37 +02:00
|
|
|
}
|
2021-01-26 16:48:04 +01:00
|
|
|
else if (idx.data(Qt::ToolTipRole).isValid()) {
|
2019-05-27 21:10:37 +02:00
|
|
|
// If the item has a tooltip text, display it
|
2021-01-26 16:48:04 +01:00
|
|
|
QString tooltip_text = idx.data(Qt::ToolTipRole).toString();
|
2021-06-20 19:04:08 +02:00
|
|
|
QToolTip::showText(event->globalPos(), tooltip_text, view);
|
2019-05-27 21:10:37 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
// in case that another text was previously displayed
|
|
|
|
QToolTip::hideText();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
case QEvent::QueryWhatsThis:
|
|
|
|
return true;
|
|
|
|
|
|
|
|
case QEvent::WhatsThis:
|
2021-06-20 19:04:08 +02:00
|
|
|
QWhatsThis::showText(event->globalPos(), text, view);
|
2019-05-27 21:10:37 +02:00
|
|
|
return true;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|