1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
5 **
6 ** This file is part of the documentation of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** You may use this file under the terms of the BSD license as follows:
10 **
11 ** "Redistribution and use in source and binary forms, with or without
12 ** modification, are permitted provided that the following conditions are
13 ** met:
14 ** * Redistributions of source code must retain the above copyright
15 ** notice, this list of conditions and the following disclaimer.
16 ** * Redistributions in binary form must reproduce the above copyright
17 ** notice, this list of conditions and the following disclaimer in
18 ** the documentation and/or other materials provided with the
19 ** distribution.
20 ** * Neither the name of The Qt Company Ltd nor the names of its
21 ** contributors may be used to endorse or promote products derived
22 ** from this software without specific prior written permission.
23 **
24 **
25 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
36 **
37 ** $QT_END_LICENSE$
38 **
39 ****************************************************************************/
40
41 //! [0]
42 item->scene().sceneRect().intersects(item->sceneBoundingRect());
43 //! [0]
44
45
46 //! [1]
47 class TileScene : public QGraphicsScene
48 {
49 public:
50 ...
51
52 void setTiles(const QPixmap &pixmap, int h, int v,
53 int tileHeight, int tileWidth);
54 void setTile(int x, int y, int tilenum);
55
56 private:
57 QRect tileRect(int x, int y) const;
58 QRect tileRect(int tileNum) const;
59
60 QVector<QVector<int> > tiles;
61 QPixmap tilePixmap;
62 int tileW, tileH;
63 int hTiles, vTiles;
64 };
65 //! [1]
66
67
68 //! [2]
setTiles(const QPixmap & pixmap,int h,int v,int tileHeight,int tileWidth)69 void TileScene::setTiles(const QPixmap &pixmap, int h, int v,
70 int tileHeight, int tileWidth)
71 {
72 tilePixmap = pixmap;
73 tileW = tileWidth;
74 tileH = tileHeight;
75 hTiles = h;
76 vTiles = v;
77
78 tiles.resize(v);
79 for (int y = 0; y < v; ++y)
80 tiles[y].resize(h);
81 }
82 //! [2]
83
84
85 //! [3]
setTile(int x,int y,int tilenum)86 void TileScene::setTile(int x, int y, int tilenum)
87 {
88 tiles[y][x] = tilenum;
89 update(tileRect(x, y));
90 }
91 //! [3]
92
93
94 //! [4]
tileRect(int x,int y) const95 QRect TileScene::tileRect(int x, int y) const
96 {
97 return QRect(x * tileW, y * tileH, tileW, tileH);
98 }
99 //! [4]
100
101
102 //! [5]
tileRect(int tileNum) const103 QRect TileScene::tileRect(int tileNum) const
104 {
105 int numHTiles = tilePixmap.width() / tileW;
106 int numVTiles = tilePixmap.height() / tileH;
107 return tileRect(tileNum % numHTiles, tileNum / numHTiles);
108 }
109 //! [5]
110
111
112 //! [6]
drawBackground(QPainter * painter,const QRectF & exposed)113 void drawBackground(QPainter *painter, const QRectF &exposed)
114 {
115 for (int y = 0; y < vTiles; ++y) {
116 for (int x = 0; x < hTiles; ++x) {
117 QRect destRect = tileRect(x, y);
118 if (exposed.intersects(destRect)) {
119 painter->drawPixmap(destRect, tilePixmap,
120 tileRect(tiles[y][x]));
121 }
122 }
123 }
124 }
125 //! [6]
126
127
128 //! [7]
129 // Before
130 Q3CanvasEllipse ellipse(10, 10);
131
132 // After
133 QGraphicsEllipseItem ellipse(-5, -5, 10, 10);
134 //! [7]
135
136
137 //! [8]
fromControlPoints(const Q3PointArray & pa)138 static QPainterPath fromControlPoints(const Q3PointArray &pa)
139 {
140 QPainterPath path;
141 path.moveTo(pa[0]);
142 for (int i = 1; i < pa.size(); i += 3)
143 path.cubicTo(pa[i], pa[(i + 1) % pa.size()], pa[(i + 2) % pa.size()]);
144 return path;
145 }
146 //! [8]
147
148
149 //! [9]
150 wildcardPath.replace("%1", "*");
151 QFileInfo fi(wildcardPath);
152
153 QList<QPixmap> frames;
154 foreach (QString entry, QDir(fi.path(), fi.fileName()).entryList())
155 frames << QPixmap(fi.path() + "/" + entry);
156 //! [9]
157