~ubuntu-branches/ubuntu/precise/kde-workspace/precise-security

« back to all changes in this revision

Viewing changes to plasma/generic/applets/battery/brightnessosdwidget.cpp

Tags: upstream-4.7.2
Import upstream version 4.7.2

Show diffs side-by-side

added added

removed removed

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