KDeclarative

clipboard.cpp
1/*
2 SPDX-FileCopyrightText: 2014 Aleix Pol Gonzalez <aleixpol@blue-systems.com>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "clipboard.h"
8#include <QDebug>
9#include <QGuiApplication>
10#include <QMimeData>
11#include <QUrl>
12
13Clipboard::Clipboard(QObject *parent)
14 : QObject(parent)
15 , m_clipboard(QGuiApplication::clipboard())
16 , m_mode(QClipboard::Clipboard)
17{
18 connect(m_clipboard, &QClipboard::changed, this, &Clipboard::clipboardChanged);
19}
20
21void Clipboard::setMode(QClipboard::Mode mode)
22{
23 m_mode = mode;
24 Q_EMIT modeChanged(m_mode);
25}
26
27void Clipboard::clipboardChanged(QClipboard::Mode m)
28{
29 if (m == m_mode) {
30 Q_EMIT contentChanged();
31 }
32}
33
35{
36 m_clipboard->clear(m_mode);
37}
38
40{
41 return m_mode;
42}
43
45{
46 const QMimeData *data = m_clipboard->mimeData(m_mode);
47 QVariant ret;
48 if (format == QLatin1String("text/uri-list")) {
49 QVariantList retList;
50 const auto urls = data->urls();
51 for (const QUrl &url : urls) {
52 retList += url;
53 }
54 ret = retList;
55 } else if (format.startsWith(QLatin1String("text/"))) {
56 ret = data->text();
57 } else if (format.startsWith(QLatin1String("image/"))) {
58 ret = data->imageData();
59 } else {
60 ret = data->data(format.isEmpty() ? data->formats().first() : format);
61 }
62
63 return ret;
64}
65
67{
68 return contentFormat(m_clipboard->mimeData(m_mode)->formats().first());
69}
70
71void Clipboard::setContent(const QVariant &content)
72{
73 QMimeData *mimeData = new QMimeData;
74 switch (content.userType()) {
76 mimeData->setText(content.toString());
77 break;
79 mimeData->setColorData(content.toString());
80 break;
83 mimeData->setImageData(content);
84 break;
85 default:
87 const QVariantList list = content.toList();
88 QList<QUrl> urls;
89 bool wasUrlList = true;
90 for (const QVariant &url : list) {
91 if (url.userType() != QMetaType::QUrl) {
92 wasUrlList = false;
93 break;
94 }
95 urls += url.toUrl();
96 }
97 if (wasUrlList) {
98 mimeData->setUrls(urls);
99 break;
100 }
101 }
102
103 if (content.canConvert<QString>()) {
104 mimeData->setText(content.toString());
105 } else {
106 mimeData->setData(QStringLiteral("application/octet-stream"), content.toByteArray());
107 qWarning() << "Couldn't figure out the content type, storing as application/octet-stream";
108 }
109 break;
110 }
111 m_clipboard->setMimeData(mimeData, m_mode);
112}
113
115{
116 return m_clipboard->mimeData(m_mode)->formats();
117}
118
119#include "moc_clipboard.cpp"
Wrapper for QClipboard.
Definition clipboard.h:38
Q_SCRIPTABLE void clear()
Definition clipboard.cpp:34
QStringList formats
Figure out the nature of the contents in the clipboard as mimetype strings.
Definition clipboard.h:54
QVariant content
Provides the contents currently in the clipboard and lets modify them.
Definition clipboard.h:49
QML_ELEMENTQClipboard::Mode mode
Controls the state this object will be monitoring and extracting its contents from.
Definition clipboard.h:44
Q_SCRIPTABLE QVariant contentFormat(const QString &format) const
Definition clipboard.cpp:44
KIOCORE_EXPORT QStringList list(const QString &fileClass)
void changed(QClipboard::Mode mode)
void clear(Mode mode)
const QMimeData * mimeData(Mode mode) const const
void setMimeData(QMimeData *src, Mode mode)
T & first()
QByteArray data(const QString &mimeType) const const
virtual QStringList formats() const const
QVariant imageData() const const
void setColorData(const QVariant &color)
void setData(const QString &mimeType, const QByteArray &data)
void setImageData(const QVariant &image)
void setText(const QString &text)
void setUrls(const QList< QUrl > &urls)
QString text() const const
QList< QUrl > urls() const const
Q_EMITQ_EMIT
bool isEmpty() const const
bool startsWith(QChar c, Qt::CaseSensitivity cs) const const
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
bool canConvert() const const
QByteArray toByteArray() const const
QList< QVariant > toList() const const
QString toString() const const
int userType() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:14:03 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.