Falkon Develop
Cross-platform Qt-based web browser
tablistdelegate.cpp
Go to the documentation of this file.
1/* ============================================================
2* VerticalTabs plugin for Falkon
3* Copyright (C) 2018 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 "tablistdelegate.h"
19#include "tablistview.h"
20#include "loadinganimator.h"
21
22#include "tabmodel.h"
23#include "tabicon.h"
24
25#include <QPainter>
26
28 : QStyledItemDelegate()
29 , m_view(view)
30{
31 m_padding = qMax(5, m_view->style()->pixelMetric(QStyle::PM_FocusFrameHMargin) + 1);
32
33 m_loadingAnimator = new LoadingAnimator(this);
34 connect(m_loadingAnimator, &LoadingAnimator::updateIndex, m_view, &TabListView::updateIndex);
35}
36
37QRect TabListDelegate::audioButtonRect(const QModelIndex &index) const
38{
39 if (!index.data(TabModel::AudioPlayingRole).toBool() && !index.data(TabModel::AudioMutedRole).toBool()) {
40 return QRect();
41 }
42 const QRect rect = m_view->visualRect(index);
43 const int center = rect.height() / 2 + rect.top();
44 return QRect(rect.right() - 16, center - 16 / 2, 16, 16);
45}
46
47void TabListDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
48{
49 const QWidget *w = option.widget;
50 const QStyle *style = w ? w->style() : m_view->style();
51
52 QStyleOptionViewItem opt = option;
53 initStyleOption(&opt, index);
54 m_view->adjustStyleOption(&opt);
55
56 const int height = opt.rect.height();
57 const int center = height / 2 + opt.rect.top();
58
59 painter->setRenderHint(QPainter::Antialiasing);
60
61 // Draw background
62 style->drawPrimitive(QStyle::PE_PanelItemViewItem, &opt, painter, w);
63
64 // Draw icon
65 const int iconSize = 16;
66 const int iconYPos = center - (iconSize / 2);
67 QRect iconRect(opt.rect.left() + (opt.rect.width() - iconSize) / 2, iconYPos, iconSize, iconSize);
68 QPixmap pixmap;
69 if (index.data(TabModel::LoadingRole).toBool()) {
70 pixmap = m_loadingAnimator->pixmap(index);
71 } else {
72 pixmap = index.data(Qt::DecorationRole).value<QIcon>().pixmap(iconSize);
73 }
74 painter->drawPixmap(iconRect, pixmap);
75
76 // Draw audio icon
77 const bool audioMuted = index.data(TabModel::AudioMutedRole).toBool();
78 const bool audioPlaying = index.data(TabModel::AudioPlayingRole).toBool();
79 if (audioMuted || audioPlaying) {
80 QSize audioSize(16, 16);
81 QPoint pos(opt.rect.right() - audioSize.width(), center - audioSize.height() / 2);
82 QRect audioRect(pos, audioSize);
83
84 QColor c = opt.palette.color(QPalette::Window);
85 c.setAlpha(180);
86 painter->setPen(c);
87 painter->setBrush(c);
88 painter->drawEllipse(audioRect);
89
90 painter->drawPixmap(audioRect, audioMuted ? TabIcon::data()->audioMutedPixmap : TabIcon::data()->audioPlayingPixmap);
91 }
92
93 // Draw background activity indicator
94 const bool backgroundActivity = index.data(TabModel::BackgroundActivityRole).toBool();
95 if (backgroundActivity) {
96 QSize activitySize(7, 7);
97 QPoint pos(iconRect.center().x() - activitySize.width() / 2 + 1, iconRect.bottom() - 2);
98 QRect activityRect(pos, activitySize);
99
100 QColor c1 = opt.palette.color(QPalette::Window);
101 c1.setAlpha(180);
102 painter->setPen(Qt::transparent);
103 painter->setBrush(c1);
104 painter->drawEllipse(activityRect);
105
106 const QRect r2 = activityRect.adjusted(1, 1, -1, -1);
107 painter->setPen(Qt::transparent);
108 painter->setBrush(opt.palette.color(QPalette::Text));
109 painter->drawEllipse(r2);
110 }
111}
112
113QSize TabListDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
114{
115 QStyleOptionViewItem opt(option);
116 initStyleOption(&opt, index);
117
118 return QSize(m_padding * 4 + 16, m_padding * 2 + opt.fontMetrics.height());
119}
void updateIndex(const QModelIndex &index)
QPixmap pixmap(const QModelIndex &index)
static Data * data()
Definition: tabicon.cpp:99
QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const override
QRect audioButtonRect(const QModelIndex &index) const
TabListDelegate(TabListView *view)
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override
void adjustStyleOption(QStyleOptionViewItem *option)
Definition: tablistview.cpp:75
void updateIndex(const QModelIndex &index)
Definition: tablistview.cpp:63
@ AudioPlayingRole
Definition: tabmodel.h:61
@ LoadingRole
Definition: tabmodel.h:60
@ BackgroundActivityRole
Definition: tabmodel.h:63
@ AudioMutedRole
Definition: tabmodel.h:62