moment/src/clipboard.h

119 lines
3.1 KiB
C
Raw Normal View History

// 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.
#ifndef CLIPBOARD_H
#define CLIPBOARD_H
#include <QBuffer>
#include <QByteArray>
2020-06-13 04:10:11 +10:00
#include <QClipboard>
#include <QGuiApplication>
#include <QIODevice>
#include <QImage>
#include <QMimeData>
#include <QMutex>
#include <QMutexLocker>
#include <QObject>
2020-07-11 14:51:53 +10:00
class Clipboard : public QObject {
Q_OBJECT
Q_PROPERTY(QString text READ text WRITE setText NOTIFY textChanged)
Q_PROPERTY(QByteArray image READ image WRITE setImage NOTIFY imageChanged)
Q_PROPERTY(bool hasImage READ hasImage NOTIFY hasImageChanged)
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,
this, &Clipboard::mainClipboardChanged);
2020-06-13 04:10:11 +10:00
connect(this->clipboard, &QClipboard::selectionChanged,
this, &Clipboard::selectionChanged);
}
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);
}
QImage *qimage() {
QMutexLocker locker(&(this->imageRetrievalLock));
if (this->cachedImage.isNull())
this->cachedImage = this->clipboard->image();
return &(this->cachedImage);
}
QByteArray image() {
QByteArray byteArray;
QBuffer buffer(&byteArray);
buffer.open(QIODevice::WriteOnly);
QImage *image = this->qimage();
// minimum compression, fastest to not freeze the UI
image->save(&buffer, "PNG", 100);
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
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();
}
signals:
void contentChanged();
void textChanged();
void imageChanged();
void hasImageChanged();
void selectionChanged();
private:
2020-06-13 04:10:11 +10:00
QClipboard *clipboard = QGuiApplication::clipboard();
QImage cachedImage = QImage();
QMutex imageRetrievalLock;
void mainClipboardChanged() {
this->contentChanged();
this->cachedImage = QImage();
this->hasImage() ? this->imageChanged() : this->textChanged();
this->hasImageChanged();
};
};
#endif