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

akonadi/contact

displaynameeditwidget.cpp
00001 /*
00002     This file is part of Akonadi Contact.
00003 
00004     Copyright (c) 2009 Tobias Koenig <tokoe@kde.org>
00005 
00006     This library is free software; you can redistribute it and/or modify it
00007     under the terms of the GNU Library General Public License as published by
00008     the Free Software Foundation; either version 2 of the License, or (at your
00009     option) any later version.
00010 
00011     This library is distributed in the hope that it will be useful, but WITHOUT
00012     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00013     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
00014     License for more details.
00015 
00016     You should have received a copy of the GNU Library General Public License
00017     along with this library; see the file COPYING.LIB.  If not, write to the
00018     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00019     02110-1301, USA.
00020 */
00021 
00022 #include "displaynameeditwidget.h"
00023 
00024 #include <QtCore/QEvent>
00025 #include <QtCore/QString>
00026 #include <QtGui/QAbstractItemView>
00027 #include <QtGui/QHBoxLayout>
00028 #include <QtGui/QPainter>
00029 #include <QtGui/QStyledItemDelegate>
00030 
00031 #include <kabc/addressee.h>
00032 #include <kcombobox.h>
00033 #include <kdialog.h>
00034 #include <klocale.h>
00035 
00036 // Tries to guess the display type that is used for the passed contact
00037 static DisplayNameEditWidget::DisplayType guessedDisplayType( const KABC::Addressee &contact )
00038 {
00039   if ( contact.formattedName() == (contact.givenName() + QLatin1Char( ' ' ) + contact.familyName()) )
00040     return DisplayNameEditWidget::SimpleName;
00041   else if ( contact.formattedName() == contact.assembledName() )
00042     return DisplayNameEditWidget::FullName;
00043   else if ( contact.formattedName() == (contact.familyName() + QLatin1String( ", " ) + contact.givenName()) )
00044     return DisplayNameEditWidget::ReverseNameWithComma;
00045   else if ( contact.formattedName() == (contact.familyName() + QLatin1Char( ' ' ) + contact.givenName()) )
00046     return DisplayNameEditWidget::ReverseName;
00047   else if ( contact.formattedName() == contact.organization() )
00048     return DisplayNameEditWidget::Organization;
00049   else
00050     return DisplayNameEditWidget::CustomName;
00051 }
00052 
00053 class DisplayNameDelegate : public QStyledItemDelegate
00054 {
00055   public:
00056     DisplayNameDelegate( QAbstractItemView *view, QObject *parent = 0 )
00057       : QStyledItemDelegate( parent ), mMaxDescriptionWidth( 0 )
00058     {
00059       mDescriptions.append( i18n( "Short Name" ) );
00060       mDescriptions.append( i18n( "Full Name" ) );
00061       mDescriptions.append( i18n( "Reverse Name with Comma" ) );
00062       mDescriptions.append( i18n( "Reverse Name" ) );
00063       mDescriptions.append( i18n( "Organization" ) );
00064       mDescriptions.append( i18nc( "@item:inlistbox A custom name format", "Custom" ) );
00065 
00066       QFont font = view->font();
00067       font.setStyle( QFont::StyleItalic );
00068       QFontMetrics metrics( font );
00069       foreach ( const QString &description, mDescriptions )
00070         mMaxDescriptionWidth = qMax( mMaxDescriptionWidth, metrics.width( description ) );
00071 
00072       mMaxDescriptionWidth += 3;
00073     }
00074 
00075     int maximumDescriptionWidth() const
00076     {
00077       return mMaxDescriptionWidth;
00078     }
00079 
00080     virtual void paint( QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index ) const
00081     {
00082       QStyledItemDelegate::paint( painter, option, index );
00083       const QRect rect( option.rect.width() - mMaxDescriptionWidth, option.rect.y(), mMaxDescriptionWidth, option.rect.height() );
00084       painter->save();
00085       QFont font( painter->font() );
00086       font.setStyle( QFont::StyleItalic );
00087       painter->setFont( font );
00088       if ( option.state & QStyle::State_Selected )
00089         painter->setPen( option.palette.color( QPalette::Normal, QPalette::BrightText ) );
00090       else
00091         painter->setPen( option.palette.color( QPalette::Disabled, QPalette::Text ) );
00092       painter->drawText( rect, Qt::AlignLeft, mDescriptions.at( index.row() ) );
00093       painter->restore();
00094     }
00095 
00096     QSize sizeHint( const QStyleOptionViewItem& option, const QModelIndex& index ) const
00097     {
00098       QSize size = QStyledItemDelegate::sizeHint( option, index );
00099       size.setWidth( size.width() + mMaxDescriptionWidth );
00100 
00101       return size;
00102     }
00103 
00104   private:
00105     QStringList mDescriptions;
00106     int mMaxDescriptionWidth;
00107 };
00108 
00109 DisplayNameEditWidget::DisplayNameEditWidget( QWidget *parent )
00110   : QWidget( parent ),
00111     mDisplayType( FullName )
00112 {
00113   QHBoxLayout *layout = new QHBoxLayout( this );
00114   layout->setMargin( 0 );
00115   layout->setSpacing( KDialog::spacingHint() );
00116 
00117   mView = new KComboBox( this );
00118   mView->addItems( QStringList() << QString() << QString() << QString()
00119                                  << QString() << QString() << QString() );
00120 
00121   layout->addWidget( mView );
00122 
00123   connect( mView, SIGNAL(activated(int)), SLOT(displayTypeChanged(int)) );
00124 
00125   DisplayNameDelegate *delegate = new DisplayNameDelegate( mView->view() );
00126   mView->view()->setItemDelegate( delegate );
00127 
00128   mAdditionalPopupWidth = delegate->maximumDescriptionWidth();
00129 
00130   mViewport = mView->view()->viewport();
00131   mViewport->installEventFilter( this );
00132 }
00133 
00134 DisplayNameEditWidget::~DisplayNameEditWidget()
00135 {
00136 }
00137 
00138 void DisplayNameEditWidget::setReadOnly( bool readOnly )
00139 {
00140   mView->setEnabled( !readOnly );
00141 }
00142 
00143 void DisplayNameEditWidget::setDisplayType( DisplayType type )
00144 {
00145   if ( type == -1 ) {
00146     // guess the used display type
00147     mDisplayType = guessedDisplayType( mContact );
00148   } else
00149     mDisplayType = type;
00150 
00151   updateView();
00152 }
00153 
00154 DisplayNameEditWidget::DisplayType DisplayNameEditWidget::displayType() const
00155 {
00156   return mDisplayType;
00157 }
00158 
00159 void DisplayNameEditWidget::loadContact( const KABC::Addressee &contact )
00160 {
00161   mContact = contact;
00162 
00163   mDisplayType = guessedDisplayType( mContact );
00164 
00165   updateView();
00166 }
00167 
00168 void DisplayNameEditWidget::storeContact( KABC::Addressee &contact ) const
00169 {
00170   contact.setFormattedName( mView->currentText() );
00171 }
00172 
00173 void DisplayNameEditWidget::changeName( const KABC::Addressee &contact )
00174 {
00175   const QString organization = mContact.organization();
00176   mContact = contact;
00177   mContact.setOrganization( organization );
00178   if ( mDisplayType == CustomName )
00179     mContact.setFormattedName( mView->currentText() );
00180 
00181   updateView();
00182 }
00183 
00184 void DisplayNameEditWidget::changeOrganization( const QString &organization )
00185 {
00186   mContact.setOrganization( organization );
00187 
00188   updateView();
00189 }
00190 
00191 void DisplayNameEditWidget::displayTypeChanged( int type )
00192 {
00193   mDisplayType = (DisplayType)type;
00194 
00195   updateView();
00196 }
00197 
00198 bool DisplayNameEditWidget::eventFilter( QObject *object, QEvent *event )
00199 {
00200   if ( object == mViewport ) {
00201     if ( event->type() == QEvent::Show ) {
00202       // retrieve the widget that contains the popup view
00203       QWidget *parentWidget = mViewport->parentWidget()->parentWidget();
00204 
00205       int maxWidth = 0;
00206       QFontMetrics metrics( mView->font() );
00207       for ( int i = 0; i < mView->count(); ++i )
00208         maxWidth = qMax( maxWidth, metrics.width( mView->itemText( i ) ) );
00209 
00210       // resize it to show the complete content
00211       parentWidget->resize( maxWidth + mAdditionalPopupWidth + 20, parentWidget->height() );
00212     }
00213     return false;
00214   }
00215 
00216   return QWidget::eventFilter( object, event );
00217 }
00218 
00219 void DisplayNameEditWidget::updateView()
00220 {
00221   // SimpleName:
00222   mView->setItemText( 0, mContact.givenName() + QLatin1Char( ' ' ) + mContact.familyName() );
00223 
00224   // FullName:
00225   mView->setItemText( 1, mContact.assembledName() );
00226 
00227   // ReverseNameWithComma:
00228   mView->setItemText( 2, mContact.familyName() + QLatin1String( ", " ) + mContact.givenName() );
00229 
00230   // ReverseName:
00231   mView->setItemText( 3, mContact.familyName() + QLatin1Char( ' ' ) + mContact.givenName() );
00232 
00233   // Organization:
00234   mView->setItemText( 4, mContact.organization() );
00235 
00236   // CustomName:
00237   mView->setItemText( 5, mContact.formattedName() );
00238 
00239   // delay the state change here, since we might have been called from mView via a signal
00240   QMetaObject::invokeMethod( this, "setComboBoxEditable", Qt::QueuedConnection, Q_ARG( bool, mDisplayType == CustomName ) );
00241 
00242   mView->setCurrentIndex( (int)mDisplayType );
00243 }
00244 
00245 void DisplayNameEditWidget::setComboBoxEditable( bool value )
00246 {
00247   mView->setEditable( value );
00248 }
00249 
00250 #include "displaynameeditwidget.moc"
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Thu May 10 2012 22:19:26 by doxygen 1.8.0 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.8.3 API Reference

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