Transform Clipboard to singleton and add rich text copying feature
This commit is contained in:
@@ -209,7 +209,7 @@ Control {
|
||||
icon.name: "mail-attachment"
|
||||
|
||||
onClicked: {
|
||||
if (imageClipboard.hasImage) {
|
||||
if (Clipboard.hasImage) {
|
||||
attachDialog.open()
|
||||
} else {
|
||||
var fileDialog = openFileDialog.createObject(ApplicationWindow.overlay)
|
||||
|
||||
@@ -5,13 +5,14 @@ import Qt.labs.qmlmodels 1.0
|
||||
import QtQuick.Controls.Material 2.12
|
||||
|
||||
import org.kde.kirigami 2.4 as Kirigami
|
||||
|
||||
import org.kde.kitemmodels 1.0
|
||||
import org.kde.neochat 1.0
|
||||
|
||||
import Spectral.Component 2.0
|
||||
import Spectral.Component.Timeline 2.0
|
||||
import Spectral.Dialog 2.0
|
||||
import Spectral.Effect 2.0
|
||||
import Spectral.Menu.Timeline 2.0
|
||||
import Spectral 0.1
|
||||
|
||||
Kirigami.ScrollablePage {
|
||||
@@ -27,10 +28,6 @@ Kirigami.ScrollablePage {
|
||||
room: currentRoom
|
||||
}
|
||||
|
||||
ImageClipboard {
|
||||
id: imageClipboard
|
||||
}
|
||||
|
||||
QQC2.Popup {
|
||||
anchors.centerIn: parent
|
||||
|
||||
@@ -74,7 +71,7 @@ Kirigami.ScrollablePage {
|
||||
text: i18n("Clipboard image")
|
||||
onClicked: {
|
||||
var localPath = StandardPaths.writableLocation(StandardPaths.CacheLocation) + "/screenshots/" + (new Date()).getTime() + ".png"
|
||||
if (!imageClipboard.saveImage(localPath)) return
|
||||
if (!Clipboard.saveImage(localPath)) return
|
||||
chatTextInput.attach(localPath)
|
||||
attachDialog.close()
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ add_executable(neochat
|
||||
accountlistmodel.cpp
|
||||
controller.cpp
|
||||
emojimodel.cpp
|
||||
imageclipboard.cpp
|
||||
clipboard.cpp
|
||||
matriximageprovider.cpp
|
||||
messageeventmodel.cpp
|
||||
roomlistmodel.cpp
|
||||
|
||||
@@ -3,32 +3,35 @@
|
||||
*
|
||||
* SPDX-LicenseIdentifier: GPL-3.0-only
|
||||
*/
|
||||
#include "imageclipboard.h"
|
||||
#include "clipboard.h"
|
||||
|
||||
#include <QDebug>
|
||||
#include <QClipboard>
|
||||
#include <QMimeData>
|
||||
#include <QRegularExpression>
|
||||
#include <QImage>
|
||||
#include <QDir>
|
||||
#include <QFileInfo>
|
||||
#include <QGuiApplication>
|
||||
#include <QUrl>
|
||||
|
||||
ImageClipboard::ImageClipboard(QObject *parent)
|
||||
Clipboard::Clipboard(QObject *parent)
|
||||
: QObject(parent)
|
||||
, m_clipboard(QGuiApplication::clipboard())
|
||||
{
|
||||
connect(m_clipboard, &QClipboard::changed, this, &ImageClipboard::imageChanged);
|
||||
connect(m_clipboard, &QClipboard::changed, this, &Clipboard::imageChanged);
|
||||
}
|
||||
|
||||
bool ImageClipboard::hasImage() const
|
||||
bool Clipboard::hasImage() const
|
||||
{
|
||||
return !image().isNull();
|
||||
}
|
||||
|
||||
QImage ImageClipboard::image() const
|
||||
QImage Clipboard::image() const
|
||||
{
|
||||
return m_clipboard->image();
|
||||
}
|
||||
|
||||
bool ImageClipboard::saveImage(const QUrl &localPath)
|
||||
bool Clipboard::saveImage(const QUrl &localPath)
|
||||
{
|
||||
if (!localPath.isLocalFile())
|
||||
return false;
|
||||
@@ -48,3 +51,12 @@ bool ImageClipboard::saveImage(const QUrl &localPath)
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void Clipboard::saveText(QString message)
|
||||
{
|
||||
QRegularExpression re("<[^>]*>");
|
||||
auto *mineData = new QMimeData; // ownership is transfered to clipboard
|
||||
mineData->setHtml(message);
|
||||
mineData->setText(message.replace(re, ""));
|
||||
m_clipboard->setMimeData(mineData);
|
||||
}
|
||||
@@ -3,32 +3,35 @@
|
||||
*
|
||||
* SPDX-LicenseIdentifier: GPL-3.0-only
|
||||
*/
|
||||
#ifndef IMAGECLIPBOARD_H
|
||||
#define IMAGECLIPBOARD_H
|
||||
#pragma once
|
||||
|
||||
#include <QClipboard>
|
||||
#include <QImage>
|
||||
#include <QObject>
|
||||
|
||||
class ImageClipboard : public QObject
|
||||
class QClipboard;
|
||||
class QImage;
|
||||
|
||||
/**
|
||||
* Clipboard proxy
|
||||
*/
|
||||
class Clipboard : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
Q_PROPERTY(bool hasImage READ hasImage NOTIFY imageChanged)
|
||||
Q_PROPERTY(QImage image READ image NOTIFY imageChanged)
|
||||
|
||||
public:
|
||||
explicit ImageClipboard(QObject *parent = nullptr);
|
||||
explicit Clipboard(QObject *parent = nullptr);
|
||||
|
||||
bool hasImage() const;
|
||||
QImage image() const;
|
||||
|
||||
Q_INVOKABLE bool saveImage(const QUrl &localPath);
|
||||
|
||||
Q_INVOKABLE void saveText(QString message);
|
||||
|
||||
private:
|
||||
QClipboard *m_clipboard;
|
||||
|
||||
Q_SIGNALS:
|
||||
void imageChanged();
|
||||
};
|
||||
|
||||
#endif // IMAGECLIPBOARD_H
|
||||
@@ -23,7 +23,7 @@
|
||||
#include "csapi/joining.h"
|
||||
#include "csapi/leaving.h"
|
||||
#include "emojimodel.h"
|
||||
#include "imageclipboard.h"
|
||||
#include "clipboard.h"
|
||||
#include "matriximageprovider.h"
|
||||
#include "messageeventmodel.h"
|
||||
#include "notificationsmanager.h"
|
||||
@@ -57,7 +57,10 @@ int main(int argc, char *argv[])
|
||||
app.setOrganizationName("KDE");
|
||||
app.setWindowIcon(QIcon(":/assets/img/icon.png"));
|
||||
|
||||
Clipboard clipboard;
|
||||
|
||||
qmlRegisterSingletonInstance("Spectral", 0, 1, "Controller", &Controller::instance());
|
||||
qmlRegisterSingletonInstance("org.kde.neochat", 1, 0, "Clipboard", &clipboard);
|
||||
qmlRegisterType<AccountListModel>("Spectral", 0, 1, "AccountListModel");
|
||||
qmlRegisterType<RoomListModel>("Spectral", 0, 1, "RoomListModel");
|
||||
qmlRegisterType<UserListModel>("Spectral", 0, 1, "UserListModel");
|
||||
@@ -67,7 +70,6 @@ int main(int argc, char *argv[])
|
||||
qmlRegisterType<EmojiModel>("Spectral", 0, 1, "EmojiModel");
|
||||
qmlRegisterType<NotificationsManager>("Spectral", 0, 1, "NotificationsManager");
|
||||
qmlRegisterType<TrayIcon>("Spectral", 0, 1, "TrayIcon");
|
||||
qmlRegisterType<ImageClipboard>("Spectral", 0, 1, "ImageClipboard");
|
||||
qmlRegisterUncreatableType<RoomMessageEvent>("Spectral", 0, 1, "RoomMessageEvent", "ENUM");
|
||||
qmlRegisterUncreatableType<RoomType>("Spectral", 0, 1, "RoomType", "ENUM");
|
||||
qmlRegisterUncreatableType<UserType>("Spectral", 0, 1, "UserType", "ENUM");
|
||||
|
||||
Reference in New Issue
Block a user