• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.14.38 API Reference
  • KDE Home
  • Contact Us
 

KDEUI

  • kdeui
  • widgets
kfadewidgeteffect.cpp
Go to the documentation of this file.
1/* This file is part of the KDE project
2 Copyright (C) 2008 Matthias Kretz <kretz@kde.org>
3 Copyright (C) 2008 Rafael Fernández López <ereslibre@kde.org>
4
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) version 3.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to
17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
19
20*/
21
22#include "kfadewidgeteffect.h"
23#include "kfadewidgeteffect_p.h"
24
25#include <config.h> // for HAVE_XRENDER
26
27#include <QtCore/QEvent>
28#include <QtGui/QPaintEngine>
29#include <QtGui/QPainter>
30
31#include <kglobalsettings.h>
32
33#if defined(Q_WS_X11) && defined(HAVE_XRENDER)
34# include <X11/Xlib.h>
35# include <X11/extensions/Xrender.h>
36# include <QX11Info>
37# undef KeyPress
38# undef FocusOut
39#endif
40
41KFadeWidgetEffectPrivate::KFadeWidgetEffectPrivate(QWidget *_destWidget)
42 : destWidget(_destWidget), disabled(false)
43{
44}
45
46// Code from KFileItemDelegate (Author: Frederik Höglund)
47// Fast transitions. Read:
48// http://techbase.kde.org/Development/Tutorials/Graphics/Performance
49// for further information on why not use setOpacity.
50QPixmap KFadeWidgetEffectPrivate::transition(const QPixmap &from, const QPixmap &to, qreal amount) const
51{
52 const int value = int(0xff * amount);
53
54 if (value == 0)
55 return from;
56
57 if (value == 1)
58 return to;
59
60 QColor color;
61 color.setAlphaF(amount);
62
63 // If the native paint engine supports Porter/Duff compositing and CompositionMode_Plus
64 if (from.paintEngine()->hasFeature(QPaintEngine::PorterDuff) &&
65 from.paintEngine()->hasFeature(QPaintEngine::BlendModes))
66 {
67 QPixmap under = from;
68 QPixmap over = to;
69
70 QPainter p;
71 p.begin(&over);
72 p.setCompositionMode(QPainter::CompositionMode_DestinationIn);
73 p.fillRect(over.rect(), color);
74 p.end();
75
76 p.begin(&under);
77 p.setCompositionMode(QPainter::CompositionMode_DestinationOut);
78 p.fillRect(under.rect(), color);
79 p.setCompositionMode(QPainter::CompositionMode_Plus);
80 p.drawPixmap(0, 0, over);
81 p.end();
82
83 return under;
84 }
85#if defined(Q_WS_X11) && defined(HAVE_XRENDER)
86 else if (from.paintEngine()->hasFeature(QPaintEngine::PorterDuff)) // We have Xrender support
87 {
88 // QX11PaintEngine doesn't implement CompositionMode_Plus in Qt 4.3,
89 // which we need to be able to do a transition from one pixmap to
90 // another.
91 //
92 // In order to avoid the overhead of converting the pixmaps to images
93 // and doing the operation entirely in software, this function has a
94 // specialized path for X11 that uses Xrender directly to do the
95 // transition. This operation can be fully accelerated in HW.
96 //
97 // This specialization can be removed when QX11PaintEngine supports
98 // CompositionMode_Plus.
99 QPixmap source(to), destination(from);
100
101 source.detach();
102 destination.detach();
103
104 Display *dpy = QX11Info::display();
105
106 XRenderPictFormat *format = XRenderFindStandardFormat(dpy, PictStandardA8);
107 XRenderPictureAttributes pa;
108 pa.repeat = 1; // RepeatNormal
109
110 // Create a 1x1 8 bit repeating alpha picture
111 Pixmap pixmap = XCreatePixmap(dpy, destination.handle(), 1, 1, 8);
112 Picture alpha = XRenderCreatePicture(dpy, pixmap, format, CPRepeat, &pa);
113 XFreePixmap(dpy, pixmap);
114
115 // Fill the alpha picture with the opacity value
116 XRenderColor xcolor;
117 xcolor.alpha = quint16(0xffff * amount);
118 XRenderFillRectangle(dpy, PictOpSrc, alpha, &xcolor, 0, 0, 1, 1);
119
120 // Reduce the alpha of the destination with 1 - opacity
121 XRenderComposite(dpy, PictOpOutReverse, alpha, None, destination.x11PictureHandle(),
122 0, 0, 0, 0, 0, 0, destination.width(), destination.height());
123
124 // Add source * opacity to the destination
125 XRenderComposite(dpy, PictOpAdd, source.x11PictureHandle(), alpha,
126 destination.x11PictureHandle(),
127 0, 0, 0, 0, 0, 0, destination.width(), destination.height());
128
129 XRenderFreePicture(dpy, alpha);
130 return destination;
131 }
132#endif
133 else
134 {
135 // Fall back to using QRasterPaintEngine to do the transition.
136 QImage under = from.toImage();
137 QImage over = to.toImage();
138
139 QPainter p;
140 p.begin(&over);
141 p.setCompositionMode(QPainter::CompositionMode_DestinationIn);
142 p.fillRect(over.rect(), color);
143 p.end();
144
145 p.begin(&under);
146 p.setCompositionMode(QPainter::CompositionMode_DestinationOut);
147 p.fillRect(under.rect(), color);
148 p.setCompositionMode(QPainter::CompositionMode_Plus);
149 p.drawImage(0, 0, over);
150 p.end();
151
152 return QPixmap::fromImage(under);
153 }
154}
155
156KFadeWidgetEffect::KFadeWidgetEffect(QWidget *destWidget)
157 : QWidget(destWidget ? destWidget->parentWidget() : 0),
158 d_ptr(new KFadeWidgetEffectPrivate(destWidget))
159{
160 Q_D(KFadeWidgetEffect);
161 d->q_ptr = this;
162 Q_ASSERT(destWidget && destWidget->parentWidget());
163 if (!destWidget || !destWidget->parentWidget() || !destWidget->isVisible() ||
164 !(KGlobalSettings::graphicEffectsLevel() & KGlobalSettings::SimpleAnimationEffects)) {
165 d->disabled = true;
166 hide();
167 return;
168 }
169 setGeometry(QRect(destWidget->mapTo(parentWidget(), QPoint(0, 0)), destWidget->size()));
170 d->oldPixmap = QPixmap::grabWidget(destWidget);
171 d->timeLine.setFrameRange(0, 255);
172 d->timeLine.setCurveShape(QTimeLine::EaseOutCurve);
173 connect(&d->timeLine, SIGNAL(finished()), SLOT(finished()));
174 connect(&d->timeLine, SIGNAL(frameChanged(int)), SLOT(repaint()));
175 show();
176}
177
178KFadeWidgetEffect::~KFadeWidgetEffect()
179{
180 delete d_ptr;
181}
182
183void KFadeWidgetEffectPrivate::finished()
184{
185 Q_Q(KFadeWidgetEffect);
186 destWidget->setUpdatesEnabled(false);
187 q->hide();
188 q->deleteLater();
189 destWidget->setUpdatesEnabled(true);
190}
191
192void KFadeWidgetEffect::start(int duration)
193{
194 Q_D(KFadeWidgetEffect);
195 if (d->disabled) {
196 deleteLater();
197 return;
198 }
199 d->newPixmap = QPixmap::grabWidget(d->destWidget);
200 d->timeLine.setDuration(duration);
201 d->timeLine.start();
202}
203
204void KFadeWidgetEffect::paintEvent(QPaintEvent *)
205{
206 Q_D(KFadeWidgetEffect);
207 QPainter p(this);
208 p.drawPixmap(rect(), d->transition(d->oldPixmap, d->newPixmap, d->timeLine.currentValue()));
209 p.end();
210}
211
212#include "moc_kfadewidgeteffect.cpp"
KFadeWidgetEffect
Animates changes fading the new UI over the old look.
Definition: kfadewidgeteffect.h:49
KFadeWidgetEffect::KFadeWidgetEffect
KFadeWidgetEffect(QWidget *destWidget)
Create the animation widget.
Definition: kfadewidgeteffect.cpp:156
KFadeWidgetEffect::start
void start(int duration=250)
Starts the animation.
Definition: kfadewidgeteffect.cpp:192
KFadeWidgetEffect::paintEvent
void paintEvent(QPaintEvent *)
Definition: kfadewidgeteffect.cpp:204
KFadeWidgetEffect::d_ptr
KFadeWidgetEffectPrivate *const d_ptr
Definition: kfadewidgeteffect.h:86
KFadeWidgetEffect::~KFadeWidgetEffect
~KFadeWidgetEffect()
Destructor.
Definition: kfadewidgeteffect.cpp:178
KGlobalSettings::SimpleAnimationEffects
@ SimpleAnimationEffects
GUI with simple animations enabled.
Definition: kglobalsettings.h:467
KGlobalSettings::graphicEffectsLevel
static GraphicEffects graphicEffectsLevel()
This function determines the desired level of effects on the GUI.
Definition: kglobalsettings.cpp:782
QWidget
kfadewidgeteffect.h
kglobalsettings.h
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Feb 20 2023 00:00:00 by doxygen 1.9.6 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KDEUI

Skip menu "KDEUI"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Modules
  • Related Pages

kdelibs-4.14.38 API Reference

Skip menu "kdelibs-4.14.38 API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal