Mailcommon

invalidfilterlistitemdelegate.cpp
1/*
2 SPDX-FileCopyrightText: 2015-2024 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5*/
6#include "invalidfilterlistitemdelegate.h"
7#include "invalidfilterlistmodel.h"
8
9#include <QAbstractItemView>
10#include <QIcon>
11#include <QLabel>
12#include <QPainter>
13#include <QPushButton>
14#include <QToolButton>
15
16using namespace MailCommon;
17
18InvalidFilterListItemDelegate::InvalidFilterListItemDelegate(QAbstractItemView *itemView, QObject *parent)
19 : KWidgetItemDelegate(itemView, parent)
20{
21}
22
23InvalidFilterListItemDelegate::~InvalidFilterListItemDelegate() = default;
24
25QSize InvalidFilterListItemDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
26{
27 Q_UNUSED(index)
28
29 const QStyle *style = itemView()->style();
30 const int buttonHeight = style->pixelMetric(QStyle::PM_ButtonMargin) * 2 + style->pixelMetric(QStyle::PM_ButtonIconSize);
31 const int fontHeight = option.fontMetrics.height();
32 return {100, qMax(buttonHeight, fontHeight)};
33}
34
35void InvalidFilterListItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
36{
37 Q_UNUSED(index)
38 painter->save();
39
41
42 if (option.state & QStyle::State_Selected) {
43 painter->setPen(option.palette.highlightedText().color());
44 }
45
46 painter->restore();
47}
48
49QList<QWidget *> InvalidFilterListItemDelegate::createItemWidgets(const QModelIndex &) const
50{
51 auto label = new QLabel();
52 auto showInformationToolButton = new QToolButton();
53 connect(showInformationToolButton, &QAbstractButton::clicked, this, &InvalidFilterListItemDelegate::slotShowDetails);
54 return QList<QWidget *>() << label << showInformationToolButton;
55}
56
57void InvalidFilterListItemDelegate::updateItemWidgets(const QList<QWidget *> &widgets,
58 const QStyleOptionViewItem &option,
59 const QPersistentModelIndex &index) const
60{
61 auto label = static_cast<QLabel *>(widgets[0]);
62 const QAbstractItemModel *model = index.model();
63 label->setText(model->data(index).toString());
64 const QString details = model->data(index, InvalidFilterListModel::InformationRole).toString();
65
66 auto showInformationToolButton = static_cast<QToolButton *>(widgets[1]);
67 showInformationToolButton->setEnabled(!details.isEmpty());
68 const int itemHeight = sizeHint(option, index).height();
69 showInformationToolButton->setIcon(QIcon::fromTheme(QStringLiteral("help-hint")));
70 showInformationToolButton->resize(showInformationToolButton->sizeHint());
71 showInformationToolButton->move(option.rect.right() - showInformationToolButton->width(), (itemHeight - showInformationToolButton->height()) / 2);
72
73 int labelWidth = option.rect.width();
74 labelWidth -= showInformationToolButton->sizeHint().width();
75 label->resize(labelWidth, label->sizeHint().height());
76 label->move(0, (itemHeight - label->height()) / 2);
77}
78
79void InvalidFilterListItemDelegate::slotShowDetails()
80{
81 const QAbstractItemModel *model = focusedIndex().model();
82
83 const QString information = model->data(focusedIndex(), InvalidFilterListModel::InformationRole).toString();
84 if (!information.isEmpty()) {
85 Q_EMIT showDetails(information);
86 }
87}
88
89#include "moc_invalidfilterlistitemdelegate.cpp"
QAbstractItemView * itemView() const
QPersistentModelIndex focusedIndex() const
void information(QWidget *parent, const QString &text, const QString &title=QString(), const QString &dontShowAgainName=QString(), Options options=Notify)
QString label(StandardShortcut id)
The filter dialog.
void clicked(bool checked)
virtual QVariant data(const QModelIndex &index, int role) const const=0
QIcon fromTheme(const QString &name)
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
void restore()
void save()
void setPen(Qt::PenStyle style)
const QAbstractItemModel * model() const const
int height() const const
bool isEmpty() const const
void resize(qsizetype newSize, QChar fillChar)
PE_PanelItemViewItem
virtual void drawPrimitive(PrimitiveElement element, const QStyleOption *option, QPainter *painter, const QWidget *widget) const const=0
virtual int pixelMetric(PixelMetric metric, const QStyleOption *option, const QWidget *widget) const const=0
QString toString() const const
void setEnabled(bool)
QStyle * style() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:14:00 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.