Commit 72d604c6 authored by Gustavo Carneiro's avatar Gustavo Carneiro
Browse files

Move terminal color methods to a new class TerminalColor.

parent 4ee99c6c
......@@ -183,6 +183,7 @@ set(konsoleprivate_SRCS ${windowadaptors_SRCS}
terminalDisplay/TerminalDisplay.cpp
terminalDisplay/TerminalPainter.cpp
terminalDisplay/TerminalScrollBar.cpp
terminalDisplay/TerminalColor.cpp
widgets/TerminalDisplayAccessible.cpp
widgets/TerminalHeaderBar.cpp
......
......@@ -12,6 +12,8 @@
// Konsole
#include "../terminalDisplay/TerminalDisplay.h"
#include "../terminalDisplay/TerminalScrollBar.hpp"
#include "../terminalDisplay/TerminalColor.hpp"
#include "../characters/CharacterColor.h"
#include "colorscheme/ColorScheme.h"
......@@ -50,9 +52,9 @@ void TerminalTest::testColorTable()
auto display = new TerminalDisplay(nullptr);
display->setColorTable(defaultTable);
display->terminalColor()->setColorTable(defaultTable);
const ColorEntry *colorTable = display->colorTable();
const ColorEntry *colorTable = display->terminalColor()->colorTable();
for (int i = 0; i < TABLE_COLORS; i++) {
QCOMPARE(colorTable[i], defaultTable[i]);
......
......@@ -9,6 +9,7 @@
#include "Filter.h"
#include "terminalDisplay/TerminalDisplay.h"
#include "terminalDisplay/TerminalColor.hpp"
#include <QRect>
#include <QEvent>
......@@ -229,7 +230,7 @@ void FilterChain::paint(TerminalDisplay* td, QPainter& painter)
auto [cursorLine, cursorColumn] = td->getCharacterPosition(cursorPos, false);
Character cursorCharacter = td->getCursorCharacter( std::min(cursorColumn, td->columns() - 1), cursorLine);
painter.setPen(QPen(cursorCharacter.foregroundColor.color(td->colorTable())));
painter.setPen(QPen(cursorCharacter.foregroundColor.color(td->terminalColor()->colorTable())));
// iterate over hotspots identified by the display's currently active filters
// and draw appropriate visuals to indicate the presence of the hotspot
......
......@@ -87,6 +87,7 @@
#include "widgets/IncrementalSearchBar.h"
#include "terminalDisplay/TerminalDisplay.h"
#include "terminalDisplay/TerminalColor.hpp"
// For Unix signal names
#include <csignal>
......@@ -186,8 +187,8 @@ SessionController::SessionController(Session* session, TerminalDisplay* view, QO
connect(_sessionDisplayConnection->session() , &Konsole::Session::currentDirectoryChanged , this , &Konsole::SessionController::currentDirectoryChanged);
// listen for color changes
connect(_sessionDisplayConnection->session(), &Konsole::Session::changeBackgroundColorRequest, _sessionDisplayConnection->view(), &Konsole::TerminalDisplay::setBackgroundColor);
connect(_sessionDisplayConnection->session(), &Konsole::Session::changeForegroundColorRequest, _sessionDisplayConnection->view(), &Konsole::TerminalDisplay::setForegroundColor);
connect(_sessionDisplayConnection->session(), &Konsole::Session::changeBackgroundColorRequest, _sessionDisplayConnection->view()->terminalColor(), &Konsole::TerminalColor::setBackgroundColor);
connect(_sessionDisplayConnection->session(), &Konsole::Session::changeForegroundColorRequest, _sessionDisplayConnection->view()->terminalColor(), &Konsole::TerminalColor::setForegroundColor);
// update the title when the session starts
connect(_sessionDisplayConnection->session(), &Konsole::Session::started, this, &Konsole::SessionController::snapshot);
......@@ -562,13 +563,13 @@ void SessionController::sendSignal(QAction* action)
void SessionController::sendForegroundColor(uint terminator)
{
const QColor c = _sessionDisplayConnection->view()->getForegroundColor();
const QColor c = _sessionDisplayConnection->view()->terminalColor()->foregroundColor();
_sessionDisplayConnection->session()->reportForegroundColor(c, terminator);
}
void Konsole::SessionController::sendBackgroundColor(uint terminator)
{
const QColor c = _sessionDisplayConnection->view()->getBackgroundColor();
const QColor c = _sessionDisplayConnection->view()->terminalColor()->backgroundColor();
_sessionDisplayConnection->session()->reportBackgroundColor(c, terminator);
}
......
/*
SPDX-FileCopyrightText: 2020-2020 Gustavo Carneiro <gcarneiroa@hotmail.com>
SPDX-FileCopyrightText: 2007-2008 Robert Knight <robertknight@gmail.com>
SPDX-FileCopyrightText: 1997, 1998 Lars Doelle <lars.doelle@on-line.de>
SPDX-License-Identifier: GPL-2.0-or-later
*/
// Own
#include "TerminalColor.hpp"
// Konsole
#include "colorscheme/ColorScheme.h"
// Qt
#include <QTimer>
#include <QPalette>
#include <QApplication>
namespace Konsole
{
TerminalColor::TerminalColor(QWidget *parent)
: QWidget(parent)
, m_opacity(1.0)
, m_blendColor(qRgba(0, 0, 0, 0xff))
, m_cursorColor(QColor())
, m_cursorTextColor(QColor())
{
setColorTable(ColorScheme::defaultTable);
}
void TerminalColor::applyProfile(const Profile::Ptr &profile, ColorScheme const *colorScheme, int randomSeed)
{
ColorEntry table[TABLE_COLORS];
colorScheme->getColorTable(table, randomSeed);
setColorTable(table);
setOpacity(colorScheme->opacity());
m_cursorColor = profile->useCustomCursorColor() ? profile->customCursorColor() : QColor();
m_cursorTextColor = profile->useCustomCursorColor() ? profile->customCursorTextColor() : QColor();
}
QColor TerminalColor::backgroundColor() const
{
return m_colorTable[DEFAULT_BACK_COLOR];
}
QColor TerminalColor::foregroundColor() const
{
return m_colorTable[DEFAULT_FORE_COLOR];
}
void TerminalColor::setColorTable(const ColorEntry *table)
{
for (int index = 0; index < TABLE_COLORS; index++) {
m_colorTable[index] = table[index];
}
setBackgroundColor(m_colorTable[DEFAULT_BACK_COLOR]);
onColorsChanged();
}
const ColorEntry* TerminalColor::colorTable() const
{
return m_colorTable;
}
void TerminalColor::setOpacity(qreal opacity)
{
QColor color(m_blendColor);
color.setAlphaF(opacity);
m_opacity = opacity;
m_blendColor = color.rgba();
onColorsChanged();
}
void TerminalColor::visualBell()
{
swapFGBGColors();
QTimer::singleShot(200, this, &TerminalColor::swapFGBGColors);
}
qreal TerminalColor::opacity() const
{
return m_opacity;
}
QRgb TerminalColor::blendColor() const
{
return m_blendColor;
}
void TerminalColor::setBackgroundColor(const QColor &color)
{
m_colorTable[DEFAULT_BACK_COLOR] = color;
onColorsChanged();
}
void TerminalColor::setForegroundColor(const QColor &color)
{
m_colorTable[DEFAULT_FORE_COLOR] = color;
onColorsChanged();
}
bool TerminalColor::event(QEvent *event)
{
switch (event->type()) {
case QEvent::PaletteChange:
case QEvent::ApplicationPaletteChange:
onColorsChanged();
break;
default:
break;
}
return QWidget::event(event);
}
void TerminalColor::onColorsChanged()
{
QPalette palette = QApplication::palette();
QColor buttonTextColor = m_colorTable[DEFAULT_FORE_COLOR];
QColor backgroundColor = m_colorTable[DEFAULT_BACK_COLOR];
backgroundColor.setAlpha(m_opacity);
QColor buttonColor = backgroundColor.toHsv();
if (buttonColor.valueF() < 0.5) {
buttonColor = buttonColor.lighter();
} else {
buttonColor = buttonColor.darker();
}
palette.setColor(QPalette::Button, buttonColor);
palette.setColor(QPalette::Window, backgroundColor);
palette.setColor(QPalette::Base, backgroundColor);
palette.setColor(QPalette::WindowText, buttonTextColor);
palette.setColor(QPalette::ButtonText, buttonTextColor);
QWidget *widget = qobject_cast<QWidget*>(parent());
widget->setPalette(palette);
emit onPalette(palette);
widget->update();
}
void TerminalColor::swapFGBGColors()
{
ColorEntry color = m_colorTable[DEFAULT_BACK_COLOR];
m_colorTable[DEFAULT_BACK_COLOR] = m_colorTable[DEFAULT_FORE_COLOR];
m_colorTable[DEFAULT_FORE_COLOR] = color;
onColorsChanged();
}
}
/*
SPDX-FileCopyrightText: 2020-2020 Gustavo Carneiro <gcarneiroa@hotmail.com>
SPDX-FileCopyrightText: 2007-2008 Robert Knight <robertknight@gmail.com>
SPDX-FileCopyrightText: 1997, 1998 Lars Doelle <lars.doelle@on-line.de>
SPDX-License-Identifier: GPL-2.0-or-later
*/
#ifndef TERMINALCOLOR_HPP
#define TERMINALCOLOR_HPP
// Qt
#include <QWidget>
#include <QColor>
// Konsole
#include "profile/Profile.h"
#include "characters/CharacterColor.h"
#include "konsoleprivate_export.h"
namespace Konsole
{
class Profile;
class ColorScheme;
class KONSOLEPRIVATE_EXPORT TerminalColor : public QWidget
{
Q_OBJECT
public:
explicit TerminalColor(QWidget *parent);
void applyProfile(const Profile::Ptr &profile, ColorScheme const *colorScheme, int randomSeed);
QColor backgroundColor() const;
QColor foregroundColor() const;
void setColorTable(const ColorEntry *table);
const ColorEntry *colorTable() const;
void setOpacity(qreal opacity);
void visualBell();
qreal opacity() const;
QRgb blendColor() const;
QColor cursorColor() const
{
return m_cursorColor;
}
QColor cursorTextColor() const
{
return m_cursorTextColor;
}
public Q_SLOTS:
void setBackgroundColor(const QColor &color);
void setForegroundColor(const QColor &color);
Q_SIGNALS:
void onPalette(const QPalette &);
protected:
bool event(QEvent *event) override;
void onColorsChanged();
private Q_SLOTS:
void swapFGBGColors();
private:
qreal m_opacity;
QRgb m_blendColor;
QColor m_cursorColor;
QColor m_cursorTextColor;
ColorEntry m_colorTable[TABLE_COLORS];
};
}
#endif
......@@ -73,6 +73,7 @@
#include "TerminalPainter.hpp"
#include "TerminalScrollBar.hpp"
#include "TerminalColor.hpp"
using namespace Konsole;
......@@ -144,77 +145,6 @@ void TerminalDisplay::setScreenWindow(ScreenWindow* window)
}
}
const ColorEntry* TerminalDisplay::colorTable() const
{
return _colorTable;
}
void TerminalDisplay::onColorsChanged()
{
// Mostly just fix the scrollbar
// this is a workaround to add some readability to old themes like Fusion
// changing the light value for button a bit makes themes like fusion, windows and oxygen way more readable and pleasing
QPalette p = QApplication::palette();
QColor buttonTextColor = _colorTable[DEFAULT_FORE_COLOR];
QColor backgroundColor = _colorTable[DEFAULT_BACK_COLOR];
backgroundColor.setAlphaF(_opacity);
QColor buttonColor = backgroundColor.toHsv();
if (buttonColor.valueF() < 0.5) {
buttonColor = buttonColor.lighter();
} else {
buttonColor = buttonColor.darker();
}
p.setColor(QPalette::Button, buttonColor);
p.setColor(QPalette::Window, backgroundColor);
p.setColor(QPalette::Base, backgroundColor);
p.setColor(QPalette::WindowText, buttonTextColor);
p.setColor(QPalette::ButtonText, buttonTextColor);
setPalette(p);
_scrollBar->setPalette(p);
update();
}
void TerminalDisplay::setBackgroundColor(const QColor& color)
{
_colorTable[DEFAULT_BACK_COLOR] = color;
onColorsChanged();
}
QColor TerminalDisplay::getBackgroundColor() const
{
return _colorTable[DEFAULT_BACK_COLOR];
}
void TerminalDisplay::setForegroundColor(const QColor& color)
{
_colorTable[DEFAULT_FORE_COLOR] = color;
onColorsChanged();
}
QColor TerminalDisplay::getForegroundColor() const
{
return _colorTable[DEFAULT_FORE_COLOR];
}
void TerminalDisplay::setColorTable(const ColorEntry table[])
{
for (int i = 0; i < TABLE_COLORS; i++) {
_colorTable[i] = table[i];
}
setBackgroundColor(_colorTable[DEFAULT_BACK_COLOR]);
onColorsChanged();
}
/* ------------------------------------------------------------------------- */
/* */
/* Font */
......@@ -466,7 +396,6 @@ TerminalDisplay::TerminalDisplay(QWidget* parent)
, _outputSuspendedMessageWidget(nullptr)
, _lineSpacing(0)
, _size(QSize())
, _blendColor(qRgba(0, 0, 0, 0xff))
, _wallpaper(nullptr)
, _filterChain(new TerminalImageFilterChain(this))
, _filterUpdateRequired(true)
......@@ -481,7 +410,6 @@ TerminalDisplay::TerminalDisplay(QWidget* parent)
, _centerContents(false)
, _readOnlyMessageWidget(nullptr)
, _readOnly(false)
, _opacity(1.0)
, _dimWhenInactive(false)
, _scrollWheelState(ScrollState())
, _searchBar(new IncrementalSearchBar(this))
......@@ -489,6 +417,7 @@ TerminalDisplay::TerminalDisplay(QWidget* parent)
, _searchResultRect(QRect())
, _drawOverlay(false)
, _scrollBar(nullptr)
, _terminalColor(nullptr)
, _printManager(nullptr)
{
// terminal applications are not designed with Right-To-Left in mind,
......@@ -524,8 +453,6 @@ TerminalDisplay::TerminalDisplay(QWidget* parent)
setUsesMouseTracking(false);
setBracketedPasteMode(false);
setColorTable(ColorScheme::defaultTable);
// Enable drag and drop support
setAcceptDrops(true);
_dragInfo.state = diNone;
......@@ -562,13 +489,15 @@ TerminalDisplay::TerminalDisplay(QWidget* parent)
connect(KonsoleSettings::self(), &KonsoleSettings::configChanged, this, &TerminalDisplay::setupHeaderVisibility);
_terminalColor = new TerminalColor(this);
connect(_terminalColor, &TerminalColor::onPalette, _scrollBar, &TerminalScrollBar::setPalette);
_terminalPainter = new TerminalPainter(this);
connect(this, &TerminalDisplay::drawContents, _terminalPainter, &TerminalPainter::drawContents);
connect(this, &TerminalDisplay::drawCurrentResultRect, _terminalPainter, &TerminalPainter::drawCurrentResultRect);
connect(this, &TerminalDisplay::highlightScrolledLines, _terminalPainter, &TerminalPainter::highlightScrolledLines);
connect(this, &TerminalDisplay::highlightScrolledLinesRegion, _terminalPainter, &TerminalPainter::highlightScrolledLinesRegion);
connect(this, &TerminalDisplay::drawBackground, _terminalPainter, &TerminalPainter::drawBackground);
connect(this, &TerminalDisplay::drawCursor, _terminalPainter, &TerminalPainter::drawCursor);
connect(this, &TerminalDisplay::drawCharacters, _terminalPainter, &TerminalPainter::drawCharacters);
connect(this, &TerminalDisplay::drawInputMethodPreeditString, _terminalPainter, &TerminalPainter::drawInputMethodPreeditString);
......@@ -580,7 +509,7 @@ TerminalDisplay::TerminalDisplay(QWidget* parent)
emit drawContents(_image, paint, rect, friendly, _imageSize, _bidiEnabled, _fixedFont, _lineProperties);
};
auto lgetBackgroundColor = [this]() {
return getBackgroundColor();
return _terminalColor->backgroundColor();
};
_printManager = new KonsolePrintManager(ldrawBackground, ldrawContents, lgetBackgroundColor);
}
......@@ -599,6 +528,7 @@ TerminalDisplay::~TerminalDisplay()
_outputSuspendedMessageWidget = nullptr;
delete _terminalPainter;
delete _terminalColor;
delete _printManager;
}
......@@ -677,16 +607,6 @@ void TerminalDisplay::resetCursorStyle()
}
}
void TerminalDisplay::setOpacity(qreal opacity)
{
QColor color(_blendColor);
color.setAlphaF(opacity);
_opacity = opacity;
_blendColor = color.rgba();
onColorsChanged();
}
void TerminalDisplay::setWallpaper(const ColorSchemeWallpaper::Ptr &p)
{
_wallpaper = p;
......@@ -984,12 +904,12 @@ void TerminalDisplay::paintEvent(QPaintEvent* pe)
for (const QRect &rect : region) {
dirtyImageRegion += widgetToImage(rect);
emit drawBackground(paint, rect, getBackgroundColor(), true /* use opacity setting */);
emit drawBackground(paint, rect, _terminalColor->backgroundColor(), true /* use opacity setting */);
}
if (_displayVerticalLine) {
const int x = (_fontWidth/2) + (_fontWidth * _displayVerticalLineAtChar);
const QColor lineColor = getForegroundColor();
const QColor lineColor = _terminalColor->foregroundColor();
paint.setPen(lineColor);
paint.drawLine(QPoint(x, 0), QPoint(x, height()));
......@@ -2841,10 +2761,6 @@ bool TerminalDisplay::event(QEvent* event)
case QEvent::ShortcutOverride:
eventHandled = handleShortcutOverrideEvent(static_cast<QKeyEvent*>(event));
break;
case QEvent::PaletteChange:
case QEvent::ApplicationPaletteChange:
onColorsChanged();
break;
case QEvent::FocusOut:
case QEvent::FocusIn:
if(_screenWindow != nullptr) {
......@@ -2904,7 +2820,7 @@ void TerminalDisplay::bell(const QString& message)
message, QPixmap(), this);
break;
case Enum::VisualBell:
visualBell();
_terminalColor->visualBell();
break;
default:
break;
......@@ -2919,22 +2835,6 @@ void TerminalDisplay::bell(const QString& message)
});
}
void TerminalDisplay::visualBell()
{
swapFGBGColors();
QTimer::singleShot(200, this, &Konsole::TerminalDisplay::swapFGBGColors);
}
void TerminalDisplay::swapFGBGColors()
{
// swap the default foreground & background color
ColorEntry color = _colorTable[DEFAULT_BACK_COLOR];
_colorTable[DEFAULT_BACK_COLOR] = _colorTable[DEFAULT_FORE_COLOR];
_colorTable[DEFAULT_FORE_COLOR] = color;
onColorsChanged();
}
/* --------------------------------------------------------------------- */
/* */
/* Drag & Drop */
......@@ -3087,11 +2987,8 @@ IncrementalSearchBar *TerminalDisplay::searchBar() const
void TerminalDisplay::applyProfile(const Profile::Ptr &profile)
{
// load color scheme
ColorEntry table[TABLE_COLORS];
_colorScheme = ViewManager::colorSchemeForProfile(profile);
_colorScheme->getColorTable(table, randomSeed());
setColorTable(table);
setOpacity(_colorScheme->opacity());
_terminalColor->applyProfile(profile, _colorScheme, randomSeed());
setWallpaper(_colorScheme->wallpaper());
// load font
......@@ -3152,8 +3049,6 @@ void TerminalDisplay::applyProfile(const Profile::Ptr &profile)
_filterChain->setReverseUrlHints(profile->property<bool>(Profile::ReverseUrlHints));
_peekPrimaryShortcut = profile->peekPrimaryKeySequence();
_terminalPainter->applyProfile(profile);
}
void TerminalDisplay::printScreen()
......
......@@ -41,6 +41,7 @@ class KMessageWidget;
namespace Konsole {
class TerminalPainter;
class TerminalScrollBar;
class TerminalColor;
class KonsolePrintManager;
......@@ -74,10 +75,6 @@ public:
void applyProfile(const QExplicitlySharedDataPointer<Profile>& profile);
/** Returns the terminal color palette used by the display. */
const ColorEntry *colorTable() const;
/** Sets the terminal color palette used by the display. */
void setColorTable(const ColorEntry table[]);
/**
* Sets the seed used to generate random colors for the display
* (in color schemes that support them).
......@@ -245,9 +242,6 @@ public:
*/
int bellMode() const;
/** Play a visual bell for prompt or warning. */
void visualBell();
/** Returns the font used to draw characters in the display */
QFont getVTFont()
{
......@@ -301,18 +295,6 @@ public:
*/
void selectAll();
/**
* Gets the foreground of the display
* @see setForegroundColor(), setColorTable(), setBackgroundColor()
*/
QColor getForegroundColor() const;
/**
* Gets the background of the display
* @see setBackgroundColor(), setColorTable(), setForegroundColor()
*/
QColor getBackgroundColor() const;
bool bracketedPasteMode() const;
/**
......@@ -351,14 +333,9 @@ public:
return _scrollBar;
}
qreal opacity() const