File: kchatbaseitemdelegate.cpp

package info (click to toggle)
libkdegames 4%3A20.12.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 35,316 kB
  • sloc: cpp: 13,600; perl: 5,276; sh: 56; makefile: 13
file content (92 lines) | stat: -rw-r--r-- 3,340 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*
    This file is part of the KDE games library
    Copyright (C) 2007 Gael de Chalendar (aka Kleag) <kleag@free.fr>

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License version 2 as published by the Free Software Foundation.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301, USA.
*/

#include "kchatbaseitemdelegate.h"
#include "kchatbasemodel.h"

#include <KLocalizedString>
#include <QPainter>
#include <QDebug>

KChatBaseItemDelegate::KChatBaseItemDelegate(QObject *parent) : 
  QAbstractItemDelegate(parent)
{
}

KChatBaseItemDelegate::~KChatBaseItemDelegate()
{
}

void KChatBaseItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
                const QModelIndex &index) const
{
//  qDebug() << "KChatBaseItemDelegate::paint";
 KChatBaseMessage m  = index.model()->data(index, Qt::DisplayRole).value<KChatBaseMessage>();
 paint(painter, option, index,m.first, m.second);
}

void KChatBaseItemDelegate::paint(QPainter *painter, 
				  const QStyleOptionViewItem &option,
				  const QModelIndex &index,
				   const QString& sender,
				   const QString& message) const
{
//  qDebug() << "KChatBaseItemDelegate::paint";
 QFontMetrics fm = painter->fontMetrics();
 painter->setFont(((KChatBaseModel*)index.model())->nameFont());
 painter->drawText(option.rect.x(), 
		   QFontMetrics(option.font).height()+option.rect.y(), i18n("%1: ",sender));
 painter->setFont(((KChatBaseModel*)index.model())->messageFont());
 painter->drawText(option.rect.x() + 3 + QFontMetrics(((KChatBaseModel*)index.model())->nameFont()).boundingRect(i18n("%1: ",sender)).width(),
		   QFontMetrics(option.font).height()+option.rect.y(), message);
}

QSize KChatBaseItemDelegate::sizeHint(const QStyleOptionViewItem &  option ,
		    const QModelIndex &  index ) const
{
//   qDebug() << "KChatBaseItemDelegate::sizeHint";
  KChatBaseMessage m  = index.model()->data(index, Qt::DisplayRole).value<KChatBaseMessage>();
  return sizeHint(option, index, m.first, m.second);
}

QSize KChatBaseItemDelegate::sizeHint(const QStyleOptionViewItem &  option ,
		    const QModelIndex &  index,
				   const QString& sender,
				   const QString& message ) const
{
//   qDebug() << "KChatBaseItemDelegate::sizeHint";
  int w = 0;
  w += 6;
  w += QFontMetrics(option.font).boundingRect(sender+i18n("%1: ",sender)).width();
  w += QFontMetrics(option.font).boundingRect(message).width();
  int h = 0;
  h += 2;
  if (QFontMetrics(((KChatBaseModel*)index.model())->nameFont()).lineSpacing() > 
    QFontMetrics(((KChatBaseModel*)index.model())->messageFont()).lineSpacing()) 
  {
    h += QFontMetrics(((KChatBaseModel*)index.model())->nameFont()).lineSpacing();
  } 
  else 
  {
    h += QFontMetrics(((KChatBaseModel*)index.model())->messageFont()).lineSpacing();
  }
  return QSize(w,h);
}