24 #include <KDE/KLineEdit>
27 #include <QtGui/QApplication>
28 #include <QtGui/QTextOption>
29 #include <QtGui/QPainter>
30 #include <QtGui/QVBoxLayout>
31 #include <QtGui/QListView>
39 class ContactGridDelegate :
public QAbstractItemDelegate
42 Q_DISABLE_COPY(ContactGridDelegate)
45 ContactGridDelegate(
QObject *parent);
46 virtual ~ContactGridDelegate();
48 virtual
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const;
49 virtual QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const;
60 class KTp::ContactGridDelegate::Private
63 Private(KTp::ContactGridDelegate *parent)
72 KTp::ContactGridDelegate *q;
75 KTp::ContactGridDelegate::ContactGridDelegate(
QObject *parent)
76 : QAbstractItemDelegate(parent),
77 d(new KTp::ContactGridDelegate::Private(this))
81 KTp::ContactGridDelegate::~ContactGridDelegate()
86 void KTp::ContactGridDelegate::paint(QPainter *painter,
const QStyleOptionViewItem &option,
const QModelIndex &index)
const
88 QStyle *style = QApplication::style();
89 int textHeight = option.fontMetrics.height() * 2;
91 style->drawPrimitive(QStyle::PE_PanelItemViewItem, &option, painter);
93 QRect avatarRect = option.rect.adjusted(0, 0, 0, -textHeight);
94 QRect textRect = option.rect.adjusted(0, option.rect.height() - textHeight, 0, -3);
98 if (avatar.isNull()) {
99 avatar = KIcon(QLatin1String(
"im-user-online")).pixmap(option.decorationSize);
100 }
else if (avatar.width() > option.decorationSize.width() || avatar.height() > option.decorationSize.height()) {
102 avatar = avatar.scaled(option.decorationSize, Qt::KeepAspectRatio);
105 style->drawItemPixmap(painter, avatarRect, Qt::AlignCenter, avatar);
108 QTextOption textOption;
109 textOption.setAlignment(Qt::AlignCenter);
110 textOption.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere);
111 painter->drawText(textRect, index.data().toString(), textOption);
115 QSize KTp::ContactGridDelegate::sizeHint(
const QStyleOptionViewItem &option,
const QModelIndex &index)
const
118 int textHeight = option.fontMetrics.height() * 2;
119 return QSize(option.decorationSize.width() + 4, option.decorationSize.height() + textHeight + 3);
124 class KTp::ContactGridWidget::Private
129 layout(new QVBoxLayout(parent)),
130 contactGridView(new QListView(parent)),
131 contactFilterLineEdit(new KLineEdit(parent)),
141 void _k_onSelectionChanged(QItemSelection,QItemSelection);
145 QListView *contactGridView;
146 KLineEdit *contactFilterLineEdit;
151 void KTp::ContactGridWidget::Private::_k_onSelectionChanged(QItemSelection newSelection, QItemSelection oldSelection)
153 Q_UNUSED(oldSelection)
154 kDebug() << newSelection << oldSelection;
156 if (newSelection.isEmpty()) {
161 Q_EMIT q->selectionChanged(q->selectedAccount(), q->selectedContact());
172 d->contactsModel = model;
173 d->filterModel->setSourceModel(d->contactsModel);
175 d->contactGridView->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
176 d->contactGridView->setResizeMode(QListView::Adjust);
177 d->contactGridView->setSpacing(5);
178 d->contactGridView->setViewMode(QListView::IconMode);
179 d->contactGridView->setIconSize(QSize(80, 80));
181 d->contactFilterLineEdit->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
182 d->contactFilterLineEdit->setClearButtonShown(
true);
184 d->layout->setMargin(0);
185 d->layout->addWidget(d->contactGridView);
186 d->layout->addWidget(d->contactFilterLineEdit);
187 setLayout(d->layout);
189 d->contactGridView->setModel(d->filterModel);
190 d->contactGridView->setItemDelegate(
new ContactGridDelegate(d->contactGridView));
192 connect(d->contactGridView->selectionModel(),
194 SLOT(_k_onSelectionChanged(QItemSelection,QItemSelection)));
196 connect(d->contactFilterLineEdit,
197 SIGNAL(textChanged(QString)),
199 SLOT(setDisplayNameFilterString(QString)));
209 return d->contactFilterLineEdit->text();
214 setDisplayNameFilter(QString());
219 if (displayNameFilter != d->contactFilterLineEdit->text()) {
220 d->contactFilterLineEdit->setText(displayNameFilter);
221 Q_EMIT displayNameFilterChanged(displayNameFilter);
227 return d->contactGridView->iconSize();
233 if (iconSize != d->contactGridView->iconSize()) {
234 d->contactGridView->setIconSize(iconSize);
235 Q_EMIT iconSizeChanged(iconSize);
241 return d->contactGridView->selectionModel()->hasSelection();
246 return d->contactGridView->currentIndex().data(
KTp::AccountRole).value<Tp::AccountPtr>();
256 return d->filterModel;
261 return d->contactFilterLineEdit;
264 #include "contact-grid-widget.moc"
265 #include "moc_contact-grid-widget.cpp"