<commit_before>/*
 * QStationModelItem.cpp
 *
 *  Created on: May 5, 2011
 *      Author: martinc
 */

#include "QStationModelGraphicsItem.h"
#include <iostream>

/////////////////////////////////////////////////////////////////////////////////////////////////
QStationModelGraphicsItem::QStationModelGraphicsItem(
		double x,
		double y,
		double spdKnots,
		double dirMet,
		double tDryC,
		double RH,
		double presOrHeight,
		bool isPres,
		int hh,
		int mm,
		double scale) :
QGraphicsItem(),
_x(x),
_y(y),
_spdKnots(spdKnots),
_dirMet(dirMet),
_tDryC(tDryC),
_RH(RH),
_presOrHeight(presOrHeight),
_isPres(isPres),
_hh(hh),
_mm(mm),
_scale(scale),
_aspectRatio(1.0)
{
	_text[N] = "";
	_text[NE] = "";
	_text[E] = "";
	_text[SE] = "";
	_text[S] = "";
	_text[SW] = "";
	_text[W] = "";
	_text[NW] = "";

	setPos(_x, _y);

	setAcceptedMouseButtons(Qt::LeftButton);

	// turn off transformations. The item will now draw with local
	// scale in terms of screen pixels.
	setFlag(QGraphicsItem::ItemIgnoresTransformations, true);
	setFlag(QGraphicsItem::ItemIsFocusable, true);

	// accept hover events
	setAcceptHoverEvents(true);
}

/////////////////////////////////////////////////////////////////////////////////////////////////
QStationModelGraphicsItem::~QStationModelGraphicsItem() {
}

/////////////////////////////////////////////////////////////////////////////////////////////////
void QStationModelGraphicsItem::mousePressEvent(QGraphicsSceneMouseEvent * event) {

	// Event handling is just stubbed out here, until we find
	// something useful to do with it.

	std::cout << "mouse press " << event->scenePos().x() << _spdKnots
			<< std::endl;
	QGraphicsItem::mousePressEvent(event);
}

/////////////////////////////////////////////////////////////////////////////////////////////////
void QStationModelGraphicsItem::hoverEnterEvent(QGraphicsSceneHoverEvent * event) {

	// Event handling is just stubbed out here, until we find
	// something useful to do with it.

	std::cout << "hover enter " << _spdKnots << std::endl;

	QGraphicsItem::hoverEnterEvent(event);
}

/////////////////////////////////////////////////////////////////////////////////////////////////
void QStationModelGraphicsItem::hoverLeaveEvent(QGraphicsSceneHoverEvent * event) {

	// Event handling is just stubbed out here, until we find
	// something useful to do with it.

	std::cout << " hover leave " << _spdKnots << std::endl;
	QGraphicsItem::hoverEnterEvent(event);
}

/////////////////////////////////////////////////////////////////////////////////////////////////
QRectF QStationModelGraphicsItem::boundingRect() const {

	QRectF r(-_scale, -_scale, 2 * _scale, 2 * _scale);

	return r;
}

/////////////////////////////////////////////////////////////////////////////////////////////////
void QStationModelGraphicsItem::paint(QPainter *painter,
		const QStyleOptionGraphicsItem *option, QWidget *widget) {

	QPen oldPen = painter->pen();

	painter->setPen(QPen("black"));
	drawWindFlag(painter);

	painter->setPen(QPen("blue"));
	drawTextFields(painter);

	painter->setPen(oldPen);
}

/////////////////////////////////////////////////////////////////////////////////////////////////
void QStationModelGraphicsItem::drawTextFields(QPainter* painter) {

	// get the sector and coordinate assignments for this wind direction
	TextSectors sectors(_dirMet, 10);

	// Draw each text field
	QString tdry = QString("%1").arg(_tDryC, 0, 'f', 1);

	QString rh = QString("%1").arg(_RH, 0, 'f', 0);

	double presOrHeight = _presOrHeight;
	if (_isPres) {
		if (_presOrHeight >= 1000.0) {
			presOrHeight = _presOrHeight - 1000.0;
		} else {
			if (_presOrHeight >= 900.0) {
				presOrHeight = _presOrHeight - 900.0;
			}
		}
	}
	QString pht = QString("%1").arg(presOrHeight, 0, 'f', 0);

	int t = _hh * 100 + _mm;
	QString time = QString("%1").arg(t, 4, 10, QChar('0'));	// filled with leading 0's

	drawTextField(painter, sectors, TextSectors::TDRY, tdry);
	drawTextField(painter, sectors, TextSectors::RH, rh);
	drawTextField(painter, sectors, TextSectors::PHT, pht);
	drawTextField(painter, sectors, TextSectors::TIME, time);
}

/////////////////////////////////////////////////////////////////////////////////////////////////
void QStationModelGraphicsItem::drawTextField(QPainter* painter,
		TextSectors& sectors, TextSectors::TEXT_TYPE typ, QString txt) {

	QRect textBox = painter->fontMetrics().boundingRect(txt);

	double xoffset = 0;
	if (sectors._hjust[typ] == TextSectors::RIGHT) {
		xoffset = -textBox.width();
	}

	double yoffset = 0;
	if (sectors._vjust[typ] == TextSectors::TOP) {
		yoffset = textBox.height();
	}

	double x = sectors._x[typ] + xoffset;
	double y = sectors._y[typ] + yoffset;

	painter->drawText(x, y, txt);
}

/////////////////////////////////////////////////////////////////////////////////////////////////
void QStationModelGraphicsItem::drawWindFlag(QPainter *painter) {

	// draw the dot at the center of the flag
	double dotRadius = 3;
	painter->drawEllipse(-dotRadius, -dotRadius, 2 * dotRadius, 2 * dotRadius);
	if (_spdKnots < 0) {
		// winds are missing; draw double circle
		painter->drawEllipse(-1.5 * dotRadius, -1.5 * dotRadius, 3 * dotRadius,
				3 * dotRadius);
	}

	if (_spdKnots < 0.1) {
		// Don't try to draw a flag when wind speed is missing or zero.<commit_msg>Do not draw data field or wind bar on station model if data is missing.<commit_after>/*
 * QStationModelItem.cpp
 *
 *  Created on: May 5, 2011
 *      Author: martinc
 */

#include "QStationModelGraphicsItem.h"
#include <iostream>

/////////////////////////////////////////////////////////////////////////////////////////////////
QStationModelGraphicsItem::QStationModelGraphicsItem(
		double x,
		double y,
		double spdKnots,
		double dirMet,
		double tDryC,
		double RH,
		double presOrHeight,
		bool isPres,
		int hh,
		int mm,
		double scale) :
QGraphicsItem(),
_x(x),
_y(y),
_spdKnots(spdKnots),
_dirMet(dirMet),
_tDryC(tDryC),
_RH(RH),
_presOrHeight(presOrHeight),
_isPres(isPres),
_hh(hh),
_mm(mm),
_scale(scale),
_aspectRatio(1.0)
{
	_text[N] = "";
	_text[NE] = "";
	_text[E] = "";
	_text[SE] = "";
	_text[S] = "";
	_text[SW] = "";
	_text[W] = "";
	_text[NW] = "";

	setPos(_x, _y);

	setAcceptedMouseButtons(Qt::LeftButton);

	// turn off transformations. The item will now draw with local
	// scale in terms of screen pixels.
	setFlag(QGraphicsItem::ItemIgnoresTransformations, true);
	setFlag(QGraphicsItem::ItemIsFocusable, true);

	// accept hover events
	setAcceptHoverEvents(true);
}

/////////////////////////////////////////////////////////////////////////////////////////////////
QStationModelGraphicsItem::~QStationModelGraphicsItem() {
}

/////////////////////////////////////////////////////////////////////////////////////////////////
void QStationModelGraphicsItem::mousePressEvent(QGraphicsSceneMouseEvent * event) {

	// Event handling is just stubbed out here, until we find
	// something useful to do with it.

	std::cout << "mouse press " << event->scenePos().x() << _spdKnots
			<< std::endl;
	QGraphicsItem::mousePressEvent(event);
}

/////////////////////////////////////////////////////////////////////////////////////////////////
void QStationModelGraphicsItem::hoverEnterEvent(QGraphicsSceneHoverEvent * event) {

	// Event handling is just stubbed out here, until we find
	// something useful to do with it.

	std::cout << "hover enter " << _spdKnots << std::endl;

	QGraphicsItem::hoverEnterEvent(event);
}

/////////////////////////////////////////////////////////////////////////////////////////////////
void QStationModelGraphicsItem::hoverLeaveEvent(QGraphicsSceneHoverEvent * event) {

	// Event handling is just stubbed out here, until we find
	// something useful to do with it.

	std::cout << " hover leave " << _spdKnots << std::endl;
	QGraphicsItem::hoverEnterEvent(event);
}

/////////////////////////////////////////////////////////////////////////////////////////////////
QRectF QStationModelGraphicsItem::boundingRect() const {

	QRectF r(-_scale, -_scale, 2 * _scale, 2 * _scale);

	return r;
}

/////////////////////////////////////////////////////////////////////////////////////////////////
void QStationModelGraphicsItem::paint(QPainter *painter,
		const QStyleOptionGraphicsItem *option, QWidget *widget) {

	QPen oldPen = painter->pen();

	painter->setPen(QPen("black"));
	drawWindFlag(painter);

	painter->setPen(QPen("blue"));
	drawTextFields(painter);

	painter->setPen(oldPen);
}

/////////////////////////////////////////////////////////////////////////////////////////////////
void QStationModelGraphicsItem::drawTextFields(QPainter* painter) {

	// get the sector and coordinate assignments for this wind direction
	TextSectors sectors(_dirMet, 10);

	// Draw each text field
	QString tdry = QString("%1").arg(_tDryC, 0, 'f', 1);

	QString rh = QString("%1").arg(_RH, 0, 'f', 1);

	double presOrHeight = _presOrHeight;
	if (_isPres) {
		if (_presOrHeight >= 1000.0) {
			presOrHeight = _presOrHeight - 1000.0;
		} else {
			if (_presOrHeight >= 900.0) {
				presOrHeight = _presOrHeight - 900.0;
			}
		}
	}
	QString pht = QString("%1").arg(presOrHeight, 0, 'f', 0);

	int t = _hh * 100 + _mm;
	QString time = QString("%1").arg(t, 4, 10, QChar('0'));	// filled with leading 0's

	if (_tDryC != -999.0)
		drawTextField(painter, sectors, TextSectors::TDRY, tdry);
	if (_RH != -999.0)
		drawTextField(painter, sectors, TextSectors::RH, rh);
	if (_presOrHeight != -999.0)
		drawTextField(painter, sectors, TextSectors::PHT, pht);
	drawTextField(painter, sectors, TextSectors::TIME, time);
}

/////////////////////////////////////////////////////////////////////////////////////////////////
void QStationModelGraphicsItem::drawTextField(QPainter* painter,
		TextSectors& sectors, TextSectors::TEXT_TYPE typ, QString txt) {

	QRect textBox = painter->fontMetrics().boundingRect(txt);

	double xoffset = 0;
	if (sectors._hjust[typ] == TextSectors::RIGHT) {
		xoffset = -textBox.width();
	}

	double yoffset = 0;
	if (sectors._vjust[typ] == TextSectors::TOP) {
		yoffset = textBox.height();
	}

	double x = sectors._x[typ] + xoffset;
	double y = sectors._y[typ] + yoffset;

	painter->drawText(x, y, txt);
}

/////////////////////////////////////////////////////////////////////////////////////////////////
void QStationModelGraphicsItem::drawWindFlag(QPainter *painter) {

	// draw the dot at the center of the flag
	double dotRadius = 3;
	painter->drawEllipse(-dotRadius, -dotRadius, 2 * dotRadius, 2 * dotRadius);
	if (_spdKnots == 0.0) {
		// calm winds, draw double circle
		painter->drawEllipse(-1.5 * dotRadius, -1.5 * dotRadius, 3 * dotRadius,
				3 * dotRadius);
	}

	if (_spdKnots < 0.1) {
		// Don't try to draw a flag when wind speed is missing or zero.
{'fraction_non_alphanumeric': 0.29676997759522034, 'fraction_numerical': 0.01260268857356236, 'mean_word_length': 2.0573630136986303, 'pattern_counts': {'":': 0, '<': 48, '<?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': '6485044', 'n_tokens_mistral': 3463, 'n_tokens_neox': 3172, 'n_words': 759}