diff --git a/src/effects/desktopgrid/desktopgrideffect.cpp b/src/effects/desktopgrid/desktopgrideffect.cpp index 12e6fe8b44..5c2a033cee 100644 --- a/src/effects/desktopgrid/desktopgrideffect.cpp +++ b/src/effects/desktopgrid/desktopgrideffect.cpp @@ -342,9 +342,11 @@ void DesktopGridEffect::cancelPartialActivate() void DesktopGridEffect::deactivate(int timeout) { - const auto screenViews = views(); - for (QuickSceneView *view : screenViews) { - QMetaObject::invokeMethod(view->rootItem(), "stop"); + const auto screens = effects->screens(); + for (const auto screen : screens) { + if (QuickSceneView *view = viewForScreen(screen)) { + QMetaObject::invokeMethod(view->rootItem(), "stop"); + } } m_shutdownTimer->start(timeout); diff --git a/src/effects/overview/overvieweffect.cpp b/src/effects/overview/overvieweffect.cpp index f2431bccfa..16917a049a 100644 --- a/src/effects/overview/overvieweffect.cpp +++ b/src/effects/overview/overvieweffect.cpp @@ -261,9 +261,11 @@ void OverviewEffect::cancelPartialActivate() void OverviewEffect::deactivate() { - const auto screenViews = views(); - for (QuickSceneView *view : screenViews) { - QMetaObject::invokeMethod(view->rootItem(), "stop"); + const auto screens = effects->screens(); + for (const auto screen : screens) { + if (QuickSceneView *view = viewForScreen(screen)) { + QMetaObject::invokeMethod(view->rootItem(), "stop"); + } } m_shutdownTimer->start(animationDuration()); diff --git a/src/effects/windowview/windowvieweffect.cpp b/src/effects/windowview/windowvieweffect.cpp index e463d35146..a3700c6bd0 100644 --- a/src/effects/windowview/windowvieweffect.cpp +++ b/src/effects/windowview/windowvieweffect.cpp @@ -379,9 +379,11 @@ void WindowViewEffect::cancelPartialActivate() void WindowViewEffect::deactivate(int timeout) { - const auto screenViews = views(); - for (QuickSceneView *view : screenViews) { - QMetaObject::invokeMethod(view->rootItem(), "stop"); + const auto screens = effects->screens(); + for (const auto screen : screens) { + if (QuickSceneView *view = viewForScreen(screen)) { + QMetaObject::invokeMethod(view->rootItem(), "stop"); + } } m_shutdownTimer->start(timeout); diff --git a/src/libkwineffects/kwinanimationeffect.cpp b/src/libkwineffects/kwinanimationeffect.cpp index 67b741efef..dcad5fc16f 100644 --- a/src/libkwineffects/kwinanimationeffect.cpp +++ b/src/libkwineffects/kwinanimationeffect.cpp @@ -47,7 +47,7 @@ quint64 AnimationEffectPrivate::m_animCounter = 0; AnimationEffect::AnimationEffect() : CrossFadeEffect() - , d_ptr(new AnimationEffectPrivate()) + , d_ptr(std::make_unique()) { if (!s_clock.isValid()) { s_clock.start(); @@ -57,10 +57,7 @@ AnimationEffect::AnimationEffect() QMetaObject::invokeMethod(this, &AnimationEffect::init, Qt::QueuedConnection); } -AnimationEffect::~AnimationEffect() -{ - delete d_ptr; -} +AnimationEffect::~AnimationEffect() = default; void AnimationEffect::init() { diff --git a/src/libkwineffects/kwinanimationeffect.h b/src/libkwineffects/kwinanimationeffect.h index f666e8c19a..953cae33f5 100644 --- a/src/libkwineffects/kwinanimationeffect.h +++ b/src/libkwineffects/kwinanimationeffect.h @@ -524,7 +524,7 @@ private Q_SLOTS: private: static QElapsedTimer s_clock; - AnimationEffectPrivate *const d_ptr; + const std::unique_ptr d_ptr; Q_DECLARE_PRIVATE(AnimationEffect) Q_DISABLE_COPY(AnimationEffect) }; diff --git a/src/libkwineffects/kwineffects.cpp b/src/libkwineffects/kwineffects.cpp index d0b40e3d6f..d45337f3fe 100644 --- a/src/libkwineffects/kwineffects.cpp +++ b/src/libkwineffects/kwineffects.cpp @@ -60,14 +60,11 @@ public: }; PaintData::PaintData() - : d(new PaintDataPrivate()) + : d(std::make_unique()) { } -PaintData::~PaintData() -{ - delete d; -} +PaintData::~PaintData() = default; qreal PaintData::xScale() const { @@ -244,7 +241,7 @@ WindowPaintData::WindowPaintData() WindowPaintData::WindowPaintData(const QMatrix4x4 &screenProjectionMatrix) : PaintData() , shader(nullptr) - , d(new WindowPaintDataPrivate()) + , d(std::make_unique()) { d->screenProjectionMatrix = screenProjectionMatrix; setOpacity(1.0); @@ -257,7 +254,7 @@ WindowPaintData::WindowPaintData(const QMatrix4x4 &screenProjectionMatrix) WindowPaintData::WindowPaintData(const WindowPaintData &other) : PaintData() , shader(other.shader) - , d(new WindowPaintDataPrivate()) + , d(std::make_unique()) { setXScale(other.xScale()); setYScale(other.yScale()); @@ -275,10 +272,7 @@ WindowPaintData::WindowPaintData(const WindowPaintData &other) d->screenProjectionMatrix = other.d->screenProjectionMatrix; } -WindowPaintData::~WindowPaintData() -{ - delete d; -} +WindowPaintData::~WindowPaintData() = default; qreal WindowPaintData::opacity() const { @@ -1489,14 +1483,11 @@ EffectFramePrivate::~EffectFramePrivate() EffectFrame ***************************************************************/ EffectFrame::EffectFrame() - : d(new EffectFramePrivate) + : d(std::make_unique()) { } -EffectFrame::~EffectFrame() -{ - delete d; -} +EffectFrame::~EffectFrame() = default; qreal EffectFrame::crossFadeProgress() const { diff --git a/src/libkwineffects/kwineffects.h b/src/libkwineffects/kwineffects.h index 41db02dced..dea6455a49 100644 --- a/src/libkwineffects/kwineffects.h +++ b/src/libkwineffects/kwineffects.h @@ -3206,7 +3206,7 @@ protected: PaintData(const PaintData &other); private: - PaintDataPrivate *const d; + const std::unique_ptr d; }; class KWINEFFECTS_EXPORT WindowPaintData : public PaintData @@ -3393,7 +3393,7 @@ public: GLShader *shader; private: - WindowPaintDataPrivate *const d; + const std::unique_ptr d; }; class KWINEFFECTS_EXPORT ScreenPaintData @@ -3816,7 +3816,7 @@ public: virtual qreal crossFadeProgress() const = 0; private: - EffectFramePrivate *const d; + const std::unique_ptr d; }; /** diff --git a/src/libkwineffects/kwinglplatform.cpp b/src/libkwineffects/kwinglplatform.cpp index b3f1e2971f..513b2ba4e8 100644 --- a/src/libkwineffects/kwinglplatform.cpp +++ b/src/libkwineffects/kwinglplatform.cpp @@ -27,7 +27,7 @@ namespace KWin { -GLPlatform *GLPlatform::s_platform = nullptr; +std::unique_ptr GLPlatform::s_platform; static qint64 parseVersionString(const QByteArray &version) { @@ -1362,8 +1362,7 @@ bool GLPlatform::isGLES() const void GLPlatform::cleanup() { - delete s_platform; - s_platform = nullptr; + s_platform.reset(); } } // namespace KWin diff --git a/src/libkwineffects/kwinglplatform.h b/src/libkwineffects/kwinglplatform.h index 927f47d53a..d95ec2749d 100644 --- a/src/libkwineffects/kwinglplatform.h +++ b/src/libkwineffects/kwinglplatform.h @@ -15,6 +15,7 @@ #include #include +#include namespace KWin { @@ -453,16 +454,15 @@ private: bool m_preferBufferSubData : 1; OpenGLPlatformInterface m_platformInterface; bool m_gles : 1; - static GLPlatform *s_platform; + static std::unique_ptr s_platform; }; inline GLPlatform *GLPlatform::instance() { if (!s_platform) { - s_platform = new GLPlatform; + s_platform.reset(new GLPlatform()); } - - return s_platform; + return s_platform.get(); } } // namespace KWin diff --git a/src/libkwineffects/kwinglutils.cpp b/src/libkwineffects/kwinglutils.cpp index f4cbd3cd3f..34ab5ff997 100644 --- a/src/libkwineffects/kwinglutils.cpp +++ b/src/libkwineffects/kwinglutils.cpp @@ -620,20 +620,19 @@ QMatrix4x4 GLShader::getUniformMatrix4x4(const char *name) //**************************************** // ShaderManager //**************************************** -ShaderManager *ShaderManager::s_shaderManager = nullptr; +std::unique_ptr ShaderManager::s_shaderManager; ShaderManager *ShaderManager::instance() { if (!s_shaderManager) { - s_shaderManager = new ShaderManager(); + s_shaderManager.reset(new ShaderManager()); } - return s_shaderManager; + return s_shaderManager.get(); } void ShaderManager::cleanup() { - delete s_shaderManager; - s_shaderManager = nullptr; + s_shaderManager.reset(); } ShaderManager::ShaderManager() @@ -1628,7 +1627,7 @@ public: GLenum usage; int stride; int vertexCount; - static GLVertexBuffer *streamingBuffer; + static std::unique_ptr streamingBuffer; static bool haveBufferStorage; static bool haveSyncFences; static bool hasMapBufferRange; @@ -1648,15 +1647,15 @@ public: FrameSizesArray<4> frameSizes; VertexAttrib attrib[VertexAttributeCount]; Bitfield enabledArrays; - static IndexBuffer *s_indexBuffer; + static std::unique_ptr s_indexBuffer; }; bool GLVertexBufferPrivate::hasMapBufferRange = false; bool GLVertexBufferPrivate::supportsIndexedQuads = false; -GLVertexBuffer *GLVertexBufferPrivate::streamingBuffer = nullptr; +std::unique_ptr GLVertexBufferPrivate::streamingBuffer; bool GLVertexBufferPrivate::haveBufferStorage = false; bool GLVertexBufferPrivate::haveSyncFences = false; -IndexBuffer *GLVertexBufferPrivate::s_indexBuffer = nullptr; +std::unique_ptr GLVertexBufferPrivate::s_indexBuffer; void GLVertexBufferPrivate::interleaveArrays(float *dst, int dim, const float *vertices, const float *texcoords, @@ -1854,14 +1853,11 @@ GLvoid *GLVertexBufferPrivate::mapNextFreeRange(size_t size) //********************************* GLVertexBuffer::GLVertexBuffer(UsageHint hint) - : d(new GLVertexBufferPrivate(hint)) + : d(std::make_unique(hint)) { } -GLVertexBuffer::~GLVertexBuffer() -{ - delete d; -} +GLVertexBuffer::~GLVertexBuffer() = default; void GLVertexBuffer::setData(const void *data, size_t size) { @@ -2012,14 +2008,12 @@ void GLVertexBuffer::draw(GLenum primitiveMode, int first, int count) void GLVertexBuffer::draw(const QRegion ®ion, GLenum primitiveMode, int first, int count, bool hardwareClipping) { if (primitiveMode == GL_QUADS) { - IndexBuffer *&indexBuffer = GLVertexBufferPrivate::s_indexBuffer; - - if (!indexBuffer) { - indexBuffer = new IndexBuffer; + if (!GLVertexBufferPrivate::s_indexBuffer) { + GLVertexBufferPrivate::s_indexBuffer = std::make_unique(); } - indexBuffer->bind(); - indexBuffer->accommodate(count / 4); + GLVertexBufferPrivate::s_indexBuffer->bind(); + GLVertexBufferPrivate::s_indexBuffer->accommodate(count / 4); count = count * 6 / 4; @@ -2145,8 +2139,8 @@ void GLVertexBuffer::initStatic() GLVertexBufferPrivate::haveBufferStorage = hasGLVersion(4, 4) || hasGLExtension("GL_ARB_buffer_storage"); GLVertexBufferPrivate::haveSyncFences = hasGLVersion(3, 2) || hasGLExtension("GL_ARB_sync"); } - GLVertexBufferPrivate::s_indexBuffer = nullptr; - GLVertexBufferPrivate::streamingBuffer = new GLVertexBuffer(GLVertexBuffer::Stream); + GLVertexBufferPrivate::s_indexBuffer.reset(); + GLVertexBufferPrivate::streamingBuffer = std::make_unique(GLVertexBuffer::Stream); if (GLVertexBufferPrivate::haveBufferStorage && GLVertexBufferPrivate::haveSyncFences) { if (qgetenv("KWIN_PERSISTENT_VBO") != QByteArrayLiteral("0")) { @@ -2157,17 +2151,15 @@ void GLVertexBuffer::initStatic() void GLVertexBuffer::cleanup() { - delete GLVertexBufferPrivate::s_indexBuffer; - GLVertexBufferPrivate::s_indexBuffer = nullptr; + GLVertexBufferPrivate::s_indexBuffer.reset(); GLVertexBufferPrivate::hasMapBufferRange = false; GLVertexBufferPrivate::supportsIndexedQuads = false; - delete GLVertexBufferPrivate::streamingBuffer; - GLVertexBufferPrivate::streamingBuffer = nullptr; + GLVertexBufferPrivate::streamingBuffer.reset(); } GLVertexBuffer *GLVertexBuffer::streamingBuffer() { - return GLVertexBufferPrivate::streamingBuffer; + return GLVertexBufferPrivate::streamingBuffer.get(); } } // namespace diff --git a/src/libkwineffects/kwinglutils.h b/src/libkwineffects/kwinglutils.h index 494ce7df75..382594cbd9 100644 --- a/src/libkwineffects/kwinglutils.h +++ b/src/libkwineffects/kwinglutils.h @@ -7,9 +7,7 @@ SPDX-License-Identifier: GPL-2.0-or-later */ - -#ifndef KWIN_GLUTILS_H -#define KWIN_GLUTILS_H +#pragma once // kwin #include "kwingltexture.h" @@ -194,6 +192,8 @@ Q_DECLARE_FLAGS(ShaderTraits, ShaderTrait) class KWINGLUTILS_EXPORT ShaderManager { public: + ~ShaderManager(); + /** * Returns a shader with the given traits, creating it if necessary. */ @@ -294,7 +294,6 @@ public: private: ShaderManager(); - ~ShaderManager(); void bindFragDataLocations(GLShader *shader); void bindAttributeLocations(GLShader *shader) const; @@ -305,7 +304,7 @@ private: QStack m_boundShaders; std::map> m_shaderHash; - static ShaderManager *s_shaderManager; + static std::unique_ptr s_shaderManager; }; /** @@ -712,7 +711,7 @@ public: static GLVertexBuffer *streamingBuffer(); private: - GLVertexBufferPrivate *const d; + const std::unique_ptr d; }; } // namespace @@ -720,5 +719,3 @@ private: Q_DECLARE_OPERATORS_FOR_FLAGS(KWin::ShaderTraits) /** @} */ - -#endif diff --git a/src/libkwineffects/kwinoffscreeneffect.cpp b/src/libkwineffects/kwinoffscreeneffect.cpp index 3f699356ed..5caf3f3d32 100644 --- a/src/libkwineffects/kwinoffscreeneffect.cpp +++ b/src/libkwineffects/kwinoffscreeneffect.cpp @@ -33,21 +33,18 @@ private: class OffscreenEffectPrivate { public: - QHash windows; + std::map> windows; QMetaObject::Connection windowDamagedConnection; QMetaObject::Connection windowDeletedConnection; }; OffscreenEffect::OffscreenEffect(QObject *parent) : Effect(parent) - , d(new OffscreenEffectPrivate) + , d(std::make_unique()) { } -OffscreenEffect::~OffscreenEffect() -{ - qDeleteAll(d->windows); -} +OffscreenEffect::~OffscreenEffect() = default; bool OffscreenEffect::supported() { @@ -56,21 +53,21 @@ bool OffscreenEffect::supported() void OffscreenEffect::redirect(EffectWindow *window) { - OffscreenData *&offscreenData = d->windows[window]; + std::unique_ptr &offscreenData = d->windows[window]; if (offscreenData) { return; } - offscreenData = new OffscreenData; + offscreenData = std::make_unique(); - if (d->windows.count() == 1) { + if (d->windows.size() == 1) { setupConnections(); } } void OffscreenEffect::unredirect(EffectWindow *window) { - delete d->windows.take(window); - if (d->windows.isEmpty()) { + d->windows.erase(window); + if (d->windows.empty()) { destroyConnections(); } } @@ -193,11 +190,12 @@ void OffscreenData::paint(EffectWindow *window, const QRegion ®ion, void OffscreenEffect::drawWindow(EffectWindow *window, int mask, const QRegion ®ion, WindowPaintData &data) { - OffscreenData *offscreenData = d->windows.value(window); - if (!offscreenData) { + const auto it = d->windows.find(window); + if (it == d->windows.end()) { effects->drawWindow(window, mask, region, data); return; } + OffscreenData *offscreenData = it->second.get(); const QRectF expandedGeometry = window->expandedGeometry(); const QRectF frameGeometry = window->frameGeometry(); @@ -220,9 +218,8 @@ void OffscreenEffect::drawWindow(EffectWindow *window, int mask, const QRegion & void OffscreenEffect::handleWindowDamaged(EffectWindow *window) { - OffscreenData *offscreenData = d->windows.value(window); - if (offscreenData) { - offscreenData->setDirty(); + if (const auto it = d->windows.find(window); it != d->windows.end()) { + it->second->setDirty(); } } @@ -258,33 +255,31 @@ public: class CrossFadeEffectPrivate { public: - QHash windows; + std::map> windows; qreal progress; }; CrossFadeEffect::CrossFadeEffect(QObject *parent) : Effect(parent) - , d(new CrossFadeEffectPrivate) + , d(std::make_unique()) { } -CrossFadeEffect::~CrossFadeEffect() -{ - qDeleteAll(d->windows); -} +CrossFadeEffect::~CrossFadeEffect() = default; void CrossFadeEffect::drawWindow(EffectWindow *window, int mask, const QRegion ®ion, WindowPaintData &data) { - CrossFadeWindowData *offscreenData = d->windows.value(window); + const auto it = d->windows.find(window); // paint the new window (if applicable) underneath - if (data.crossFadeProgress() > 0 || !offscreenData) { + if (data.crossFadeProgress() > 0 || it == d->windows.end()) { Effect::drawWindow(window, mask, region, data); } - if (!offscreenData) { + if (it == d->windows.end()) { return; } + CrossFadeWindowData *offscreenData = it->second.get(); // paint old snapshot on top WindowPaintData previousWindowData = data; @@ -324,15 +319,15 @@ void CrossFadeEffect::drawWindow(EffectWindow *window, int mask, const QRegion & void CrossFadeEffect::redirect(EffectWindow *window) { - if (d->windows.isEmpty()) { + if (d->windows.empty()) { connect(effects, &EffectsHandler::windowDeleted, this, &CrossFadeEffect::handleWindowDeleted); } - CrossFadeWindowData *&offscreenData = d->windows[window]; + std::unique_ptr &offscreenData = d->windows[window]; if (offscreenData) { return; } - offscreenData = new CrossFadeWindowData; + offscreenData = std::make_unique(); effects->makeOpenGLContextCurrent(); offscreenData->maybeRender(window); @@ -341,8 +336,8 @@ void CrossFadeEffect::redirect(EffectWindow *window) void CrossFadeEffect::unredirect(EffectWindow *window) { - delete d->windows.take(window); - if (d->windows.isEmpty()) { + d->windows.erase(window); + if (d->windows.empty()) { disconnect(effects, &EffectsHandler::windowDeleted, this, &CrossFadeEffect::handleWindowDeleted); } } @@ -354,9 +349,8 @@ void CrossFadeEffect::handleWindowDeleted(EffectWindow *window) void CrossFadeEffect::setShader(EffectWindow *window, GLShader *shader) { - CrossFadeWindowData *offscreenData = d->windows.value(window); - if (offscreenData) { - offscreenData->setShader(shader); + if (const auto it = d->windows.find(window); it != d->windows.end()) { + it->second->setShader(shader); } } diff --git a/src/libkwineffects/kwinoffscreenquickview.cpp b/src/libkwineffects/kwinoffscreenquickview.cpp index 1258f102f9..86ac4b7c97 100644 --- a/src/libkwineffects/kwinoffscreenquickview.cpp +++ b/src/libkwineffects/kwinoffscreenquickview.cpp @@ -68,13 +68,13 @@ private: class Q_DECL_HIDDEN OffscreenQuickView::Private { public: - QQuickWindow *m_view; - QQuickRenderControl *m_renderControl; + std::unique_ptr m_view; + std::unique_ptr m_renderControl; std::unique_ptr m_offscreenSurface; std::unique_ptr m_glcontext; std::unique_ptr m_fbo; - QTimer *m_repaintTimer; + std::unique_ptr m_repaintTimer; QImage m_image; std::unique_ptr m_textureExport; // if we should capture a QImage after rendering into our BO. @@ -132,9 +132,9 @@ OffscreenQuickView::OffscreenQuickView(QObject *parent, QWindow *renderWindow, E : QObject(parent) , d(new OffscreenQuickView::Private) { - d->m_renderControl = new EffectQuickRenderControl(this, renderWindow); + d->m_renderControl = std::make_unique(this, renderWindow); - d->m_view = new QQuickWindow(d->m_renderControl); + d->m_view = std::make_unique(d->m_renderControl.get()); d->m_view->setFlags(Qt::FramelessWindowHint); d->m_view->setColor(Qt::transparent); @@ -190,16 +190,16 @@ OffscreenQuickView::OffscreenQuickView(QObject *parent, QWindow *renderWindow, E contentItem()->setSize(d->m_view->size()); }; updateSize(); - connect(d->m_view, &QWindow::widthChanged, this, updateSize); - connect(d->m_view, &QWindow::heightChanged, this, updateSize); + connect(d->m_view.get(), &QWindow::widthChanged, this, updateSize); + connect(d->m_view.get(), &QWindow::heightChanged, this, updateSize); - d->m_repaintTimer = new QTimer(this); + d->m_repaintTimer = std::make_unique(); d->m_repaintTimer->setSingleShot(true); d->m_repaintTimer->setInterval(10); - connect(d->m_repaintTimer, &QTimer::timeout, this, &OffscreenQuickView::update); - connect(d->m_renderControl, &QQuickRenderControl::renderRequested, this, &OffscreenQuickView::handleRenderRequested); - connect(d->m_renderControl, &QQuickRenderControl::sceneChanged, this, &OffscreenQuickView::handleSceneChanged); + connect(d->m_repaintTimer.get(), &QTimer::timeout, this, &OffscreenQuickView::update); + connect(d->m_renderControl.get(), &QQuickRenderControl::renderRequested, this, &OffscreenQuickView::handleRenderRequested); + connect(d->m_renderControl.get(), &QQuickRenderControl::sceneChanged, this, &OffscreenQuickView::handleSceneChanged); #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) d->touchDevice = new QTouchDevice{}; @@ -218,11 +218,9 @@ OffscreenQuickView::~OffscreenQuickView() d->m_glcontext->makeCurrent(d->m_offscreenSurface.get()); } - delete d->m_renderControl; // Always delete render control first. - d->m_renderControl = nullptr; - - delete d->m_view; - d->m_view = nullptr; + // Always delete render control first. + d->m_renderControl.reset(); + d->m_view.reset(); } bool OffscreenQuickView::automaticRepaint() const @@ -331,7 +329,7 @@ void OffscreenQuickView::forwardMouseEvent(QEvent *e) QMouseEvent *me = static_cast(e); const QPoint widgetPos = d->m_view->mapFromGlobal(me->pos()); QMouseEvent cloneEvent(me->type(), widgetPos, me->pos(), me->button(), me->buttons(), me->modifiers()); - QCoreApplication::sendEvent(d->m_view, &cloneEvent); + QCoreApplication::sendEvent(d->m_view.get(), &cloneEvent); e->setAccepted(cloneEvent.isAccepted()); if (e->type() == QEvent::MouseButtonPress) { @@ -342,7 +340,7 @@ void OffscreenQuickView::forwardMouseEvent(QEvent *e) if (doubleClick) { d->lastMousePressButton = Qt::NoButton; QMouseEvent doubleClickEvent(QEvent::MouseButtonDblClick, me->localPos(), me->windowPos(), me->screenPos(), me->button(), me->buttons(), me->modifiers()); - QCoreApplication::sendEvent(d->m_view, &doubleClickEvent); + QCoreApplication::sendEvent(d->m_view.get(), &doubleClickEvent); } } @@ -355,7 +353,7 @@ void OffscreenQuickView::forwardMouseEvent(QEvent *e) const QPointF widgetPos = d->m_view->mapFromGlobal(he->pos()); const QPointF oldWidgetPos = d->m_view->mapFromGlobal(he->oldPos()); QHoverEvent cloneEvent(he->type(), widgetPos, oldWidgetPos, he->modifiers()); - QCoreApplication::sendEvent(d->m_view, &cloneEvent); + QCoreApplication::sendEvent(d->m_view.get(), &cloneEvent); e->setAccepted(cloneEvent.isAccepted()); return; } @@ -364,7 +362,7 @@ void OffscreenQuickView::forwardMouseEvent(QEvent *e) const QPointF widgetPos = d->m_view->mapFromGlobal(we->position().toPoint()); QWheelEvent cloneEvent(widgetPos, we->globalPosition(), we->pixelDelta(), we->angleDelta(), we->buttons(), we->modifiers(), we->phase(), we->inverted()); - QCoreApplication::sendEvent(d->m_view, &cloneEvent); + QCoreApplication::sendEvent(d->m_view.get(), &cloneEvent); e->setAccepted(cloneEvent.isAccepted()); return; } @@ -378,7 +376,7 @@ void OffscreenQuickView::forwardKeyEvent(QKeyEvent *keyEvent) if (!d->m_visible) { return; } - QCoreApplication::sendEvent(d->m_view, keyEvent); + QCoreApplication::sendEvent(d->m_view.get(), keyEvent); } bool OffscreenQuickView::forwardTouchDown(qint32 id, const QPointF &pos, quint32 time) @@ -391,7 +389,7 @@ bool OffscreenQuickView::forwardTouchDown(qint32 id, const QPointF &pos, quint32 QTouchEvent event(QEvent::TouchBegin, d->touchDevice, Qt::NoModifier, d->touchPoints); #endif event.setTimestamp(time); - QCoreApplication::sendEvent(d->m_view, &event); + QCoreApplication::sendEvent(d->m_view.get(), &event); return event.isAccepted(); } @@ -406,7 +404,7 @@ bool OffscreenQuickView::forwardTouchMotion(qint32 id, const QPointF &pos, quint QTouchEvent event(QEvent::TouchUpdate, d->touchDevice, Qt::NoModifier, d->touchPoints); #endif event.setTimestamp(time); - QCoreApplication::sendEvent(d->m_view, &event); + QCoreApplication::sendEvent(d->m_view.get(), &event); return event.isAccepted(); } @@ -421,7 +419,7 @@ bool OffscreenQuickView::forwardTouchUp(qint32 id, quint32 time) QTouchEvent event(QEvent::TouchEnd, d->touchDevice, Qt::NoModifier, d->touchPoints); #endif event.setTimestamp(time); - QCoreApplication::sendEvent(d->m_view, &event); + QCoreApplication::sendEvent(d->m_view.get(), &event); return event.isAccepted(); } @@ -448,7 +446,7 @@ QQuickItem *OffscreenQuickView::contentItem() const QQuickWindow *OffscreenQuickView::window() const { - return d->m_view; + return d->m_view.get(); } void OffscreenQuickView::setVisible(bool visible) diff --git a/src/libkwineffects/kwinquickeffect.cpp b/src/libkwineffects/kwinquickeffect.cpp index bf7a07ab78..3eabe7d0dc 100644 --- a/src/libkwineffects/kwinquickeffect.cpp +++ b/src/libkwineffects/kwinquickeffect.cpp @@ -28,7 +28,7 @@ public: SharedQmlEngine::Ptr qmlEngine; std::unique_ptr qmlComponent; QUrl source; - QHash views; + std::map> views; QPointer mouseImplicitGrab; bool running = false; std::unique_ptr dummyWindow; @@ -40,7 +40,7 @@ bool QuickSceneEffectPrivate::isItemOnScreen(QQuickItem *item, EffectScreen *scr return false; } - const QuickSceneView *view = views[screen]; + const auto &view = views.at(screen); return item->window() == view->window(); } @@ -129,9 +129,9 @@ void QuickSceneEffect::checkItemDraggedOutOfScreen(QQuickItem *item) const QRectF globalGeom = QRectF(item->mapToGlobal(QPointF(0, 0)), QSizeF(item->width(), item->height())); QList screens; - for (auto it = d->views.constBegin(); it != d->views.constEnd(); ++it) { - if (!d->isItemOnScreen(item, it.key()) && it.key()->geometry().intersects(globalGeom.toRect())) { - screens << it.key(); + for (const auto &[screen, view] : d->views) { + if (!d->isItemOnScreen(item, screen) && screen->geometry().intersects(globalGeom.toRect())) { + screens << screen; } } @@ -140,16 +140,12 @@ void QuickSceneEffect::checkItemDraggedOutOfScreen(QQuickItem *item) void QuickSceneEffect::checkItemDroppedOutOfScreen(const QPointF &globalPos, QQuickItem *item) { - EffectScreen *screen = nullptr; - for (auto it = d->views.constBegin(); it != d->views.constEnd(); ++it) { - if (!d->isItemOnScreen(item, it.key()) && it.key()->geometry().contains(globalPos.toPoint())) { - screen = it.key(); - break; - } - } - - if (screen) { - Q_EMIT itemDroppedOutOfScreen(globalPos, item, screen); + const auto it = std::find_if(d->views.begin(), d->views.end(), [this, globalPos, item](const auto &view) { + EffectScreen *screen = view.first; + return !d->isItemOnScreen(item, screen) && screen->geometry().contains(globalPos.toPoint()); + }); + if (it != d->views.end()) { + Q_EMIT itemDroppedOutOfScreen(globalPos, item, it->first); } } @@ -196,36 +192,26 @@ void QuickSceneEffect::setSource(const QUrl &url) } } -QHash QuickSceneEffect::views() const +QuickSceneView *QuickSceneEffect::viewForScreen(EffectScreen *screen) const { - return d->views; + const auto it = d->views.find(screen); + return it == d->views.end() ? nullptr : it->second.get(); } QuickSceneView *QuickSceneEffect::viewAt(const QPoint &pos) const { - for (QuickSceneView *screenView : std::as_const(d->views)) { - if (screenView->geometry().contains(pos)) { - return screenView; - } - } - return nullptr; + const auto it = std::find_if(d->views.begin(), d->views.end(), [pos](const auto &view) { + return view.second->geometry().contains(pos); + }); + return it == d->views.end() ? nullptr : it->second.get(); } QuickSceneView *QuickSceneEffect::activeView() const { - auto it = std::find_if(d->views.constBegin(), d->views.constEnd(), [](QuickSceneView *v) { - return v->window()->activeFocusItem(); + const auto it = std::find_if(d->views.begin(), d->views.end(), [](const auto &view) { + return view.second->window()->activeFocusItem(); }); - - QuickSceneView *screenView = nullptr; - - if (it == d->views.constEnd()) { - screenView = d->views.value(effects->activeScreen()); - } else { - screenView = (*it); - } - - return screenView; + return it == d->views.end() ? d->views[effects->activeScreen()].get() : it->second.get(); } KWin::QuickSceneView *QuickSceneEffect::getView(Qt::Edge edge) @@ -234,34 +220,34 @@ KWin::QuickSceneView *QuickSceneEffect::getView(Qt::Edge edge) QuickSceneView *candidate = nullptr; - for (auto *v : d->views) { + for (const auto &[screen, view] : d->views) { switch (edge) { case Qt::LeftEdge: - if (v->geometry().left() < screenView->geometry().left()) { + if (view->geometry().left() < screenView->geometry().left()) { // Look for the nearest view from the current - if (!candidate || v->geometry().left() > candidate->geometry().left() || (v->geometry().left() == candidate->geometry().left() && v->geometry().top() > candidate->geometry().top())) { - candidate = v; + if (!candidate || view->geometry().left() > candidate->geometry().left() || (view->geometry().left() == candidate->geometry().left() && view->geometry().top() > candidate->geometry().top())) { + candidate = view.get(); } } break; case Qt::TopEdge: - if (v->geometry().top() < screenView->geometry().top()) { - if (!candidate || v->geometry().top() > candidate->geometry().top() || (v->geometry().top() == candidate->geometry().top() && v->geometry().left() > candidate->geometry().left())) { - candidate = v; + if (view->geometry().top() < screenView->geometry().top()) { + if (!candidate || view->geometry().top() > candidate->geometry().top() || (view->geometry().top() == candidate->geometry().top() && view->geometry().left() > candidate->geometry().left())) { + candidate = view.get(); } } break; case Qt::RightEdge: - if (v->geometry().right() > screenView->geometry().right()) { - if (!candidate || v->geometry().right() < candidate->geometry().right() || (v->geometry().right() == candidate->geometry().right() && v->geometry().top() > candidate->geometry().top())) { - candidate = v; + if (view->geometry().right() > screenView->geometry().right()) { + if (!candidate || view->geometry().right() < candidate->geometry().right() || (view->geometry().right() == candidate->geometry().right() && view->geometry().top() > candidate->geometry().top())) { + candidate = view.get(); } } break; case Qt::BottomEdge: - if (v->geometry().bottom() > screenView->geometry().bottom()) { - if (!candidate || v->geometry().bottom() < candidate->geometry().bottom() || (v->geometry().bottom() == candidate->geometry().bottom() && v->geometry().left() > candidate->geometry().left())) { - candidate = v; + if (view->geometry().bottom() > screenView->geometry().bottom()) { + if (!candidate || view->geometry().bottom() < candidate->geometry().bottom() || (view->geometry().bottom() == candidate->geometry().bottom() && view->geometry().left() > candidate->geometry().left())) { + candidate = view.get(); } } break; @@ -283,11 +269,11 @@ void QuickSceneEffect::activateView(QuickSceneView *view) return; } - for (auto *otherView : d->views) { - if (otherView == view && !view->window()->activeFocusItem()) { + for (const auto &[screen, otherView] : d->views) { + if (otherView.get() == view && !view->window()->activeFocusItem()) { QFocusEvent focusEvent(QEvent::FocusIn, Qt::ActiveWindowFocusReason); qApp->sendEvent(view->window(), &focusEvent); - } else if (otherView != view && otherView->window()->activeFocusItem()) { + } else if (otherView.get() != view && otherView->window()->activeFocusItem()) { QFocusEvent focusEvent(QEvent::FocusOut, Qt::ActiveWindowFocusReason); qApp->sendEvent(otherView->window(), &focusEvent); } @@ -300,13 +286,13 @@ void QuickSceneEffect::activateView(QuickSceneView *view) void QuickSceneEffect::prePaintScreen(ScreenPrePaintData &data, std::chrono::milliseconds presentTime) { if (effects->waylandDisplay()) { - QuickSceneView *screenView = d->views.value(data.screen); - if (screenView && screenView->isDirty()) { - screenView->update(); - screenView->resetDirty(); + const auto it = d->views.find(data.screen); + if (it != d->views.end() && it->second->isDirty()) { + it->second->update(); + it->second->resetDirty(); } } else { - for (QuickSceneView *screenView : std::as_const(d->views)) { + for (const auto &[screen, screenView] : d->views) { if (screenView->isDirty()) { screenView->update(); screenView->resetDirty(); @@ -318,20 +304,20 @@ void QuickSceneEffect::prePaintScreen(ScreenPrePaintData &data, std::chrono::mil void QuickSceneEffect::paintScreen(int mask, const QRegion ®ion, ScreenPaintData &data) { if (effects->waylandDisplay()) { - QuickSceneView *screenView = d->views.value(data.screen()); - if (screenView) { - effects->renderOffscreenQuickView(screenView); + const auto it = d->views.find(data.screen()); + if (it != d->views.end()) { + effects->renderOffscreenQuickView(it->second.get()); } } else { - for (QuickSceneView *screenView : std::as_const(d->views)) { - effects->renderOffscreenQuickView(screenView); + for (const auto &[screen, screenView] : d->views) { + effects->renderOffscreenQuickView(screenView.get()); } } } bool QuickSceneEffect::isActive() const { - return !d->views.isEmpty() && !effects->isScreenLocked(); + return !d->views.empty() && !effects->isScreenLocked(); } QVariantMap QuickSceneEffect::initialProperties(EffectScreen *screen) @@ -346,7 +332,7 @@ void QuickSceneEffect::handleScreenAdded(EffectScreen *screen) void QuickSceneEffect::handleScreenRemoved(EffectScreen *screen) { - delete d->views.take(screen); + d->views.erase(screen); } void QuickSceneEffect::addScreen(EffectScreen *screen) @@ -369,7 +355,7 @@ void QuickSceneEffect::addScreen(EffectScreen *screen) connect(view, &QuickSceneView::sceneChanged, view, &QuickSceneView::scheduleRepaint); view->scheduleRepaint(); - d->views.insert(screen, view); + d->views[screen].reset(view); } void QuickSceneEffect::startInternal() @@ -434,7 +420,6 @@ void QuickSceneEffect::stopInternal() disconnect(effects, &EffectsHandler::screenAdded, this, &QuickSceneEffect::handleScreenAdded); disconnect(effects, &EffectsHandler::screenRemoved, this, &QuickSceneEffect::handleScreenRemoved); - qDeleteAll(d->views); d->views.clear(); d->dummyWindow.reset(); d->running = false; @@ -495,9 +480,9 @@ void QuickSceneEffect::grabbedKeyboardEvent(QKeyEvent *keyEvent) bool QuickSceneEffect::touchDown(qint32 id, const QPointF &pos, quint32 time) { - for (QuickSceneView *screenView : std::as_const(d->views)) { + for (const auto &[screen, screenView] : d->views) { if (screenView->geometry().contains(pos.toPoint())) { - activateView(screenView); + activateView(screenView.get()); return screenView->forwardTouchDown(id, pos, time); } } @@ -506,7 +491,7 @@ bool QuickSceneEffect::touchDown(qint32 id, const QPointF &pos, quint32 time) bool QuickSceneEffect::touchMotion(qint32 id, const QPointF &pos, quint32 time) { - for (QuickSceneView *screenView : std::as_const(d->views)) { + for (const auto &[screen, screenView] : d->views) { if (screenView->geometry().contains(pos.toPoint())) { return screenView->forwardTouchMotion(id, pos, time); } @@ -516,7 +501,7 @@ bool QuickSceneEffect::touchMotion(qint32 id, const QPointF &pos, quint32 time) bool QuickSceneEffect::touchUp(qint32 id, quint32 time) { - for (QuickSceneView *screenView : std::as_const(d->views)) { + for (const auto &[screen, screenView] : d->views) { if (screenView->forwardTouchUp(id, time)) { return true; } diff --git a/src/libkwineffects/kwinquickeffect.h b/src/libkwineffects/kwinquickeffect.h index 7149ece9bb..51635734d7 100644 --- a/src/libkwineffects/kwinquickeffect.h +++ b/src/libkwineffects/kwinquickeffect.h @@ -85,10 +85,9 @@ public: QuickSceneView *activeView() const; /** - * Returns all scene views managed by this effect. If the effect is not running, - * this function returns an empty QHash. + * Returns the scene view on the specified screen */ - QHash views() const; + QuickSceneView *viewForScreen(EffectScreen *screen) const; /** * Returns the view at the specified @a pos in the global screen coordinates.