OpenCoverage

qclipboard.cpp

Absolute File Name:/home/qt/qt5_coco/qt5/qtbase/src/gui/kernel/qclipboard.cpp
Source codeSwitch to Preprocessed file
LineSourceCount
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 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 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 "qclipboard.h"-
41-
42#ifndef QT_NO_CLIPBOARD-
43-
44#include "qmimedata.h"-
45#include "qpixmap.h"-
46#include "qvariant.h"-
47#include "qbuffer.h"-
48#include "qimage.h"-
49#include "qtextcodec.h"-
50-
51#include "private/qguiapplication_p.h"-
52#include <qpa/qplatformintegration.h>-
53#include <qpa/qplatformclipboard.h>-
54-
55QT_BEGIN_NAMESPACE-
56-
57/*!-
58 \class QClipboard-
59 \brief The QClipboard class provides access to the window system clipboard.-
60 \inmodule QtGui-
61-
62 The clipboard offers a simple mechanism to copy and paste data-
63 between applications.-
64-
65 QClipboard supports the same data types that QDrag does, and uses-
66 similar mechanisms. For advanced clipboard usage read \l{Drag and-
67 Drop}.-
68-
69 There is a single QClipboard object in an application, accessible-
70 as QGuiApplication::clipboard().-
71-
72 Example:-
73 \snippet code/src_gui_kernel_qclipboard.cpp 0-
74-
75 QClipboard features some convenience functions to access common-
76 data types: setText() allows the exchange of Unicode text and-
77 setPixmap() and setImage() allows the exchange of QPixmaps and-
78 QImages between applications. The setMimeData() function is the-
79 ultimate in flexibility: it allows you to add any QMimeData into-
80 the clipboard. There are corresponding getters for each of these,-
81 e.g. text(), image() and pixmap(). You can clear the clipboard by-
82 calling clear().-
83-
84 A typical example of the use of these functions follows:-
85-
86 \snippet droparea.cpp 0-
87-
88 \section1 Notes for X11 Users-
89-
90 \list-
91-
92 \li The X11 Window System has the concept of a separate selection-
93 and clipboard. When text is selected, it is immediately available-
94 as the global mouse selection. The global mouse selection may-
95 later be copied to the clipboard. By convention, the middle mouse-
96 button is used to paste the global mouse selection.-
97-
98 \li X11 also has the concept of ownership; if you change the-
99 selection within a window, X11 will only notify the owner and the-
100 previous owner of the change, i.e. it will not notify all-
101 applications that the selection or clipboard data changed.-
102-
103 \li Lastly, the X11 clipboard is event driven, i.e. the clipboard-
104 will not function properly if the event loop is not running.-
105 Similarly, it is recommended that the contents of the clipboard-
106 are stored or retrieved in direct response to user-input events,-
107 e.g. mouse button or key presses and releases. You should not-
108 store or retrieve the clipboard contents in response to timer or-
109 non-user-input events.-
110-
111 \li Since there is no standard way to copy and paste files between-
112 applications on X11, various MIME types and conventions are currently-
113 in use. For instance, Nautilus expects files to be supplied with a-
114 \c{x-special/gnome-copied-files} MIME type with data beginning with-
115 the cut/copy action, a newline character, and the URL of the file.-
116-
117 \endlist-
118-
119 \section1 Notes for \macos Users-
120-
121 \macos supports a separate find buffer that holds the current-
122 search string in Find operations. This find clipboard can be accessed-
123 by specifying the FindBuffer mode.-
124-
125 \section1 Notes for Windows and \macos Users-
126-
127 \list-
128-
129 \li Windows and \macos do not support the global mouse-
130 selection; they only supports the global clipboard, i.e. they-
131 only add text to the clipboard when an explicit copy or cut is-
132 made.-
133-
134 \li Windows and \macos does not have the concept of ownership;-
135 the clipboard is a fully global resource so all applications are-
136 notified of changes.-
137-
138 \endlist-
139-
140 \sa QGuiApplication-
141*/-
142-
143/*!-
144 \internal-
145-
146 Constructs a clipboard object.-
147-
148 Do not call this function.-
149-
150 Call QGuiApplication::clipboard() instead to get a pointer to the-
151 application's global clipboard object.-
152-
153 There is only one clipboard in the window system, and creating-
154 more than one object to represent it is almost certainly an error.-
155*/-
156-
157QClipboard::QClipboard(QObject *parent)-
158 : QObject(parent)-
159{-
160 // nothing-
161}
never executed: end of block
0
162-
163/*!-
164 \internal-
165-
166 Destroys the clipboard.-
167-
168 You should never delete the clipboard. QGuiApplication will do this-
169 when the application terminates.-
170*/-
171QClipboard::~QClipboard()-
172{-
173}-
174-
175/*!-
176 \fn void QClipboard::changed(QClipboard::Mode mode)-
177 \since 4.2-
178-
179 This signal is emitted when the data for the given clipboard \a-
180 mode is changed.-
181-
182 \sa dataChanged(), selectionChanged(), findBufferChanged()-
183*/-
184-
185/*!-
186 \fn void QClipboard::dataChanged()-
187-
188 This signal is emitted when the clipboard data is changed.-
189-
190 On \macos and with Qt version 4.3 or higher, clipboard-
191 changes made by other applications will only be detected-
192 when the application is activated.-
193-
194 \sa findBufferChanged(), selectionChanged(), changed()-
195*/-
196-
197/*!-
198 \fn void QClipboard::selectionChanged()-
199-
200 This signal is emitted when the selection is changed. This only-
201 applies to windowing systems that support selections, e.g. X11.-
202 Windows and \macos don't support selections.-
203-
204 \sa dataChanged(), findBufferChanged(), changed()-
205*/-
206-
207/*!-
208 \fn void QClipboard::findBufferChanged()-
209 \since 4.2-
210-
211 This signal is emitted when the find buffer is changed. This only-
212 applies to \macos.-
213-
214 With Qt version 4.3 or higher, clipboard changes made by other-
215 applications will only be detected when the application is activated.-
216-
217 \sa dataChanged(), selectionChanged(), changed()-
218*/-
219-
220-
221/*! \enum QClipboard::Mode-
222 \keyword clipboard mode-
223-
224 This enum type is used to control which part of the system clipboard is-
225 used by QClipboard::mimeData(), QClipboard::setMimeData() and related functions.-
226-
227 \value Clipboard indicates that data should be stored and retrieved from-
228 the global clipboard.-
229-
230 \value Selection indicates that data should be stored and retrieved from-
231 the global mouse selection. Support for \c Selection is provided only on-
232 systems with a global mouse selection (e.g. X11).-
233-
234 \value FindBuffer indicates that data should be stored and retrieved from-
235 the Find buffer. This mode is used for holding search strings on \macos.-
236-
237 \omitvalue LastMode-
238-
239 \sa QClipboard::supportsSelection()-
240*/-
241-
242-
243/*!-
244 \overload-
245-
246 Returns the clipboard text in subtype \a subtype, or an empty string-
247 if the clipboard does not contain any text. If \a subtype is null,-
248 any subtype is acceptable, and \a subtype is set to the chosen-
249 subtype.-
250-
251 The \a mode argument is used to control which part of the system-
252 clipboard is used. If \a mode is QClipboard::Clipboard, the-
253 text is retrieved from the global clipboard. If \a mode is-
254 QClipboard::Selection, the text is retrieved from the global-
255 mouse selection.-
256-
257 Common values for \a subtype are "plain" and "html".-
258-
259 Note that calling this function repeatedly, for instance from a-
260 key event handler, may be slow. In such cases, you should use the-
261 \c dataChanged() signal instead.-
262-
263 \sa setText(), mimeData()-
264*/-
265QString QClipboard::text(QString &subtype, Mode mode) const-
266{-
267 const QMimeData *const data = mimeData(mode);-
268 if (!data)
!dataDescription
TRUEnever evaluated
FALSEnever evaluated
0
269 return QString();
never executed: return QString();
0
270-
271 const QStringList formats = data->formats();-
272 if (subtype.isEmpty()) {
subtype.isEmpty()Description
TRUEnever evaluated
FALSEnever evaluated
0
273 if (formats.contains(QLatin1String("text/plain")))
formats.contai..."text/plain"))Description
TRUEnever evaluated
FALSEnever evaluated
0
274 subtype = QLatin1String("plain");
never executed: subtype = QLatin1String("plain");
0
275 else {-
276 for (int i = 0; i < formats.size(); ++i)
i < formats.size()Description
TRUEnever evaluated
FALSEnever evaluated
0
277 if (formats.at(i).startsWith(QLatin1String("text/"))) {
formats.at(i)....ring("text/"))Description
TRUEnever evaluated
FALSEnever evaluated
0
278 subtype = formats.at(i).mid(5);-
279 break;
never executed: break;
0
280 }-
281 if (subtype.isEmpty())
subtype.isEmpty()Description
TRUEnever evaluated
FALSEnever evaluated
0
282 return QString();
never executed: return QString();
0
283 }
never executed: end of block
0
284 } else if (!formats.contains(QLatin1String("text/") + subtype)) {
!formats.conta.../") + subtype)Description
TRUEnever evaluated
FALSEnever evaluated
0
285 return QString();
never executed: return QString();
0
286 }-
287-
288 const QByteArray rawData = data->data(QLatin1String("text/") + subtype);-
289-
290#ifndef QT_NO_TEXTCODEC-
291 QTextCodec* codec = QTextCodec::codecForMib(106); // utf-8 is default-
292 if (subtype == QLatin1String("html"))
subtype == QLa...String("html")Description
TRUEnever evaluated
FALSEnever evaluated
0
293 codec = QTextCodec::codecForHtml(rawData, codec);
never executed: codec = QTextCodec::codecForHtml(rawData, codec);
0
294 else-
295 codec = QTextCodec::codecForUtfText(rawData, codec);
never executed: codec = QTextCodec::codecForUtfText(rawData, codec);
0
296 return codec->toUnicode(rawData);
never executed: return codec->toUnicode(rawData);
0
297#else //QT_NO_TEXTCODEC-
298 return rawData;-
299#endif //QT_NO_TEXTCODEC-
300}-
301-
302/*!-
303 Returns the clipboard text as plain text, or an empty string if the-
304 clipboard does not contain any text.-
305-
306 The \a mode argument is used to control which part of the system-
307 clipboard is used. If \a mode is QClipboard::Clipboard, the-
308 text is retrieved from the global clipboard. If \a mode is-
309 QClipboard::Selection, the text is retrieved from the global-
310 mouse selection. If \a mode is QClipboard::FindBuffer, the-
311 text is retrieved from the search string buffer.-
312-
313 \sa setText(), mimeData()-
314*/-
315QString QClipboard::text(Mode mode) const-
316{-
317 const QMimeData *data = mimeData(mode);-
318 return data ? data->text() : QString();
never executed: return data ? data->text() : QString();
0
319}-
320-
321/*!-
322 Copies \a text into the clipboard as plain text.-
323-
324 The \a mode argument is used to control which part of the system-
325 clipboard is used. If \a mode is QClipboard::Clipboard, the-
326 text is stored in the global clipboard. If \a mode is-
327 QClipboard::Selection, the text is stored in the global-
328 mouse selection. If \a mode is QClipboard::FindBuffer, the-
329 text is stored in the search string buffer.-
330-
331 \sa text(), setMimeData()-
332*/-
333void QClipboard::setText(const QString &text, Mode mode)-
334{-
335 QMimeData *data = new QMimeData;-
336 data->setText(text);-
337 setMimeData(data, mode);-
338}
never executed: end of block
0
339-
340/*!-
341 Returns the clipboard image, or returns a null image if the-
342 clipboard does not contain an image or if it contains an image in-
343 an unsupported image format.-
344-
345 The \a mode argument is used to control which part of the system-
346 clipboard is used. If \a mode is QClipboard::Clipboard, the-
347 image is retrieved from the global clipboard. If \a mode is-
348 QClipboard::Selection, the image is retrieved from the global-
349 mouse selection.-
350-
351 \sa setImage(), pixmap(), mimeData(), QImage::isNull()-
352*/-
353QImage QClipboard::image(Mode mode) const-
354{-
355 const QMimeData *data = mimeData(mode);-
356 if (!data)
!dataDescription
TRUEnever evaluated
FALSEnever evaluated
0
357 return QImage();
never executed: return QImage();
0
358 return qvariant_cast<QImage>(data->imageData());
never executed: return qvariant_cast<QImage>(data->imageData());
0
359}-
360-
361/*!-
362 Copies the \a image into the clipboard.-
363-
364 The \a mode argument is used to control which part of the system-
365 clipboard is used. If \a mode is QClipboard::Clipboard, the-
366 image is stored in the global clipboard. If \a mode is-
367 QClipboard::Selection, the data is stored in the global-
368 mouse selection.-
369-
370 This is shorthand for:-
371-
372 \snippet code/src_gui_kernel_qclipboard.cpp 1-
373-
374 \sa image(), setPixmap(), setMimeData()-
375*/-
376void QClipboard::setImage(const QImage &image, Mode mode)-
377{-
378 QMimeData *data = new QMimeData;-
379 data->setImageData(image);-
380 setMimeData(data, mode);-
381}
never executed: end of block
0
382-
383/*!-
384 Returns the clipboard pixmap, or null if the clipboard does not-
385 contain a pixmap. Note that this can lose information. For-
386 example, if the image is 24-bit and the display is 8-bit, the-
387 result is converted to 8 bits, and if the image has an alpha-
388 channel, the result just has a mask.-
389-
390 The \a mode argument is used to control which part of the system-
391 clipboard is used. If \a mode is QClipboard::Clipboard, the-
392 pixmap is retrieved from the global clipboard. If \a mode is-
393 QClipboard::Selection, the pixmap is retrieved from the global-
394 mouse selection.-
395-
396 \sa setPixmap(), image(), mimeData(), QPixmap::convertFromImage()-
397*/-
398QPixmap QClipboard::pixmap(Mode mode) const-
399{-
400 const QMimeData *data = mimeData(mode);-
401 return data ? qvariant_cast<QPixmap>(data->imageData()) : QPixmap();
never executed: return data ? qvariant_cast<QPixmap>(data->imageData()) : QPixmap();
0
402}-
403-
404/*!-
405 Copies \a pixmap into the clipboard. Note that this is slower-
406 than setImage() because it needs to convert the QPixmap to a-
407 QImage first.-
408-
409 The \a mode argument is used to control which part of the system-
410 clipboard is used. If \a mode is QClipboard::Clipboard, the-
411 pixmap is stored in the global clipboard. If \a mode is-
412 QClipboard::Selection, the pixmap is stored in the global-
413 mouse selection.-
414-
415 \sa pixmap(), setImage(), setMimeData()-
416*/-
417void QClipboard::setPixmap(const QPixmap &pixmap, Mode mode)-
418{-
419 QMimeData *data = new QMimeData;-
420 data->setImageData(pixmap);-
421 setMimeData(data, mode);-
422}
never executed: end of block
0
423-
424-
425/*!-
426 \fn QMimeData *QClipboard::mimeData(Mode mode) const-
427-
428 Returns a pointer to a QMimeData representation of the current-
429 clipboard data (can be NULL if the given \a mode is not-
430 supported by the platform).-
431-
432 The \a mode argument is used to control which part of the system-
433 clipboard is used. If \a mode is QClipboard::Clipboard, the-
434 data is retrieved from the global clipboard. If \a mode is-
435 QClipboard::Selection, the data is retrieved from the global-
436 mouse selection. If \a mode is QClipboard::FindBuffer, the-
437 data is retrieved from the search string buffer.-
438-
439 The text(), image(), and pixmap() functions are simpler-
440 wrappers for retrieving text, image, and pixmap data.-
441-
442 \sa setMimeData()-
443*/-
444const QMimeData* QClipboard::mimeData(Mode mode) const-
445{-
446 QPlatformClipboard *clipboard = QGuiApplicationPrivate::platformIntegration()->clipboard();-
447 if (!clipboard->supportsMode(mode)) return 0;
never executed: return 0;
!clipboard->supportsMode(mode)Description
TRUEnever evaluated
FALSEnever evaluated
0
448 return clipboard->mimeData(mode);
never executed: return clipboard->mimeData(mode);
0
449}-
450-
451/*!-
452 \fn void QClipboard::setMimeData(QMimeData *src, Mode mode)-
453-
454 Sets the clipboard data to \a src. Ownership of the data is-
455 transferred to the clipboard. If you want to remove the data-
456 either call clear() or call setMimeData() again with new data.-
457-
458 The \a mode argument is used to control which part of the system-
459 clipboard is used. If \a mode is QClipboard::Clipboard, the-
460 data is stored in the global clipboard. If \a mode is-
461 QClipboard::Selection, the data is stored in the global-
462 mouse selection. If \a mode is QClipboard::FindBuffer, the-
463 data is stored in the search string buffer.-
464-
465 The setText(), setImage() and setPixmap() functions are simpler-
466 wrappers for setting text, image and pixmap data respectively.-
467-
468 \sa mimeData()-
469*/-
470void QClipboard::setMimeData(QMimeData* src, Mode mode)-
471{-
472 QPlatformClipboard *clipboard = QGuiApplicationPrivate::platformIntegration()->clipboard();-
473 if (!clipboard->supportsMode(mode)) {
!clipboard->supportsMode(mode)Description
TRUEnever evaluated
FALSEnever evaluated
0
474 if (src != 0) {
src != 0Description
TRUEnever evaluated
FALSEnever evaluated
0
475 qWarning("Data set on unsupported clipboard mode. QMimeData object will be deleted.");-
476 src->deleteLater();-
477 }
never executed: end of block
0
478 } else {
never executed: end of block
0
479 clipboard->setMimeData(src,mode);-
480 }
never executed: end of block
0
481}-
482-
483/*!-
484 \fn void QClipboard::clear(Mode mode)-
485 Clear the clipboard contents.-
486-
487 The \a mode argument is used to control which part of the system-
488 clipboard is used. If \a mode is QClipboard::Clipboard, this-
489 function clears the global clipboard contents. If \a mode is-
490 QClipboard::Selection, this function clears the global mouse-
491 selection contents. If \a mode is QClipboard::FindBuffer, this-
492 function clears the search string buffer.-
493-
494 \sa QClipboard::Mode, supportsSelection()-
495*/-
496void QClipboard::clear(Mode mode)-
497{-
498 setMimeData(0, mode);-
499}
never executed: end of block
0
500-
501/*!-
502 Returns \c true if the clipboard supports mouse selection; otherwise-
503 returns \c false.-
504*/-
505bool QClipboard::supportsSelection() const-
506{-
507 return supportsMode(Selection);
never executed: return supportsMode(Selection);
0
508}-
509-
510/*!-
511 Returns \c true if the clipboard supports a separate search buffer; otherwise-
512 returns \c false.-
513*/-
514bool QClipboard::supportsFindBuffer() const-
515{-
516 return supportsMode(FindBuffer);
never executed: return supportsMode(FindBuffer);
0
517}-
518-
519/*!-
520 Returns \c true if this clipboard object owns the clipboard data;-
521 otherwise returns \c false.-
522*/-
523bool QClipboard::ownsClipboard() const-
524{-
525 return ownsMode(Clipboard);
never executed: return ownsMode(Clipboard);
0
526}-
527-
528/*!-
529 Returns \c true if this clipboard object owns the mouse selection-
530 data; otherwise returns \c false.-
531*/-
532bool QClipboard::ownsSelection() const-
533{-
534 return ownsMode(Selection);
never executed: return ownsMode(Selection);
0
535}-
536-
537/*!-
538 \since 4.2-
539-
540 Returns \c true if this clipboard object owns the find buffer data;-
541 otherwise returns \c false.-
542*/-
543bool QClipboard::ownsFindBuffer() const-
544{-
545 return ownsMode(FindBuffer);
never executed: return ownsMode(FindBuffer);
0
546}-
547-
548/*!-
549 \internal-
550 \fn bool QClipboard::supportsMode(Mode mode) const;-
551 Returns \c true if the clipboard supports the clipboard mode speacified by \a mode;-
552 otherwise returns \c false.-
553*/-
554bool QClipboard::supportsMode(Mode mode) const-
555{-
556 QPlatformClipboard *clipboard = QGuiApplicationPrivate::platformIntegration()->clipboard();-
557 return clipboard->supportsMode(mode);
never executed: return clipboard->supportsMode(mode);
0
558}-
559-
560/*!-
561 \internal-
562 \fn bool QClipboard::ownsMode(Mode mode) const;-
563 Returns \c true if the clipboard supports the clipboard data speacified by \a mode;-
564 otherwise returns \c false.-
565*/-
566bool QClipboard::ownsMode(Mode mode) const-
567{-
568 QPlatformClipboard *clipboard = QGuiApplicationPrivate::platformIntegration()->clipboard();-
569 return clipboard->ownsMode(mode);
never executed: return clipboard->ownsMode(mode);
0
570}-
571-
572/*!-
573 \internal-
574 Emits the appropriate changed signal for \a mode.-
575*/-
576void QClipboard::emitChanged(Mode mode)-
577{-
578 switch (mode) {-
579 case Clipboard:
never executed: case Clipboard:
0
580 emit dataChanged();-
581 break;
never executed: break;
0
582 case Selection:
never executed: case Selection:
0
583 emit selectionChanged();-
584 break;
never executed: break;
0
585 case FindBuffer:
never executed: case FindBuffer:
0
586 emit findBufferChanged();-
587 break;
never executed: break;
0
588 default:
never executed: default:
0
589 break;
never executed: break;
0
590 }-
591 emit changed(mode);-
592}
never executed: end of block
0
593-
594QT_END_NAMESPACE-
595-
596#endif // QT_NO_CLIPBOARD-
Source codeSwitch to Preprocessed file

Generated by Squish Coco Non-Commercial 4.3.0-BETA-master-30-08-2018-4cb69e9