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
|
// -*- c-basic-offset: 4 -*-
/*
Rosegarden
A sequencer and musical notation editor.
This program is Copyright 2000-2008
Guillaume Laurent <[email protected]>,
Chris Cannam <[email protected]>,
Richard Bown <[email protected]>
The moral right of the authors to claim authorship of this work
has been asserted.
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. See the file
COPYING included with this distribution for more information.
*/
#include "SnapGrid.h"
#include "Composition.h"
namespace Rosegarden {
//////////////////////////////////////////////////////////////////////
// SnapGrid
//////////////////////////////////////////////////////////////////////
const timeT SnapGrid::NoSnap = -1;
const timeT SnapGrid::SnapToBar = -2;
const timeT SnapGrid::SnapToBeat = -3;
const timeT SnapGrid::SnapToUnit = -4;
SnapGrid::SnapGrid(RulerScale *rulerScale, int ysnap) :
m_rulerScale(rulerScale),
m_snapTime(SnapToBeat),
m_ysnap(ysnap)
{
// nothing else
}
void
SnapGrid::setSnapTime(timeT snap)
{
assert(snap > 0 ||
snap == NoSnap ||
snap == SnapToBar ||
snap == SnapToBeat ||
snap == SnapToUnit);
m_snapTime = snap;
}
timeT
SnapGrid::getSnapSetting() const
{
return m_snapTime;
}
timeT
SnapGrid::getSnapTime(double x) const
{
timeT time = m_rulerScale->getTimeForX(x);
return getSnapTime(time);
}
timeT
SnapGrid::getSnapTime(timeT time) const
{
if (m_snapTime == NoSnap) return 0;
Rosegarden::Composition *composition = m_rulerScale->getComposition();
int barNo = composition->getBarNumber(time);
std::pair<timeT, timeT> barRange = composition->getBarRange(barNo);
timeT snapTime = barRange.second - barRange.first;
if (m_snapTime == SnapToBeat) {
snapTime = composition->getTimeSignatureAt(time).getBeatDuration();
} else if (m_snapTime == SnapToUnit) {
snapTime = composition->getTimeSignatureAt(time).getUnitDuration();
} else if (m_snapTime != SnapToBar && m_snapTime < snapTime) {
snapTime = m_snapTime;
}
return snapTime;
}
timeT
SnapGrid::snapX(double x, SnapDirection direction) const
{
return snapTime(m_rulerScale->getTimeForX(x), direction);
}
timeT
SnapGrid::snapTime(timeT time, SnapDirection direction) const
{
if (m_snapTime == NoSnap) return time;
Rosegarden::Composition *composition = m_rulerScale->getComposition();
int barNo = composition->getBarNumber(time);
std::pair<timeT, timeT> barRange = composition->getBarRange(barNo);
timeT snapTime = barRange.second - barRange.first;
if (m_snapTime == SnapToBeat) {
snapTime = composition->getTimeSignatureAt(time).getBeatDuration();
} else if (m_snapTime == SnapToUnit) {
snapTime = composition->getTimeSignatureAt(time).getUnitDuration();
} else if (m_snapTime != SnapToBar && m_snapTime < snapTime) {
snapTime = m_snapTime;
}
timeT offset = (time - barRange.first);
timeT rounded = (offset / snapTime) * snapTime;
timeT left = rounded + barRange.first;
timeT right = left + snapTime;
if (direction == SnapLeft) return left;
else if (direction == SnapRight) return right;
else if ((offset - rounded) > (rounded + snapTime - offset)) return right;
else return left;
}
int
SnapGrid::getYBin(int y) const
{
if (m_ysnap == 0) return y;
int cy = 0;
std::map<int, int>::const_iterator i = m_ymultiple.begin();
int nextbin = -1;
if (i != m_ymultiple.end()) nextbin = i->first;
for (int b = 0; ; ++b) {
if (nextbin == b) {
cy += i->second * m_ysnap;
++i;
if (i == m_ymultiple.end()) nextbin = -1;
else nextbin = i->first;
} else {
cy += m_ysnap;
}
if (cy > y) {
return b;
}
}
}
int
SnapGrid::getYBinCoordinate(int bin) const
{
if (m_ysnap == 0) return bin;
int y = 0;
std::map<int, int>::const_iterator i = m_ymultiple.begin();
int nextbin = -1;
if (i != m_ymultiple.end()) nextbin = i->first;
for (int b = 0; b < bin; ++b) {
if (nextbin == b) {
y += i->second * m_ysnap;
++i;
if (i == m_ymultiple.end()) nextbin = -1;
else nextbin = i->first;
} else {
y += m_ysnap;
}
}
return y;
}
}
|