/* * * Copyright 2013 Luca Beltrame * * 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 . * */ // Own #include "danboorupostdelegate.h" #include "libdanbooru/danboorupost.h" // Qt #include #include #include #include // KDE #include 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); } void DanbooruPostDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const { if (!index.isValid()) { return; } QStyle *style = QApplication::style(); style->drawPrimitive(QStyle::PE_PanelItemViewItem, &option, painter, 0); painter->setRenderHint(QPainter::Antialiasing); QPixmap pixmap = index.data(Qt::DecorationRole).value(); if (pixmap.isNull()) { return; } // Scaling is unavoidable to keep things in the right dimension //TODO: Replace fixed values by configurable values QPixmap scaled = pixmap.scaled(256-MARGIN, 256-MARGIN, Qt::KeepAspectRatio, Qt::SmoothTransformation); QRect rect = option.rect; // Draw in the center QPoint centerCoordinate = rect.center() - QPoint(scaled.width() / 2, scaled.height() / 2); // Get the bottom coordinate for the buttons, made as center - half // of the pixmap width (left) then half of the height minus the button // size and margin (because the move() call later on uses the // coordinates as TOP coordinates, while we want the bottom of the // button to align with the bottom of the pixmap) QPoint scaleFactor = QPoint( -(scaled.width() / 2) + (MARGIN / 2), (scaled.height() / 2) - m_buttonSize + (MARGIN * 1.5) ); QPoint bottomCoordinate = rect.center() + scaleFactor; painter->drawPixmap(centerCoordinate, scaled); if (option.state & QStyle::State_MouseOver) { m_downloadButton->move(centerCoordinate + QPoint(MARGIN / 2, 0)); m_viewButton->move(bottomCoordinate); m_downloadButton->show(); m_viewButton->show(); } else if (!hoveredIndex().isValid()) { m_downloadButton->hide(); m_viewButton->hide(); } } 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); } } // namespace Danbooru