]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kitemlistviewlayouter.cpp
Move drawing of textbackground to KItemListWidget
[dolphin.git] / src / kitemviews / kitemlistviewlayouter.cpp
1 /***************************************************************************
2 * Copyright (C) 2011 by Peter Penz <peter.penz19@gmail.com> *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
8 * *
9 * This program 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 *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
18 ***************************************************************************/
19
20 #include "kitemlistviewlayouter_p.h"
21
22 #include "kitemmodelbase.h"
23 #include "kitemlistsizehintresolver_p.h"
24
25 #include <KDebug>
26
27 #define KITEMLISTVIEWLAYOUTER_DEBUG
28
29 namespace {
30 // TODO
31 const int HeaderHeight = 50;
32 };
33
34 KItemListViewLayouter::KItemListViewLayouter(QObject* parent) :
35 QObject(parent),
36 m_dirty(true),
37 m_visibleIndexesDirty(true),
38 m_grouped(false),
39 m_scrollOrientation(Qt::Vertical),
40 m_size(),
41 m_itemSize(128, 128),
42 m_model(0),
43 m_sizeHintResolver(0),
44 m_offset(0),
45 m_maximumOffset(0),
46 m_firstVisibleIndex(-1),
47 m_lastVisibleIndex(-1),
48 m_firstVisibleGroupIndex(-1),
49 m_columnWidth(0),
50 m_xPosInc(0),
51 m_columnCount(0),
52 m_groups(),
53 m_groupIndexes(),
54 m_itemBoundingRects()
55 {
56 }
57
58 KItemListViewLayouter::~KItemListViewLayouter()
59 {
60 }
61
62 void KItemListViewLayouter::setScrollOrientation(Qt::Orientation orientation)
63 {
64 if (m_scrollOrientation != orientation) {
65 m_scrollOrientation = orientation;
66 m_dirty = true;
67 }
68 }
69
70 Qt::Orientation KItemListViewLayouter::scrollOrientation() const
71 {
72 return m_scrollOrientation;
73 }
74
75 void KItemListViewLayouter::setSize(const QSizeF& size)
76 {
77 if (m_size != size) {
78 m_size = size;
79 m_dirty = true;
80 }
81 }
82
83 QSizeF KItemListViewLayouter::size() const
84 {
85 return m_size;
86 }
87
88 void KItemListViewLayouter::setItemSize(const QSizeF& size)
89 {
90 if (m_itemSize != size) {
91 m_itemSize = size;
92 m_dirty = true;
93 }
94 }
95
96 QSizeF KItemListViewLayouter::itemSize() const
97 {
98 return m_itemSize;
99 }
100
101 void KItemListViewLayouter::setOffset(qreal offset)
102 {
103 if (m_offset != offset) {
104 m_offset = offset;
105 m_visibleIndexesDirty = true;
106 }
107 }
108
109 qreal KItemListViewLayouter::offset() const
110 {
111 return m_offset;
112 }
113
114 void KItemListViewLayouter::setModel(const KItemModelBase* model)
115 {
116 if (m_model != model) {
117 m_model = model;
118 m_dirty = true;
119 }
120 }
121
122 const KItemModelBase* KItemListViewLayouter::model() const
123 {
124 return m_model;
125 }
126
127 void KItemListViewLayouter::setSizeHintResolver(const KItemListSizeHintResolver* sizeHintResolver)
128 {
129 if (m_sizeHintResolver != sizeHintResolver) {
130 m_sizeHintResolver = sizeHintResolver;
131 m_dirty = true;
132 }
133 }
134
135 const KItemListSizeHintResolver* KItemListViewLayouter::sizeHintResolver() const
136 {
137 return m_sizeHintResolver;
138 }
139
140 qreal KItemListViewLayouter::maximumOffset() const
141 {
142 const_cast<KItemListViewLayouter*>(this)->doLayout();
143 return m_maximumOffset;
144 }
145
146 int KItemListViewLayouter::firstVisibleIndex() const
147 {
148 const_cast<KItemListViewLayouter*>(this)->doLayout();
149 return m_firstVisibleIndex;
150 }
151
152 int KItemListViewLayouter::lastVisibleIndex() const
153 {
154 const_cast<KItemListViewLayouter*>(this)->doLayout();
155 return m_lastVisibleIndex;
156 }
157
158 QRectF KItemListViewLayouter::itemBoundingRect(int index) const
159 {
160 const_cast<KItemListViewLayouter*>(this)->doLayout();
161 if (index < 0 || index >= m_itemBoundingRects.count()) {
162 return QRectF();
163 }
164
165 if (m_scrollOrientation == Qt::Horizontal) {
166 // Rotate the logical direction which is always vertical by 90°
167 // to get the physical horizontal direction
168 const QRectF& b = m_itemBoundingRects[index];
169 QRectF bounds(b.y(), b.x(), b.height(), b.width());
170 QPointF pos = bounds.topLeft();
171 pos.rx() -= m_offset;
172 bounds.moveTo(pos);
173 return bounds;
174 }
175
176 QRectF bounds = m_itemBoundingRects[index];
177 QPointF pos = bounds.topLeft();
178 pos.ry() -= m_offset;
179 bounds.moveTo(pos);
180 return bounds;
181 }
182
183 int KItemListViewLayouter::maximumVisibleItems() const
184 {
185 const_cast<KItemListViewLayouter*>(this)->doLayout();
186
187 const int height = static_cast<int>(m_size.height());
188 const int rowHeight = static_cast<int>(m_itemSize.height());
189 int rows = height / rowHeight;
190 if (height % rowHeight != 0) {
191 ++rows;
192 }
193
194 return rows * m_columnCount;
195 }
196
197 bool KItemListViewLayouter::isFirstGroupItem(int itemIndex) const
198 {
199 return m_groupIndexes.contains(itemIndex);
200 }
201
202 void KItemListViewLayouter::markAsDirty()
203 {
204 m_dirty = true;
205 }
206
207 void KItemListViewLayouter::doLayout()
208 {
209 if (m_dirty) {
210 #ifdef KITEMLISTVIEWLAYOUTER_DEBUG
211 QElapsedTimer timer;
212 timer.start();
213 #endif
214
215 m_visibleIndexesDirty = true;
216
217 QSizeF itemSize = m_itemSize;
218 QSizeF size = m_size;
219
220 const bool horizontalScrolling = (m_scrollOrientation == Qt::Horizontal);
221 if (horizontalScrolling) {
222 itemSize.setWidth(m_itemSize.height());
223 itemSize.setHeight(m_itemSize.width());
224 size.setWidth(m_size.height());
225 size.setHeight(m_size.width());
226 }
227
228 m_columnWidth = itemSize.width();
229 m_columnCount = qMax(1, int(size.width() / m_columnWidth));
230 m_xPosInc = 0;
231
232 const int itemCount = m_model->count();
233 if (itemCount > m_columnCount) {
234 // Apply the unused width equally to each column
235 const qreal unusedWidth = size.width() - m_columnCount * m_columnWidth;
236 const qreal columnInc = unusedWidth / (m_columnCount + 1);
237 m_columnWidth += columnInc;
238 m_xPosInc += columnInc;
239 }
240
241 int rowCount = itemCount / m_columnCount;
242 if (itemCount % m_columnCount != 0) {
243 ++rowCount;
244 }
245
246 m_itemBoundingRects.reserve(itemCount);
247
248 qreal y = 0;
249 int rowIndex = 0;
250
251 int index = 0;
252 while (index < itemCount) {
253 qreal x = m_xPosInc;
254 qreal maxItemHeight = itemSize.height();
255
256 int column = 0;
257 while (index < itemCount && column < m_columnCount) {
258 qreal requiredItemHeight = itemSize.height();
259 if (m_sizeHintResolver) {
260 const QSizeF sizeHint = m_sizeHintResolver->sizeHint(index);
261 const qreal sizeHintHeight = horizontalScrolling ? sizeHint.width() : sizeHint.height();
262 if (sizeHintHeight > requiredItemHeight) {
263 requiredItemHeight = sizeHintHeight;
264 }
265 }
266
267 const QRectF bounds(x, y, itemSize.width(), requiredItemHeight);
268 if (index < m_itemBoundingRects.count()) {
269 m_itemBoundingRects[index] = bounds;
270 } else {
271 m_itemBoundingRects.append(bounds);
272 }
273
274 maxItemHeight = qMax(maxItemHeight, requiredItemHeight);
275 x += m_columnWidth;
276 ++index;
277 ++column;
278 }
279
280 y += maxItemHeight;
281 ++rowIndex;
282 }
283 if (m_itemBoundingRects.count() > itemCount) {
284 m_itemBoundingRects.erase(m_itemBoundingRects.begin() + itemCount,
285 m_itemBoundingRects.end());
286 }
287
288 m_maximumOffset = (itemCount > 0) ? m_itemBoundingRects.last().bottom() : 0;
289
290 m_grouped = !m_model->groupRole().isEmpty();
291 /*if (m_grouped) {
292 createGroupHeaders();
293
294 const int lastGroupItemCount = m_model->count() - m_groups.last().firstItemIndex;
295 m_maximumOffset = m_groups.last().y + (lastGroupItemCount / m_columnCount) * m_rowHeight;
296 if (lastGroupItemCount % m_columnCount != 0) {
297 m_maximumOffset += m_rowHeight;
298 }
299 } else {*/
300 // m_maximumOffset = m_minimumRowHeight * rowCount;
301 //}
302
303 #ifdef KITEMLISTVIEWLAYOUTER_DEBUG
304 kDebug() << "[TIME] doLayout() for " << m_model->count() << "items:" << timer.elapsed();
305 #endif
306 m_dirty = false;
307 }
308
309 if (m_grouped) {
310 updateGroupedVisibleIndexes();
311 } else {
312 updateVisibleIndexes();
313 }
314 }
315
316 void KItemListViewLayouter::updateVisibleIndexes()
317 {
318 if (!m_visibleIndexesDirty) {
319 return;
320 }
321
322 Q_ASSERT(!m_grouped);
323 Q_ASSERT(!m_dirty);
324
325 if (m_model->count() <= 0) {
326 m_firstVisibleIndex = -1;
327 m_lastVisibleIndex = -1;
328 m_visibleIndexesDirty = false;
329 return;
330 }
331
332 const bool horizontalScrolling = (m_scrollOrientation == Qt::Horizontal);
333 const int minimumHeight = horizontalScrolling ? m_itemSize.width()
334 : m_itemSize.height();
335
336 // Calculate the first visible index:
337 // 1. Guess the index by using the minimum row height
338 const int maxIndex = m_model->count() - 1;
339 m_firstVisibleIndex = int(m_offset / minimumHeight) * m_columnCount;
340
341 // 2. Decrease the index by checking the real row heights
342 int prevRowIndex = m_firstVisibleIndex - m_columnCount;
343 while (prevRowIndex > maxIndex) {
344 prevRowIndex -= m_columnCount;
345 }
346
347 while (prevRowIndex >= 0 && m_itemBoundingRects[prevRowIndex].bottom() >= m_offset) {
348 m_firstVisibleIndex = prevRowIndex;
349 prevRowIndex -= m_columnCount;
350 }
351 m_firstVisibleIndex = qBound(0, m_firstVisibleIndex, maxIndex);
352
353 // Calculate the last visible index
354 const int visibleHeight = horizontalScrolling ? m_size.width() : m_size.height();
355 const qreal bottom = m_offset + visibleHeight;
356 m_lastVisibleIndex = m_firstVisibleIndex; // first visible row, first column
357 int nextRowIndex = m_lastVisibleIndex + m_columnCount;
358 while (nextRowIndex <= maxIndex && m_itemBoundingRects[nextRowIndex].y() <= bottom) {
359 m_lastVisibleIndex = nextRowIndex;
360 nextRowIndex += m_columnCount;
361 }
362 m_lastVisibleIndex += m_columnCount - 1; // move it to the last column
363 m_lastVisibleIndex = qBound(0, m_lastVisibleIndex, maxIndex);
364
365 m_visibleIndexesDirty = false;
366 }
367
368 void KItemListViewLayouter::updateGroupedVisibleIndexes()
369 {
370 if (!m_visibleIndexesDirty) {
371 return;
372 }
373
374 Q_ASSERT(m_grouped);
375 Q_ASSERT(!m_dirty);
376
377 if (m_model->count() <= 0) {
378 m_firstVisibleIndex = -1;
379 m_lastVisibleIndex = -1;
380 m_visibleIndexesDirty = false;
381 return;
382 }
383
384 // Find the first visible group
385 const int lastGroupIndex = m_groups.count() - 1;
386 int groupIndex = lastGroupIndex;
387 for (int i = 1; i < m_groups.count(); ++i) {
388 if (m_groups[i].y >= m_offset) {
389 groupIndex = i - 1;
390 break;
391 }
392 }
393
394 // Calculate the first visible index
395 qreal groupY = m_groups[groupIndex].y;
396 m_firstVisibleIndex = m_groups[groupIndex].firstItemIndex;
397 const int invisibleRowCount = int(m_offset - groupY) / int(m_itemSize.height());
398 m_firstVisibleIndex += invisibleRowCount * m_columnCount;
399 if (groupIndex + 1 <= lastGroupIndex) {
400 // Check whether the calculated first visible index remains inside the current
401 // group. If this is not the case let the first element of the next group be the first
402 // visible index.
403 const int nextGroupIndex = m_groups[groupIndex + 1].firstItemIndex;
404 if (m_firstVisibleIndex > nextGroupIndex) {
405 m_firstVisibleIndex = nextGroupIndex;
406 }
407 }
408
409 m_firstVisibleGroupIndex = groupIndex;
410
411 const int maxIndex = m_model->count() - 1;
412 m_firstVisibleIndex = qBound(0, m_firstVisibleIndex, maxIndex);
413
414 // Calculate the last visible index: Find group where the last visible item is shown.
415 const qreal visibleBottom = m_offset + m_size.height(); // TODO: respect Qt::Horizontal alignment
416 while ((groupIndex < lastGroupIndex) && (m_groups[groupIndex + 1].y < visibleBottom)) {
417 ++groupIndex;
418 }
419
420 groupY = m_groups[groupIndex].y;
421 m_lastVisibleIndex = m_groups[groupIndex].firstItemIndex;
422 const int availableHeight = static_cast<int>(visibleBottom - groupY);
423 int visibleRowCount = availableHeight / int(m_itemSize.height());
424 if (availableHeight % int(m_itemSize.height()) != 0) {
425 ++visibleRowCount;
426 }
427 m_lastVisibleIndex += visibleRowCount * m_columnCount - 1;
428
429 if (groupIndex + 1 <= lastGroupIndex) {
430 // Check whether the calculate last visible index remains inside the current group.
431 // If this is not the case let the last element of this group be the last visible index.
432 const int nextGroupIndex = m_groups[groupIndex + 1].firstItemIndex;
433 if (m_lastVisibleIndex >= nextGroupIndex) {
434 m_lastVisibleIndex = nextGroupIndex - 1;
435 }
436 }
437 //Q_ASSERT(m_lastVisibleIndex < m_model->count());
438 m_lastVisibleIndex = qBound(0, m_lastVisibleIndex, maxIndex);
439
440 m_visibleIndexesDirty = false;
441 }
442
443 void KItemListViewLayouter::createGroupHeaders()
444 {
445 m_groups.clear();
446 m_groupIndexes.clear();
447
448 // TODO:
449 QList<int> numbers;
450 numbers << 0 << 5 << 6 << 13 << 20 << 25 << 30 << 35 << 50;
451
452 qreal y = 0;
453 for (int i = 0; i < numbers.count(); ++i) {
454 if (i > 0) {
455 const int previousGroupItemCount = numbers[i] - m_groups.last().firstItemIndex;
456 int previousGroupRowCount = previousGroupItemCount / m_columnCount;
457 if (previousGroupItemCount % m_columnCount != 0) {
458 ++previousGroupRowCount;
459 }
460 const qreal previousGroupHeight = previousGroupRowCount * m_itemSize.height();
461 y += previousGroupHeight;
462 }
463 y += HeaderHeight;
464
465 ItemGroup itemGroup;
466 itemGroup.firstItemIndex = numbers[i];
467 itemGroup.y = y;
468
469 m_groups.append(itemGroup);
470 m_groupIndexes.insert(itemGroup.firstItemIndex);
471 }
472 }
473
474 #include "kitemlistviewlayouter_p.moc"