mirror of
https://github.com/KDE/konsole.git
synced 2025-12-23 23:38:08 -05:00
TerminalDisplay: Extract copying related functions
This commit is contained in:
@@ -181,7 +181,7 @@ set(konsoleprivate_SRCS ${windowadaptors_SRCS}
|
||||
terminalDisplay/TerminalScrollBar.cpp
|
||||
terminalDisplay/TerminalColor.cpp
|
||||
terminalDisplay/TerminalFonts.cpp
|
||||
terminalDisplay/TerminalPasting.cpp
|
||||
terminalDisplay/TerminalClipboard.cpp
|
||||
|
||||
widgets/TerminalDisplayAccessible.cpp
|
||||
widgets/TerminalHeaderBar.cpp
|
||||
|
||||
@@ -17,10 +17,9 @@
|
||||
#include <KShell>
|
||||
#include <KLocalizedString>
|
||||
|
||||
#include "TerminalPasting.h"
|
||||
#include "TerminalClipboard.h"
|
||||
|
||||
namespace Konsole {
|
||||
namespace terminalPasting {
|
||||
namespace Konsole::terminalClipboard {
|
||||
|
||||
// Most code in Konsole uses UTF-32. We're filtering
|
||||
// UTF-16 here, as all control characters can be represented
|
||||
@@ -142,5 +141,26 @@ bool isUnsafe(const QChar c) {
|
||||
return (c.category() == QChar::Category::Other_Control && std::find(ALLOWLIST.begin(), ALLOWLIST.end(), c.unicode()) != ALLOWLIST.end());
|
||||
}
|
||||
|
||||
void copyToX11Selection(const QString &textToCopy, bool isHtml, bool autoCopySelectedText)
|
||||
{
|
||||
if (textToCopy.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
auto mimeData = new QMimeData;
|
||||
mimeData->setText(textToCopy);
|
||||
|
||||
if (isHtml) {
|
||||
mimeData->setHtml(textToCopy);
|
||||
}
|
||||
|
||||
if (QApplication::clipboard()->supportsSelection()) {
|
||||
QApplication::clipboard()->setMimeData(mimeData, QClipboard::Selection);
|
||||
}
|
||||
|
||||
if (autoCopySelectedText) {
|
||||
QApplication::clipboard()->setMimeData(mimeData, QClipboard::Clipboard);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -15,8 +15,7 @@ class QStringList;
|
||||
|
||||
#include <optional>
|
||||
|
||||
namespace Konsole {
|
||||
namespace terminalPasting {
|
||||
namespace Konsole::terminalClipboard {
|
||||
|
||||
/**
|
||||
* Retrieves the content of the clipboard and preprocesses it for pasting
|
||||
@@ -48,5 +47,6 @@ QStringList checkForUnsafeCharacters(const QString &text);
|
||||
*/
|
||||
bool isUnsafe(const QChar c);
|
||||
|
||||
}
|
||||
void copyToX11Selection(const QString &textToCopy, bool copyAsHtml, bool autoCopySelectedText);
|
||||
|
||||
}
|
||||
@@ -75,7 +75,7 @@
|
||||
#include "TerminalScrollBar.h"
|
||||
#include "TerminalColor.h"
|
||||
#include "TerminalFonts.h"
|
||||
#include "TerminalPasting.h"
|
||||
#include "TerminalClipboard.h"
|
||||
|
||||
using namespace Konsole;
|
||||
|
||||
@@ -1569,7 +1569,7 @@ void TerminalDisplay::processMidButtonClick(QMouseEvent* ev)
|
||||
if (_middleClickPasteMode == Enum::PasteFromX11Selection) {
|
||||
pasteFromX11Selection(appendEnter);
|
||||
} else if (_middleClickPasteMode == Enum::PasteFromClipboard) {
|
||||
doPaste(terminalPasting::pasteFromClipboard(), appendEnter);
|
||||
doPaste(terminalClipboard::pasteFromClipboard(), appendEnter);
|
||||
} else {
|
||||
Q_ASSERT(false);
|
||||
}
|
||||
@@ -2121,7 +2121,7 @@ void TerminalDisplay::doPaste(QString text, bool appendReturn)
|
||||
}
|
||||
}
|
||||
|
||||
auto unsafeCharacters = terminalPasting::checkForUnsafeCharacters(text);
|
||||
auto unsafeCharacters = terminalClipboard::checkForUnsafeCharacters(text);
|
||||
|
||||
if (!unsafeCharacters.isEmpty()) {
|
||||
int result = KMessageBox::warningYesNoCancelList(window(),
|
||||
@@ -2144,7 +2144,7 @@ void TerminalDisplay::doPaste(QString text, bool appendReturn)
|
||||
case KMessageBox::Cancel:
|
||||
return;
|
||||
case KMessageBox::Yes: {
|
||||
text = terminalPasting::sanitizeString(text);
|
||||
text = terminalClipboard::sanitizeString(text);
|
||||
}
|
||||
case KMessageBox::No:
|
||||
break;
|
||||
@@ -2153,7 +2153,7 @@ void TerminalDisplay::doPaste(QString text, bool appendReturn)
|
||||
}
|
||||
}
|
||||
|
||||
auto pasteString = terminalPasting::prepareStringForPasting(text, appendReturn, bracketedPasteMode());
|
||||
auto pasteString = terminalClipboard::prepareStringForPasting(text, appendReturn, bracketedPasteMode());
|
||||
if (pasteString.has_value()) {
|
||||
// perform paste by simulating keypress events
|
||||
QKeyEvent e(QEvent::KeyPress, 0, Qt::NoModifier, text);
|
||||
@@ -2182,26 +2182,11 @@ void TerminalDisplay::copyToX11Selection()
|
||||
return;
|
||||
}
|
||||
|
||||
const auto &text = _copyTextAsHTML ?
|
||||
_screenWindow->selectedText(currentDecodingOptions() | Screen::ConvertToHtml)
|
||||
: _screenWindow->selectedText(currentDecodingOptions());
|
||||
|
||||
const QString &text = _screenWindow->selectedText(currentDecodingOptions());
|
||||
if (text.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
auto mimeData = new QMimeData;
|
||||
mimeData->setText(text);
|
||||
|
||||
if (_copyTextAsHTML) {
|
||||
mimeData->setHtml(_screenWindow->selectedText(currentDecodingOptions() | Screen::ConvertToHtml));
|
||||
}
|
||||
|
||||
if (QApplication::clipboard()->supportsSelection()) {
|
||||
QApplication::clipboard()->setMimeData(mimeData, QClipboard::Selection);
|
||||
}
|
||||
|
||||
if (_autoCopySelectedText) {
|
||||
QApplication::clipboard()->setMimeData(mimeData, QClipboard::Clipboard);
|
||||
}
|
||||
terminalClipboard::copyToX11Selection(text, _copyTextAsHTML, _autoCopySelectedText);
|
||||
}
|
||||
|
||||
void TerminalDisplay::copyToClipboard()
|
||||
@@ -2705,7 +2690,7 @@ void TerminalDisplay::setSessionController(SessionController* controller)
|
||||
{
|
||||
_sessionController = controller;
|
||||
connect(_sessionController, &Konsole::SessionController::pasteFromClipboardRequested, [this] {
|
||||
doPaste(terminalPasting::pasteFromClipboard(), false);
|
||||
doPaste(terminalClipboard::pasteFromClipboard(), false);
|
||||
});
|
||||
_headerBar->finishHeaderSetup(controller);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user