1 // vim: set tabstop=4 shiftwidth=4 expandtab:
2 /*
3 Gwenview: an image viewer
4 Copyright 2008 Aurélien Gâteau <agateau@kde.org>
5
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License
8 as published by the Free Software Foundation; either version 2
9 of the License, or (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Cambridge, MA 02110-1301, USA.
19
20 */
21 // Self
22 #include "hud/hudwidget.h"
23
24 // Qt
25 #include <QGraphicsLinearLayout>
26 #include <QGraphicsProxyWidget>
27 #include <QIcon>
28 #include <QLayout>
29 #include <QPainter>
30 #include <QPropertyAnimation>
31
32 // KF
33 #include <KLocalizedString>
34
35 // Local
36 #include <hud/hudbutton.h>
37 #include <hud/hudtheme.h>
38
39 namespace Gwenview
40 {
41 struct HudWidgetPrivate {
42 HudWidget *q;
43 QPropertyAnimation *mAnim;
44 QGraphicsWidget *mMainWidget;
45 HudButton *mCloseButton;
46 bool mAutoDeleteOnFadeout;
47
fadeToGwenview::HudWidgetPrivate48 void fadeTo(qreal value)
49 {
50 if (qFuzzyCompare(q->opacity(), value)) {
51 return;
52 }
53 mAnim->stop();
54 mAnim->setStartValue(q->opacity());
55 mAnim->setEndValue(value);
56 mAnim->start();
57 }
58 };
59
HudWidget(QGraphicsWidget * parent)60 HudWidget::HudWidget(QGraphicsWidget *parent)
61 : QGraphicsWidget(parent)
62 , d(new HudWidgetPrivate)
63 {
64 d->q = this;
65 d->mAnim = new QPropertyAnimation(this, "opacity", this);
66 d->mMainWidget = nullptr;
67 d->mCloseButton = nullptr;
68 d->mAutoDeleteOnFadeout = false;
69
70 connect(d->mAnim, &QPropertyAnimation::finished, this, &HudWidget::slotFadeAnimationFinished);
71 }
72
~HudWidget()73 HudWidget::~HudWidget()
74 {
75 delete d;
76 }
77
init(QWidget * mainWidget,Options options)78 void HudWidget::init(QWidget *mainWidget, Options options)
79 {
80 auto *proxy = new QGraphicsProxyWidget(this);
81 proxy->setWidget(mainWidget);
82 init(proxy, options);
83 }
84
init(QGraphicsWidget * mainWidget,Options options)85 void HudWidget::init(QGraphicsWidget *mainWidget, Options options)
86 {
87 if (options & OptionOpaque) {
88 setProperty("opaque", QVariant(true));
89 }
90
91 auto *layout = new QGraphicsLinearLayout(this);
92 layout->setContentsMargins(4, 4, 4, 4);
93 d->mMainWidget = mainWidget;
94 if (d->mMainWidget) {
95 if (d->mMainWidget->layout()) {
96 d->mMainWidget->layout()->setContentsMargins(0, 0, 0, 0);
97 }
98 layout->addItem(d->mMainWidget);
99 }
100
101 if (options & OptionCloseButton) {
102 d->mCloseButton = new HudButton(this);
103 d->mCloseButton->setIcon(QIcon::fromTheme(QStringLiteral("window-close")));
104 d->mCloseButton->setToolTip(i18nc("@info:tooltip", "Close"));
105
106 layout->addItem(d->mCloseButton);
107 layout->setAlignment(d->mCloseButton, Qt::AlignTop | Qt::AlignHCenter);
108
109 connect(d->mCloseButton, &HudButton::clicked, this, &HudWidget::slotCloseButtonClicked);
110 }
111 }
112
slotCloseButtonClicked()113 void HudWidget::slotCloseButtonClicked()
114 {
115 close();
116 Q_EMIT closed();
117 }
118
paint(QPainter * painter,const QStyleOptionGraphicsItem *,QWidget *)119 void HudWidget::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
120 {
121 HudTheme::RenderInfo renderInfo = HudTheme::renderInfo(HudTheme::FrameWidget);
122 painter->setPen(renderInfo.borderPen);
123 painter->setRenderHint(QPainter::Antialiasing);
124 painter->setBrush(renderInfo.bgBrush);
125 painter->drawRoundedRect(boundingRect().adjusted(.5, .5, -.5, -.5), renderInfo.borderRadius, renderInfo.borderRadius);
126 }
127
fadeIn()128 void HudWidget::fadeIn()
129 {
130 d->fadeTo(1.);
131 }
132
fadeOut()133 void HudWidget::fadeOut()
134 {
135 d->fadeTo(0.);
136 }
137
slotFadeAnimationFinished()138 void HudWidget::slotFadeAnimationFinished()
139 {
140 if (qFuzzyCompare(opacity(), 1)) {
141 Q_EMIT fadedIn();
142 } else {
143 Q_EMIT fadedOut();
144 if (d->mAutoDeleteOnFadeout) {
145 deleteLater();
146 }
147 }
148 }
149
setAutoDeleteOnFadeout(bool value)150 void HudWidget::setAutoDeleteOnFadeout(bool value)
151 {
152 d->mAutoDeleteOnFadeout = value;
153 }
154
155 } // namespace
156