/*  This file is part of the KDE project
    Copyright (C) 2007 Matthias Kretz <kretz@kde.org>

    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License as
    published by the Free Software Foundation; either version 2 of
    the License, or (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
    02110-1301, USA.

*/

#include "volumewidget.h"

#include <QtGui/QDesktopWidget>
#include <QtGui/QPainter>
#include <QtGui/QPaintEvent>
#include <QtGui/QPolygonF>
#include <QtSvg/QSvgRenderer>

#include <kicon.h>

VolumeWidget::VolumeWidget(QWidget *parent)
    : QWidget(parent, Qt::Window | Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint | Qt::X11BypassWindowManagerHint),
    m_svgBackground(222, 66)
{
    {
        QSvgRenderer renderer(QLatin1String(":/svgs/background.svg"));
        m_svgBackground.fill(Qt::transparent);
        QPainter p(&m_svgBackground);
        p.setRenderHints(QPainter::Antialiasing);
        renderer.render(&p);
    }
    const QRect desktopRect = QDesktopWidget().screenGeometry();
    QRect frameRect(0, 0, 222, 66);
    frameRect.moveCenter(desktopRect.center());
    setGeometry(frameRect);
    connect(&m_timer, SIGNAL(timeout()), SLOT(hide()));
    m_timer.setSingleShot(true);
    m_timer.setInterval(1000);
    m_pixmap = KIcon("speaker").pixmap(64, 64);
}

void VolumeWidget::setPercentage(int p)
{
    m_percentage = p;
    update();
    show();
    m_timer.start();
}

void VolumeWidget::setMuted(bool m)
{
    if (m) {
        m_pixmap = KIcon("player-volume-muted").pixmap(64, 64);
    } else {
        m_pixmap = KIcon("speaker").pixmap(64, 64);
    }
    update();
    show();
    m_timer.start();
}

void VolumeWidget::paintEvent(QPaintEvent *e)
{
    QPainter p(this);
    p.setRenderHints(QPainter::Antialiasing);
    p.setClipRect(e->rect());

    p.save();
    p.setCompositionMode(QPainter::CompositionMode_Source);
    p.fillRect(e->rect(), Qt::transparent);
    p.restore();

    p.drawPixmap(0, 0, m_svgBackground);
    p.drawPixmap((222 - 64) / 2, 1, m_pixmap);

    QPolygonF polygon1;

    p.setPen(QPen(palette().brush(QPalette::Window), 2.0));

    p.setPen(Qt::NoPen);
    p.setBrush(palette().brush(QPalette::Window));
    p.setOpacity(0.2);
    polygon1 << QPointF(11.0, 54.0) << QPointF(211.0, 54.0) << QPointF(211.0, 10.0);
    p.drawPolygon(polygon1);

    QPolygonF polygon2;
    p.setPen(Qt::NoPen);
    p.setBrush(palette().brush(QPalette::WindowText));
    p.setOpacity(0.7);
    polygon2 << QPointF(11.0, 54.0) << QPointF(11.0 + m_percentage * 2, 54.0) << QPointF(11.0 + m_percentage * 2, 54.0 - m_percentage * 0.44);
    p.drawPolygon(polygon2);

    p.setPen(QPen(palette().brush(QPalette::Window), 2.0));
    p.setOpacity(0.2);
    p.drawPolygon(polygon1);
}

// vim: sw=4 sts=4 et tw=100
