moment/src/clipboard.h

68 lines
1.6 KiB
C
Raw Normal View History

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 <QGuiApplication>
2020-06-13 04:10:11 +10:00
#include <QClipboard>
#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(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::textChanged);
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);
}
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 textChanged();
void selectionChanged();
private:
2020-06-13 04:10:11 +10:00
QClipboard *clipboard = QGuiApplication::clipboard();
};
#endif