So long, custom C++ delegate: you won't be missed
This commit is contained in:
parent
8b56570223
commit
5d9988feed
2 changed files with 0 additions and 358 deletions
|
@ -1,258 +0,0 @@
|
|||
/*
|
||||
* <one line to give the library's name and an idea of what it does.>
|
||||
* Copyright 2013 Luca Beltrame <lbeltrame@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) version 3 or any later version
|
||||
* accepted by the membership of KDE e.V. (or its successor approved
|
||||
* by the membership of KDE e.V.), which shall act as a proxy
|
||||
* defined in Section 14 of version 3 of the license.
|
||||
*
|
||||
* 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/>.
|
||||
*
|
||||
*/
|
||||
|
||||
// Own
|
||||
|
||||
#include "danboorupostdelegate.h"
|
||||
#include "libdanbooru/danboorupost.h"
|
||||
|
||||
// Qt
|
||||
|
||||
#include <QListView>
|
||||
#include <QPainter>
|
||||
#include <QApplication>
|
||||
|
||||
// KDE
|
||||
|
||||
#include <QPushButton>
|
||||
#include <KLocalizedString>
|
||||
#include <KIconLoader>
|
||||
#include <KFormat>
|
||||
|
||||
namespace Danbooru
|
||||
{
|
||||
|
||||
const int DanbooruPostDelegate::MARGIN = 5;
|
||||
|
||||
DanbooruPostDelegate::DanbooruPostDelegate(QListView *itemView): QStyledItemDelegate(itemView),
|
||||
m_itemView(itemView)
|
||||
{
|
||||
|
||||
// Get the sizes for the buttons
|
||||
|
||||
const int iconSize = KIconLoader::global()->currentSize(
|
||||
KIconLoader::Toolbar
|
||||
);
|
||||
|
||||
const QSize sz = itemView->style()->sizeFromContents(
|
||||
QStyle::CT_ToolButton, 0,
|
||||
QSize(iconSize, iconSize));
|
||||
|
||||
m_buttonSize = qMax(sz.width(), sz.height());
|
||||
|
||||
m_downloadButton = new QPushButton(QIcon::fromTheme("download"),
|
||||
QString(), itemView);
|
||||
m_viewButton = new QPushButton(QIcon::fromTheme("view-preview"), QString(),
|
||||
itemView);
|
||||
|
||||
m_downloadButton->hide();
|
||||
m_viewButton->hide();
|
||||
|
||||
m_downloadButton->resize(m_buttonSize, m_buttonSize);
|
||||
m_viewButton->resize(m_buttonSize, m_buttonSize);
|
||||
m_downloadButton->setToolTip(i18n("Download image"));
|
||||
m_viewButton->setToolTip(i18n("View image"));
|
||||
|
||||
// Signal-slot connections
|
||||
|
||||
connect(m_viewButton, &QPushButton::clicked, this, &DanbooruPostDelegate::viewButtonClicked);
|
||||
connect(m_downloadButton, &QPushButton::clicked, this, &DanbooruPostDelegate::downloadButtonClicked);
|
||||
|
||||
}
|
||||
|
||||
void DanbooruPostDelegate::paint(QPainter *painter,
|
||||
const QStyleOptionViewItem &option,
|
||||
const QModelIndex &index) const
|
||||
{
|
||||
|
||||
if (!index.isValid()) {
|
||||
return;
|
||||
}
|
||||
|
||||
QStyleOptionViewItemV4 opt(option);
|
||||
|
||||
// Pixmap
|
||||
|
||||
QStyle *style = QApplication::style();
|
||||
|
||||
style->drawPrimitive(QStyle::PE_PanelItemViewItem, &opt, painter, 0);
|
||||
painter->setRenderHint(QPainter::Antialiasing);
|
||||
|
||||
QPixmap pixmap = index.data(Qt::DecorationRole).value<QPixmap>();
|
||||
|
||||
if (pixmap.isNull()) {
|
||||
return;
|
||||
}
|
||||
|
||||
QRect rect = opt.rect;
|
||||
QFontMetrics metrics = opt.fontMetrics;
|
||||
QRect textRect(rect.left() + MARGIN, rect.bottom() - 3 * metrics.height(),
|
||||
rect.width(), 3 * metrics.height());
|
||||
|
||||
// Scaling is unavoidable to keep things in the right dimension
|
||||
|
||||
QPixmap scaled;
|
||||
|
||||
// Reserve enough space for the pixmap + the 3 lines of text:
|
||||
// this prevents issues with images that have height > width
|
||||
// (like in yande.re which keeps a lot of scans)
|
||||
|
||||
int maxHeight = rect.height() - 3 * metrics.height() - 2 * MARGIN;
|
||||
|
||||
scaled = pixmap.scaled(rect.width() - 2 * MARGIN,
|
||||
maxHeight,
|
||||
Qt::KeepAspectRatio,
|
||||
Qt::SmoothTransformation);
|
||||
|
||||
QRect pixRect = scaled.rect();
|
||||
pixRect.moveCenter(rect.center());
|
||||
|
||||
// move the pixmap up to accomodate some lines of text
|
||||
|
||||
pixRect.moveTo(pixRect.left(),
|
||||
pixRect.top() - textRect.height() / 2);
|
||||
pixRect.moveBottom(textRect.top() - MARGIN);
|
||||
|
||||
painter->drawPixmap(pixRect, scaled);
|
||||
|
||||
painter->save();
|
||||
|
||||
// Show buttons on mouseover
|
||||
|
||||
if (option.state & QStyle::State_MouseOver) {
|
||||
|
||||
// Get the bottom coordinate for the buttons
|
||||
// TODO: Perhaps add some transition?
|
||||
|
||||
m_downloadButton->move(pixRect.topLeft());
|
||||
m_viewButton->move(pixRect.bottomLeft() - QPoint(
|
||||
0, m_viewButton->height() - 1.5 * MARGIN));
|
||||
|
||||
m_downloadButton->show();
|
||||
m_viewButton->show();
|
||||
|
||||
} else if (!hoveredIndex().isValid()) {
|
||||
m_downloadButton->hide();
|
||||
m_viewButton->hide();
|
||||
}
|
||||
|
||||
painter->restore();
|
||||
|
||||
// Text
|
||||
|
||||
DanbooruPost *post = index.data().value<Danbooru::DanbooruPost *>();
|
||||
|
||||
painter->save();
|
||||
|
||||
int imageHeight = post->height();
|
||||
int imageWidth = post->width();
|
||||
|
||||
KFormat format;
|
||||
|
||||
QString imageText = i18n(
|
||||
"File size: %1",
|
||||
format.formatByteSize(post->size()));
|
||||
|
||||
KLocalizedString sizestr = ki18np("1 pixel", "%1 pixels");
|
||||
|
||||
imageText += "\n";
|
||||
imageText += i18n("Resolution: %1 x %2",
|
||||
sizestr.subs(imageWidth).toString(),
|
||||
sizestr.subs(imageHeight).toString());
|
||||
imageText += "\n";
|
||||
|
||||
QString ratingString;
|
||||
|
||||
switch (post->rating()) {
|
||||
case DanbooruPost::Safe:
|
||||
ratingString = i18n("Safe");
|
||||
break;
|
||||
case DanbooruPost::Questionable:
|
||||
ratingString = i18n("Questionable");
|
||||
break;
|
||||
case DanbooruPost::Explicit:
|
||||
ratingString = i18n("Explicit");
|
||||
break;
|
||||
default:
|
||||
ratingString = i18nc("Unknown post rating", "Unknown");
|
||||
}
|
||||
|
||||
imageText += i18n("Rating: %1", ratingString);
|
||||
|
||||
painter->drawText(textRect, imageText);
|
||||
painter->restore();
|
||||
|
||||
}
|
||||
|
||||
QSize DanbooruPostDelegate::sizeHint(const QStyleOptionViewItem &option,
|
||||
const QModelIndex &index) const
|
||||
{
|
||||
|
||||
Q_UNUSED(option)
|
||||
|
||||
if (!index.isValid()) {
|
||||
return QSize();
|
||||
}
|
||||
|
||||
return m_itemView->gridSize();
|
||||
|
||||
}
|
||||
|
||||
QModelIndex DanbooruPostDelegate::hoveredIndex() const
|
||||
{
|
||||
const QPoint pos = m_itemView->viewport()->mapFromGlobal(QCursor::pos());
|
||||
return m_itemView->indexAt(pos);
|
||||
}
|
||||
|
||||
void DanbooruPostDelegate::downloadButtonClicked()
|
||||
{
|
||||
QModelIndex index = hoveredIndex();
|
||||
|
||||
if (!index.isValid()) {
|
||||
return;
|
||||
}
|
||||
|
||||
QVariant data = index.data(Qt::DisplayRole);
|
||||
const DanbooruPost *post = data.value<DanbooruPost *>();
|
||||
|
||||
if (post) {
|
||||
Q_EMIT(postDownloadRequested(post->fileUrl()));
|
||||
}
|
||||
}
|
||||
|
||||
void DanbooruPostDelegate::viewButtonClicked()
|
||||
{
|
||||
|
||||
QModelIndex index = hoveredIndex();
|
||||
|
||||
if (!index.isValid()) {
|
||||
return;
|
||||
}
|
||||
|
||||
QVariant data = index.data(Qt::DisplayRole);
|
||||
const DanbooruPost *post = data.value<DanbooruPost *>();
|
||||
if (post) {
|
||||
Q_EMIT(postViewRequested(post->fileUrl()));
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace Danbooru
|
|
@ -1,100 +0,0 @@
|
|||
/*
|
||||
* This file is part of Danbooru Client.
|
||||
* Copyright 2013 Luca Beltrame <lbeltrame@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) version 3 or any later version
|
||||
* accepted by the membership of KDE e.V. (or its successor approved
|
||||
* by the membership of KDE e.V.), which shall act as a proxy
|
||||
* defined in Section 14 of version 3 of the license.
|
||||
*
|
||||
* 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/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef DANBOORUPOSTDELEGATE_H
|
||||
#define DANBOORUPOSTDELEGATE_H
|
||||
|
||||
/**
|
||||
* @brief This file includes custom delegates to represent DanbooruItems.
|
||||
* @file danboorupostdelegate.h
|
||||
*
|
||||
**/
|
||||
|
||||
#include <QStyledItemDelegate>
|
||||
|
||||
#include <QUrl>
|
||||
|
||||
class QListView;
|
||||
class QPainter;
|
||||
class QPushButton;
|
||||
|
||||
namespace Danbooru
|
||||
{
|
||||
|
||||
/**
|
||||
* @brief Specific delegate for Danbooru items.
|
||||
*
|
||||
*/
|
||||
|
||||
class DanbooruPost;
|
||||
|
||||
class DanbooruPostDelegate : public QStyledItemDelegate
|
||||
{
|
||||
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
DanbooruPostDelegate(QListView *itemView);
|
||||
|
||||
void paint(QPainter *painter, const QStyleOptionViewItem &option,
|
||||
const QModelIndex &index) const;
|
||||
QSize sizeHint(const QStyleOptionViewItem &option,
|
||||
const QModelIndex &index) const;
|
||||
|
||||
QModelIndex hoveredIndex() const;
|
||||
|
||||
private:
|
||||
|
||||
QListView *m_itemView;
|
||||
QPushButton *m_downloadButton;
|
||||
QPushButton *m_viewButton;
|
||||
|
||||
int m_buttonSize;
|
||||
static const int MARGIN;
|
||||
|
||||
Q_SIGNALS:
|
||||
|
||||
/**
|
||||
* @brief Emitted when the view button is clicked.
|
||||
*
|
||||
* @param postUrl the URL to the full picture.
|
||||
*
|
||||
*/
|
||||
void postViewRequested(QUrl postUrl);
|
||||
|
||||
/**
|
||||
* @brief Emitted when the download button is clicked.
|
||||
*
|
||||
* @param postUrl the URL to the full picture.
|
||||
*
|
||||
*/
|
||||
void postDownloadRequested(QUrl postUrl);
|
||||
|
||||
private Q_SLOTS:
|
||||
void viewButtonClicked();
|
||||
void downloadButtonClicked();
|
||||
|
||||
};
|
||||
|
||||
} // namespace Danbooru
|
||||
|
||||
#endif // DANBOORUPOSTDELEGATE_H
|
Loading…
Add table
Add a link
Reference in a new issue