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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
|
/***************************************************************************
* Copyright (C) 2005 by *
* Joris Guisson <joris.guisson@gmail.com> *
* Vincent Wagelaar <vincent@ricardis.tudelft.nl> *
* *
* 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 <qpainter.h>
#include <qpen.h>
#include <qbrush.h>
#include <qvaluelist.h>
#include <qpixmap.h>
#include <math.h>
#include <qtooltip.h>
#include <klocale.h>
#include <qmime.h>
#include <qimage.h>
#include <util/log.h>
#include <interfaces/torrentinterface.h>
#include <util/bitset.h>
#include <torrent/globals.h>
#include "chunkbar.h"
using namespace bt;
using namespace kt;
namespace kt
{
struct Range
{
int first,last;
int fac;
};
static void FillAndFrameBlack(QImage* image, uint color, int size)
{
image->fill(color);
for (int i = 0; i < size; i++)
{
image->setPixel(0, i, 0);
image->setPixel(size - 1, i, 0);
image->setPixel(i, 0, 0);
image->setPixel(i, size - 1, 0);
}
}
static void InitializeToolTipImages(ChunkBar* bar)
{
static bool images_initialized = false;
if (images_initialized)
return;
images_initialized = true;
QMimeSourceFactory* factory = QMimeSourceFactory::defaultFactory();
QImage excluded(16, 16, 32);
FillAndFrameBlack(&excluded, bar->colorGroup().color(QColorGroup::Mid).pixel(), 16);
factory->setImage("excluded_color", excluded);
QImage available(16, 16, 32);
FillAndFrameBlack(&available, bar->colorGroup().highlight().pixel(), 16);
factory->setImage("available_color", available);
QImage unavailable(16, 16, 32);
FillAndFrameBlack(&unavailable, bar->colorGroup().base().pixel(), 16);
factory->setImage("unavailable_color", unavailable);
}
ChunkBar::ChunkBar(QWidget *parent, const char *name)
: QFrame(parent, name),curr_tc(0)
{
setFrameShape(StyledPanel);
setFrameShadow(Sunken);
setLineWidth(3);
setMidLineWidth(3);
show_excluded = false;
InitializeToolTipImages(this);
QToolTip::add(this, i18n("<img src=\"available_color\"> - Downloaded Chunks<br><img src=\"unavailable_color\"> - Chunks to Download<br><img src=\"excluded_color\"> - Excluded Chunks"));
}
ChunkBar::~ChunkBar()
{}
void ChunkBar::updateBar()
{
const BitSet & bs = getBitSet();
QSize s = contentsRect().size();
bool changed = !(curr == bs);
if (show_excluded && curr_tc)
{
BitSet ebs = curr_tc->excludedChunksBitSet();
ebs.orBitSet(curr_tc->onlySeedChunksBitSet()),
changed = changed || !(curr_ebs == ebs);
curr_ebs = ebs;
}
if (changed || pixmap.isNull() || pixmap.width() != s.width())
{
// PROFILE("ChunkBar::updateBar");
// Out() << "Pixmap : " << s.width() << " " << s.height() << endl;
pixmap.resize(s);
pixmap.fill(colorGroup().color(QColorGroup::Base));
QPainter painter(&pixmap);
drawBarContents(&painter);
update();
}
}
void ChunkBar::drawContents(QPainter *p)
{
// first draw background
if (isEnabled())
p->setBrush(colorGroup().base());
else
p->setBrush(colorGroup().background());
p->setPen(Qt::NoPen);
p->drawRect(contentsRect());
if (isEnabled())
p->drawPixmap(contentsRect(),pixmap);
}
void ChunkBar::setTC(kt::TorrentInterface* tc)
{
curr_tc = tc;
QSize s = contentsRect().size();
//Out() << "Pixmap : " << s.width() << " " << s.height() << endl;
pixmap.resize(s);
pixmap.fill(colorGroup().color(QColorGroup::Base));
QPainter painter(&pixmap);
drawBarContents(&painter);
update();
}
void ChunkBar::drawBarContents(QPainter *p)
{
p->saveWorldMatrix();
if (curr_tc)
{
const TorrentStats & s = curr_tc->getStats();
Uint32 w = contentsRect().width();
const BitSet & bs = getBitSet();
curr = bs;
if (bs.allOn())
drawAllOn(p,colorGroup().highlight());
else if (s.total_chunks > w)
drawMoreChunksThenPixels(p,bs,colorGroup().highlight());
else
drawEqual(p,bs,colorGroup().highlight());
if (show_excluded && s.num_chunks_excluded > 0)
{
QColor c = colorGroup().color(QColorGroup::Mid);
if (curr_ebs.allOn())
drawAllOn(p,c);
else if (s.total_chunks > w)
drawMoreChunksThenPixels(p,curr_ebs,c);
else
drawEqual(p,curr_ebs,c);
}
}
p->restoreWorldMatrix();
}
void ChunkBar::drawEqual(QPainter *p,const BitSet & bs,const QColor & color)
{
//p->setPen(QPen(colorGroup().highlight(),1,Qt::SolidLine));
QColor c = color;
Uint32 w = contentsRect().width();
double scale = 1.0;
Uint32 total_chunks = curr_tc->getStats().total_chunks;
if (curr_tc->getStats().total_chunks != w)
scale = (double)w / total_chunks;
p->setPen(QPen(c,1,Qt::SolidLine));
p->setBrush(c);
QValueList<Range> rs;
for (Uint32 i = 0;i < bs.getNumBits();i++)
{
if (!bs.get(i))
continue;
if (rs.empty())
{
Range r = {i,i,0};
rs.append(r);
}
else
{
Range & l = rs.last();
if (l.last == int(i - 1))
{
l.last = i;
}
else
{
Range r = {i,i,0};
rs.append(r);
}
}
}
QRect r = contentsRect();
for (QValueList<Range>::iterator i = rs.begin();i != rs.end();++i)
{
Range & ra = *i;
int rw = ra.last - ra.first + 1;
p->drawRect((int)(scale * ra.first),0,(int)(rw * scale),r.height());
}
}
void ChunkBar::drawMoreChunksThenPixels(QPainter *p,const BitSet & bs,const QColor & color)
{
Uint32 w = contentsRect().width();
double chunks_per_pixel = (double)bs.getNumBits() / w;
QValueList<Range> rs;
for (Uint32 i = 0;i < w;i++)
{
Uint32 num_dl = 0;
Uint32 jStart = (Uint32) (i*chunks_per_pixel);
Uint32 jEnd = (Uint32) ((i+1)*chunks_per_pixel+0.5);
for (Uint32 j = jStart;j < jEnd;j++)
if (bs.get(j))
num_dl++;
if (num_dl == 0)
continue;
int fac = int(100*((double)num_dl / (jEnd - jStart)) + 0.5);
if (rs.empty())
{
Range r = {i,i,fac};
rs.append(r);
}
else
{
Range & l = rs.last();
if (l.last == int(i - 1) && l.fac == fac)
{
l.last = i;
}
else
{
Range r = {i,i,fac};
rs.append(r);
}
}
}
QRect r = contentsRect();
for (QValueList<Range>::iterator i = rs.begin();i != rs.end();++i)
{
Range & ra = *i;
int rw = ra.last - ra.first + 1;
int fac = ra.fac;
QColor c = color;
if (fac < 100)
{
// do some rounding off
if (fac <= 25)
fac = 25;
else if (fac <= 50)
fac = 45;
else
fac = 65;
c = color.light(200-fac);
}
p->setPen(QPen(c,1,Qt::SolidLine));
p->setBrush(c);
p->drawRect(ra.first,0,rw,r.height());
}
}
void ChunkBar::drawAllOn(QPainter *p,const QColor & color)
{
p->setPen(QPen(color,1,Qt::SolidLine));
p->setBrush(color);
QSize s = contentsRect().size();
p->drawRect(0,0,s.width(),s.height());
}
}
#include "chunkbar.moc"
|