~ubuntu-branches/ubuntu/natty/kdemultimedia/natty-proposed

« back to all changes in this revision

Viewing changes to kmix/osdwidget.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Debian Qt/KDE Maintainers
  • Date: 2011-05-26 02:41:36 UTC
  • mfrom: (0.2.3 upstream)
  • mto: This revision was merged to the branch mainline in revision 108.
  • Revision ID: james.westby@ubuntu.com-20110526024136-jjwsigfy402jhupm
Tags: upstream-4.6.3
Import upstream version 4.6.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*******************************************************************
2
 
* osdwidget.cpp
3
 
* Copyright  2009    Aurélien Gâteau <agateau@kde.org>
4
 
* Copyright  2009    Dario Andres Rodriguez <andresbajotierra@gmail.com>
5
 
* Copyright  2009    Christian Esken <christian.esken@arcor.de>   
6
 
*
7
 
* This program is free software; you can redistribute it and/or
8
 
* modify it under the terms of the GNU General Public License as
9
 
* published by the Free Software Foundation; either version 2 of
10
 
* the License, or (at your option) any later version.
11
 
*
12
 
* This program is distributed in the hope that it will be useful,
13
 
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 
* GNU General Public License for more details.
16
 
*
17
 
* You should have received a copy of the GNU General Public License
18
 
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 
*
20
 
******************************************************************/
21
 
 
22
 
#include "osdwidget.h"
23
 
 
24
 
// Qt
25
 
#include <QGraphicsLinearLayout>
26
 
#include <QPainter>
27
 
#include <QTimer>
28
 
#include <QLabel>
29
 
 
30
 
// KDE
31
 
#include <KIcon>
32
 
#include <KDialog>
33
 
#include <Plasma/FrameSvg>
34
 
#include <Plasma/Label>
35
 
#include <Plasma/Meter>
36
 
 
37
 
OSDWidget::OSDWidget(QWidget * parent)
38
 
    : QGraphicsView(parent),
39
 
    m_background(new Plasma::FrameSvg(this)),
40
 
    m_scene(new QGraphicsScene(this)),
41
 
    m_container(new QGraphicsWidget),
42
 
    m_iconLabel(new Plasma::Label),
43
 
    m_volumeLabel(new Plasma::Label),
44
 
    m_meter(new Plasma::Meter),
45
 
    m_hideTimer(new QTimer(this))
46
 
{
47
 
    //Setup the window properties
48
 
    setWindowFlags(Qt::X11BypassWindowManagerHint);
49
 
    setFrameStyle(QFrame::NoFrame);
50
 
    viewport()->setAutoFillBackground(false);
51
 
    setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
52
 
    setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
53
 
    setAttribute(Qt::WA_TranslucentBackground);
54
 
 
55
 
    //Cache the icon pixmaps
56
 
    QSize iconSize = QSize(KIconLoader::SizeSmallMedium, KIconLoader::SizeSmallMedium);
57
 
    m_volumeHighPixmap = KIcon("audio-volume-high").pixmap(iconSize);
58
 
    m_volumeMediumPixmap = KIcon("audio-volume-medium").pixmap(iconSize);
59
 
    m_volumeLowPixmap = KIcon("audio-volume-low").pixmap(iconSize);
60
 
    m_volumeMutedPixmap = KIcon("audio-volume-muted").pixmap(iconSize);
61
 
 
62
 
    //Setup the widgets
63
 
    m_background->setImagePath("widgets/tooltip");
64
 
 
65
 
    m_iconLabel->nativeWidget()->setPixmap(m_volumeHighPixmap);
66
 
    m_iconLabel->nativeWidget()->setFixedSize(iconSize);
67
 
    m_iconLabel->setMinimumSize(iconSize);
68
 
    m_iconLabel->setMaximumSize(iconSize);
69
 
 
70
 
    m_meter->setMeterType(Plasma::Meter::BarMeterHorizontal);
71
 
    m_meter->setMaximum(100);
72
 
    m_meter->setMaximumHeight(iconSize.height());
73
 
 
74
 
    m_volumeLabel->setAlignment(Qt::AlignCenter);
75
 
 
76
 
    //Setup the auto-hide timer
77
 
    m_hideTimer->setInterval(2000);
78
 
    m_hideTimer->setSingleShot(true);
79
 
    connect(m_hideTimer, SIGNAL(timeout()), this, SLOT(hide()));
80
 
 
81
 
    //Setup the OSD layout
82
 
    QGraphicsLinearLayout *layout = new QGraphicsLinearLayout(m_container);
83
 
    layout->addItem(m_iconLabel);
84
 
    layout->addItem(m_meter);
85
 
 
86
 
    m_scene->addItem(m_container);
87
 
    setScene(m_scene);
88
 
}
89
 
 
90
 
void OSDWidget::activateOSD()
91
 
{
92
 
    m_hideTimer->start();
93
 
}
94
 
 
95
 
void OSDWidget::setCurrentVolume(int volumeLevel, bool muted)
96
 
{
97
 
    m_meter->setValue(volumeLevel);
98
 
 
99
 
    if (!muted) {
100
 
        if (volumeLevel < 25) {
101
 
            m_iconLabel->nativeWidget()->setPixmap(m_volumeLowPixmap);
102
 
        } else if (volumeLevel < 75) {
103
 
            m_iconLabel->nativeWidget()->setPixmap(m_volumeMediumPixmap);
104
 
        } else {
105
 
            m_iconLabel->nativeWidget()->setPixmap(m_volumeHighPixmap);
106
 
        }
107
 
    } else {
108
 
        m_iconLabel->nativeWidget()->setPixmap(m_volumeMutedPixmap);
109
 
    }
110
 
 
111
 
    //Show the volume %
112
 
    //m_meter->setLabel(0, QString::number(volumeLevel) + " %");
113
 
}
114
 
 
115
 
void OSDWidget::drawBackground(QPainter *painter, const QRectF &/*rectF*/)
116
 
{
117
 
    painter->save();
118
 
    painter->setCompositionMode(QPainter::CompositionMode_Source);
119
 
    m_background->paintFrame(painter);
120
 
    painter->restore();
121
 
}
122
 
 
123
 
QSize OSDWidget::sizeHint() const
124
 
{
125
 
    int iconSize = m_iconLabel->nativeWidget()->pixmap()->height();
126
 
    int meterHeight = iconSize;
127
 
    int meterWidth = iconSize * 12;
128
 
    qreal left, top, right, bottom;
129
 
    m_background->getMargins(left, top, right, bottom);
130
 
    return QSize(meterWidth + iconSize + left + right, meterHeight + top + bottom);
131
 
}
132
 
 
133
 
void OSDWidget::resizeEvent(QResizeEvent*)
134
 
{
135
 
    m_background->resizeFrame(size());
136
 
    m_container->setGeometry(0, 0, width(), height());
137
 
    qreal left, top, right, bottom;
138
 
    m_background->getMargins(left, top, right, bottom);
139
 
    m_container->layout()->setContentsMargins(left, top, right, bottom);
140
 
 
141
 
    m_scene->setSceneRect(0, 0, width(), height());
142
 
    setMask(m_background->mask());
143
 
}
144
 
 
145
 
#include "osdwidget.moc"