comparison data/model/Labeller.h @ 1291:22f66068b464 subdivide-instants

Implement "Subdivide Selected Instants". Also add the relabel command to the history, which I previously forgot to do!
author Chris Cannam
date Fri, 25 Nov 2016 09:38:53 +0000
parents 2f49be7d4264
children 4b0c968a581a
comparison
equal deleted inserted replaced
1290:fa574c909c3d 1291:22f66068b464
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 * multi-selection, according to the labelling properties of this
177 * labeller. Return a command that has been executed but not yet
178 * added to the history.
179 */
180 template <typename PointType>
181 Command *labelAll(SparseModel<PointType> &model, MultiSelection *ms) {
176 182
177 typename SparseModel<PointType>::PointList::iterator i; 183 typename SparseModel<PointType>::PointList::iterator i;
178 typename SparseModel<PointType>::PointList pl(model.getPoints()); 184 typename SparseModel<PointType>::PointList pl(model.getPoints());
179 185
180 typename SparseModel<PointType>::EditCommand *command = 186 typename SparseModel<PointType>::EditCommand *command =
213 } 219 }
214 220
215 prevPoint = p; 221 prevPoint = p;
216 } 222 }
217 223
218 command->finish(); 224 return command->finish();
225 }
226
227 /**
228 * For each point in the given model (except the last), if that
229 * point lies within the given multi-selection, add n-1 new points
230 * at equally spaced intervals between it and the following point.
231 * Return a command that has been executed but not yet added to
232 * the history.
233 */
234 template <typename PointType>
235 Command *subdivide(SparseModel<PointType> &model, MultiSelection *ms, int n) {
236
237 typename SparseModel<PointType>::PointList::iterator i;
238 typename SparseModel<PointType>::PointList pl(model.getPoints());
239
240 typename SparseModel<PointType>::EditCommand *command =
241 new typename SparseModel<PointType>::EditCommand
242 (&model, tr("Subdivide"));
243
244 for (i = pl.begin(); i != pl.end(); ++i) {
245
246 auto j = i;
247 // require a "next point" even if it's not in selection
248 if (++j == pl.end()) {
249 break;
250 }
251
252 bool inRange = true;
253 if (ms) {
254 Selection s(ms->getContainingSelection(i->frame, false));
255 if (s.isEmpty() || !s.contains(i->frame)) {
256 inRange = false;
257 }
258 }
259 if (!inRange) {
260 continue;
261 }
262
263 PointType p(*i);
264 PointType nextP(*j);
265
266 // n is the number of subdivisions, so we add n-1 new
267 // points equally spaced between p and nextP
268
269 for (int m = 1; m < n; ++m) {
270 sv_frame_t f = p.frame + (m * (nextP.frame - p.frame)) / n;
271 PointType newPoint(p);
272 newPoint.frame = f;
273 newPoint.label = tr("%1.%2").arg(p.label).arg(m+1);
274 command->addPoint(newPoint);
275 }
276 }
277
278 return command->finish();
219 } 279 }
220 280
221 template <typename PointType> 281 template <typename PointType>
222 void setValue(PointType &newPoint, PointType *prevPoint = 0) { 282 void setValue(PointType &newPoint, PointType *prevPoint = 0) {
223 if (m_type == ValueFromExistingNeighbour) { 283 if (m_type == ValueFromExistingNeighbour) {