22 #include "displaynameeditwidget.h"
24 #include <QtCore/QEvent>
25 #include <QAbstractItemView>
26 #include <QHBoxLayout>
28 #include <QStyledItemDelegate>
30 #include <kabc/addressee.h>
31 #include <kcombobox.h>
33 #include <klocalizedstring.h>
38 if ( contact.formattedName() == ( contact.givenName() + QLatin1Char(
' ' ) + contact.familyName() ) ) {
40 }
else if ( contact.formattedName() == contact.assembledName() ) {
42 }
else if ( contact.formattedName() == ( contact.familyName() + QLatin1String(
", " ) + contact.givenName() ) ) {
44 }
else if ( contact.formattedName() == ( contact.familyName() + QLatin1Char(
' ' ) + contact.givenName() ) ) {
46 }
else if ( contact.formattedName() == contact.organization() ) {
53 class DisplayNameDelegate :
public QStyledItemDelegate
56 DisplayNameDelegate( QAbstractItemView *view, QObject *parent = 0 )
57 : QStyledItemDelegate( parent ), mMaxDescriptionWidth( 0 )
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" ) );
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 ) );
73 mMaxDescriptionWidth += 3;
76 int maximumDescriptionWidth()
const
78 return mMaxDescriptionWidth;
81 virtual void paint( QPainter *painter,
const QStyleOptionViewItem &option,
const QModelIndex &index )
const
83 QStyledItemDelegate::paint( painter, option, index );
84 const QRect rect( option.rect.width() - mMaxDescriptionWidth, option.rect.y(), mMaxDescriptionWidth, option.rect.height() );
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 ) );
92 painter->setPen( option.palette.color( QPalette::Disabled, QPalette::Text ) );
94 painter->drawText( rect, Qt::AlignLeft, mDescriptions.at( index.row() ) );
98 QSize sizeHint(
const QStyleOptionViewItem& option,
const QModelIndex& index )
const
100 QSize size = QStyledItemDelegate::sizeHint( option, index );
101 size.setWidth( size.width() + mMaxDescriptionWidth );
107 QStringList mDescriptions;
108 int mMaxDescriptionWidth;
111 DisplayNameEditWidget::DisplayNameEditWidget( QWidget *parent )
113 mDisplayType( FullName )
115 QHBoxLayout *layout =
new QHBoxLayout(
this );
116 layout->setMargin( 0 );
117 layout->setSpacing( KDialog::spacingHint() );
119 mView =
new KComboBox(
this );
120 mView->addItems( QStringList() << QString() << QString() << QString()
121 << QString() << QString() << QString() );
123 layout->addWidget( mView );
124 setFocusProxy( mView );
125 setFocusPolicy( Qt::StrongFocus );
126 connect( mView, SIGNAL(activated(
int)), SLOT(displayTypeChanged(
int)) );
128 DisplayNameDelegate *delegate =
new DisplayNameDelegate( mView->view() );
129 mView->view()->setItemDelegate( delegate );
131 mAdditionalPopupWidth = delegate->maximumDescriptionWidth();
133 mViewport = mView->view()->viewport();
134 mViewport->installEventFilter(
this );
137 DisplayNameEditWidget::~DisplayNameEditWidget()
141 void DisplayNameEditWidget::setReadOnly(
bool readOnly )
143 mView->setEnabled( !readOnly );
146 void DisplayNameEditWidget::setDisplayType( DisplayType type )
150 mDisplayType = guessedDisplayType( mContact );
162 void DisplayNameEditWidget::loadContact(
const KABC::Addressee &contact )
166 mDisplayType = guessedDisplayType( mContact );
171 void DisplayNameEditWidget::storeContact( KABC::Addressee &contact )
const
173 contact.setFormattedName( mView->currentText() );
176 void DisplayNameEditWidget::changeName(
const KABC::Addressee &contact )
178 const QString organization = mContact.organization();
180 mContact.setOrganization( organization );
181 if ( mDisplayType == CustomName ) {
182 mContact.setFormattedName( mView->currentText() );
188 void DisplayNameEditWidget::changeOrganization(
const QString &organization )
190 mContact.setOrganization( organization );
195 void DisplayNameEditWidget::displayTypeChanged(
int type )
197 mDisplayType = (DisplayType)type;
202 bool DisplayNameEditWidget::eventFilter( QObject *
object, QEvent *event )
204 if (
object == mViewport ) {
205 if ( event->type() == QEvent::Show ) {
207 QWidget *parentWidget = mViewport->parentWidget()->parentWidget();
210 QFontMetrics metrics( mView->font() );
211 for (
int i = 0; i < mView->count(); ++i ) {
212 maxWidth = qMax( maxWidth, metrics.width( mView->itemText( i ) ) );
216 parentWidget->resize( maxWidth + mAdditionalPopupWidth + 20, parentWidget->height() );
221 return QWidget::eventFilter(
object, event );
224 void DisplayNameEditWidget::updateView()
227 mView->setItemText( 0, mContact.givenName() + QLatin1Char(
' ' ) + mContact.familyName() );
230 mView->setItemText( 1, mContact.assembledName() );
233 mView->setItemText( 2, mContact.familyName() + QLatin1String(
", " ) + mContact.givenName() );
236 mView->setItemText( 3, mContact.familyName() + QLatin1Char(
' ' ) + mContact.givenName() );
239 mView->setItemText( 4, mContact.organization() );
242 mView->setItemText( 5, mContact.formattedName() );
245 QMetaObject::invokeMethod(
this,
"setComboBoxEditable", Qt::QueuedConnection, Q_ARG(
bool, mDisplayType == CustomName ) );
247 mView->setCurrentIndex( (
int)mDisplayType );
250 void DisplayNameEditWidget::setComboBoxEditable(
bool value )
252 mView->setEditable( value );