KDEGames

kchatbaseitemdelegate.cpp
1 /*
2  This file is part of the KDE games library
3  SPDX-FileCopyrightText: 2007 Gael de Chalendar (aka Kleag) <[email protected]>
4 
5  SPDX-License-Identifier: LGPL-2.0-only
6 */
7 
8 #include "kchatbaseitemdelegate.h"
9 
10 // own
11 #include "kchatbasemodel.h"
12 // KF
13 #include <KLocalizedString>
14 // Qt
15 #include <QPainter>
16 #include <QDebug>
17 
19  QAbstractItemDelegate(parent)
20 {
21 }
22 
24 {
25 }
26 
28  const QModelIndex &index) const
29 {
30 // qDebug() << "KChatBaseItemDelegate::paint";
32  paint(painter, option, index,m.first, m.second);
33 }
34 
36  const QStyleOptionViewItem &option,
37  const QModelIndex &index,
38  const QString& sender,
39  const QString& message) const
40 {
41 // qDebug() << "KChatBaseItemDelegate::paint";
42  painter->setFont(((KChatBaseModel*)index.model())->nameFont());
43  painter->drawText(option.rect.x(),
44  QFontMetrics(option.font).height()+option.rect.y(), i18n("%1: ",sender));
45  painter->setFont(((KChatBaseModel*)index.model())->messageFont());
46  painter->drawText(option.rect.x() + 3 + QFontMetrics(((KChatBaseModel*)index.model())->nameFont()).boundingRect(i18n("%1: ",sender)).width(),
47  QFontMetrics(option.font).height()+option.rect.y(), message);
48 }
49 
51  const QModelIndex & index ) const
52 {
53 // qDebug() << "KChatBaseItemDelegate::sizeHint";
55  return sizeHint(option, index, m.first, m.second);
56 }
57 
59  const QModelIndex & index,
60  const QString& sender,
61  const QString& message ) const
62 {
63 // qDebug() << "KChatBaseItemDelegate::sizeHint";
64  int w = 0;
65  w += 6;
66  w += QFontMetrics(option.font).boundingRect(sender+i18n("%1: ",sender)).width();
67  w += QFontMetrics(option.font).boundingRect(message).width();
68  int h = 0;
69  h += 2;
70  if (QFontMetrics(((KChatBaseModel*)index.model())->nameFont()).lineSpacing() >
71  QFontMetrics(((KChatBaseModel*)index.model())->messageFont()).lineSpacing())
72  {
73  h += QFontMetrics(((KChatBaseModel*)index.model())->nameFont()).lineSpacing();
74  }
75  else
76  {
77  h += QFontMetrics(((KChatBaseModel*)index.model())->messageFont()).lineSpacing();
78  }
79  return QSize(w,h);
80 }
81 
82 
The class of the elements stored in the chat list model.
QObject * sender() const const
T value() const const
QFont nameFont() const
This font should be used for the name (the "from: " part) of a message.
Definition: kchatbase.cpp:328
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override
Reimplementation of the default paint method.
int width() const const
QRect boundingRect(QChar ch) const const
void setFont(const QFont &font)
QFont messageFont() const
This font should be used for a message.
Definition: kchatbase.cpp:335
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...)
~KChatBaseItemDelegate() override
Destruct a KChatBaseItemDelegate object.
int width() const const
const QAbstractItemModel * model() const const
int height() const const
KChatBaseItemDelegate(QObject *parent=nullptr)
Constructs a KChatBaseItemDelegate object.
QObject * parent() const const
The model used to store messages displayed in the chat dialog messages list.
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Tue Dec 7 2021 22:34:14 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.