File indexing completed on 2024-03-24 04:02:19
0001 /* 0002 This file is part of the KDE libraries 0003 SPDX-FileCopyrightText: 2013 Nicolás Alvarez <nicolas.alvarez@gmail.com> 0004 0005 SPDX-License-Identifier: LGPL-2.1-or-later 0006 */ 0007 0008 #include <optional> 0009 0010 #include <QApplication> 0011 #include <QColorDialog> 0012 #include <QDebug> 0013 #include <QPaintEvent> 0014 #include <QPainter> 0015 #include <QPushButton> 0016 #include <QScopeGuard> 0017 #include <QSlider> 0018 #include <QVBoxLayout> 0019 #include <QWidget> 0020 0021 #include <kwindoweffects.h> 0022 0023 class ContrastTestWindow : public QWidget 0024 { 0025 public: 0026 ContrastTestWindow(); 0027 0028 void resizeEvent(QResizeEvent *) override; 0029 0030 private: 0031 QPushButton *m_btnNothing; 0032 QPushButton *m_btnFullWindow; 0033 QPushButton *m_btnRect; 0034 QPushButton *m_btnEllipse; 0035 0036 QPushButton *m_blur; 0037 bool m_doBlur; 0038 0039 QPushButton *m_bg; 0040 QColor m_bgColour; 0041 0042 QSlider *m_contSlider; 0043 QSlider *m_intSlider; 0044 QSlider *m_satSlider; 0045 QWidget *m_area; 0046 0047 qreal m_contrast; 0048 qreal m_intensity; 0049 qreal m_saturation; 0050 0051 enum { Nothing, FullWindow, Rect, Ellipse } m_state; 0052 0053 void disableContrast(); 0054 void enableContrast(); 0055 void enableContrastRect(); 0056 void enableContrastEllipse(); 0057 void updateContrast(int contrast); 0058 void updateIntensity(int contrast); 0059 void updateSaturation(int contrast); 0060 void update(); 0061 0062 void paintEvent(QPaintEvent *event) override 0063 { 0064 Q_UNUSED(event) 0065 0066 QPainter p(this); 0067 p.setPen(Qt::transparent); 0068 p.setBrush(m_bgColour); 0069 p.drawRect(this->rect()); 0070 0071 QWidget::paintEvent(event); 0072 } 0073 }; 0074 0075 ContrastTestWindow::ContrastTestWindow() 0076 { 0077 m_state = Nothing; 0078 m_contrast = 1; 0079 m_intensity = 1; 0080 m_saturation = 1; 0081 m_bgColour = Qt::transparent; 0082 setAttribute(Qt::WA_TranslucentBackground); 0083 setAttribute(Qt::WA_NoSystemBackground, true); 0084 0085 m_btnNothing = new QPushButton("Nothing"); 0086 m_btnFullWindow = new QPushButton("Full window"); 0087 m_btnRect = new QPushButton("Rectangle"); 0088 m_btnEllipse = new QPushButton("Ellipse"); 0089 m_blur = new QPushButton("Enable Blur"); 0090 m_blur->setCheckable(true); 0091 m_bg = new QPushButton("Set Background Colour..."); 0092 connect(m_bg, &QPushButton::pressed, this, [this]() { 0093 m_bgColour = QColorDialog::getColor(Qt::white, nullptr, "pick colour", QColorDialog::ShowAlphaChannel); 0094 0095 repaint(); 0096 }); 0097 0098 connect(m_blur, &QPushButton::toggled, this, [this](bool checked) { 0099 m_blur->setText(checked ? "Disable Blur" : "Enable Blur"); 0100 m_doBlur = checked; 0101 0102 update(); 0103 }); 0104 0105 m_contSlider = new QSlider(); 0106 m_contSlider->setMaximum(200); 0107 m_contSlider->setValue(100); 0108 m_contSlider->setOrientation(Qt::Horizontal); 0109 0110 m_intSlider = new QSlider(); 0111 m_intSlider->setMaximum(200); 0112 m_intSlider->setValue(100); 0113 m_intSlider->setOrientation(Qt::Horizontal); 0114 0115 m_satSlider = new QSlider(); 0116 m_satSlider->setMaximum(200); 0117 m_satSlider->setValue(100); 0118 m_satSlider->setOrientation(Qt::Horizontal); 0119 0120 m_area = new QWidget; 0121 m_area->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); 0122 0123 connect(m_btnNothing, &QPushButton::clicked, this, &ContrastTestWindow::disableContrast); 0124 connect(m_btnFullWindow, &QPushButton::clicked, this, &ContrastTestWindow::enableContrast); 0125 connect(m_btnRect, &QPushButton::clicked, this, &ContrastTestWindow::enableContrastRect); 0126 connect(m_btnEllipse, &QPushButton::clicked, this, &ContrastTestWindow::enableContrastEllipse); 0127 0128 connect(m_contSlider, &QSlider::valueChanged, this, &ContrastTestWindow::updateContrast); 0129 connect(m_intSlider, &QSlider::valueChanged, this, &ContrastTestWindow::updateIntensity); 0130 connect(m_satSlider, &QSlider::valueChanged, this, &ContrastTestWindow::updateSaturation); 0131 0132 QVBoxLayout *layout = new QVBoxLayout(this); 0133 layout->addWidget(m_btnNothing); 0134 layout->addWidget(m_btnFullWindow); 0135 layout->addWidget(m_btnRect); 0136 layout->addWidget(m_btnEllipse); 0137 layout->addWidget(m_contSlider); 0138 layout->addWidget(m_intSlider); 0139 layout->addWidget(m_satSlider); 0140 layout->addWidget(m_area); 0141 layout->addWidget(m_blur); 0142 layout->addWidget(m_bg); 0143 0144 winId(); // force creation of the associated window 0145 } 0146 0147 void ContrastTestWindow::update() 0148 { 0149 const auto s = qScopeGuard([this]() { 0150 repaint(); 0151 }); 0152 0153 if (m_state == Nothing) { 0154 KWindowEffects::enableBackgroundContrast(windowHandle(), false); 0155 KWindowEffects::enableBlurBehind(windowHandle(), false); 0156 } 0157 0158 auto region = QRegion(); 0159 switch (m_state) { 0160 case Nothing: 0161 case FullWindow: 0162 break; 0163 case Rect: 0164 region = m_area->geometry(); 0165 break; 0166 case Ellipse: 0167 region = QRegion(m_area->geometry(), QRegion::Ellipse); 0168 break; 0169 } 0170 0171 KWindowEffects::enableBlurBehind(windowHandle(), m_doBlur, region); 0172 0173 KWindowEffects::enableBackgroundContrast(windowHandle(), true, m_contrast, m_intensity, m_saturation, region); 0174 0175 repaint(); 0176 } 0177 0178 void ContrastTestWindow::disableContrast() 0179 { 0180 m_state = Nothing; 0181 0182 update(); 0183 } 0184 void ContrastTestWindow::enableContrast() 0185 { 0186 m_state = FullWindow; 0187 0188 update(); 0189 } 0190 void ContrastTestWindow::enableContrastRect() 0191 { 0192 m_state = Rect; 0193 0194 update(); 0195 } 0196 void ContrastTestWindow::enableContrastEllipse() 0197 { 0198 m_state = Ellipse; 0199 0200 update(); 0201 } 0202 0203 void ContrastTestWindow::updateContrast(int contrast) 0204 { 0205 m_contrast = (qreal)contrast / 100; 0206 0207 update(); 0208 } 0209 0210 void ContrastTestWindow::updateIntensity(int contrast) 0211 { 0212 m_intensity = (qreal)contrast / 100; 0213 0214 update(); 0215 } 0216 0217 void ContrastTestWindow::updateSaturation(int contrast) 0218 { 0219 m_saturation = (qreal)contrast / 100; 0220 0221 update(); 0222 } 0223 0224 void ContrastTestWindow::resizeEvent(QResizeEvent *) 0225 { 0226 update(); 0227 } 0228 0229 int main(int argc, char **argv) 0230 { 0231 QApplication app(argc, argv); 0232 0233 ContrastTestWindow wnd; 0234 wnd.show(); 0235 0236 return app.exec(); 0237 }