/**********************************************************************
 * LeechCraft - modular cross-platform feature rich internet client.
 * Copyright (C) 2011-2012  Minh Ngo
 * Copyright (C) 2006-2013  Georg Rudoy
 *
 * Boost Software License - Version 1.0 - August 17th, 2003
 *
 * Permission is hereby granted, free of charge, to any person or organization
 * obtaining a copy of the software and accompanying documentation covered by
 * this license (the "Software") to use, reproduce, display, distribute,
 * execute, and transmit the Software, and to prepare derivative works of the
 * Software, and to permit third-parties to whom the Software is furnished to
 * do so, all subject to the following:
 *
 * The copyright notices in the Software and this entire statement, including
 * the above license grant, this restriction and the following disclaimer,
 * must be included in all copies of the Software, in whole or in part, and
 * all derivative works of the Software, unless such copies or derivative
 * works are solely in the form of machine-executable object code generated by
 * a source language processor.
 *
 * 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, TITLE AND NON-INFRINGEMENT. IN NO EVENT
 * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
 * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 **********************************************************************/

#include "volumeslider.h"
#include <QMouseEvent>
#include <QPainter>
#include <QTimer>
#include <QStyle>

namespace LeechCraft
{
namespace Laure
{
	VolumeSlider::VolumeSlider (QWidget *parent)
	: QSlider (parent)
	, VolumeSliderInset_ (QPixmap (":/plugins/laure/resources/img/volumeslider-inset.png"))
	{
		GenerateGradient ();
	}

	void VolumeSlider::GenerateGradient ()
	{
		const QImage mask (":/plugins/laure/resources/img/volumeslider-gradient.png");
		QImage gradient_image (mask.size (),
				QImage::Format_ARGB32_Premultiplied);
		QPainter p (&gradient_image);

		QLinearGradient gradient (gradient_image.rect ().topLeft (),
				gradient_image.rect ().topRight ());
		gradient.setColorAt (0, palette ().color (QPalette::Background));
		gradient.setColorAt (1, palette ().color (QPalette::Highlight));
		p.fillRect (gradient_image.rect (), QBrush (gradient));

		p.setCompositionMode (QPainter::CompositionMode_DestinationIn);
		p.drawImage (0, 0, mask);
		p.end ();

		VolumeSliderGradient_ = QPixmap::fromImage (gradient_image);
	}

	void VolumeSlider::paintEvent (QPaintEvent*)
	{
		const int padding = 7;
		const int offset = double ((width () - 2 * padding) * value ()) / maximum ();

		QPainter p (this);
		p.drawPixmap(0, 0, VolumeSliderGradient_, 0, 0, offset + padding, 0);
		p.drawPixmap (0, 0,  VolumeSliderInset_);

		p.setPen (palette ().color (QPalette::Disabled, QPalette::Text).dark ());
		QFont font;
		font.setPixelSize (9);
		p.setFont (font);
		const QRect rect (0, 0, 34, 15);
		p.drawText (rect, Qt::AlignRight | Qt::AlignVCenter, QString::number (value ()) + '%');
	}

	void VolumeSlider::mousePressEvent (QMouseEvent *ev)
	{
		if (ev->button () != Qt::RightButton)
			QSlider::setValue (QStyle::sliderValueFromPosition(minimum (),
							maximum (), ev->pos ().x (), width () - 2));
	}

	void VolumeSlider::mouseMoveEvent (QMouseEvent *ev)
	{
		mousePressEvent (ev);
	}
}
}
