akonadi/contact
displaynameeditwidget.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
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
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( i18n( "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
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
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
00211 parentWidget->resize( maxWidth + mAdditionalPopupWidth + 20, parentWidget->height() );
00212 }
00213 return false;
00214 }
00215
00216 return eventFilter( object, event );
00217 }
00218
00219 void DisplayNameEditWidget::updateView()
00220 {
00221
00222 mView->setItemText( 0, mContact.givenName() + QLatin1Char( ' ' ) + mContact.familyName() );
00223
00224
00225 mView->setItemText( 1, mContact.assembledName() );
00226
00227
00228 mView->setItemText( 2, mContact.familyName() + QLatin1String( ", " ) + mContact.givenName() );
00229
00230
00231 mView->setItemText( 3, mContact.familyName() + QLatin1Char( ' ' ) + mContact.givenName() );
00232
00233
00234 mView->setItemText( 4, mContact.organization() );
00235
00236
00237 mView->setItemText( 5, mContact.formattedName() );
00238
00239 mView->setEditable( mDisplayType == CustomName );
00240
00241 mView->setCurrentIndex( (int)mDisplayType );
00242 }
00243
00244 #include "displaynameeditwidget.moc"