<commit_before>/**
 * Wrapper for QWebPage
 *
 * @package ad-reger
 * @author <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

#include "qwebpage.h"
#include "connector.h"

#include <QDebug>
#include <QDesktopWidget>
#include <QApplication>

#include <complex>

namespace Wrapper {
	QString QWebPage::userAgent = "qt";

	QWebPage::QWebPage(QObject *parent) : ::QWebPage(parent) {
		// Set cache settings
		settings()->setMaximumPagesInCache(0);
		settings()->setObjectCacheCapacities(0,0,0);
	}

	void QWebPage::javaScriptConsoleMessage(const QString &message, int lineNumber, const QString &sourceID) {
		qDebug() << message << " @ no. " << lineNumber << " sourceid: " << sourceID;
	}

	void QWebPage::javaScriptAlert(QWebFrame *frame, const QString &msg) {
		Q_UNUSED(frame);

		qDebug() << msg;
	}

	bool QWebPage::javaScriptPrompt(QWebFrame *frame, const QString &msg, const QString &defaultValue, QString *result) {
		Q_UNUSED(frame);

		qDebug() << msg << " @ default is '" << defaultValue << "' : " << (result ? *result : "canceled");

		return result == NULL;
	}

	bool QWebPage::javaScriptConfirm(QWebFrame *frame, const QString &msg) {
		Q_UNUSED(frame);

		qDebug() << msg;

		return msg == "";
	}

	QString QWebPage::userAgentForUrl(const QUrl &url) const {
		Q_UNUSED(url);

		return userAgent; 
	}

	bool QWebPage::moveMouseTo(QWebElement elementMoveTo, int delay) const {
		if (elementMoveTo.isNull()) {
			return false;
		}

		QPoint moveFrom = QCursor::pos();
		qDebug() << "Move from (cursor position)" << moveFrom;

		QRect viewFrameGeometry = view()->frameGeometry();
		qDebug() << "View frame geometry" << viewFrameGeometry;

		QRect geometry = elementMoveTo.geometry();
		qDebug() << "Element geometry" << geometry;

		// TODO : some randomize
		QPoint moveTo;
		// Calculate real center, not the upper border
		{
			moveTo = geometry.bottomLeft();
			qDebug() << "Geometry bottom left" << moveTo;

			moveTo.setX(viewFrameGeometry.x() + moveTo.x() + (geometry.width() / 2));
			moveTo.setY(viewFrameGeometry.y() + moveTo.y() + (geometry.height() / 2));
		}
		qDebug() << "Move to" << moveTo;

		Module::Connector connector;

		// default ratio is 1
		float ratioX = 1;
		float ratioY = 1;
		int maxLoopIterations = 0;
		{
			int xDiff = std::abs(moveTo.x() - moveFrom.x());
			int yDiff = std::abs(moveTo.y() - moveFrom.y());

			// We already at this element
			if ((xDiff == 0) && (yDiff == 0)) {
				return true;
			}

			if (xDiff < yDiff) {
				ratioY = xDiff ? int((yDiff / xDiff) + 0.5) : 0;
			} else {
				ratioX = yDiff ? int((xDiff / yDiff) + 0.5) : 0;
			}

			maxLoopIterations = std::max(xDiff, yDiff);

			qDebug() << "Diff" << "x:" << xDiff << "y:" << yDiff;
			qDebug() << "Max loop interations" << maxLoopIterations;
		}
		qDebug() << "Ratio" << "x:" << ratioX << "y:" << ratioY;

		// Move
		for (int i = 0; (i < maxLoopIterations) || (moveFrom != moveTo); ++i) {
			if (moveTo.x() != moveFrom.x()) {
				moveFrom.setX((moveFrom.x() > moveTo.x()) ? (moveFrom.x() - ratioX) : (moveFrom.x() + ratioX));
			}

			if (moveTo.y() != moveFrom.y()) {
				moveFrom.setY((moveFrom.y() > moveTo.y()) ? (moveFrom.y() - ratioY) : (moveFrom.y() + ratioY));
			}

			QCursor::setPos(moveFrom);

			connector.waitWithoutGuiBlock(delay);
		}

		return true;
	}
}
<commit_msg>- Decrease maxLoopIteratios for moveMouseTo method<commit_after>/**
 * Wrapper for QWebPage
 *
 * @package ad-reger
 * @author <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

#include "qwebpage.h"
#include "connector.h"

#include <QDebug>
#include <QDesktopWidget>
#include <QApplication>

#include <complex>

namespace Wrapper {
	QString QWebPage::userAgent = "qt";

	QWebPage::QWebPage(QObject *parent) : ::QWebPage(parent) {
		// Set cache settings
		settings()->setMaximumPagesInCache(0);
		settings()->setObjectCacheCapacities(0,0,0);
	}

	void QWebPage::javaScriptConsoleMessage(const QString &message, int lineNumber, const QString &sourceID) {
		qDebug() << message << " @ no. " << lineNumber << " sourceid: " << sourceID;
	}

	void QWebPage::javaScriptAlert(QWebFrame *frame, const QString &msg) {
		Q_UNUSED(frame);

		qDebug() << msg;
	}

	bool QWebPage::javaScriptPrompt(QWebFrame *frame, const QString &msg, const QString &defaultValue, QString *result) {
		Q_UNUSED(frame);

		qDebug() << msg << " @ default is '" << defaultValue << "' : " << (result ? *result : "canceled");

		return result == NULL;
	}

	bool QWebPage::javaScriptConfirm(QWebFrame *frame, const QString &msg) {
		Q_UNUSED(frame);

		qDebug() << msg;

		return msg == "";
	}

	QString QWebPage::userAgentForUrl(const QUrl &url) const {
		Q_UNUSED(url);

		return userAgent; 
	}

	bool QWebPage::moveMouseTo(QWebElement elementMoveTo, int delay) const {
		if (elementMoveTo.isNull()) {
			return false;
		}

		QPoint moveFrom = QCursor::pos();
		qDebug() << "Move from (cursor position)" << moveFrom;

		QRect viewFrameGeometry = view()->frameGeometry();
		qDebug() << "View frame geometry" << viewFrameGeometry;

		QRect geometry = elementMoveTo.geometry();
		qDebug() << "Element geometry" << geometry;

		// TODO : some randomize
		QPoint moveTo;
		// Calculate real center, not the upper border
		{
			moveTo = geometry.bottomLeft();
			qDebug() << "Geometry bottom left" << moveTo;

			moveTo.setX(viewFrameGeometry.x() + moveTo.x() + (geometry.width() / 2));
			moveTo.setY(viewFrameGeometry.y() + moveTo.y() + (geometry.height() / 2));
		}
		qDebug() << "Move to" << moveTo;

		Module::Connector connector;

		// default ratio is 1
		float ratioX = 1;
		float ratioY = 1;
		int maxLoopIterations = 0;
		{
			int xDiff = std::abs(moveTo.x() - moveFrom.x());
			int yDiff = std::abs(moveTo.y() - moveFrom.y());

			// We already at this element
			if ((xDiff == 0) && (yDiff == 0)) {
				return true;
			}

			if (xDiff < yDiff) {
				ratioY = xDiff ? int((yDiff / xDiff) + 0.5) : 0;
			} else {
				ratioX = yDiff ? int((xDiff / yDiff) + 0.5) : 0;
			}

			maxLoopIterations = ratioX ? (xDiff / ratioX) : (yDiff / ratioY);

			qDebug() << "Diff" << "x:" << xDiff << "y:" << yDiff;
			qDebug() << "Max loop interations" << maxLoopIterations;
		}
		qDebug() << "Ratio" << "x:" << ratioX << "y:" << ratioY;

		// Move
		for (int i = 0; (moveFrom != moveTo /* Just in case */) || (i < maxLoopIterations); ++i) {
			if (moveTo.x() != moveFrom.x()) {
				moveFrom.setX((moveFrom.x() > moveTo.x()) ? (moveFrom.x() - ratioX) : (moveFrom.x() + ratioX));
			}

			if (moveTo.y() != moveFrom.y()) {
				moveFrom.setY((moveFrom.y() > moveTo.y()) ? (moveFrom.y() - ratioY) : (moveFrom.y() + ratioY));
			}

			QCursor::setPos(moveFrom);

			connector.waitWithoutGuiBlock(delay);
		}

		return true;
	}
}

{'fraction_non_alphanumeric': 0.1733952948010456, 'fraction_numerical': 0.00551844321812373, 'mean_word_length': 3.701023890784983, 'pattern_counts': {'":': 0, '<': 157, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5348755', 'n_tokens_mistral': 2681, 'n_tokens_neox': 2398, 'n_words': 585}