KWin
Loading...
Searching...
No Matches
effectframe.cpp
Go to the documentation of this file.
1/*
2 SPDX-FileCopyrightText: 2006 Lubos Lunak <l.lunak@kde.org>
3 SPDX-FileCopyrightText: 2010, 2011 Martin Gräßlin <mgraesslin@kde.org>
4
5 SPDX-License-Identifier: GPL-2.0-or-later
6*/
7
10
11#include <QQuickItem>
12#include <QStandardPaths>
13
14namespace KWin
15{
16
17EffectFrameQuickScene::EffectFrameQuickScene(EffectFrameStyle style, bool staticSize, QPoint position, Qt::Alignment alignment)
18 : m_style(style)
19 , m_static(staticSize)
20 , m_point(position)
21 , m_alignment(alignment)
22{
23
24 QString name;
25 switch (style) {
26 case EffectFrameNone:
27 name = QStringLiteral("none");
28 break;
30 name = QStringLiteral("unstyled");
31 break;
33 name = QStringLiteral("styled");
34 break;
35 }
36
37 const QString defaultPath = QStringLiteral("kwin/frames/plasma/frame_%1.qml").arg(name);
38 // TODO read from kwinApp()->config() "QmlPath" like Outline/OnScreenNotification
39 // *if* someone really needs this to be configurable.
40 const QString path = QStandardPaths::locate(QStandardPaths::GenericDataLocation, defaultPath);
41
42 setSource(QUrl::fromLocalFile(path), QVariantMap{{QStringLiteral("effectFrame"), QVariant::fromValue(this)}});
43
44 if (rootItem()) {
45 connect(rootItem(), &QQuickItem::implicitWidthChanged, this, &EffectFrameQuickScene::reposition);
46 connect(rootItem(), &QQuickItem::implicitHeightChanged, this, &EffectFrameQuickScene::reposition);
47 }
48}
49
51
53{
54 return m_style;
55}
56
58{
59 return m_static;
60}
61
63{
64 return m_font;
65}
66
67void EffectFrameQuickScene::setFont(const QFont &font)
68{
69 if (m_font == font) {
70 return;
71 }
72
73 m_font = font;
74 Q_EMIT fontChanged(font);
75 reposition();
76}
77
79{
80 return m_icon;
81}
82
83void EffectFrameQuickScene::setIcon(const QIcon &icon)
84{
85 m_icon = icon;
86 Q_EMIT iconChanged(icon);
87 reposition();
88}
89
91{
92 return m_iconSize;
93}
94
95void EffectFrameQuickScene::setIconSize(const QSize &iconSize)
96{
97 if (m_iconSize == iconSize) {
98 return;
99 }
100
101 m_iconSize = iconSize;
103 reposition();
104}
105
107{
108 return m_text;
109}
110
111void EffectFrameQuickScene::setText(const QString &text)
112{
113 if (m_text == text) {
114 return;
115 }
116
117 m_text = text;
118 Q_EMIT textChanged(text);
119 reposition();
120}
121
123{
124 return m_frameOpacity;
125}
126
128{
129 if (m_frameOpacity != frameOpacity) {
130 m_frameOpacity = frameOpacity;
132 }
133}
134
136{
137 return m_crossFadeEnabled;
138}
139
141{
142 if (m_crossFadeEnabled != enabled) {
143 m_crossFadeEnabled = enabled;
144 Q_EMIT crossFadeEnabledChanged(enabled);
145 }
146}
147
149{
150 return m_crossFadeProgress;
151}
152
154{
155 if (m_crossFadeProgress != progress) {
156 m_crossFadeProgress = progress;
157 Q_EMIT crossFadeProgressChanged(progress);
158 }
159}
160
162{
163 return m_alignment;
164}
165
166void EffectFrameQuickScene::setAlignment(Qt::Alignment alignment)
167{
168 if (m_alignment == alignment) {
169 return;
170 }
171
172 m_alignment = alignment;
173 reposition();
174}
175
177{
178 return m_point;
179}
180
181void EffectFrameQuickScene::setPosition(const QPoint &point)
182{
183 if (m_point == point) {
184 return;
185 }
186
187 m_point = point;
188 reposition();
189}
190
191void EffectFrameQuickScene::reposition()
192{
193 if (!rootItem() || m_point.x() < 0 || m_point.y() < 0) {
194 return;
195 }
196
197 QSizeF size;
198 if (m_static) {
199 size = rootItem()->size();
200 } else {
201 size = QSizeF(rootItem()->implicitWidth(), rootItem()->implicitHeight());
202 }
203
204 QRect geometry(QPoint(), size.toSize());
205
206 if (m_alignment & Qt::AlignLeft)
207 geometry.moveLeft(m_point.x());
208 else if (m_alignment & Qt::AlignRight)
209 geometry.moveLeft(m_point.x() - geometry.width());
210 else
211 geometry.moveLeft(m_point.x() - geometry.width() / 2);
212 if (m_alignment & Qt::AlignTop)
213 geometry.moveTop(m_point.y());
214 else if (m_alignment & Qt::AlignBottom)
215 geometry.moveTop(m_point.y() - geometry.height());
216 else
217 geometry.moveTop(m_point.y() - geometry.height() / 2);
218
219 if (geometry == this->geometry()) {
220 return;
221 }
222
224}
225
226EffectFrame::EffectFrame(EffectFrameStyle style, bool staticSize, QPoint position, Qt::Alignment alignment)
227 : m_view(new EffectFrameQuickScene(style, staticSize, position, alignment))
228{
229 connect(m_view, &OffscreenQuickScene::repaintNeeded, this, [this] {
231 });
232 connect(m_view, &OffscreenQuickScene::geometryChanged, this, [](const QRect &oldGeometry, const QRect &newGeometry) {
233 effects->addRepaint(oldGeometry);
234 effects->addRepaint(newGeometry);
235 });
236}
237
239{
240 // Effects often destroy their cached TextFrames in pre/postPaintScreen.
241 // Destroying an OffscreenQuickView changes GL context, which we
242 // must not do during effect rendering.
243 // Delay destruction of the view until after the rendering.
244 m_view->deleteLater();
245}
246
247Qt::Alignment EffectFrame::alignment() const
248{
249 return m_view->alignment();
250}
251
252void EffectFrame::setAlignment(Qt::Alignment alignment)
253{
254 m_view->setAlignment(alignment);
255}
256
257QFont EffectFrame::font() const
258{
259 return m_view->font();
260}
261
262void EffectFrame::setFont(const QFont &font)
263{
264 m_view->setFont(font);
265}
266
268{
269 m_view->hide();
270}
271
273{
274 return m_view->geometry();
275}
276
277void EffectFrame::setGeometry(const QRect &geometry, bool force)
278{
279 m_view->setGeometry(geometry);
280}
281
282QIcon EffectFrame::icon() const
283{
284 return m_view->icon();
285}
286
287void EffectFrame::setIcon(const QIcon &icon)
288{
289 m_view->setIcon(icon);
290
291 if (m_view->iconSize().isEmpty() && !icon.availableSizes().isEmpty()) { // Set a size if we don't already have one
292 setIconSize(icon.availableSizes().constFirst());
293 }
294}
295
297{
298 return m_view->iconSize();
299}
300
301void EffectFrame::setIconSize(const QSize &size)
302{
303 m_view->setIconSize(size);
304}
305
306void EffectFrame::setPosition(const QPoint &point)
307{
308 m_view->setPosition(point);
309}
310
311void EffectFrame::render(const RenderTarget &renderTarget, const RenderViewport &viewport, const QRegion &region, double opacity, double frameOpacity)
312{
313 if (!m_view->rootItem()) {
314 return;
315 }
316
317 m_view->show();
318
319 m_view->setOpacity(opacity);
320 m_view->setFrameOpacity(frameOpacity);
321
322 effects->renderOffscreenQuickView(renderTarget, viewport, m_view);
323}
324
325QString EffectFrame::text() const
326{
327 return m_view->text();
328}
329
330void EffectFrame::setText(const QString &text)
331{
332 m_view->setText(text);
333}
334
336{
337 return m_view->style();
338}
339
341{
342 return m_view->crossFadeEnabled();
343}
344
346{
347 m_view->setCrossFadeEnabled(enable);
348}
349
351{
352 return m_view->crossFadeProgress();
353}
354
356{
357 m_view->setCrossFadeProgress(progress);
358}
359
360} // namespace KWin
361
362#include "moc_effectframe.cpp"
QIcon icon() const
void setIconSize(const QSize &size)
EffectFrame(EffectFrameStyle style, bool staticSize=true, QPoint position=QPoint(-1, -1), Qt::Alignment alignment=Qt::AlignCenter)
void render(const RenderTarget &renderTarget, const RenderViewport &viewport, const QRegion &region=infiniteRegion(), double opacity=1.0, double frameOpacity=1.0)
void setAlignment(Qt::Alignment alignment)
void setCrossFadeProgress(qreal progress)
QRect geometry() const
Qt::Alignment alignment() const
QSize iconSize() const
EffectFrameStyle style() const
QString text() const
void setGeometry(const QRect &geometry, bool force=false)
void enableCrossFade(bool enable)
void setFont(const QFont &font)
void setText(const QString &text)
bool isCrossFade() const
void setPosition(const QPoint &point)
QFont font() const
void setIcon(const QIcon &icon)
qreal crossFadeProgress() const
EffectFrameStyle style() const
Qt::Alignment alignment() const
EffectFrameQuickScene(EffectFrameStyle style, bool staticSize, QPoint position, Qt::Alignment alignment)
void setFrameOpacity(qreal frameOpacity)
Q_SIGNAL void textChanged(const QString &text)
Q_SIGNAL void fontChanged(const QFont &font)
void setAlignment(Qt::Alignment alignment)
Q_SIGNAL void iconSizeChanged(const QSize &iconSize)
void setText(const QString &text)
void setFont(const QFont &font)
void setIconSize(const QSize &iconSize)
void setPosition(const QPoint &point)
void setCrossFadeEnabled(bool enabled)
Q_SIGNAL void iconChanged(const QIcon &icon)
Q_SIGNAL void frameOpacityChanged(qreal frameOpacity)
void setIcon(const QIcon &icon)
Q_SIGNAL void crossFadeProgressChanged(qreal progress)
Q_SIGNAL void crossFadeEnabledChanged(bool enabled)
void setCrossFadeProgress(qreal progress)
Q_SCRIPTABLE void addRepaint(const QRectF &r)
void renderOffscreenQuickView(const RenderTarget &renderTarget, const RenderViewport &viewport, OffscreenQuickView *effectQuickView) const
void setSource(const QUrl &source)
void setGeometry(const QRect &rect)
void geometryChanged(const QRect &oldGeometry, const QRect &newGeometry)
EffectFrameStyle
Definition effectframe.h:26
@ EffectFrameStyled
Displays a Plasma-styled frame around the contents.
Definition effectframe.h:29
@ EffectFrameUnstyled
Displays a basic box around the contents.
Definition effectframe.h:28
@ EffectFrameNone
Displays no frame around the contents.
Definition effectframe.h:27
EffectsHandler * effects