File indexing completed on 2024-05-19 16:30:43

0001 /*
0002  * SPDX-FileCopyrightText: 2014 Hugo Pereira Da Costa <hugo.pereira@free.fr>
0003  *
0004  * SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 
0007 #ifndef breezetileset_h
0008 #define breezetileset_h
0009 
0010 #include <QPixmap>
0011 #include <QRect>
0012 #include <QVector>
0013 
0014 //* handles proper scaling of pixmap to match widget rect.
0015 /**
0016 tilesets are collections of stretchable pixmaps corresponding to a given widget corners, sides, and center.
0017 corner pixmaps are never stretched. center pixmaps are
0018 */
0019 namespace Breeze
0020 {
0021 class TileSet
0022 {
0023 public:
0024     /**
0025     Create a TileSet from a pixmap. The size of the bottom/right chunks is
0026     whatever is left over from the other chunks, whose size is specified
0027     in the required parameters.
0028 
0029     @param w1 width of the left chunks
0030     @param h1 height of the top chunks
0031     @param w2 width of the not-left-or-right chunks
0032     @param h2 height of the not-top-or-bottom chunks
0033     */
0034     TileSet(const QPixmap &, int w1, int h1, int w2, int h2);
0035 
0036     //* empty constructor
0037     TileSet();
0038 
0039     //* destructor
0040     virtual ~TileSet()
0041     {
0042     }
0043 
0044     /**
0045     Flags specifying what sides to draw in ::render. Corners are drawn when
0046     the sides forming that corner are drawn, e.g. Top|Left draws the
0047     top-center, center-left, and top-left chunks. The center-center chunk is
0048     only drawn when Center is requested.
0049     */
0050     enum Tile {
0051         Top = 0x1,
0052         Left = 0x2,
0053         Bottom = 0x4,
0054         Right = 0x8,
0055         Center = 0x10,
0056         TopLeft = Top | Left,
0057         TopRight = Top | Right,
0058         BottomLeft = Bottom | Left,
0059         BottomRight = Bottom | Right,
0060         Ring = Top | Left | Bottom | Right,
0061         Horizontal = Left | Right | Center,
0062         Vertical = Top | Bottom | Center,
0063         Full = Ring | Center,
0064     };
0065     Q_DECLARE_FLAGS(Tiles, Tile)
0066 
0067     /**
0068     Fills the specified rect with tiled chunks. Corners are never tiled,
0069     edges are tiled in one direction, and the center chunk is tiled in both
0070     directions. Partial tiles are used as needed so that the entire rect is
0071     perfectly filled. Filling is performed as if all chunks are being drawn.
0072     */
0073     void render(const QRect &, QPainter *, Tiles = Ring) const;
0074 
0075     //* return size associated to this tileset
0076     QSize size() const
0077     {
0078         return QSize(_w1 + _w3, _h1 + _h3);
0079     }
0080 
0081     //* is valid
0082     bool isValid() const
0083     {
0084         return _pixmaps.size() == 9;
0085     }
0086 
0087     //* returns pixmap for given index
0088     QPixmap pixmap(int index) const
0089     {
0090         return _pixmaps[index];
0091     }
0092 
0093 protected:
0094     //* shortcut to pixmap list
0095     using PixmapList = QVector<QPixmap>;
0096 
0097     //* initialize pixmap
0098     void initPixmap(PixmapList &, const QPixmap &, int w, int h, const QRect &);
0099 
0100 private:
0101     //* pixmap arry
0102     PixmapList _pixmaps;
0103 
0104     // dimensions
0105     int _w1;
0106     int _h1;
0107     int _w3;
0108     int _h3;
0109 };
0110 
0111 }
0112 
0113 Q_DECLARE_OPERATORS_FOR_FLAGS(Breeze::TileSet::Tiles)
0114 
0115 #endif // TILESET_H