<commit_before>﻿#include "sensorsGraph.h"
#include "ui_sensorsGraph.h"

using namespace utils::sensorsGraph;

struct SensorsGraph::TrackObject {
	int index;
	QString inParserName;
	QString displayName;

	TrackObject(int const &id, QString const &internalName, QString const &externalName)
		: index(id)
		, inParserName(internalName)
		, displayName(externalName)
	{
	}

	TrackObject(int const &id)
		: index(id)
		, inParserName("")
		, displayName("")
	{
	}

	bool operator ==(TrackObject const &other) const
	{
		return index == other.index;
	}
};

SensorsGraph::SensorsGraph(utils::ExpressionsParser const *parser, QWidget *parent)
	: QWidget(parent)
	, mUi(new Ui::SensorsGraph)
	, mParser(parser)
	, mUpdateInterval(50)
	, mCurrentSlot(0)
{
	mUi->setupUi(this);
	initGui();
	makeConnections();

	mUpdateTimer.start(mUpdateInterval);
	connect(&mUpdateTimer, SIGNAL(timeout()), this, SLOT(updateValues()));
	mPlotFrame->centerOn(mPlotFrame->sceneRect().center());
}

SensorsGraph::~SensorsGraph()
{
	delete mPlotFrame;
	delete mUi;
}

void SensorsGraph::addTrackingObject(int const index, QString const &inParserName, QString const &displayName)
{
	TrackObject newObject(index, inParserName, displayName);
	if (mWatchList.contains(newObject)) {
		mWatchList.removeOne(newObject);
	}

	mWatchList.append(newObject);
	watchListChanged();
}

void SensorsGraph::removeTracking(const int index)
{
	TrackObject temp(index);
	mWatchList.removeOne(temp);
	watchListChanged();
}

void SensorsGraph::clearTrackingObjects()
{
	for (int i = 0; i < mWatchList.count(); ++i) {
		removeTracking(i);
	}
}

void SensorsGraph::paintEvent(QPaintEvent *event)
{
	Q_UNUSED(event);
	qreal const verticalCoefficent = 0.8;
	qreal const horizontalCoefficent = 0.8;
	qreal const plotWidth = size().width() * horizontalCoefficent;
	qreal const plotHeight = size().height() * verticalCoefficent;

	mPlotFrame->setSceneRect(-plotWidth, -plotHeight, plotWidth, plotHeight);
	mPlotFrame->setFixedSize(plotWidth, plotHeight);
}

void SensorsGraph::initGui()
{
	setupToolElements();

	mToolLayout.addWidget(&mStopButton, 0);
	mToolLayout.addWidget(&mStartButton, 0);
	mToolLayout.addWidget(&mZoomInButton, 0);
	mToolLayout.addWidget(&mZoomOutButton, 0);
	mToolLayout.addWidget(&mResetButton, 0);

	mPlotFrame = new SensorViewer(this);

	mUi->mainLayout->addWidget(mPlotFrame, 0, 0);
	mUi->mainLayout->addLayout(&mToolLayout, 0, 1);
	mUi->mainLayout->addWidget(&mSlotComboBox, 1, 0);
	mUi->mainLayout->setAlignment(mPlotFrame, Qt::AlignTop);
	mUi->mainLayout->setAlignment(&mSlotComboBox, Qt::AlignTop);
	mPlotFrame->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
}

void SensorsGraph::setupToolElements()
{
	QSize const iconSize(20, 20);

	mStopButton.setIcon(QPixmap("://graphicsWatcher/icons/stop_btn.png"));
	mStopButton.setIconSize(iconSize);

	mStartButton.setIcon(QPixmap("://graphicsWatcher/icons/start_btn.png"));
	mStartButton.setIconSize(iconSize);

	mResetButton.setIcon(QPixmap("://graphicsWatcher/icons/reset_btn.png"));
	mResetButton.setIconSize(iconSize);

	mZoomInButton.setIcon(QPixmap("://graphicsWatcher/icons/zoomIn_btn.png"));
	mZoomInButton.setIconSize(iconSize);

	mZoomOutButton.setIcon(QPixmap("://graphicsWatcher/icons/zoomOut_btn.png"));
	mZoomOutButton.setIconSize(iconSize);
}

void SensorsGraph::makeConnections()
{
	connect(&mStopButton, SIGNAL(clicked()), mPlotFrame, SLOT(stopJob()));
	connect(&mStartButton, SIGNAL(clicked()), mPlotFrame, SLOT(startJob()));
	connect(&mResetButton, SIGNAL(clicked()), mPlotFrame, SLOT(clear()));
	connect(&mZoomInButton, SIGNAL(clicked()), mPlotFrame, SLOT(zoomIn()));
	connect(&mZoomOutButton, SIGNAL(clicked()), mPlotFrame, SLOT(zoomOut()));
	connect(&mSlotComboBox, SIGNAL(currentIndexChanged(int)), mPlotFrame, SLOT(onSensorChange()));
	connect(&mSlotComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(setCurrentSensor(int)));
}

void SensorsGraph::watchListChanged()
{
	mSlotComboBox.clear();
	if (mWatchList.isEmpty()) {
		return;
	}

	foreach (TrackObject const &item, mWatchList) {
		mSlotComboBox.addItem(tr(item.displayName.toUtf8()), item.index);
	}
}

void SensorsGraph::sensorsInput(int const slotIndex, qreal const value)
{
	if (mCurrentSlot == slotIndex) {
		mPlotFrame->setNextValue(value);
	}
}

void SensorsGraph::setCurrentSensor(const int newSlotIndex)
{
	mCurrentSlot = newSlotIndex;
}

void SensorsGraph::startJob()
{
	if (mWatchList.isEmpty()) {
		return;
	}
	mUpdateTimer.start(mUpdateInterval);
	mPlotFrame->startJob();
}

void SensorsGraph::updateValues()
{
	int const notExists = -1;
	QMap<QString, Number *> const &variables = mParser->variables();

	TrackObject currentObject(mCurrentSlot);
	int const index = mWatchList.indexOf(currentObject);
	if (index != notExists) {
		sensorsInput(mCurrentSlot, variables[mWatchList.at(index).inParserName]->value().toDouble());
	}
}

void SensorsGraph::stopJob()
{
	mUpdateTimer.stop();
	mPlotFrame->stopJob();
}<commit_msg>Fixed segfault on opening some nxt models in trik mode<commit_after>﻿#include "sensorsGraph.h"
#include "ui_sensorsGraph.h"

using namespace utils::sensorsGraph;

struct SensorsGraph::TrackObject {
	int index;
	QString inParserName;
	QString displayName;

	TrackObject(int const &id, QString const &internalName, QString const &externalName)
		: index(id)
		, inParserName(internalName)
		, displayName(externalName)
	{
	}

	TrackObject(int const &id)
		: index(id)
		, inParserName("")
		, displayName("")
	{
	}

	bool operator ==(TrackObject const &other) const
	{
		return index == other.index;
	}
};

SensorsGraph::SensorsGraph(utils::ExpressionsParser const *parser, QWidget *parent)
	: QWidget(parent)
	, mUi(new Ui::SensorsGraph)
	, mParser(parser)
	, mUpdateInterval(100)
	, mCurrentSlot(0)
{
	mUi->setupUi(this);
	initGui();
	makeConnections();

	mUpdateTimer.start(mUpdateInterval);
	connect(&mUpdateTimer, SIGNAL(timeout()), this, SLOT(updateValues()));
	mPlotFrame->centerOn(mPlotFrame->sceneRect().center());
}

SensorsGraph::~SensorsGraph()
{
	delete mPlotFrame;
	delete mUi;
}

void SensorsGraph::addTrackingObject(int const index, QString const &inParserName, QString const &displayName)
{
	TrackObject newObject(index, inParserName, displayName);
	if (mWatchList.contains(newObject)) {
		mWatchList.removeOne(newObject);
	}

	mWatchList.append(newObject);
	watchListChanged();
}

void SensorsGraph::removeTracking(const int index)
{
	TrackObject temp(index);
	mWatchList.removeOne(temp);
	watchListChanged();
}

void SensorsGraph::clearTrackingObjects()
{
	for (int i = 0; i < mWatchList.count(); ++i) {
		removeTracking(i);
	}
}

void SensorsGraph::paintEvent(QPaintEvent *event)
{
	Q_UNUSED(event);
	qreal const verticalCoefficent = 0.8;
	qreal const horizontalCoefficent = 0.8;
	qreal const plotWidth = size().width() * horizontalCoefficent;
	qreal const plotHeight = size().height() * verticalCoefficent;

	mPlotFrame->setSceneRect(-plotWidth, -plotHeight, plotWidth, plotHeight);
	mPlotFrame->setFixedSize(plotWidth, plotHeight);
}

void SensorsGraph::initGui()
{
	setupToolElements();

	mToolLayout.addWidget(&mStopButton, 0);
	mToolLayout.addWidget(&mStartButton, 0);
	mToolLayout.addWidget(&mZoomInButton, 0);
	mToolLayout.addWidget(&mZoomOutButton, 0);
	mToolLayout.addWidget(&mResetButton, 0);

	mPlotFrame = new SensorViewer(this);

	mUi->mainLayout->addWidget(mPlotFrame, 0, 0);
	mUi->mainLayout->addLayout(&mToolLayout, 0, 1);
	mUi->mainLayout->addWidget(&mSlotComboBox, 1, 0);
	mUi->mainLayout->setAlignment(mPlotFrame, Qt::AlignTop);
	mUi->mainLayout->setAlignment(&mSlotComboBox, Qt::AlignTop);
	mPlotFrame->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
}

void SensorsGraph::setupToolElements()
{
	QSize const iconSize(20, 20);

	mStopButton.setIcon(QPixmap("://graphicsWatcher/icons/stop_btn.png"));
	mStopButton.setIconSize(iconSize);

	mStartButton.setIcon(QPixmap("://graphicsWatcher/icons/start_btn.png"));
	mStartButton.setIconSize(iconSize);

	mResetButton.setIcon(QPixmap("://graphicsWatcher/icons/reset_btn.png"));
	mResetButton.setIconSize(iconSize);

	mZoomInButton.setIcon(QPixmap("://graphicsWatcher/icons/zoomIn_btn.png"));
	mZoomInButton.setIconSize(iconSize);

	mZoomOutButton.setIcon(QPixmap("://graphicsWatcher/icons/zoomOut_btn.png"));
	mZoomOutButton.setIconSize(iconSize);
}

void SensorsGraph::makeConnections()
{
	connect(&mStopButton, SIGNAL(clicked()), mPlotFrame, SLOT(stopJob()));
	connect(&mStartButton, SIGNAL(clicked()), mPlotFrame, SLOT(startJob()));
	connect(&mResetButton, SIGNAL(clicked()), mPlotFrame, SLOT(clear()));
	connect(&mZoomInButton, SIGNAL(clicked()), mPlotFrame, SLOT(zoomIn()));
	connect(&mZoomOutButton, SIGNAL(clicked()), mPlotFrame, SLOT(zoomOut()));
	connect(&mSlotComboBox, SIGNAL(currentIndexChanged(int)), mPlotFrame, SLOT(onSensorChange()));
	connect(&mSlotComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(setCurrentSensor(int)));
}

void SensorsGraph::watchListChanged()
{
	mSlotComboBox.clear();
	if (mWatchList.isEmpty()) {
		return;
	}

	foreach (TrackObject const &item, mWatchList) {
		mSlotComboBox.addItem(tr(item.displayName.toUtf8()), item.index);
	}
}

void SensorsGraph::sensorsInput(int const slotIndex, qreal const value)
{
	if (mCurrentSlot == slotIndex) {
		mPlotFrame->setNextValue(value);
	}
}

void SensorsGraph::setCurrentSensor(const int newSlotIndex)
{
	mCurrentSlot = newSlotIndex;
}

void SensorsGraph::startJob()
{
	if (mWatchList.isEmpty()) {
		return;
	}
	mUpdateTimer.start(mUpdateInterval);
	mPlotFrame->startJob();
}

void SensorsGraph::updateValues()
{
	int const notExists = -1;
	QMap<QString, Number *> const &variables = mParser->variables();

	TrackObject currentObject(mCurrentSlot);
	int const index = mWatchList.indexOf(currentObject);
	if (index != notExists) {
		Number *const number = variables[mWatchList.at(index).inParserName];
		if (number) {
			sensorsInput(mCurrentSlot, number->value().toDouble());
		}
	}
}

void SensorsGraph::stopJob()
{
	mUpdateTimer.stop();
	mPlotFrame->stopJob();
}
{'fraction_non_alphanumeric': 0.14732946589317863, 'fraction_numerical': 0.005101020204040808, 'mean_word_length': 7.20935960591133, 'pattern_counts': {'":': 10, '<': 7, '<?xml version=': 0, '>': 47, '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': '3092961', 'n_tokens_mistral': 3622, 'n_tokens_neox': 3454, 'n_words': 559}