| 1 |
/* |
| 2 |
* |
| 3 |
* |
| 4 |
* Copyright (C) 2023, KylinSoft Co., Ltd. |
| 5 |
* |
| 6 |
* This library is free software; you can redistribute it and/or |
| 7 |
* modify it under the terms of the GNU Lesser General Public |
| 8 |
* License as published by the Free Software Foundation; either |
| 9 |
* version 3 of the License, or (at your option) any later version. |
| 10 |
* |
| 11 |
* This library is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 |
* Lesser General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU General Public License |
| 17 |
* along with this library. If not, see <https://www.gnu.org/licenses/>. |
| 18 |
* |
| 19 |
* Authors: Zhen Sun <[email protected]> |
| 20 |
* |
| 21 |
*/ |
| 22 |
|
| 23 |
#include "widget.h" |
| 24 |
#include "kpressbutton.h" |
| 25 |
#include "kpushbutton.h" |
| 26 |
#include "ksearchlineedit.h" |
| 27 |
#include "kslider.h" |
| 28 |
#include "kswitchbutton.h" |
| 29 |
#include <QEvent> |
| 30 |
#include <QLabel> |
| 31 |
#include <QLineEdit> |
| 32 |
#include <QPalette> |
| 33 |
#include <QVBoxLayout> |
| 34 |
#include <QWidget> |
| 35 |
#include <kwindoweffects.h> |
| 36 |
|
| 37 |
using namespace kdk; |
| 38 |
|
| 39 |
Widget::Widget(QWidget *parent) |
| 40 |
: QWidget(parent) |
| 41 |
{ |
| 42 |
setAttribute(Qt::WA_TranslucentBackground); |
| 43 |
|
| 44 |
QVBoxLayout *mainLayout = new QVBoxLayout(this); |
| 45 |
QWidget *mainWidget = new QWidget(this); |
| 46 |
mainWidget->setFocusPolicy(Qt::FocusPolicy::ClickFocus); |
| 47 |
mainWidget->installEventFilter(this); |
| 48 |
QLabel *label1 = new QLabel("KSlider", this); |
| 49 |
label1->move(50, 100); |
| 50 |
KSlider *slider = new KSlider(mainWidget); |
| 51 |
slider->setToolTip("this is a tool tip"); |
| 52 |
slider->setRange(0, 100); |
| 53 |
// 测试接口 |
| 54 |
slider->setTranslucent(true); |
| 55 |
slider->setTickInterval(20); |
| 56 |
slider->setFixedWidth(300); |
| 57 |
slider->move(200, 100); |
| 58 |
|
| 59 |
QLabel *label2 = new QLabel("KSwitchButton", this); |
| 60 |
label2->setFixedWidth(150); |
| 61 |
label2->move(50, 200); |
| 62 |
KSwitchButton *switchbutton = new KSwitchButton(mainWidget); |
| 63 |
// 测试接口 |
| 64 |
switchbutton->setTranslucent(true); |
| 65 |
switchbutton->move(200, 200); |
| 66 |
|
| 67 |
QLabel *label3 = new QLabel("KSearchLineEdit", this); |
| 68 |
label3->move(50, 300); |
| 69 |
KSearchLineEdit *lineEdit = new KSearchLineEdit(mainWidget); |
| 70 |
lineEdit->move(200, 300); |
| 71 |
// 测试接口 |
| 72 |
lineEdit->setTranslucent(true); |
| 73 |
|
| 74 |
QLabel *label4 = new QLabel("KPressButton", this); |
| 75 |
label4->move(50, 400); |
| 76 |
KPressButton *pressBtn1 = new KPressButton(this); |
| 77 |
pressBtn1->setButtonType(KPressButton::CircleType); |
| 78 |
pressBtn1->setFixedSize(40, 40); |
| 79 |
pressBtn1->setIcon(QIcon::fromTheme("ukui-alarm-symbolic")); |
| 80 |
// 测试接口 |
| 81 |
pressBtn1->setTranslucent(true); |
| 82 |
pressBtn1->move(200, 400); |
| 83 |
|
| 84 |
KPressButton *pressBtn2 = new KPressButton(this); |
| 85 |
pressBtn2->setFixedSize(40, 40); |
| 86 |
pressBtn2->setIcon(QIcon::fromTheme("ukui-alarm-symbolic")); |
| 87 |
// 测试接口 |
| 88 |
pressBtn2->setTranslucent(true); |
| 89 |
pressBtn2->move(250, 400); |
| 90 |
|
| 91 |
QLabel *label5 = new QLabel("KPushButton", this); |
| 92 |
label5->move(50, 500); |
| 93 |
|
| 94 |
KPushButton *pushbtn1 = new KPushButton(this); |
| 95 |
pushbtn1->setButtonType(KPushButton::CircleType); |
| 96 |
pushbtn1->setFixedSize(40, 40); |
| 97 |
pushbtn1->setIcon(QIcon::fromTheme("ukui-alarm-symbolic")); |
| 98 |
pushbtn1->setTranslucent(true); |
| 99 |
pushbtn1->move(200, 500); |
| 100 |
|
| 101 |
KPushButton *pushbtn2 = new KPushButton(this); |
| 102 |
pushbtn2->setFixedSize(40, 40); |
| 103 |
pushbtn2->setIcon(QIcon::fromTheme("ukui-alarm-symbolic")); |
| 104 |
pushbtn2->setIconColor(QColor(255, 0, 0)); |
| 105 |
// 测试接口 |
| 106 |
pushbtn2->setTranslucent(true); |
| 107 |
pushbtn2->move(250, 500); |
| 108 |
|
| 109 |
mainLayout->addWidget(mainWidget); |
| 110 |
mainLayout->setContentsMargins(0, 0, 0, 0); |
| 111 |
setFixedSize(800, 600); |
| 112 |
} |
| 113 |
|
| 114 |
Widget::~Widget() |
| 115 |
{ |
| 116 |
} |
| 117 |
|
| 118 |
bool Widget::eventFilter(QObject *watched, QEvent *event) |
| 119 |
{ |
| 120 |
if (event->type() == QEvent::Paint) { |
| 121 |
auto widget = qobject_cast<QWidget *>(watched); |
| 122 |
if (widget) { |
| 123 |
widget->setAutoFillBackground(true); |
| 124 |
|
| 125 |
QPalette palette = this->palette(); |
| 126 |
auto color = this->palette().color(QPalette::Window); |
| 127 |
color.setAlphaF(0.39); |
| 128 |
palette.setColor(QPalette::Window, color); |
| 129 |
widget->setPalette(palette); |
| 130 |
widget->setBackgroundRole(QPalette::Window); |
| 131 |
} |
| 132 |
} |
| 133 |
return QObject::eventFilter(watched, event); |
| 134 |
} |