1 /*****************************************************************************
2  *   Copyright 2007 Matthew Woehlke <mw_triad@users.sourceforge.net>         *
3  *   Copyright 2007 - 2010 Craig Drummond <craig.p.drummond@gmail.com>       *
4  *   Copyright 2013 - 2015 Yichao Yu <yyc1992@gmail.com>                     *
5  *                                                                           *
6  *   This program is free software; you can redistribute it and/or modify    *
7  *   it under the terms of the GNU Lesser General Public License as          *
8  *   published by the Free Software Foundation; either version 2.1 of the    *
9  *   License, or (at your option) version 3, or any later version accepted   *
10  *   by the membership of KDE e.V. (or its successor approved by the         *
11  *   membership of KDE e.V.), which shall act as a proxy defined in          *
12  *   Section 6 of version 3 of the license.                                  *
13  *                                                                           *
14  *   This program is distributed in the hope that it will be useful,         *
15  *   but WITHOUT ANY WARRANTY; without even the implied warranty of          *
16  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU       *
17  *   Lesser General Public License for more details.                         *
18  *                                                                           *
19  *   You should have received a copy of the GNU Lesser General Public        *
20  *   License along with this library. If not,                                *
21  *   see <http://www.gnu.org/licenses/>.                                     *
22  *****************************************************************************/
23 
24 #ifndef TILESET_H
25 #define TILESET_H
26 
27 #include <QPixmap>
28 #include <QRect>
29 #include <QVector>
30 
31 //! handles proper scaling of pixmap to match widget rect.
32 /*!
33 tilesets are collections of stretchable pixmaps corresponding to a given widget corners, sides, and center.
34 corner pixmaps are never stretched. center pixmaps are
35 */
36 class TileSet {
37 public:
38    /**
39     * Create a TileSet from a pixmap. The size of the bottom/right chunks is
40     * whatever is left over from the other chunks, whose size is specified
41     * in the required parameters.
42     *
43     * @param w1 width of the left chunks
44     * @param h1 height of the top chunks
45     * @param w2 width of the not-left-or-right chunks
46     * @param h2 height of the not-top-or-bottom chunks
47     */
48     TileSet(const QPixmap&, int w1, int h1, int w2, int h2);
49 
50    /**
51     * Create a TileSet from a pixmap. The size of the top/left and bottom/right
52     * chunks is specified, with the middle chunks created from the specified
53     * portion of the pixmap. This allows the middle chunks to overlap the outer
54     * chunks (or to not use all pixels). The top/left and bottom/right chunks
55     * are carved out of the corners of the pixmap.
56     *
57     * @param w1 width of the left chunks
58     * @param h1 height of the top chunks
59     * @param w3 width of the right chunks
60     * @param h3 height of bottom chunks
61     * @param x2 x-coordinate of the top of the not-left-or-right chunks
62     * @param y2 y-coordinate of the left of the not-top-or-bottom chunks
63     * @param w2 width of the not-left-or-right chunks
64     * @param h2 height of the not-top-or-bottom chunks
65     */
66     TileSet(const QPixmap &pix, int w1, int h1, int w3, int h3, int x2, int y2, int w2, int h2);
67 
68     //! empty constructor
69     TileSet();
70 
71     /**
72      * Flags specifying what sides to draw in ::render. Corners are drawn when
73      * the sides forming that corner are drawn, e.g. Top|Left draws the
74      * top-center, center-left, and top-left chunks. The center-center chunk is
75      * only drawn when Center is requested.
76      */
77     enum Tile {
78         Top = 0x1,
79         Left = 0x2,
80         Right = 0x8,
81         Bottom = 0x4,
82         Center = 0x10,
83         Ring = 0x0f,
84         Horizontal = 0x1a,
85         Vertical = 0x15,
86         Full = 0x1f
87     };
88     Q_DECLARE_FLAGS(Tiles, Tile)
89 
90     /**
91      * Fills the specified rect with tiled chunks. Corners are never tiled,
92      * edges are tiled in one direction, and the center chunk is tiled in both
93      * directions. Partial tiles are used as needed so that the entire rect is
94      * perfectly filled. Filling is performed as if all chunks are being drawn.
95      */
96     void render(const QRect&, QPainter*, Tiles = Ring) const;
97 
98 protected:
99     // initialize pixmap
100     void initPixmap( int, const QPixmap&, int w, int h, const QRect &region);
101 
102     //! pixmap arry
103     QVector<QPixmap> _pixmap;
104 
105     // dimensions
106     int _w1;
107     int _h1;
108     int _w3;
109     int _h3;
110 };
111 
112 Q_DECLARE_OPERATORS_FOR_FLAGS(TileSet::Tiles)
113 
114 #endif //TILESET_H
115