1 /*
2     This file is part of the KDE libraries
3     SPDX-FileCopyrightText: 2013 Nicolás Alvarez <nicolas.alvarez@gmail.com>
4 
5     SPDX-License-Identifier: LGPL-2.1-or-later
6 */
7 
8 #include <QApplication>
9 #include <QPushButton>
10 #include <QVBoxLayout>
11 #include <QWidget>
12 
13 #include <kwindoweffects.h>
14 
15 class BlurTestWindow : public QWidget
16 {
17 public:
18     BlurTestWindow();
19 
20     void resizeEvent(QResizeEvent *) override;
21 
22 private:
23     QPushButton *m_btnNothing;
24     QPushButton *m_btnFullWindow;
25     QPushButton *m_btnRect;
26     QPushButton *m_btnEllipse;
27     QWidget *m_area;
28 
29     enum { Nothing, FullWindow, Rect, Ellipse } m_state;
30 
31     void setWindowAlpha(int alpha);
32 
33     void disableBlur();
34     void enableBlur();
35     void enableBlurRect();
36     void enableBlurEllipse();
37 };
38 
BlurTestWindow()39 BlurTestWindow::BlurTestWindow()
40 {
41     m_state = Nothing;
42     setAttribute(Qt::WA_TranslucentBackground);
43     setAttribute(Qt::WA_NoSystemBackground, false);
44     setWindowAlpha(192);
45 
46     m_btnNothing = new QPushButton("Nothing");
47     m_btnFullWindow = new QPushButton("Full window");
48     m_btnRect = new QPushButton("Rectangle");
49     m_btnEllipse = new QPushButton("Ellipse");
50 
51     m_area = new QWidget;
52     m_area->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
53 
54     connect(m_btnNothing, &QPushButton::clicked, this, &BlurTestWindow::disableBlur);
55     connect(m_btnFullWindow, &QPushButton::clicked, this, &BlurTestWindow::enableBlur);
56     connect(m_btnRect, &QPushButton::clicked, this, &BlurTestWindow::enableBlurRect);
57     connect(m_btnEllipse, &QPushButton::clicked, this, &BlurTestWindow::enableBlurEllipse);
58 
59     QVBoxLayout *layout = new QVBoxLayout(this);
60     layout->addWidget(m_btnNothing);
61     layout->addWidget(m_btnFullWindow);
62     layout->addWidget(m_btnRect);
63     layout->addWidget(m_btnEllipse);
64     layout->addWidget(m_area);
65 
66     winId(); // force creation of the associated window
67 }
68 
disableBlur()69 void BlurTestWindow::disableBlur()
70 {
71     m_state = Nothing;
72     KWindowEffects::enableBlurBehind(windowHandle(), false);
73     repaint();
74 }
enableBlur()75 void BlurTestWindow::enableBlur()
76 {
77     m_state = FullWindow;
78     KWindowEffects::enableBlurBehind(windowHandle(), true);
79     repaint();
80 }
enableBlurRect()81 void BlurTestWindow::enableBlurRect()
82 {
83     m_state = Rect;
84     QRegion rgn(m_area->geometry());
85     KWindowEffects::enableBlurBehind(windowHandle(), true, rgn);
86     repaint();
87 }
enableBlurEllipse()88 void BlurTestWindow::enableBlurEllipse()
89 {
90     m_state = Ellipse;
91     QRegion rgn(m_area->geometry(), QRegion::Ellipse);
92     KWindowEffects::enableBlurBehind(windowHandle(), true, rgn);
93     repaint();
94 }
95 
resizeEvent(QResizeEvent *)96 void BlurTestWindow::resizeEvent(QResizeEvent *)
97 {
98     if (m_state == Rect) {
99         enableBlurRect();
100     } else if (m_state == Ellipse) {
101         enableBlurEllipse();
102     }
103 }
104 
setWindowAlpha(int alpha)105 void BlurTestWindow::setWindowAlpha(int alpha)
106 {
107     QPalette pal = this->palette();
108     QColor windowColor = pal.color(QPalette::Window);
109     windowColor.setAlpha(alpha);
110     pal.setColor(QPalette::Window, windowColor);
111     this->setPalette(pal);
112 }
113 
main(int argc,char ** argv)114 int main(int argc, char **argv)
115 {
116     QApplication app(argc, argv);
117 
118     BlurTestWindow wnd;
119     wnd.show();
120 
121     return app.exec();
122 }
123