source: trunk/src/gui/kernel/qclipboard.cpp@ 5

Last change on this file since 5 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 19.3 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the QtGui module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
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
54QT_BEGIN_NAMESPACE
55
56/*!
57 \class QClipboard
58 \brief The QClipboard class provides access to the window system clipboard.
59
60 \ingroup io
61 \ingroup environment
62 \mainclass
63
64 The clipboard offers a simple mechanism to copy and paste data
65 between applications.
66
67 QClipboard supports the same data types that QDrag does, and uses
68 similar mechanisms. For advanced clipboard usage read \l{Drag and
69 Drop}.
70
71 There is a single QClipboard object in an application, accessible
72 as QApplication::clipboard().
73
74 Example:
75 \snippet doc/src/snippets/code/src_gui_kernel_qclipboard.cpp 0
76
77 QClipboard features some convenience functions to access common
78 data types: setText() allows the exchange of Unicode text and
79 setPixmap() and setImage() allows the exchange of QPixmaps and
80 QImages between applications. The setMimeData() function is the
81 ultimate in flexibility: it allows you to add any QMimeData into
82 the clipboard. There are corresponding getters for each of these,
83 e.g. text(), image() and pixmap(). You can clear the clipboard by
84 calling clear().
85
86 A typical example of the use of these functions follows:
87
88 \snippet doc/src/snippets/droparea.cpp 0
89
90 \section1 Notes for X11 Users
91
92 \list
93
94 \i The X11 Window System has the concept of a separate selection
95 and clipboard. When text is selected, it is immediately available
96 as the global mouse selection. The global mouse selection may
97 later be copied to the clipboard. By convention, the middle mouse
98 button is used to paste the global mouse selection.
99
100 \i X11 also has the concept of ownership; if you change the
101 selection within a window, X11 will only notify the owner and the
102 previous owner of the change, i.e. it will not notify all
103 applications that the selection or clipboard data changed.
104
105 \i Lastly, the X11 clipboard is event driven, i.e. the clipboard
106 will not function properly if the event loop is not running.
107 Similarly, it is recommended that the contents of the clipboard
108 are stored or retrieved in direct response to user-input events,
109 e.g. mouse button or key presses and releases. You should not
110 store or retrieve the clipboard contents in response to timer or
111 non-user-input events.
112
113 \endlist
114
115 \section1 Notes for Mac OS X Users
116
117 Mac OS X supports a separate find buffer that holds the current
118 search string in Find operations. This find clipboard can be accessed
119 by specifying the FindBuffer mode.
120
121 \section1 Notes for Windows and Mac OS X Users
122
123 \list
124
125 \i Windows and Mac OS X do not support the global mouse
126 selection; they only supports the global clipboard, i.e. they
127 only add text to the clipboard when an explicit copy or cut is
128 made.
129
130 \i Windows and Mac OS X does not have the concept of ownership;
131 the clipboard is a fully global resource so all applications are
132 notified of changes.
133
134 \endlist
135
136 \sa QApplication
137*/
138
139#ifndef Q_WS_X11
140// for X11 there is a separate implementation of a constructor.
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(*new QClipboardPrivate, parent)
157{
158 // nothing
159}
160#endif
161
162#ifndef Q_WS_WIN32
163/*!
164 \internal
165
166 Destroys the clipboard.
167
168 You should never delete the clipboard. QApplication will do this
169 when the application terminates.
170*/
171QClipboard::~QClipboard()
172{
173}
174#endif
175
176/*!
177 \fn void QClipboard::changed(QClipboard::Mode mode)
178 \since 4.2
179
180 This signal is emitted when the data for the given clipboard \a
181 mode is changed.
182
183 \sa dataChanged(), selectionChanged(), findBufferChanged()
184*/
185
186/*!
187 \fn void QClipboard::dataChanged()
188
189 This signal is emitted when the clipboard data is changed.
190
191 On Mac OS X and with Qt version 4.3 or higher, clipboard
192 changes made by other applications will only be detected
193 when the application is activated.
194
195 \sa findBufferChanged(), selectionChanged(), changed()
196*/
197
198/*!
199 \fn void QClipboard::selectionChanged()
200
201 This signal is emitted when the selection is changed. This only
202 applies to windowing systems that support selections, e.g. X11.
203 Windows and Mac OS X don't support selections.
204
205 \sa dataChanged(), findBufferChanged(), changed()
206*/
207
208/*!
209 \fn void QClipboard::findBufferChanged()
210 \since 4.2
211
212 This signal is emitted when the find buffer is changed. This only
213 applies to Mac OS X.
214
215 With Qt version 4.3 or higher, clipboard changes made by other
216 applications will only be detected when the application is activated.
217
218 \sa dataChanged(), selectionChanged(), changed()
219*/
220
221
222/*! \enum QClipboard::Mode
223 \keyword clipboard mode
224
225 This enum type is used to control which part of the system clipboard is
226 used by QClipboard::mimeData(), QClipboard::setMimeData() and related functions.
227
228 \value Clipboard indicates that data should be stored and retrieved from
229 the global clipboard.
230
231 \value Selection indicates that data should be stored and retrieved from
232 the global mouse selection. Support for \c Selection is provided only on
233 systems with a global mouse selection (e.g. X11).
234
235 \value FindBuffer indicates that data should be stored and retrieved from
236 the Find buffer. This mode is used for holding search strings on Mac OS X.
237
238 \omitvalue LastMode
239
240 \sa QClipboard::supportsSelection()
241*/
242
243
244/*****************************************************************************
245 QApplication member functions related to QClipboard.
246 *****************************************************************************/
247
248// text handling is done directly in qclipboard_qws, for now
249
250/*!
251 \fn bool QClipboard::event(QEvent *e)
252 \reimp
253*/
254
255/*!
256 \overload
257
258 Returns the clipboard text in subtype \a subtype, or an empty string
259 if the clipboard does not contain any text. If \a subtype is null,
260 any subtype is acceptable, and \a subtype is set to the chosen
261 subtype.
262
263 The \a mode argument is used to control which part of the system
264 clipboard is used. If \a mode is QClipboard::Clipboard, the
265 text is retrieved from the global clipboard. If \a mode is
266 QClipboard::Selection, the text is retrieved from the global
267 mouse selection.
268
269 Common values for \a subtype are "plain" and "html".
270
271 Note that calling this function repeatedly, for instance from a
272 key event handler, may be slow. In such cases, you should use the
273 \c dataChanged() signal instead.
274
275 \sa setText(), mimeData()
276*/
277QString QClipboard::text(QString &subtype, Mode mode) const
278{
279 const QMimeData *data = mimeData(mode);
280 if (!data)
281 return QString();
282 if (subtype.isEmpty()) {
283 QStringList formats = data->formats();
284 if (formats.contains(QLatin1String("text/plain")))
285 subtype = QLatin1String("plain");
286 else {
287 for (int i = 0; i < formats.size(); ++i)
288 if (formats.at(i).startsWith(QLatin1String("text/"))) {
289 subtype = formats.at(i).mid(5);
290 break;
291 }
292 }
293 }
294 if (subtype.isEmpty())
295 return QString();
296 if (subtype == QLatin1String("plain"))
297 return data->text();
298 return QString::fromUtf8(data->data(QLatin1String("text/") + subtype));
299}
300
301/*!
302 Returns the clipboard text as plain text, or an empty string if the
303 clipboard does not contain any text.
304
305 The \a mode argument is used to control which part of the system
306 clipboard is used. If \a mode is QClipboard::Clipboard, the
307 text is retrieved from the global clipboard. If \a mode is
308 QClipboard::Selection, the text is retrieved from the global
309 mouse selection. If \a mode is QClipboard::FindBuffer, the
310 text is retrieved from the search string buffer.
311
312 \sa setText(), mimeData()
313*/
314QString QClipboard::text(Mode mode) const
315{
316 const QMimeData *data = mimeData(mode);
317 return data ? data->text() : QString();
318}
319
320/*!
321 Copies \a text into the clipboard as plain text.
322
323 The \a mode argument is used to control which part of the system
324 clipboard is used. If \a mode is QClipboard::Clipboard, the
325 text is stored in the global clipboard. If \a mode is
326 QClipboard::Selection, the text is stored in the global
327 mouse selection. If \a mode is QClipboard::FindBuffer, the
328 text is stored in the search string buffer.
329
330 \sa text(), setMimeData()
331*/
332void QClipboard::setText(const QString &text, Mode mode)
333{
334 QMimeData *data = new QMimeData;
335 data->setText(text);
336 setMimeData(data, mode);
337}
338
339/*!
340 Returns the clipboard image, or returns a null image if the
341 clipboard does not contain an image or if it contains an image in
342 an unsupported image format.
343
344 The \a mode argument is used to control which part of the system
345 clipboard is used. If \a mode is QClipboard::Clipboard, the
346 image is retrieved from the global clipboard. If \a mode is
347 QClipboard::Selection, the image is retrieved from the global
348 mouse selection.
349
350 \sa setImage() pixmap() mimeData(), QImage::isNull()
351*/
352QImage QClipboard::image(Mode mode) const
353{
354 const QMimeData *data = mimeData(mode);
355 if (!data)
356 return QImage();
357 return qvariant_cast<QImage>(data->imageData());
358}
359
360/*!
361 Copies the \a image into the clipboard.
362
363 The \a mode argument is used to control which part of the system
364 clipboard is used. If \a mode is QClipboard::Clipboard, the
365 image is stored in the global clipboard. If \a mode is
366 QClipboard::Selection, the data is stored in the global
367 mouse selection.
368
369 This is shorthand for:
370
371 \snippet doc/src/snippets/code/src_gui_kernel_qclipboard.cpp 1
372
373 \sa image(), setPixmap() setMimeData()
374*/
375void QClipboard::setImage(const QImage &image, Mode mode)
376{
377 QMimeData *data = new QMimeData;
378 data->setImageData(image);
379 setMimeData(data, mode);
380}
381
382/*!
383 Returns the clipboard pixmap, or null if the clipboard does not
384 contain a pixmap. Note that this can lose information. For
385 example, if the image is 24-bit and the display is 8-bit, the
386 result is converted to 8 bits, and if the image has an alpha
387 channel, the result just has a mask.
388
389 The \a mode argument is used to control which part of the system
390 clipboard is used. If \a mode is QClipboard::Clipboard, the
391 pixmap is retrieved from the global clipboard. If \a mode is
392 QClipboard::Selection, the pixmap is retrieved from the global
393 mouse selection.
394
395 \sa setPixmap() image() mimeData() QPixmap::convertFromImage()
396*/
397QPixmap QClipboard::pixmap(Mode mode) const
398{
399 const QMimeData *data = mimeData(mode);
400 return data ? qvariant_cast<QPixmap>(data->imageData()) : QPixmap();
401}
402
403/*!
404 Copies \a pixmap into the clipboard. Note that this is slower
405 than setImage() because it needs to convert the QPixmap to a
406 QImage first.
407
408 The \a mode argument is used to control which part of the system
409 clipboard is used. If \a mode is QClipboard::Clipboard, the
410 pixmap is stored in the global clipboard. If \a mode is
411 QClipboard::Selection, the pixmap is stored in the global
412 mouse selection.
413
414 \sa pixmap() setImage() setMimeData()
415*/
416void QClipboard::setPixmap(const QPixmap &pixmap, Mode mode)
417{
418 QMimeData *data = new QMimeData;
419 data->setImageData(pixmap);
420 setMimeData(data, mode);
421}
422
423
424/*!
425 \fn QMimeData *QClipboard::mimeData(Mode mode) const
426
427 Returns a reference to a QMimeData representation of the current
428 clipboard data.
429
430 The \a mode argument is used to control which part of the system
431 clipboard is used. If \a mode is QClipboard::Clipboard, the
432 data is retrieved from the global clipboard. If \a mode is
433 QClipboard::Selection, the data is retrieved from the global
434 mouse selection. If \a mode is QClipboard::FindBuffer, the
435 data is retrieved from the search string buffer.
436
437 The text(), image(), and pixmap() functions are simpler
438 wrappers for retrieving text, image, and pixmap data.
439
440 \sa setMimeData()
441*/
442
443/*!
444 \fn void QClipboard::setMimeData(QMimeData *src, Mode mode)
445
446 Sets the clipboard data to \a src. Ownership of the data is
447 transferred to the clipboard. If you want to remove the data
448 either call clear() or call setMimeData() again with new data.
449
450 The \a mode argument is used to control which part of the system
451 clipboard is used. If \a mode is QClipboard::Clipboard, the
452 data is stored in the global clipboard. If \a mode is
453 QClipboard::Selection, the data is stored in the global
454 mouse selection. If \a mode is QClipboard::FindBuffer, the
455 data is stored in the search string buffer.
456
457 The setText(), setImage() and setPixmap() functions are simpler
458 wrappers for setting text, image and pixmap data respectively.
459
460 \sa mimeData()
461*/
462
463/*!
464 \fn void QClipboard::clear(Mode mode)
465 Clear the clipboard contents.
466
467 The \a mode argument is used to control which part of the system
468 clipboard is used. If \a mode is QClipboard::Clipboard, this
469 function clears the the global clipboard contents. If \a mode is
470 QClipboard::Selection, this function clears the global mouse
471 selection contents. If \a mode is QClipboard::FindBuffer, this
472 function clears the search string buffer.
473
474 \sa QClipboard::Mode, supportsSelection()
475*/
476
477#ifdef QT3_SUPPORT
478/*!
479 \fn QMimeSource *QClipboard::data(Mode mode) const
480 \compat
481
482 Use mimeData() instead.
483*/
484QMimeSource *QClipboard::data(Mode mode) const
485{
486 Q_D(const QClipboard);
487
488 if (supportsMode(mode) == false)
489 return 0;
490
491 if (d->compat_data[mode])
492 return d->compat_data[mode];
493
494 d->wrapper[mode]->data = mimeData(mode);
495 return d->wrapper[mode];
496}
497
498
499/*!
500 \fn void QClipboard::setData(QMimeSource *src, Mode mode)
501 \compat
502
503 Use setMimeData() instead.
504*/
505void QClipboard::setData(QMimeSource *source, Mode mode)
506{
507 Q_D(QClipboard);
508
509 if (supportsMode(mode) == false)
510 return;
511
512 d->compat_data[mode] = source;
513 setMimeData(new QMimeSourceWrapper(d, mode), mode);
514}
515#endif // QT3_SUPPORT
516
517/*!
518 Returns true if the clipboard supports mouse selection; otherwise
519 returns false.
520*/
521bool QClipboard::supportsSelection() const
522{
523 return supportsMode(Selection);
524}
525
526/*!
527 Returns true if the clipboard supports a separate search buffer; otherwise
528 returns false.
529*/
530bool QClipboard::supportsFindBuffer() const
531{
532 return supportsMode(FindBuffer);
533}
534
535/*!
536 Returns true if this clipboard object owns the clipboard data;
537 otherwise returns false.
538*/
539bool QClipboard::ownsClipboard() const
540{
541 return ownsMode(Clipboard);
542}
543
544/*!
545 Returns true if this clipboard object owns the mouse selection
546 data; otherwise returns false.
547*/
548bool QClipboard::ownsSelection() const
549{
550 return ownsMode(Selection);
551}
552
553/*!
554 \since 4.2
555
556 Returns true if this clipboard object owns the find buffer data;
557 otherwise returns false.
558*/
559bool QClipboard::ownsFindBuffer() const
560{
561 return ownsMode(FindBuffer);
562}
563
564/*!
565 \internal
566 \fn bool QClipboard::supportsMode(Mode mode) const;
567 Returns true if the clipboard supports the clipboard mode speacified by \a mode;
568 otherwise returns false.
569*/
570
571/*!
572 \internal
573 \fn bool QClipboard::ownsMode(Mode mode) const;
574 Returns true if the clipboard supports the clipboard data speacified by \a mode;
575 otherwise returns false.
576*/
577
578/*!
579 \internal
580 Emits the appropriate changed signal for \a mode.
581*/
582void QClipboard::emitChanged(Mode mode)
583{
584 switch (mode) {
585 case Clipboard:
586 emit dataChanged();
587 break;
588 case Selection:
589 emit selectionChanged();
590 break;
591 case FindBuffer:
592 emit findBufferChanged();
593 break;
594 default:
595 break;
596 }
597 emit changed(mode);
598}
599
600const char* QMimeDataWrapper::format(int n) const
601{
602 if (formats.isEmpty()) {
603 QStringList fmts = data->formats();
604 for (int i = 0; i < fmts.size(); ++i)
605 formats.append(fmts.at(i).toLatin1());
606 }
607 if (n < 0 || n >= formats.size())
608 return 0;
609 return formats.at(n).data();
610}
611
612QByteArray QMimeDataWrapper::encodedData(const char *format) const
613{
614 if (QLatin1String(format) != QLatin1String("application/x-qt-image")){
615 return data->data(QLatin1String(format));
616 } else{
617 QVariant variant = data->imageData();
618 QImage img = qVariantValue<QImage>(variant);
619 QByteArray ba;
620 QBuffer buffer(&ba);
621 buffer.open(QIODevice::WriteOnly);
622 img.save(&buffer, "PNG");
623 return ba;
624 }
625}
626
627QVariant QMimeSourceWrapper::retrieveData(const QString &mimetype, QVariant::Type) const
628{
629 return source->encodedData(mimetype.toLatin1());
630}
631
632bool QMimeSourceWrapper::hasFormat(const QString &mimetype) const
633{
634 return source->provides(mimetype.toLatin1());
635}
636
637QStringList QMimeSourceWrapper::formats() const
638{
639 QStringList fmts;
640 int i = 0;
641 const char *fmt;
642 while ((fmt = source->format(i))) {
643 fmts.append(QLatin1String(fmt));
644 ++i;
645 }
646 return fmts;
647}
648
649#endif // QT_NO_CLIPBOARD
650
651QT_END_NAMESPACE
Note: See TracBrowser for help on using the repository browser.