<commit_before>#include "d2ModelScene.h"

#include <QtWidgets/QGraphicsSceneMouseEvent>
#include <QtGui/QKeyEvent>
#include <QtGui/QPainter>

#include <qrkernel/settingsManager.h>
#include <qrutils/graphicsUtils/gridDrawer.h>

#include "robotItem.h"
#include "commonTwoDModel/engine/configurer.h"

#include "src/engine/model/model.h"
#include "src/engine/items/wallItem.h"
#include "src/engine/items/stylusItem.h"
#include "src/engine/items/ellipseItem.h"

using namespace twoDModel;
using namespace view;
using namespace qReal;
using namespace graphicsUtils;

D2ModelScene::D2ModelScene(model::Model &model
		, Configurer const &configurer
		, AbstractView *view
		, QObject *parent)
	: AbstractScene(view, parent)
	, mModel(model)
	, mConfigurer(configurer)
	, mDrawingAction(none)
	, mRobot(nullptr)
	, mCurrentWall(nullptr)
	, mCurrentLine(nullptr)
	, mCurrentStylus(nullptr)
	, mCurrentEllipse(nullptr)
{
	mFirstPenWidth = 6;
	mSizeEmptyRectX = 1000;
	mSizeEmptyRectY = 1000;
	setEmptyRect(-500, -500, mSizeEmptyRectX, mSizeEmptyRectY);
	setItemIndexMethod(NoIndex);
	setEmptyPenBrushItems();

	connect(&mModel.worldModel(), &model::WorldModel::wallAdded, this, &QGraphicsScene::addItem);
	connect(&mModel.worldModel(), &model::WorldModel::wallAdded, [this](items::WallItem *wall) {
		connect(wall, &items::WallItem::wallDragged, this, &D2ModelScene::worldWallDragged);
	});
	connect(&mModel.worldModel(), &model::WorldModel::colorItemAdded, this, &QGraphicsScene::addItem);
	connect(&mModel.worldModel(), &model::WorldModel::itemRemoved, [](QGraphicsItem *item) { delete item; });

	drawInitialRobot();
}

D2ModelScene::~D2ModelScene()
{
	delete mRobot;
}

void D2ModelScene::drawInitialRobot()
{
	mRobot = new RobotItem(mConfigurer.robotImage(), mModel.robotModel());
	connect(mRobot, &RobotItem::changedPosition, this, &D2ModelScene::handleNewRobotPosition);
	connect(mRobot, &RobotItem::mousePressed, this, &D2ModelScene::robotPressed);
	addItem(mRobot);

	Rotater * const rotater = new Rotater();
	rotater->setMasterItem(mRobot);
	rotater->setVisible(false);

	mRobot->setRotater(rotater);

	centerOnRobot();
}

void D2ModelScene::handleNewRobotPosition()
{
	for (items::WallItem const *wall : mModel.worldModel().walls()) {
		if (wall->realShape().intersects(mRobot->realBoundingRect())) {
			mRobot->recoverDragStartPosition();
			return;
		}
	}
}

void D2ModelScene::mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent)
{
	QPointF const position = mouseEvent->scenePos();

	mRobot->checkSelection();
	for (SensorItem *sensor : mRobot->sensors().values()) {
		if (sensor) {
			sensor->checkSelection();
		}
	}

	emit mousePressed();

	auto initItem = [this, mouseEvent](QGraphicsItem *item) {
		removeMoveFlag(mouseEvent, item);
		// This will deselect alll items
		setSelectionArea(QPainterPath());
	};

	auto initColorField = [this, mouseEvent, initItem](items::ColorFieldItem *item) {
		initItem(item);
		item->setPenBrush(penStyleItems(), penWidthItems(), penColorItems()
				, brushStyleItems(), brushColorItems());
		mModel.worldModel().addColorField(item);
	};

	if (!mRobot->realBoundingRect().contains(position)) {
		switch (mDrawingAction) {
		case wall:
			mCurrentWall = new items::WallItem(position, position);
			initItem(mCurrentWall);
			mModel.worldModel().addWall(mCurrentWall);
			break;
		case line:
			mCurrentLine = new items::LineItem(position, position);
			initColorField(mCurrentLine);<commit_msg>Fixed taking lambda by value<commit_after>#include "d2ModelScene.h"

#include <QtWidgets/QGraphicsSceneMouseEvent>
#include <QtGui/QKeyEvent>
#include <QtGui/QPainter>

#include <qrkernel/settingsManager.h>
#include <qrutils/graphicsUtils/gridDrawer.h>

#include "robotItem.h"
#include "commonTwoDModel/engine/configurer.h"

#include "src/engine/model/model.h"
#include "src/engine/items/wallItem.h"
#include "src/engine/items/stylusItem.h"
#include "src/engine/items/ellipseItem.h"

using namespace twoDModel;
using namespace view;
using namespace qReal;
using namespace graphicsUtils;

D2ModelScene::D2ModelScene(model::Model &model
		, Configurer const &configurer
		, AbstractView *view
		, QObject *parent)
	: AbstractScene(view, parent)
	, mModel(model)
	, mConfigurer(configurer)
	, mDrawingAction(none)
	, mRobot(nullptr)
	, mCurrentWall(nullptr)
	, mCurrentLine(nullptr)
	, mCurrentStylus(nullptr)
	, mCurrentEllipse(nullptr)
{
	mFirstPenWidth = 6;
	mSizeEmptyRectX = 1000;
	mSizeEmptyRectY = 1000;
	setEmptyRect(-500, -500, mSizeEmptyRectX, mSizeEmptyRectY);
	setItemIndexMethod(NoIndex);
	setEmptyPenBrushItems();

	connect(&mModel.worldModel(), &model::WorldModel::wallAdded, this, &QGraphicsScene::addItem);
	connect(&mModel.worldModel(), &model::WorldModel::wallAdded, [this](items::WallItem *wall) {
		connect(wall, &items::WallItem::wallDragged, this, &D2ModelScene::worldWallDragged);
	});
	connect(&mModel.worldModel(), &model::WorldModel::colorItemAdded, this, &QGraphicsScene::addItem);
	connect(&mModel.worldModel(), &model::WorldModel::itemRemoved, [](QGraphicsItem *item) { delete item; });

	drawInitialRobot();
}

D2ModelScene::~D2ModelScene()
{
	delete mRobot;
}

void D2ModelScene::drawInitialRobot()
{
	mRobot = new RobotItem(mConfigurer.robotImage(), mModel.robotModel());
	connect(mRobot, &RobotItem::changedPosition, this, &D2ModelScene::handleNewRobotPosition);
	connect(mRobot, &RobotItem::mousePressed, this, &D2ModelScene::robotPressed);
	addItem(mRobot);

	Rotater * const rotater = new Rotater();
	rotater->setMasterItem(mRobot);
	rotater->setVisible(false);

	mRobot->setRotater(rotater);

	centerOnRobot();
}

void D2ModelScene::handleNewRobotPosition()
{
	for (items::WallItem const *wall : mModel.worldModel().walls()) {
		if (wall->realShape().intersects(mRobot->realBoundingRect())) {
			mRobot->recoverDragStartPosition();
			return;
		}
	}
}

void D2ModelScene::mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent)
{
	QPointF const position = mouseEvent->scenePos();

	mRobot->checkSelection();
	for (SensorItem *sensor : mRobot->sensors().values()) {
		if (sensor) {
			sensor->checkSelection();
		}
	}

	emit mousePressed();

	auto initItem = [this, mouseEvent](QGraphicsItem *item) {
		removeMoveFlag(mouseEvent, item);
		// This will deselect alll items
		setSelectionArea(QPainterPath());
	};

	auto initColorField = [this, mouseEvent, &initItem](items::ColorFieldItem *item) {
		initItem(item);
		item->setPenBrush(penStyleItems(), penWidthItems(), penColorItems()
				, brushStyleItems(), brushColorItems());
		mModel.worldModel().addColorField(item);
	};

	if (!mRobot->realBoundingRect().contains(position)) {
		switch (mDrawingAction) {
		case wall:
			mCurrentWall = new items::WallItem(position, position);
			initItem(mCurrentWall);
			mModel.worldModel().addWall(mCurrentWall);
			break;
		case line:
			mCurrentLine = new items::LineItem(position, position);
			initColorField(mCurrentLine);
{'fraction_non_alphanumeric': 0.1447444831591173, 'fraction_numerical': 0.007549361207897794, 'mean_word_length': 6.927502876869966, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 37, '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': '2279395', 'n_tokens_mistral': 2484, 'n_tokens_neox': 2357, 'n_words': 385}