Absolute File Name: | /home/opencoverage/opencoverage/guest-scripts/qtdeclarative/src/qtdeclarative/src/quick/items/qquickrectangle.cpp |
Source code | Switch to Preprocessed file |
Line | Source | Count | ||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | /**************************************************************************** | - | ||||||||||||||||||||||||
2 | ** | - | ||||||||||||||||||||||||
3 | ** Copyright (C) 2016 The Qt Company Ltd. | - | ||||||||||||||||||||||||
4 | ** Contact: https://www.qt.io/licensing/ | - | ||||||||||||||||||||||||
5 | ** | - | ||||||||||||||||||||||||
6 | ** This file is part of the QtQuick module of the Qt Toolkit. | - | ||||||||||||||||||||||||
7 | ** | - | ||||||||||||||||||||||||
8 | ** $QT_BEGIN_LICENSE:LGPL$ | - | ||||||||||||||||||||||||
9 | ** Commercial License Usage | - | ||||||||||||||||||||||||
10 | ** Licensees holding valid commercial Qt licenses may use this file in | - | ||||||||||||||||||||||||
11 | ** accordance with the commercial license agreement provided with the | - | ||||||||||||||||||||||||
12 | ** Software or, alternatively, in accordance with the terms contained in | - | ||||||||||||||||||||||||
13 | ** a written agreement between you and The Qt Company. For licensing terms | - | ||||||||||||||||||||||||
14 | ** and conditions see https://www.qt.io/terms-conditions. For further | - | ||||||||||||||||||||||||
15 | ** information use the contact form at https://www.qt.io/contact-us. | - | ||||||||||||||||||||||||
16 | ** | - | ||||||||||||||||||||||||
17 | ** GNU Lesser General Public License Usage | - | ||||||||||||||||||||||||
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser | - | ||||||||||||||||||||||||
19 | ** General Public License version 3 as published by the Free Software | - | ||||||||||||||||||||||||
20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the | - | ||||||||||||||||||||||||
21 | ** packaging of this file. Please review the following information to | - | ||||||||||||||||||||||||
22 | ** ensure the GNU Lesser General Public License version 3 requirements | - | ||||||||||||||||||||||||
23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. | - | ||||||||||||||||||||||||
24 | ** | - | ||||||||||||||||||||||||
25 | ** GNU General Public License Usage | - | ||||||||||||||||||||||||
26 | ** Alternatively, this file may be used under the terms of the GNU | - | ||||||||||||||||||||||||
27 | ** General Public License version 2.0 or (at your option) the GNU General | - | ||||||||||||||||||||||||
28 | ** Public license version 3 or any later version approved by the KDE Free | - | ||||||||||||||||||||||||
29 | ** Qt Foundation. The licenses are as published by the Free Software | - | ||||||||||||||||||||||||
30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 | - | ||||||||||||||||||||||||
31 | ** included in the packaging of this file. Please review the following | - | ||||||||||||||||||||||||
32 | ** information to ensure the GNU General Public License requirements will | - | ||||||||||||||||||||||||
33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and | - | ||||||||||||||||||||||||
34 | ** https://www.gnu.org/licenses/gpl-3.0.html. | - | ||||||||||||||||||||||||
35 | ** | - | ||||||||||||||||||||||||
36 | ** $QT_END_LICENSE$ | - | ||||||||||||||||||||||||
37 | ** | - | ||||||||||||||||||||||||
38 | ****************************************************************************/ | - | ||||||||||||||||||||||||
39 | - | |||||||||||||||||||||||||
40 | #include "qquickrectangle_p.h" | - | ||||||||||||||||||||||||
41 | #include "qquickrectangle_p_p.h" | - | ||||||||||||||||||||||||
42 | - | |||||||||||||||||||||||||
43 | #include <QtQuick/private/qsgcontext_p.h> | - | ||||||||||||||||||||||||
44 | #include <private/qsgadaptationlayer_p.h> | - | ||||||||||||||||||||||||
45 | - | |||||||||||||||||||||||||
46 | #include <QtGui/qpixmapcache.h> | - | ||||||||||||||||||||||||
47 | #include <QtCore/qmath.h> | - | ||||||||||||||||||||||||
48 | #include <QtCore/qmetaobject.h> | - | ||||||||||||||||||||||||
49 | - | |||||||||||||||||||||||||
50 | QT_BEGIN_NAMESPACE | - | ||||||||||||||||||||||||
51 | - | |||||||||||||||||||||||||
52 | // XXX todo - should we change rectangle to draw entirely within its width/height? | - | ||||||||||||||||||||||||
53 | /*! | - | ||||||||||||||||||||||||
54 | \internal | - | ||||||||||||||||||||||||
55 | \class QQuickPen | - | ||||||||||||||||||||||||
56 | \brief For specifying a pen used for drawing rectangle borders on a QQuickView | - | ||||||||||||||||||||||||
57 | - | |||||||||||||||||||||||||
58 | By default, the pen is invalid and nothing is drawn. You must either set a color (then the default | - | ||||||||||||||||||||||||
59 | width is 1) or a width (then the default color is black). | - | ||||||||||||||||||||||||
60 | - | |||||||||||||||||||||||||
61 | A width of 1 indicates is a single-pixel line on the border of the item being painted. | - | ||||||||||||||||||||||||
62 | - | |||||||||||||||||||||||||
63 | Example: | - | ||||||||||||||||||||||||
64 | \qml | - | ||||||||||||||||||||||||
65 | Rectangle { | - | ||||||||||||||||||||||||
66 | border.width: 2 | - | ||||||||||||||||||||||||
67 | border.color: "red" | - | ||||||||||||||||||||||||
68 | } | - | ||||||||||||||||||||||||
69 | \endqml | - | ||||||||||||||||||||||||
70 | */ | - | ||||||||||||||||||||||||
71 | - | |||||||||||||||||||||||||
72 | QQuickPen::QQuickPen(QObject *parent) | - | ||||||||||||||||||||||||
73 | : QObject(parent) | - | ||||||||||||||||||||||||
74 | , m_width(1) | - | ||||||||||||||||||||||||
75 | , m_color(Qt::black) | - | ||||||||||||||||||||||||
76 | , m_aligned(true) | - | ||||||||||||||||||||||||
77 | , m_valid(false) | - | ||||||||||||||||||||||||
78 | { | - | ||||||||||||||||||||||||
79 | } executed 53997 times by 22 tests: end of block Executed by:
| 53997 | ||||||||||||||||||||||||
80 | - | |||||||||||||||||||||||||
81 | qreal QQuickPen::width() const | - | ||||||||||||||||||||||||
82 | { | - | ||||||||||||||||||||||||
83 | return m_width; executed 44924 times by 20 tests: return m_width; Executed by:
| 44924 | ||||||||||||||||||||||||
84 | } | - | ||||||||||||||||||||||||
85 | - | |||||||||||||||||||||||||
86 | void QQuickPen::setWidth(qreal w) | - | ||||||||||||||||||||||||
87 | { | - | ||||||||||||||||||||||||
88 | if (m_width == w && m_valid)
| 1152-44038 | ||||||||||||||||||||||||
89 | return; executed 2022 times by 3 tests: return; Executed by:
| 2022 | ||||||||||||||||||||||||
90 | - | |||||||||||||||||||||||||
91 | m_width = w; | - | ||||||||||||||||||||||||
92 | m_valid = m_color.alpha() && (qRound(m_width) >= 1 || (!m_aligned && m_width > 0));
| 0-43168 | ||||||||||||||||||||||||
93 | static_cast<QQuickItem*>(parent())->update(); | - | ||||||||||||||||||||||||
94 | emit penChanged(); | - | ||||||||||||||||||||||||
95 | } executed 43168 times by 20 tests: end of block Executed by:
| 43168 | ||||||||||||||||||||||||
96 | - | |||||||||||||||||||||||||
97 | QColor QQuickPen::color() const | - | ||||||||||||||||||||||||
98 | { | - | ||||||||||||||||||||||||
99 | return m_color; executed 44838 times by 17 tests: return m_color; Executed by:
| 44838 | ||||||||||||||||||||||||
100 | } | - | ||||||||||||||||||||||||
101 | - | |||||||||||||||||||||||||
102 | void QQuickPen::setColor(const QColor &c) | - | ||||||||||||||||||||||||
103 | { | - | ||||||||||||||||||||||||
104 | m_color = c; | - | ||||||||||||||||||||||||
105 | m_valid = m_color.alpha() && (qRound(m_width) >= 1 || (!m_aligned && m_width > 0));
| 0-12275 | ||||||||||||||||||||||||
106 | static_cast<QQuickItem*>(parent())->update(); | - | ||||||||||||||||||||||||
107 | emit penChanged(); | - | ||||||||||||||||||||||||
108 | } executed 12547 times by 15 tests: end of block Executed by:
| 12547 | ||||||||||||||||||||||||
109 | - | |||||||||||||||||||||||||
110 | bool QQuickPen::pixelAligned() const | - | ||||||||||||||||||||||||
111 | { | - | ||||||||||||||||||||||||
112 | return m_aligned; executed 44269 times by 16 tests: return m_aligned; Executed by:
| 44269 | ||||||||||||||||||||||||
113 | } | - | ||||||||||||||||||||||||
114 | - | |||||||||||||||||||||||||
115 | void QQuickPen::setPixelAligned(bool aligned) | - | ||||||||||||||||||||||||
116 | { | - | ||||||||||||||||||||||||
117 | if (aligned == m_aligned)
| 0 | ||||||||||||||||||||||||
118 | return; never executed: return; | 0 | ||||||||||||||||||||||||
119 | m_aligned = aligned; | - | ||||||||||||||||||||||||
120 | m_valid = m_color.alpha() && (qRound(m_width) >= 1 || (!m_aligned && m_width > 0));
| 0 | ||||||||||||||||||||||||
121 | static_cast<QQuickItem*>(parent())->update(); | - | ||||||||||||||||||||||||
122 | emit penChanged(); | - | ||||||||||||||||||||||||
123 | } never executed: end of block | 0 | ||||||||||||||||||||||||
124 | - | |||||||||||||||||||||||||
125 | bool QQuickPen::isValid() const | - | ||||||||||||||||||||||||
126 | { | - | ||||||||||||||||||||||||
127 | return m_valid; executed 44519 times by 15 tests: return m_valid; Executed by:
| 44519 | ||||||||||||||||||||||||
128 | } | - | ||||||||||||||||||||||||
129 | - | |||||||||||||||||||||||||
130 | /*! | - | ||||||||||||||||||||||||
131 | \qmltype GradientStop | - | ||||||||||||||||||||||||
132 | \instantiates QQuickGradientStop | - | ||||||||||||||||||||||||
133 | \inqmlmodule QtQuick | - | ||||||||||||||||||||||||
134 | \ingroup qtquick-visual-utility | - | ||||||||||||||||||||||||
135 | \brief Defines the color at a position in a Gradient. | - | ||||||||||||||||||||||||
136 | - | |||||||||||||||||||||||||
137 | \sa Gradient | - | ||||||||||||||||||||||||
138 | */ | - | ||||||||||||||||||||||||
139 | - | |||||||||||||||||||||||||
140 | /*! | - | ||||||||||||||||||||||||
141 | \qmlproperty real QtQuick::GradientStop::position | - | ||||||||||||||||||||||||
142 | \qmlproperty color QtQuick::GradientStop::color | - | ||||||||||||||||||||||||
143 | - | |||||||||||||||||||||||||
144 | The position and color properties describe the color used at a given | - | ||||||||||||||||||||||||
145 | position in a gradient, as represented by a gradient stop. | - | ||||||||||||||||||||||||
146 | - | |||||||||||||||||||||||||
147 | The default position is 0.0; the default color is black. | - | ||||||||||||||||||||||||
148 | - | |||||||||||||||||||||||||
149 | \sa Gradient | - | ||||||||||||||||||||||||
150 | */ | - | ||||||||||||||||||||||||
151 | QQuickGradientStop::QQuickGradientStop(QObject *parent) | - | ||||||||||||||||||||||||
152 | : QObject(parent) | - | ||||||||||||||||||||||||
153 | { | - | ||||||||||||||||||||||||
154 | } executed 1310 times by 7 tests: end of block Executed by:
| 1310 | ||||||||||||||||||||||||
155 | - | |||||||||||||||||||||||||
156 | qreal QQuickGradientStop::position() const | - | ||||||||||||||||||||||||
157 | { | - | ||||||||||||||||||||||||
158 | return m_position; executed 2258 times by 7 tests: return m_position; Executed by:
| 2258 | ||||||||||||||||||||||||
159 | } | - | ||||||||||||||||||||||||
160 | - | |||||||||||||||||||||||||
161 | void QQuickGradientStop::setPosition(qreal position) | - | ||||||||||||||||||||||||
162 | { | - | ||||||||||||||||||||||||
163 | m_position = position; updateGradient(); | - | ||||||||||||||||||||||||
164 | } executed 1312 times by 7 tests: end of block Executed by:
| 1312 | ||||||||||||||||||||||||
165 | - | |||||||||||||||||||||||||
166 | QColor QQuickGradientStop::color() const | - | ||||||||||||||||||||||||
167 | { | - | ||||||||||||||||||||||||
168 | return m_color; executed 1396 times by 7 tests: return m_color; Executed by:
| 1396 | ||||||||||||||||||||||||
169 | } | - | ||||||||||||||||||||||||
170 | - | |||||||||||||||||||||||||
171 | void QQuickGradientStop::setColor(const QColor &color) | - | ||||||||||||||||||||||||
172 | { | - | ||||||||||||||||||||||||
173 | m_color = color; updateGradient(); | - | ||||||||||||||||||||||||
174 | } executed 1630 times by 7 tests: end of block Executed by:
| 1630 | ||||||||||||||||||||||||
175 | - | |||||||||||||||||||||||||
176 | void QQuickGradientStop::updateGradient() | - | ||||||||||||||||||||||||
177 | { | - | ||||||||||||||||||||||||
178 | if (QQuickGradient *grad = qobject_cast<QQuickGradient*>(parent()))
| 0-2942 | ||||||||||||||||||||||||
179 | grad->doUpdate(); executed 2942 times by 7 tests: grad->doUpdate(); Executed by:
| 2942 | ||||||||||||||||||||||||
180 | } executed 2942 times by 7 tests: end of block Executed by:
| 2942 | ||||||||||||||||||||||||
181 | - | |||||||||||||||||||||||||
182 | /*! | - | ||||||||||||||||||||||||
183 | \qmltype Gradient | - | ||||||||||||||||||||||||
184 | \instantiates QQuickGradient | - | ||||||||||||||||||||||||
185 | \inqmlmodule QtQuick | - | ||||||||||||||||||||||||
186 | \ingroup qtquick-visual-utility | - | ||||||||||||||||||||||||
187 | \brief Defines a gradient fill. | - | ||||||||||||||||||||||||
188 | - | |||||||||||||||||||||||||
189 | A gradient is defined by two or more colors, which will be blended seamlessly. | - | ||||||||||||||||||||||||
190 | - | |||||||||||||||||||||||||
191 | The colors are specified as a set of GradientStop child items, each of | - | ||||||||||||||||||||||||
192 | which defines a position on the gradient from 0.0 to 1.0 and a color. | - | ||||||||||||||||||||||||
193 | The position of each GradientStop is defined by setting its | - | ||||||||||||||||||||||||
194 | \l{GradientStop::}{position} property; its color is defined using its | - | ||||||||||||||||||||||||
195 | \l{GradientStop::}{color} property. | - | ||||||||||||||||||||||||
196 | - | |||||||||||||||||||||||||
197 | A gradient without any gradient stops is rendered as a solid white fill. | - | ||||||||||||||||||||||||
198 | - | |||||||||||||||||||||||||
199 | Note that this item is not a visual representation of a gradient. To display a | - | ||||||||||||||||||||||||
200 | gradient, use a visual item (like \l Rectangle) which supports the use | - | ||||||||||||||||||||||||
201 | of gradients. | - | ||||||||||||||||||||||||
202 | - | |||||||||||||||||||||||||
203 | \section1 Example Usage | - | ||||||||||||||||||||||||
204 | - | |||||||||||||||||||||||||
205 | \div {class="float-right"} | - | ||||||||||||||||||||||||
206 | \inlineimage qml-gradient.png | - | ||||||||||||||||||||||||
207 | \enddiv | - | ||||||||||||||||||||||||
208 | - | |||||||||||||||||||||||||
209 | The following example declares a \l Rectangle item with a gradient starting | - | ||||||||||||||||||||||||
210 | with red, blending to yellow at one third of the height of the rectangle, | - | ||||||||||||||||||||||||
211 | and ending with green: | - | ||||||||||||||||||||||||
212 | - | |||||||||||||||||||||||||
213 | \snippet qml/gradient.qml code | - | ||||||||||||||||||||||||
214 | - | |||||||||||||||||||||||||
215 | \clearfloat | - | ||||||||||||||||||||||||
216 | \section1 Performance and Limitations | - | ||||||||||||||||||||||||
217 | - | |||||||||||||||||||||||||
218 | Calculating gradients can be computationally expensive compared to the use | - | ||||||||||||||||||||||||
219 | of solid color fills or images. Consider using gradients for static items | - | ||||||||||||||||||||||||
220 | in a user interface. | - | ||||||||||||||||||||||||
221 | - | |||||||||||||||||||||||||
222 | Since Qt 5.12, vertical and horizontal linear gradients can be applied to items. | - | ||||||||||||||||||||||||
223 | If you need to apply angled gradients, a combination of rotation and clipping | - | ||||||||||||||||||||||||
224 | can be applied to the relevant items. Alternatively, consider using | - | ||||||||||||||||||||||||
225 | QtQuick.Shapes::LinearGradient or QtGraphicalEffects::LinearGradient. These | - | ||||||||||||||||||||||||
226 | approaches can all introduce additional performance requirements for your application. | - | ||||||||||||||||||||||||
227 | - | |||||||||||||||||||||||||
228 | The use of animations involving gradient stops may not give the desired | - | ||||||||||||||||||||||||
229 | result. An alternative way to animate gradients is to use pre-generated | - | ||||||||||||||||||||||||
230 | images or SVG drawings containing gradients. | - | ||||||||||||||||||||||||
231 | - | |||||||||||||||||||||||||
232 | \sa GradientStop | - | ||||||||||||||||||||||||
233 | */ | - | ||||||||||||||||||||||||
234 | - | |||||||||||||||||||||||||
235 | /*! | - | ||||||||||||||||||||||||
236 | \qmlproperty list<GradientStop> QtQuick::Gradient::stops | - | ||||||||||||||||||||||||
237 | \default | - | ||||||||||||||||||||||||
238 | - | |||||||||||||||||||||||||
239 | This property holds the gradient stops describing the gradient. | - | ||||||||||||||||||||||||
240 | - | |||||||||||||||||||||||||
241 | By default, this property contains an empty list. | - | ||||||||||||||||||||||||
242 | - | |||||||||||||||||||||||||
243 | To set the gradient stops, define them as children of the Gradient. | - | ||||||||||||||||||||||||
244 | */ | - | ||||||||||||||||||||||||
245 | QQuickGradient::QQuickGradient(QObject *parent) | - | ||||||||||||||||||||||||
246 | : QObject(parent) | - | ||||||||||||||||||||||||
247 | { | - | ||||||||||||||||||||||||
248 | } executed 604 times by 8 tests: end of block Executed by:
| 604 | ||||||||||||||||||||||||
249 | - | |||||||||||||||||||||||||
250 | QQuickGradient::~QQuickGradient() | - | ||||||||||||||||||||||||
251 | { | - | ||||||||||||||||||||||||
252 | } | - | ||||||||||||||||||||||||
253 | - | |||||||||||||||||||||||||
254 | QQmlListProperty<QQuickGradientStop> QQuickGradient::stops() | - | ||||||||||||||||||||||||
255 | { | - | ||||||||||||||||||||||||
256 | return QQmlListProperty<QQuickGradientStop>(this, m_stops); executed 588 times by 7 tests: return QQmlListProperty<QQuickGradientStop>(this, m_stops); Executed by:
| 588 | ||||||||||||||||||||||||
257 | } | - | ||||||||||||||||||||||||
258 | - | |||||||||||||||||||||||||
259 | /*! | - | ||||||||||||||||||||||||
260 | \qmlproperty enumeration QtQuick::Gradient::orientation | - | ||||||||||||||||||||||||
261 | \since 5.12 | - | ||||||||||||||||||||||||
262 | - | |||||||||||||||||||||||||
263 | Set this property to define the direction of the gradient. | - | ||||||||||||||||||||||||
264 | \list | - | ||||||||||||||||||||||||
265 | \li Gradient.Vertical - a vertical gradient | - | ||||||||||||||||||||||||
266 | \li Gradient.Horizontal - a horizontal gradient | - | ||||||||||||||||||||||||
267 | \endlist | - | ||||||||||||||||||||||||
268 | - | |||||||||||||||||||||||||
269 | The default is Gradient.Vertical. | - | ||||||||||||||||||||||||
270 | */ | - | ||||||||||||||||||||||||
271 | void QQuickGradient::setOrientation(Orientation orientation) | - | ||||||||||||||||||||||||
272 | { | - | ||||||||||||||||||||||||
273 | if (m_orientation == orientation)
| 0 | ||||||||||||||||||||||||
274 | return; never executed: return; | 0 | ||||||||||||||||||||||||
275 | - | |||||||||||||||||||||||||
276 | m_orientation = orientation; | - | ||||||||||||||||||||||||
277 | emit orientationChanged(); | - | ||||||||||||||||||||||||
278 | emit updated(); | - | ||||||||||||||||||||||||
279 | } never executed: end of block | 0 | ||||||||||||||||||||||||
280 | - | |||||||||||||||||||||||||
281 | QGradientStops QQuickGradient::gradientStops() const | - | ||||||||||||||||||||||||
282 | { | - | ||||||||||||||||||||||||
283 | QGradientStops stops; | - | ||||||||||||||||||||||||
284 | for (int i = 0; i < m_stops.size(); ++i){
| 562-1252 | ||||||||||||||||||||||||
285 | int j = 0; | - | ||||||||||||||||||||||||
286 | while (j < stops.size() && stops.at(j).first < m_stops[i]->position())
| 2-1250 | ||||||||||||||||||||||||
287 | j++; executed 1000 times by 7 tests: j++; Executed by:
| 1000 | ||||||||||||||||||||||||
288 | stops.insert(j, QGradientStop(m_stops.at(i)->position(), m_stops.at(i)->color())); | - | ||||||||||||||||||||||||
289 | } executed 1252 times by 7 tests: end of block Executed by:
| 1252 | ||||||||||||||||||||||||
290 | return stops; executed 562 times by 7 tests: return stops; Executed by:
| 562 | ||||||||||||||||||||||||
291 | } | - | ||||||||||||||||||||||||
292 | - | |||||||||||||||||||||||||
293 | void QQuickGradient::doUpdate() | - | ||||||||||||||||||||||||
294 | { | - | ||||||||||||||||||||||||
295 | emit updated(); | - | ||||||||||||||||||||||||
296 | } executed 2942 times by 7 tests: end of block Executed by:
| 2942 | ||||||||||||||||||||||||
297 | - | |||||||||||||||||||||||||
298 | int QQuickRectanglePrivate::doUpdateSlotIdx = -1; | - | ||||||||||||||||||||||||
299 | - | |||||||||||||||||||||||||
300 | /*! | - | ||||||||||||||||||||||||
301 | \qmltype Rectangle | - | ||||||||||||||||||||||||
302 | \instantiates QQuickRectangle | - | ||||||||||||||||||||||||
303 | \inqmlmodule QtQuick | - | ||||||||||||||||||||||||
304 | \inherits Item | - | ||||||||||||||||||||||||
305 | \ingroup qtquick-visual | - | ||||||||||||||||||||||||
306 | \brief Paints a filled rectangle with an optional border. | - | ||||||||||||||||||||||||
307 | - | |||||||||||||||||||||||||
308 | Rectangle items are used to fill areas with solid color or gradients, and/or | - | ||||||||||||||||||||||||
309 | to provide a rectangular border. | - | ||||||||||||||||||||||||
310 | - | |||||||||||||||||||||||||
311 | \section1 Appearance | - | ||||||||||||||||||||||||
312 | - | |||||||||||||||||||||||||
313 | Each Rectangle item is painted using either a solid fill color, specified using | - | ||||||||||||||||||||||||
314 | the \l color property, or a gradient, defined using a Gradient type and set | - | ||||||||||||||||||||||||
315 | using the \l gradient property. If both a color and a gradient are specified, | - | ||||||||||||||||||||||||
316 | the gradient is used. | - | ||||||||||||||||||||||||
317 | - | |||||||||||||||||||||||||
318 | You can add an optional border to a rectangle with its own color and thickness | - | ||||||||||||||||||||||||
319 | by setting the \l border.color and \l border.width properties. Set the color | - | ||||||||||||||||||||||||
320 | to "transparent" to paint a border without a fill color. | - | ||||||||||||||||||||||||
321 | - | |||||||||||||||||||||||||
322 | You can also create rounded rectangles using the \l radius property. Since this | - | ||||||||||||||||||||||||
323 | introduces curved edges to the corners of a rectangle, it may be appropriate to | - | ||||||||||||||||||||||||
324 | set the \l Item::antialiasing property to improve its appearance. | - | ||||||||||||||||||||||||
325 | - | |||||||||||||||||||||||||
326 | \section1 Example Usage | - | ||||||||||||||||||||||||
327 | - | |||||||||||||||||||||||||
328 | \div {class="float-right"} | - | ||||||||||||||||||||||||
329 | \inlineimage declarative-rect.png | - | ||||||||||||||||||||||||
330 | \enddiv | - | ||||||||||||||||||||||||
331 | - | |||||||||||||||||||||||||
332 | The following example shows the effects of some of the common properties on a | - | ||||||||||||||||||||||||
333 | Rectangle item, which in this case is used to create a square: | - | ||||||||||||||||||||||||
334 | - | |||||||||||||||||||||||||
335 | \snippet qml/rectangle/rectangle.qml document | - | ||||||||||||||||||||||||
336 | - | |||||||||||||||||||||||||
337 | \clearfloat | - | ||||||||||||||||||||||||
338 | \section1 Performance | - | ||||||||||||||||||||||||
339 | - | |||||||||||||||||||||||||
340 | Using the \l Item::antialiasing property improves the appearance of a rounded rectangle at | - | ||||||||||||||||||||||||
341 | the cost of rendering performance. You should consider unsetting this property | - | ||||||||||||||||||||||||
342 | for rectangles in motion, and only set it when they are stationary. | - | ||||||||||||||||||||||||
343 | - | |||||||||||||||||||||||||
344 | \sa Image | - | ||||||||||||||||||||||||
345 | */ | - | ||||||||||||||||||||||||
346 | - | |||||||||||||||||||||||||
347 | QQuickRectangle::QQuickRectangle(QQuickItem *parent) | - | ||||||||||||||||||||||||
348 | : QQuickItem(*(new QQuickRectanglePrivate), parent) | - | ||||||||||||||||||||||||
349 | { | - | ||||||||||||||||||||||||
350 | setFlag(ItemHasContents); | - | ||||||||||||||||||||||||
351 | #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) | - | ||||||||||||||||||||||||
352 | setAcceptTouchEvents(false); | - | ||||||||||||||||||||||||
353 | #endif | - | ||||||||||||||||||||||||
354 | } executed 94806 times by 86 tests: end of block Executed by:
| 94806 | ||||||||||||||||||||||||
355 | - | |||||||||||||||||||||||||
356 | void QQuickRectangle::doUpdate() | - | ||||||||||||||||||||||||
357 | { | - | ||||||||||||||||||||||||
358 | update(); | - | ||||||||||||||||||||||||
359 | } executed 754 times by 3 tests: end of block Executed by:
| 754 | ||||||||||||||||||||||||
360 | - | |||||||||||||||||||||||||
361 | /*! | - | ||||||||||||||||||||||||
362 | \qmlproperty bool QtQuick::Rectangle::antialiasing | - | ||||||||||||||||||||||||
363 | - | |||||||||||||||||||||||||
364 | Used to decide if the Rectangle should use antialiasing or not. | - | ||||||||||||||||||||||||
365 | \l {Antialiasing} provides information on the performance implications | - | ||||||||||||||||||||||||
366 | of this property. | - | ||||||||||||||||||||||||
367 | - | |||||||||||||||||||||||||
368 | The default is true for Rectangles with a radius, and false otherwise. | - | ||||||||||||||||||||||||
369 | */ | - | ||||||||||||||||||||||||
370 | - | |||||||||||||||||||||||||
371 | /*! | - | ||||||||||||||||||||||||
372 | \qmlpropertygroup QtQuick::Rectangle::border | - | ||||||||||||||||||||||||
373 | \qmlproperty int QtQuick::Rectangle::border.width | - | ||||||||||||||||||||||||
374 | \qmlproperty color QtQuick::Rectangle::border.color | - | ||||||||||||||||||||||||
375 | - | |||||||||||||||||||||||||
376 | The width and color used to draw the border of the rectangle. | - | ||||||||||||||||||||||||
377 | - | |||||||||||||||||||||||||
378 | A width of 1 creates a thin line. For no line, use a width of 0 or a transparent color. | - | ||||||||||||||||||||||||
379 | - | |||||||||||||||||||||||||
380 | \note The width of the rectangle's border does not affect the geometry of the | - | ||||||||||||||||||||||||
381 | rectangle itself or its position relative to other items if anchors are used. | - | ||||||||||||||||||||||||
382 | - | |||||||||||||||||||||||||
383 | The border is rendered within the rectangle's boundaries. | - | ||||||||||||||||||||||||
384 | */ | - | ||||||||||||||||||||||||
385 | QQuickPen *QQuickRectangle::border() | - | ||||||||||||||||||||||||
386 | { | - | ||||||||||||||||||||||||
387 | Q_D(QQuickRectangle); | - | ||||||||||||||||||||||||
388 | if (!d->pen) {
| 100-53997 | ||||||||||||||||||||||||
389 | d->pen = new QQuickPen; | - | ||||||||||||||||||||||||
390 | QQml_setParent_noEvent(d->pen, this); | - | ||||||||||||||||||||||||
391 | } executed 53997 times by 22 tests: end of block Executed by:
| 53997 | ||||||||||||||||||||||||
392 | return d->pen; executed 54097 times by 22 tests: return d->pen; Executed by:
| 54097 | ||||||||||||||||||||||||
393 | } | - | ||||||||||||||||||||||||
394 | - | |||||||||||||||||||||||||
395 | /*! | - | ||||||||||||||||||||||||
396 | \qmlproperty Gradient QtQuick::Rectangle::gradient | - | ||||||||||||||||||||||||
397 | - | |||||||||||||||||||||||||
398 | The gradient to use to fill the rectangle. | - | ||||||||||||||||||||||||
399 | - | |||||||||||||||||||||||||
400 | This property allows for the construction of simple vertical or horizontal gradients. | - | ||||||||||||||||||||||||
401 | Other gradients may be formed by adding rotation to the rectangle. | - | ||||||||||||||||||||||||
402 | - | |||||||||||||||||||||||||
403 | \div {class="float-left"} | - | ||||||||||||||||||||||||
404 | \inlineimage declarative-rect_gradient.png | - | ||||||||||||||||||||||||
405 | \enddiv | - | ||||||||||||||||||||||||
406 | - | |||||||||||||||||||||||||
407 | \snippet qml/rectangle/rectangle-gradient.qml rectangles | - | ||||||||||||||||||||||||
408 | \clearfloat | - | ||||||||||||||||||||||||
409 | - | |||||||||||||||||||||||||
410 | If both a gradient and a color are specified, the gradient will be used. | - | ||||||||||||||||||||||||
411 | - | |||||||||||||||||||||||||
412 | \sa Gradient, color | - | ||||||||||||||||||||||||
413 | */ | - | ||||||||||||||||||||||||
414 | QQuickGradient *QQuickRectangle::gradient() const | - | ||||||||||||||||||||||||
415 | { | - | ||||||||||||||||||||||||
416 | Q_D(const QQuickRectangle); | - | ||||||||||||||||||||||||
417 | return d->gradient; executed 42 times by 3 tests: return d->gradient; Executed by:
| 42 | ||||||||||||||||||||||||
418 | } | - | ||||||||||||||||||||||||
419 | - | |||||||||||||||||||||||||
420 | void QQuickRectangle::setGradient(QQuickGradient *gradient) | - | ||||||||||||||||||||||||
421 | { | - | ||||||||||||||||||||||||
422 | Q_D(QQuickRectangle); | - | ||||||||||||||||||||||||
423 | if (d->gradient == gradient)
| 0-578 | ||||||||||||||||||||||||
424 | return; never executed: return; | 0 | ||||||||||||||||||||||||
425 | static int updatedSignalIdx = -1; | - | ||||||||||||||||||||||||
426 | if (updatedSignalIdx < 0)
| 14-564 | ||||||||||||||||||||||||
427 | updatedSignalIdx = QMetaMethod::fromSignal(&QQuickGradient::updated).methodIndex(); executed 14 times by 7 tests: updatedSignalIdx = QMetaMethod::fromSignal(&QQuickGradient::updated).methodIndex(); Executed by:
| 14 | ||||||||||||||||||||||||
428 | if (d->doUpdateSlotIdx < 0)
| 14-564 | ||||||||||||||||||||||||
429 | d->doUpdateSlotIdx = QQuickRectangle::staticMetaObject.indexOfSlot("doUpdate()"); executed 14 times by 7 tests: d->doUpdateSlotIdx = QQuickRectangle::staticMetaObject.indexOfSlot("doUpdate()"); Executed by:
| 14 | ||||||||||||||||||||||||
430 | if (d->gradient)
| 2-576 | ||||||||||||||||||||||||
431 | QMetaObject::disconnect(d->gradient, updatedSignalIdx, this, d->doUpdateSlotIdx); executed 2 times by 1 test: QMetaObject::disconnect(d->gradient, updatedSignalIdx, this, d->doUpdateSlotIdx); Executed by:
| 2 | ||||||||||||||||||||||||
432 | d->gradient = gradient; | - | ||||||||||||||||||||||||
433 | if (d->gradient)
| 2-576 | ||||||||||||||||||||||||
434 | QMetaObject::connect(d->gradient, updatedSignalIdx, this, d->doUpdateSlotIdx); executed 576 times by 7 tests: QMetaObject::connect(d->gradient, updatedSignalIdx, this, d->doUpdateSlotIdx); Executed by:
| 576 | ||||||||||||||||||||||||
435 | update(); | - | ||||||||||||||||||||||||
436 | } executed 578 times by 7 tests: end of block Executed by:
| 578 | ||||||||||||||||||||||||
437 | - | |||||||||||||||||||||||||
438 | void QQuickRectangle::resetGradient() | - | ||||||||||||||||||||||||
439 | { | - | ||||||||||||||||||||||||
440 | setGradient(nullptr); | - | ||||||||||||||||||||||||
441 | } executed 2 times by 1 test: end of block Executed by:
| 2 | ||||||||||||||||||||||||
442 | - | |||||||||||||||||||||||||
443 | /*! | - | ||||||||||||||||||||||||
444 | \qmlproperty real QtQuick::Rectangle::radius | - | ||||||||||||||||||||||||
445 | This property holds the corner radius used to draw a rounded rectangle. | - | ||||||||||||||||||||||||
446 | - | |||||||||||||||||||||||||
447 | If radius is non-zero, the rectangle will be painted as a rounded rectangle, otherwise it will be | - | ||||||||||||||||||||||||
448 | painted as a normal rectangle. The same radius is used by all 4 corners; there is currently | - | ||||||||||||||||||||||||
449 | no way to specify different radii for different corners. | - | ||||||||||||||||||||||||
450 | */ | - | ||||||||||||||||||||||||
451 | qreal QQuickRectangle::radius() const | - | ||||||||||||||||||||||||
452 | { | - | ||||||||||||||||||||||||
453 | Q_D(const QQuickRectangle); | - | ||||||||||||||||||||||||
454 | return d->radius; executed 4384 times by 5 tests: return d->radius; Executed by:
| 4384 | ||||||||||||||||||||||||
455 | } | - | ||||||||||||||||||||||||
456 | - | |||||||||||||||||||||||||
457 | void QQuickRectangle::setRadius(qreal radius) | - | ||||||||||||||||||||||||
458 | { | - | ||||||||||||||||||||||||
459 | Q_D(QQuickRectangle); | - | ||||||||||||||||||||||||
460 | if (d->radius == radius)
| 388-2830 | ||||||||||||||||||||||||
461 | return; executed 388 times by 4 tests: return; Executed by:
| 388 | ||||||||||||||||||||||||
462 | - | |||||||||||||||||||||||||
463 | d->radius = radius; | - | ||||||||||||||||||||||||
464 | d->setImplicitAntialiasing(radius != 0.0); | - | ||||||||||||||||||||||||
465 | - | |||||||||||||||||||||||||
466 | update(); | - | ||||||||||||||||||||||||
467 | emit radiusChanged(); | - | ||||||||||||||||||||||||
468 | } executed 2830 times by 13 tests: end of block Executed by:
| 2830 | ||||||||||||||||||||||||
469 | - | |||||||||||||||||||||||||
470 | /*! | - | ||||||||||||||||||||||||
471 | \qmlproperty color QtQuick::Rectangle::color | - | ||||||||||||||||||||||||
472 | This property holds the color used to fill the rectangle. | - | ||||||||||||||||||||||||
473 | - | |||||||||||||||||||||||||
474 | The default color is white. | - | ||||||||||||||||||||||||
475 | - | |||||||||||||||||||||||||
476 | \div {class="float-right"} | - | ||||||||||||||||||||||||
477 | \inlineimage rect-color.png | - | ||||||||||||||||||||||||
478 | \enddiv | - | ||||||||||||||||||||||||
479 | - | |||||||||||||||||||||||||
480 | The following example shows rectangles with colors specified | - | ||||||||||||||||||||||||
481 | using hexadecimal and named color notation: | - | ||||||||||||||||||||||||
482 | - | |||||||||||||||||||||||||
483 | \snippet qml/rectangle/rectangle-colors.qml rectangles | - | ||||||||||||||||||||||||
484 | - | |||||||||||||||||||||||||
485 | \clearfloat | - | ||||||||||||||||||||||||
486 | If both a gradient and a color are specified, the gradient will be used. | - | ||||||||||||||||||||||||
487 | - | |||||||||||||||||||||||||
488 | \sa gradient | - | ||||||||||||||||||||||||
489 | */ | - | ||||||||||||||||||||||||
490 | QColor QQuickRectangle::color() const | - | ||||||||||||||||||||||||
491 | { | - | ||||||||||||||||||||||||
492 | Q_D(const QQuickRectangle); | - | ||||||||||||||||||||||||
493 | return d->color; executed 764 times by 15 tests: return d->color; Executed by:
| 764 | ||||||||||||||||||||||||
494 | } | - | ||||||||||||||||||||||||
495 | - | |||||||||||||||||||||||||
496 | void QQuickRectangle::setColor(const QColor &c) | - | ||||||||||||||||||||||||
497 | { | - | ||||||||||||||||||||||||
498 | Q_D(QQuickRectangle); | - | ||||||||||||||||||||||||
499 | if (d->color == c)
| 36295-55888 | ||||||||||||||||||||||||
500 | return; executed 55888 times by 27 tests: return; Executed by:
| 55888 | ||||||||||||||||||||||||
501 | - | |||||||||||||||||||||||||
502 | d->color = c; | - | ||||||||||||||||||||||||
503 | update(); | - | ||||||||||||||||||||||||
504 | emit colorChanged(); | - | ||||||||||||||||||||||||
505 | } executed 36295 times by 77 tests: end of block Executed by:
| 36295 | ||||||||||||||||||||||||
506 | - | |||||||||||||||||||||||||
507 | QSGNode *QQuickRectangle::updatePaintNode(QSGNode *oldNode, UpdatePaintNodeData *data) | - | ||||||||||||||||||||||||
508 | { | - | ||||||||||||||||||||||||
509 | Q_UNUSED(data); | - | ||||||||||||||||||||||||
510 | Q_D(QQuickRectangle); | - | ||||||||||||||||||||||||
511 | - | |||||||||||||||||||||||||
512 | if (width() <= 0 || height() <= 0
| 100-78799 | ||||||||||||||||||||||||
513 | || (d->color.alpha() == 0 && (!d->pen || d->pen->width() == 0 || d->pen->color().alpha() == 0))) {
| 0-76414 | ||||||||||||||||||||||||
514 | delete oldNode; | - | ||||||||||||||||||||||||
515 | return nullptr; executed 2660 times by 10 tests: return nullptr; Executed by:
| 2660 | ||||||||||||||||||||||||
516 | } | - | ||||||||||||||||||||||||
517 | - | |||||||||||||||||||||||||
518 | QSGInternalRectangleNode *rectangle = static_cast<QSGInternalRectangleNode *>(oldNode); | - | ||||||||||||||||||||||||
519 | if (!rectangle) rectangle = d->sceneGraphContext()->createInternalRectangleNode(); executed 71757 times by 66 tests: rectangle = d->sceneGraphContext()->createInternalRectangleNode(); Executed by:
| 5222-71757 | ||||||||||||||||||||||||
520 | - | |||||||||||||||||||||||||
521 | rectangle->setRect(QRectF(0, 0, width(), height())); | - | ||||||||||||||||||||||||
522 | rectangle->setColor(d->color); | - | ||||||||||||||||||||||||
523 | - | |||||||||||||||||||||||||
524 | if (d->pen && d->pen->isValid()) {
| 254-44519 | ||||||||||||||||||||||||
525 | rectangle->setPenColor(d->pen->color()); | - | ||||||||||||||||||||||||
526 | rectangle->setPenWidth(d->pen->width()); | - | ||||||||||||||||||||||||
527 | rectangle->setAligned(d->pen->pixelAligned()); | - | ||||||||||||||||||||||||
528 | } else { executed 44265 times by 15 tests: end of block Executed by:
| 44265 | ||||||||||||||||||||||||
529 | rectangle->setPenWidth(0); | - | ||||||||||||||||||||||||
530 | } executed 32714 times by 65 tests: end of block Executed by:
| 32714 | ||||||||||||||||||||||||
531 | - | |||||||||||||||||||||||||
532 | rectangle->setRadius(d->radius); | - | ||||||||||||||||||||||||
533 | rectangle->setAntialiasing(antialiasing()); | - | ||||||||||||||||||||||||
534 | - | |||||||||||||||||||||||||
535 | QGradientStops stops; | - | ||||||||||||||||||||||||
536 | bool vertical = true; | - | ||||||||||||||||||||||||
537 | if (d->gradient) {
| 533-76446 | ||||||||||||||||||||||||
538 | stops = d->gradient->gradientStops(); | - | ||||||||||||||||||||||||
539 | vertical = d->gradient->orientation() == QQuickGradient::Vertical; | - | ||||||||||||||||||||||||
540 | } executed 533 times by 5 tests: end of block Executed by:
| 533 | ||||||||||||||||||||||||
541 | rectangle->setGradientStops(stops); | - | ||||||||||||||||||||||||
542 | rectangle->setGradientVertical(vertical); | - | ||||||||||||||||||||||||
543 | - | |||||||||||||||||||||||||
544 | rectangle->update(); | - | ||||||||||||||||||||||||
545 | - | |||||||||||||||||||||||||
546 | return rectangle; executed 76979 times by 66 tests: return rectangle; Executed by:
| 76979 | ||||||||||||||||||||||||
547 | } | - | ||||||||||||||||||||||||
548 | - | |||||||||||||||||||||||||
549 | QT_END_NAMESPACE | - | ||||||||||||||||||||||||
550 | - | |||||||||||||||||||||||||
551 | #include "moc_qquickrectangle_p.cpp" | - | ||||||||||||||||||||||||
Source code | Switch to Preprocessed file |