mirror of https://github.com/cutefishos/core
feat: add clipboard
parent
97fc1bdffe
commit
5290885d1c
@ -0,0 +1,26 @@
|
||||
cmake_minimum_required(VERSION 3.14)
|
||||
|
||||
project(cutefish-clipboard LANGUAGES CXX)
|
||||
|
||||
set(CMAKE_INCLUDE_CURRENT_DIR ON)
|
||||
|
||||
set(CMAKE_AUTOUIC ON)
|
||||
set(CMAKE_AUTOMOC ON)
|
||||
set(CMAKE_AUTORCC ON)
|
||||
|
||||
set(CMAKE_CXX_STANDARD 11)
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||
|
||||
find_package(Qt5 COMPONENTS Core Gui Widgets REQUIRED)
|
||||
|
||||
add_executable(cutefish-clipboard
|
||||
main.cpp
|
||||
clipboard.cpp
|
||||
)
|
||||
target_link_libraries(cutefish-clipboard
|
||||
Qt5::Core
|
||||
Qt5::Gui
|
||||
Qt5::Widgets
|
||||
)
|
||||
|
||||
install(TARGETS cutefish-clipboard DESTINATION ${CMAKE_INSTALL_BINDIR})
|
||||
@ -0,0 +1,71 @@
|
||||
/*
|
||||
* Copyright (C) 2021 CutefishOS Team.
|
||||
*
|
||||
* Author: Kate Leet <kate@cutefishos.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "clipboard.h"
|
||||
|
||||
#include <QApplication>
|
||||
#include <QMimeData>
|
||||
#include <QPixmap>
|
||||
#include <QDebug>
|
||||
|
||||
#include <QBuffer>
|
||||
|
||||
Clipboard::Clipboard(QObject *parent)
|
||||
: QObject(parent)
|
||||
, m_qtClipboard(qApp->clipboard())
|
||||
{
|
||||
connect(m_qtClipboard, &QClipboard::dataChanged, this, &Clipboard::onDataChanged);
|
||||
}
|
||||
|
||||
void Clipboard::onDataChanged()
|
||||
{
|
||||
const QMimeData *mimeData = m_qtClipboard->mimeData();
|
||||
|
||||
if (mimeData->formats().isEmpty())
|
||||
return;
|
||||
|
||||
if (mimeData->hasFormat("application/x-cutefish-clipboard") &&
|
||||
mimeData->data("application/x-cutefish-clipboard") == "1")
|
||||
return;
|
||||
|
||||
QByteArray timeStamp = mimeData->data("TIMESTAMP");
|
||||
|
||||
QMimeData *newMimeData = new QMimeData;
|
||||
if (mimeData->hasImage()) {
|
||||
QPixmap srcPix = m_qtClipboard->pixmap();
|
||||
|
||||
QByteArray bArray;
|
||||
QBuffer buffer(&bArray);
|
||||
buffer.open(QIODevice::WriteOnly);
|
||||
|
||||
srcPix.save(&buffer);
|
||||
|
||||
newMimeData->setImageData(srcPix);
|
||||
newMimeData->setData("TIMESTAMP", timeStamp);
|
||||
} else {
|
||||
for (const QString &key : mimeData->formats()) {
|
||||
newMimeData->setData(key, mimeData->data(key));
|
||||
}
|
||||
}
|
||||
|
||||
// cutefish flag.
|
||||
newMimeData->setData("application/x-cutefish-clipboard", QByteArray("1"));
|
||||
|
||||
m_qtClipboard->setMimeData(newMimeData);
|
||||
}
|
||||
@ -0,0 +1,40 @@
|
||||
/*
|
||||
* Copyright (C) 2021 CutefishOS Team.
|
||||
*
|
||||
* Author: Kate Leet <kate@cutefishos.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef CLIPBOARD_H
|
||||
#define CLIPBOARD_H
|
||||
|
||||
#include <QObject>
|
||||
#include <QClipboard>
|
||||
|
||||
class Clipboard : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit Clipboard(QObject *parent = nullptr);
|
||||
|
||||
private slots:
|
||||
void onDataChanged();
|
||||
|
||||
private:
|
||||
QClipboard *m_qtClipboard;
|
||||
};
|
||||
|
||||
#endif // CLIPBOARD_H
|
||||
@ -0,0 +1,30 @@
|
||||
/*
|
||||
* Copyright (C) 2021 CutefishOS Team.
|
||||
*
|
||||
* Author: Kate Leet <kate@cutefishos.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <QApplication>
|
||||
#include "clipboard.h"
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
QApplication a(argc, argv);
|
||||
|
||||
Clipboard clipboard;
|
||||
|
||||
return a.exec();
|
||||
}
|
||||
Loading…
Reference in New Issue