1
/***************************************************************************
2
* Copyright (C) 2012-2013 by Savoir-Faire Linux *
3
* Author : Emmanuel Lepage Vallee <emmanuel.lepage@savoirfairelinux.com>*
5
* This program is free software; you can redistribute it and/or modify *
6
* it under the terms of the GNU General Public License as published by *
7
* the Free Software Foundation; either version 3 of the License, or *
8
* (at your option) any later version. *
10
* This program is distributed in the hope that it will be useful, *
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13
* GNU General Public License for more details. *
15
* You should have received a copy of the GNU General Public License *
16
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
17
**************************************************************************/
20
#include "../lib/instantmessagingmodel.h"
21
#include "../lib/call.h"
22
#include <QtGui/QPainter>
25
#include <QtGui/QFont>
27
///Delegate contructor
28
ImDelegates::ImDelegates(IMTab* parent) : QStyledItemDelegate(parent),m_pParent(parent)
34
QSize ImDelegates::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const {
36
QPixmap* icon = (QPixmap*)index.data(InstantMessagingModel::MESSAGE_IMAGE_ROLE).value<void*>();
37
QFontMetrics metric( option.font);
38
QRect requiredRect = metric.boundingRect(0,0,m_pParent->width()-30 - 48 - 10 /*margin*/,500,Qt::TextWordWrap|Qt::AlignLeft,index.data(InstantMessagingModel::MESSAGE_TYPE_ROLE).toString());
39
height+=requiredRect.height();
40
height+=metric.height()+10;
41
if (icon && dynamic_cast<QPixmap*>(icon) && height < icon->height()) {
42
height = icon->height();
44
return QSize(m_pParent->width()-30,height);
48
void ImDelegates::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
50
Q_ASSERT(index.isValid());
51
QPixmap* icon = (QPixmap*)index.data(InstantMessagingModel::MESSAGE_IMAGE_ROLE).value<void*>();
53
if (icon && dynamic_cast<QPixmap*>(icon)) {
54
painter->drawPixmap(option.rect.x()+5,option.rect.y()+(option.rect.height()/2)-(icon->height()/2),*icon);
55
icnWidth = icon->width();
58
((QAbstractListModel*) index.model())->setData(index,QPixmap(KIcon("user-identity").pixmap(QSize(48,48))),InstantMessagingModel::MESSAGE_IMAGE_ROLE);
61
QFontMetrics metric(painter->font());
62
QString text = index.data(InstantMessagingModel::MESSAGE_TYPE_ROLE).toString();
63
QRect requiredRect = metric.boundingRect(option.rect.x()+icnWidth+10,option.rect.y()+metric.height()+5,option.rect.width() - icnWidth - 10 /*margin*/,500,Qt::TextWordWrap|Qt::AlignLeft,text);
64
painter->drawText(requiredRect,Qt::AlignLeft|Qt::TextWordWrap,text);
66
QFont font = painter->font();
68
painter->setFont(font);
69
painter->drawText(option.rect.x()+icnWidth+10,option.rect.y()+metric.height(),index.data(InstantMessagingModel::MESSAGE_FROM_ROLE).toString());
71
painter->setFont(font);
76
IMTab::IMTab(InstantMessagingModel* model,QWidget* parent) : QListView(parent)
79
setAlternatingRowColors(true);
81
setUniformItemSizes(false);
82
setItemDelegate(new ImDelegates(this));
83
setVerticalScrollMode(ScrollPerPixel);
84
connect(model,SIGNAL(dataChanged(QModelIndex,QModelIndex)),this,SLOT(scrollBottom()));
87
///Scroll to last message
88
void IMTab::scrollBottom()
90
scrollTo(model()->index(model()->rowCount()-1,0));