• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdepimlibs-4.14.10 API Reference
  • KDE Home
  • Contact Us
 

akonadi/contact

  • akonadi
  • contact
  • editor
displaynameeditwidget.cpp
1/*
2 This file is part of Akonadi Contact.
3
4 Copyright (c) 2009 Tobias Koenig <tokoe@kde.org>
5
6 This library is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Library General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or (at your
9 option) any later version.
10
11 This library is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
14 License for more details.
15
16 You should have received a copy of the GNU Library General Public License
17 along with this library; see the file COPYING.LIB. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301, USA.
20*/
21
22#include "displaynameeditwidget.h"
23
24#include <QtCore/QEvent>
25#include <QAbstractItemView>
26#include <QHBoxLayout>
27#include <QPainter>
28#include <QStyledItemDelegate>
29
30#include <kabc/addressee.h>
31#include <kcombobox.h>
32#include <kdialog.h>
33#include <klocalizedstring.h>
34
35// Tries to guess the display type that is used for the passed contact
36static DisplayNameEditWidget::DisplayType guessedDisplayType(const KABC::Addressee &contact)
37{
38 if (contact.formattedName() == (contact.givenName() + QLatin1Char(' ') + contact.familyName())) {
39 return DisplayNameEditWidget::SimpleName;
40 } else if (contact.formattedName() == contact.assembledName()) {
41 return DisplayNameEditWidget::FullName;
42 } else if (contact.formattedName() == (contact.familyName() + QLatin1String(", ") + contact.givenName())) {
43 return DisplayNameEditWidget::ReverseNameWithComma;
44 } else if (contact.formattedName() == (contact.familyName() + QLatin1Char(' ') + contact.givenName())) {
45 return DisplayNameEditWidget::ReverseName;
46 } else if (contact.formattedName() == contact.organization()) {
47 return DisplayNameEditWidget::Organization;
48 } else {
49 return DisplayNameEditWidget::CustomName;
50 }
51}
52
53class DisplayNameDelegate : public QStyledItemDelegate
54{
55public:
56 DisplayNameDelegate(QAbstractItemView *view, QObject *parent = 0)
57 : QStyledItemDelegate(parent)
58 , mMaxDescriptionWidth(0)
59 {
60 mDescriptions.append(i18n("Short Name"));
61 mDescriptions.append(i18n("Full Name"));
62 mDescriptions.append(i18n("Reverse Name with Comma"));
63 mDescriptions.append(i18n("Reverse Name"));
64 mDescriptions.append(i18n("Organization"));
65 mDescriptions.append(i18nc("@item:inlistbox A custom name format", "Custom"));
66
67 QFont font = view->font();
68 font.setStyle(QFont::StyleItalic);
69 QFontMetrics metrics(font);
70 foreach (const QString &description, mDescriptions) {
71 mMaxDescriptionWidth = qMax(mMaxDescriptionWidth, metrics.width(description));
72 }
73
74 mMaxDescriptionWidth += 3;
75 }
76
77 int maximumDescriptionWidth() const
78 {
79 return mMaxDescriptionWidth;
80 }
81
82 virtual void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
83 {
84 QStyledItemDelegate::paint(painter, option, index);
85 const QRect rect(option.rect.width() - mMaxDescriptionWidth, option.rect.y(), mMaxDescriptionWidth, option.rect.height());
86 painter->save();
87 QFont font(painter->font());
88 font.setStyle(QFont::StyleItalic);
89 painter->setFont(font);
90 if (option.state & QStyle::State_Selected) {
91 painter->setPen(option.palette.color(QPalette::Normal, QPalette::BrightText));
92 } else {
93 painter->setPen(option.palette.color(QPalette::Disabled, QPalette::Text));
94 }
95 painter->drawText(rect, Qt::AlignLeft, mDescriptions.at(index.row()));
96 painter->restore();
97 }
98
99 QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
100 {
101 QSize size = QStyledItemDelegate::sizeHint(option, index);
102 size.setWidth(size.width() + mMaxDescriptionWidth);
103
104 return size;
105 }
106
107private:
108 QStringList mDescriptions;
109 int mMaxDescriptionWidth;
110};
111
112DisplayNameEditWidget::DisplayNameEditWidget(QWidget *parent)
113 : QWidget(parent)
114 , mDisplayType(FullName)
115{
116 QHBoxLayout *layout = new QHBoxLayout(this);
117 layout->setMargin(0);
118 layout->setSpacing(KDialog::spacingHint());
119
120 mView = new KComboBox(this);
121 mView->addItems(QStringList() << QString() << QString() << QString()
122 << QString() << QString() << QString());
123
124 layout->addWidget(mView);
125 setFocusProxy(mView);
126 setFocusPolicy(Qt::StrongFocus);
127 connect(mView, SIGNAL(activated(int)), SLOT(displayTypeChanged(int)));
128
129 DisplayNameDelegate *delegate = new DisplayNameDelegate(mView->view());
130 mView->view()->setItemDelegate(delegate);
131
132 mAdditionalPopupWidth = delegate->maximumDescriptionWidth();
133
134 mViewport = mView->view()->viewport();
135 mViewport->installEventFilter(this);
136}
137
138DisplayNameEditWidget::~DisplayNameEditWidget()
139{
140}
141
142void DisplayNameEditWidget::setReadOnly(bool readOnly)
143{
144 mView->setEnabled(!readOnly);
145}
146
147void DisplayNameEditWidget::setDisplayType(DisplayType type)
148{
149 if (type == -1) {
150 // guess the used display type
151 mDisplayType = guessedDisplayType(mContact);
152 } else {
153 mDisplayType = type;
154 }
155
156 updateView();
157}
158
159DisplayNameEditWidget::DisplayType DisplayNameEditWidget::displayType() const
160{
161 return mDisplayType;
162}
163
164void DisplayNameEditWidget::loadContact(const KABC::Addressee &contact)
165{
166 mContact = contact;
167
168 mDisplayType = guessedDisplayType(mContact);
169
170 updateView();
171}
172
173void DisplayNameEditWidget::storeContact(KABC::Addressee &contact) const
174{
175 contact.setFormattedName(mView->currentText());
176}
177
178void DisplayNameEditWidget::changeName(const KABC::Addressee &contact)
179{
180 const QString organization = mContact.organization();
181 mContact = contact;
182 mContact.setOrganization(organization);
183 if (mDisplayType == CustomName) {
184 mContact.setFormattedName(mView->currentText());
185 }
186
187 updateView();
188}
189
190void DisplayNameEditWidget::changeOrganization(const QString &organization)
191{
192 mContact.setOrganization(organization);
193
194 updateView();
195}
196
197void DisplayNameEditWidget::displayTypeChanged(int type)
198{
199 mDisplayType = (DisplayType)type;
200
201 updateView();
202}
203
204bool DisplayNameEditWidget::eventFilter(QObject *object, QEvent *event)
205{
206 if (object == mViewport) {
207 if (event->type() == QEvent::Show) {
208 // retrieve the widget that contains the popup view
209 QWidget *parentWidget = mViewport->parentWidget()->parentWidget();
210
211 int maxWidth = 0;
212 QFontMetrics metrics(mView->font());
213 for (int i = 0; i < mView->count(); ++i) {
214 maxWidth = qMax(maxWidth, metrics.width(mView->itemText(i)));
215 }
216
217 // resize it to show the complete content
218 parentWidget->resize(maxWidth + mAdditionalPopupWidth + 20, parentWidget->height());
219 }
220 return false;
221 }
222
223 return QWidget::eventFilter(object, event);
224}
225
226void DisplayNameEditWidget::updateView()
227{
228 // SimpleName:
229 mView->setItemText(0, mContact.givenName() + QLatin1Char(' ') + mContact.familyName());
230
231 // FullName:
232 mView->setItemText(1, mContact.assembledName());
233
234 // ReverseNameWithComma:
235 mView->setItemText(2, mContact.familyName() + QLatin1String(", ") + mContact.givenName());
236
237 // ReverseName:
238 mView->setItemText(3, mContact.familyName() + QLatin1Char(' ') + mContact.givenName());
239
240 // Organization:
241 mView->setItemText(4, mContact.organization());
242
243 // CustomName:
244 mView->setItemText(5, mContact.formattedName());
245
246 // delay the state change here, since we might have been called from mView via a signal
247 QMetaObject::invokeMethod(this, "setComboBoxEditable", Qt::QueuedConnection, Q_ARG(bool, mDisplayType == CustomName));
248
249 mView->setCurrentIndex((int)mDisplayType);
250}
251
252void DisplayNameEditWidget::setComboBoxEditable(bool value)
253{
254 mView->setEditable(value);
255}
DisplayNameEditWidget::DisplayType
DisplayType
Describes what the display name should look like.
Definition: displaynameeditwidget.h:45
DisplayNameEditWidget::Organization
@ Organization
The organization name.
Definition: displaynameeditwidget.h:50
DisplayNameEditWidget::ReverseName
@ ReverseName
A name of the form: familyName givenName.
Definition: displaynameeditwidget.h:49
DisplayNameEditWidget::SimpleName
@ SimpleName
A name of the form: givenName familyName.
Definition: displaynameeditwidget.h:46
DisplayNameEditWidget::ReverseNameWithComma
@ ReverseNameWithComma
A name of the form: familyName, givenName.
Definition: displaynameeditwidget.h:48
DisplayNameEditWidget::FullName
@ FullName
A name of the form: prefix givenName additionalName familyName suffix.
Definition: displaynameeditwidget.h:47
DisplayNameEditWidget::CustomName
@ CustomName
Let the user input a display name.
Definition: displaynameeditwidget.h:51
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Thu Jul 21 2022 00:00:00 by doxygen 1.9.5 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

akonadi/contact

Skip menu "akonadi/contact"
  • Main Page
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • Related Pages

kdepimlibs-4.14.10 API Reference

Skip menu "kdepimlibs-4.14.10 API Reference"
  • akonadi
  •   contact
  •   kmime
  •   socialutils
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • microblog
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
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