File indexing completed on 2025-01-19 04:23:25
0001 #include "customcolorscheme.h" 0002 #include "ColorScheme.h" 0003 #include <QDebug> 0004 #include <QCoreApplication> 0005 #include <QTimer> 0006 0007 CustomColorScheme::CustomColorScheme(QObject *parent) : QObject(parent) 0008 ,m_scheme(new Konsole::ColorScheme(this)) 0009 ,m_timer(new QTimer(this)) 0010 { 0011 m_timer->setInterval(100); 0012 m_timer->setSingleShot(true); 0013 0014 connect(this, &CustomColorScheme::descriptionChanged, [this](QString description) 0015 { 0016 m_scheme->setDescription(description); 0017 }); 0018 connect(this, &CustomColorScheme::backgroundColorChanged, [this](QColor ) 0019 { 0020 m_timer->start(); 0021 }); 0022 0023 connect(this, &CustomColorScheme::foregroundColorChanged, [this](QColor ) 0024 { 0025 m_timer->start(); 0026 }); 0027 0028 connect(this, &CustomColorScheme::color2Changed, [this](QColor ) 0029 { 0030 m_timer->start(); 0031 }); 0032 0033 connect(this, &CustomColorScheme::color3Changed, [this](QColor ) 0034 { 0035 m_timer->start(); 0036 }); 0037 0038 connect(this, &CustomColorScheme::color4Changed, [this](QColor ) 0039 { 0040 m_timer->start(); 0041 }); 0042 0043 connect(this, &CustomColorScheme::color5Changed, [this](QColor ) 0044 { 0045 m_timer->start(); 0046 }); 0047 0048 connect(this, &CustomColorScheme::color6Changed, [this](QColor ) 0049 { 0050 m_timer->start(); 0051 }); 0052 0053 connect(this, &CustomColorScheme::color7Changed, [this](QColor ) 0054 { 0055 m_timer->start(); 0056 }); 0057 0058 connect(this, &CustomColorScheme::color8Changed, [this](QColor ) 0059 { 0060 m_timer->start(); 0061 }); 0062 0063 connect(this, &CustomColorScheme::color9Changed, [this](QColor ) 0064 { 0065 m_timer->start(); 0066 }); 0067 0068 connect(m_timer, &QTimer::timeout, [this]() 0069 { 0070 save(); 0071 }); 0072 } 0073 0074 void CustomColorScheme::classBegin() 0075 { 0076 } 0077 0078 void CustomColorScheme::componentComplete() 0079 { 0080 save(); 0081 } 0082 0083 QString CustomColorScheme::name() const 0084 { 0085 return m_name; 0086 } 0087 0088 QString CustomColorScheme::description() const 0089 { 0090 return m_description; 0091 } 0092 0093 QColor CustomColorScheme::backgroundColor() const 0094 { 0095 return m_backgroundColor; 0096 } 0097 0098 QColor CustomColorScheme::foregroundColor() const 0099 { 0100 return m_foregroundColor; 0101 } 0102 0103 0104 QColor CustomColorScheme::color2() const 0105 { 0106 return m_color2; 0107 } 0108 0109 QColor CustomColorScheme::color3() const 0110 { 0111 return m_color3; 0112 } 0113 0114 QColor CustomColorScheme::color4() const 0115 { 0116 return m_color4; 0117 } 0118 0119 QColor CustomColorScheme::color5() const 0120 { 0121 return m_color5; 0122 } 0123 0124 QColor CustomColorScheme::color6() const 0125 { 0126 return m_color6; 0127 } 0128 0129 QColor CustomColorScheme::color7() const 0130 { 0131 return m_color7; 0132 } 0133 0134 QColor CustomColorScheme::color8() const 0135 { 0136 return m_color8; 0137 } 0138 0139 QColor CustomColorScheme::color9() const 0140 { 0141 return m_color9; 0142 } 0143 0144 const Konsole::ColorScheme *CustomColorScheme::getScheme() 0145 { 0146 return m_scheme; 0147 } 0148 0149 void CustomColorScheme::setDescription(QString description) 0150 { 0151 if (m_description == description) 0152 return; 0153 0154 m_description = description; 0155 Q_EMIT descriptionChanged(m_description); 0156 } 0157 0158 void CustomColorScheme::setBackgroundColor(QColor backgroundColor) 0159 { 0160 if (m_backgroundColor == backgroundColor) 0161 return; 0162 0163 m_backgroundColor = backgroundColor; 0164 Q_EMIT backgroundColorChanged(m_backgroundColor); 0165 } 0166 0167 void CustomColorScheme::setForegroundColor(QColor foregroundColor) 0168 { 0169 if (m_foregroundColor == foregroundColor) 0170 return; 0171 0172 m_foregroundColor = foregroundColor; 0173 Q_EMIT foregroundColorChanged(m_foregroundColor); 0174 } 0175 0176 void CustomColorScheme::setColor2(QColor color2) 0177 { 0178 if (m_color2 == color2) 0179 return; 0180 0181 m_color2 = color2; 0182 Q_EMIT color2Changed(m_color2); 0183 } 0184 0185 void CustomColorScheme::setColor3(QColor color3) 0186 { 0187 if (m_color3 == color3) 0188 return; 0189 0190 m_color3 = color3; 0191 Q_EMIT color3Changed(m_color3); 0192 } 0193 0194 void CustomColorScheme::setColor4(QColor color4) 0195 { 0196 if (m_color4 == color4) 0197 return; 0198 0199 m_color4 = color4; 0200 Q_EMIT color4Changed(m_color4); 0201 } 0202 0203 void CustomColorScheme::setColor5(QColor color5) 0204 { 0205 if (m_color5 == color5) 0206 return; 0207 0208 m_color5 = color5; 0209 Q_EMIT color5Changed(m_color5); 0210 } 0211 0212 void CustomColorScheme::setColor6(QColor color6) 0213 { 0214 if (m_color6 == color6) 0215 return; 0216 0217 m_color6 = color6; 0218 Q_EMIT color6Changed(m_color6); 0219 } 0220 0221 void CustomColorScheme::setColor7(QColor color7) 0222 { 0223 if (m_color7 == color7) 0224 return; 0225 0226 m_color7 = color7; 0227 Q_EMIT color7Changed(m_color7); 0228 } 0229 0230 void CustomColorScheme::setColor8(QColor color8) 0231 { 0232 if (m_color8 == color8) 0233 return; 0234 0235 m_color8 = color8; 0236 Q_EMIT color8Changed(m_color8); 0237 } 0238 0239 void CustomColorScheme::setColor9(QColor color9) 0240 { 0241 if (m_color9 == color9) 0242 return; 0243 0244 m_color9 = color9; 0245 Q_EMIT color9Changed(m_color9); 0246 } 0247 0248 static bool isLight(const QColor &color) 0249 { 0250 auto luma = [](const QColor &color) { 0251 return (0.299 * color.red() + 0.587 * color.green() + 0.114 * color.blue()) / 255; 0252 }; 0253 0254 return luma(color) > 0.5 ? true : false; 0255 } 0256 0257 void CustomColorScheme::save() 0258 { 0259 qDebug() << "Changing color in custom intance"; 0260 0261 const auto light = isLight(m_backgroundColor); 0262 0263 m_scheme->setColor(0, m_foregroundColor); 0264 m_scheme->setColor(1, m_backgroundColor); 0265 m_scheme->setColor(2, m_color2); 0266 m_scheme->setColor(3, m_color3); 0267 m_scheme->setColor(4, m_color4); 0268 m_scheme->setColor(5, m_color5); 0269 m_scheme->setColor(6, m_color6); 0270 m_scheme->setColor(7, m_color7); 0271 m_scheme->setColor(8, m_color8); 0272 m_scheme->setColor(9, m_color9); 0273 0274 m_scheme->setColor(10, light ? QColor(m_foregroundColor).darker(120) : QColor(m_foregroundColor).lighter(120)); 0275 m_scheme->setColor(11, light ? QColor(m_backgroundColor).darker(120) : QColor(m_foregroundColor).lighter(120)); 0276 0277 m_scheme->setColor(12, light ? QColor(m_color2).darker(120) : QColor(m_color2).lighter(120)); 0278 m_scheme->setColor(13, light ? QColor(m_color3).darker(120) : QColor(m_color3).lighter(120)); 0279 m_scheme->setColor(14, light ? QColor(m_color4).darker(120) : QColor(m_color4).lighter(120)); 0280 m_scheme->setColor(15, light ? QColor(m_color5).darker(120) : QColor(m_color5).lighter(120)); 0281 m_scheme->setColor(16, light ? QColor(m_color6).darker(120) : QColor(m_color6).lighter(120)); 0282 m_scheme->setColor(17, light ? QColor(m_color7).darker(120) : QColor(m_color7).lighter(120)); 0283 m_scheme->setColor(18, light ? QColor(m_color8).darker(120) : QColor(m_color8).lighter(120)); 0284 m_scheme->setColor(19, light ? QColor(m_color9).darker(120) : QColor(m_color9).lighter(120)); 0285 } 0286 0287