File indexing completed on 2024-05-12 17:10:47

0001 /***************************************************************************
0002  *   Copyright (C) 2011 by Dario Freddi <drf@kde.org>                      *
0003  *                                                                         *
0004  *   This program is free software; you can redistribute it and/or modify  *
0005  *   it under the terms of the GNU General Public License as published by  *
0006  *   the Free Software Foundation; either version 2 of the License, or     *
0007  *   (at your option) any later version.                                   *
0008  *                                                                         *
0009  *   This program is distributed in the hope that it will be useful,       *
0010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0012  *   GNU General Public License for more details.                          *
0013  *                                                                         *
0014  *   You should have received a copy of the GNU General Public License     *
0015  *   along with this program; if not, write to the                         *
0016  *   Free Software Foundation, Inc.,                                       *
0017  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA .        *
0018  ***************************************************************************/
0019 
0020 #include "ErrorOverlay.h"
0021 
0022 #include <QEvent>
0023 #include <QLabel>
0024 #include <QVBoxLayout>
0025 #include <QIcon>
0026 
0027 #include <KLocalizedString>
0028 
0029 ErrorOverlay::ErrorOverlay(QWidget *baseWidget, const QString &details, QWidget *parent) :
0030     QWidget(parent ? parent : baseWidget->window()),
0031     m_BaseWidget(baseWidget)
0032 {
0033     // Build the UI
0034     QVBoxLayout *layout = new QVBoxLayout;
0035     layout->setSpacing(10);
0036 
0037     QLabel *pixmap = new QLabel();
0038     pixmap->setPixmap(QIcon::fromTheme("dialog-error").pixmap(64));
0039 
0040     QLabel *message = new QLabel(i18n("Power Management configuration module could not be loaded.\n%1", details));
0041 
0042     pixmap->setAlignment(Qt::AlignHCenter);
0043     message->setAlignment(Qt::AlignHCenter);
0044 
0045     layout->addStretch();
0046     layout->addWidget(pixmap);
0047     layout->addWidget(message);
0048     layout->addStretch();
0049 
0050     setLayout(layout);
0051 
0052     // Draw the transparent overlay background
0053     QPalette p = palette();
0054     p.setColor(backgroundRole(), QColor(0, 0, 0, 128));
0055     p.setColor(foregroundRole(), Qt::white);
0056     setPalette(p);
0057     setAutoFillBackground(true);
0058 
0059     m_BaseWidget->installEventFilter(this);
0060 
0061     reposition();
0062 }
0063 
0064 ErrorOverlay::~ErrorOverlay()
0065 {
0066 }
0067 
0068 void ErrorOverlay::reposition()
0069 {
0070     if (!m_BaseWidget) {
0071         return;
0072     }
0073 
0074     // follow base widget visibility
0075     // needed eg. in tab widgets
0076     if (!m_BaseWidget->isVisible()) {
0077         hide();
0078         return;
0079     }
0080 
0081     show();
0082 
0083     // follow position changes
0084     const QPoint topLevelPos = m_BaseWidget->mapTo(window(), QPoint(0, 0));
0085     const QPoint parentPos = parentWidget()->mapFrom(window(), topLevelPos);
0086     move(parentPos);
0087 
0088     // follow size changes
0089     // TODO: hide/scale icon if we don't have enough space
0090     resize(m_BaseWidget->size());
0091 }
0092 
0093 bool ErrorOverlay::eventFilter(QObject * object, QEvent * event)
0094 {
0095     if (object == m_BaseWidget &&
0096         (event->type() == QEvent::Move || event->type() == QEvent::Resize ||
0097         event->type() == QEvent::Show || event->type() == QEvent::Hide ||
0098         event->type() == QEvent::ParentChange)) {
0099         reposition();
0100     }
0101     return QWidget::eventFilter(object, event);
0102 }