Qt 4.8
qclipboard.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 "qclipboard.h"
43 
44 #ifndef QT_NO_CLIPBOARD
45 
46 #include "qapplication.h"
47 #include "qapplication_p.h"
48 #include "qpixmap.h"
49 #include "qclipboard_p.h"
50 #include "qvariant.h"
51 #include "qbuffer.h"
52 #include "qimage.h"
53 #include "qtextcodec.h"
54 
56 
145 #ifndef Q_WS_X11
146 // for X11 there is a separate implementation of a constructor.
165  : QObject(*new QClipboardPrivate, parent)
166 {
167  // nothing
168 }
169 #endif
170 
171 #ifndef Q_WS_WIN32
172 
184 {
185 }
186 #endif
187 
271 /*****************************************************************************
272  QApplication member functions related to QClipboard.
273  *****************************************************************************/
274 
275 // text handling is done directly in qclipboard_qws, for now
276 
307 QString QClipboard::text(QString &subtype, Mode mode) const
308 {
309  const QMimeData *const data = mimeData(mode);
310  if (!data)
311  return QString();
312 
313  const QStringList formats = data->formats();
314  if (subtype.isEmpty()) {
315  if (formats.contains(QLatin1String("text/plain")))
316  subtype = QLatin1String("plain");
317  else {
318  for (int i = 0; i < formats.size(); ++i)
319  if (formats.at(i).startsWith(QLatin1String("text/"))) {
320  subtype = formats.at(i).mid(5);
321  break;
322  }
323  if (subtype.isEmpty())
324  return QString();
325  }
326  } else if (!formats.contains(QLatin1String("text/") + subtype)) {
327  return QString();
328  }
329 
330  const QByteArray rawData = data->data(QLatin1String("text/") + subtype);
331 
332 #ifndef QT_NO_TEXTCODEC
333  QTextCodec* codec = QTextCodec::codecForMib(106); // utf-8 is default
334  if (subtype == QLatin1String("html"))
335  codec = QTextCodec::codecForHtml(rawData, codec);
336  else
337  codec = QTextCodec::codecForUtfText(rawData, codec);
338  return codec->toUnicode(rawData);
339 #else //QT_NO_TEXTCODEC
340  return rawData;
341 #endif //QT_NO_TEXTCODEC
342 }
343 
358 {
359  const QMimeData *data = mimeData(mode);
360  return data ? data->text() : QString();
361 }
362 
376 {
377  QMimeData *data = new QMimeData;
378  data->setText(text);
379  setMimeData(data, mode);
380 }
381 
396 {
397  const QMimeData *data = mimeData(mode);
398  if (!data)
399  return QImage();
400  return qvariant_cast<QImage>(data->imageData());
401 }
402 
418 void QClipboard::setImage(const QImage &image, Mode mode)
419 {
420  QMimeData *data = new QMimeData;
421  data->setImageData(image);
422  setMimeData(data, mode);
423 }
424 
441 {
442  const QMimeData *data = mimeData(mode);
443  return data ? qvariant_cast<QPixmap>(data->imageData()) : QPixmap();
444 }
445 
459 void QClipboard::setPixmap(const QPixmap &pixmap, Mode mode)
460 {
461  QMimeData *data = new QMimeData;
462  data->setImageData(pixmap);
463  setMimeData(data, mode);
464 }
465 
466 
523 #ifdef QT3_SUPPORT
524 
530 QMimeSource *QClipboard::data(Mode mode) const
531 {
532  Q_D(const QClipboard);
533 
534  if (supportsMode(mode) == false)
535  return 0;
536 
537  if (d->compat_data[mode])
538  return d->compat_data[mode];
539 
540  d->wrapper[mode]->data = mimeData(mode);
541  return d->wrapper[mode];
542 }
543 
544 
551 void QClipboard::setData(QMimeSource *source, Mode mode)
552 {
553  Q_D(QClipboard);
554 
555  if (supportsMode(mode) == false)
556  return;
557 
558  d->compat_data[mode] = source;
559  setMimeData(new QMimeSourceWrapper(d, mode), mode);
560 }
561 #endif // QT3_SUPPORT
562 
568 {
569  return supportsMode(Selection);
570 }
571 
577 {
578  return supportsMode(FindBuffer);
579 }
580 
586 {
587  return ownsMode(Clipboard);
588 }
589 
595 {
596  return ownsMode(Selection);
597 }
598 
609 {
610  return ownsMode(FindBuffer);
611 }
612 
641 {
642  switch (mode) {
643  case Clipboard:
644  emit dataChanged();
645  break;
646  case Selection:
647  emit selectionChanged();
648  break;
649  case FindBuffer:
650  emit findBufferChanged();
651  break;
652  default:
653  break;
654  }
655  emit changed(mode);
656 }
657 
658 const char* QMimeDataWrapper::format(int n) const
659 {
660  if (formats.isEmpty()) {
661  QStringList fmts = data->formats();
662  for (int i = 0; i < fmts.size(); ++i)
663  formats.append(fmts.at(i).toLatin1());
664  }
665  if (n < 0 || n >= formats.size())
666  return 0;
667  return formats.at(n).data();
668 }
669 
671 {
672  if (QLatin1String(format) != QLatin1String("application/x-qt-image")){
673  return data->data(QLatin1String(format));
674  } else{
675  QVariant variant = data->imageData();
677  QByteArray ba;
678  QBuffer buffer(&ba);
679  buffer.open(QIODevice::WriteOnly);
680  img.save(&buffer, "PNG");
681  return ba;
682  }
683 }
684 
686 {
687  return source->encodedData(mimetype.toLatin1());
688 }
689 
690 bool QMimeSourceWrapper::hasFormat(const QString &mimetype) const
691 {
692  return source->provides(mimetype.toLatin1());
693 }
694 
696 {
697  QStringList fmts;
698  int i = 0;
699  const char *fmt;
700  while ((fmt = source->format(i))) {
701  fmts.append(QLatin1String(fmt));
702  ++i;
703  }
704  return fmts;
705 }
706 
707 #endif // QT_NO_CLIPBOARD
708 
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
Mode
This enum type is used to control which part of the system clipboard is used by QClipboard::mimeData(...
Definition: qclipboard.h:71
bool ownsClipboard() const
Returns true if this clipboard object owns the clipboard data; otherwise returns false.
Definition: qclipboard.cpp:585
double d
Definition: qnumeric_p.h:62
void emitChanged(Mode mode)
Emits the appropriate changed signal for mode.
Definition: qclipboard.cpp:640
void setImage(const QImage &, Mode mode=Clipboard)
Copies the image into the clipboard.
Definition: qclipboard.cpp:418
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
virtual QStringList formats() const
Returns a list of formats supported by the object.
Definition: qmimedata.cpp:579
QClipboard(QObject *parent)
void setImageData(const QVariant &image)
Sets the data in the object to the given image.
Definition: qmimedata.cpp:459
bool open(OpenMode openMode)
Reimplemented Function
Definition: qbuffer.cpp:338
bool ownsFindBuffer() const
Returns true if this clipboard object owns the find buffer data; otherwise returns false...
Definition: qclipboard.cpp:608
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
QStringList formats() const
Returns a list of formats supported by the object.
Definition: qclipboard.cpp:695
static QTextCodec * codecForHtml(const QByteArray &ba)
Tries to detect the encoding of the provided snippet of HTML in the given byte array, ba, by checking the BOM (Byte Order Mark) and the content-type meta header and returns a QTextCodec instance that is capable of decoding the html to unicode.
bool save(const QString &fileName, const char *format=0, int quality=-1) const
Saves the image to the file with the given fileName, using the given image file format and quality fa...
Definition: qimage.cpp:5346
bool startsWith(const QString &s, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Returns true if the string starts with s; otherwise returns false.
Definition: qstring.cpp:3734
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
QByteArray data(const QString &mimetype) const
Returns the data stored in the object in the format described by the MIME type specified by mimeType...
Definition: qmimedata.cpp:524
The QBuffer class provides a QIODevice interface for a QByteArray.
Definition: qbuffer.h:57
The QString class provides a Unicode character string.
Definition: qstring.h:83
void setText(const QString &, Mode mode=Clipboard)
Copies text into the clipboard as plain text.
Definition: qclipboard.cpp:375
bool hasFormat(const QString &mimetype) const
Returns true if the object can return data for the MIME type specified by mimeType; otherwise returns...
Definition: qclipboard.cpp:690
The QObject class is the base class of all Qt objects.
Definition: qobject.h:111
#define Q_D(Class)
Definition: qglobal.h:2482
The QMimeSource class is an abstraction of objects that provided formatted data of a certain MIME typ...
Definition: qmime.h:53
void append(const T &t)
Inserts value at the end of the list.
Definition: qlist.h:507
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
QString text() const
Returns a plain text (MIME type text/plain) representation of the data.
Definition: qmimedata.cpp:364
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
static bool setData(const QByteArray &data, STGMEDIUM *pmedium)
Definition: qmime_win.cpp:141
QVariant retrieveData(const QString &mimetype, QVariant::Type) const
Returns a variant with the given type containing data for the MIME type specified by mimeType...
Definition: qclipboard.cpp:685
void setPixmap(const QPixmap &, Mode mode=Clipboard)
Copies pixmap into the clipboard.
Definition: qclipboard.cpp:459
const T & at(int i) const
Returns the item at index position i in the list.
Definition: qlist.h:468
#define emit
Definition: qobjectdefs.h:76
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
The QImage class provides a hardware-independent image representation that allows direct access to th...
Definition: qimage.h:87
static const char * data(const QByteArray &arr)
The QLatin1String class provides a thin wrapper around an US-ASCII/Latin-1 encoded string literal...
Definition: qstring.h:654
static QTextCodec * codec(MYSQL *mysql)
Definition: qsql_mysql.cpp:220
Type
This enum type defines the types of variable that a QVariant can contain.
Definition: qvariant.h:95
QBool contains(const QString &str, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Returns true if the list contains the string str; otherwise returns false.
Definition: qstringlist.h:172
static QTextCodec * codecForMib(int mib)
Returns the QTextCodec which matches the MIBenum mib.
virtual bool provides(const char *) const
Returns true if the object can provide the data in format mimeType; otherwise returns false...
Definition: qmime.cpp:90
The QClipboard class provides access to the window system clipboard.
Definition: qclipboard.h:62
QPixmap pixmap(Mode mode=Clipboard) const
Returns the clipboard pixmap, or null if the clipboard does not contain a pixmap. ...
Definition: qclipboard.cpp:440
QByteArray toLatin1() const Q_REQUIRED_RESULT
Returns a Latin-1 representation of the string as a QByteArray.
Definition: qstring.cpp:3993
bool supportsFindBuffer() const
Returns true if the clipboard supports a separate search buffer; otherwise returns false...
Definition: qclipboard.cpp:576
const char * format(int n) const
Returns the (i - 1)-th supported MIME format, or 0.
Definition: qclipboard.cpp:658
The QMimeData class provides a container for data that records information about its MIME type...
Definition: qmimedata.h:57
bool supportsSelection() const
Returns true if the clipboard supports mouse selection; otherwise returns false.
Definition: qclipboard.cpp:567
QVariant imageData() const
Returns a QVariant storing a QImage if the object can return an image; otherwise returns a null varia...
Definition: qmimedata.cpp:442
QString toUnicode(const QByteArray &) const
Converts a from the encoding of this codec to Unicode, and returns the result in a QString...
QString mid(int position, int n=-1) const Q_REQUIRED_RESULT
Returns a string that contains n characters of this string, starting at the specified position index...
Definition: qstring.cpp:3706
int size() const
Returns the number of items in the list.
Definition: qlist.h:137
bool ownsSelection() const
Returns true if this clipboard object owns the mouse selection data; otherwise returns false...
Definition: qclipboard.cpp:594
T qvariant_cast(const QVariant &)
Definition: qvariant.h:571
virtual const char * format(int n=0) const =0
Returns the (i - 1)-th supported MIME format, or 0.
The QPixmap class is an off-screen image representation that can be used as a paint device...
Definition: qpixmap.h:71
void setText(const QString &text)
Sets text as the plain text (MIME type text/plain) used to represent the data.
Definition: qmimedata.cpp:377
static QTextCodec * codecForUtfText(const QByteArray &ba)
Tries to detect the encoding of the provided snippet ba by using the BOM (Byte Order Mark) and return...
const char * variant
The QTextCodec class provides conversions between text encodings.
Definition: qtextcodec.h:62
QByteArray encodedData(const char *) const
Returns the encoded data of this object in the specified MIME format.
Definition: qclipboard.cpp:670
QString text(Mode mode=Clipboard) const
Returns the clipboard text as plain text, or an empty string if the clipboard does not contain any te...
Definition: qclipboard.cpp:357
virtual QByteArray encodedData(const char *) const =0
Returns the encoded data of this object in the specified MIME format.
#define text
Definition: qobjectdefs.h:80
QImage image(Mode mode=Clipboard) const
Returns the clipboard image, or returns a null image if the clipboard does not contain an image or if...
Definition: qclipboard.cpp:395