KPeople

mergedelegate.cpp
1/*
2 KPeople - Duplicates
3 SPDX-FileCopyrightText: 2013 Franck Arrecot <franck.arrecot@gmail.com>
4
5 SPDX-License-Identifier: LGPL-2.1-or-later
6*/
7
8#include "mergedelegate.h"
9#include "mergedialog.h"
10#include "personsmodel.h"
11#include <match_p.h>
12
13#include "kpeople_widgets_debug.h"
14#include <KLocalizedString>
15#include <QAbstractItemView>
16#include <QCheckBox>
17#include <QIcon>
18#include <QLabel>
19#include <QModelIndex>
20#include <QPainter>
21#include <QStyleOptionViewItem>
22
23#define MAX_MATCHING_CONTACTS_ICON 5
24#define SIZE_STANDARD_PIXMAP 35
25
26using namespace KPeople;
27
28// TODO: use proper, runtime values there
29QSize MergeDelegate::s_decorationSize(SIZE_STANDARD_PIXMAP, SIZE_STANDARD_PIXMAP);
30QSize MergeDelegate::s_arrowSize(15, 15);
31
32QSize MergeDelegate::pictureSize()
33{
34 return s_decorationSize;
35}
36
37MergeDelegate::MergeDelegate(QAbstractItemView *parent)
39{
40 static QIcon arrowD = QIcon::fromTheme(QStringLiteral("arrow-down"));
41 setContractPixmap(arrowD.pixmap(s_arrowSize));
42
43 static QIcon arrowR = QIcon::fromTheme(QStringLiteral("arrow-right"));
44 setExtendPixmap(arrowR.pixmap(s_arrowSize));
45}
46
47MergeDelegate::~MergeDelegate()
48{
49}
50
51void MergeDelegate::onClickContactParent(const QModelIndex &parent)
52{
53 if (isExtended(parent)) {
55 } else {
57 onSelectedContactsChanged(item, QItemSelection());
58 }
59}
60
61void MergeDelegate::onSelectedContactsChanged(const QItemSelection &now, const QItemSelection &old)
62{
63 if (!old.indexes().isEmpty()) {
64 QModelIndex oldIdx = old.indexes().first();
65
66 if (isExtended(oldIdx)) {
67 contractItem(oldIdx);
68 }
69 }
70 if (!now.indexes().isEmpty()) {
71 QModelIndex idx = now.indexes().first();
72 extendItem(buildMultipleLineLabel(idx), idx);
73 }
74}
75
76QWidget *MergeDelegate::buildMultipleLineLabel(const QModelIndex &idx)
77{
78 QString contents;
79 const QAbstractItemModel *model = idx.model();
80 const int rows = model->rowCount(idx);
81 for (int i = 0; i < rows; ++i) {
82 const QModelIndex child = model->index(i, 0, idx);
83 Match m = child.data(MergeDialog::MergeReasonRole).value<Match>();
84
85 QString name = m.indexB.data(Qt::DisplayRole).toString();
86 QString display = i18nc("name: merge reasons", "%1: %2", name, m.matchReasons().join(i18nc("reasons join", ", ")));
87 contents += display + QLatin1String("<p/>");
88 }
89 QLabel *childDisplay = new QLabel(contents, dynamic_cast<QWidget *>(parent()));
90 childDisplay->setAlignment(Qt::AlignRight);
92 return childDisplay;
93}
94
95void MergeDelegate::paint(QPainter *painter, const QStyleOptionViewItem &optionOld, const QModelIndex &index) const
96{
97 QStyleOptionViewItem option(optionOld);
98 QStyleOptionViewItem opt(option);
99 KExtendableItemDelegate::paint(painter, option, index);
100
101 const int separation = 5;
102
103 const QAbstractItemModel *model = index.model();
104 int facesRows = qMin(model->rowCount(index), MAX_MATCHING_CONTACTS_ICON);
105 for (int i = 0; i < facesRows; i++) { // Children Icon Displaying Loop
106 const QModelIndex child = model->index(i, 0, index);
107
108 QVariant decoration = child.data(Qt::DecorationRole);
109 Q_ASSERT(decoration.userType() == QMetaType::QIcon);
110
111 QIcon pix = decoration.value<QIcon>();
112 QPoint pixmapPoint = {option.rect.width() / 2 + i * (s_decorationSize.width() + separation), option.rect.top()};
113 painter->drawPixmap(pixmapPoint, pix.pixmap(s_decorationSize));
114 }
115 // draw a vertical line to separate the original person and the merging contacts
116 int midWidth = option.rect.width() / 2;
117 painter->setPen(opt.palette.color(QPalette::Window));
118 painter->drawLine(option.rect.left() + midWidth - SIZE_STANDARD_PIXMAP,
119 option.rect.bottom() - 5,
120 option.rect.left() + midWidth - SIZE_STANDARD_PIXMAP,
121 option.rect.top() + 5);
122}
123
124#include "moc_mergedelegate.cpp"
void contractItem(const QModelIndex &index)
bool isExtended(const QModelIndex &index) const
void extendItem(QWidget *extender, const QModelIndex &index)
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString name(StandardShortcut id)
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const const=0
virtual int rowCount(const QModelIndex &parent) const const=0
void setFrameStyle(int style)
QPixmap pixmap(QWindow *window, const QSize &size, Mode mode, State state) const const
QIcon fromTheme(const QString &name)
QModelIndexList indexes() const const
void setAlignment(Qt::Alignment)
QVariant data(int role) const const
const QAbstractItemModel * model() const const
QObject * parent() const const
void drawLine(const QLine &line)
void drawPixmap(const QPoint &point, const QPixmap &pixmap)
void setPen(Qt::PenStyle style)
int width() const const
QChar * data()
AlignRight
DisplayRole
int userType() const const
T value() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:17:46 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.