Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/gui/kernel/qcursor.cpp |
Source code | Switch to Preprocessed file |
Line | Source | Count | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
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 QtGui module of the Qt Toolkit. | - | ||||||||||||||||||
7 | ** | - | ||||||||||||||||||
8 | ** $QT_BEGIN_LICENSE:LGPL21$ | - | ||||||||||||||||||
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 http://www.qt.io/terms-conditions. For further | - | ||||||||||||||||||
15 | ** information use the contact form at http://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 2.1 or version 3 as published by the Free | - | ||||||||||||||||||
20 | ** Software Foundation and appearing in the file LICENSE.LGPLv21 and | - | ||||||||||||||||||
21 | ** LICENSE.LGPLv3 included in the packaging of this file. Please review the | - | ||||||||||||||||||
22 | ** following information to ensure the GNU Lesser General Public License | - | ||||||||||||||||||
23 | ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and | - | ||||||||||||||||||
24 | ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | - | ||||||||||||||||||
25 | ** | - | ||||||||||||||||||
26 | ** As a special exception, The Qt Company gives you certain additional | - | ||||||||||||||||||
27 | ** rights. These rights are described in The Qt Company LGPL Exception | - | ||||||||||||||||||
28 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. | - | ||||||||||||||||||
29 | ** | - | ||||||||||||||||||
30 | ** $QT_END_LICENSE$ | - | ||||||||||||||||||
31 | ** | - | ||||||||||||||||||
32 | ****************************************************************************/ | - | ||||||||||||||||||
33 | - | |||||||||||||||||||
34 | #include "qcursor.h" | - | ||||||||||||||||||
35 | - | |||||||||||||||||||
36 | #include <qcoreapplication.h> | - | ||||||||||||||||||
37 | #include <qbitmap.h> | - | ||||||||||||||||||
38 | #include <qimage.h> | - | ||||||||||||||||||
39 | #include <qdatastream.h> | - | ||||||||||||||||||
40 | #include <qvariant.h> | - | ||||||||||||||||||
41 | #include <private/qcursor_p.h> | - | ||||||||||||||||||
42 | #include <qdebug.h> | - | ||||||||||||||||||
43 | - | |||||||||||||||||||
44 | #include <qpa/qplatformcursor.h> | - | ||||||||||||||||||
45 | #include <private/qguiapplication_p.h> | - | ||||||||||||||||||
46 | #include <private/qhighdpiscaling_p.h> | - | ||||||||||||||||||
47 | - | |||||||||||||||||||
48 | QT_BEGIN_NAMESPACE | - | ||||||||||||||||||
49 | - | |||||||||||||||||||
50 | /*! | - | ||||||||||||||||||
51 | \class QCursor | - | ||||||||||||||||||
52 | - | |||||||||||||||||||
53 | \brief The QCursor class provides a mouse cursor with an arbitrary | - | ||||||||||||||||||
54 | shape. | - | ||||||||||||||||||
55 | - | |||||||||||||||||||
56 | \inmodule QtGui | - | ||||||||||||||||||
57 | \ingroup appearance | - | ||||||||||||||||||
58 | \ingroup shared | - | ||||||||||||||||||
59 | - | |||||||||||||||||||
60 | - | |||||||||||||||||||
61 | This class is mainly used to create mouse cursors that are | - | ||||||||||||||||||
62 | associated with particular widgets and to get and set the position | - | ||||||||||||||||||
63 | of the mouse cursor. | - | ||||||||||||||||||
64 | - | |||||||||||||||||||
65 | Qt has a number of standard cursor shapes, but you can also make | - | ||||||||||||||||||
66 | custom cursor shapes based on a QBitmap, a mask and a hotspot. | - | ||||||||||||||||||
67 | - | |||||||||||||||||||
68 | To associate a cursor with a widget, use QWidget::setCursor(). To | - | ||||||||||||||||||
69 | associate a cursor with all widgets (normally for a short period | - | ||||||||||||||||||
70 | of time), use QGuiApplication::setOverrideCursor(). | - | ||||||||||||||||||
71 | - | |||||||||||||||||||
72 | To set a cursor shape use QCursor::setShape() or use the QCursor | - | ||||||||||||||||||
73 | constructor which takes the shape as argument, or you can use one | - | ||||||||||||||||||
74 | of the predefined cursors defined in the \l Qt::CursorShape enum. | - | ||||||||||||||||||
75 | - | |||||||||||||||||||
76 | If you want to create a cursor with your own bitmap, either use | - | ||||||||||||||||||
77 | the QCursor constructor which takes a bitmap and a mask or the | - | ||||||||||||||||||
78 | constructor which takes a pixmap as arguments. | - | ||||||||||||||||||
79 | - | |||||||||||||||||||
80 | To set or get the position of the mouse cursor use the static | - | ||||||||||||||||||
81 | methods QCursor::pos() and QCursor::setPos(). | - | ||||||||||||||||||
82 | - | |||||||||||||||||||
83 | \b{Note:} It is possible to create a QCursor before | - | ||||||||||||||||||
84 | QGuiApplication, but it is not useful except as a place-holder for a | - | ||||||||||||||||||
85 | real QCursor created after QGuiApplication. Attempting to use a | - | ||||||||||||||||||
86 | QCursor that was created before QGuiApplication will result in a | - | ||||||||||||||||||
87 | crash. | - | ||||||||||||||||||
88 | - | |||||||||||||||||||
89 | \section1 A Note for X11 Users | - | ||||||||||||||||||
90 | - | |||||||||||||||||||
91 | On X11, Qt supports the \l{Xcursor}{Xcursor} | - | ||||||||||||||||||
92 | library, which allows for full color icon themes. The table below | - | ||||||||||||||||||
93 | shows the cursor name used for each Qt::CursorShape value. If a | - | ||||||||||||||||||
94 | cursor cannot be found using the name shown below, a standard X11 | - | ||||||||||||||||||
95 | cursor will be used instead. Note: X11 does not provide | - | ||||||||||||||||||
96 | appropriate cursors for all possible Qt::CursorShape values. It | - | ||||||||||||||||||
97 | is possible that some cursors will be taken from the Xcursor | - | ||||||||||||||||||
98 | theme, while others will use an internal bitmap cursor. | - | ||||||||||||||||||
99 | - | |||||||||||||||||||
100 | \table | - | ||||||||||||||||||
101 | \header \li Shape \li Qt::CursorShape Value \li Cursor Name | - | ||||||||||||||||||
102 | \li Shape \li Qt::CursorShape Value \li Cursor Name | - | ||||||||||||||||||
103 | \row \li \inlineimage cursor-arrow.png | - | ||||||||||||||||||
104 | \li Qt::ArrowCursor \li \c left_ptr | - | ||||||||||||||||||
105 | \li \inlineimage cursor-sizev.png | - | ||||||||||||||||||
106 | \li Qt::SizeVerCursor \li \c size_ver | - | ||||||||||||||||||
107 | \row \li \inlineimage cursor-uparrow.png | - | ||||||||||||||||||
108 | \li Qt::UpArrowCursor \li \c up_arrow | - | ||||||||||||||||||
109 | \li \inlineimage cursor-sizeh.png | - | ||||||||||||||||||
110 | \li Qt::SizeHorCursor \li \c size_hor | - | ||||||||||||||||||
111 | \row \li \inlineimage cursor-cross.png | - | ||||||||||||||||||
112 | \li Qt::CrossCursor \li \c cross | - | ||||||||||||||||||
113 | \li \inlineimage cursor-sizeb.png | - | ||||||||||||||||||
114 | \li Qt::SizeBDiagCursor \li \c size_bdiag | - | ||||||||||||||||||
115 | \row \li \inlineimage cursor-ibeam.png | - | ||||||||||||||||||
116 | \li Qt::IBeamCursor \li \c ibeam | - | ||||||||||||||||||
117 | \li \inlineimage cursor-sizef.png | - | ||||||||||||||||||
118 | \li Qt::SizeFDiagCursor \li \c size_fdiag | - | ||||||||||||||||||
119 | \row \li \inlineimage cursor-wait.png | - | ||||||||||||||||||
120 | \li Qt::WaitCursor \li \c wait | - | ||||||||||||||||||
121 | \li \inlineimage cursor-sizeall.png | - | ||||||||||||||||||
122 | \li Qt::SizeAllCursor \li \c size_all | - | ||||||||||||||||||
123 | \row \li \inlineimage cursor-busy.png | - | ||||||||||||||||||
124 | \li Qt::BusyCursor \li \c left_ptr_watch | - | ||||||||||||||||||
125 | \li \inlineimage cursor-vsplit.png | - | ||||||||||||||||||
126 | \li Qt::SplitVCursor \li \c split_v | - | ||||||||||||||||||
127 | \row \li \inlineimage cursor-forbidden.png | - | ||||||||||||||||||
128 | \li Qt::ForbiddenCursor \li \c forbidden | - | ||||||||||||||||||
129 | \li \inlineimage cursor-hsplit.png | - | ||||||||||||||||||
130 | \li Qt::SplitHCursor \li \c split_h | - | ||||||||||||||||||
131 | \row \li \inlineimage cursor-hand.png | - | ||||||||||||||||||
132 | \li Qt::PointingHandCursor \li \c pointing_hand | - | ||||||||||||||||||
133 | \li \inlineimage cursor-openhand.png | - | ||||||||||||||||||
134 | \li Qt::OpenHandCursor \li \c openhand | - | ||||||||||||||||||
135 | \row \li \inlineimage cursor-whatsthis.png | - | ||||||||||||||||||
136 | \li Qt::WhatsThisCursor \li \c whats_this | - | ||||||||||||||||||
137 | \li \inlineimage cursor-closedhand.png | - | ||||||||||||||||||
138 | \li Qt::ClosedHandCursor \li \c closedhand | - | ||||||||||||||||||
139 | \row \li | - | ||||||||||||||||||
140 | \li Qt::DragMoveCursor \li \c dnd-move or \c move | - | ||||||||||||||||||
141 | \li | - | ||||||||||||||||||
142 | \li Qt::DragCopyCursor \li \c dnd-copy or \c copy | - | ||||||||||||||||||
143 | \row \li | - | ||||||||||||||||||
144 | \li Qt::DragLinkCursor \li \c dnd-link or \c link | - | ||||||||||||||||||
145 | \endtable | - | ||||||||||||||||||
146 | - | |||||||||||||||||||
147 | \sa QWidget, {fowler}{GUI Design Handbook: Cursors} | - | ||||||||||||||||||
148 | */ | - | ||||||||||||||||||
149 | - | |||||||||||||||||||
150 | /*! | - | ||||||||||||||||||
151 | \fn QCursor::QCursor(QCursor &&other) | - | ||||||||||||||||||
152 | \since 5.5 | - | ||||||||||||||||||
153 | - | |||||||||||||||||||
154 | Move-constructs a cursor from \a other. After being moved from, | - | ||||||||||||||||||
155 | the only valid operations on \a other are destruction and | - | ||||||||||||||||||
156 | (move and copy) assignment. The effects of calling any other | - | ||||||||||||||||||
157 | member function on a moved-from instance are undefined. | - | ||||||||||||||||||
158 | */ | - | ||||||||||||||||||
159 | - | |||||||||||||||||||
160 | /*! | - | ||||||||||||||||||
161 | \fn QCursor &QCursor::operator=(QCursor &&other) | - | ||||||||||||||||||
162 | - | |||||||||||||||||||
163 | Move-assigns \a other to this QCursor instance. | - | ||||||||||||||||||
164 | - | |||||||||||||||||||
165 | \since 5.2 | - | ||||||||||||||||||
166 | */ | - | ||||||||||||||||||
167 | - | |||||||||||||||||||
168 | /*! | - | ||||||||||||||||||
169 | \fn QPoint QCursor::pos(const QScreen *screen) | - | ||||||||||||||||||
170 | - | |||||||||||||||||||
171 | Returns the position of the cursor (hot spot) of the \a screen | - | ||||||||||||||||||
172 | in global screen coordinates. | - | ||||||||||||||||||
173 | - | |||||||||||||||||||
174 | You can call QWidget::mapFromGlobal() to translate it to widget | - | ||||||||||||||||||
175 | coordinates. | - | ||||||||||||||||||
176 | - | |||||||||||||||||||
177 | \sa setPos(), QWidget::mapFromGlobal(), QWidget::mapToGlobal() | - | ||||||||||||||||||
178 | */ | - | ||||||||||||||||||
179 | QPoint QCursor::pos(const QScreen *screen) | - | ||||||||||||||||||
180 | { | - | ||||||||||||||||||
181 | if (screen) {
| 0 | ||||||||||||||||||
182 | if (const QPlatformCursor *cursor = screen->handle()->cursor()) {
| 0 | ||||||||||||||||||
183 | const QPlatformScreen *ps = screen->handle(); | - | ||||||||||||||||||
184 | QPoint nativePos = cursor->pos(); | - | ||||||||||||||||||
185 | ps = ps->screenForPosition(nativePos); | - | ||||||||||||||||||
186 | return QHighDpi::fromNativePixels(nativePos, ps->screen()); never executed: return QHighDpi::fromNativePixels(nativePos, ps->screen()); | 0 | ||||||||||||||||||
187 | } | - | ||||||||||||||||||
188 | } never executed: end of block | 0 | ||||||||||||||||||
189 | return QGuiApplicationPrivate::lastCursorPosition.toPoint(); never executed: return QGuiApplicationPrivate::lastCursorPosition.toPoint(); | 0 | ||||||||||||||||||
190 | } | - | ||||||||||||||||||
191 | - | |||||||||||||||||||
192 | /*! | - | ||||||||||||||||||
193 | \fn QPoint QCursor::pos() | - | ||||||||||||||||||
194 | - | |||||||||||||||||||
195 | Returns the position of the cursor (hot spot) of | - | ||||||||||||||||||
196 | the primary screen in global screen coordinates. | - | ||||||||||||||||||
197 | - | |||||||||||||||||||
198 | You can call QWidget::mapFromGlobal() to translate it to widget | - | ||||||||||||||||||
199 | coordinates. | - | ||||||||||||||||||
200 | - | |||||||||||||||||||
201 | \note The position is queried from the windowing system. If mouse events are generated | - | ||||||||||||||||||
202 | via other means (e.g., via QWindowSystemInterface in a unit test), those fake mouse | - | ||||||||||||||||||
203 | moves will not be reflected in the returned value. | - | ||||||||||||||||||
204 | - | |||||||||||||||||||
205 | \note On platforms where there is no windowing system or cursors are not available, the returned | - | ||||||||||||||||||
206 | position is based on the mouse move events generated via QWindowSystemInterface. | - | ||||||||||||||||||
207 | - | |||||||||||||||||||
208 | \sa setPos(), QWidget::mapFromGlobal(), QWidget::mapToGlobal(), QGuiApplication::primaryScreen() | - | ||||||||||||||||||
209 | */ | - | ||||||||||||||||||
210 | QPoint QCursor::pos() | - | ||||||||||||||||||
211 | { | - | ||||||||||||||||||
212 | return QCursor::pos(QGuiApplication::primaryScreen()); never executed: return QCursor::pos(QGuiApplication::primaryScreen()); | 0 | ||||||||||||||||||
213 | } | - | ||||||||||||||||||
214 | - | |||||||||||||||||||
215 | /*! | - | ||||||||||||||||||
216 | \fn void QCursor::setPos(QScreen *screen, int x, int y) | - | ||||||||||||||||||
217 | - | |||||||||||||||||||
218 | Moves the cursor (hot spot) of the \a screen to the global | - | ||||||||||||||||||
219 | screen position (\a x, \a y). | - | ||||||||||||||||||
220 | - | |||||||||||||||||||
221 | You can call QWidget::mapToGlobal() to translate widget | - | ||||||||||||||||||
222 | coordinates to global screen coordinates. | - | ||||||||||||||||||
223 | - | |||||||||||||||||||
224 | \note Calling this function results in changing the cursor position through the windowing | - | ||||||||||||||||||
225 | system. The windowing system will typically respond by sending mouse events to the application's | - | ||||||||||||||||||
226 | window. This means that the usage of this function should be avoided in unit tests and | - | ||||||||||||||||||
227 | everywhere where fake mouse events are being injected via QWindowSystemInterface because the | - | ||||||||||||||||||
228 | windowing system's mouse state (with regards to buttons for example) may not match the state in | - | ||||||||||||||||||
229 | the application-generated events. | - | ||||||||||||||||||
230 | - | |||||||||||||||||||
231 | \note On platforms where there is no windowing system or cursors are not available, this | - | ||||||||||||||||||
232 | function may do nothing. | - | ||||||||||||||||||
233 | - | |||||||||||||||||||
234 | \sa pos(), QWidget::mapFromGlobal(), QWidget::mapToGlobal() | - | ||||||||||||||||||
235 | */ | - | ||||||||||||||||||
236 | void QCursor::setPos(QScreen *screen, int x, int y) | - | ||||||||||||||||||
237 | { | - | ||||||||||||||||||
238 | if (screen) {
| 0 | ||||||||||||||||||
239 | if (QPlatformCursor *cursor = screen->handle()->cursor()) {
| 0 | ||||||||||||||||||
240 | const QPoint devicePos = QHighDpi::toNativePixels(QPoint(x, y), screen); | - | ||||||||||||||||||
241 | // Need to check, since some X servers generate null mouse move | - | ||||||||||||||||||
242 | // events, causing looping in applications which call setPos() on | - | ||||||||||||||||||
243 | // every mouse move event. | - | ||||||||||||||||||
244 | if (devicePos != cursor->pos())
| 0 | ||||||||||||||||||
245 | cursor->setPos(devicePos); never executed: cursor->setPos(devicePos); | 0 | ||||||||||||||||||
246 | } never executed: end of block | 0 | ||||||||||||||||||
247 | } never executed: end of block | 0 | ||||||||||||||||||
248 | } never executed: end of block | 0 | ||||||||||||||||||
249 | - | |||||||||||||||||||
250 | /*! | - | ||||||||||||||||||
251 | \fn void QCursor::setPos(int x, int y) | - | ||||||||||||||||||
252 | - | |||||||||||||||||||
253 | Moves the cursor (hot spot) of the primary screen | - | ||||||||||||||||||
254 | to the global screen position (\a x, \a y). | - | ||||||||||||||||||
255 | - | |||||||||||||||||||
256 | You can call QWidget::mapToGlobal() to translate widget | - | ||||||||||||||||||
257 | coordinates to global screen coordinates. | - | ||||||||||||||||||
258 | - | |||||||||||||||||||
259 | \sa pos(), QWidget::mapFromGlobal(), QWidget::mapToGlobal(), QGuiApplication::primaryScreen() | - | ||||||||||||||||||
260 | */ | - | ||||||||||||||||||
261 | void QCursor::setPos(int x, int y) | - | ||||||||||||||||||
262 | { | - | ||||||||||||||||||
263 | QCursor::setPos(QGuiApplication::primaryScreen(), x, y); | - | ||||||||||||||||||
264 | } never executed: end of block | 0 | ||||||||||||||||||
265 | - | |||||||||||||||||||
266 | #ifndef QT_NO_CURSOR | - | ||||||||||||||||||
267 | - | |||||||||||||||||||
268 | /*! | - | ||||||||||||||||||
269 | \fn void QCursor::setPos (const QPoint &p) | - | ||||||||||||||||||
270 | - | |||||||||||||||||||
271 | \overload | - | ||||||||||||||||||
272 | - | |||||||||||||||||||
273 | Moves the cursor (hot spot) to the global screen position at point | - | ||||||||||||||||||
274 | \a p. | - | ||||||||||||||||||
275 | */ | - | ||||||||||||||||||
276 | - | |||||||||||||||||||
277 | /*! | - | ||||||||||||||||||
278 | \fn void QCursor::setPos (QScreen *screen,const QPoint &p) | - | ||||||||||||||||||
279 | - | |||||||||||||||||||
280 | \overload | - | ||||||||||||||||||
281 | - | |||||||||||||||||||
282 | Moves the cursor (hot spot) to the global screen position of the | - | ||||||||||||||||||
283 | \a screen at point \a p. | - | ||||||||||||||||||
284 | */ | - | ||||||||||||||||||
285 | - | |||||||||||||||||||
286 | /***************************************************************************** | - | ||||||||||||||||||
287 | QCursor stream functions | - | ||||||||||||||||||
288 | *****************************************************************************/ | - | ||||||||||||||||||
289 | - | |||||||||||||||||||
290 | #ifndef QT_NO_DATASTREAM | - | ||||||||||||||||||
291 | - | |||||||||||||||||||
292 | - | |||||||||||||||||||
293 | /*! | - | ||||||||||||||||||
294 | \fn QDataStream &operator<<(QDataStream &stream, const QCursor &cursor) | - | ||||||||||||||||||
295 | \relates QCursor | - | ||||||||||||||||||
296 | - | |||||||||||||||||||
297 | Writes the \a cursor to the \a stream. | - | ||||||||||||||||||
298 | - | |||||||||||||||||||
299 | \sa {Serializing Qt Data Types} | - | ||||||||||||||||||
300 | */ | - | ||||||||||||||||||
301 | - | |||||||||||||||||||
302 | QDataStream &operator<<(QDataStream &s, const QCursor &c) | - | ||||||||||||||||||
303 | { | - | ||||||||||||||||||
304 | s << (qint16)c.shape(); // write shape id to stream | - | ||||||||||||||||||
305 | if (c.shape() == Qt::BitmapCursor) { // bitmap cursor
| 0 | ||||||||||||||||||
306 | bool isPixmap = false; | - | ||||||||||||||||||
307 | if (s.version() >= 7) {
| 0 | ||||||||||||||||||
308 | isPixmap = !c.pixmap().isNull(); | - | ||||||||||||||||||
309 | s << isPixmap; | - | ||||||||||||||||||
310 | } never executed: end of block | 0 | ||||||||||||||||||
311 | if (isPixmap)
| 0 | ||||||||||||||||||
312 | s << c.pixmap(); never executed: s << c.pixmap(); | 0 | ||||||||||||||||||
313 | else | - | ||||||||||||||||||
314 | s << *c.bitmap() << *c.mask(); never executed: s << *c.bitmap() << *c.mask(); | 0 | ||||||||||||||||||
315 | s << c.hotSpot(); | - | ||||||||||||||||||
316 | } never executed: end of block | 0 | ||||||||||||||||||
317 | return s; never executed: return s; | 0 | ||||||||||||||||||
318 | } | - | ||||||||||||||||||
319 | - | |||||||||||||||||||
320 | /*! | - | ||||||||||||||||||
321 | \fn QDataStream &operator>>(QDataStream &stream, QCursor &cursor) | - | ||||||||||||||||||
322 | \relates QCursor | - | ||||||||||||||||||
323 | - | |||||||||||||||||||
324 | Reads the \a cursor from the \a stream. | - | ||||||||||||||||||
325 | - | |||||||||||||||||||
326 | \sa {Serializing Qt Data Types} | - | ||||||||||||||||||
327 | */ | - | ||||||||||||||||||
328 | - | |||||||||||||||||||
329 | QDataStream &operator>>(QDataStream &s, QCursor &c) | - | ||||||||||||||||||
330 | { | - | ||||||||||||||||||
331 | qint16 shape; | - | ||||||||||||||||||
332 | s >> shape; // read shape id from stream | - | ||||||||||||||||||
333 | if (shape == Qt::BitmapCursor) { // read bitmap cursor
| 0 | ||||||||||||||||||
334 | bool isPixmap = false; | - | ||||||||||||||||||
335 | if (s.version() >= 7)
| 0 | ||||||||||||||||||
336 | s >> isPixmap; never executed: s >> isPixmap; | 0 | ||||||||||||||||||
337 | if (isPixmap) {
| 0 | ||||||||||||||||||
338 | QPixmap pm; | - | ||||||||||||||||||
339 | QPoint hot; | - | ||||||||||||||||||
340 | s >> pm >> hot; | - | ||||||||||||||||||
341 | c = QCursor(pm, hot.x(), hot.y()); | - | ||||||||||||||||||
342 | } else { never executed: end of block | 0 | ||||||||||||||||||
343 | QBitmap bm, bmm; | - | ||||||||||||||||||
344 | QPoint hot; | - | ||||||||||||||||||
345 | s >> bm >> bmm >> hot; | - | ||||||||||||||||||
346 | c = QCursor(bm, bmm, hot.x(), hot.y()); | - | ||||||||||||||||||
347 | } never executed: end of block | 0 | ||||||||||||||||||
348 | } else { | - | ||||||||||||||||||
349 | c.setShape((Qt::CursorShape)shape); // create cursor with shape | - | ||||||||||||||||||
350 | } never executed: end of block | 0 | ||||||||||||||||||
351 | return s; never executed: return s; | 0 | ||||||||||||||||||
352 | } | - | ||||||||||||||||||
353 | #endif // QT_NO_DATASTREAM | - | ||||||||||||||||||
354 | - | |||||||||||||||||||
355 | - | |||||||||||||||||||
356 | /*! | - | ||||||||||||||||||
357 | Constructs a custom pixmap cursor. | - | ||||||||||||||||||
358 | - | |||||||||||||||||||
359 | \a pixmap is the image. It is usual to give it a mask (set using | - | ||||||||||||||||||
360 | QPixmap::setMask()). \a hotX and \a hotY define the cursor's hot | - | ||||||||||||||||||
361 | spot. | - | ||||||||||||||||||
362 | - | |||||||||||||||||||
363 | If \a hotX is negative, it is set to the \c{pixmap().width()/2}. | - | ||||||||||||||||||
364 | If \a hotY is negative, it is set to the \c{pixmap().height()/2}. | - | ||||||||||||||||||
365 | - | |||||||||||||||||||
366 | Valid cursor sizes depend on the display hardware (or the | - | ||||||||||||||||||
367 | underlying window system). We recommend using 32 x 32 cursors, | - | ||||||||||||||||||
368 | because this size is supported on all platforms. Some platforms | - | ||||||||||||||||||
369 | also support 16 x 16, 48 x 48, and 64 x 64 cursors. | - | ||||||||||||||||||
370 | - | |||||||||||||||||||
371 | \note On Windows CE, the cursor size is fixed. If the pixmap | - | ||||||||||||||||||
372 | is bigger than the system size, it will be scaled. | - | ||||||||||||||||||
373 | - | |||||||||||||||||||
374 | \sa QPixmap::QPixmap(), QPixmap::setMask() | - | ||||||||||||||||||
375 | */ | - | ||||||||||||||||||
376 | - | |||||||||||||||||||
377 | QCursor::QCursor(const QPixmap &pixmap, int hotX, int hotY) | - | ||||||||||||||||||
378 | : d(0) | - | ||||||||||||||||||
379 | { | - | ||||||||||||||||||
380 | QImage img = pixmap.toImage().convertToFormat(QImage::Format_Indexed8, Qt::ThresholdDither|Qt::AvoidDither); | - | ||||||||||||||||||
381 | QBitmap bm = QBitmap::fromImage(img, Qt::ThresholdDither|Qt::AvoidDither); | - | ||||||||||||||||||
382 | QBitmap bmm = pixmap.mask(); | - | ||||||||||||||||||
383 | if (!bmm.isNull()) {
| 0 | ||||||||||||||||||
384 | QBitmap nullBm; | - | ||||||||||||||||||
385 | bm.setMask(nullBm); | - | ||||||||||||||||||
386 | } never executed: end of block | 0 | ||||||||||||||||||
387 | else if (!pixmap.mask().isNull()) {
| 0 | ||||||||||||||||||
388 | QImage mimg = pixmap.mask().toImage().convertToFormat(QImage::Format_Indexed8, Qt::ThresholdDither|Qt::AvoidDither); | - | ||||||||||||||||||
389 | bmm = QBitmap::fromImage(mimg, Qt::ThresholdDither|Qt::AvoidDither); | - | ||||||||||||||||||
390 | } never executed: end of block | 0 | ||||||||||||||||||
391 | else { | - | ||||||||||||||||||
392 | bmm = QBitmap(bm.size()); | - | ||||||||||||||||||
393 | bmm.fill(Qt::color1); | - | ||||||||||||||||||
394 | } never executed: end of block | 0 | ||||||||||||||||||
395 | - | |||||||||||||||||||
396 | d = QCursorData::setBitmap(bm, bmm, hotX, hotY, pixmap.devicePixelRatio()); | - | ||||||||||||||||||
397 | d->pixmap = pixmap; | - | ||||||||||||||||||
398 | } never executed: end of block | 0 | ||||||||||||||||||
399 | - | |||||||||||||||||||
400 | - | |||||||||||||||||||
401 | - | |||||||||||||||||||
402 | /*! | - | ||||||||||||||||||
403 | Constructs a custom bitmap cursor. | - | ||||||||||||||||||
404 | - | |||||||||||||||||||
405 | \a bitmap and | - | ||||||||||||||||||
406 | \a mask make up the bitmap. | - | ||||||||||||||||||
407 | \a hotX and | - | ||||||||||||||||||
408 | \a hotY define the cursor's hot spot. | - | ||||||||||||||||||
409 | - | |||||||||||||||||||
410 | If \a hotX is negative, it is set to the \c{bitmap().width()/2}. | - | ||||||||||||||||||
411 | If \a hotY is negative, it is set to the \c{bitmap().height()/2}. | - | ||||||||||||||||||
412 | - | |||||||||||||||||||
413 | The cursor \a bitmap (B) and \a mask (M) bits are combined like this: | - | ||||||||||||||||||
414 | \list | - | ||||||||||||||||||
415 | \li B=1 and M=1 gives black. | - | ||||||||||||||||||
416 | \li B=0 and M=1 gives white. | - | ||||||||||||||||||
417 | \li B=0 and M=0 gives transparent. | - | ||||||||||||||||||
418 | \li B=1 and M=0 gives an XOR'd result under Windows, undefined | - | ||||||||||||||||||
419 | results on all other platforms. | - | ||||||||||||||||||
420 | \endlist | - | ||||||||||||||||||
421 | - | |||||||||||||||||||
422 | Use the global Qt color Qt::color0 to draw 0-pixels and Qt::color1 to | - | ||||||||||||||||||
423 | draw 1-pixels in the bitmaps. | - | ||||||||||||||||||
424 | - | |||||||||||||||||||
425 | Valid cursor sizes depend on the display hardware (or the | - | ||||||||||||||||||
426 | underlying window system). We recommend using 32 x 32 cursors, | - | ||||||||||||||||||
427 | because this size is supported on all platforms. Some platforms | - | ||||||||||||||||||
428 | also support 16 x 16, 48 x 48, and 64 x 64 cursors. | - | ||||||||||||||||||
429 | - | |||||||||||||||||||
430 | \note On Windows CE, the cursor size is fixed. If the pixmap | - | ||||||||||||||||||
431 | is bigger than the system size, it will be scaled. | - | ||||||||||||||||||
432 | - | |||||||||||||||||||
433 | \sa QBitmap::QBitmap(), QBitmap::setMask() | - | ||||||||||||||||||
434 | */ | - | ||||||||||||||||||
435 | - | |||||||||||||||||||
436 | QCursor::QCursor(const QBitmap &bitmap, const QBitmap &mask, int hotX, int hotY) | - | ||||||||||||||||||
437 | : d(0) | - | ||||||||||||||||||
438 | { | - | ||||||||||||||||||
439 | d = QCursorData::setBitmap(bitmap, mask, hotX, hotY, 1.0); | - | ||||||||||||||||||
440 | } never executed: end of block | 0 | ||||||||||||||||||
441 | - | |||||||||||||||||||
442 | /*! | - | ||||||||||||||||||
443 | Constructs a cursor with the default arrow shape. | - | ||||||||||||||||||
444 | */ | - | ||||||||||||||||||
445 | QCursor::QCursor() | - | ||||||||||||||||||
446 | { | - | ||||||||||||||||||
447 | if (!QCursorData::initialized) {
| 0 | ||||||||||||||||||
448 | if (QCoreApplication::startingUp()) {
| 0 | ||||||||||||||||||
449 | d = 0; | - | ||||||||||||||||||
450 | return; never executed: return; | 0 | ||||||||||||||||||
451 | } | - | ||||||||||||||||||
452 | QCursorData::initialize(); | - | ||||||||||||||||||
453 | } never executed: end of block | 0 | ||||||||||||||||||
454 | QCursorData *c = qt_cursorTable[0]; | - | ||||||||||||||||||
455 | c->ref.ref(); | - | ||||||||||||||||||
456 | d = c; | - | ||||||||||||||||||
457 | } never executed: end of block | 0 | ||||||||||||||||||
458 | - | |||||||||||||||||||
459 | /*! | - | ||||||||||||||||||
460 | Constructs a cursor with the specified \a shape. | - | ||||||||||||||||||
461 | - | |||||||||||||||||||
462 | See \l Qt::CursorShape for a list of shapes. | - | ||||||||||||||||||
463 | - | |||||||||||||||||||
464 | \sa setShape() | - | ||||||||||||||||||
465 | */ | - | ||||||||||||||||||
466 | QCursor::QCursor(Qt::CursorShape shape) | - | ||||||||||||||||||
467 | : d(0) | - | ||||||||||||||||||
468 | { | - | ||||||||||||||||||
469 | if (!QCursorData::initialized)
| 0 | ||||||||||||||||||
470 | QCursorData::initialize(); never executed: QCursorData::initialize(); | 0 | ||||||||||||||||||
471 | setShape(shape); | - | ||||||||||||||||||
472 | } never executed: end of block | 0 | ||||||||||||||||||
473 | - | |||||||||||||||||||
474 | - | |||||||||||||||||||
475 | /*! | - | ||||||||||||||||||
476 | Returns the cursor shape identifier. The return value is one of | - | ||||||||||||||||||
477 | the \l Qt::CursorShape enum values (cast to an int). | - | ||||||||||||||||||
478 | - | |||||||||||||||||||
479 | \sa setShape() | - | ||||||||||||||||||
480 | */ | - | ||||||||||||||||||
481 | Qt::CursorShape QCursor::shape() const | - | ||||||||||||||||||
482 | { | - | ||||||||||||||||||
483 | if (!QCursorData::initialized)
| 0 | ||||||||||||||||||
484 | QCursorData::initialize(); never executed: QCursorData::initialize(); | 0 | ||||||||||||||||||
485 | return d->cshape; never executed: return d->cshape; | 0 | ||||||||||||||||||
486 | } | - | ||||||||||||||||||
487 | - | |||||||||||||||||||
488 | /*! | - | ||||||||||||||||||
489 | Sets the cursor to the shape identified by \a shape. | - | ||||||||||||||||||
490 | - | |||||||||||||||||||
491 | See \l Qt::CursorShape for the list of cursor shapes. | - | ||||||||||||||||||
492 | - | |||||||||||||||||||
493 | \sa shape() | - | ||||||||||||||||||
494 | */ | - | ||||||||||||||||||
495 | void QCursor::setShape(Qt::CursorShape shape) | - | ||||||||||||||||||
496 | { | - | ||||||||||||||||||
497 | if (!QCursorData::initialized)
| 0 | ||||||||||||||||||
498 | QCursorData::initialize(); never executed: QCursorData::initialize(); | 0 | ||||||||||||||||||
499 | QCursorData *c = uint(shape) <= Qt::LastCursor ? qt_cursorTable[shape] : 0;
| 0 | ||||||||||||||||||
500 | if (!c)
| 0 | ||||||||||||||||||
501 | c = qt_cursorTable[0]; never executed: c = qt_cursorTable[0]; | 0 | ||||||||||||||||||
502 | c->ref.ref(); | - | ||||||||||||||||||
503 | if (!d) {
| 0 | ||||||||||||||||||
504 | d = c; | - | ||||||||||||||||||
505 | } else { never executed: end of block | 0 | ||||||||||||||||||
506 | if (!d->ref.deref())
| 0 | ||||||||||||||||||
507 | delete d; never executed: delete d; | 0 | ||||||||||||||||||
508 | d = c; | - | ||||||||||||||||||
509 | } never executed: end of block | 0 | ||||||||||||||||||
510 | } | - | ||||||||||||||||||
511 | - | |||||||||||||||||||
512 | /*! | - | ||||||||||||||||||
513 | Returns the cursor bitmap, or 0 if it is one of the standard | - | ||||||||||||||||||
514 | cursors. | - | ||||||||||||||||||
515 | */ | - | ||||||||||||||||||
516 | const QBitmap *QCursor::bitmap() const | - | ||||||||||||||||||
517 | { | - | ||||||||||||||||||
518 | if (!QCursorData::initialized)
| 0 | ||||||||||||||||||
519 | QCursorData::initialize(); never executed: QCursorData::initialize(); | 0 | ||||||||||||||||||
520 | return d->bm; never executed: return d->bm; | 0 | ||||||||||||||||||
521 | } | - | ||||||||||||||||||
522 | - | |||||||||||||||||||
523 | /*! | - | ||||||||||||||||||
524 | Returns the cursor bitmap mask, or 0 if it is one of the standard | - | ||||||||||||||||||
525 | cursors. | - | ||||||||||||||||||
526 | */ | - | ||||||||||||||||||
527 | - | |||||||||||||||||||
528 | const QBitmap *QCursor::mask() const | - | ||||||||||||||||||
529 | { | - | ||||||||||||||||||
530 | if (!QCursorData::initialized)
| 0 | ||||||||||||||||||
531 | QCursorData::initialize(); never executed: QCursorData::initialize(); | 0 | ||||||||||||||||||
532 | return d->bmm; never executed: return d->bmm; | 0 | ||||||||||||||||||
533 | } | - | ||||||||||||||||||
534 | - | |||||||||||||||||||
535 | /*! | - | ||||||||||||||||||
536 | Returns the cursor pixmap. This is only valid if the cursor is a | - | ||||||||||||||||||
537 | pixmap cursor. | - | ||||||||||||||||||
538 | */ | - | ||||||||||||||||||
539 | - | |||||||||||||||||||
540 | QPixmap QCursor::pixmap() const | - | ||||||||||||||||||
541 | { | - | ||||||||||||||||||
542 | if (!QCursorData::initialized)
| 0 | ||||||||||||||||||
543 | QCursorData::initialize(); never executed: QCursorData::initialize(); | 0 | ||||||||||||||||||
544 | return d->pixmap; never executed: return d->pixmap; | 0 | ||||||||||||||||||
545 | } | - | ||||||||||||||||||
546 | - | |||||||||||||||||||
547 | /*! | - | ||||||||||||||||||
548 | Returns the cursor hot spot, or (0, 0) if it is one of the | - | ||||||||||||||||||
549 | standard cursors. | - | ||||||||||||||||||
550 | */ | - | ||||||||||||||||||
551 | - | |||||||||||||||||||
552 | QPoint QCursor::hotSpot() const | - | ||||||||||||||||||
553 | { | - | ||||||||||||||||||
554 | if (!QCursorData::initialized)
| 0 | ||||||||||||||||||
555 | QCursorData::initialize(); never executed: QCursorData::initialize(); | 0 | ||||||||||||||||||
556 | return QPoint(d->hx, d->hy); never executed: return QPoint(d->hx, d->hy); | 0 | ||||||||||||||||||
557 | } | - | ||||||||||||||||||
558 | - | |||||||||||||||||||
559 | /*! | - | ||||||||||||||||||
560 | Constructs a copy of the cursor \a c. | - | ||||||||||||||||||
561 | */ | - | ||||||||||||||||||
562 | - | |||||||||||||||||||
563 | QCursor::QCursor(const QCursor &c) | - | ||||||||||||||||||
564 | { | - | ||||||||||||||||||
565 | if (!QCursorData::initialized)
| 0 | ||||||||||||||||||
566 | QCursorData::initialize(); never executed: QCursorData::initialize(); | 0 | ||||||||||||||||||
567 | d = c.d; | - | ||||||||||||||||||
568 | d->ref.ref(); | - | ||||||||||||||||||
569 | } never executed: end of block | 0 | ||||||||||||||||||
570 | - | |||||||||||||||||||
571 | /*! | - | ||||||||||||||||||
572 | Destroys the cursor. | - | ||||||||||||||||||
573 | */ | - | ||||||||||||||||||
574 | - | |||||||||||||||||||
575 | QCursor::~QCursor() | - | ||||||||||||||||||
576 | { | - | ||||||||||||||||||
577 | if (d && !d->ref.deref())
| 0 | ||||||||||||||||||
578 | delete d; never executed: delete d; | 0 | ||||||||||||||||||
579 | } never executed: end of block | 0 | ||||||||||||||||||
580 | - | |||||||||||||||||||
581 | - | |||||||||||||||||||
582 | /*! | - | ||||||||||||||||||
583 | Assigns \a c to this cursor and returns a reference to this | - | ||||||||||||||||||
584 | cursor. | - | ||||||||||||||||||
585 | */ | - | ||||||||||||||||||
586 | - | |||||||||||||||||||
587 | QCursor &QCursor::operator=(const QCursor &c) | - | ||||||||||||||||||
588 | { | - | ||||||||||||||||||
589 | if (!QCursorData::initialized)
| 0 | ||||||||||||||||||
590 | QCursorData::initialize(); never executed: QCursorData::initialize(); | 0 | ||||||||||||||||||
591 | if (c.d)
| 0 | ||||||||||||||||||
592 | c.d->ref.ref(); never executed: c.d->ref.ref(); | 0 | ||||||||||||||||||
593 | if (d && !d->ref.deref())
| 0 | ||||||||||||||||||
594 | delete d; never executed: delete d; | 0 | ||||||||||||||||||
595 | d = c.d; | - | ||||||||||||||||||
596 | return *this; never executed: return *this; | 0 | ||||||||||||||||||
597 | } | - | ||||||||||||||||||
598 | - | |||||||||||||||||||
599 | /*! | - | ||||||||||||||||||
600 | Returns the cursor as a QVariant. | - | ||||||||||||||||||
601 | */ | - | ||||||||||||||||||
602 | QCursor::operator QVariant() const | - | ||||||||||||||||||
603 | { | - | ||||||||||||||||||
604 | return QVariant(QVariant::Cursor, this); never executed: return QVariant(QVariant::Cursor, this); | 0 | ||||||||||||||||||
605 | } | - | ||||||||||||||||||
606 | - | |||||||||||||||||||
607 | #ifndef QT_NO_DEBUG_STREAM | - | ||||||||||||||||||
608 | QDebug operator<<(QDebug dbg, const QCursor &c) | - | ||||||||||||||||||
609 | { | - | ||||||||||||||||||
610 | QDebugStateSaver saver(dbg); | - | ||||||||||||||||||
611 | dbg.nospace() << "QCursor(Qt::CursorShape(" << c.shape() << "))"; | - | ||||||||||||||||||
612 | return dbg; never executed: return dbg; | 0 | ||||||||||||||||||
613 | } | - | ||||||||||||||||||
614 | #endif | - | ||||||||||||||||||
615 | - | |||||||||||||||||||
616 | /***************************************************************************** | - | ||||||||||||||||||
617 | Internal QCursorData class | - | ||||||||||||||||||
618 | *****************************************************************************/ | - | ||||||||||||||||||
619 | - | |||||||||||||||||||
620 | QCursorData *qt_cursorTable[Qt::LastCursor + 1]; | - | ||||||||||||||||||
621 | bool QCursorData::initialized = false; | - | ||||||||||||||||||
622 | - | |||||||||||||||||||
623 | QCursorData::QCursorData(Qt::CursorShape s) | - | ||||||||||||||||||
624 | : ref(1), cshape(s), bm(0), bmm(0), hx(0), hy(0) | - | ||||||||||||||||||
625 | { | - | ||||||||||||||||||
626 | } never executed: end of block | 0 | ||||||||||||||||||
627 | - | |||||||||||||||||||
628 | QCursorData::~QCursorData() | - | ||||||||||||||||||
629 | { | - | ||||||||||||||||||
630 | delete bm; | - | ||||||||||||||||||
631 | delete bmm; | - | ||||||||||||||||||
632 | } never executed: end of block | 0 | ||||||||||||||||||
633 | - | |||||||||||||||||||
634 | /*! \internal */ | - | ||||||||||||||||||
635 | void QCursorData::cleanup() | - | ||||||||||||||||||
636 | { | - | ||||||||||||||||||
637 | if(!QCursorData::initialized)
| 0 | ||||||||||||||||||
638 | return; never executed: return; | 0 | ||||||||||||||||||
639 | - | |||||||||||||||||||
640 | for (int shape = 0; shape <= Qt::LastCursor; ++shape) {
| 0 | ||||||||||||||||||
641 | // In case someone has a static QCursor defined with this shape | - | ||||||||||||||||||
642 | if (!qt_cursorTable[shape]->ref.deref())
| 0 | ||||||||||||||||||
643 | delete qt_cursorTable[shape]; never executed: delete qt_cursorTable[shape]; | 0 | ||||||||||||||||||
644 | qt_cursorTable[shape] = 0; | - | ||||||||||||||||||
645 | } never executed: end of block | 0 | ||||||||||||||||||
646 | QCursorData::initialized = false; | - | ||||||||||||||||||
647 | } never executed: end of block | 0 | ||||||||||||||||||
648 | - | |||||||||||||||||||
649 | /*! \internal */ | - | ||||||||||||||||||
650 | void QCursorData::initialize() | - | ||||||||||||||||||
651 | { | - | ||||||||||||||||||
652 | if (QCursorData::initialized)
| 0 | ||||||||||||||||||
653 | return; never executed: return; | 0 | ||||||||||||||||||
654 | for (int shape = 0; shape <= Qt::LastCursor; ++shape)
| 0 | ||||||||||||||||||
655 | qt_cursorTable[shape] = new QCursorData((Qt::CursorShape)shape); never executed: qt_cursorTable[shape] = new QCursorData((Qt::CursorShape)shape); | 0 | ||||||||||||||||||
656 | QCursorData::initialized = true; | - | ||||||||||||||||||
657 | } never executed: end of block | 0 | ||||||||||||||||||
658 | - | |||||||||||||||||||
659 | QCursorData *QCursorData::setBitmap(const QBitmap &bitmap, const QBitmap &mask, int hotX, int hotY, qreal devicePixelRatio) | - | ||||||||||||||||||
660 | { | - | ||||||||||||||||||
661 | if (!QCursorData::initialized)
| 0 | ||||||||||||||||||
662 | QCursorData::initialize(); never executed: QCursorData::initialize(); | 0 | ||||||||||||||||||
663 | if (bitmap.depth() != 1 || mask.depth() != 1 || bitmap.size() != mask.size()) {
| 0 | ||||||||||||||||||
664 | qWarning("QCursor: Cannot create bitmap cursor; invalid bitmap(s)"); | - | ||||||||||||||||||
665 | QCursorData *c = qt_cursorTable[0]; | - | ||||||||||||||||||
666 | c->ref.ref(); | - | ||||||||||||||||||
667 | return c; never executed: return c; | 0 | ||||||||||||||||||
668 | } | - | ||||||||||||||||||
669 | QCursorData *d = new QCursorData; | - | ||||||||||||||||||
670 | d->bm = new QBitmap(bitmap); | - | ||||||||||||||||||
671 | d->bmm = new QBitmap(mask); | - | ||||||||||||||||||
672 | d->cshape = Qt::BitmapCursor; | - | ||||||||||||||||||
673 | d->hx = hotX >= 0 ? hotX : bitmap.width() / 2 / devicePixelRatio;
| 0 | ||||||||||||||||||
674 | d->hy = hotY >= 0 ? hotY : bitmap.height() / 2 / devicePixelRatio;
| 0 | ||||||||||||||||||
675 | - | |||||||||||||||||||
676 | return d; never executed: return d; | 0 | ||||||||||||||||||
677 | } | - | ||||||||||||||||||
678 | - | |||||||||||||||||||
679 | void QCursorData::update() | - | ||||||||||||||||||
680 | { | - | ||||||||||||||||||
681 | } | - | ||||||||||||||||||
682 | - | |||||||||||||||||||
683 | QT_END_NAMESPACE | - | ||||||||||||||||||
684 | #endif // QT_NO_CURSOR | - | ||||||||||||||||||
685 | - | |||||||||||||||||||
Source code | Switch to Preprocessed file |