22 #include "contactgroupeditordelegate_p.h"
24 #include "contactcompletionmodel_p.h"
25 #include "contactgroupmodel_p.h"
27 #include <akonadi/entitytreemodel.h>
28 #include <kcombobox.h>
30 #include <klocalizedstring.h>
32 #include <QtCore/QTimer>
33 #include <QAbstractItemView>
35 #include <QMouseEvent>
36 #include <QSortFilterProxyModel>
37 #include <QToolButton>
39 using namespace Akonadi;
44 class ContactsWithEmailFilterModel :
public QSortFilterProxyModel
47 ContactsWithEmailFilterModel( QObject *parent )
48 : QSortFilterProxyModel( parent )
51 setSortLocaleAware(
true );
55 virtual bool filterAcceptsRow(
int row,
const QModelIndex &parent )
const
57 const QModelIndex index = sourceModel()->index( row, Akonadi::ContactCompletionModel::EmailColumn, parent );
58 if ( !index.isValid() ) {
62 return !index.data().toString().isEmpty();
66 ContactLineEdit::ContactLineEdit(
bool isReference, QWidget *parent )
67 : KLineEdit( parent ), mIsReference( isReference )
71 ContactsWithEmailFilterModel *filter =
new ContactsWithEmailFilterModel(
this );
72 filter->setSourceModel( Akonadi::ContactCompletionModel::self() );
74 QCompleter *completer =
new QCompleter( filter,
this );
75 completer->setCompletionColumn( Akonadi::ContactCompletionModel::NameColumn );
76 completer->setCaseSensitivity( Qt::CaseInsensitive );
77 connect( completer, SIGNAL(activated(QModelIndex)), SLOT(completed(QModelIndex)) );
79 setCompleter( completer );
81 connect(
this, SIGNAL(textEdited(QString)), SLOT(slotTextEdited()) );
84 bool ContactLineEdit::isReference()
const
89 Akonadi::Item ContactLineEdit::completedItem()
const
94 void ContactLineEdit::completed(
const QModelIndex &index )
96 if ( index.isValid() ) {
101 mIsReference =
false;
104 emit completed(
this );
107 void ContactLineEdit::slotTextEdited()
110 mIsReference =
false;
113 class ContactGroupEditorDelegate::Private
117 : mButtonSize( 16, 16 ), mIcon( QLatin1String(
"list-remove" ) ), mItemView( 0 )
123 QAbstractItemView *mItemView;
126 ContactGroupEditorDelegate::ContactGroupEditorDelegate( QAbstractItemView *view, QObject *parent )
127 : QStyledItemDelegate( parent ), d( new Private )
132 ContactGroupEditorDelegate::~ContactGroupEditorDelegate()
137 QWidget* ContactGroupEditorDelegate::createEditor( QWidget *parent,
const QStyleOptionViewItem&,
138 const QModelIndex &index )
const
140 if ( index.column() == 0 ) {
141 ContactLineEdit *edit = 0;
142 if ( index.data( ContactGroupModel::IsReferenceRole ).toBool() ) {
143 edit =
new ContactLineEdit(
true, parent );
145 edit =
new ContactLineEdit(
false, parent );
148 connect( edit, SIGNAL(completed(QWidget*)), SLOT(completed(QWidget*)) );
152 if ( index.data( ContactGroupModel::IsReferenceRole ).toBool() ) {
153 KComboBox *comboBox =
new KComboBox( parent );
154 comboBox->setFrame(
false );
155 comboBox->setAutoFillBackground(
true );
158 KLineEdit *lineEdit =
new KLineEdit( parent );
159 lineEdit->setFrame(
false );
165 void ContactGroupEditorDelegate::setEditorData( QWidget *editor,
const QModelIndex &index )
const
167 if ( index.data( ContactGroupModel::IsReferenceRole ).toBool() ) {
168 if ( index.column() == 0 ) {
169 KLineEdit *lineEdit = qobject_cast<KLineEdit*>( editor );
174 lineEdit->setText( index.data( Qt::EditRole ).toString() );
176 KComboBox *comboBox = qobject_cast<KComboBox*>( editor );
181 const QStringList emails = index.data( ContactGroupModel::AllEmailsRole ).toStringList();
183 comboBox->addItems( emails );
184 comboBox->setCurrentIndex( comboBox->findText( index.data( Qt::EditRole ).toString() ) );
187 KLineEdit *lineEdit = qobject_cast<KLineEdit*>( editor );
192 lineEdit->setText( index.data( Qt::EditRole ).toString() );
196 void ContactGroupEditorDelegate::setModelData( QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index )
const
198 if ( index.data( ContactGroupModel::IsReferenceRole ).toBool() ) {
199 if ( index.column() == 0 ) {
200 ContactLineEdit *lineEdit =
static_cast<ContactLineEdit*
>( editor );
202 const bool isReference = lineEdit->isReference();
203 const Item item = lineEdit->completedItem();
204 model->setData( index, isReference, ContactGroupModel::IsReferenceRole );
206 if ( item.isValid() ) {
207 model->setData( index, item.id(), Qt::EditRole );
210 model->setData( index, lineEdit->text(), Qt::EditRole );
214 if ( index.column() == 1 ) {
215 KComboBox *comboBox = qobject_cast<KComboBox*>( editor );
220 model->setData( index, comboBox->currentText(), Qt::EditRole );
223 if ( index.column() == 0 ) {
224 ContactLineEdit *lineEdit =
static_cast<ContactLineEdit*
>( editor );
226 const bool isReference = lineEdit->isReference();
227 const Item item = lineEdit->completedItem();
228 model->setData( index, isReference, ContactGroupModel::IsReferenceRole );
230 if ( item.isValid() ) {
231 model->setData( index, item.id(), Qt::EditRole );
234 model->setData( index, lineEdit->text(), Qt::EditRole );
237 if ( index.column() == 1 ) {
238 KLineEdit *lineEdit = qobject_cast<KLineEdit*>( editor );
243 model->setData( index, lineEdit->text(), Qt::EditRole );
248 static bool isLastRow(
const QModelIndex &index )
250 return ( index.row() == ( index.model()->rowCount() - 1 ) );
253 void ContactGroupEditorDelegate::paint( QPainter *painter,
const QStyleOptionViewItem &option,
const QModelIndex &index )
const
255 QStyledItemDelegate::paint( painter, option, index );
257 if ( index.column() == 1 && !isLastRow( index ) ) {
258 d->mIcon.paint( painter, option.rect, Qt::AlignRight );
262 QSize ContactGroupEditorDelegate::sizeHint(
const QStyleOptionViewItem& option,
const QModelIndex& index )
const
266 QSize hint = QStyledItemDelegate::sizeHint( option, index );
267 hint.setHeight( qMax( hint.height(), d->mButtonSize.height() ) );
269 if ( index.column() == 1 ) {
270 hint.setWidth( hint.width() + d->mButtonSize.width() );
276 bool ContactGroupEditorDelegate::editorEvent( QEvent *event, QAbstractItemModel *model,
const QStyleOptionViewItem &option,
const QModelIndex &index )
278 if ( index.column() == 1 && !isLastRow( index ) ) {
279 if ( event->type() == QEvent::MouseButtonRelease ) {
280 const QMouseEvent *mouseEvent =
static_cast<QMouseEvent*
>( event );
281 QRect buttonRect = d->mItemView->visualRect( index );
282 buttonRect.setLeft( buttonRect.right() - d->mButtonSize.width() );
284 if ( buttonRect.contains( mouseEvent->pos() ) ) {
285 model->removeRows( index.row(), 1 );
286 QTimer::singleShot( 0,
this, SLOT(setFirstColumnAsCurrent()) );
291 return QStyledItemDelegate::editorEvent( event, model, option, index );
294 void ContactGroupEditorDelegate::completed( QWidget *widget )
296 emit commitData( widget );
297 emit closeEditor( widget );
300 void ContactGroupEditorDelegate::setFirstColumnAsCurrent()
302 d->mItemView->setCurrentIndex( d->mItemView->model()->index( d->mItemView->currentIndex().row(), 0 ) );
305 #include "moc_contactgroupeditordelegate_p.cpp"