<commit_before>
/*!
	\file

	\author <NAME> (igor.mironchik at gmail dot com).

	Copyright (c) 2014 <NAME>

	Permission is hereby granted, free of charge, to any person
	obtaining a copy of this software and associated documentation
	files (the "Software"), to deal in the Software without
	restriction, including without limitation the rights to use,
	copy, modify, merge, publish, distribute, sublicense, and/or sell
	copies of the Software, and to permit persons to whom the
	Software is furnished to do so, subject to the following
	conditions:

	The above copyright notice and this permission notice shall be
	included in all copies or substantial portions of the Software.

	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
	EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
	OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
	NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
	HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
	WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
	FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
	OTHER DEALINGS IN THE SOFTWARE.
*/

// QtMWidgets include.
#include "switch.hpp"
#include "private/color.hpp"

// Qt include.
#include <QStyleOption>
#include <QPainter>
#include <QBitmap>
#include <QRegion>
#include <QLinearGradient>
#include <QMouseEvent>


namespace QtMWidgets {

//
// SwitchPrivate
//

class SwitchPrivate {
public:
	explicit SwitchPrivate( Switch * parent )
		:	q( parent )
		,	state( Switch::AcceptedUncheck )
		,	radius( 0 )
		,	textWidth( 0 )
		,	offset( 0 )
		,	leftMouseButtonPressed( false )
	{
		init();
	}

	bool isChecked() const;
	void setState( Switch::State st );
	void init();
	void emitSignals();
	void initPaintRegion( const QRect & r );
	void drawText( QPainter * p, const QStyleOption & opt,
		const QColor & on, const QColor & off );
	void drawSlider( QPainter * p, const QStyleOption & opt,
		const QColor & lightColor, const QColor & borderColor );
	void initOffset( const QRect & r );

	Switch * q;
	Switch::State state;
	QString onText;
	QString offText;
	QColor onColor;
	int radius;
	int textWidth;
	QRegion paintRegion;
	int offset;
	bool leftMouseButtonPressed;
	QPoint mousePos;
}; // class SwitchPrivate

bool
SwitchPrivate::isChecked() const
{
	switch( state )
	{
		case Switch::NotAcceptedUncheck :
		case Switch::AcceptedUncheck :
			return false;

		case Switch::NotAcceptedCheck :
		case Switch::AcceptedCheck :
			return true;

		default :
			return false;
	}
}

void
SwitchPrivate::setState( Switch::State st )
{
	if( state != st )
	{
		state = st;

		QStyleOption opt;
		opt.initFrom( q );

		initOffset( opt.rect );

		emitSignals();

		q->update();
	}
}

void
SwitchPrivate::init()
{
	QStyleOption opt;
	opt.initFrom( q );

	onColor = opt.palette.color( QPalette::Highlight );
}

void
SwitchPrivate::emitSignals()
{
	emit q->toggled( isChecked() );
	emit q->stateChanged( (int) state );
}

void
SwitchPrivate::initPaintRegion( const QRect & r )
{
	QBitmap mask( r.width(), r.height() );
	mask.clear();

	QPainter p( &mask );
	p.setRenderHint( QPainter::Antialiasing );
	p.setPen( Qt::color1 );
	p.setBrush( Qt::color1 );
	p.drawRoundedRect( -1, -1, r.width() + 2,
		radius * 2 + 2, radius + 1, radius + 1 );

	paintRegion = QRegion( mask );
}

void
SwitchPrivate::drawText( QPainter * p, const QStyleOption & opt,
	const QColor & on, const QColor & off )
{
	const QRect r( radius, radius / 2, opt.rect.width() - radius * 2, radius );

	switch( state )
	{
		case Switch::NotAcceptedCheck :
		case Switch::AcceptedCheck :
		{
			if( !onText.isEmpty() )
			{
				p->setPen( on );
				p->drawText( r, Qt::AlignLeft | Qt::TextSingleLine, onText );
			}
		}
		break;

		case Switch::NotAcceptedUncheck :
		case Switch::AcceptedUncheck :
		{
			if( !offText.isEmpty() )
			{
				p->setPen( off );
				p->drawText( r, Qt::AlignRight | Qt::TextSingleLine, offText );
			}
		}
		break;
	}
}

void
SwitchPrivate::drawSlider( QPainter * p, const QStyleOption & opt,
	const QColor & lightColor, const QColor & borderColor )
{
	Q_UNUSED( opt )

	p->setPen( QPen( borderColor, 2 ) );
	p->setBrush( lightColor );<commit_msg>Text in Switch now vertical aligned too.<commit_after>
/*!
	\file

	\author <NAME> (igor.mironchik at gmail dot com).

	Copyright (c) 2014 <NAME>

	Permission is hereby granted, free of charge, to any person
	obtaining a copy of this software and associated documentation
	files (the "Software"), to deal in the Software without
	restriction, including without limitation the rights to use,
	copy, modify, merge, publish, distribute, sublicense, and/or sell
	copies of the Software, and to permit persons to whom the
	Software is furnished to do so, subject to the following
	conditions:

	The above copyright notice and this permission notice shall be
	included in all copies or substantial portions of the Software.

	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
	EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
	OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
	NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
	HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
	WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
	FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
	OTHER DEALINGS IN THE SOFTWARE.
*/

// QtMWidgets include.
#include "switch.hpp"
#include "private/color.hpp"

// Qt include.
#include <QStyleOption>
#include <QPainter>
#include <QBitmap>
#include <QRegion>
#include <QLinearGradient>
#include <QMouseEvent>


namespace QtMWidgets {

//
// SwitchPrivate
//

class SwitchPrivate {
public:
	explicit SwitchPrivate( Switch * parent )
		:	q( parent )
		,	state( Switch::AcceptedUncheck )
		,	radius( 0 )
		,	textWidth( 0 )
		,	offset( 0 )
		,	leftMouseButtonPressed( false )
	{
		init();
	}

	bool isChecked() const;
	void setState( Switch::State st );
	void init();
	void emitSignals();
	void initPaintRegion( const QRect & r );
	void drawText( QPainter * p, const QStyleOption & opt,
		const QColor & on, const QColor & off );
	void drawSlider( QPainter * p, const QStyleOption & opt,
		const QColor & lightColor, const QColor & borderColor );
	void initOffset( const QRect & r );

	Switch * q;
	Switch::State state;
	QString onText;
	QString offText;
	QColor onColor;
	int radius;
	int textWidth;
	QRegion paintRegion;
	int offset;
	bool leftMouseButtonPressed;
	QPoint mousePos;
}; // class SwitchPrivate

bool
SwitchPrivate::isChecked() const
{
	switch( state )
	{
		case Switch::NotAcceptedUncheck :
		case Switch::AcceptedUncheck :
			return false;

		case Switch::NotAcceptedCheck :
		case Switch::AcceptedCheck :
			return true;

		default :
			return false;
	}
}

void
SwitchPrivate::setState( Switch::State st )
{
	if( state != st )
	{
		state = st;

		QStyleOption opt;
		opt.initFrom( q );

		initOffset( opt.rect );

		emitSignals();

		q->update();
	}
}

void
SwitchPrivate::init()
{
	QStyleOption opt;
	opt.initFrom( q );

	onColor = opt.palette.color( QPalette::Highlight );
}

void
SwitchPrivate::emitSignals()
{
	emit q->toggled( isChecked() );
	emit q->stateChanged( (int) state );
}

void
SwitchPrivate::initPaintRegion( const QRect & r )
{
	QBitmap mask( r.width(), r.height() );
	mask.clear();

	QPainter p( &mask );
	p.setRenderHint( QPainter::Antialiasing );
	p.setPen( Qt::color1 );
	p.setBrush( Qt::color1 );
	p.drawRoundedRect( -1, -1, r.width() + 2,
		radius * 2 + 2, radius + 1, radius + 1 );

	paintRegion = QRegion( mask );
}

void
SwitchPrivate::drawText( QPainter * p, const QStyleOption & opt,
	const QColor & on, const QColor & off )
{
	const QRect r( radius, 0, opt.rect.width() - radius * 2, radius * 2 );

	switch( state )
	{
		case Switch::NotAcceptedCheck :
		case Switch::AcceptedCheck :
		{
			if( !onText.isEmpty() )
			{
				p->setPen( on );
				p->drawText( r,
					Qt::AlignLeft | Qt::TextSingleLine | Qt::AlignVCenter,
					onText );
			}
		}
		break;

		case Switch::NotAcceptedUncheck :
		case Switch::AcceptedUncheck :
		{
			if( !offText.isEmpty() )
			{
				p->setPen( off );
				p->drawText( r,
					Qt::AlignRight | Qt::TextSingleLine | Qt::AlignVCenter,
					offText );
			}
		}
		break;
	}
}

void
SwitchPrivate::drawSlider( QPainter * p, const QStyleOption & opt,
	const QColor & lightColor, const QColor & borderColor )
{
	Q_UNUSED( opt )

	p->setPen( QPen( borderColor, 2 ) );
	p->setBrush( lightColor );
{'fraction_non_alphanumeric': 0.1125177472787506, 'fraction_numerical': 0.004614292475153809, 'mean_word_length': 3.8889531521110468, 'pattern_counts': {'":': 0, '<': 19, '<?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': '2588263', 'n_tokens_mistral': 3054, 'n_tokens_neox': 2798, 'n_words': 912}