<commit_before>/* Copyright 2007-2016 QReal Research Group
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License. */

#include "editorViewScene.h"

#include <QtGui/QClipboard>
#include <QtWidgets/QApplication>
#include <QtWidgets/QGraphicsTextItem>
#include <QtWidgets/QGraphicsItem>
#include <QtWidgets/QGraphicsDropShadowEffect>
#include <QtWidgets/QMenu>
#include <math.h>
#include <qmath.h>

#include <qrkernel/definitions.h>
#include <qrkernel/logging.h>
#include <qrgui/models/models.h>
#include <qrgui/mouseGestures/mouseMovementManager.h>
#include <qrgui/mouseGestures/dummyMouseMovementManager.h>
#include <metaMetaModel/nodeElementType.h>
#include <metaMetaModel/edgeElementType.h>

#include "editor/sceneCustomizer.h"
#include "editor/labels/label.h"
#include "editor/commands/removeAndUpdateCommand.h"
#include "editor/commands/createAndUpdatePatternCommand.h"
#include "editor/commands/insertIntoEdgeCommand.h"
#include "editor/commands/reshapeEdgeCommand.h"
#include "editor/commands/resizeCommand.h"
#include "editor/commands/expandCommand.h"

using namespace qReal;
using namespace qReal::commands;
using namespace qReal::gui::editor;
using namespace qReal::gui::editor::commands;

EditorViewScene::EditorViewScene(const models::Models &models
		, Controller &controller
		, const SceneCustomizer &sceneCustomizer
		, const Id &rootId
		, QObject *parent)
	: QGraphicsScene(parent)
	, mModels(models)
	, mEditorManager(models.logicalModelAssistApi().editorManagerInterface())
	, mController(controller)
	, mSceneCustomizer(sceneCustomizer)
	, mRootId(rootId)
	, mLastCreatedFromLinker(nullptr)
	, mClipboardHandler(controller, models)
	, mRightButtonPressed(false)
	, mLeftButtonPressed(false)
	, mHighlightNode(nullptr)
	, mMouseMovementManager(new gestures::DummyMouseMovementManager(mRootId, mEditorManager))
	, mActionSignalMapper(new QSignalMapper(this))
	, mTimer(new QTimer(this))
	, mTimerForArrowButtons(new QTimer(this))
	, mOffset(QPointF(0, 0))
	, mShouldReparentItems(false)
	, mTopLeftCorner(new QGraphicsRectItem(0, 0, 1, 1))
	, mBottomRightCorner(new QGraphicsRectItem(0, 0, 1, 1))
	, mMouseGesturesEnabled(false)
	, mExploser(models, controller, sceneCustomizer, this)
	, mActionDeleteFromDiagram(nullptr)

{
	mNeedDrawGrid = SettingsManager::value("ShowGrid").toBool();
	mWidthOfGrid = static_cast<qreal>(SettingsManager::value("GridWidth").toInt()) / 100;
	mRealIndexGrid = SettingsManager::value("IndexGrid").toInt();

	setItemIndexMethod(NoIndex);
	setEnabled(false);

	initCorners();
	initializeActions();

	connect(mTimer, SIGNAL(timeout()), this, SLOT(getObjectByGesture()));
	connect(mTimerForArrowButtons, SIGNAL(timeout()), this, SLOT(updateMovedElements()));
	connect(this, &QGraphicsScene::selectionChanged, this, &EditorViewScene::deselectLabels);
	connect(&mExploser, &view::details::ExploserView::goTo, this, &EditorViewScene::goTo);
	connect(&mExploser, &view::details::ExploserView::refreshPalette, this, &EditorViewScene::refreshPalette);
	connect(&mExploser, &view::details::ExploserView::openShapeEditor, this, &EditorViewScene::openShapeEditor);
	connect(&mExploser, &view::details::ExploserView::expandElement, [=](const Id &element) {
		if (NodeElement * const node = getNodeById(element)) {
			mController.execute(new ExpandCommand(node));
		}
	});
}

EditorViewScene::~EditorViewScene()
{
	/// @todo Hack: if gestures manager is not initialized yet then it should not be deleted. Yep, memory will leak,
	/// but it is a pretty rare scenario to give a f*ck right now.
	if (!mMouseMovementManager->gesturesInitialized()) {
		mMouseMovementManager.take();
	}
}

qreal EditorViewScene::realIndexGrid()
{
	return mRealIndexGrid;
}

void EditorViewScene::setRealIndexGrid(qreal newIndexGrid)
{
	mRealIndexGrid = newIndexGrid;
}<commit_msg>Fixed segfaulting when closing tab with selection<commit_after>/* Copyright 2007-2016 QReal Research Group
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License. */

#include "editorViewScene.h"

#include <QtGui/QClipboard>
#include <QtWidgets/QApplication>
#include <QtWidgets/QGraphicsTextItem>
#include <QtWidgets/QGraphicsItem>
#include <QtWidgets/QGraphicsDropShadowEffect>
#include <QtWidgets/QMenu>
#include <math.h>
#include <qmath.h>

#include <qrkernel/definitions.h>
#include <qrkernel/logging.h>
#include <qrgui/models/models.h>
#include <qrgui/mouseGestures/mouseMovementManager.h>
#include <qrgui/mouseGestures/dummyMouseMovementManager.h>
#include <metaMetaModel/nodeElementType.h>
#include <metaMetaModel/edgeElementType.h>

#include "editor/sceneCustomizer.h"
#include "editor/labels/label.h"
#include "editor/commands/removeAndUpdateCommand.h"
#include "editor/commands/createAndUpdatePatternCommand.h"
#include "editor/commands/insertIntoEdgeCommand.h"
#include "editor/commands/reshapeEdgeCommand.h"
#include "editor/commands/resizeCommand.h"
#include "editor/commands/expandCommand.h"

using namespace qReal;
using namespace qReal::commands;
using namespace qReal::gui::editor;
using namespace qReal::gui::editor::commands;

EditorViewScene::EditorViewScene(const models::Models &models
		, Controller &controller
		, const SceneCustomizer &sceneCustomizer
		, const Id &rootId
		, QObject *parent)
	: QGraphicsScene(parent)
	, mModels(models)
	, mEditorManager(models.logicalModelAssistApi().editorManagerInterface())
	, mController(controller)
	, mSceneCustomizer(sceneCustomizer)
	, mRootId(rootId)
	, mLastCreatedFromLinker(nullptr)
	, mClipboardHandler(controller, models)
	, mRightButtonPressed(false)
	, mLeftButtonPressed(false)
	, mHighlightNode(nullptr)
	, mMouseMovementManager(new gestures::DummyMouseMovementManager(mRootId, mEditorManager))
	, mActionSignalMapper(new QSignalMapper(this))
	, mTimer(new QTimer(this))
	, mTimerForArrowButtons(new QTimer(this))
	, mOffset(QPointF(0, 0))
	, mShouldReparentItems(false)
	, mTopLeftCorner(new QGraphicsRectItem(0, 0, 1, 1))
	, mBottomRightCorner(new QGraphicsRectItem(0, 0, 1, 1))
	, mMouseGesturesEnabled(false)
	, mExploser(models, controller, sceneCustomizer, this)
	, mActionDeleteFromDiagram(nullptr)

{
	mNeedDrawGrid = SettingsManager::value("ShowGrid").toBool();
	mWidthOfGrid = static_cast<qreal>(SettingsManager::value("GridWidth").toInt()) / 100;
	mRealIndexGrid = SettingsManager::value("IndexGrid").toInt();

	setItemIndexMethod(NoIndex);
	setEnabled(false);

	initCorners();
	initializeActions();

	connect(mTimer, SIGNAL(timeout()), this, SLOT(getObjectByGesture()));
	connect(mTimerForArrowButtons, SIGNAL(timeout()), this, SLOT(updateMovedElements()));
	connect(this, &QGraphicsScene::selectionChanged, this, &EditorViewScene::deselectLabels);
	connect(&mExploser, &view::details::ExploserView::goTo, this, &EditorViewScene::goTo);
	connect(&mExploser, &view::details::ExploserView::refreshPalette, this, &EditorViewScene::refreshPalette);
	connect(&mExploser, &view::details::ExploserView::openShapeEditor, this, &EditorViewScene::openShapeEditor);
	connect(&mExploser, &view::details::ExploserView::expandElement, [=](const Id &element) {
		if (NodeElement * const node = getNodeById(element)) {
			mController.execute(new ExpandCommand(node));
		}
	});
}

EditorViewScene::~EditorViewScene()
{
	clearScene();
	/// @todo Hack: if gestures manager is not initialized yet then it should not be deleted. Yep, memory will leak,
	/// but it is a pretty rare scenario to give a f*ck right now.
	if (!mMouseMovementManager->gesturesInitialized()) {
		mMouseMovementManager.take();
	}
}

qreal EditorViewScene::realIndexGrid()
{
	return mRealIndexGrid;
}

void EditorViewScene::setRealIndexGrid(qreal newIndexGrid)
{
	mRealIndexGrid = newIndexGrid;
}
{'fraction_non_alphanumeric': 0.12119458270633175, 'fraction_numerical': 0.005787706910522051, 'mean_word_length': 7.236415633937083, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '4604340', 'n_tokens_mistral': 2785, 'n_tokens_neox': 2694, 'n_words': 585}