55MouseGestureRecognizer::MouseGestureRecognizer(
int minimumMovement,
double minimumMatch,
bool allowDiagonals)
96 if (dx * dx + dy * dy < d->minimumMovement2) {
101 matched = recognizeGesture();
131bool MouseGestureRecognizer::recognizeGesture()
134 double minLength = calcLength(directions) * d->
minimumMatch;
136 while (directions.size() > 0 && calcLength(directions) > minLength) {
137 for (GestureList::const_iterator gi = d->
gestures.begin(); gi != d->
gestures.end(); ++gi) {
138 if (gi->directions.size() == directions.size()) {
140 PosList::const_iterator pi = directions.begin();
141 for (DirectionList::const_iterator di = gi->directions.begin(); di != gi->directions.end() && match; ++di, ++pi) {
144 if (!(pi->y < 0 && pi->x < 0)) {
150 if (!(pi->y < 0 && pi->x > 0)) {
156 if (!(pi->y > 0 && pi->x < 0)) {
162 if (!(pi->y > 0 && pi->x > 0)) {
168 if (pi->y >= 0 || pi->x != 0) {
174 if (pi->y <= 0 || pi->x != 0) {
180 if (pi->x >= 0 || pi->y != 0) {
186 if (pi->x <= 0 || pi->y != 0) {
192 if (pi->x == 0 || pi->y != 0) {
198 if (pi->y == 0 || pi->x != 0) {
211 gi->callbackClass->callback();
217 directions = simplify(removeShortest(directions));
220 for (GestureList::const_iterator gi = d->
gestures.begin(); gi != d->
gestures.end(); ++gi) {
221 if (gi->directions.size() == 1) {
222 if (gi->directions.back() ==
NoMatch) {
223 gi->callbackClass->callback();
239PosList MouseGestureRecognizer::limitDirections(
const PosList &positions,
bool allowDiagonals)
243 bool firstTime =
true;
245 for (PosList::const_iterator ii = positions.begin(); ii != positions.end(); ++ii) {
258 const int directions[8][2] = { {0, 15}, {0, -15}, {15, 0}, { -15, 0}, {10, 10}, { -10, 10}, { -10, -10}, {10, -10} };
262 for (
int i = 0;
i < (allowDiagonals ? 8 : 4);
i++) {
263 int value = dx * directions[
i][0] + dy * directions[
i][1];
264 if (
value > maxValue) {
270 if (maxIndex == -1) {
274 dx = directions[maxIndex][0];
275 dy = directions[maxIndex][1];
278 res.push_back(
Pos(dx, dy));
296 int lastdx = 0, lastdy = 0;
297 bool firstTime =
true;
299 for (PosList::const_iterator ii = positions.begin(); ii != positions.end(); ++ii) {
310 if (((lastdx > 0 && ii->x > 0) || (lastdx < 0 && ii->x < 0)) && (lastdy == 0 && ii->y == 0)) {
315 if (((lastdy > 0 && ii->y > 0) || (lastdy < 0 && ii->y < 0)) && (lastdx == 0 && ii->x == 0)) {
320 if (((lastdx > 0 && ii->x > 0) || (lastdx < 0 && ii->x < 0)) && (lastdy > 0 && ii->y > 0)) {
326 if (((lastdx > 0 && ii->x > 0) || (lastdx < 0 && ii->x < 0)) && (lastdy < 0 && ii->y < 0)) {
333 res.push_back(
Pos(lastdx, lastdy));
341 if (lastdx != 0 || lastdy != 0) {
342 res.push_back(
Pos(lastdx, lastdy));
353PosList MouseGestureRecognizer::removeShortest(
const PosList &positions)
358 PosList::const_iterator shortest;
359 bool firstTime =
true;
361 for (PosList::const_iterator ii = positions.begin(); ii != positions.end(); ++ii) {
363 shortestSoFar = ii->x * ii->x + ii->y * ii->y;
369 if ((ii->x * ii->x + ii->y * ii->y) < shortestSoFar) {
370 shortestSoFar = ii->x * ii->x + ii->y * ii->y;
376 for (PosList::const_iterator ii = positions.begin(); ii != positions.end(); ++ii) {
377 if (ii != shortest) {
388int MouseGestureRecognizer::calcLength(
const PosList &positions)
392 for (PosList::const_iterator ii = positions.begin(); ii != positions.end(); ++ii) {
396 else if (ii->x < 0) {
399 else if (ii->y > 0) {
void clearGestureDefinitions()
bool endGesture(int x, int y)
void addGestureDefinition(const GestureDefinition &gesture)
void startGesture(int x, int y)
~MouseGestureRecognizer()
PosList currentPath() const
void addPoint(int x, int y)
int value(const QColor &c)
std::vector< Pos > PosList
std::vector< GestureDefinition > GestureList