2020-09-24 09:57:54 +10:00
|
|
|
// Copyright Mirage authors & contributors <https://github.com/mirukana/mirage>
|
2019-12-19 22:46:16 +11:00
|
|
|
// SPDX-License-Identifier: LGPL-3.0-or-later
|
|
|
|
|
2019-12-18 09:50:21 +11:00
|
|
|
// The Clipboard class exposes system clipboard management and retrieval
|
|
|
|
// to QML.
|
|
|
|
|
2019-10-25 23:36:24 +11:00
|
|
|
#ifndef CLIPBOARD_H
|
|
|
|
#define CLIPBOARD_H
|
|
|
|
|
2020-07-16 05:10:34 +10:00
|
|
|
#include <QBuffer>
|
|
|
|
#include <QByteArray>
|
2020-06-13 04:10:11 +10:00
|
|
|
#include <QClipboard>
|
2020-07-16 05:10:34 +10:00
|
|
|
#include <QGuiApplication>
|
|
|
|
#include <QIODevice>
|
|
|
|
#include <QImage>
|
|
|
|
#include <QMimeData>
|
2020-07-17 06:24:16 +10:00
|
|
|
#include <QMutex>
|
|
|
|
#include <QMutexLocker>
|
2019-10-25 23:36:24 +11:00
|
|
|
#include <QObject>
|
|
|
|
|
|
|
|
|
2020-07-11 14:51:53 +10:00
|
|
|
class Clipboard : public QObject {
|
2019-10-25 23:36:24 +11:00
|
|
|
Q_OBJECT
|
|
|
|
Q_PROPERTY(QString text READ text WRITE setText NOTIFY textChanged)
|
2020-07-16 05:10:34 +10:00
|
|
|
Q_PROPERTY(QByteArray image READ image WRITE setImage NOTIFY imageChanged)
|
|
|
|
Q_PROPERTY(bool hasImage READ hasImage NOTIFY hasImageChanged)
|
|
|
|
|
2019-10-25 23:36:24 +11:00
|
|
|
Q_PROPERTY(QString selection READ selection WRITE setSelection
|
|
|
|
NOTIFY selectionChanged)
|
|
|
|
|
|
|
|
Q_PROPERTY(bool supportsSelection READ supportsSelection CONSTANT)
|
|
|
|
|
|
|
|
public:
|
2020-06-13 04:10:11 +10:00
|
|
|
explicit Clipboard(QObject *parent = nullptr) : QObject(parent) {
|
|
|
|
connect(this->clipboard, &QClipboard::dataChanged,
|
2020-07-16 05:10:34 +10:00
|
|
|
this, &Clipboard::mainClipboardChanged);
|
2020-06-13 04:10:11 +10:00
|
|
|
|
|
|
|
connect(this->clipboard, &QClipboard::selectionChanged,
|
|
|
|
this, &Clipboard::selectionChanged);
|
|
|
|
}
|
2019-10-25 23:36:24 +11:00
|
|
|
|
2019-12-18 09:50:21 +11:00
|
|
|
// Normal primary clipboard
|
2020-06-13 04:10:11 +10:00
|
|
|
|
|
|
|
QString text() const {
|
|
|
|
return this->clipboard->text(QClipboard::Clipboard);
|
|
|
|
}
|
|
|
|
|
|
|
|
void setText(const QString &text) const {
|
|
|
|
this->clipboard->setText(text, QClipboard::Clipboard);
|
|
|
|
}
|
2019-10-25 23:36:24 +11:00
|
|
|
|
2020-07-17 05:15:10 +10:00
|
|
|
QImage *qimage() {
|
2020-07-17 06:24:16 +10:00
|
|
|
QMutexLocker locker(&(this->imageRetrievalLock));
|
|
|
|
|
2020-07-17 05:15:10 +10:00
|
|
|
if (this->cachedImage.isNull())
|
|
|
|
this->cachedImage = this->clipboard->image();
|
|
|
|
|
|
|
|
return &(this->cachedImage);
|
|
|
|
}
|
|
|
|
|
|
|
|
QByteArray image() {
|
2020-07-16 05:10:34 +10:00
|
|
|
QByteArray byteArray;
|
|
|
|
QBuffer buffer(&byteArray);
|
|
|
|
buffer.open(QIODevice::WriteOnly);
|
2020-07-17 05:15:10 +10:00
|
|
|
QImage *image = this->qimage();
|
|
|
|
// minimum compression, fastest to not freeze the UI
|
|
|
|
image->save(&buffer, "PNG", 100);
|
2020-07-16 05:10:34 +10:00
|
|
|
buffer.close();
|
|
|
|
return byteArray;
|
|
|
|
}
|
|
|
|
|
|
|
|
void setImage(const QByteArray &image) const { // TODO
|
|
|
|
Q_UNUSED(image)
|
|
|
|
}
|
|
|
|
|
|
|
|
bool hasImage() const {
|
|
|
|
return this->clipboard->mimeData()->hasImage();
|
|
|
|
}
|
|
|
|
|
2019-12-18 09:50:21 +11:00
|
|
|
// X11 select-middle-click-paste clipboard
|
2019-10-25 23:36:24 +11:00
|
|
|
|
2020-06-13 04:10:11 +10:00
|
|
|
QString selection() const {
|
|
|
|
return this->clipboard->text(QClipboard::Selection);
|
|
|
|
}
|
|
|
|
|
|
|
|
void setSelection(const QString &text) const {
|
|
|
|
if (this->clipboard->supportsSelection()) {
|
|
|
|
this->clipboard->setText(text, QClipboard::Selection);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Info
|
|
|
|
|
|
|
|
bool supportsSelection() const {
|
|
|
|
return this->clipboard->supportsSelection();
|
|
|
|
}
|
2019-10-25 23:36:24 +11:00
|
|
|
|
|
|
|
signals:
|
2020-07-17 03:54:17 +10:00
|
|
|
void contentChanged();
|
2019-10-25 23:36:24 +11:00
|
|
|
void textChanged();
|
2020-07-16 05:10:34 +10:00
|
|
|
void imageChanged();
|
|
|
|
void hasImageChanged();
|
2019-10-25 23:36:24 +11:00
|
|
|
void selectionChanged();
|
|
|
|
|
|
|
|
private:
|
2020-06-13 04:10:11 +10:00
|
|
|
QClipboard *clipboard = QGuiApplication::clipboard();
|
2020-07-17 06:24:16 +10:00
|
|
|
QImage cachedImage = QImage();
|
|
|
|
QMutex imageRetrievalLock;
|
2020-07-16 05:10:34 +10:00
|
|
|
|
|
|
|
void mainClipboardChanged() {
|
2020-07-17 05:15:10 +10:00
|
|
|
this->contentChanged();
|
|
|
|
this->cachedImage = QImage();
|
2020-07-16 05:10:34 +10:00
|
|
|
this->hasImage() ? this->imageChanged() : this->textChanged();
|
|
|
|
this->hasImageChanged();
|
|
|
|
};
|
2019-10-25 23:36:24 +11:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|