3#include "lazy_view_layout_p.hpp"
4#include "ordered_extent_index_p.hpp"
14class MasonryViewLayout final :
public LazyViewLayout {
16 int fixedSlotCount = 0;
17 qreal minimum = 140, estimate = 140, mainSpacing = 0, crossSpacing = 0;
18 bool mirrored =
false;
20 int slotCount()
const {
return m_columns.fixedSlotCount(); }
21 qreal cellExtent()
const {
return m_columns.cellExtent(); }
22 bool supportsOrientation(Qt::Orientation value)
const override {
return value == Qt::Vertical; }
23 bool discreteItems()
const override {
return true; }
24 ItemSnapshotPtr snapshot()
const override {
return m_snapshot; }
25 int count()
const override {
return m_locations.size(); }
26 bool estimated()
const override {
return m_cache.size() < count(); }
27 bool needsMeasurement(
int index)
const override {
28 if (! m_snapshot || index < 0 || index >= count())
return true;
29 const auto& row = m_snapshot->descriptorAt(index);
30 const auto measured = m_cache.constFind(row.key);
31 return ! m_bound.contains(row.key) || measured == m_cache.cend() ||
32 measured->type != row.reuseType || measured->revision != row.contentRevision;
34 void clear()
override {
42 Result apply(ItemSnapshotPtr next, qreal cross, qreal position, qreal)
override {
43 if (! next || ! std::isfinite(position))
return {
false, position };
45 if (! columns.reset(next->count(),
53 return {
false, position };
55 ItemAnchor::capture(m_snapshot, next, indexAt(position), position, [
this](
int index) {
58 const bool sameColumns = m_snapshot &&
59 m_columns.fixedSlotCount() == columns.fixedSlotCount() &&
60 m_columns.cellExtent() == columns.cellExtent();
62 m_snapshot && ((m_snapshot->stableKeys() && next->stableKeys()) || m_snapshot == next);
64 if (sameColumns && stable) {
65 while (prefix < std::min(count(), next->count()) &&
66 m_snapshot->descriptorAt(prefix).key == next->descriptorAt(prefix).key)
69 const int laneCount = std::min(columns.fixedSlotCount(), std::max(1, next->count()));
70 std::vector<OrderedExtentIndex> lanes;
71 lanes.reserve(laneCount);
72 for (
int i = 0; i < laneCount; ++i) lanes.emplace_back(mainSpacing);
73 QVector<int> locations;
74 locations.reserve(next->count());
75 QHash<QString, Measurement> cache;
77 std::set<std::pair<qreal, int>> frontier;
78 for (
int lane = 0; lane < laneCount; ++lane) frontier.emplace(0, lane);
79 for (
int i = 0; i < next->count(); ++i) {
80 const auto& row = next->descriptorAt(i);
81 qreal height = estimate;
82 const auto measured = m_cache.constFind(row.key);
83 if (sameColumns && stable && measured != m_cache.cend() &&
84 measured->type == row.reuseType && measured->revision == row.contentRevision) {
85 height = measured->extent;
86 cache.insert(row.key, *measured);
88 const bool keepLane = i < prefix && m_bound.contains(row.key);
89 const int lane = keepLane ? m_locations[i] : frontier.begin()->second;
90 frontier.erase({ lanes[lane].prefix(i), lane });
91 if (! lanes[lane].set(i, height))
return {
false, position };
92 locations.append(lane);
93 if (keepLane) bound.insert(row.key);
94 frontier.emplace(lanes[lane].prefix(i + 1), lane);
96 m_snapshot = std::move(next);
98 m_lanes = std::move(lanes);
99 m_locations = std::move(locations);
100 m_cache = std::move(cache);
101 m_bound = std::move(bound);
102 qreal corrected = count() ? position : 0;
103 if (! anchor.key.isEmpty())
104 corrected = offset(m_snapshot->indexOfKey(anchor.key)) - anchor.screenOffset;
105 return {
true, corrected };
107 Result measure(
int index, qreal value, qreal position)
override {
108 if (index < 0 || index >= count() || ! std::isfinite(position) || ! std::isfinite(value) ||
110 return {
false, position };
111 const int anchor = indexAt(position);
112 const qreal before = offset(anchor);
113 const auto& row = m_snapshot->descriptorAt(index);
114 const int oldLane = m_locations[index];
116 if (! m_bound.contains(row.key)) {
118 qreal shortest = std::numeric_limits<qreal>::infinity();
119 for (
int candidate = 0; candidate < int(m_lanes.size()); ++candidate) {
120 const qreal end = m_lanes[candidate].prefix(index);
121 if (end < shortest) {
127 if (! m_lanes[lane].set(index, value))
return {
false, position };
128 if (lane != oldLane) {
129 m_lanes[oldLane].erase(index);
130 m_locations[index] = lane;
132 m_cache.insert(row.key, { value, row.reuseType, row.contentRevision });
133 m_bound.insert(row.key);
134 return {
true, position + offset(anchor) - before };
136 qreal offset(
int index)
const override {
137 return index >= 0 && index < count() ? m_lanes[m_locations[index]].prefix(index) : 0;
139 qreal extent(
int index)
const override {
140 return index >= 0 && index < count() ? m_lanes[m_locations[index]].extent(index) : 0;
142 qreal totalExtent()
const override {
144 for (
const auto& lane : m_lanes) result = std::max(result, lane.totalExtent());
147 QRectF rect(
int index)
const override {
148 if (index < 0 || index >= count())
return {};
149 const auto cell = m_columns.rect(m_locations[index]);
150 return { cell.x(), offset(index), cell.width(), extent(index) };
152 int indexAt(qreal position)
const override {
154 bool intersects =
false;
155 qreal distance = std::numeric_limits<qreal>::infinity();
156 for (
const auto& lane : m_lanes) {
157 const int candidate = lane.indexAt(position);
158 if (candidate < 0)
continue;
160 const bool contains = offset(candidate) <= position + mainSpacing &&
161 offset(candidate) + extent(candidate) > position;
162 const qreal delta = std::abs(offset(candidate) - position);
163 if ((contains && ! intersects) ||
164 (contains == intersects &&
165 (delta < distance || (delta == distance && candidate < result)))) {
168 intersects = contains;
173 QVector<int> indices(qreal begin, qreal end)
const {
175 if (cellExtent() <= 0)
return result;
176 for (
const auto& lane : m_lanes)
177 for (
int index : lane.intersecting(begin, end)) result.append(index);
178 std::sort(result.begin(), result.end());
181 ItemRange visibleRange(qreal begin, qreal end)
const override {
182 const auto items = indices(begin, end);
183 return items.isEmpty() ? ItemRange {} : ItemRange { items.first(), items.last() };
185 QVector<int> requestedIndices(qreal position, qreal viewport, qreal cache)
const override {
186 auto requested = indices(position, position + viewport);
187 QSet<int> selected(requested.begin(), requested.end());
188 QVector<int> candidates;
189 for (
const auto& lane : m_lanes) {
190 for (
int index : lane.intersecting(position - cache, position, 128,
true)) {
191 if (! selected.contains(index)) {
192 candidates.append(index);
193 selected.insert(index);
197 lane.intersecting(position + viewport, position + viewport + cache, 128)) {
198 if (! selected.contains(index)) {
199 candidates.append(index);
200 selected.insert(index);
204 const auto distance = [&](
int index) {
205 return std::max({ position - offset(index) - extent(index),
206 offset(index) - position - viewport,
209 std::sort(candidates.begin(), candidates.end(), [&](
int a,
int b) {
210 const qreal da = distance(a), db = distance(b);
211 return da < db || (da == db && a < b);
213 if (candidates.size() > 256) candidates.resize(256);
214 requested.append(candidates);
215 std::sort(requested.begin(), requested.end());
218 ItemRange requestedRange(qreal position, qreal viewport, qreal cache)
const override {
219 const auto items = requestedIndices(position, viewport, cache);
220 return items.isEmpty() ? ItemRange {} : ItemRange { items.first(), items.last() };
222 ItemRange demandRange(qreal position, qreal viewport, qreal cache)
const override {
223 return requestedRange(position, viewport, cache);
225 bool reachesEnd(qreal position, qreal viewport, qreal cache)
const override {
226 if (! count())
return true;
227 for (
const auto& lane : m_lanes)
228 if (lane.totalExtent() <= position + viewport + cache)
return true;
231 int neighbor(
int index,
int lineDelta,
int slotDelta)
const override {
232 if (index < 0 || index >= count())
return count() ? 0 : -1;
233 const int sourceLane = m_locations[index];
236 for (qint64 i = 0; i < std::abs(qint64(lineDelta)); ++i) {
237 const int next = lineDelta > 0 ? m_lanes[sourceLane].next(result)
238 : m_lanes[sourceLane].previous(result);
244 const int lane = sourceLane + (mirrored ? -slotDelta : slotDelta);
245 if (lane < 0 || lane >=
int(m_lanes.size()))
return index;
246 const int target = m_lanes[lane].nearestCenter(offset(index) + extent(index) / 2);
247 return target >= 0 ? target : index;
256 ItemSnapshotPtr m_snapshot;
257 GridLayout m_columns;
258 std::vector<OrderedExtentIndex> m_lanes;
259 QVector<int> m_locations;
260 QHash<QString, Measurement> m_cache;
261 QSet<QString> m_bound;