File indexing completed on 2024-12-22 03:51:25
0001 /*************************************************************************** 0002 * Copyright 2010 Stefan Majewsky <majewsky@gmx.net> * 0003 * * 0004 * This program is free software; you can redistribute it and/or modify * 0005 * it under the terms of the GNU Library General Public License * 0006 * version 2 as published by the Free Software Foundation * 0007 * * 0008 * This program is distributed in the hope that it will be useful, * 0009 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 0010 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 0011 * GNU Library General Public License for more details. * 0012 * * 0013 * You should have received a copy of the GNU Library General Public * 0014 * License along with this program; if not, write to the * 0015 * Free Software Foundation, Inc., * 0016 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * 0017 ***************************************************************************/ 0018 0019 #ifndef TAGARO_BOARD_H 0020 #define TAGARO_BOARD_H 0021 0022 #include <QGraphicsObject> 0023 0024 namespace Tagaro { 0025 0026 /** 0027 * @class Tagaro::Board board.h <Tagaro/Board> 0028 * 0029 * The Tagaro::Board is basically a usual QGraphicsItem which can be used to 0030 * group items. However, it has two special features: 0031 * @li It can adjust its size automatically to fit into the bounding rect of the 0032 * parent item (or the scene rect, if there is no parent item). This 0033 * behavior is controlled by the alignment() property. 0034 * @li When it is resized, it will automatically adjust the renderSize of any 0035 * contained Tagaro::SpriteObjectItems. 0036 */ 0037 class Board : public QGraphicsObject 0038 { 0039 Q_OBJECT 0040 public: 0041 ///Creates a new Tagaro::Board instance below the given @a parent item. 0042 ///The logicalSize() is initialized to (1,1). The physicalSize() is 0043 ///determined from the parent item's bounding rect by using the default 0044 ///alignment Qt::AlignCenter. 0045 explicit Board(QGraphicsItem* parent = nullptr); 0046 ///Destroys the Tagaro::Board and all its children. 0047 ~Board() override; 0048 0049 ///@return the logical size of this board, i.e. the size of its 0050 ///bounding rect in the item's local coordinates 0051 QSizeF logicalSize() const; 0052 ///Sets the logical size of this board, i.e. the size of its bounding 0053 ///rect in the item's local coordinates. 0054 void setLogicalSize(const QSizeF& size); 0055 ///@return the size of this board, i.e. the size of its bounding rect in 0056 ///the parent item's local coordinates 0057 QSizeF size() const; 0058 ///Sets the size of this board, i.e. the size of its bounding rect in 0059 ///the parent item's local coordinates. 0060 ///@warning Calls to this method will disable automatic alignment by 0061 ///setting the alignment() to 0. 0062 void setSize(const QSizeF& size); 0063 ///@return the physical size factor @see setPhysicalSizeFactor 0064 qreal physicalSizeFactor() const; 0065 ///Sets the physical size factor. This factor will be used in the 0066 ///calculation of renderSizes for contained Tagaro::SpriteObjectItems. 0067 ///Leave this at 1.0 (the default) if scene coordinates and viewport 0068 ///coordinates have an equal scale (e.g. Tagaro::Scene and its main 0069 ///view). 0070 /// 0071 ///Values between 0.0 and 1.0 mean that the render size of the item is 0072 ///smaller than its actual size, e.g. because the viewport is smaller 0073 ///is smaller than the scene which contains. If the viewport is bigger, 0074 ///you will have to increase the render sizes with factors above 1.0. 0075 /// 0076 ///So if the viewport renders the board with size @a s in an area of 0077 ///size @a vs, you need to do: 0078 ///@code setPhysicalSizeFactor(vs.width() / s.width()); @endcode 0079 ///Of course, Repeat this whenever that ratio changes. 0080 void setPhysicalSizeFactor(qreal physicalSizeFactor); 0081 0082 ///@return the alignment of this board in the parent item's bounding 0083 ///rect (or the scene rect, if there is no parent item) 0084 Qt::Alignment alignment() const; 0085 ///Sets the @a alignment of this board in the parent item's bounding 0086 ///rect (or the scene rect, if there is no parent item). If an alignment 0087 ///is set, changes to the scene rect will cause the board to change its 0088 ///size and location to fit into the parent item. The board keeps its 0089 ///aspect ratio and determines its position from the @a alignment. 0090 /// 0091 ///The default alignment is Qt::AlignCenter. Call this function with 0092 ///argument 0 to disable the alignment behavior. 0093 ///@note The flag Qt::AlignJustify is not interpreted. 0094 void setAlignment(Qt::Alignment alignment); 0095 0096 QRectF boundingRect() const override; 0097 void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget = nullptr) override; 0098 protected: 0099 QVariant itemChange(QGraphicsItem::GraphicsItemChange change, const QVariant& value) override; 0100 void timerEvent(QTimerEvent* event) override; 0101 private: 0102 struct Private; 0103 Private* const d; 0104 Q_PRIVATE_SLOT(d, void _k_update()) 0105 Q_PRIVATE_SLOT(d, void _k_updateItem()) 0106 }; 0107 0108 } //namespace Tagaro 0109 0110 #endif // TAGARO_BOARD_H