KDEGames

kchatbaseitemdelegate.cpp
1 /*
2  This file is part of the KDE games library
3  Copyright (C) 2007 Gael de Chalendar (aka Kleag) <[email protected]>
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Library General Public
7  License version 2 as published by the Free Software Foundation.
8 
9  This library is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  Library General Public License for more details.
13 
14  You should have received a copy of the GNU Library General Public License
15  along with this library; see the file COPYING.LIB. If not, write to
16  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  Boston, MA 02110-1301, USA.
18 */
19 
20 #include "kchatbaseitemdelegate.h"
21 #include "kchatbasemodel.h"
22 
23 #include <KLocalizedString>
24 #include <QPainter>
25 #include <QDebug>
26 
28  QAbstractItemDelegate(parent)
29 {
30 }
31 
33 {
34 }
35 
37  const QModelIndex &index) const
38 {
39 // qDebug() << "KChatBaseItemDelegate::paint";
41  paint(painter, option, index,m.first, m.second);
42 }
43 
45  const QStyleOptionViewItem &option,
46  const QModelIndex &index,
47  const QString& sender,
48  const QString& message) const
49 {
50 // qDebug() << "KChatBaseItemDelegate::paint";
51  QFontMetrics fm = painter->fontMetrics();
52  painter->setFont(((KChatBaseModel*)index.model())->nameFont());
53  painter->drawText(option.rect.x(),
54  QFontMetrics(option.font).height()+option.rect.y(), i18n("%1: ",sender));
55  painter->setFont(((KChatBaseModel*)index.model())->messageFont());
56  painter->drawText(option.rect.x() + 3 + QFontMetrics(((KChatBaseModel*)index.model())->nameFont()).boundingRect(i18n("%1: ",sender)).width(),
57  QFontMetrics(option.font).height()+option.rect.y(), message);
58 }
59 
61  const QModelIndex & index ) const
62 {
63 // qDebug() << "KChatBaseItemDelegate::sizeHint";
65  return sizeHint(option, index, m.first, m.second);
66 }
67 
69  const QModelIndex & index,
70  const QString& sender,
71  const QString& message ) const
72 {
73 // qDebug() << "KChatBaseItemDelegate::sizeHint";
74  int w = 0;
75  w += 6;
76  w += QFontMetrics(option.font).boundingRect(sender+i18n("%1: ",sender)).width();
77  w += QFontMetrics(option.font).boundingRect(message).width();
78  int h = 0;
79  h += 2;
80  if (QFontMetrics(((KChatBaseModel*)index.model())->nameFont()).lineSpacing() >
81  QFontMetrics(((KChatBaseModel*)index.model())->messageFont()).lineSpacing())
82  {
83  h += QFontMetrics(((KChatBaseModel*)index.model())->nameFont()).lineSpacing();
84  }
85  else
86  {
87  h += QFontMetrics(((KChatBaseModel*)index.model())->messageFont()).lineSpacing();
88  }
89  return QSize(w,h);
90 }
91 
92 
The class of the elements stored in the chat list model.
QObject * sender() const const
T value() const const
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override
Reimplementation of the default paint method.
QRect boundingRect(QChar ch) const const
void setFont(const QFont &font)
DisplayRole
void drawText(const QPointF &position, const QString &text)
virtual QVariant data(const QModelIndex &index, int role) const const =0
QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const override
Reimplementation of the default sizeHint.
QString i18n(const char *text, const TYPE &arg...)
int width() const const
const QAbstractItemModel * model() const const
virtual ~KChatBaseItemDelegate()
Destruct a KChatBaseItemDelegate object.
QFontMetrics fontMetrics() const const
int height() const const
KChatBaseItemDelegate(QObject *parent=nullptr)
Constructs a KChatBaseItemDelegate object.
The model used to store messages displayed in the chat dialog messages list.
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Thu Nov 26 2020 22:36:17 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.