2012-12-28 01:59:39 +01:00
|
|
|
/* ============================================================
|
|
|
|
* QupZilla - WebKit based browser
|
2016-04-24 10:17:13 +02:00
|
|
|
* Copyright (C) 2010-2016 David Rosca <nowrep@gmail.com>
|
2012-12-28 01:59:39 +01:00
|
|
|
*
|
|
|
|
* 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 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
* ============================================================ */
|
|
|
|
#include "listitemdelegate.h"
|
2014-02-11 22:53:06 +01:00
|
|
|
#include "mainapplication.h"
|
|
|
|
#include "proxystyle.h"
|
2012-12-28 01:59:39 +01:00
|
|
|
|
|
|
|
#include <QApplication>
|
|
|
|
#include <QPainter>
|
|
|
|
|
|
|
|
ListItemDelegate::ListItemDelegate(int iconSize, QWidget* parent)
|
|
|
|
: QStyledItemDelegate(parent)
|
|
|
|
, m_iconSize(iconSize)
|
2013-02-11 17:11:13 +01:00
|
|
|
, m_updateParentHeight(false)
|
2013-02-20 11:46:05 +01:00
|
|
|
, m_uniformItemSizes(false)
|
2012-12-28 01:59:39 +01:00
|
|
|
, m_itemHeight(0)
|
2013-02-20 11:46:05 +01:00
|
|
|
, m_itemWidth(0)
|
2012-12-28 01:59:39 +01:00
|
|
|
, m_padding(0)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-02-11 17:11:13 +01:00
|
|
|
void ListItemDelegate::setUpdateParentHeight(bool update)
|
|
|
|
{
|
|
|
|
m_updateParentHeight = update;
|
|
|
|
}
|
|
|
|
|
2013-02-20 11:46:05 +01:00
|
|
|
void ListItemDelegate::setUniformItemSizes(bool uniform)
|
|
|
|
{
|
|
|
|
m_uniformItemSizes = uniform;
|
|
|
|
}
|
|
|
|
|
2012-12-28 01:59:39 +01:00
|
|
|
int ListItemDelegate::itemHeight() const
|
|
|
|
{
|
|
|
|
return m_itemHeight;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ListItemDelegate::paint(QPainter* painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
|
|
|
|
{
|
2016-04-24 10:17:13 +02:00
|
|
|
QStyleOptionViewItem opt = option;
|
2012-12-28 01:59:39 +01:00
|
|
|
initStyleOption(&opt, index);
|
|
|
|
|
|
|
|
const QWidget* w = opt.widget;
|
|
|
|
const QStyle* style = w ? w->style() : QApplication::style();
|
|
|
|
const Qt::LayoutDirection direction = w ? w->layoutDirection() : QApplication::layoutDirection();
|
|
|
|
|
|
|
|
#ifdef Q_OS_WIN
|
|
|
|
const QPalette::ColorRole colorRole = QPalette::Text;
|
|
|
|
#else
|
|
|
|
const QPalette::ColorRole colorRole = opt.state & QStyle::State_Selected ? QPalette::HighlightedText : QPalette::Text;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
int topPosition = opt.rect.top() + m_padding;
|
|
|
|
|
|
|
|
// Draw background
|
2014-02-11 14:54:18 +01:00
|
|
|
// Use PanelItemViewRow, because of Qt5's Fusion style incorrectly renders PanelItemViewItem
|
2014-03-10 00:47:07 +01:00
|
|
|
if (mApp->styleName() == QLatin1String("fusion")) {
|
2014-02-11 22:53:06 +01:00
|
|
|
style->drawPrimitive(QStyle::PE_PanelItemViewRow, &opt, painter, w);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
style->drawPrimitive(QStyle::PE_PanelItemViewItem, &opt, painter, w);
|
|
|
|
}
|
2012-12-28 01:59:39 +01:00
|
|
|
|
|
|
|
// Draw icon
|
|
|
|
QRect iconRect(opt.rect.left() + (opt.rect.width() - m_iconSize) / 2, topPosition, m_iconSize, m_iconSize);
|
|
|
|
QRect visualIconRect = style->visualRect(direction, opt.rect, iconRect);
|
|
|
|
QPixmap pixmap = index.data(Qt::DecorationRole).value<QIcon>().pixmap(m_iconSize);
|
|
|
|
painter->drawPixmap(visualIconRect, pixmap);
|
|
|
|
topPosition += m_iconSize + m_padding;
|
|
|
|
|
|
|
|
// Draw title
|
2013-12-30 13:43:48 +01:00
|
|
|
const QString title = index.data(Qt::DisplayRole).toString();
|
2012-12-28 01:59:39 +01:00
|
|
|
const int leftTitleEdge = opt.rect.left() + m_padding;
|
|
|
|
QRect titleRect(leftTitleEdge, topPosition, opt.rect.width() - 2 * m_padding, opt.fontMetrics.height());
|
|
|
|
QRect visualTitleRect = style->visualRect(direction, opt.rect, titleRect);
|
|
|
|
style->drawItemText(painter, visualTitleRect, Qt::AlignCenter, opt.palette, true, title, colorRole);
|
|
|
|
}
|
|
|
|
|
|
|
|
QSize ListItemDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
|
|
|
|
{
|
|
|
|
if (!m_itemHeight) {
|
2016-04-24 10:17:13 +02:00
|
|
|
QStyleOptionViewItem opt(option);
|
2012-12-28 01:59:39 +01:00
|
|
|
initStyleOption(&opt, index);
|
|
|
|
|
|
|
|
const QWidget* w = opt.widget;
|
|
|
|
const QStyle* style = w ? w->style() : QApplication::style();
|
|
|
|
const int padding = style->pixelMetric(QStyle::PM_FocusFrameHMargin, 0) + 1;
|
|
|
|
|
|
|
|
m_padding = padding > 5 ? padding : 5;
|
|
|
|
|
|
|
|
m_itemHeight = 3 * m_padding + opt.fontMetrics.height() + m_iconSize;
|
2013-02-11 17:11:13 +01:00
|
|
|
|
|
|
|
// Update height of parent widget
|
|
|
|
QWidget* p = qobject_cast<QWidget*>(parent());
|
|
|
|
if (p && m_updateParentHeight) {
|
2013-05-27 18:00:20 +02:00
|
|
|
int frameWidth = p->style()->pixelMetric(QStyle::PM_DefaultFrameWidth, 0, p);
|
|
|
|
p->setFixedHeight(m_itemHeight + 2 * frameWidth);
|
2013-02-11 17:11:13 +01:00
|
|
|
}
|
2012-12-28 01:59:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int width = 2 * m_padding + option.fontMetrics.width(index.data(Qt::DisplayRole).toString());
|
2013-02-20 11:46:05 +01:00
|
|
|
width = width > (m_iconSize + 2 * m_padding) ? width : m_iconSize + 2 * m_padding;
|
|
|
|
|
|
|
|
if (m_uniformItemSizes) {
|
|
|
|
if (width > m_itemWidth) {
|
|
|
|
m_itemWidth = width;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
width = m_itemWidth;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return QSize(width, m_itemHeight);
|
2012-12-28 01:59:39 +01:00
|
|
|
}
|