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

« back to all changes in this revision

Viewing changes to powerdevil/kcmodule/common/ErrorOverlay.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
 *   Copyright (C) 2011 by Dario Freddi <drf@kde.org>                      *
 
3
 *                                                                         *
 
4
 *   This program is free software; you can redistribute it and/or modify  *
 
5
 *   it under the terms of the GNU General Public License as published by  *
 
6
 *   the Free Software Foundation; either version 2 of the License, or     *
 
7
 *   (at your option) any later version.                                   *
 
8
 *                                                                         *
 
9
 *   This program is distributed in the hope that it will be useful,       *
 
10
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 
11
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 
12
 *   GNU General Public License for more details.                          *
 
13
 *                                                                         *
 
14
 *   You should have received a copy of the GNU General Public License     *
 
15
 *   along with this program; if not, write to the                         *
 
16
 *   Free Software Foundation, Inc.,                                       *
 
17
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA .        *
 
18
 ***************************************************************************/
 
19
 
 
20
#include "ErrorOverlay.h"
 
21
 
 
22
#include <QtCore/QEvent>
 
23
#include <QtGui/QLabel>
 
24
#include <QtGui/QVBoxLayout>
 
25
 
 
26
#include <KIcon>
 
27
#include <KLocalizedString>
 
28
 
 
29
ErrorOverlay::ErrorOverlay(QWidget *baseWidget, const QString &details, QWidget *parent) :
 
30
    QWidget(parent ? parent : baseWidget->window()),
 
31
    m_BaseWidget(baseWidget)
 
32
{
 
33
    // Build the UI
 
34
    QVBoxLayout *layout = new QVBoxLayout;
 
35
    layout->setSpacing(10);
 
36
 
 
37
    QLabel *pixmap = new QLabel();
 
38
    pixmap->setPixmap(KIcon("dialog-error").pixmap(64));
 
39
 
 
40
    QLabel *message = new QLabel(i18n("Power Management configuration module could not be loaded.\n%1", details));
 
41
 
 
42
    pixmap->setAlignment(Qt::AlignHCenter);
 
43
    message->setAlignment(Qt::AlignHCenter);
 
44
 
 
45
    layout->addStretch();
 
46
    layout->addWidget(pixmap);
 
47
    layout->addWidget(message);
 
48
    layout->addStretch();
 
49
 
 
50
    setLayout(layout);
 
51
 
 
52
    // Draw the transparent overlay background
 
53
    QPalette p = palette();
 
54
    p.setColor(backgroundRole(), QColor(0, 0, 0, 128));
 
55
    p.setColor(foregroundRole(), Qt::white);
 
56
    setPalette(p);
 
57
    setAutoFillBackground(true);
 
58
 
 
59
    m_BaseWidget->installEventFilter(this);
 
60
 
 
61
    reposition();
 
62
}
 
63
 
 
64
ErrorOverlay::~ErrorOverlay()
 
65
{
 
66
}
 
67
 
 
68
void ErrorOverlay::reposition()
 
69
{
 
70
    if (!m_BaseWidget) {
 
71
        return;
 
72
    }
 
73
 
 
74
    // reparent to the current top level widget of the base widget if needed
 
75
    // needed eg. in dock widgets
 
76
    if (parentWidget() != m_BaseWidget->window()) {
 
77
        setParent(m_BaseWidget->window());
 
78
    }
 
79
 
 
80
    // follow base widget visibility
 
81
    // needed eg. in tab widgets
 
82
    if (!m_BaseWidget->isVisible()) {
 
83
        hide();
 
84
        return;
 
85
    }
 
86
 
 
87
    show();
 
88
 
 
89
    // follow position changes
 
90
    const QPoint topLevelPos = m_BaseWidget->mapTo(window(), QPoint(0, 0));
 
91
    const QPoint parentPos = parentWidget()->mapFrom(window(), topLevelPos);
 
92
    move(parentPos);
 
93
 
 
94
    // follow size changes
 
95
    // TODO: hide/scale icon if we don't have enough space
 
96
    resize(m_BaseWidget->size());
 
97
}
 
98
 
 
99
bool ErrorOverlay::eventFilter(QObject * object, QEvent * event)
 
100
{
 
101
    if (object == m_BaseWidget &&
 
102
        (event->type() == QEvent::Move || event->type() == QEvent::Resize ||
 
103
        event->type() == QEvent::Show || event->type() == QEvent::Hide ||
 
104
        event->type() == QEvent::ParentChange)) {
 
105
        reposition();
 
106
    }
 
107
    return QWidget::eventFilter(object, event);
 
108
}
 
109
 
 
110
#include "ErrorOverlay.moc"