1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
|
/*
SPDX-FileCopyrightText: 2008-2014 Eike Hein <hein@kde.org>
SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
*/
#include "terminal.h"
#include "settings.h"
#include <KActionCollection>
#include <KColorScheme>
#include <KLocalizedString>
#include <KParts/PartLoader>
#include <KXMLGUIBuilder>
#include <KXMLGUIFactory>
#include <kde_terminal_interface.h>
#include <QAction>
#include <QApplication>
#include <QHBoxLayout>
#include <QLabel>
#include <QWidget>
#include <QKeyEvent>
int Terminal::m_availableTerminalId = 0;
Terminal::Terminal(const QString &workingDir, QWidget *parent)
: QObject(nullptr)
{
m_terminalId = m_availableTerminalId;
m_availableTerminalId++;
m_parentSplitter = parent;
KPluginMetaData part(QStringLiteral("kf6/parts/konsolepart"));
m_part = KParts::PartLoader::instantiatePart<KParts::Part>(part, parent).plugin;
if (!m_part) {
displayKPartLoadError();
return;
}
connect(m_part, SIGNAL(setWindowCaption(QString)), this, SLOT(setTitle(QString)));
connect(m_part, SIGNAL(overrideShortcut(QKeyEvent *, bool &)), this, SLOT(overrideShortcut(QKeyEvent *, bool &)));
connect(m_part, &KParts::Part::destroyed, this, [this] {
m_part = nullptr;
if (!m_destroying) {
Q_EMIT closeRequested(m_terminalId);
}
});
m_partWidget = m_part->widget();
m_terminalWidget = m_part->widget()->focusWidget();
if (m_terminalWidget) {
m_terminalWidget->setFocusPolicy(Qt::WheelFocus);
m_terminalWidget->installEventFilter(this);
if (!m_part->factory() && m_partWidget) {
if (!m_part->clientBuilder()) {
m_part->setClientBuilder(new KXMLGUIBuilder(m_partWidget));
}
auto factory = new KXMLGUIFactory(m_part->clientBuilder(), this);
factory->addClient(m_part);
// Prevents the KXMLGui warning about removing the client
connect(m_partWidget, &QObject::destroyed, this, [factory, this] {
factory->removeClient(m_part);
});
}
}
disableOffendingPartActions();
m_terminalInterface = qobject_cast<TerminalInterface *>(m_part);
if (!m_terminalInterface) {
qFatal("Version of Konsole is outdated. Konsole didn't return a valid TerminalInterface.");
return;
}
bool startInWorkingDir = m_terminalInterface->profileProperty(QStringLiteral("StartInCurrentSessionDir")).toBool();
if (startInWorkingDir && !workingDir.isEmpty()) {
m_terminalInterface->showShellInDir(workingDir);
}
QMetaObject::invokeMethod(m_part, "isBlurEnabled", Qt::DirectConnection, Q_RETURN_ARG(bool, m_wantsBlur));
// Remove shortcut from close action because it conflicts with the shortcut from out own close action
// https://bugs.kde.org/show_bug.cgi?id=319172
const auto childClients = m_part->childClients();
for (const auto childClient : childClients) {
QAction *closeSessionAction = childClient->actionCollection()->action(QStringLiteral("close-session"));
if (closeSessionAction) {
closeSessionAction->setShortcut(QKeySequence());
}
}
}
Terminal::~Terminal()
{
m_destroying = true;
// The ownership of m_part is a mess
// When the terminal exits, e.g. the user pressed Ctrl+D, the part deletes itself
// When we close a terminal we need to delete the part ourselves
if (m_part) {
delete m_part;
}
}
bool Terminal::eventFilter(QObject * /* watched */, QEvent *event)
{
if (event->type() == QEvent::FocusIn) {
Q_EMIT activated(m_terminalId);
QFocusEvent *focusEvent = static_cast<QFocusEvent *>(event);
if (focusEvent->reason() == Qt::MouseFocusReason || focusEvent->reason() == Qt::OtherFocusReason || focusEvent->reason() == Qt::BacktabFocusReason)
Q_EMIT manuallyActivated(this);
} else if (event->type() == QEvent::MouseMove) {
if (Settings::focusFollowsMouse() && m_terminalWidget && !m_terminalWidget->hasFocus())
m_terminalWidget->setFocus();
}
if (!m_keyboardInputEnabled) {
if (event->type() == QEvent::KeyPress) {
QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
if (keyEvent->modifiers() == Qt::NoModifier)
Q_EMIT keyboardInputBlocked(this);
return true;
} else if (event->type() == QEvent::KeyRelease)
return true;
}
return false;
}
void Terminal::displayKPartLoadError()
{
KColorScheme colorScheme(QPalette::Active);
QColor warningColor = colorScheme.background(KColorScheme::NeutralBackground).color();
QColor warningColorLight = KColorScheme::shade(warningColor, KColorScheme::LightShade, 0.1);
QString gradient = QStringLiteral("qlineargradient(x1:0, y1:0, x2:0, y2:1,stop: 0 %1, stop: 0.6 %1, stop: 1.0 %2)");
gradient = gradient.arg(warningColor.name(), warningColorLight.name());
QString styleSheet = QStringLiteral("QLabel { background: %1; }");
QWidget *widget = new QWidget(m_parentSplitter);
widget->setStyleSheet(styleSheet.arg(gradient));
m_partWidget = widget;
m_terminalWidget = widget;
m_terminalWidget->setFocusPolicy(Qt::WheelFocus);
m_terminalWidget->installEventFilter(this);
QLabel *label = new QLabel(widget);
label->setContentsMargins(10, 10, 10, 10);
label->setWordWrap(false);
label->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
label->setTextInteractionFlags(Qt::TextSelectableByMouse);
label->setText(xi18nc("@info",
"<application>Yakuake</application> was unable to load "
"the <application>Konsole</application> component.<nl/> "
"A <application>Konsole</application> installation is "
"required to use Yakuake."));
QLabel *icon = new QLabel(widget);
icon->setContentsMargins(10, 10, 10, 10);
icon->setPixmap(QIcon::fromTheme(QStringLiteral("dialog-warning")).pixmap(QSize(48, 48)));
icon->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
QHBoxLayout *layout = new QHBoxLayout(widget);
layout->addWidget(icon);
layout->addWidget(label);
layout->setSpacing(0);
layout->setContentsMargins(0, 0, 0, 0);
layout->setStretchFactor(icon, 1);
layout->setStretchFactor(label, 5);
}
void Terminal::disableOffendingPartActions()
{
// This is an unwelcome stop-gap that will be removed once we can
// count on a Konsole version that doesn't pollute a KPart user's
// shortcut "namespace".
if (!m_part)
return;
KActionCollection *actionCollection = m_part->actionCollection();
if (actionCollection) {
QAction *action = nullptr;
action = actionCollection->action(QStringLiteral("next-view"));
if (action)
action->setEnabled(false);
action = actionCollection->action(QStringLiteral("previous-view"));
if (action)
action->setEnabled(false);
action = actionCollection->action(QStringLiteral("close-active-view"));
if (action)
action->setEnabled(false);
action = actionCollection->action(QStringLiteral("split-view-left-right"));
if (action)
action->setEnabled(false);
action = actionCollection->action(QStringLiteral("split-view-auto"));
if (action)
action->setEnabled(false);
action = actionCollection->action(QStringLiteral("split-view-top-bottom"));
if (action)
action->setEnabled(false);
action = actionCollection->action(QStringLiteral("rename-session"));
if (action)
action->setEnabled(false);
action = actionCollection->action(QStringLiteral("enlarge-font"));
if (action)
action->setEnabled(false);
action = actionCollection->action(QStringLiteral("shrink-font"));
if (action)
action->setEnabled(false);
}
}
void Terminal::setTitle(const QString &title)
{
m_title = title;
Q_EMIT titleChanged(m_terminalId, m_title);
}
void Terminal::runCommand(const QString &command)
{
m_terminalInterface->sendInput(command + QStringLiteral("\n"));
}
void Terminal::manageProfiles()
{
QMetaObject::invokeMethod(m_part, "showManageProfilesDialog", Qt::QueuedConnection, Q_ARG(QWidget *, QApplication::activeWindow()));
}
void Terminal::editProfile()
{
QMetaObject::invokeMethod(m_part, "showEditCurrentProfileDialog", Qt::QueuedConnection, Q_ARG(QWidget *, QApplication::activeWindow()));
}
void Terminal::overrideShortcut(QKeyEvent * /* event */, bool &override)
{
override = false;
}
void Terminal::setMonitorActivityEnabled(bool enabled)
{
m_monitorActivityEnabled = enabled;
if (enabled) {
connect(m_part, SIGNAL(activityDetected()), this, SLOT(activityDetected()), Qt::UniqueConnection);
QMetaObject::invokeMethod(m_part, "setMonitorActivityEnabled", Qt::QueuedConnection, Q_ARG(bool, true));
} else {
disconnect(m_part, SIGNAL(activityDetected()), this, SLOT(activityDetected()));
QMetaObject::invokeMethod(m_part, "setMonitorActivityEnabled", Qt::QueuedConnection, Q_ARG(bool, false));
}
}
void Terminal::setMonitorSilenceEnabled(bool enabled)
{
m_monitorSilenceEnabled = enabled;
if (enabled) {
connect(m_part, SIGNAL(silenceDetected()), this, SLOT(silenceDetected()), Qt::UniqueConnection);
QMetaObject::invokeMethod(m_part, "setMonitorSilenceEnabled", Qt::QueuedConnection, Q_ARG(bool, true));
} else {
disconnect(m_part, SIGNAL(silenceDetected()), this, SLOT(silenceDetected()));
QMetaObject::invokeMethod(m_part, "setMonitorSilenceEnabled", Qt::QueuedConnection, Q_ARG(bool, false));
}
}
void Terminal::activityDetected()
{
Q_EMIT activityDetected(this);
}
void Terminal::silenceDetected()
{
Q_EMIT silenceDetected(this);
}
QString Terminal::currentWorkingDirectory() const
{
return m_terminalInterface->currentWorkingDirectory();
}
KActionCollection *Terminal::actionCollection()
{
if (m_part->factory()) {
const auto guiClients = m_part->childClients();
for (auto *client : guiClients) {
if (client->actionCollection()->associatedWidgets().contains(m_terminalWidget)) {
return client->actionCollection();
}
}
}
return nullptr;
}
#include "moc_terminal.cpp"
|