File indexing completed on 2023-05-30 10:41:17
0001 /* 0002 * Copyright 2014 Aleix Pol Gonzalez <aleixpol@blue-systems.com> 0003 * 0004 * This library is free software; you can redistribute it and/or 0005 * modify it under the terms of the GNU Library General Public 0006 * License as published by the Free Software Foundation; either 0007 * version 2 of the License, or (at your option) any later version. 0008 * 0009 * This library is distributed in the hope that it will be useful, 0010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 0011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 0012 * Library General Public License for more details. 0013 * 0014 * You should have received a copy of the GNU Library General Public License 0015 * along with this library; see the file COPYING.LIB. If not, write to 0016 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 0017 * Boston, MA 02110-1301, USA. 0018 */ 0019 0020 #include "clipboard.h" 0021 #include <QDebug> 0022 #include <QGuiApplication> 0023 #include <QMimeData> 0024 #include <QUrl> 0025 0026 Clipboard::Clipboard(QObject* parent) 0027 : QObject(parent) 0028 , m_clipboard(QGuiApplication::clipboard()) 0029 , m_mode(QClipboard::Clipboard) 0030 { 0031 connect(m_clipboard, &QClipboard::changed, this, &Clipboard::clipboardChanged); 0032 } 0033 0034 void Clipboard::setMode(QClipboard::Mode mode) 0035 { 0036 m_mode = mode; 0037 Q_EMIT modeChanged(m_mode); 0038 } 0039 0040 void Clipboard::clipboardChanged(QClipboard::Mode m) 0041 { 0042 if (m == m_mode) { 0043 Q_EMIT contentChanged(); 0044 } 0045 } 0046 0047 void Clipboard::clear() 0048 { 0049 m_clipboard->clear(m_mode); 0050 } 0051 0052 QClipboard::Mode Clipboard::mode() const 0053 { 0054 return m_mode; 0055 } 0056 0057 QVariant Clipboard::contentFormat(const QString &format) const 0058 { 0059 const QMimeData* data = m_clipboard->mimeData(m_mode); 0060 QVariant ret; 0061 if(format == QStringLiteral("text/uri-list")) { 0062 QVariantList retList; 0063 foreach(const QUrl& url, data->urls()) 0064 retList += url; 0065 ret = retList; 0066 } else if(format.startsWith(QStringLiteral("text/"))) { 0067 ret = data->text(); 0068 } else if(format.startsWith(QStringLiteral("image/"))) { 0069 ret = data->imageData(); 0070 } else 0071 ret = data->data(format.isEmpty() ? data->formats().first(): format); 0072 0073 return ret; 0074 } 0075 0076 QVariant Clipboard::content() const 0077 { 0078 return contentFormat(m_clipboard->mimeData(m_mode)->formats().first()); 0079 } 0080 0081 void Clipboard::setContent(const QVariant &content) 0082 { 0083 QMimeData* mimeData = new QMimeData; 0084 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) 0085 switch(content.type()) 0086 #else 0087 switch(content.userType()) 0088 #endif 0089 { 0090 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) 0091 case QVariant::String: 0092 #else 0093 case QMetaType::QString: 0094 #endif 0095 mimeData->setText(content.toString()); 0096 break; 0097 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) 0098 case QVariant::Color: 0099 #else 0100 case QMetaType::QColor: 0101 #endif 0102 mimeData->setColorData(content.toString()); 0103 break; 0104 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) 0105 case QVariant::Pixmap: 0106 case QVariant::Image: 0107 #else 0108 case QMetaType::QPixmap: 0109 case QMetaType::QImage: 0110 #endif 0111 mimeData->setImageData(content); 0112 break; 0113 default: 0114 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) 0115 if (content.type() == QVariant::List) { 0116 #else 0117 if (content.userType() == QMetaType::QVariantList) { 0118 #endif 0119 QVariantList list = content.toList(); 0120 QList<QUrl> urls; 0121 bool wasUrlList = true; 0122 foreach (const QVariant& url, list) { 0123 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) 0124 if (url.type() != QVariant::Url) { 0125 #else 0126 if (url.userType() != QMetaType::QUrl) { 0127 #endif 0128 wasUrlList = false; 0129 break; 0130 } 0131 urls += url.toUrl(); 0132 } 0133 if(wasUrlList) { 0134 mimeData->setUrls(urls); 0135 break; 0136 } 0137 } 0138 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) 0139 if (content.canConvert(QVariant::String)) { 0140 #else 0141 if (content.canConvert<QString>()) { 0142 #endif 0143 mimeData->setText(content.toString()); 0144 } else { 0145 mimeData->setData(QStringLiteral("application/octet-stream"), content.toByteArray()); 0146 qWarning() << "Couldn't figure out the content type, storing as application/octet-stream"; 0147 } 0148 break; 0149 } 0150 m_clipboard->setMimeData(mimeData, m_mode); 0151 } 0152 0153 QStringList Clipboard::formats() const 0154 { 0155 return m_clipboard->mimeData(m_mode)->formats(); 0156 }