KPeople

mergedelegate.cpp
1 /*
2  KPeople - Duplicates
3  SPDX-FileCopyrightText: 2013 Franck Arrecot <[email protected]>
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 
26 using namespace KPeople;
27 
28 // TODO: use proper, runtime values there
29 QSize MergeDelegate::s_decorationSize(SIZE_STANDARD_PIXMAP, SIZE_STANDARD_PIXMAP);
30 QSize MergeDelegate::s_arrowSize(15, 15);
31 
32 QSize MergeDelegate::pictureSize()
33 {
34  return s_decorationSize;
35 }
36 
37 MergeDelegate::MergeDelegate(QAbstractItemView *parent)
38  : KExtendableItemDelegate(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 
47 MergeDelegate::~MergeDelegate()
48 {
49 }
50 
51 void MergeDelegate::onClickContactParent(const QModelIndex &parent)
52 {
53  if (isExtended(parent)) {
54  contractItem(parent);
55  } else {
56  QItemSelection item = QItemSelection(parent, parent);
57  onSelectedContactsChanged(item, QItemSelection());
58  }
59 }
60 
61 void 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 
76 QWidget *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 
95 void 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.type() == (QVariant::Icon));
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 }
AlignRight
void setPen(const QColor &color)
DisplayRole
virtual int rowCount(const QModelIndex &parent) const const=0
void drawPixmap(const QRectF &target, const QPixmap &pixmap, const QRectF &source)
T value() const const
QIcon fromTheme(const QString &name)
void setFrameStyle(int style)
QVariant data(int role) const const
QModelIndexList indexes() const const
QVariant::Type type() const const
void setAlignment(Qt::Alignment)
QPixmap pixmap(const QSize &size, QIcon::Mode mode, QIcon::State state) const const
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override
QString name(StandardShortcut id)
void drawLine(const QLineF &line)
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const const=0
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QChar * data()
const QAbstractItemModel * model() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Sun Aug 14 2022 04:09:08 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.