Mailcommon

invalidfilterlistitemdelegate.cpp
1 /*
2  SPDX-FileCopyrightText: 2015-2022 Laurent Montel <[email protected]>
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 
16 using namespace MailCommon;
17 
18 InvalidFilterListItemDelegate::InvalidFilterListItemDelegate(QAbstractItemView *itemView, QObject *parent)
19  : KWidgetItemDelegate(itemView, parent)
20 {
21 }
22 
23 InvalidFilterListItemDelegate::~InvalidFilterListItemDelegate() = default;
24 
25 QSize 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 
35 void InvalidFilterListItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
36 {
37  Q_UNUSED(index)
38  painter->save();
39 
40  itemView()->style()->drawPrimitive(QStyle::PE_PanelItemViewItem, &option, painter);
41 
42  if (option.state & QStyle::State_Selected) {
43  painter->setPen(option.palette.highlightedText().color());
44  }
45 
46  painter->restore();
47 }
48 
49 QList<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 
57 void 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 
79 void 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 }
void setPen(const QColor &color)
const QAbstractItemModel * model() const const
virtual QVariant data(const QModelIndex &index, int role) const const=0
void clicked(bool checked)
QIcon fromTheme(const QString &name)
void resize(int size)
virtual int pixelMetric(QStyle::PixelMetric metric, const QStyleOption *option, const QWidget *widget) const const=0
bool isEmpty() const const
void setEnabled(bool)
QString label(StandardShortcut id)
void restore()
void save()
void information(QWidget *parent, const QString &text, const QString &title=QString(), const QString &dontShowAgainName=QString(), Options options=Notify)
PE_PanelItemViewItem
The filter dialog.
QString toString() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Sat Sep 24 2022 03:58:15 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.