Pimcommon

configurepluginstreewidgetdelegate.cpp
1/*
2 SPDX-FileCopyrightText: 2024 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5*/
6
7#include "configurepluginstreewidgetdelegate.h"
8#include "configureplugins/configurepluginslistwidget.h"
9#include <QApplication>
10#include <QPainter>
11
12using namespace PimCommon;
13ConfigurePluginsTreeWidgetDelegate::ConfigurePluginsTreeWidgetDelegate(QObject *parent)
14 : QStyledItemDelegate{parent}
15{
16}
17
18ConfigurePluginsTreeWidgetDelegate::~ConfigurePluginsTreeWidgetDelegate() = default;
19
20void ConfigurePluginsTreeWidgetDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
21{
22 if (!index.isValid())
23 return;
24
25 // Get the text and split it into two lines
26 const QString text = index.data(Qt::DisplayRole).toString();
27 if (text.isEmpty()) {
28 return;
29 }
30
31 const QString description = index.data(ConfigurePluginsListWidget::PluginItemData::Description).toString();
32 if (description.isEmpty()) {
33 QStyledItemDelegate::paint(painter, option, index);
34 return;
35 }
36
37 QStyleOptionViewItem opt(option);
38 opt.showDecorationSelected = true;
40
41 painter->save();
42
43 // Check if the item has a CheckStateRole
44 const QVariant checkStateData = index.data(Qt::CheckStateRole);
45 if (checkStateData.isValid()) {
46 // Get the checkbox state
47 const Qt::CheckState checkState = static_cast<Qt::CheckState>(checkStateData.toInt());
48
49 // Get the checkbox style options
50 QStyleOptionButton checkboxOption;
51 checkboxOption.state = QStyle::State_Enabled;
52 if (checkState == Qt::Checked) {
53 checkboxOption.state |= QStyle::State_On;
54 } else if (checkState == Qt::Unchecked) {
55 checkboxOption.state |= QStyle::State_Off;
56 } else {
57 checkboxOption.state |= QStyle::State_NoChange;
58 }
59
60 // Center the checkbox in the item's rect
62 checkboxOption.rect.moveCenter(QPoint(option.rect.left(), option.rect.center().y()));
63
64 // Draw the checkbox
65 QApplication::style()->drawControl(QStyle::CE_CheckBox, &checkboxOption, painter);
66 }
67
68 // Set the painter font
69 painter->setFont(option.font);
70
71 // Calculate the text rectangles
72 QFontMetrics fontMetrics(option.font);
73 const QRect rect = option.rect;
74
75 const QSize checkboxSize = QApplication::style()->sizeFromContents(QStyle::CT_CheckBox, nullptr, QSize(), nullptr);
76
77 // Draw the two lines
78 painter->setPen(option.palette.text().color());
79 const QRect line1Rect(rect.left() + 5 + checkboxSize.width(), rect.top(), rect.width(), fontMetrics.height());
80 painter->drawText(line1Rect, Qt::AlignLeft | Qt::AlignVCenter, text);
81 if (!description.isEmpty()) {
82 QFont f = option.font;
83 f.setItalic(true);
84 f.setPointSize(f.pointSize() - 2);
85 painter->setFont(f);
86 const QRect line2Rect(rect.left() + 5 + checkboxSize.width(), rect.top() + fontMetrics.height(), rect.width(), fontMetrics.height());
87 painter->drawText(line2Rect, Qt::AlignLeft | Qt::AlignVCenter, description);
88 }
89 painter->restore();
90}
91
92QSize ConfigurePluginsTreeWidgetDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
93{
94 QFontMetrics fontMetrics(option.font);
95 const int height = fontMetrics.height() * 2; // Height for two lines
96
97 const QString description = index.data(ConfigurePluginsListWidget::PluginItemData::Description).toString();
98 int widthDescription = 0;
99 if (!description.isEmpty()) {
100 QFont f = option.font;
101 f.setItalic(true);
102 f.setPointSize(f.pointSize() - 2);
103 widthDescription = fontMetrics.horizontalAdvance(description);
104 }
105
106 const int width = qMax(fontMetrics.horizontalAdvance(index.data(Qt::DisplayRole).toString()), widthDescription);
107 return QSize(width, height);
108}
109
110#include "moc_configurepluginstreewidgetdelegate.cpp"
folderdialogacltab.h
QStyle * style()
int pointSize() const const
void setItalic(bool enable)
void setPointSize(int pointSize)
QVariant data(int role) const const
bool isValid() const const
void drawText(const QPoint &position, const QString &text)
void restore()
void save()
void setFont(const QFont &font)
void setPen(Qt::PenStyle style)
int left() const const
int top() const const
int width() const const
int width() const const
bool isEmpty() const const
PE_PanelItemViewItem
SE_ItemViewItemCheckIndicator
virtual void drawControl(ControlElement element, const QStyleOption *option, QPainter *painter, const QWidget *widget) const const=0
virtual void drawPrimitive(PrimitiveElement element, const QStyleOption *option, QPainter *painter, const QWidget *widget) const const=0
virtual QSize sizeFromContents(ContentsType type, const QStyleOption *option, const QSize &contentsSize, const QWidget *widget) const const=0
virtual QRect subElementRect(SubElement element, const QStyleOption *option, const QWidget *widget) const const=0
virtual void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const const override
AlignLeft
CheckState
DisplayRole
bool isValid() const const
int toInt(bool *ok) const const
QString toString() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 17:02:55 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.