kernel/qclipboard.cpp

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

Generated by Squish Coco Non-Commercial