• Skip to content
  • Skip to link menu
KDE API Documentation - mergedelegate.cpp Source File (KPeople)
  • KDE Home
  • Contact Us
 

KPeople

  • frameworks
  • kpeople
  • src
  • widgets
mergedelegate.cpp
1 /*
2  KPeople - Duplicates
3  Copyright (C) 2013 Franck Arrecot <franck.arrecot@gmail.com>
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Lesser General Public
7  License as published by the Free Software Foundation; either
8  version 2.1 of the License, or (at your option) any later version.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public
16  License along with this library; if not, write to the Free Software
17  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19 
20 #include "mergedelegate.h"
21 #include "personsmodel.h"
22 #include "mergedialog.h"
23 #include <match_p.h>
24 
25 #include <QPainter>
26 #include <QPixmap>
27 #include <QStyleOptionViewItem>
28 #include <QAbstractItemView>
29 #include <QModelIndex>
30 #include <QApplication>
31 #include <QLabel>
32 #include <QCheckBox>
33 #include <QItemSelection>
34 #include <QDebug>
35 #include <QIcon>
36 #include <KLocalizedString>
37 
38 #define MAX_MATCHING_CONTACTS_ICON 5
39 #define SIZE_STANDARD_PIXMAP 35
40 
41 using namespace KPeople;
42 
43 //TODO: use proper, runtime values there
44 QSize MergeDelegate::s_decorationSize(SIZE_STANDARD_PIXMAP, SIZE_STANDARD_PIXMAP);
45 QSize MergeDelegate::s_arrowSize(15, 15);
46 
47 QSize MergeDelegate::pictureSize()
48 {
49  return s_decorationSize;
50 }
51 
52 MergeDelegate::MergeDelegate(QAbstractItemView *parent)
53  : KExtendableItemDelegate(parent)
54 {
55  static QIcon arrowD = QIcon::fromTheme(QStringLiteral("arrow-down"));
56  setContractPixmap(arrowD.pixmap(s_arrowSize));
57 
58  static QIcon arrowR = QIcon::fromTheme(QStringLiteral("arrow-right"));
59  setExtendPixmap(arrowR.pixmap(s_arrowSize));
60 }
61 
62 MergeDelegate::~MergeDelegate()
63 {}
64 
65 void MergeDelegate::onClickContactParent(const QModelIndex &parent)
66 {
67  if (isExtended(parent)) {
68  contractItem(parent);
69  } else {
70  QItemSelection item = QItemSelection(parent, parent);
71  onSelectedContactsChanged(item, QItemSelection());
72  }
73 }
74 
75 void MergeDelegate::onSelectedContactsChanged(const QItemSelection &now , const QItemSelection &old)
76 {
77  if (old.indexes().size()) {
78  QModelIndex oldIdx = old.indexes().first();
79 
80  if (isExtended(oldIdx)) {
81  contractItem(oldIdx);
82  }
83  }
84  if (now.indexes().size()) {
85  QModelIndex idx = now.indexes().first();
86  extendItem(buildMultipleLineLabel(idx), idx);
87  }
88 }
89 
90 QWidget *MergeDelegate::buildMultipleLineLabel(const QModelIndex &idx)
91 {
92  QString contents;
93  int rows = idx.model()->rowCount(idx);
94  for (int i = 0 ; i < rows; ++i) {
95  QModelIndex child = idx.child(i, 0);
96  Match m = child.data(MergeDialog::MergeReasonRole).value<Match>();
97 
98  QString name = m.indexB.data(Qt::DisplayRole).toString();
99  QString display = i18nc("name: merge reasons", "%1: %2", name, m.matchReasons().join(i18nc("reasons join", ", ")));
100  contents += display + QLatin1String("<p/>");
101  }
102  QLabel *childDisplay = new QLabel(contents, dynamic_cast<QWidget *>(parent()));
103  childDisplay->setAlignment(Qt::AlignRight);
104  childDisplay->setFrameStyle(QFrame::StyledPanel | QFrame::Sunken);
105  return childDisplay;
106 }
107 
108 void MergeDelegate::paint(QPainter *painter, const QStyleOptionViewItem &optionOld, const QModelIndex &index) const
109 {
110  QStyleOptionViewItem option(optionOld);
111  QStyleOptionViewItemV4 opt(option);
112  KExtendableItemDelegate::paint(painter, option, index);
113 
114  const int separation = 5;
115 
116  int facesRows = qMin(index.model()->rowCount(index), MAX_MATCHING_CONTACTS_ICON);
117  for (int i = 0; i < facesRows; i++) { // Children Icon Displaying Loop
118  const QModelIndex child = index.child(i, 0);
119 
120  QVariant decoration = child.data(Qt::DecorationRole);
121  Q_ASSERT(decoration.type() == (QVariant::Icon));
122 
123  QIcon pix = decoration.value<QIcon>();
124  QPoint pixmapPoint = {option.rect.width() / 2 + i *(s_decorationSize.width() + separation), option.rect.top()};
125  painter->drawPixmap(pixmapPoint, pix.pixmap(s_decorationSize));
126  }
127  // draw a vertical line to separate the original person and the merging contacts
128  int midWidth = option.rect.width() / 2;
129  painter->setPen(opt.palette.color(QPalette::Background));
130  painter->drawLine(option.rect.left() + midWidth - SIZE_STANDARD_PIXMAP, option.rect.bottom() - 5,
131  option.rect.left() + midWidth - SIZE_STANDARD_PIXMAP, option.rect.top() + 5);
132 }
KPeople
The KPeople namespace contains all the classes for Libkpeople.
This file is part of the KDE documentation.
Documentation copyright © 1996-2015 The KDE developers.
Generated on Fri Feb 13 2015 15:16:39 by doxygen 1.8.9.1 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KPeople

Skip menu "KPeople"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • File List

Class Picker

Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal