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
|
/*
SPDX-FileCopyrightText: 2009 Eike Hein <hein@kde.org>
SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
*/
#include "visualeventoverlay.h"
#include "sessionstack.h"
#include "settings.h"
#include "terminal.h"
#include <KColorScheme>
#include <KStatefulBrush>
#include <QPainter>
#include <QTimer>
EventRect::EventRect(const QPoint &topLeft, const QPoint &bottomRight, EventType type, EventFlags flags)
: QRect(topLeft, bottomRight)
{
m_eventType = type;
m_eventFlags = flags;
m_timeStamp.start();
}
EventRect::~EventRect() = default;
bool EventRect::operator==(const EventRect &eventRect) const
{
if (m_eventType == eventRect.eventType() && eventRect.testFlag(EventRect::Singleton))
return true;
if (m_eventType != eventRect.eventType())
return false;
return x() == eventRect.x() && y() == eventRect.y() && width() == eventRect.width() && height() == eventRect.height();
}
bool EventRect::operator<(const EventRect &eventRect) const
{
if (!testFlag(EventRect::Exclusive) && eventRect.testFlag(EventRect::Exclusive))
return false;
if (m_eventType < eventRect.eventType())
return true;
else if (m_timeStamp < eventRect.timeStamp())
return true;
return false;
}
VisualEventOverlay::VisualEventOverlay(SessionStack *parent)
: QWidget(parent)
{
m_sessionStack = parent;
setAutoFillBackground(false);
setAttribute(Qt::WA_TranslucentBackground, true);
setFocusPolicy(Qt::NoFocus);
setAttribute(Qt::WA_TransparentForMouseEvents, true);
m_cleanupTimer = new QTimer(this);
m_cleanupTimer->setSingleShot(true);
connect(m_cleanupTimer, SIGNAL(timeout()), this, SLOT(cleanupOverlay()));
m_cleanupTimerCeiling = 0;
hide();
}
VisualEventOverlay::~VisualEventOverlay() = default;
void VisualEventOverlay::highlightTerminal(Terminal *terminal, bool persistent)
{
if (!persistent && Settings::terminalHighlightDuration() == 0)
return;
if (isHidden())
show();
EventRect::EventFlags flags = EventRect::Singleton | EventRect::Exclusive;
if (persistent)
flags |= EventRect::Persistent;
terminalEvent(terminal, EventRect::TerminalHighlight, flags);
if (!persistent)
scheduleCleanup(Settings::terminalHighlightDuration());
}
void VisualEventOverlay::removeTerminalHighlight()
{
if (!m_eventRects.count())
return;
QMutableListIterator<EventRect> i(m_eventRects);
while (i.hasNext()) {
if (i.next().eventType() == EventRect::TerminalHighlight)
i.remove();
}
if (m_sessionStack->requiresVisualEventOverlay())
update();
else
hide();
}
void VisualEventOverlay::indicateKeyboardInputBlocked(Terminal *terminal)
{
if (Settings::keyboardInputBlockIndicatorDuration() == 0)
return;
terminalEvent(terminal, EventRect::KeyboardInputBlocked);
scheduleCleanup(Settings::keyboardInputBlockIndicatorDuration());
}
void VisualEventOverlay::terminalEvent(Terminal *terminal, EventRect::EventType type, EventRect::EventFlags flags)
{
QRect partRect(terminal->partWidget()->rect());
const QWidget *partWidget = terminal->partWidget();
QPoint topLeft(partWidget->mapTo(parentWidget(), partRect.topLeft()));
QPoint bottomRight(partWidget->mapTo(parentWidget(), partRect.bottomRight()));
EventRect eventRect(topLeft, bottomRight, type, flags);
m_eventRects.removeAll(eventRect);
m_eventRects.append(eventRect);
std::sort(m_eventRects.begin(), m_eventRects.end());
update();
}
void VisualEventOverlay::paintEvent(QPaintEvent *)
{
if (!m_eventRects.count())
return;
QPainter painter(this);
m_time.start();
bool painted = false;
QListIterator<EventRect> i(m_eventRects);
while (i.hasNext()) {
const EventRect &eventRect = i.next();
painted = false;
if (eventRect.eventType() == EventRect::TerminalHighlight
&& (eventRect.timeStamp().msecsTo(m_time) <= Settings::terminalHighlightDuration() || eventRect.testFlag(EventRect::Persistent))) {
KStatefulBrush terminalHighlightBrush(KColorScheme::View, KColorScheme::HoverColor);
painter.setOpacity(Settings::terminalHighlightOpacity());
painter.fillRect(eventRect, terminalHighlightBrush.brush(palette()));
painted = true;
} else if (eventRect.eventType() == EventRect::KeyboardInputBlocked
&& eventRect.timeStamp().msecsTo(m_time) <= Settings::keyboardInputBlockIndicatorDuration()) {
painter.setOpacity(Settings::keyboardInputBlockIndicatorOpacity());
painter.fillRect(eventRect, Settings::keyboardInputBlockIndicatorColor());
painted = true;
}
if (painted && i.hasNext() && eventRect.testFlag(EventRect::Exclusive)) {
if (!painter.hasClipping())
painter.setClipRect(rect());
painter.setClipRegion(painter.clipRegion().subtracted(eventRect));
}
}
}
void VisualEventOverlay::showEvent(QShowEvent *)
{
resize(parentWidget()->rect().size());
raise();
}
void VisualEventOverlay::hideEvent(QHideEvent *)
{
m_cleanupTimer->stop();
m_eventRects.clear();
}
void VisualEventOverlay::scheduleCleanup(int in)
{
int left = (m_cleanupTimerStarted.isValid()) ? m_cleanupTimerCeiling - m_cleanupTimerStarted.elapsed() : 0;
if (in > left) {
m_cleanupTimerCeiling = in;
m_cleanupTimerStarted.start();
m_cleanupTimer->start(in);
}
}
void VisualEventOverlay::cleanupOverlay()
{
if (m_eventRects.count()) {
m_time.start();
QMutableListIterator<EventRect> i(m_eventRects);
while (i.hasNext()) {
const EventRect &eventRect = i.next();
if (eventRect.eventType() == EventRect::TerminalHighlight && eventRect.timeStamp().msecsTo(m_time) >= Settings::terminalHighlightDuration()
&& !eventRect.testFlag(EventRect::Persistent)) {
i.remove();
} else if (eventRect.eventType() == EventRect::KeyboardInputBlocked
&& eventRect.timeStamp().msecsTo(m_time) >= Settings::keyboardInputBlockIndicatorDuration()) {
i.remove();
}
}
}
if (m_sessionStack->requiresVisualEventOverlay())
update();
else
hide();
}
#include "moc_visualeventoverlay.cpp"
|