• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdepimlibs-4.11.3 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
36 static 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 
53 class DisplayNameDelegate : public QStyledItemDelegate
54 {
55  public:
56  DisplayNameDelegate( QAbstractItemView *view, QObject *parent = 0 )
57  : QStyledItemDelegate( parent ), mMaxDescriptionWidth( 0 )
58  {
59  mDescriptions.append( i18n( "Short Name" ) );
60  mDescriptions.append( i18n( "Full Name" ) );
61  mDescriptions.append( i18n( "Reverse Name with Comma" ) );
62  mDescriptions.append( i18n( "Reverse Name" ) );
63  mDescriptions.append( i18n( "Organization" ) );
64  mDescriptions.append( i18nc( "@item:inlistbox A custom name format", "Custom" ) );
65 
66  QFont font = view->font();
67  font.setStyle( QFont::StyleItalic );
68  QFontMetrics metrics( font );
69  foreach ( const QString &description, mDescriptions ) {
70  mMaxDescriptionWidth = qMax( mMaxDescriptionWidth, metrics.width( description ) );
71  }
72 
73  mMaxDescriptionWidth += 3;
74  }
75 
76  int maximumDescriptionWidth() const
77  {
78  return mMaxDescriptionWidth;
79  }
80 
81  virtual void paint( QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index ) const
82  {
83  QStyledItemDelegate::paint( painter, option, index );
84  const QRect rect( option.rect.width() - mMaxDescriptionWidth, option.rect.y(), mMaxDescriptionWidth, option.rect.height() );
85  painter->save();
86  QFont font( painter->font() );
87  font.setStyle( QFont::StyleItalic );
88  painter->setFont( font );
89  if ( option.state & QStyle::State_Selected ) {
90  painter->setPen( option.palette.color( QPalette::Normal, QPalette::BrightText ) );
91  } else {
92  painter->setPen( option.palette.color( QPalette::Disabled, QPalette::Text ) );
93  }
94  painter->drawText( rect, Qt::AlignLeft, mDescriptions.at( index.row() ) );
95  painter->restore();
96  }
97 
98  QSize sizeHint( const QStyleOptionViewItem& option, const QModelIndex& index ) const
99  {
100  QSize size = QStyledItemDelegate::sizeHint( option, index );
101  size.setWidth( size.width() + mMaxDescriptionWidth );
102 
103  return size;
104  }
105 
106  private:
107  QStringList mDescriptions;
108  int mMaxDescriptionWidth;
109 };
110 
111 DisplayNameEditWidget::DisplayNameEditWidget( QWidget *parent )
112  : QWidget( parent ),
113  mDisplayType( FullName )
114 {
115  QHBoxLayout *layout = new QHBoxLayout( this );
116  layout->setMargin( 0 );
117  layout->setSpacing( KDialog::spacingHint() );
118 
119  mView = new KComboBox( this );
120  mView->addItems( QStringList() << QString() << QString() << QString()
121  << QString() << QString() << QString() );
122 
123  layout->addWidget( mView );
124 
125  connect( mView, SIGNAL(activated(int)), SLOT(displayTypeChanged(int)) );
126 
127  DisplayNameDelegate *delegate = new DisplayNameDelegate( mView->view() );
128  mView->view()->setItemDelegate( delegate );
129 
130  mAdditionalPopupWidth = delegate->maximumDescriptionWidth();
131 
132  mViewport = mView->view()->viewport();
133  mViewport->installEventFilter( this );
134 }
135 
136 DisplayNameEditWidget::~DisplayNameEditWidget()
137 {
138 }
139 
140 void DisplayNameEditWidget::setReadOnly( bool readOnly )
141 {
142  mView->setEnabled( !readOnly );
143 }
144 
145 void DisplayNameEditWidget::setDisplayType( DisplayType type )
146 {
147  if ( type == -1 ) {
148  // guess the used display type
149  mDisplayType = guessedDisplayType( mContact );
150  } else
151  mDisplayType = type;
152 
153  updateView();
154 }
155 
156 DisplayNameEditWidget::DisplayType DisplayNameEditWidget::displayType() const
157 {
158  return mDisplayType;
159 }
160 
161 void DisplayNameEditWidget::loadContact( const KABC::Addressee &contact )
162 {
163  mContact = contact;
164 
165  mDisplayType = guessedDisplayType( mContact );
166 
167  updateView();
168 }
169 
170 void DisplayNameEditWidget::storeContact( KABC::Addressee &contact ) const
171 {
172  contact.setFormattedName( mView->currentText() );
173 }
174 
175 void DisplayNameEditWidget::changeName( const KABC::Addressee &contact )
176 {
177  const QString organization = mContact.organization();
178  mContact = contact;
179  mContact.setOrganization( organization );
180  if ( mDisplayType == CustomName ) {
181  mContact.setFormattedName( mView->currentText() );
182  }
183 
184  updateView();
185 }
186 
187 void DisplayNameEditWidget::changeOrganization( const QString &organization )
188 {
189  mContact.setOrganization( organization );
190 
191  updateView();
192 }
193 
194 void DisplayNameEditWidget::displayTypeChanged( int type )
195 {
196  mDisplayType = (DisplayType)type;
197 
198  updateView();
199 }
200 
201 bool DisplayNameEditWidget::eventFilter( QObject *object, QEvent *event )
202 {
203  if ( object == mViewport ) {
204  if ( event->type() == QEvent::Show ) {
205  // retrieve the widget that contains the popup view
206  QWidget *parentWidget = mViewport->parentWidget()->parentWidget();
207 
208  int maxWidth = 0;
209  QFontMetrics metrics( mView->font() );
210  for ( int i = 0; i < mView->count(); ++i ) {
211  maxWidth = qMax( maxWidth, metrics.width( mView->itemText( i ) ) );
212  }
213 
214  // resize it to show the complete content
215  parentWidget->resize( maxWidth + mAdditionalPopupWidth + 20, parentWidget->height() );
216  }
217  return false;
218  }
219 
220  return QWidget::eventFilter( object, event );
221 }
222 
223 void DisplayNameEditWidget::updateView()
224 {
225  // SimpleName:
226  mView->setItemText( 0, mContact.givenName() + QLatin1Char( ' ' ) + mContact.familyName() );
227 
228  // FullName:
229  mView->setItemText( 1, mContact.assembledName() );
230 
231  // ReverseNameWithComma:
232  mView->setItemText( 2, mContact.familyName() + QLatin1String( ", " ) + mContact.givenName() );
233 
234  // ReverseName:
235  mView->setItemText( 3, mContact.familyName() + QLatin1Char( ' ' ) + mContact.givenName() );
236 
237  // Organization:
238  mView->setItemText( 4, mContact.organization() );
239 
240  // CustomName:
241  mView->setItemText( 5, mContact.formattedName() );
242 
243  // delay the state change here, since we might have been called from mView via a signal
244  QMetaObject::invokeMethod( this, "setComboBoxEditable", Qt::QueuedConnection, Q_ARG( bool, mDisplayType == CustomName ) );
245 
246  mView->setCurrentIndex( (int)mDisplayType );
247 }
248 
249 void DisplayNameEditWidget::setComboBoxEditable( bool value )
250 {
251  mView->setEditable( value );
252 }
253 
DisplayNameEditWidget::Organization
The organization name.
Definition: displaynameeditwidget.h:50
DisplayNameEditWidget::CustomName
Let the user input a display name.
Definition: displaynameeditwidget.h:51
DisplayNameEditWidget::FullName
A name of the form: prefix givenName additionalName familyName suffix.
Definition: displaynameeditwidget.h:47
DisplayNameEditWidget::ReverseName
A name of the form: familyName givenName.
Definition: displaynameeditwidget.h:49
DisplayNameEditWidget::ReverseNameWithComma
A name of the form: familyName, givenName.
Definition: displaynameeditwidget.h:48
DisplayNameEditWidget::DisplayType
DisplayType
Describes what the display name should look like.
Definition: displaynameeditwidget.h:45
DisplayNameEditWidget::SimpleName
A name of the form: givenName familyName.
Definition: displaynameeditwidget.h:46
This file is part of the KDE documentation.
Documentation copyright © 1996-2013 The KDE developers.
Generated on Tue Nov 26 2013 09:03:47 by doxygen 1.8.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.11.3 API Reference

Skip menu "kdepimlibs-4.11.3 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