-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathtiledlistview.cpp
383 lines (336 loc) · 12.5 KB
/
tiledlistview.cpp
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
/*
Copyright (c) 2009-10 Qtrac Ltd. All rights reserved.
This program or module 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 3 of
the License, or (at your option) any later version. It is provided
for educational purposes and 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.
*/
#include "tiledlistview.hpp"
#include <QApplication>
#include <QPainter>
#include <QPaintEvent>
#include <QScrollBar>
#include <QStyleOptionViewItem>
namespace {
const int ExtraHeight = 3;
}
TiledListView::TiledListView(QWidget *parent)
: QAbstractItemView(parent), idealWidth(0), idealHeight(0),
hashIsDirty(false)
{
setFocusPolicy(Qt::WheelFocus);
setFont(QApplication::font("QListView"));
horizontalScrollBar()->setRange(0, 0);
verticalScrollBar()->setRange(0, 0);
}
void TiledListView::setModel(QAbstractItemModel *model)
{
QAbstractItemView::setModel(model);
hashIsDirty = true;
}
#ifndef SQL_FRIENDLY
void TiledListView::calculateRectsIfNecessary() const
{
if (!hashIsDirty)
return;
const int ExtraWidth = 10;
QFontMetrics fm(font());
const int RowHeight = fm.height() + ExtraHeight;
const int MaxWidth = viewport()->width();
int minimumWidth = 0;
int x = 0;
int y = 0;
for (int row = 0; row < model()->rowCount(rootIndex()); ++row) {
QModelIndex index = model()->index(row, 0, rootIndex());
QString text = model()->data(index).toString();
int textWidth = fm.width(text);
if (!(x == 0 || x + textWidth + ExtraWidth < MaxWidth)) {
y += RowHeight;
x = 0;
}
else if (x != 0)
x += ExtraWidth;
rectForRow[row] = QRectF(x, y, textWidth + ExtraWidth,
RowHeight);
if (textWidth > minimumWidth)
minimumWidth = textWidth;
x += textWidth;
}
idealWidth = minimumWidth + ExtraWidth;
idealHeight = y + RowHeight;
hashIsDirty = false;
viewport()->update();
}
#else
// This is only needed if we use a SQL-based model for a database that
// doesn't report its query size.
void TiledListView::calculateRectsIfNecessary() const
{
if (!hashIsDirty)
return;
const int ExtraWidth = 10;
QFontMetrics fm(font());
const int RowHeight = fm.height() + ExtraHeight;
const int MaxWidth = viewport()->width();
int minimumWidth = 0;
int x = 0;
int y = 0;
int row = 0;
forever {
for (; row < model()->rowCount(rootIndex()); ++row) {
QModelIndex index = model()->index(row, 0, rootIndex());
QString text = model()->data(index).toString();
int textWidth = fm.width(text);
if (!(x == 0 || x + textWidth + ExtraWidth < MaxWidth)) {
y += RowHeight;
x = 0;
}
else if (x != 0)
x += ExtraWidth;
rectForRow[row] = QRectF(x, y, textWidth + ExtraWidth,
RowHeight);
if (textWidth > minimumWidth)
minimumWidth = textWidth;
x += textWidth;
}
if (!model()->canFetchMore(rootIndex())) {
break;
}
model()->fetchMore(rootIndex());
}
idealWidth = minimumWidth + ExtraWidth;
idealHeight = y + RowHeight;
hashIsDirty = false;
viewport()->update();
}
#endif
QRect TiledListView::visualRect(const QModelIndex &index) const
{
QRect rect;
if (index.isValid())
rect = viewportRectForRow(index.row()).toRect();
return rect;
}
QRectF TiledListView::viewportRectForRow(int row) const
{
calculateRectsIfNecessary();
QRectF rect = rectForRow.value(row).toRect();
if (!rect.isValid())
return rect;
return QRectF(rect.x() - horizontalScrollBar()->value(),
rect.y() - verticalScrollBar()->value(),
rect.width(), rect.height());
}
void TiledListView::scrollTo(const QModelIndex &index,
QAbstractItemView::ScrollHint)
{
QRect viewRect = viewport()->rect();
QRect itemRect = visualRect(index);
if (itemRect.left() < viewRect.left())
horizontalScrollBar()->setValue(horizontalScrollBar()->value()
+ itemRect.left() - viewRect.left());
else if (itemRect.right() > viewRect.right())
horizontalScrollBar()->setValue(horizontalScrollBar()->value()
+ qMin(itemRect.right() - viewRect.right(),
itemRect.left() - viewRect.left()));
if (itemRect.top() < viewRect.top())
verticalScrollBar()->setValue(verticalScrollBar()->value() +
itemRect.top() - viewRect.top());
else if (itemRect.bottom() > viewRect.bottom())
verticalScrollBar()->setValue(verticalScrollBar()->value() +
qMin(itemRect.bottom() - viewRect.bottom(),
itemRect.top() - viewRect.top()));
viewport()->update();
}
QModelIndex TiledListView::indexAt(const QPoint &point_) const
{
QPoint point(point_);
point.rx() += horizontalScrollBar()->value();
point.ry() += verticalScrollBar()->value();
calculateRectsIfNecessary();
QHashIterator<int, QRectF> i(rectForRow);
while (i.hasNext()) {
i.next();
if (i.value().contains(point))
return model()->index(i.key(), 0, rootIndex());
}
return QModelIndex();
}
void TiledListView::dataChanged(const QModelIndex &topLeft,
const QModelIndex &bottomRight)
{
hashIsDirty = true;
QAbstractItemView::dataChanged(topLeft, bottomRight);
}
void TiledListView::rowsInserted(const QModelIndex &parent, int start,
int end)
{
hashIsDirty = true;
QAbstractItemView::rowsInserted(parent, start, end);
}
void TiledListView::rowsAboutToBeRemoved(const QModelIndex &parent,
int start, int end)
{
hashIsDirty = true;
QAbstractItemView::rowsAboutToBeRemoved(parent, start, end);
}
// BUG: If the focus goes to the TiledListView by default, e.g., not by
// tabbing or clicking, but because there are no other focus-accepting
// widgets---i.e., because they are hidden, then the only arrow keys
// that work are Up and Down (and they move prev and next row). Once the
// TiledListView is clicked though (or the Tab key pressed), the arrow
// keys work correctly, i.e., as coded below.
// Reimplementing keyPressEvent(), or even event() does not solve this.
// In practice this is very unlikely to be a problem since it would be
// rare to have a real application where the TiledListView was the only
// focus-accepting widget in a window.
QModelIndex TiledListView::moveCursor(
QAbstractItemView::CursorAction cursorAction,
Qt::KeyboardModifiers)
{
QModelIndex index = currentIndex();
if (index.isValid()) {
if ((cursorAction == MoveLeft && index.row() > 0) ||
(cursorAction == MoveRight &&
index.row() + 1 < model()->rowCount())) {
const int offset = (cursorAction == MoveLeft ? -1 : 1);
index = model()->index(index.row() + offset,
index.column(), index.parent());
}
else if ((cursorAction == MoveUp && index.row() > 0) ||
(cursorAction == MoveDown &&
index.row() + 1 < model()->rowCount())) {
QFontMetrics fm(font());
const int RowHeight = (fm.height() + ExtraHeight) *
(cursorAction == MoveUp ? -1 : 1);
QRect rect = viewportRectForRow(index.row()).toRect();
QPoint point(rect.center().x(),
rect.center().y() + RowHeight);
while (point.x() >= 0) {
index = indexAt(point);
if (index.isValid())
break;
point.rx() -= fm.width("n");
}
}
}
return index;
}
int TiledListView::horizontalOffset() const
{
return horizontalScrollBar()->value();
}
int TiledListView::verticalOffset() const
{
return verticalScrollBar()->value();
}
void TiledListView::scrollContentsBy(int dx, int dy)
{
scrollDirtyRegion(dx, dy);
viewport()->scroll(dx, dy);
}
void TiledListView::setSelection(const QRect &rect,
QFlags<QItemSelectionModel::SelectionFlag> flags)
{
QRect rectangle = rect.translated(horizontalScrollBar()->value(),
verticalScrollBar()->value()).normalized();
calculateRectsIfNecessary();
QHashIterator<int, QRectF> i(rectForRow);
int firstRow = model()->rowCount();
int lastRow = -1;
while (i.hasNext()) {
i.next();
if (i.value().intersects(rectangle)) {
firstRow = firstRow < i.key() ? firstRow : i.key();
lastRow = lastRow > i.key() ? lastRow : i.key();
}
}
if (firstRow != model()->rowCount() && lastRow != -1) {
QItemSelection selection(
model()->index(firstRow, 0, rootIndex()),
model()->index(lastRow, 0, rootIndex()));
selectionModel()->select(selection, flags);
}
else {
QModelIndex invalid;
QItemSelection selection(invalid, invalid);
selectionModel()->select(selection, flags);
}
}
QRegion TiledListView::visualRegionForSelection(
const QItemSelection &selection) const
{
QRegion region;
foreach (const QItemSelectionRange &range, selection) {
for (int row = range.top(); row <= range.bottom(); ++row) {
for (int column = range.left(); column < range.right();
++column) {
QModelIndex index = model()->index(row, column,
rootIndex());
region += visualRect(index);
}
}
}
return region;
}
void TiledListView::paintEvent(QPaintEvent*)
{
QPainter painter(viewport());
painter.setRenderHints(QPainter::Antialiasing|
QPainter::TextAntialiasing);
for (int row = 0; row < model()->rowCount(rootIndex()); ++row) {
QModelIndex index = model()->index(row, 0, rootIndex());
QRectF rect = viewportRectForRow(row);
if (!rect.isValid() || rect.bottom() < 0 ||
rect.y() > viewport()->height())
continue;
QStyleOptionViewItem option = viewOptions();
option.rect = rect.toRect();
if (selectionModel()->isSelected(index))
option.state |= QStyle::State_Selected;
if (currentIndex() == index)
option.state |= QStyle::State_HasFocus;
itemDelegate()->paint(&painter, option, index);
paintOutline(&painter, rect);
}
}
void TiledListView::paintOutline(QPainter *painter,
const QRectF &rectangle)
{
const QRectF rect = rectangle.adjusted(0, 0, -1, -1);
painter->save();
painter->setPen(QPen(palette().dark().color(), 0.5));
painter->drawRect(rect);
painter->setPen(QPen(Qt::black, 0.5));
painter->drawLine(rect.bottomLeft(), rect.bottomRight());
painter->drawLine(rect.bottomRight(), rect.topRight());
painter->restore();
}
void TiledListView::resizeEvent(QResizeEvent*)
{
hashIsDirty = true;
calculateRectsIfNecessary();
updateGeometries();
}
void TiledListView::updateGeometries()
{
QFontMetrics fm(font());
const int RowHeight = fm.height() + ExtraHeight;
horizontalScrollBar()->setSingleStep(fm.width("n"));
horizontalScrollBar()->setPageStep(viewport()->width());
horizontalScrollBar()->setRange(0,
qMax(0, idealWidth - viewport()->width()));
verticalScrollBar()->setSingleStep(RowHeight);
verticalScrollBar()->setPageStep(viewport()->height());
verticalScrollBar()->setRange(0,
qMax(0, idealHeight - viewport()->height()));
}
void TiledListView::mousePressEvent(QMouseEvent *event)
{
QAbstractItemView::mousePressEvent(event);
setCurrentIndex(indexAt(event->pos()));
}