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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
|
/***************************************************************************
* Copyright (C) 2011 by Peter Penz <[email protected]> *
* *
* 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 "kitemlistviewlayouter_p.h"
#include "kitemmodelbase.h"
#include "kitemlistsizehintresolver_p.h"
#include <KDebug>
#define KITEMLISTVIEWLAYOUTER_DEBUG
namespace {
// TODO
const int HeaderHeight = 50;
};
KItemListViewLayouter::KItemListViewLayouter(QObject* parent) :
QObject(parent),
m_dirty(true),
m_visibleIndexesDirty(true),
m_grouped(false),
m_scrollOrientation(Qt::Vertical),
m_size(),
m_itemSize(128, 128),
m_model(0),
m_sizeHintResolver(0),
m_offset(0),
m_maximumOffset(0),
m_firstVisibleIndex(-1),
m_lastVisibleIndex(-1),
m_firstVisibleGroupIndex(-1),
m_columnWidth(0),
m_xPosInc(0),
m_columnCount(0),
m_groups(),
m_groupIndexes(),
m_itemBoundingRects()
{
}
KItemListViewLayouter::~KItemListViewLayouter()
{
}
void KItemListViewLayouter::setScrollOrientation(Qt::Orientation orientation)
{
if (m_scrollOrientation != orientation) {
m_scrollOrientation = orientation;
m_dirty = true;
}
}
Qt::Orientation KItemListViewLayouter::scrollOrientation() const
{
return m_scrollOrientation;
}
void KItemListViewLayouter::setSize(const QSizeF& size)
{
if (m_size != size) {
m_size = size;
m_dirty = true;
}
}
QSizeF KItemListViewLayouter::size() const
{
return m_size;
}
void KItemListViewLayouter::setItemSize(const QSizeF& size)
{
if (m_itemSize != size) {
m_itemSize = size;
m_dirty = true;
}
}
QSizeF KItemListViewLayouter::itemSize() const
{
return m_itemSize;
}
void KItemListViewLayouter::setOffset(qreal offset)
{
if (m_offset != offset) {
m_offset = offset;
m_visibleIndexesDirty = true;
}
}
qreal KItemListViewLayouter::offset() const
{
return m_offset;
}
void KItemListViewLayouter::setModel(const KItemModelBase* model)
{
if (m_model != model) {
m_model = model;
m_dirty = true;
}
}
const KItemModelBase* KItemListViewLayouter::model() const
{
return m_model;
}
void KItemListViewLayouter::setSizeHintResolver(const KItemListSizeHintResolver* sizeHintResolver)
{
if (m_sizeHintResolver != sizeHintResolver) {
m_sizeHintResolver = sizeHintResolver;
m_dirty = true;
}
}
const KItemListSizeHintResolver* KItemListViewLayouter::sizeHintResolver() const
{
return m_sizeHintResolver;
}
qreal KItemListViewLayouter::maximumOffset() const
{
const_cast<KItemListViewLayouter*>(this)->doLayout();
return m_maximumOffset;
}
int KItemListViewLayouter::firstVisibleIndex() const
{
const_cast<KItemListViewLayouter*>(this)->doLayout();
return m_firstVisibleIndex;
}
int KItemListViewLayouter::lastVisibleIndex() const
{
const_cast<KItemListViewLayouter*>(this)->doLayout();
return m_lastVisibleIndex;
}
QRectF KItemListViewLayouter::itemBoundingRect(int index) const
{
const_cast<KItemListViewLayouter*>(this)->doLayout();
if (index < 0 || index >= m_itemBoundingRects.count()) {
return QRectF();
}
if (m_scrollOrientation == Qt::Horizontal) {
// Rotate the logical direction which is always vertical by 90°
// to get the physical horizontal direction
const QRectF& b = m_itemBoundingRects[index];
QRectF bounds(b.y(), b.x(), b.height(), b.width());
QPointF pos = bounds.topLeft();
pos.rx() -= m_offset;
bounds.moveTo(pos);
return bounds;
}
QRectF bounds = m_itemBoundingRects[index];
QPointF pos = bounds.topLeft();
pos.ry() -= m_offset;
bounds.moveTo(pos);
return bounds;
}
int KItemListViewLayouter::maximumVisibleItems() const
{
const_cast<KItemListViewLayouter*>(this)->doLayout();
const int height = static_cast<int>(m_size.height());
const int rowHeight = static_cast<int>(m_itemSize.height());
int rows = height / rowHeight;
if (height % rowHeight != 0) {
++rows;
}
return rows * m_columnCount;
}
int KItemListViewLayouter::itemsPerOffset() const
{
return m_columnCount;
}
bool KItemListViewLayouter::isFirstGroupItem(int itemIndex) const
{
return m_groupIndexes.contains(itemIndex);
}
void KItemListViewLayouter::markAsDirty()
{
m_dirty = true;
}
void KItemListViewLayouter::doLayout()
{
if (m_dirty) {
#ifdef KITEMLISTVIEWLAYOUTER_DEBUG
QElapsedTimer timer;
timer.start();
#endif
m_visibleIndexesDirty = true;
QSizeF itemSize = m_itemSize;
QSizeF size = m_size;
const bool horizontalScrolling = (m_scrollOrientation == Qt::Horizontal);
if (horizontalScrolling) {
itemSize.setWidth(m_itemSize.height());
itemSize.setHeight(m_itemSize.width());
size.setWidth(m_size.height());
size.setHeight(m_size.width());
}
m_columnWidth = itemSize.width();
m_columnCount = qMax(1, int(size.width() / m_columnWidth));
m_xPosInc = 0;
const int itemCount = m_model->count();
if (itemCount > m_columnCount) {
// Apply the unused width equally to each column
const qreal unusedWidth = size.width() - m_columnCount * m_columnWidth;
const qreal columnInc = unusedWidth / (m_columnCount + 1);
m_columnWidth += columnInc;
m_xPosInc += columnInc;
}
int rowCount = itemCount / m_columnCount;
if (itemCount % m_columnCount != 0) {
++rowCount;
}
m_itemBoundingRects.reserve(itemCount);
qreal y = 0;
int rowIndex = 0;
int index = 0;
while (index < itemCount) {
qreal x = m_xPosInc;
qreal maxItemHeight = itemSize.height();
int column = 0;
while (index < itemCount && column < m_columnCount) {
qreal requiredItemHeight = itemSize.height();
if (m_sizeHintResolver) {
const QSizeF sizeHint = m_sizeHintResolver->sizeHint(index);
const qreal sizeHintHeight = horizontalScrolling ? sizeHint.width() : sizeHint.height();
if (sizeHintHeight > requiredItemHeight) {
requiredItemHeight = sizeHintHeight;
}
}
const QRectF bounds(x, y, itemSize.width(), requiredItemHeight);
if (index < m_itemBoundingRects.count()) {
m_itemBoundingRects[index] = bounds;
} else {
m_itemBoundingRects.append(bounds);
}
maxItemHeight = qMax(maxItemHeight, requiredItemHeight);
x += m_columnWidth;
++index;
++column;
}
y += maxItemHeight;
++rowIndex;
}
if (m_itemBoundingRects.count() > itemCount) {
m_itemBoundingRects.erase(m_itemBoundingRects.begin() + itemCount,
m_itemBoundingRects.end());
}
m_maximumOffset = (itemCount > 0) ? m_itemBoundingRects.last().bottom() : 0;
m_grouped = !m_model->groupRole().isEmpty();
/*if (m_grouped) {
createGroupHeaders();
const int lastGroupItemCount = m_model->count() - m_groups.last().firstItemIndex;
m_maximumOffset = m_groups.last().y + (lastGroupItemCount / m_columnCount) * m_rowHeight;
if (lastGroupItemCount % m_columnCount != 0) {
m_maximumOffset += m_rowHeight;
}
} else {*/
// m_maximumOffset = m_minimumRowHeight * rowCount;
//}
#ifdef KITEMLISTVIEWLAYOUTER_DEBUG
kDebug() << "[TIME] doLayout() for " << m_model->count() << "items:" << timer.elapsed();
#endif
m_dirty = false;
}
if (m_grouped) {
updateGroupedVisibleIndexes();
} else {
updateVisibleIndexes();
}
}
void KItemListViewLayouter::updateVisibleIndexes()
{
if (!m_visibleIndexesDirty) {
return;
}
Q_ASSERT(!m_grouped);
Q_ASSERT(!m_dirty);
if (m_model->count() <= 0) {
m_firstVisibleIndex = -1;
m_lastVisibleIndex = -1;
m_visibleIndexesDirty = false;
return;
}
const bool horizontalScrolling = (m_scrollOrientation == Qt::Horizontal);
const int minimumHeight = horizontalScrolling ? m_itemSize.width()
: m_itemSize.height();
// Calculate the first visible index:
// 1. Guess the index by using the minimum row height
const int maxIndex = m_model->count() - 1;
m_firstVisibleIndex = int(m_offset / minimumHeight) * m_columnCount;
// 2. Decrease the index by checking the real row heights
int prevRowIndex = m_firstVisibleIndex - m_columnCount;
while (prevRowIndex > maxIndex) {
prevRowIndex -= m_columnCount;
}
while (prevRowIndex >= 0 && m_itemBoundingRects[prevRowIndex].bottom() >= m_offset) {
m_firstVisibleIndex = prevRowIndex;
prevRowIndex -= m_columnCount;
}
m_firstVisibleIndex = qBound(0, m_firstVisibleIndex, maxIndex);
// Calculate the last visible index
const int visibleHeight = horizontalScrolling ? m_size.width() : m_size.height();
const qreal bottom = m_offset + visibleHeight;
m_lastVisibleIndex = m_firstVisibleIndex; // first visible row, first column
int nextRowIndex = m_lastVisibleIndex + m_columnCount;
while (nextRowIndex <= maxIndex && m_itemBoundingRects[nextRowIndex].y() <= bottom) {
m_lastVisibleIndex = nextRowIndex;
nextRowIndex += m_columnCount;
}
m_lastVisibleIndex += m_columnCount - 1; // move it to the last column
m_lastVisibleIndex = qBound(0, m_lastVisibleIndex, maxIndex);
m_visibleIndexesDirty = false;
}
void KItemListViewLayouter::updateGroupedVisibleIndexes()
{
if (!m_visibleIndexesDirty) {
return;
}
Q_ASSERT(m_grouped);
Q_ASSERT(!m_dirty);
if (m_model->count() <= 0) {
m_firstVisibleIndex = -1;
m_lastVisibleIndex = -1;
m_visibleIndexesDirty = false;
return;
}
// Find the first visible group
const int lastGroupIndex = m_groups.count() - 1;
int groupIndex = lastGroupIndex;
for (int i = 1; i < m_groups.count(); ++i) {
if (m_groups[i].y >= m_offset) {
groupIndex = i - 1;
break;
}
}
// Calculate the first visible index
qreal groupY = m_groups[groupIndex].y;
m_firstVisibleIndex = m_groups[groupIndex].firstItemIndex;
const int invisibleRowCount = int(m_offset - groupY) / int(m_itemSize.height());
m_firstVisibleIndex += invisibleRowCount * m_columnCount;
if (groupIndex + 1 <= lastGroupIndex) {
// Check whether the calculated first visible index remains inside the current
// group. If this is not the case let the first element of the next group be the first
// visible index.
const int nextGroupIndex = m_groups[groupIndex + 1].firstItemIndex;
if (m_firstVisibleIndex > nextGroupIndex) {
m_firstVisibleIndex = nextGroupIndex;
}
}
m_firstVisibleGroupIndex = groupIndex;
const int maxIndex = m_model->count() - 1;
m_firstVisibleIndex = qBound(0, m_firstVisibleIndex, maxIndex);
// Calculate the last visible index: Find group where the last visible item is shown.
const qreal visibleBottom = m_offset + m_size.height(); // TODO: respect Qt::Horizontal alignment
while ((groupIndex < lastGroupIndex) && (m_groups[groupIndex + 1].y < visibleBottom)) {
++groupIndex;
}
groupY = m_groups[groupIndex].y;
m_lastVisibleIndex = m_groups[groupIndex].firstItemIndex;
const int availableHeight = static_cast<int>(visibleBottom - groupY);
int visibleRowCount = availableHeight / int(m_itemSize.height());
if (availableHeight % int(m_itemSize.height()) != 0) {
++visibleRowCount;
}
m_lastVisibleIndex += visibleRowCount * m_columnCount - 1;
if (groupIndex + 1 <= lastGroupIndex) {
// Check whether the calculate last visible index remains inside the current group.
// If this is not the case let the last element of this group be the last visible index.
const int nextGroupIndex = m_groups[groupIndex + 1].firstItemIndex;
if (m_lastVisibleIndex >= nextGroupIndex) {
m_lastVisibleIndex = nextGroupIndex - 1;
}
}
//Q_ASSERT(m_lastVisibleIndex < m_model->count());
m_lastVisibleIndex = qBound(0, m_lastVisibleIndex, maxIndex);
m_visibleIndexesDirty = false;
}
void KItemListViewLayouter::createGroupHeaders()
{
m_groups.clear();
m_groupIndexes.clear();
// TODO:
QList<int> numbers;
numbers << 0 << 5 << 6 << 13 << 20 << 25 << 30 << 35 << 50;
qreal y = 0;
for (int i = 0; i < numbers.count(); ++i) {
if (i > 0) {
const int previousGroupItemCount = numbers[i] - m_groups.last().firstItemIndex;
int previousGroupRowCount = previousGroupItemCount / m_columnCount;
if (previousGroupItemCount % m_columnCount != 0) {
++previousGroupRowCount;
}
const qreal previousGroupHeight = previousGroupRowCount * m_itemSize.height();
y += previousGroupHeight;
}
y += HeaderHeight;
ItemGroup itemGroup;
itemGroup.firstItemIndex = numbers[i];
itemGroup.y = y;
m_groups.append(itemGroup);
m_groupIndexes.insert(itemGroup.firstItemIndex);
}
}
#include "kitemlistviewlayouter_p.moc"
|