From 30823a9bf516881d34a4d71e63989e7fc5cba959 Mon Sep 17 00:00:00 2001 From: miruka Date: Fri, 25 Oct 2019 08:36:24 -0400 Subject: [PATCH] Add C++ Clipboard class accessible from QML --- harmonyqml.pro | 4 ++-- src/clipboard.cpp | 38 ++++++++++++++++++++++++++++++++++++++ src/clipboard.h | 37 +++++++++++++++++++++++++++++++++++++ src/main.cpp | 2 ++ 4 files changed, 79 insertions(+), 2 deletions(-) create mode 100644 src/clipboard.cpp create mode 100644 src/clipboard.h diff --git a/harmonyqml.pro b/harmonyqml.pro index 70c21276..38ce5481 100644 --- a/harmonyqml.pro +++ b/harmonyqml.pro @@ -12,8 +12,8 @@ RCC_DIR = $$BUILD_DIR/rcc QRC_FILE = $$BUILD_DIR/resources.qrc RESOURCES += $$QRC_FILE -HEADERS += src/utils.h -SOURCES += src/main.cpp src/utils.cpp +HEADERS += src/utils.h src/clipboard.h +SOURCES += src/main.cpp src/utils.cpp src/clipboard.cpp TARGET = harmonyqml diff --git a/src/clipboard.cpp b/src/clipboard.cpp new file mode 100644 index 00000000..148569df --- /dev/null +++ b/src/clipboard.cpp @@ -0,0 +1,38 @@ +#include +#include "clipboard.h" + + +Clipboard::Clipboard(QObject *parent) + : QObject(parent) { + + connect(m_clipboard, &QClipboard::dataChanged, + this, &Clipboard::textChanged); + + connect(m_clipboard, &QClipboard::selectionChanged, + this, &Clipboard::selectionChanged); +} + + +QString Clipboard::text() const { + return m_clipboard->text(QClipboard::Clipboard); +} + +void Clipboard::setText(const QString &text) { + m_clipboard->setText(text, QClipboard::Clipboard); +} + + +QString Clipboard::selection() const { + return m_clipboard->text(QClipboard::Selection); +} + +void Clipboard::setSelection(const QString &text) { + if (m_clipboard->supportsSelection()) { + m_clipboard->setText(text, QClipboard::Selection); + } +} + + +bool Clipboard::supportsSelection() const { + return m_clipboard->supportsSelection(); +} diff --git a/src/clipboard.h b/src/clipboard.h new file mode 100644 index 00000000..edf7a49a --- /dev/null +++ b/src/clipboard.h @@ -0,0 +1,37 @@ +#ifndef CLIPBOARD_H +#define CLIPBOARD_H + +#include +#include + + +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: + explicit Clipboard(QObject *parent = 0); + + QString text() const; + void setText(const QString &text); + + QString selection() const; + void setSelection(const QString &text); + + bool supportsSelection() const; + +signals: + void textChanged(); + void selectionChanged(); + +private: + QClipboard *m_clipboard = QGuiApplication::clipboard(); +}; + + +#endif diff --git a/src/main.cpp b/src/main.cpp index ec48866a..55155e73 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -5,6 +5,7 @@ #include #include "utils.h" +#include "clipboard.h" int main(int argc, char *argv[]) { @@ -25,6 +26,7 @@ int main(int argc, char *argv[]) { #endif objectContext->setContextProperty("CppUtils", new Utils()); + objectContext->setContextProperty("Clipboard", new Clipboard()); QFileInfo qrcPath(":src/qml/Window.qml");