File indexing completed on 2024-04-21 15:05:42

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_frost;
0037     QColor m_frostColor;
0038 
0039     QPushButton *m_blur;
0040     bool m_doBlur;
0041 
0042     QPushButton *m_bg;
0043     QColor m_bgColour;
0044 
0045     QSlider *m_contSlider;
0046     QSlider *m_intSlider;
0047     QSlider *m_satSlider;
0048     QWidget *m_area;
0049 
0050     qreal m_contrast;
0051     qreal m_intensity;
0052     qreal m_saturation;
0053 
0054     enum { Nothing, FullWindow, Rect, Ellipse } m_state;
0055 
0056     void disableContrast();
0057     void enableContrast();
0058     void enableContrastRect();
0059     void enableContrastEllipse();
0060     void updateContrast(int contrast);
0061     void updateIntensity(int contrast);
0062     void updateSaturation(int contrast);
0063     void update();
0064 
0065     void paintEvent(QPaintEvent *event) override
0066     {
0067         Q_UNUSED(event)
0068 
0069         QPainter p(this);
0070         p.setPen(Qt::transparent);
0071         p.setBrush(m_bgColour);
0072         p.drawRect(this->rect());
0073 
0074         QWidget::paintEvent(event);
0075     }
0076 };
0077 
0078 ContrastTestWindow::ContrastTestWindow()
0079 {
0080     m_state = Nothing;
0081     m_contrast = 1;
0082     m_intensity = 1;
0083     m_saturation = 1;
0084     m_bgColour = Qt::transparent;
0085     setAttribute(Qt::WA_TranslucentBackground);
0086     setAttribute(Qt::WA_NoSystemBackground, true);
0087 
0088     m_btnNothing = new QPushButton("Nothing");
0089     m_btnFullWindow = new QPushButton("Full window");
0090     m_btnRect = new QPushButton("Rectangle");
0091     m_btnEllipse = new QPushButton("Ellipse");
0092     m_frost = new QPushButton("Enable Frost");
0093     m_frost->setCheckable(true);
0094     m_blur = new QPushButton("Enable Blur");
0095     m_blur->setCheckable(true);
0096     m_bg = new QPushButton("Set Background Colour...");
0097     connect(m_bg, &QPushButton::pressed, this, [this]() {
0098         m_bgColour = QColorDialog::getColor(Qt::white, nullptr, "pick colour", QColorDialog::ShowAlphaChannel);
0099 
0100         repaint();
0101     });
0102 
0103     connect(m_frost, &QPushButton::toggled, this, [this](bool checked) {
0104         m_frost->setText(checked ? "Disable Frost" : "Enable Frost");
0105 
0106         if (!checked) {
0107             m_frostColor = QColor();
0108 
0109             update();
0110 
0111             return;
0112         }
0113 
0114         m_frostColor = QColorDialog::getColor(Qt::white, nullptr, "pick colour", QColorDialog::ShowAlphaChannel);
0115 
0116         update();
0117     });
0118     connect(m_blur, &QPushButton::toggled, this, [this](bool checked) {
0119         m_blur->setText(checked ? "Disable Blur" : "Enable Blur");
0120         m_doBlur = checked;
0121 
0122         update();
0123     });
0124 
0125     m_contSlider = new QSlider();
0126     m_contSlider->setMaximum(200);
0127     m_contSlider->setValue(100);
0128     m_contSlider->setOrientation(Qt::Horizontal);
0129 
0130     m_intSlider = new QSlider();
0131     m_intSlider->setMaximum(200);
0132     m_intSlider->setValue(100);
0133     m_intSlider->setOrientation(Qt::Horizontal);
0134 
0135     m_satSlider = new QSlider();
0136     m_satSlider->setMaximum(200);
0137     m_satSlider->setValue(100);
0138     m_satSlider->setOrientation(Qt::Horizontal);
0139 
0140     m_area = new QWidget;
0141     m_area->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
0142 
0143     connect(m_btnNothing, &QPushButton::clicked, this, &ContrastTestWindow::disableContrast);
0144     connect(m_btnFullWindow, &QPushButton::clicked, this, &ContrastTestWindow::enableContrast);
0145     connect(m_btnRect, &QPushButton::clicked, this, &ContrastTestWindow::enableContrastRect);
0146     connect(m_btnEllipse, &QPushButton::clicked, this, &ContrastTestWindow::enableContrastEllipse);
0147 
0148     connect(m_contSlider, &QSlider::valueChanged, this, &ContrastTestWindow::updateContrast);
0149     connect(m_intSlider, &QSlider::valueChanged, this, &ContrastTestWindow::updateIntensity);
0150     connect(m_satSlider, &QSlider::valueChanged, this, &ContrastTestWindow::updateSaturation);
0151 
0152     QVBoxLayout *layout = new QVBoxLayout(this);
0153     layout->addWidget(m_btnNothing);
0154     layout->addWidget(m_btnFullWindow);
0155     layout->addWidget(m_btnRect);
0156     layout->addWidget(m_btnEllipse);
0157     layout->addWidget(m_contSlider);
0158     layout->addWidget(m_intSlider);
0159     layout->addWidget(m_satSlider);
0160     layout->addWidget(m_area);
0161     layout->addWidget(m_frost);
0162     layout->addWidget(m_blur);
0163     layout->addWidget(m_bg);
0164 
0165     winId(); // force creation of the associated window
0166 }
0167 
0168 void ContrastTestWindow::update()
0169 {
0170     const auto s = qScopeGuard([this]() {
0171         repaint();
0172     });
0173 
0174     if (m_state == Nothing) {
0175         KWindowEffects::enableBackgroundContrast(windowHandle(), false);
0176         KWindowEffects::enableBlurBehind(windowHandle(), false);
0177     }
0178 
0179     auto region = QRegion();
0180     switch (m_state) {
0181     case Nothing:
0182     case FullWindow:
0183         break;
0184     case Rect:
0185         region = m_area->geometry();
0186         break;
0187     case Ellipse:
0188         region = QRegion(m_area->geometry(), QRegion::Ellipse);
0189         break;
0190     }
0191 
0192     KWindowEffects::enableBlurBehind(windowHandle(), m_doBlur, region);
0193 
0194     if (!m_frostColor.isValid()) {
0195         KWindowEffects::enableBackgroundContrast(windowHandle(), true, m_contrast, m_intensity, m_saturation, region);
0196     } else {
0197         qWarning() << "frost color";
0198         KWindowEffects::setBackgroundFrost(windowHandle(), m_frostColor, region);
0199     }
0200 
0201     repaint();
0202 }
0203 
0204 void ContrastTestWindow::disableContrast()
0205 {
0206     m_state = Nothing;
0207 
0208     update();
0209 }
0210 void ContrastTestWindow::enableContrast()
0211 {
0212     m_state = FullWindow;
0213 
0214     update();
0215 }
0216 void ContrastTestWindow::enableContrastRect()
0217 {
0218     m_state = Rect;
0219 
0220     update();
0221 }
0222 void ContrastTestWindow::enableContrastEllipse()
0223 {
0224     m_state = Ellipse;
0225 
0226     update();
0227 }
0228 
0229 void ContrastTestWindow::updateContrast(int contrast)
0230 {
0231     m_contrast = (qreal)contrast / 100;
0232 
0233     update();
0234 }
0235 
0236 void ContrastTestWindow::updateIntensity(int contrast)
0237 {
0238     m_intensity = (qreal)contrast / 100;
0239 
0240     update();
0241 }
0242 
0243 void ContrastTestWindow::updateSaturation(int contrast)
0244 {
0245     m_saturation = (qreal)contrast / 100;
0246 
0247     update();
0248 }
0249 
0250 void ContrastTestWindow::resizeEvent(QResizeEvent *)
0251 {
0252     update();
0253 }
0254 
0255 int main(int argc, char **argv)
0256 {
0257     QApplication app(argc, argv);
0258 
0259     ContrastTestWindow wnd;
0260     wnd.show();
0261 
0262     return app.exec();
0263 }