File indexing completed on 2024-04-28 05:26:27

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