File indexing completed on 2024-11-10 04:57:14

0001 /*
0002     SPDX-FileCopyrightText: 2022 Vlad Zahorodnii <vlad.zahorodnii@kde.org>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "scene/cursoritem.h"
0008 #include "cursor.h"
0009 #include "cursorsource.h"
0010 #include "effect/effect.h"
0011 #include "scene/imageitem.h"
0012 #include "scene/itemrenderer.h"
0013 #include "scene/scene.h"
0014 #include "scene/surfaceitem_wayland.h"
0015 
0016 namespace KWin
0017 {
0018 
0019 CursorItem::CursorItem(Scene *scene, Item *parent)
0020     : Item(scene, parent)
0021 {
0022     refresh();
0023     connect(Cursors::self(), &Cursors::currentCursorChanged, this, &CursorItem::refresh);
0024 }
0025 
0026 CursorItem::~CursorItem()
0027 {
0028 }
0029 
0030 void CursorItem::refresh()
0031 {
0032     const CursorSource *source = Cursors::self()->currentCursor()->source();
0033     if (auto surfaceSource = qobject_cast<const SurfaceCursorSource *>(source)) {
0034         setSurface(surfaceSource->surface());
0035     } else if (auto shapeSource = qobject_cast<const ShapeCursorSource *>(source)) {
0036         setImage(shapeSource->image());
0037     }
0038 }
0039 
0040 void CursorItem::setSurface(SurfaceInterface *surface)
0041 {
0042     m_imageItem.reset();
0043 
0044     if (!m_surfaceItem || m_surfaceItem->surface() != surface) {
0045         if (surface) {
0046             m_surfaceItem = std::make_unique<SurfaceItemWayland>(surface, scene(), this);
0047         } else {
0048             m_surfaceItem.reset();
0049         }
0050     }
0051 }
0052 
0053 void CursorItem::setImage(const QImage &image)
0054 {
0055     m_surfaceItem.reset();
0056 
0057     if (!m_imageItem) {
0058         m_imageItem = scene()->renderer()->createImageItem(scene(), this);
0059     }
0060     m_imageItem->setImage(image);
0061     m_imageItem->setSize(image.size() / image.devicePixelRatio());
0062 }
0063 
0064 } // namespace KWin
0065 
0066 #include "moc_cursoritem.cpp"