File indexing completed on 2024-04-28 05:50:49

0001 /*
0002     SPDX-FileCopyrightText: 2020-2020 Gustavo Carneiro <gcarneiroa@hotmail.com>
0003     SPDX-FileCopyrightText: 2007-2008 Robert Knight <robertknight@gmail.com>
0004     SPDX-FileCopyrightText: 1997, 1998 Lars Doelle <lars.doelle@on-line.de>
0005 
0006     SPDX-License-Identifier: GPL-2.0-or-later
0007 */
0008 
0009 // Own
0010 #include "TerminalColor.h"
0011 
0012 // Konsole
0013 #include "colorscheme/ColorScheme.h"
0014 
0015 // Qt
0016 #include <QApplication>
0017 #include <QPalette>
0018 #include <QTimer>
0019 
0020 namespace Konsole
0021 {
0022 TerminalColor::TerminalColor(QObject *parent)
0023     : QObject(parent)
0024 {
0025     setColorTable(ColorScheme::defaultTable);
0026 }
0027 
0028 void TerminalColor::applyProfile(const Profile::Ptr &profile, const std::shared_ptr<const ColorScheme> &colorScheme, uint randomSeed)
0029 {
0030     QColor table[TABLE_COLORS];
0031     colorScheme->getColorTable(table, randomSeed);
0032     setColorTable(table);
0033     setOpacity(colorScheme->opacity());
0034 
0035     m_cursorColor = profile->useCustomCursorColor() ? profile->customCursorColor() : QColor();
0036     m_cursorTextColor = profile->useCustomCursorColor() ? profile->customCursorTextColor() : QColor();
0037 }
0038 
0039 QColor TerminalColor::backgroundColor() const
0040 {
0041     return m_colorTable[DEFAULT_BACK_COLOR];
0042 }
0043 
0044 QColor TerminalColor::foregroundColor() const
0045 {
0046     return m_colorTable[DEFAULT_FORE_COLOR];
0047 }
0048 
0049 void TerminalColor::setColorTable(const QColor *table)
0050 {
0051     std::copy(table, table + TABLE_COLORS, m_colorTable);
0052     setBackgroundColor(m_colorTable[DEFAULT_BACK_COLOR]);
0053     onColorsChanged();
0054 }
0055 
0056 const QColor *TerminalColor::colorTable() const
0057 {
0058     return m_colorTable;
0059 }
0060 
0061 void TerminalColor::setOpacity(qreal opacity)
0062 {
0063     QColor color(m_blendColor);
0064     color.setAlphaF(opacity);
0065     m_opacity = opacity;
0066 
0067     m_blendColor = color.rgba();
0068     onColorsChanged();
0069 }
0070 
0071 void TerminalColor::visualBell()
0072 {
0073     swapFGBGColors();
0074     QTimer::singleShot(200, this, &TerminalColor::swapFGBGColors);
0075 }
0076 
0077 qreal TerminalColor::opacity() const
0078 {
0079     return m_opacity;
0080 }
0081 
0082 QRgb TerminalColor::blendColor() const
0083 {
0084     return m_blendColor;
0085 }
0086 
0087 void TerminalColor::setCursorColor(const QColor &color)
0088 {
0089     m_cursorColor = color;
0090     onColorsChanged();
0091 }
0092 
0093 void TerminalColor::setBackgroundColor(const QColor &color)
0094 {
0095     m_colorTable[DEFAULT_BACK_COLOR] = color;
0096     onColorsChanged();
0097 }
0098 
0099 void TerminalColor::setForegroundColor(const QColor &color)
0100 {
0101     m_colorTable[DEFAULT_FORE_COLOR] = color;
0102     onColorsChanged();
0103 }
0104 
0105 void TerminalColor::onColorsChanged()
0106 {
0107     QPalette palette = QApplication::palette();
0108 
0109     QColor buttonTextColor = m_colorTable[DEFAULT_FORE_COLOR];
0110     QColor backgroundColor = m_colorTable[DEFAULT_BACK_COLOR];
0111     backgroundColor.setAlphaF(m_opacity);
0112 
0113     QColor buttonColor = backgroundColor.toHsv();
0114     if (buttonColor.valueF() < 0.5) {
0115         buttonColor = buttonColor.lighter();
0116     } else {
0117         buttonColor = buttonColor.darker();
0118     }
0119     palette.setColor(QPalette::Button, buttonColor);
0120     palette.setColor(QPalette::Window, backgroundColor);
0121     palette.setColor(QPalette::Base, backgroundColor);
0122     palette.setColor(QPalette::WindowText, buttonTextColor);
0123     palette.setColor(QPalette::ButtonText, buttonTextColor);
0124 
0125     QWidget *widget = qobject_cast<QWidget *>(parent());
0126 
0127     widget->setPalette(palette);
0128 
0129     Q_EMIT onPalette(palette);
0130 
0131     widget->update();
0132 }
0133 
0134 void TerminalColor::swapFGBGColors()
0135 {
0136     QColor color = m_colorTable[DEFAULT_BACK_COLOR];
0137     m_colorTable[DEFAULT_BACK_COLOR] = m_colorTable[DEFAULT_FORE_COLOR];
0138     m_colorTable[DEFAULT_FORE_COLOR] = color;
0139 
0140     onColorsChanged();
0141 }
0142 
0143 }
0144 
0145 #include "moc_TerminalColor.cpp"