Falkon Develop
Cross-platform Qt-based web browser
listitemdelegate.cpp
Go to the documentation of this file.
1/* ============================================================
2* Falkon - Qt web browser
3* Copyright (C) 2010-2017 David Rosca <nowrep@gmail.com>
4*
5* This program is free software: you can redistribute it and/or modify
6* it under the terms of the GNU General Public License as published by
7* the Free Software Foundation, either version 3 of the License, or
8* (at your option) any later version.
9*
10* This program is distributed in the hope that it will be useful,
11* but WITHOUT ANY WARRANTY; without even the implied warranty of
12* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13* GNU General Public License for more details.
14*
15* You should have received a copy of the GNU General Public License
16* along with this program. If not, see <http://www.gnu.org/licenses/>.
17* ============================================================ */
18#include "listitemdelegate.h"
19#include "mainapplication.h"
20#include "proxystyle.h"
21
22#include <QApplication>
23#include <QPainter>
24#include <QtGuiVersion>
25
26ListItemDelegate::ListItemDelegate(int iconSize, QWidget* parent)
27 : QStyledItemDelegate(parent)
28 , m_iconSize(iconSize)
29 , m_updateParentHeight(false)
30 , m_uniformItemSizes(false)
31 , m_itemHeight(0)
32 , m_itemWidth(0)
33 , m_padding(0)
34{
35}
36
38{
39 m_updateParentHeight = update;
40}
41
43{
44 m_uniformItemSizes = uniform;
45}
46
48{
49 return m_itemHeight;
50}
51
52void ListItemDelegate::paint(QPainter* painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
53{
54 QStyleOptionViewItem opt = option;
55 initStyleOption(&opt, index);
56
57 const QWidget* w = opt.widget;
58 const QStyle* style = w ? w->style() : QApplication::style();
59 const Qt::LayoutDirection direction = w ? w->layoutDirection() : QApplication::layoutDirection();
60
61 const QPalette::ColorRole colorRole = opt.state & QStyle::State_Selected ? QPalette::HighlightedText : QPalette::Text;
62
63 QPalette::ColorGroup cg = opt.state & QStyle::State_Enabled ? QPalette::Normal : QPalette::Disabled;
64 if (cg == QPalette::Normal && !(opt.state & QStyle::State_Active)) {
65 cg = QPalette::Inactive;
66 }
67
68#ifdef Q_OS_WIN
69 opt.palette.setColor(QPalette::All, QPalette::HighlightedText, opt.palette.color(QPalette::Active, QPalette::Text));
70 opt.palette.setColor(QPalette::All, QPalette::Highlight, opt.palette.base().color().darker(108));
71#endif
72
73 QPalette textPalette = opt.palette;
74 textPalette.setCurrentColorGroup(cg);
75
76 int topPosition = opt.rect.top() + m_padding;
77
78 // Draw background
79 opt.showDecorationSelected = true;
80 style->drawPrimitive(QStyle::PE_PanelItemViewItem, &opt, painter, w);
81
82 // Draw icon
83 QRect iconRect(opt.rect.left() + (opt.rect.width() - m_iconSize) / 2, topPosition, m_iconSize, m_iconSize);
84 QRect visualIconRect = style->visualRect(direction, opt.rect, iconRect);
85 QPixmap pixmap = index.data(Qt::DecorationRole).value<QIcon>().pixmap(m_iconSize);
86 painter->drawPixmap(visualIconRect, pixmap);
87 topPosition += m_iconSize + m_padding;
88
89 // Draw title
90 const QString title = index.data(Qt::DisplayRole).toString();
91 const int leftTitleEdge = opt.rect.left() + m_padding;
92 QRect titleRect(leftTitleEdge, topPosition, opt.rect.width() - 2 * m_padding, opt.fontMetrics.height());
93 QRect visualTitleRect = style->visualRect(direction, opt.rect, titleRect);
94 style->drawItemText(painter, visualTitleRect, Qt::AlignCenter, textPalette, true, title, colorRole);
95}
96
97QSize ListItemDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
98{
99 if (!m_itemHeight) {
100 QStyleOptionViewItem opt(option);
101 initStyleOption(&opt, index);
102
103 const QWidget* w = opt.widget;
104 const QStyle* style = w ? w->style() : QApplication::style();
105 const int padding = style->pixelMetric(QStyle::PM_FocusFrameHMargin, nullptr) + 1;
106
107 m_padding = padding > 5 ? padding : 5;
108
109 m_itemHeight = 3 * m_padding + opt.fontMetrics.height() + m_iconSize;
110
111 // Update height of parent widget
112 QWidget* p = qobject_cast<QWidget*>(parent());
113 if (p && m_updateParentHeight) {
114 int frameWidth = p->style()->pixelMetric(QStyle::PM_DefaultFrameWidth, nullptr, p);
115 p->setFixedHeight(m_itemHeight + 2 * frameWidth);
116 }
117 }
118 int width = 2 * m_padding + option.fontMetrics.horizontalAdvance(index.data(Qt::DisplayRole).toString());
119 width = width > (m_iconSize + 2 * m_padding) ? width : m_iconSize + 2 * m_padding;
120
121 if (m_uniformItemSizes) {
122 if (width > m_itemWidth) {
123 m_itemWidth = width;
124 }
125 else {
126 width = m_itemWidth;
127 }
128 }
129
130 return QSize(width, m_itemHeight);
131}
void setUpdateParentHeight(bool update)
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override
ListItemDelegate(int iconSize, QWidget *parent)
void setUniformItemSizes(bool uniform)
int itemHeight() const
QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const override