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

akonadi

  • 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 <QtCore/QString>
26 #include <QtGui/QAbstractItemView>
27 #include <QtGui/QHBoxLayout>
28 #include <QtGui/QPainter>
29 #include <QtGui/QStyledItemDelegate>
30 
31 #include <kabc/addressee.h>
32 #include <kcombobox.h>
33 #include <kdialog.h>
34 #include <klocale.h>
35 
36 // Tries to guess the display type that is used for the passed contact
37 static DisplayNameEditWidget::DisplayType guessedDisplayType( const KABC::Addressee &contact )
38 {
39  if ( contact.formattedName() == (contact.givenName() + QLatin1Char( ' ' ) + contact.familyName()) )
40  return DisplayNameEditWidget::SimpleName;
41  else if ( contact.formattedName() == contact.assembledName() )
42  return DisplayNameEditWidget::FullName;
43  else if ( contact.formattedName() == (contact.familyName() + QLatin1String( ", " ) + contact.givenName()) )
44  return DisplayNameEditWidget::ReverseNameWithComma;
45  else if ( contact.formattedName() == (contact.familyName() + QLatin1Char( ' ' ) + contact.givenName()) )
46  return DisplayNameEditWidget::ReverseName;
47  else if ( contact.formattedName() == contact.organization() )
48  return DisplayNameEditWidget::Organization;
49  else
50  return DisplayNameEditWidget::CustomName;
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  mMaxDescriptionWidth += 3;
73  }
74 
75  int maximumDescriptionWidth() const
76  {
77  return mMaxDescriptionWidth;
78  }
79 
80  virtual void paint( QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index ) const
81  {
82  QStyledItemDelegate::paint( painter, option, index );
83  const QRect rect( option.rect.width() - mMaxDescriptionWidth, option.rect.y(), mMaxDescriptionWidth, option.rect.height() );
84  painter->save();
85  QFont font( painter->font() );
86  font.setStyle( QFont::StyleItalic );
87  painter->setFont( font );
88  if ( option.state & QStyle::State_Selected )
89  painter->setPen( option.palette.color( QPalette::Normal, QPalette::BrightText ) );
90  else
91  painter->setPen( option.palette.color( QPalette::Disabled, QPalette::Text ) );
92  painter->drawText( rect, Qt::AlignLeft, mDescriptions.at( index.row() ) );
93  painter->restore();
94  }
95 
96  QSize sizeHint( const QStyleOptionViewItem& option, const QModelIndex& index ) const
97  {
98  QSize size = QStyledItemDelegate::sizeHint( option, index );
99  size.setWidth( size.width() + mMaxDescriptionWidth );
100 
101  return size;
102  }
103 
104  private:
105  QStringList mDescriptions;
106  int mMaxDescriptionWidth;
107 };
108 
109 DisplayNameEditWidget::DisplayNameEditWidget( QWidget *parent )
110  : QWidget( parent ),
111  mDisplayType( FullName )
112 {
113  QHBoxLayout *layout = new QHBoxLayout( this );
114  layout->setMargin( 0 );
115  layout->setSpacing( KDialog::spacingHint() );
116 
117  mView = new KComboBox( this );
118  mView->addItems( QStringList() << QString() << QString() << QString()
119  << QString() << QString() << QString() );
120 
121  layout->addWidget( mView );
122 
123  connect( mView, SIGNAL(activated(int)), SLOT(displayTypeChanged(int)) );
124 
125  DisplayNameDelegate *delegate = new DisplayNameDelegate( mView->view() );
126  mView->view()->setItemDelegate( delegate );
127 
128  mAdditionalPopupWidth = delegate->maximumDescriptionWidth();
129 
130  mViewport = mView->view()->viewport();
131  mViewport->installEventFilter( this );
132 }
133 
134 DisplayNameEditWidget::~DisplayNameEditWidget()
135 {
136 }
137 
138 void DisplayNameEditWidget::setReadOnly( bool readOnly )
139 {
140  mView->setEnabled( !readOnly );
141 }
142 
143 void DisplayNameEditWidget::setDisplayType( DisplayType type )
144 {
145  if ( type == -1 ) {
146  // guess the used display type
147  mDisplayType = guessedDisplayType( mContact );
148  } else
149  mDisplayType = type;
150 
151  updateView();
152 }
153 
154 DisplayNameEditWidget::DisplayType DisplayNameEditWidget::displayType() const
155 {
156  return mDisplayType;
157 }
158 
159 void DisplayNameEditWidget::loadContact( const KABC::Addressee &contact )
160 {
161  mContact = contact;
162 
163  mDisplayType = guessedDisplayType( mContact );
164 
165  updateView();
166 }
167 
168 void DisplayNameEditWidget::storeContact( KABC::Addressee &contact ) const
169 {
170  contact.setFormattedName( mView->currentText() );
171 }
172 
173 void DisplayNameEditWidget::changeName( const KABC::Addressee &contact )
174 {
175  const QString organization = mContact.organization();
176  mContact = contact;
177  mContact.setOrganization( organization );
178  if ( mDisplayType == CustomName )
179  mContact.setFormattedName( mView->currentText() );
180 
181  updateView();
182 }
183 
184 void DisplayNameEditWidget::changeOrganization( const QString &organization )
185 {
186  mContact.setOrganization( organization );
187 
188  updateView();
189 }
190 
191 void DisplayNameEditWidget::displayTypeChanged( int type )
192 {
193  mDisplayType = (DisplayType)type;
194 
195  updateView();
196 }
197 
198 bool DisplayNameEditWidget::eventFilter( QObject *object, QEvent *event )
199 {
200  if ( object == mViewport ) {
201  if ( event->type() == QEvent::Show ) {
202  // retrieve the widget that contains the popup view
203  QWidget *parentWidget = mViewport->parentWidget()->parentWidget();
204 
205  int maxWidth = 0;
206  QFontMetrics metrics( mView->font() );
207  for ( int i = 0; i < mView->count(); ++i )
208  maxWidth = qMax( maxWidth, metrics.width( mView->itemText( i ) ) );
209 
210  // resize it to show the complete content
211  parentWidget->resize( maxWidth + mAdditionalPopupWidth + 20, parentWidget->height() );
212  }
213  return false;
214  }
215 
216  return QWidget::eventFilter( object, event );
217 }
218 
219 void DisplayNameEditWidget::updateView()
220 {
221  // SimpleName:
222  mView->setItemText( 0, mContact.givenName() + QLatin1Char( ' ' ) + mContact.familyName() );
223 
224  // FullName:
225  mView->setItemText( 1, mContact.assembledName() );
226 
227  // ReverseNameWithComma:
228  mView->setItemText( 2, mContact.familyName() + QLatin1String( ", " ) + mContact.givenName() );
229 
230  // ReverseName:
231  mView->setItemText( 3, mContact.familyName() + QLatin1Char( ' ' ) + mContact.givenName() );
232 
233  // Organization:
234  mView->setItemText( 4, mContact.organization() );
235 
236  // CustomName:
237  mView->setItemText( 5, mContact.formattedName() );
238 
239  // delay the state change here, since we might have been called from mView via a signal
240  QMetaObject::invokeMethod( this, "setComboBoxEditable", Qt::QueuedConnection, Q_ARG( bool, mDisplayType == CustomName ) );
241 
242  mView->setCurrentIndex( (int)mDisplayType );
243 }
244 
245 void DisplayNameEditWidget::setComboBoxEditable( bool value )
246 {
247  mView->setEditable( value );
248 }
249 
250 #include "displaynameeditwidget.moc"
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Mon Sep 24 2012 09:06:25 by doxygen 1.8.1.1 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-4.9.1 API Reference

Skip menu "kdepimlibs-4.9.1 API Reference"
  • akonadi
  •   contact
  •   kmime
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  •   richtextbuilders
  • 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