QmlMaterial 0.1.0
Loading...
Searching...
No Matches
grid_layout_p.hpp
1#pragma once
2
3#include <QRectF>
4#include <Qt>
5#include <algorithm>
6#include <cmath>
7#include <limits>
8
9namespace qml_material
10{
11struct ItemRange {
12 int first = -1;
13 int last = -1;
14};
15
16class GridLayout {
17public:
18 bool reset(int count, qreal crossSize, int fixedSlotCount, qreal minimum, qreal extent,
19 qreal mainSpacing, qreal crossSpacing, bool mirrored, bool reversed = false) {
20 if (count < 0 || fixedSlotCount < 0 || ! std::isfinite(crossSize) || crossSize < 0 ||
21 ! std::isfinite(minimum) || minimum <= 0 || ! std::isfinite(extent) || extent <= 0 ||
22 ! std::isfinite(mainSpacing) || mainSpacing < 0 || ! std::isfinite(crossSpacing) ||
23 crossSpacing < 0)
24 return false;
25 const qreal adaptive = std::floor((crossSize + crossSpacing) / (minimum + crossSpacing));
26 const int resolved =
27 fixedSlotCount > 0
28 ? fixedSlotCount
29 : int(std::clamp(adaptive, qreal(1), qreal(std::numeric_limits<int>::max())));
30 // Fixed slot counts still fit narrow containers by reducing the effective gap.
31 const qreal gap = resolved > 1 ? std::min(crossSpacing, crossSize / resolved) : 0;
32 const qreal cell = (crossSize - (resolved - 1) * gap) / resolved;
33 const qint64 lines = (qint64(count) + resolved - 1) / resolved;
34 const qreal total = lines ? lines * extent + (lines - 1) * mainSpacing : 0;
35 if (! std::isfinite(total) || ! std::isfinite(cell)) return false;
36 m_count = count;
37 m_cross = crossSize;
38 m_slots = resolved;
39 m_cell = cell;
40 m_extent = extent;
41 m_mainSpacing = mainSpacing;
42 m_crossSpacing = gap;
43 m_total = total;
44 m_mirrored = mirrored;
45 m_reversed = reversed;
46 return true;
47 }
48 int count() const { return m_count; }
49 int fixedSlotCount() const { return m_slots; }
50 qreal cellExtent() const { return m_cell; }
51 qreal crossSize() const { return m_cross; }
52 qreal totalExtent() const { return m_total; }
53 qreal offset(int index) const {
54 const auto start = rawOffset(index);
55 return m_reversed ? m_total - start - m_extent : start;
56 }
57 bool reversed() const { return m_reversed; }
58 qreal extent(int) const { return m_extent; }
59 QRectF rect(int index) const {
60 const int slot = index % m_slots;
61 const qreal start = slot * (m_cell + m_crossSpacing);
62 return { m_mirrored ? m_cross - start - m_cell : start, offset(index), m_cell, m_extent };
63 }
64 int indexAt(qreal position) const {
65 if (! m_count || ! std::isfinite(position)) return -1;
66 if (m_reversed) position = m_total - position;
67 return rawIndexAt(position);
68 }
69 int rawIndexAt(qreal position) const {
70 if (! m_count || ! std::isfinite(position)) return -1;
71 const qreal line = std::clamp(std::floor(position / (m_extent + m_mainSpacing)),
72 qreal(0),
73 qreal((m_count - 1) / m_slots));
74 return int(line) * m_slots;
75 }
76 ItemRange visibleRange(qreal begin, qreal end) const {
77 if (! m_count || m_cross <= 0 || m_cell <= 0 || ! std::isfinite(begin) ||
78 ! std::isfinite(end) || begin >= end || end <= 0 || begin >= m_total)
79 return {};
80 if (m_reversed) {
81 const auto oldBegin = begin;
82 begin = m_total - end;
83 end = m_total - oldBegin;
84 }
85 int first = rawIndexAt(begin);
86 if (rawOffset(first) + m_extent <= begin) {
87 const qint64 next = qint64(first) + m_slots;
88 if (next >= m_count) return {};
89 first = int(next);
90 }
91 int last = rawIndexAt(std::min(end, m_total));
92 if (rawOffset(last) >= end) last -= m_slots;
93 if (last < first) return {};
94 last = int(std::min(qint64(m_count - 1), qint64(last) + m_slots - 1));
95 return { first, last };
96 }
97 ItemRange requestedRange(qreal offset, qreal viewport, qreal cache) const {
98 const auto desired = visibleRange(offset - cache, offset + viewport + cache);
99 const auto visible = visibleRange(offset, offset + viewport);
100 if (desired.first < 0) return {};
101 // Cache whole lines near the viewport; visible cells are never budget-limited.
102 const int lines = std::max(1, 128 / m_slots);
103 const int centerFirst = visible.first >= 0 ? visible.first : indexAt(offset);
104 const int centerLast = visible.last >= 0 ? visible.last : centerFirst;
105 return { int(std::max(qint64(desired.first),
106 qint64(centerFirst) - qint64(lines) * m_slots)),
107 int(std::min(qint64(desired.last),
108 (qint64(centerLast / m_slots) + lines + 1) * m_slots - 1)) };
109 }
110 int neighbor(int index, int lineDelta, int slotDelta) const {
111 if (index < 0 || index >= m_count) return m_count ? 0 : -1;
112 if (m_mirrored) slotDelta = -slotDelta;
113 if (m_reversed) lineDelta = -lineDelta;
114 const int slot = index % m_slots;
115 const qint64 line = qint64(index / m_slots) + lineDelta;
116 if (line < 0 || line > (m_count - 1) / m_slots || slot + slotDelta < 0 ||
117 slot + slotDelta >= m_slots)
118 return index;
119 return int(std::min(qint64(m_count - 1), line * m_slots + slot + slotDelta));
120 }
121
122private:
123 qreal rawOffset(int index) const { return (index / m_slots) * (m_extent + m_mainSpacing); }
124 int m_count = 0;
125 int m_slots = 1;
126 qreal m_cross = 0, m_cell = 0, m_extent = 48, m_mainSpacing = 0, m_crossSpacing = 0,
127 m_total = 0;
128 bool m_mirrored = false;
129 bool m_reversed = false;
130};
131} // namespace qml_material