KDEGames

kgamerenderedobjectitem.h
1 /*
2  SPDX-FileCopyrightText: 2010 Stefan Majewsky <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.0-only
5 */
6 
7 #ifndef KGAMERENDEREDOBJECTITEM_H
8 #define KGAMERENDEREDOBJECTITEM_H
9 
10 // own
11 #include "kgamerendererclient.h"
12 #include <libkdegames_export.h>
13 // Qt
14 #include <QObject>
15 #include <QGraphicsItem>
16 // Std
17 #include <memory>
18 
19 class QGraphicsView;
20 
21 class KGameRenderedObjectItemPrivate;
22 
23 /**
24  * @class KGameRenderedObjectItem kgamerenderedobjectitem.h <KGameRenderedObjectItem>
25  * @since 4.6
26  * @short A QGraphicsObject which displays pixmaps from a KGameRenderer.
27  *
28  * This item displays a pixmap which is retrieved from a KGameRenderer, and is
29  * updated automatically when the KGameRenderer changes the theme.
30  *
31  * The item has built-in handling for animated sprites (i.e. those with multiple
32  * frames). It is a QGraphicsObject and exposes a "frame" property, so you can
33  * easily run the animation by plugging in a QPropertyAnimation.
34  *
35  * @section operationalmodes Modes of operation
36  *
37  * By default, this item behaves just like a QGraphicsPixmapItem. The size of
38  * its bounding rect is equal to the size of the pixmap, i.e. the renderSize().
39  *
40  * However, the KGameRenderedObjectItem has a second mode of operation, which is
41  * enabled by setting a "primary view". (This can be done automatically via
42  * KGameRenderer::setDefaultPrimaryView.)
43  *
44  * If such a primary view is set, the following happens:
45  * \li The renderSize of the pixmap is automatically determined from the
46  * painting requests received from the primary view (manual calls to
47  * setRenderSize() are unnecessary and need to be avoided).
48  * \li The size of the item's boundingRect() is independent of the renderSize().
49  * The default fixedSize() is 1x1, which means that the item's bounding rect
50  * is the unit square (moved by the configured offset()).
51  */
52 class KDEGAMES_EXPORT KGameRenderedObjectItem : public QGraphicsObject, public KGameRendererClient
53 {
54  Q_OBJECT
55  Q_PROPERTY(int frame READ frame WRITE setFrame)
56  public:
57  ///Creates a new KGameRenderedObjectItem which renders the sprite with
58  ///the given @a spriteKey as provided by the given @a renderer.
59  KGameRenderedObjectItem(KGameRenderer* renderer, const QString& spriteKey, QGraphicsItem* parent = nullptr);
60  ~KGameRenderedObjectItem() override;
61 
62  ///@return the item's offset, which defines the point of the top-left
63  ///corner of the bounding rect, in local coordinates.
64  QPointF offset() const;
65  ///Sets the item's offset, which defines the point of the top-left
66  ///corner of the bounding rect, in local coordinates.
67  void setOffset(const QPointF& offset);
68  ///@overload
69  void setOffset(qreal x, qreal y);
70  ///@return the fixed size of this item (or (-1, -1) if this item has no
71  ///primary view)
72  QSizeF fixedSize() const;
73  ///Sets the fixed size of this item, i.e. the guaranteed size of the
74  ///item. This works only when a primary view has been set.
75  void setFixedSize(const QSizeF& size);
76 
77  ///Returns a pointer to the current primary view, or 0 if no primary
78  ///view has been set (which is the default).
79  ///@see setPrimaryView()
80  QGraphicsView* primaryView() const;
81  ///Sets the primary view of this item. (See class documentation for what
82  ///the primary view does.) Pass a null pointer to just disconnect from
83  ///the current primary view. The fixed size is then reset to (-1, -1).
84  ///If a primary view is set, the fixed size is initialized to (1, 1).
85  ///@warning While a primary view is set, avoid any manual calls to
86  ///setRenderSize().
87  ///@see {Modes of operation}
88  void setPrimaryView(QGraphicsView* view);
89 
90  //QGraphicsItem reimplementations (see comment in source file for why we need all of this)
91  QRectF boundingRect() const override;
92  bool contains(const QPointF& point) const override;
93  bool isObscuredBy(const QGraphicsItem* item) const override;
94  QPainterPath opaqueArea() const override;
95  void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget = nullptr) override;
96  QPainterPath shape() const override;
97  protected:
98  void receivePixmap(const QPixmap& pixmap) override;
99  private:
100  friend class KGameRenderedObjectItemPrivate;
101  std::unique_ptr<KGameRenderedObjectItemPrivate> const d;
102 };
103 
104 #endif // KGAMERENDEREDOBJECTITEM_H
virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)=0
A QGraphicsObject which displays pixmaps from a KGameRenderer.
virtual QPainterPath opaqueArea() const const
virtual void receivePixmap(const QPixmap &pixmap)=0
This method is called when the KGameRenderer has provided a new pixmap for this client (esp...
Cache-enabled rendering of SVG themes.
Definition: kgamerenderer.h:86
Q_PROPERTY(...)
An object that receives pixmaps from a KGameRenderer.
virtual bool isObscuredBy(const QGraphicsItem *item) const const
virtual QRectF boundingRect() const const =0
virtual QPainterPath shape() const const
virtual bool contains(const QPointF &point) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Tue Dec 7 2021 22:34:14 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.