File indexing completed on 2024-05-19 05:32:19

0001 /*
0002     SPDX-FileCopyrightText: 2021 Vlad Zahorodnii <vlad.zahorodnii@kde.org>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #pragma once
0008 
0009 #include "core/colorspace.h"
0010 #include "effect/globals.h"
0011 #include "scene/itemgeometry.h"
0012 
0013 #include <QList>
0014 #include <QMatrix4x4>
0015 #include <QObject>
0016 #include <QPointer>
0017 
0018 #include <optional>
0019 
0020 namespace KWin
0021 {
0022 
0023 class SceneDelegate;
0024 class Scene;
0025 
0026 /**
0027  * The Item class is the base class for items in the scene.
0028  */
0029 class KWIN_EXPORT Item : public QObject
0030 {
0031     Q_OBJECT
0032 
0033 public:
0034     explicit Item(Scene *scene, Item *parent = nullptr);
0035     ~Item() override;
0036 
0037     Scene *scene() const;
0038 
0039     qreal opacity() const;
0040     void setOpacity(qreal opacity);
0041 
0042     QPointF position() const;
0043     void setPosition(const QPointF &point);
0044 
0045     QSizeF size() const;
0046     void setSize(const QSizeF &size);
0047 
0048     int z() const;
0049     void setZ(int z);
0050 
0051     /**
0052      * Returns the enclosing rectangle of the item. The rect equals QRect(0, 0, width(), height()).
0053      */
0054     QRectF rect() const;
0055     /**
0056      * Returns the enclosing rectangle of the item and all of its descendants.
0057      */
0058     QRectF boundingRect() const;
0059 
0060     virtual QList<QRectF> shape() const;
0061     virtual QRegion opaque() const;
0062 
0063     /**
0064      * Returns the visual parent of the item. Note that the visual parent differs from
0065      * the QObject parent.
0066      */
0067     Item *parentItem() const;
0068     void setParentItem(Item *parent);
0069     QList<Item *> childItems() const;
0070     QList<Item *> sortedChildItems() const;
0071 
0072     QPointF rootPosition() const;
0073 
0074     QMatrix4x4 transform() const;
0075     void setTransform(const QMatrix4x4 &transform);
0076 
0077     /**
0078      * Maps the given @a region from the item's coordinate system to the scene's coordinate
0079      * system.
0080      */
0081     QRegion mapToGlobal(const QRegion &region) const;
0082     /**
0083      * Maps the given @a rect from the item's coordinate system to the scene's coordinate
0084      * system.
0085      */
0086     QRectF mapToGlobal(const QRectF &rect) const;
0087     /**
0088      * Maps the given @a rect from the scene's coordinate system to the item's coordinate
0089      * system.
0090      */
0091     QRectF mapFromGlobal(const QRectF &rect) const;
0092 
0093     /**
0094      * Moves this item right before the specified @a sibling in the parent's children list.
0095      */
0096     void stackBefore(Item *sibling);
0097     /**
0098      * Moves this item right after the specified @a sibling in the parent's children list.
0099      */
0100     void stackAfter(Item *sibling);
0101 
0102     bool explicitVisible() const;
0103     bool isVisible() const;
0104     void setVisible(bool visible);
0105 
0106     void scheduleRepaint(const QRectF &region);
0107     void scheduleRepaint(const QRegion &region);
0108     void scheduleRepaint(SceneDelegate *delegate, const QRegion &region);
0109     void scheduleFrame();
0110     QRegion repaints(SceneDelegate *delegate) const;
0111     void resetRepaints(SceneDelegate *delegate);
0112 
0113     WindowQuadList quads() const;
0114     virtual void preprocess();
0115     const ColorDescription &colorDescription() const;
0116     PresentationModeHint presentationHint() const;
0117 
0118 Q_SIGNALS:
0119     void childAdded(Item *item);
0120     /**
0121      * This signal is emitted when the position of this item has changed.
0122      */
0123     void positionChanged();
0124     /**
0125      * This signal is emitted when the size of this item has changed.
0126      */
0127     void sizeChanged();
0128 
0129     /**
0130      * This signal is emitted when the rectangle that encloses this item and all of its children
0131      * has changed.
0132      */
0133     void boundingRectChanged();
0134 
0135 protected:
0136     virtual WindowQuadList buildQuads() const;
0137     void discardQuads();
0138     void setColorDescription(const ColorDescription &description);
0139     void setPresentationHint(PresentationModeHint hint);
0140 
0141 private:
0142     void addChild(Item *item);
0143     void removeChild(Item *item);
0144     void updateBoundingRect();
0145     void scheduleRepaintInternal(const QRegion &region);
0146     void scheduleRepaintInternal(SceneDelegate *delegate, const QRegion &region);
0147     void markSortedChildItemsDirty();
0148 
0149     bool computeEffectiveVisibility() const;
0150     void updateEffectiveVisibility();
0151     void removeRepaints(SceneDelegate *delegate);
0152 
0153     Scene *m_scene;
0154     QPointer<Item> m_parentItem;
0155     QList<Item *> m_childItems;
0156     QMatrix4x4 m_transform;
0157     QRectF m_boundingRect;
0158     QPointF m_position;
0159     QSizeF m_size = QSize(0, 0);
0160     qreal m_opacity = 1;
0161     int m_z = 0;
0162     bool m_explicitVisible = true;
0163     bool m_effectiveVisible = true;
0164     QMap<SceneDelegate *, QRegion> m_repaints;
0165     mutable std::optional<WindowQuadList> m_quads;
0166     mutable std::optional<QList<Item *>> m_sortedChildItems;
0167     ColorDescription m_colorDescription = ColorDescription::sRGB;
0168     PresentationModeHint m_presentationHint = PresentationModeHint::VSync;
0169 };
0170 
0171 } // namespace KWin