<commit_before>#include "d2ModelScene.h"

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

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

#include "robotItem.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
		, AbstractView *view
		, QObject *parent)
	: AbstractScene(view, parent)
	, mModel(model)
	, mDrawingAction(none)
{
	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::otherItemAdded, this, &QGraphicsScene::addItem);
	connect(&mModel.worldModel(), &model::WorldModel::itemRemoved, this, &D2ModelScene::onItemRemoved);

	connect(&mModel, &model::Model::robotAdded, this, &D2ModelScene::onRobotAdd);
	connect(&mModel, &model::Model::robotRemoved, this, &D2ModelScene::onRobotRemove);
}

D2ModelScene::~D2ModelScene()
{
}

bool D2ModelScene::oneRobot() const
{
	return mRobots.size() == 1;
}

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

void D2ModelScene::onRobotAdd(model::RobotModel *robotModel)
{
	RobotItem * const robotItem = new RobotItem(robotModel->info().robotImage(), *robotModel);

	connect(robotItem, &RobotItem::changedPosition, this, &D2ModelScene::handleNewRobotPosition);
	connect(robotItem, &RobotItem::mousePressed, this, &D2ModelScene::robotPressed);
	connect(robotItem, &RobotItem::drawTrace, &mModel.worldModel(), &model::WorldModel::appendRobotTrace);

	addItem(robotItem);

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

	robotItem->setRotater(rotater);

	mRobots.insert(robotModel, robotItem);

	emit robotListChanged(robotItem);
}

void D2ModelScene::onRobotRemove(model::RobotModel *robotModel)
{
	RobotItem *robotItem = mRobots[robotModel];

	removeItem(robotItem);
	mRobots.remove(robotModel);

	delete robotItem;

	emit robotListChanged(nullptr);
}

void D2ModelScene::onItemRemoved(QGraphicsItem *item)
{
	removeItem(item);
	utils::DeleteLaterHelper<QGraphicsItem>::deleteLater(item);
}

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

	for (RobotItem * const robotItem : mRobots.values()) {
		robotItem->checkSelection();

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

	emit mousePressed();

	auto initItem = [this, mouseEvent](QGraphicsItem *item) {<commit_msg>Added clarification for deffered items delete<commit_after>#include "d2ModelScene.h"
#include <QtWidgets/QGraphicsSceneMouseEvent>
#include <QtGui/QKeyEvent>
#include <QtGui/QPainter>

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

#include "robotItem.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
		, AbstractView *view
		, QObject *parent)
	: AbstractScene(view, parent)
	, mModel(model)
	, mDrawingAction(none)
{
	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::otherItemAdded, this, &QGraphicsScene::addItem);
	connect(&mModel.worldModel(), &model::WorldModel::itemRemoved, this, &D2ModelScene::onItemRemoved);

	connect(&mModel, &model::Model::robotAdded, this, &D2ModelScene::onRobotAdd);
	connect(&mModel, &model::Model::robotRemoved, this, &D2ModelScene::onRobotRemove);
}

D2ModelScene::~D2ModelScene()
{
}

bool D2ModelScene::oneRobot() const
{
	return mRobots.size() == 1;
}

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

void D2ModelScene::onRobotAdd(model::RobotModel *robotModel)
{
	RobotItem * const robotItem = new RobotItem(robotModel->info().robotImage(), *robotModel);

	connect(robotItem, &RobotItem::changedPosition, this, &D2ModelScene::handleNewRobotPosition);
	connect(robotItem, &RobotItem::mousePressed, this, &D2ModelScene::robotPressed);
	connect(robotItem, &RobotItem::drawTrace, &mModel.worldModel(), &model::WorldModel::appendRobotTrace);

	addItem(robotItem);

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

	robotItem->setRotater(rotater);

	mRobots.insert(robotModel, robotItem);

	emit robotListChanged(robotItem);
}

void D2ModelScene::onRobotRemove(model::RobotModel *robotModel)
{
	RobotItem *robotItem = mRobots[robotModel];

	removeItem(robotItem);
	mRobots.remove(robotModel);

	delete robotItem;

	emit robotListChanged(nullptr);
}

void D2ModelScene::onItemRemoved(QGraphicsItem *item)
{
	removeItem(item);
	// We delete the item not immediately cause in other handlers of WorldModel`s itemRemoved() signal
	// it may still be used.
	utils::DeleteLaterHelper<QGraphicsItem>::deleteLater(item);
}

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

	for (RobotItem * const robotItem : mRobots.values()) {
		robotItem->checkSelection();

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

	emit mousePressed();

	auto initItem = [this, mouseEvent](QGraphicsItem *item) {
{'fraction_non_alphanumeric': 0.14463561337249964, 'fraction_numerical': 0.009232060428031893, 'mean_word_length': 7.73015873015873, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 39, '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': '5628425', 'n_tokens_mistral': 2565, 'n_tokens_neox': 2449, 'n_words': 398}