KDeclarative

clipboard.cpp
1 /*
2  SPDX-FileCopyrightText: 2014 Aleix Pol Gonzalez <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 
7 #include "clipboard.h"
8 #include <QGuiApplication>
9 #include <QMimeData>
10 #include <QUrl>
11 #include <QDebug>
12 
13 Clipboard::Clipboard(QObject* parent)
14  : QObject(parent)
15  , m_clipboard(QGuiApplication::clipboard())
16  , m_mode(QClipboard::Clipboard)
17 {
18  connect(m_clipboard, SIGNAL(changed(QClipboard::Mode)), SLOT(clipboardChanged(QClipboard::Mode)));
19 }
20 
21 void Clipboard::setMode(QClipboard::Mode mode)
22 {
23  m_mode = mode;
24  Q_EMIT modeChanged(m_mode);
25 }
26 
27 void Clipboard::clipboardChanged(QClipboard::Mode m)
28 {
29  if (m == m_mode) {
30  Q_EMIT contentChanged();
31  }
32 }
33 
34 void Clipboard::clear()
35 {
36  m_clipboard->clear(m_mode);
37 }
38 
39 QClipboard::Mode Clipboard::mode() const
40 {
41  return m_mode;
42 }
43 
44 QVariant Clipboard::contentFormat(const QString &format) const
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  ret = retList;
54  } else if(format.startsWith(QLatin1String("text/"))) {
55  ret = data->text();
56  } else if(format.startsWith(QLatin1String("image/"))) {
57  ret = data->imageData();
58  } else
59  ret = data->data(format.isEmpty() ? data->formats().first(): format);
60 
61  return ret;
62 }
63 
64 QVariant Clipboard::content() const
65 {
66  return contentFormat(m_clipboard->mimeData(m_mode)->formats().first());
67 }
68 
69 void Clipboard::setContent(const QVariant &content)
70 {
71  QMimeData* mimeData = new QMimeData;
72  switch(content.type())
73  {
74  case QVariant::String:
75  mimeData->setText(content.toString());
76  break;
77  case QVariant::Color:
78  mimeData->setColorData(content.toString());
79  break;
80  case QVariant::Pixmap:
81  case QVariant::Image:
82  mimeData->setImageData(content);
83  break;
84  default:
85  if (content.type() == QVariant::List) {
86  const QVariantList list = content.toList();
87  QList<QUrl> urls;
88  bool wasUrlList = true;
89  for (const QVariant& url : list) {
90  if (url.type() != QVariant::Url) {
91  wasUrlList = false;
92  break;
93  }
94  urls += url.toUrl();
95  }
96  if(wasUrlList) {
97  mimeData->setUrls(urls);
98  break;
99  }
100  }
101 
102  if (content.canConvert(QVariant::String)) {
103  mimeData->setText(content.toString());
104  } else {
105  mimeData->setData(QStringLiteral("application/octet-stream"), content.toByteArray());
106  qWarning() << "Couldn't figure out the content type, storing as application/octet-stream";
107  }
108  break;
109  }
110  m_clipboard->setMimeData(mimeData, m_mode);
111 }
112 
113 QStringList Clipboard::formats() const
114 {
115  return m_clipboard->mimeData(m_mode)->formats();
116 }
bool canConvert(int targetTypeId) const const
QByteArray toByteArray() const const
QByteArray data(const QString &mimeType) const const
QList< QVariant > toList() const const
void setColorData(const QVariant &color)
QString text() const const
bool isEmpty() const const
bool startsWith(const QString &s, Qt::CaseSensitivity cs) const const
void setText(const QString &text)
T & first()
void setImageData(const QVariant &image)
QList< QUrl > urls() const const
QVariant imageData() const const
virtual QStringList formats() const const
void setData(const QString &mimeType, const QByteArray &data)
QVariant::Type type() const const
QString toString() const const
void setUrls(const QList< QUrl > &urls)
KIOFILEWIDGETS_EXPORT QStringList list(const QString &fileClass)
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Mon Jan 25 2021 22:44:28 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.