comparison data/model/Labeller.h @ 1365:3382d914e110

Merge from branch 3.0-integration
author Chris Cannam
date Fri, 13 Jan 2017 10:29:44 +0000
parents 114de081a42d
children ad5f892c0c4d
comparison
equal deleted inserted replaced
1272:6a7ea3bd0e10 1365:3382d914e110
169 newPoint.label = QString("%1").arg(value); 169 newPoint.label = QString("%1").arg(value);
170 } 170 }
171 } 171 }
172 } 172 }
173 173
174 template <typename PointType> 174 /**
175 void labelAll(SparseModel<PointType> &model, MultiSelection *ms) { 175 * Relabel all points in the given model that lie within the given
176 176 * multi-selection, according to the labelling properties of this
177 typename SparseModel<PointType>::PointList::iterator i; 177 * labeller. Return a command that has been executed but not yet
178 typename SparseModel<PointType>::PointList pl(model.getPoints()); 178 * added to the history.
179 179 */
180 typename SparseModel<PointType>::EditCommand *command = 180 template <typename PointType>
181 new typename SparseModel<PointType>::EditCommand 181 Command *labelAll(SparseModel<PointType> &model, MultiSelection *ms) {
182
183 auto points(model.getPoints());
184 auto command = new typename SparseModel<PointType>::EditCommand
182 (&model, tr("Label Points")); 185 (&model, tr("Label Points"));
183 186
184 PointType prevPoint(0); 187 PointType prevPoint(0);
185 188 bool havePrevPoint(false);
186 for (i = pl.begin(); i != pl.end(); ++i) { 189
187 190 for (auto p: points) {
188 bool inRange = true; 191
189 if (ms) { 192 if (ms) {
190 Selection s(ms->getContainingSelection(i->frame, false)); 193 Selection s(ms->getContainingSelection(p.frame, false));
191 if (s.isEmpty() || !s.contains(i->frame)) { 194 if (!s.contains(p.frame)) {
192 inRange = false; 195 prevPoint = p;
196 havePrevPoint = true;
197 continue;
193 } 198 }
194 } 199 }
195 200
196 PointType p(*i);
197
198 if (!inRange) {
199 prevPoint = p;
200 continue;
201 }
202
203 if (actingOnPrevPoint()) { 201 if (actingOnPrevPoint()) {
204 if (i != pl.begin()) { 202 if (havePrevPoint) {
205 command->deletePoint(prevPoint); 203 command->deletePoint(prevPoint);
206 label<PointType>(p, &prevPoint); 204 label<PointType>(p, &prevPoint);
207 command->addPoint(prevPoint); 205 command->addPoint(prevPoint);
208 } 206 }
209 } else { 207 } else {
211 label<PointType>(p, &prevPoint); 209 label<PointType>(p, &prevPoint);
212 command->addPoint(p); 210 command->addPoint(p);
213 } 211 }
214 212
215 prevPoint = p; 213 prevPoint = p;
216 } 214 havePrevPoint = true;
217 215 }
218 command->finish(); 216
217 return command->finish();
218 }
219
220 /**
221 * For each point in the given model (except the last), if that
222 * point lies within the given multi-selection, add n-1 new points
223 * at equally spaced intervals between it and the following point.
224 * Return a command that has been executed but not yet added to
225 * the history.
226 */
227 template <typename PointType>
228 Command *subdivide(SparseModel<PointType> &model, MultiSelection *ms, int n) {
229
230 auto points(model.getPoints());
231 auto command = new typename SparseModel<PointType>::EditCommand
232 (&model, tr("Subdivide Points"));
233
234 for (auto i = points.begin(); i != points.end(); ++i) {
235
236 auto j = i;
237 // require a "next point" even if it's not in selection
238 if (++j == points.end()) {
239 break;
240 }
241
242 if (ms) {
243 Selection s(ms->getContainingSelection(i->frame, false));
244 if (!s.contains(i->frame)) {
245 continue;
246 }
247 }
248
249 PointType p(*i);
250 PointType nextP(*j);
251
252 // n is the number of subdivisions, so we add n-1 new
253 // points equally spaced between p and nextP
254
255 for (int m = 1; m < n; ++m) {
256 sv_frame_t f = p.frame + (m * (nextP.frame - p.frame)) / n;
257 PointType newPoint(p);
258 newPoint.frame = f;
259 newPoint.label = tr("%1.%2").arg(p.label).arg(m+1);
260 command->addPoint(newPoint);
261 }
262 }
263
264 return command->finish();
265 }
266
267 /**
268 * Return a command that has been executed but not yet added to
269 * the history.
270 */
271 template <typename PointType>
272 Command *winnow(SparseModel<PointType> &model, MultiSelection *ms, int n) {
273
274 auto points(model.getPoints());
275 auto command = new typename SparseModel<PointType>::EditCommand
276 (&model, tr("Winnow Points"));
277
278 int counter = 0;
279
280 for (auto p: points) {
281
282 if (ms) {
283 Selection s(ms->getContainingSelection(p.frame, false));
284 if (!s.contains(p.frame)) {
285 counter = 0;
286 continue;
287 }
288 }
289
290 ++counter;
291
292 if (counter == n+1) counter = 1;
293 if (counter == 1) {
294 // this is an Nth instant, don't remove it
295 continue;
296 }
297
298 command->deletePoint(p);
299 }
300
301 return command->finish();
219 } 302 }
220 303
221 template <typename PointType> 304 template <typename PointType>
222 void setValue(PointType &newPoint, PointType *prevPoint = 0) { 305 void setValue(PointType &newPoint, PointType *prevPoint = 0) {
223 if (m_type == ValueFromExistingNeighbour) { 306 if (m_type == ValueFromExistingNeighbour) {