danbooru-client/src/model/danboorupostdelegate.cpp

239 lines
6.8 KiB
C++

/*
* <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 <QDebug>
#include <QApplication>
// KDE
#include <KPushButton>
#include <KLocale>
#include <KLocalizedString>
namespace Danbooru {
const int DanbooruPostDelegate::MARGIN = 5;
DanbooruPostDelegate::DanbooruPostDelegate(QListView* itemView):QStyledItemDelegate(itemView),
m_itemView(itemView)
{
//FIXME: Copied directly from Gwenview without understanding
#define pm(x) itemView->style()->pixelMetric(QStyle::x)
m_margin = pm(PM_ToolBarItemMargin);
m_spacing = pm(PM_ToolBarItemSpacing);
#undef pm
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 KPushButton(KIcon("download"),
QString(), itemView);
m_viewButton = new KPushButton(KIcon("view-preview"), QString(),
itemView);
m_downloadButton->hide();
m_viewButton->hide();
m_downloadButton->resize(m_buttonSize, m_buttonSize);
m_viewButton->resize(m_buttonSize, m_buttonSize);
// Signal-slot connections
connect(m_viewButton, SIGNAL(clicked()), this,
SLOT(viewButtonClicked()));
connect(m_downloadButton, SIGNAL(clicked()), this,
SLOT(downloadButtonClicked()));
}
void DanbooruPostDelegate::paint(QPainter* painter,
const QStyleOptionViewItem& option,
const QModelIndex& index) const
{
if (!index.isValid()) {
return;
}
QStyleOptionViewItemV4 opt(option);
// Pixmap
painter->save();
QStyle *style = QApplication::style();
style->drawPrimitive(QStyle::PE_PanelItemViewItem, &option, painter, 0);
painter->setRenderHint(QPainter::Antialiasing);
QPixmap pixmap = index.data(Qt::DecorationRole).value<QPixmap>();
if (pixmap.isNull()) {
return;
}
QRect rect = option.rect;
QFontMetrics metrics = option.fontMetrics;
QRect textRect(rect.left(), rect.bottom() - 3 * metrics.height(),
rect.width(), 3 * metrics.height());
// Scaling is unavoidable to keep things in the right dimension
QPixmap scaled = pixmap.scaled(rect.width() - MARGIN,
rect.height() - MARGIN,
Qt::KeepAspectRatio,
Qt::SmoothTransformation);
QRect pixRect = scaled.rect();
pixRect.moveCenter(rect.center());
// painter->drawRect(option.rect);
// move the pixmap up to accomodate some lines of text
pixRect.moveTo(pixRect.left(),
pixRect.top() + textRect.height() / 2);
pixRect.setBottom(textRect.top() - MARGIN);
painter->drawPixmap(pixRect, scaled);
painter->restore();
// Buttons
painter->save();
if (option.state & QStyle::State_MouseOver) {
// Get the bottom coordinate for the buttons
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 (resolution)
DanbooruPost* post = index.data().value<Danbooru::DanbooruPost*>();
painter->save();
int imageHeight = post->height();
int imageWidth = post->width();
QString imageText = i18n(
"File size: %1",
KGlobal::locale()->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";
imageText += i18n("Rating: %1", post->rating());
painter->drawText(textRect, imageText);
painter->restore();
}
QSize DanbooruPostDelegate::sizeHint(const QStyleOptionViewItem& option,
const QModelIndex& index) const
{
Q_UNUSED(option)
Q_UNUSED(index)
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