qreal *NearestPosGridGesturesManager::getKey(PathVector const &path)
{
	Key key = KeyBuilder::getKey(path, gridSize, gridSize);
	qreal * finalKey = new qreal[gridSize * gridSize]; // deal with this too
	for (int i = 0; i < gridSize * gridSize; ++i) {
		finalKey[i] = gridSize;
	}

	if (key.isEmpty()) {
		return finalKey;
	}

	for (int i = 0; i < gridSize; ++i) {
		for (int j = 0; j < gridSize; ++j) {
			qreal dist = qAbs(key.at(0).first - i) + qAbs(key.at(0).second - j);
			for (SquarePos const &pos : key) {
				dist = qMin(dist, qAbs(pos.first - i) + qAbs(pos.second - j));
			}

			finalKey[i * gridSize + j] = dist;
		}
	}

	return finalKey;
}
double * NearestPosGridGesturesManager::getKey(PathVector const &path)
{
	Key key = KeyBuilder::getKey(path, gridSize, gridSize);
	double * finalKey = new double[gridSize * gridSize]; // deal with this too
	for (int i = 0; i < gridSize * gridSize; i++)
		finalKey[i] = gridSize;
	if (key.isEmpty())
		return finalKey;
	for (int i = 0; i < gridSize; i++) {
		for (int j = 0; j < gridSize; j++) {
			double dist = std::abs(key.at(0).first - i) + std::abs(key.at(0).second - j);
			foreach (SquarePos pos, key)
				dist = std::min(dist, std::abs(pos.first - i) + std::abs(pos.second - j));
			finalKey[i * gridSize + j] = dist;
		}
	}
	return finalKey;
}
double * NearestPosGridGesturesManager::key(PathVector const &path) const
{
	Key key = KeyBuilder::key(path, gridSize, gridSize);
	double * finalKey = new double[gridSize * gridSize];
	for (int i = 0; i < gridSize * gridSize; i ++) {
		finalKey[i] = gridSize;
	}
	if (key.isEmpty()) {
		return finalKey;
	}
	for (int i = 0; i < gridSize; i ++) {
		for (int j = 0; j < gridSize; j ++) {
			double dist = abs(key.at(0).first - i) + abs(key.at(0).second - j);
			foreach (SquarePos const &pos, key) {
			  double norm = abs(pos.first - i) + abs(pos.second - j);
				dist = std::min(dist, norm);
			}
			finalKey[i * gridSize + j] = dist;
		}
	}
double * RectangleGesturesManager::getKey(PathVector const & path)
{
	Key key = KeyBuilder::getKey(path, gridSize, gridSize);
	double *finalKey = new double[gridSize * gridSize];
	for (int i = 0; i < gridSize * gridSize; i++)
		finalKey[i] = key.size();
	for (int k = 0; k < key.size(); k++) {
		SquarePos pos = key.at(k);
		for (int i = 0; i < pos.first; i++) {
			for (int j = 0; j < pos.second; j++)
				finalKey[i * gridSize + j]--;
		}
	}
	return finalKey;
}