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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
|
//=============================================================================
//
// File : KviTalListWidget.cpp
// Creation date : Mon May 05 2008 11:25:08 by Alessandro Carbone
//
// This file is part of the KVIrc IRC client distribution
// Copyright (C) 2008 Alessandro Carbone (elfonol at gmail dot com)
//
// This program is FREE software. You can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, write to the Free Software Foundation,
// Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
//
//=============================================================================
#include "KviTalListWidget.h"
#include "KviTalIconAndRichTextItemDelegate.h"
#include <QEvent>
#include <QPainter>
#include <QHelpEvent>
#include <QApplication>
KviTalListWidget::KviTalListWidget(QWidget * pParent, QString name, Qt::WindowType f)
: QListWidget(pParent)
{
setObjectName(name);
setWindowFlags(f);
//setUniformItemSizes(true);
//setResizeMode(QListWidget::Adjust);
//setWordWrap(true);
viewport()->installEventFilter(this);
}
bool KviTalListWidget::eventFilter(QObject * o, QEvent * e)
{
if(e->type() == QEvent::Resize)
{
// A veeeery ugly hack.
// This is the only reliable way I have found to perform a full
// relayout on the items.
// The Qt ItemView framework is so complicated that it's almost unusable.
// We had the same functionality in Qt3 with 1/10 of the code.
if(model())
{
// And this does not even work when the items should shrink...
// ...but well, that's the best we can do (but only under Qt5, as with Qt4 this is even protected).
emit model()->layoutChanged();
// The model is hidden so we can't make these public.
//model()->beginResetModel();
//model()->endResetModel();
}
}
return QListWidget::eventFilter(o, e);
}
bool KviTalListWidget::event(QEvent * e)
{
if(e->type() == QEvent::ToolTip)
{
QHelpEvent * helpEvent = static_cast<QHelpEvent *>(e);
const QPoint & p = helpEvent->pos();
QListWidgetItem * item = itemAt(p);
if(item)
{
emit tipRequest(item, p);
}
}
return QListWidget::event(e);
}
KviTalListWidgetText::KviTalListWidgetText(KviTalListWidget * listbox, const QString & text)
: KviTalListWidgetItem(listbox)
{
setText(text);
}
KviTalListWidgetText::KviTalListWidgetText(const QString & text)
: KviTalListWidgetItem()
{
setText(text);
}
KviTalListWidgetText::~KviTalListWidgetText()
= default;
void KviTalListWidgetText::paint(QPainter * painter)
{
int itemHeight = height(listWidget());
QFontMetrics fm = painter->fontMetrics();
int yPos = ((itemHeight - fm.height()) / 2) + fm.ascent();
painter->drawText(3, yPos, text());
}
int KviTalListWidgetText::height(const KviTalListWidget * lb) const
{
return lb ? lb->fontMetrics().lineSpacing() + 2 : 0;
}
int KviTalListWidgetText::width(const KviTalListWidget * lb) const
{
return lb ? lb->fontMetrics().horizontalAdvance(text()) + 6 : 0;
}
int KviTalListWidgetText::rtti() const
{
return RTTI;
}
KviTalListWidgetPixmap::KviTalListWidgetPixmap(KviTalListWidget * listbox, const QPixmap & pixmap)
: KviTalListWidgetItem(listbox)
{
pm = pixmap;
}
KviTalListWidgetPixmap::KviTalListWidgetPixmap(const QPixmap & pixmap)
: KviTalListWidgetItem()
{
pm = pixmap;
}
KviTalListWidgetPixmap::~KviTalListWidgetPixmap()
= default;
KviTalListWidgetPixmap::KviTalListWidgetPixmap(KviTalListWidget * listbox, const QPixmap & pix, const QString & text)
: KviTalListWidgetItem(listbox)
{
pm = pix;
setText(text);
setIcon(QIcon(pix));
}
KviTalListWidgetPixmap::KviTalListWidgetPixmap(const QPixmap & pix, const QString & text)
: KviTalListWidgetItem()
{
pm = pix;
setText(text);
}
void KviTalListWidgetPixmap::paint(QPainter * painter)
{
int itemHeight = height(listWidget());
int yPos;
const QPixmap * pm = pixmap();
if(pm && !pm->isNull())
{
yPos = (itemHeight - pm->height()) / 2;
painter->drawPixmap(3, yPos, *pm);
}
if(!text().isEmpty())
{
QFontMetrics fm = painter->fontMetrics();
yPos = ((itemHeight - fm.height()) / 2) + fm.ascent();
painter->drawText(pm->width() + 5, yPos, text());
}
}
int KviTalListWidgetPixmap::height(const KviTalListWidget * lb) const
{
int h;
if(text().isEmpty())
h = pm.height();
else
h = qMax(pm.height(), lb->fontMetrics().lineSpacing() + 2);
return h;
}
int KviTalListWidgetPixmap::width(const KviTalListWidget * lb) const
{
if(text().isEmpty())
return pm.width() + 6;
return pm.width() + lb->fontMetrics().horizontalAdvance(text()) + 6;
}
int KviTalListWidgetPixmap::rtti() const
{
return RTTI;
}
|