Qt 4.8
qcursor.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtGui 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 Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/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 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #include "qcursor.h"
43 
44 #ifndef QT_NO_CURSOR
45 
46 #include <qapplication.h>
47 #include <qbitmap.h>
48 #include <qimage.h>
49 #include <qdatastream.h>
50 #include <qvariant.h>
51 #include <private/qcursor_p.h>
52 
54 
236 /*****************************************************************************
237  QCursor stream functions
238  *****************************************************************************/
239 
240 #ifndef QT_NO_DATASTREAM
241 
242 
256 {
257  s << (qint16)c.shape(); // write shape id to stream
258  if (c.shape() == Qt::BitmapCursor) { // bitmap cursor
259  bool isPixmap = false;
260  if (s.version() >= 7) {
261  isPixmap = !c.pixmap().isNull();
262  s << isPixmap;
263  }
264  if (isPixmap)
265  s << c.pixmap();
266  else
267  s << *c.bitmap() << *c.mask();
268  s << c.hotSpot();
269  }
270  return s;
271 }
272 
286 {
287  qint16 shape;
288  s >> shape; // read shape id from stream
289  if (shape == Qt::BitmapCursor) { // read bitmap cursor
290  bool isPixmap = false;
291  if (s.version() >= 7)
292  s >> isPixmap;
293  if (isPixmap) {
294  QPixmap pm;
295  QPoint hot;
296  s >> pm >> hot;
297  c = QCursor(pm, hot.x(), hot.y());
298  } else {
299  QBitmap bm, bmm;
300  QPoint hot;
301  s >> bm >> bmm >> hot;
302  c = QCursor(bm, bmm, hot.x(), hot.y());
303  }
304  } else {
305  c.setShape((Qt::CursorShape)shape); // create cursor with shape
306  }
307  return s;
308 }
309 #endif // QT_NO_DATASTREAM
310 
311 
333 QCursor::QCursor(const QPixmap &pixmap, int hotX, int hotY)
334  : d(0)
335 {
338  QBitmap bmm = pixmap.mask();
339  if (!bmm.isNull()) {
340  QBitmap nullBm;
341  bm.setMask(nullBm);
342  }
343  else if (!pixmap.mask().isNull()) {
346  }
347  else {
348  bmm = QBitmap(bm.size());
349  bmm.fill(Qt::color1);
350  }
351 
352  d = QCursorData::setBitmap(bm, bmm, hotX, hotY);
353  d->pixmap = pixmap;
354 }
355 
356 
357 
392 QCursor::QCursor(const QBitmap &bitmap, const QBitmap &mask, int hotX, int hotY)
393  : d(0)
394 {
395  d = QCursorData::setBitmap(bitmap, mask, hotX, hotY);
396 }
397 
399 bool QCursorData::initialized = false;
400 
403 {
405  return;
406 
407  for (int shape = 0; shape <= Qt::LastCursor; ++shape) {
408  // In case someone has a static QCursor defined with this shape
409  if (!qt_cursorTable[shape]->ref.deref())
410  delete qt_cursorTable[shape];
411  qt_cursorTable[shape] = 0;
412  }
413  QCursorData::initialized = false;
414 }
415 
418 {
420  return;
421 #ifdef Q_WS_MAC
422  // DRSWAT - Not Needed Cocoa or Carbon
423  //InitCursor();
424 #endif
425  for (int shape = 0; shape <= Qt::LastCursor; ++shape)
426  qt_cursorTable[shape] = new QCursorData((Qt::CursorShape)shape);
428 }
429 
434 {
436  if (QApplication::startingUp()) {
437  d = 0;
438  return;
439  }
441  }
442  QCursorData *c = qt_cursorTable[0];
443  c->ref.ref();
444  d = c;
445 }
446 
455  : d(0)
456 {
459  setShape(shape);
460 }
461 
462 
470 {
473  return d->cshape;
474 }
475 
484 {
487  QCursorData *c = uint(shape) <= Qt::LastCursor ? qt_cursorTable[shape] : 0;
488  if (!c)
489  c = qt_cursorTable[0];
490  c->ref.ref();
491  if (!d) {
492  d = c;
493  } else {
494  if (!d->ref.deref())
495  delete d;
496  d = c;
497  }
498 }
499 
504 const QBitmap *QCursor::bitmap() const
505 {
508  return d->bm;
509 }
510 
516 const QBitmap *QCursor::mask() const
517 {
520  return d->bmm;
521 }
522 
529 {
532  return d->pixmap;
533 }
534 
541 {
544  return QPoint(d->hx, d->hy);
545 }
546 
552 {
555  d = c.d;
556  d->ref.ref();
557 }
558 
564 {
565  if (d && !d->ref.deref())
566  delete d;
567 }
568 
569 
576 {
579  if (c.d)
580  c.d->ref.ref();
581  if (d && !d->ref.deref())
582  delete d;
583  d = c.d;
584  return *this;
585 }
586 
590 QCursor::operator QVariant() const
591 {
592  return QVariant(QVariant::Cursor, this);
593 }
595 #endif // QT_NO_CURSOR
596 
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
double d
Definition: qnumeric_p.h:62
QImage toImage() const
Converts the pixmap to a QImage.
Definition: qpixmap.cpp:542
The QCursor class provides a mouse cursor with an arbitrary shape.
Definition: qcursor.h:89
unsigned char c[8]
Definition: qnumeric_p.h:62
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
QSize size() const
Returns the size of the pixmap.
Definition: qpixmap.cpp:661
short hy
Definition: qcursor_p.h:91
QPixmap pixmap
Definition: qcursor_p.h:90
void setMask(const QBitmap &)
Sets a mask bitmap.
Definition: qpixmap.cpp:822
bool ref()
Atomically increments the value of this QAtomicInt.
static bool initialized
Definition: qcursor_p.h:125
static QCursorData * setBitmap(const QBitmap &bitmap, const QBitmap &mask, int hotX, int hotY)
Definition: qcursor_mac.mm:278
The QBitmap class provides monochrome (1-bit depth) pixmaps.
Definition: qbitmap.h:55
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
bool deref()
Atomically decrements the value of this QAtomicInt.
short qint16
Definition: qglobal.h:935
QPixmap pixmap() const
Returns the cursor pixmap.
Definition: qcursor.cpp:528
The QImage class provides a hardware-independent image representation that allows direct access to th...
Definition: qimage.h:87
unsigned int uint
Definition: qglobal.h:996
QCursorData * qt_cursorTable[Qt::LastCursor+1]
Definition: qcursor.cpp:398
QCursor & operator=(const QCursor &cursor)
Assigns c to this cursor and returns a reference to this cursor.
Definition: qcursor.cpp:575
static bool startingUp()
Returns true if an application object has not been created yet; otherwise returns false...
int version() const
Returns the version number of the data serialization format.
Definition: qdatastream.h:212
QPoint hotSpot() const
Returns the cursor hot spot, or (0, 0) if it is one of the standard cursors.
Definition: qcursor.cpp:540
void fill(const QColor &fillColor=Qt::white)
Fills the pixmap with the given color.
Definition: qpixmap.cpp:1080
Qt::CursorShape cshape
Definition: qcursor_p.h:88
QImage convertToFormat(Format f, Qt::ImageConversionFlags flags=Qt::AutoColor) const Q_REQUIRED_RESULT
Returns a copy of the image in the given format.
Definition: qimage.cpp:3966
QBitmap mask() const
Extracts a bitmap mask from the pixmap&#39;s alpha channel.
Definition: qpixmap.cpp:2077
const QBitmap * bitmap() const
Returns the cursor bitmap, or 0 if it is one of the standard cursors.
Definition: qcursor.cpp:504
CursorShape
Definition: qnamespace.h:1262
The QPoint class defines a point in the plane using integer precision.
Definition: qpoint.h:53
QCursorData * d
Definition: qcursor.h:141
short hx
Definition: qcursor_p.h:91
The QPixmap class is an off-screen image representation that can be used as a paint device...
Definition: qpixmap.h:71
QBitmap * bmm
Definition: qcursor_p.h:89
static QBitmap fromImage(const QImage &image, Qt::ImageConversionFlags flags=Qt::AutoColor)
Returns a copy of the given image converted to a bitmap using the specified image conversion flags...
Definition: qbitmap.cpp:281
static void cleanup()
Definition: qcursor.cpp:402
static void initialize()
Definition: qcursor.cpp:417
QAtomicInt ref
Definition: qcursor_p.h:87
QCursor()
Constructs a cursor with the default arrow shape.
Definition: qcursor.cpp:433
The QDataStream class provides serialization of binary data to a QIODevice.
Definition: qdatastream.h:71
bool isNull() const
Returns true if this is a null pixmap; otherwise returns false.
Definition: qpixmap.cpp:615
void setShape(Qt::CursorShape newShape)
Sets the cursor to the shape identified by shape.
Definition: qcursor.cpp:483
~QCursor()
Destroys the cursor.
Definition: qcursor.cpp:563
QBitmap * bm
Definition: qcursor_p.h:89
QDataStream & operator<<(QDataStream &out, const QUrl &url)
Writes url url to the stream out and returns a reference to the stream.
Definition: qurl.cpp:6757
const QBitmap * mask() const
Returns the cursor bitmap mask, or 0 if it is one of the standard cursors.
Definition: qcursor.cpp:516
Qt::CursorShape shape() const
Returns the cursor shape identifier.
Definition: qcursor.cpp:469
QDataStream & operator>>(QDataStream &in, QUrl &url)
Reads a url into url from the stream in and returns a reference to the stream.
Definition: qurl.cpp:6774