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

KPeople

  • frameworks
  • kpeople
  • examples
contactlistwidgets.cpp
1 /*
2  Persons Model Example
3  Copyright (C) 2012 Aleix Pol Gonzalez <aleixpol@blue-systems.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 <QApplication>
21 #include <QTreeView>
22 #include <QSortFilterProxyModel>
23 
24 #include <QVBoxLayout>
25 #include <QPushButton>
26 
27 #include <QStyledItemDelegate>
28 #include <QPainter>
29 
30 #include <personsmodel.h>
31 
32 using namespace KPeople;
33 
34 const int SPACING = 8;
35 const int PHOTO_SIZE = 32;
36 
37 class PersonsDelegate : public QStyledItemDelegate
38 {
39 public:
40  PersonsDelegate(QObject *parent = 0);
41  ~PersonsDelegate();
42 
43  void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const;
44  QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const;
45 };
46 
47 PersonsDelegate::PersonsDelegate(QObject *parent)
48  : QStyledItemDelegate(parent)
49 {
50 }
51 
52 PersonsDelegate::~PersonsDelegate()
53 {
54 }
55 
56 void PersonsDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
57 {
58  QStyleOptionViewItemV4 optV4 = option;
59  initStyleOption(&optV4, index);
60 
61  painter->save();
62 
63  painter->setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform | QPainter::HighQualityAntialiasing);
64  painter->setClipRect(optV4.rect);
65 
66  QStyle *style = QApplication::style();
67  style->drawPrimitive(QStyle::PE_PanelItemViewItem, &option, painter);
68 
69  QRect contactPhotoRect = optV4.rect;
70  contactPhotoRect.adjust(SPACING, SPACING, SPACING, SPACING);
71  contactPhotoRect.setWidth(PHOTO_SIZE);
72  contactPhotoRect.setHeight(PHOTO_SIZE);
73 
74  QImage avatar = index.data(Qt::DecorationRole).value<QImage>();
75  painter->drawImage(contactPhotoRect, avatar);
76 
77  painter->drawRect(contactPhotoRect);
78 
79  QRect nameRect = optV4.rect;
80  nameRect.adjust(SPACING + PHOTO_SIZE + SPACING, SPACING, 0, 0);
81 
82  painter->drawText(nameRect, index.data(Qt::DisplayRole).toString());
83 
84  QRect idRect = optV4.rect;
85  idRect.adjust(SPACING + PHOTO_SIZE + SPACING, SPACING + 15, 0, 0);
86  painter->drawText(idRect, index.data(PersonsModel::PersonUriRole).toString());
87 
88  painter->restore();
89 }
90 
91 QSize PersonsDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
92 {
93  Q_UNUSED(option)
94  Q_UNUSED(index)
95  return QSize(128, 48);
96 }
97 
98 class ContactListApp : public QWidget
99 {
100  Q_OBJECT
101 public:
102  ContactListApp();
103 private Q_SLOTS:
104  void onMergeClicked();
105  void onUnmergeClicked();
106 private:
107  PersonsModel *m_model;
108  QTreeView *m_view;
109 };
110 
111 ContactListApp::ContactListApp()
112 {
113  m_view = new QTreeView(this);
114  m_model = new PersonsModel(this);
115 
116  QVBoxLayout *layout = new QVBoxLayout(this);
117  QSortFilterProxyModel *sortFilter = new QSortFilterProxyModel(m_view);
118  sortFilter->setDynamicSortFilter(true);
119  sortFilter->setSourceModel(m_model);
120  sortFilter->sort(0);
121  m_view->setRootIsDecorated(false);
122  m_view->setModel(sortFilter);
123  m_view->setItemDelegate(new PersonsDelegate(this));
124  m_view->setSelectionMode(QAbstractItemView::ExtendedSelection);
125 
126  layout->addWidget(m_view);
127  QPushButton *mergeButton = new QPushButton(QStringLiteral("Merge"), this);
128  connect(mergeButton, SIGNAL(released()), SLOT(onMergeClicked()));
129  layout->addWidget(mergeButton);
130 
131  QPushButton *unmergeButton = new QPushButton(QStringLiteral("Unmerge"), this);
132  connect(unmergeButton, SIGNAL(released()), SLOT(onUnmergeClicked()));
133  layout->addWidget(unmergeButton);
134 }
135 
136 void ContactListApp::onMergeClicked()
137 {
138  QModelIndexList indexes = m_view->selectionModel()->selectedIndexes();
139  QStringList ids;
140  Q_FOREACH (const QModelIndex &index, indexes) {
141  ids << index.data(PersonsModel::PersonUriRole).toString();
142  }
143 
144  if (!ids.isEmpty()) {
145  KPeople::mergeContacts(ids);
146  }
147 }
148 
149 void ContactListApp::onUnmergeClicked()
150 {
151  QModelIndexList indexes = m_view->selectionModel()->selectedIndexes();
152  if (indexes.size()) {
153  QString id = indexes.first().data(PersonsModel::PersonUriRole).toString();
154  KPeople::unmergeContact(id);
155  }
156 }
157 
158 int main(int argc, char **argv)
159 {
160  QApplication app(argc, argv);
161 
162  ContactListApp widget;
163  widget.show();
164  app.exec();
165 }
166 
167 #include "contactlistwidgets.moc"
KPeople::mergeContacts
KPEOPLE_EXPORT QString mergeContacts(const QStringList &uris)
Merge all uris into a single person.
Definition: global.cpp:28
KPeople
The KPeople namespace contains all the classes for Libkpeople.
KPeople::PersonsModel
This class creates a model of all known contacts from all sources Contacts are represented as a tree ...
Definition: personsmodel.h:42
KPeople::unmergeContact
KPEOPLE_EXPORT bool unmergeContact(const QString &uri)
Unmerge a contact.
Definition: global.cpp:33
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