File indexing completed on 2024-04-28 04:21:27

0001 // SPDX-FileCopyrightText: 2003-2023 The KPhotoAlbum Development Team
0002 // SPDX-FileCopyrightText: 2022 Johannes Zarl-Zierl <johannes@zarl-zierl.at>
0003 //
0004 // SPDX-License-Identifier: GPL-2.0-or-later
0005 
0006 #include "TransientDisplay.h"
0007 
0008 #include <KLocalizedString>
0009 #include <QLabel>
0010 #include <QLayout>
0011 #include <QTimeLine>
0012 #include <QTimer>
0013 
0014 Viewer::TransientDisplay::TransientDisplay(QWidget *parent)
0015     : QLabel(parent)
0016 {
0017     m_timeLine = new QTimeLine(1000, this);
0018     connect(m_timeLine, &QTimeLine::frameChanged, this, qOverload<int>(&TransientDisplay::setAlphaChannel));
0019     m_timeLine->setFrameRange(0, 170);
0020     m_timeLine->setDirection(QTimeLine::Backward);
0021 
0022     m_timer = new QTimer(this);
0023     m_timer->setSingleShot(true);
0024     connect(m_timer, &QTimer::timeout, [this] {
0025         if (m_nextFadeAction == FadeOut)
0026             m_timeLine->start();
0027         else
0028             hide();
0029     });
0030 
0031     setAutoFillBackground(true);
0032 }
0033 
0034 void Viewer::TransientDisplay::display(const QString &text, std::chrono::milliseconds duration, FadeAction action)
0035 {
0036     setText(QLatin1String("<p><center><font size=\"+4\">%1</font></center></p>").arg(text));
0037     m_nextFadeAction = action;
0038     go(duration);
0039 }
0040 
0041 void Viewer::TransientDisplay::go(std::chrono::milliseconds duration)
0042 {
0043     resize(sizeHint());
0044     QWidget *p = static_cast<QWidget *>(parent());
0045     move((p->width() - width()) / 2, (p->height() - height()) / 2);
0046 
0047     setAlphaChannel(170, 255);
0048     m_timer->start(duration);
0049     m_timeLine->stop();
0050 
0051     show();
0052     raise();
0053 }
0054 
0055 void Viewer::TransientDisplay::setAlphaChannel(int backgroundAlpha, int labelAlpha)
0056 {
0057     QPalette p = palette();
0058     QColor bgColor = p.window().color();
0059     bgColor.setAlpha(backgroundAlpha);
0060     p.setColor(QPalette::Window, bgColor);
0061     QColor fgColor = p.windowText().color();
0062     fgColor.setAlpha(labelAlpha);
0063     p.setColor(QPalette::WindowText, fgColor);
0064     setPalette(p);
0065     // re-enable palette propagation:
0066     setAttribute(Qt::WA_SetPalette);
0067 }
0068 
0069 void Viewer::TransientDisplay::setAlphaChannel(int alpha)
0070 {
0071     setAlphaChannel(alpha, alpha);
0072     if (alpha == 0)
0073         hide();
0074 }
0075 
0076 // vi:expandtab:tabstop=4 shiftwidth=4:
0077 
0078 #include "moc_TransientDisplay.cpp"