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

akonadi

  • sources
  • kde-4.12
  • kdepimlibs
  • 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  setFocusProxy( mView );
125  setFocusPolicy( Qt::StrongFocus );
126  connect( mView, SIGNAL(activated(int)), SLOT(displayTypeChanged(int)) );
127 
128  DisplayNameDelegate *delegate = new DisplayNameDelegate( mView->view() );
129  mView->view()->setItemDelegate( delegate );
130 
131  mAdditionalPopupWidth = delegate->maximumDescriptionWidth();
132 
133  mViewport = mView->view()->viewport();
134  mViewport->installEventFilter( this );
135 }
136 
137 DisplayNameEditWidget::~DisplayNameEditWidget()
138 {
139 }
140 
141 void DisplayNameEditWidget::setReadOnly( bool readOnly )
142 {
143  mView->setEnabled( !readOnly );
144 }
145 
146 void DisplayNameEditWidget::setDisplayType( DisplayType type )
147 {
148  if ( type == -1 ) {
149  // guess the used display type
150  mDisplayType = guessedDisplayType( mContact );
151  } else
152  mDisplayType = type;
153 
154  updateView();
155 }
156 
157 DisplayNameEditWidget::DisplayType DisplayNameEditWidget::displayType() const
158 {
159  return mDisplayType;
160 }
161 
162 void DisplayNameEditWidget::loadContact( const KABC::Addressee &contact )
163 {
164  mContact = contact;
165 
166  mDisplayType = guessedDisplayType( mContact );
167 
168  updateView();
169 }
170 
171 void DisplayNameEditWidget::storeContact( KABC::Addressee &contact ) const
172 {
173  contact.setFormattedName( mView->currentText() );
174 }
175 
176 void DisplayNameEditWidget::changeName( const KABC::Addressee &contact )
177 {
178  const QString organization = mContact.organization();
179  mContact = contact;
180  mContact.setOrganization( organization );
181  if ( mDisplayType == CustomName ) {
182  mContact.setFormattedName( mView->currentText() );
183  }
184 
185  updateView();
186 }
187 
188 void DisplayNameEditWidget::changeOrganization( const QString &organization )
189 {
190  mContact.setOrganization( organization );
191 
192  updateView();
193 }
194 
195 void DisplayNameEditWidget::displayTypeChanged( int type )
196 {
197  mDisplayType = (DisplayType)type;
198 
199  updateView();
200 }
201 
202 bool DisplayNameEditWidget::eventFilter( QObject *object, QEvent *event )
203 {
204  if ( object == mViewport ) {
205  if ( event->type() == QEvent::Show ) {
206  // retrieve the widget that contains the popup view
207  QWidget *parentWidget = mViewport->parentWidget()->parentWidget();
208 
209  int maxWidth = 0;
210  QFontMetrics metrics( mView->font() );
211  for ( int i = 0; i < mView->count(); ++i ) {
212  maxWidth = qMax( maxWidth, metrics.width( mView->itemText( i ) ) );
213  }
214 
215  // resize it to show the complete content
216  parentWidget->resize( maxWidth + mAdditionalPopupWidth + 20, parentWidget->height() );
217  }
218  return false;
219  }
220 
221  return QWidget::eventFilter( object, event );
222 }
223 
224 void DisplayNameEditWidget::updateView()
225 {
226  // SimpleName:
227  mView->setItemText( 0, mContact.givenName() + QLatin1Char( ' ' ) + mContact.familyName() );
228 
229  // FullName:
230  mView->setItemText( 1, mContact.assembledName() );
231 
232  // ReverseNameWithComma:
233  mView->setItemText( 2, mContact.familyName() + QLatin1String( ", " ) + mContact.givenName() );
234 
235  // ReverseName:
236  mView->setItemText( 3, mContact.familyName() + QLatin1Char( ' ' ) + mContact.givenName() );
237 
238  // Organization:
239  mView->setItemText( 4, mContact.organization() );
240 
241  // CustomName:
242  mView->setItemText( 5, mContact.formattedName() );
243 
244  // delay the state change here, since we might have been called from mView via a signal
245  QMetaObject::invokeMethod( this, "setComboBoxEditable", Qt::QueuedConnection, Q_ARG( bool, mDisplayType == CustomName ) );
246 
247  mView->setCurrentIndex( (int)mDisplayType );
248 }
249 
250 void DisplayNameEditWidget::setComboBoxEditable( bool value )
251 {
252  mView->setEditable( value );
253 }
254 
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-2014 The KDE developers.
Generated on Tue Oct 14 2014 23:00:27 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

akonadi

Skip menu "akonadi"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • Modules
  • Related Pages

kdepimlibs API Reference

Skip menu "kdepimlibs API Reference"
  • akonadi
  •   contact
  •   kmime
  •   socialutils
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kldap
  • kmbox
  • kmime
  • kpimidentities
  • kpimtextedit
  • kresources
  • ktnef
  • kxmlrpcclient
  • microblog

Search



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