QmlMaterial 0.1.0
Loading...
Searching...
No Matches
masonry_layout_p.hpp
1#pragma once
2
3#include "lazy_view_layout_p.hpp"
4#include "ordered_extent_index_p.hpp"
5#include <QHash>
6#include <QSet>
7#include <QString>
8#include <QVector>
9#include <limits>
10#include <set>
11
12namespace qml_material
13{
14class MasonryViewLayout final : public LazyViewLayout {
15public:
16 int fixedSlotCount = 0;
17 qreal minimum = 140, estimate = 140, mainSpacing = 0, crossSpacing = 0;
18 bool mirrored = false;
19
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;
33 }
34 void clear() override {
35 m_snapshot.reset();
36 m_locations.clear();
37 m_lanes.clear();
38 m_cache.clear();
39 m_bound.clear();
40 m_columns = {};
41 }
42 Result apply(ItemSnapshotPtr next, qreal cross, qreal position, qreal) override {
43 if (! next || ! std::isfinite(position)) return { false, position };
44 GridLayout columns;
45 if (! columns.reset(next->count(),
46 cross,
47 fixedSlotCount,
48 minimum,
49 estimate,
50 mainSpacing,
51 crossSpacing,
52 mirrored))
53 return { false, position };
54 const auto anchor =
55 ItemAnchor::capture(m_snapshot, next, indexAt(position), position, [this](int index) {
56 return offset(index);
57 });
58 const bool sameColumns = m_snapshot &&
59 m_columns.fixedSlotCount() == columns.fixedSlotCount() &&
60 m_columns.cellExtent() == columns.cellExtent();
61 const bool stable =
62 m_snapshot && ((m_snapshot->stableKeys() && next->stableKeys()) || m_snapshot == next);
63 int prefix = 0;
64 if (sameColumns && stable) {
65 while (prefix < std::min(count(), next->count()) &&
66 m_snapshot->descriptorAt(prefix).key == next->descriptorAt(prefix).key)
67 ++prefix;
68 }
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;
76 QSet<QString> bound;
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);
87 }
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);
95 }
96 m_snapshot = std::move(next);
97 m_columns = columns;
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 };
106 }
107 Result measure(int index, qreal value, qreal position) override {
108 if (index < 0 || index >= count() || ! std::isfinite(position) || ! std::isfinite(value) ||
109 value < 0)
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];
115 int lane = oldLane;
116 if (! m_bound.contains(row.key)) {
117 // Estimates do not commit a lane until the item has actually been measured.
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) {
122 shortest = end;
123 lane = candidate;
124 }
125 }
126 }
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;
131 }
132 m_cache.insert(row.key, { value, row.reuseType, row.contentRevision });
133 m_bound.insert(row.key);
134 return { true, position + offset(anchor) - before };
135 }
136 qreal offset(int index) const override {
137 return index >= 0 && index < count() ? m_lanes[m_locations[index]].prefix(index) : 0;
138 }
139 qreal extent(int index) const override {
140 return index >= 0 && index < count() ? m_lanes[m_locations[index]].extent(index) : 0;
141 }
142 qreal totalExtent() const override {
143 qreal result = 0;
144 for (const auto& lane : m_lanes) result = std::max(result, lane.totalExtent());
145 return result;
146 }
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) };
151 }
152 int indexAt(qreal position) const override {
153 int result = -1;
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;
159 // Leading spacing belongs to the next row for anchoring, but not visibility.
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)))) {
166 result = candidate;
167 distance = delta;
168 intersects = contains;
169 }
170 }
171 return result;
172 }
173 QVector<int> indices(qreal begin, qreal end) const {
174 QVector<int> result;
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());
179 return result;
180 }
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() };
184 }
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);
194 }
195 }
196 for (int index :
197 lane.intersecting(position + viewport, position + viewport + cache, 128)) {
198 if (! selected.contains(index)) {
199 candidates.append(index);
200 selected.insert(index);
201 }
202 }
203 }
204 const auto distance = [&](int index) {
205 return std::max({ position - offset(index) - extent(index),
206 offset(index) - position - viewport,
207 qreal(0) });
208 };
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);
212 });
213 if (candidates.size() > 256) candidates.resize(256);
214 requested.append(candidates);
215 std::sort(requested.begin(), requested.end());
216 return requested;
217 }
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() };
221 }
222 ItemRange demandRange(qreal position, qreal viewport, qreal cache) const override {
223 return requestedRange(position, viewport, cache);
224 }
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;
229 return false;
230 }
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];
234 if (lineDelta) {
235 int result = 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);
239 if (next < 0) break;
240 result = next;
241 }
242 return result;
243 }
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;
248 }
249
250private:
251 struct Measurement {
252 qreal extent;
253 QString type;
254 quint64 revision;
255 };
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;
262};
263} // namespace qml_material