-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathGridModel.h
More file actions
224 lines (203 loc) · 6.87 KB
/
GridModel.h
File metadata and controls
224 lines (203 loc) · 6.87 KB
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
/*
* GridModel.h - implements a grid where objects are placed
*
* Copyright (c) 2025 - 2026 szeli1
*
* This file is part of LMMS - https://lmms.io
*
* This program 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 2 of the License, or (at your option) any later version.
*
* This program 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.
*
* You should have received a copy of the GNU General Public
* License along with this program (see COPYING); if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA.
*
*/
#ifndef LMMS_GRID_MODEl_H
#define LMMS_GRID_MODEl_H
#include <set>
#include <stddef.h>
#include <vector>
#include "base64.h"
#include "Model.h"
namespace lmms
{
static const unsigned int GRID_MAX_STEPS = 100000;
/* This class handles
1. grid's size
2. storing and pairing `ItemInfo` to custom data
3. getting `ItemInfo` custom data pairs
4. saving `ItemInfo` with paired custom data
5. changes in `ItemInfo` (position changes)
This class doesn't handle
1. constructing, storing, interacting or removing custom data in any way
*/
class LMMS_EXPORT GridModel : public Model
{
Q_OBJECT
public:
using signedSize = signed long long;
struct ItemInfo
{
float x;
float y;
};
struct Item
{
ItemInfo info;
size_t objectIndex; //!< custom data index
};
//*** util ***
//! @return index if found inside radius, else -1
//! (the first index is returned where `xPos` is found)
int findIndexFromPos(float xPos, float radius) const;
//*** item management ***
//! @return new / final index (if x changed)
size_t setInfo(size_t index, const ItemInfo& info);
size_t setInfo(size_t index, const ItemInfo& info, unsigned int horizontalSteps, unsigned int verticalSteps);
const Item& getItem(size_t index) const;
//! @return the index where [index - 1].x < xPos and xPos <= [index].x
//! so return the first index where xPos <= x (this index can be the size)
size_t findIndex(float xPos) const;
//*** model management ***
//! @return item count
size_t getCount() const;
unsigned int getLength() const;
unsigned int getHeight() const;
void resizeGrid(size_t length, size_t height);
void setSteps(unsigned int horizontalSteps, unsigned int verticalSteps);
virtual ~GridModel();
protected:
GridModel(unsigned int length, unsigned int height, unsigned int horizontalSteps, unsigned int verticalSteps,
Model* parent, QString displayName, bool defaultConstructed);
virtual void dataChangedAt(signedSize index) {}
//! @return index where added
size_t addItem(Item itemIn);
void removeItem(size_t index);
void clearItems();
//! DOESN'T EMIT, get and set index pointing to custom data
size_t& getAndSetObjectIndex(size_t index);
private:
//! only call these with fitted values (`fitPos`)
//! @return new / final index (if x changed)
size_t setX(size_t index, float newX);
void setY(size_t index, float newY);
float fitPos(float position, unsigned int max, unsigned int steps) const;
//! moves `startIndex` to `finalIndex` by swapping
void move(size_t startIndex, size_t finalIndex);
unsigned int m_length;
unsigned int m_height;
unsigned int m_horizontalSteps;
unsigned int m_verticalSteps;
//! items are sorted by x position ascending
std::vector<Item> m_items;
};
/* This class handles
1. storing custom data T
2. constructing T `ItemInfo` pair
3. removing T `ItemInfo` pair
4. getting T from index in `GridModel`
5. converting the stored data into a base64 string
*/
template<typename T, typename SaveData>
class LMMS_EXPORT GridModelTyped : public GridModel
{
private:
struct TrueDataPair
{
GridModel::ItemInfo info;
SaveData data;
};
std::vector<T> m_TCustomData;
public:
GridModelTyped(unsigned int length, unsigned int height, unsigned int horizontalSteps, unsigned int verticalSteps,
Model* parent, QString displayName = QString(), bool defaultConstructed = false)
: GridModel{length, height, horizontalSteps, verticalSteps, parent, displayName, defaultConstructed} {}
~GridModelTyped() = default;
const T& getObject(size_t index) const { return m_TCustomData[GridModel::getItem(index).objectIndex]; }
virtual void setObject(size_t index, T object)
{
m_TCustomData[GridModel::getItem(index).objectIndex] = object;
dataChangedAt(index); emit GridModel::dataChanged();
}
//! @return index where added
size_t addItem(T object, ItemInfo info)
{
m_TCustomData.push_back(object);
return GridModel::addItem(Item{info, m_TCustomData.size() - 1});
}
void removeItem(size_t index)
{
size_t customDataIndex{GridModel::getItem(index).objectIndex};
// the stored indexes need to be offset after removing (doing it before because of signals)
for (size_t i = 0; i < getCount(); ++i)
{
size_t& storedIndex{GridModel::getAndSetObjectIndex(i)};
if (storedIndex > customDataIndex) { --storedIndex; }
}
// removing the custom data
for (size_t i = customDataIndex; i < m_TCustomData.size(); ++i)
{
m_TCustomData[i] = m_TCustomData[i + 1];
}
m_TCustomData.pop_back();
// removing the Item (object* + coords pair)
GridModel::removeItem(index);
}
protected:
// save mechanism:
QString dataToBase64(float xOffset, float yOffset, const std::set<size_t>* selection = nullptr)
{
std::vector<TrueDataPair> dataArray{};
if (selection == nullptr || selection->empty())
{
dataArray.reserve(getCount());
for (size_t i = 0; i < getCount(); ++i)
{
dataArray.push_back(TrueDataPair{
GridModel::ItemInfo{getItem(i).info.x + xOffset, getItem(i).info.y + yOffset},
customDataToSaveData(getObject(i))});
}
}
else
{
dataArray.reserve(selection->size());
for (size_t i : *selection)
{
dataArray.push_back(TrueDataPair{
GridModel::ItemInfo{getItem(i).info.x + xOffset, getItem(i).info.y + yOffset},
customDataToSaveData(getObject(i))});
}
}
QString output{};
base64::encode((const char *)(dataArray.data()),
dataArray.size() * sizeof(TrueDataPair), output);
return output;
}
void addBase64Data(QString base64String, float xOffset, float yOffset)
{
int size = 0;
TrueDataPair* startPtr = nullptr;
base64::decode<TrueDataPair>(base64String, &startPtr, &size);
TrueDataPair* ptr{startPtr};
for (int i = 0; i < size; i += sizeof(TrueDataPair))
{
addItem(saveDataToCustomData(ptr->data), GridModel::ItemInfo{ptr->info.x + xOffset, ptr->info.y + yOffset});
++ptr;
}
delete[] startPtr;
}
void clearPairs() { GridModel::clearItems(); m_TCustomData.clear(); }
virtual SaveData customDataToSaveData(const T& data) { return data; }
virtual T saveDataToCustomData(const SaveData& data) { return data; }
};
} // namespace lmms
#endif // LMMS_GRID_MODEl_H