File indexing completed on 2024-05-12 04:19:41

0001 // vim: set tabstop=4 shiftwidth=4 expandtab:
0002 /*
0003 Gwenview: an image viewer
0004 Copyright 2008 Aurélien Gâteau <agateau@kde.org>
0005 
0006 This program is free software; you can redistribute it and/or
0007 modify it under the terms of the GNU General Public License
0008 as published by the Free Software Foundation; either version 2
0009 of the License, or (at your option) any later version.
0010 
0011 This program is distributed in the hope that it will be useful,
0012 but WITHOUT ANY WARRANTY; without even the implied warranty of
0013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0014 GNU General Public License for more details.
0015 
0016 You should have received a copy of the GNU General Public License
0017 along with this program; if not, write to the Free Software
0018 Foundation, Inc., 51 Franklin Street, Fifth Floor, Cambridge, MA 02110-1301, USA.
0019 
0020 */
0021 // Self
0022 #include "hud/hudwidget.h"
0023 
0024 // Qt
0025 #include <QGraphicsLinearLayout>
0026 #include <QGraphicsProxyWidget>
0027 #include <QIcon>
0028 #include <QLayout>
0029 #include <QPainter>
0030 #include <QPropertyAnimation>
0031 
0032 // KF
0033 #include <KLocalizedString>
0034 
0035 // Local
0036 #include <hud/hudbutton.h>
0037 #include <hud/hudtheme.h>
0038 
0039 namespace Gwenview
0040 {
0041 struct HudWidgetPrivate {
0042     HudWidget *q = nullptr;
0043     QPropertyAnimation *mAnim = nullptr;
0044     QGraphicsWidget *mMainWidget = nullptr;
0045     HudButton *mCloseButton = nullptr;
0046     bool mAutoDeleteOnFadeout;
0047 
0048     void fadeTo(qreal value)
0049     {
0050         if (qFuzzyCompare(q->opacity(), value)) {
0051             return;
0052         }
0053         mAnim->stop();
0054         mAnim->setStartValue(q->opacity());
0055         mAnim->setEndValue(value);
0056         mAnim->start();
0057     }
0058 };
0059 
0060 HudWidget::HudWidget(QGraphicsWidget *parent)
0061     : QGraphicsWidget(parent)
0062     , d(new HudWidgetPrivate)
0063 {
0064     d->q = this;
0065     d->mAnim = new QPropertyAnimation(this, "opacity", this);
0066     d->mMainWidget = nullptr;
0067     d->mCloseButton = nullptr;
0068     d->mAutoDeleteOnFadeout = false;
0069 
0070     connect(d->mAnim, &QPropertyAnimation::finished, this, &HudWidget::slotFadeAnimationFinished);
0071 }
0072 
0073 HudWidget::~HudWidget()
0074 {
0075     delete d;
0076 }
0077 
0078 void HudWidget::init(QWidget *mainWidget, Options options)
0079 {
0080     auto proxy = new QGraphicsProxyWidget(this);
0081     proxy->setWidget(mainWidget);
0082     init(proxy, options);
0083 }
0084 
0085 void HudWidget::init(QGraphicsWidget *mainWidget, Options options)
0086 {
0087     if (options & OptionOpaque) {
0088         setProperty("opaque", QVariant(true));
0089     }
0090 
0091     auto layout = new QGraphicsLinearLayout(this);
0092     layout->setContentsMargins(4, 4, 4, 4);
0093     d->mMainWidget = mainWidget;
0094     if (d->mMainWidget) {
0095         if (d->mMainWidget->layout()) {
0096             d->mMainWidget->layout()->setContentsMargins(0, 0, 0, 0);
0097         }
0098         layout->addItem(d->mMainWidget);
0099     }
0100 
0101     if (options & OptionCloseButton) {
0102         d->mCloseButton = new HudButton(this);
0103         d->mCloseButton->setIcon(QIcon::fromTheme(QStringLiteral("window-close")));
0104         d->mCloseButton->setToolTip(i18nc("@info:tooltip", "Close"));
0105 
0106         layout->addItem(d->mCloseButton);
0107         layout->setAlignment(d->mCloseButton, Qt::AlignTop | Qt::AlignHCenter);
0108 
0109         connect(d->mCloseButton, &HudButton::clicked, this, &HudWidget::slotCloseButtonClicked);
0110     }
0111 }
0112 
0113 void HudWidget::slotCloseButtonClicked()
0114 {
0115     close();
0116     Q_EMIT closed();
0117 }
0118 
0119 void HudWidget::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
0120 {
0121     HudTheme::RenderInfo renderInfo = HudTheme::renderInfo(HudTheme::FrameWidget);
0122     painter->setPen(renderInfo.borderPen);
0123     painter->setRenderHint(QPainter::Antialiasing);
0124     painter->setBrush(renderInfo.bgBrush);
0125     painter->drawRoundedRect(boundingRect().adjusted(.5, .5, -.5, -.5), renderInfo.borderRadius, renderInfo.borderRadius);
0126 }
0127 
0128 void HudWidget::fadeIn()
0129 {
0130     d->fadeTo(1.);
0131 }
0132 
0133 void HudWidget::fadeOut()
0134 {
0135     d->fadeTo(0.);
0136 }
0137 
0138 void HudWidget::slotFadeAnimationFinished()
0139 {
0140     if (qFuzzyCompare(opacity(), 1)) {
0141         Q_EMIT fadedIn();
0142     } else {
0143         Q_EMIT fadedOut();
0144         if (d->mAutoDeleteOnFadeout) {
0145             deleteLater();
0146         }
0147     }
0148 }
0149 
0150 void HudWidget::setAutoDeleteOnFadeout(bool value)
0151 {
0152     d->mAutoDeleteOnFadeout = value;
0153 }
0154 
0155 } // namespace
0156 
0157 #include "moc_hudwidget.cpp"