Debian Codegraph @

kysdk-qtwidgets/src/kaddbutton.cpp

raw
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: Sisheng He <[email protected]>
20 *
21 */
22 #include "kaddbutton.h"
23 #include "themeController.h"
24 #include <QApplication>
25 #include <QDBusInterface>
26 #include <QDBusReply>
27 #include <QDebug>
28 #include <QEvent>
29 #include <QGSettings/QGSettings>
30 #include <QHBoxLayout>
31 #include <QLabel>
32 #include <QPainter>
33
34 #define TOP_LEFT_CORNER 1
35 #define TOP_RIGHT_CORNER 2
36 #define BOTTOM_RIGHT_CORNER 4
37 #define BOTTOM_LEFT_CORNER 8
38
39 namespace kdk
40 {
41 class KAddButtonPrivate : public QObject, public ThemeController
42 {
43 Q_OBJECT
44 Q_DECLARE_PUBLIC(KAddButton)
45 public:
46 KAddButtonPrivate(KAddButton *parent);
47
48 private:
49 KAddButton *q_ptr;
50
51 KBaseFrame::BorderRadiusStyle m_radiusType;
52 bool m_isTabletMode = false;
53 QDBusInterface *m_statusSessionDbus = nullptr;
54 bool m_heightAdaptive = false;
55
56 private:
57 QPainterPath getPaintrPath();
58
59 private Q_SLOTS:
60 void mode_change_signal_slots(bool b);
61 };
62
63 KAddButtonPrivate::KAddButtonPrivate(KAddButton *parent)
64 : q_ptr(parent)
65 {
66 Q_Q(KAddButton);
67 setParent(parent);
68
69 QHBoxLayout *addLyt = new QHBoxLayout(q);
70
71 QLabel *iconLabel = new QLabel(q);
72 QLabel *textLabel = new QLabel(tr("Add"));
73
74 QIcon mAddIcon = QIcon::fromTheme("list-add-symbolic");
75 iconLabel->setPixmap(mAddIcon.pixmap(mAddIcon.actualSize(QSize(16, 16))));
76 iconLabel->setProperty("iconHighlightEffectMode", 1);
77 addLyt->addStretch();
78 addLyt->addWidget(iconLabel);
79 addLyt->addWidget(textLabel);
80 addLyt->addStretch();
81 q->setLayout(addLyt);
82
83 if (ThemeController::themeMode() == DarkTheme)
84 iconLabel->setProperty("useIconHighlightEffect", true);
85
86 connect(m_gsetting,&QGSettings::changed,this,[=](QString key){
87 if(key == "styleName")
88 {
89 if (ThemeController::themeMode() == LightTheme)
90 iconLabel->setProperty("useIconHighlightEffect", false);
91 else
92 iconLabel->setProperty("useIconHighlightEffect", true);
93 }
94 });
95 }
96
97 void KAddButtonPrivate::mode_change_signal_slots(bool b)
98 {
99 Q_Q(KAddButton);
100 if (m_heightAdaptive) {
101 if (b) {
102 q->setMinimumSize(550, TABLET_HEIGHT);
103 q->setMaximumSize(16777215, TABLET_HEIGHT);
104 } else {
105 q->setMinimumSize(550, PC_HEIGHT);
106 q->setMaximumSize(16777215, PC_HEIGHT);
107 }
108 }
109 m_isTabletMode = b;
110 Q_EMIT q->tabletModeChanged(b);
111 }
112
113 KAddButton::KAddButton(QWidget *parent, KBaseFrame::BorderRadiusStyle style, bool heightAdaptive)
114 : QPushButton(parent)
115 , d_ptr(new KAddButtonPrivate(this))
116 {
117 Q_D(KAddButton);
118 setObjectName("AddButton");
119 setProperty("useButtonPalette", true);
120 setProperty("needTranslucent", true);
121 setFlat(true);
122
123 d->m_radiusType = style;
124 d->m_heightAdaptive = heightAdaptive;
125 d->m_statusSessionDbus = new QDBusInterface("com.kylin.statusmanager.interface",
126 "/",
127 "com.kylin.statusmanager.interface",
128 QDBusConnection::sessionBus(), this);
129 if (d->m_statusSessionDbus->isValid()) {
130 QDBusReply<bool> is_tabletmode = d->m_statusSessionDbus->call("get_current_tabletmode");
131 d->mode_change_signal_slots(is_tabletmode.isValid() ? is_tabletmode.value() : false);
132 connect(d->m_statusSessionDbus, SIGNAL(mode_change_signal(bool)), this, SLOT(mode_change_signal_slots(bool)));
133 } else {
134 d->mode_change_signal_slots(false);
135 qWarning() << "Create com.kylin.statusmanager.interface Interface Failed When : " << QDBusConnection::systemBus().lastError();
136 }
137 }
138
139 KAddButton::~KAddButton()
140 {
141 }
142
143 void KAddButton::setRadiusType(KBaseFrame::BorderRadiusStyle style)
144 {
145 Q_D(KAddButton);
146 d->m_radiusType = style;
147 update();
148 }
149
150 bool KAddButton::getTabletMode()
151 {
152 Q_D(KAddButton);
153 return d->m_isTabletMode;
154 }
155
156 void KAddButton::setHeightAdaptive(bool flag)
157 {
158 Q_D(KAddButton);
159 d->m_heightAdaptive = flag;
160 }
161
162 void KAddButton::paintEvent(QPaintEvent *event)
163 {
164 Q_D(KAddButton);
165 QPainter painter(this);
166 painter.setRenderHint(QPainter::Antialiasing); // 反锯齿;
167 painter.setClipping(true);
168 painter.setPen(Qt::transparent);
169 // painter.setBrush(qApp->palette().base());
170 painter.setBrush(ThemeController::getCustomColorFromDT("base-active"));
171 QPainterPath painterPath = d->getPaintrPath();
172 painter.setClipPath(painterPath);
173 painter.drawPath(painterPath);
174
175 QPushButton::paintEvent(event);
176 }
177
178 QPainterPath KAddButtonPrivate::getPaintrPath()
179 {
180 Q_Q(KAddButton);
181 const QRect rect = q->rect();
182 int radius = RADIUS_VALUE;
183 int corner = 0;
184
185 if (m_radiusType == KBaseFrame::BorderRadiusStyle::None) {
186 corner = 0;
187 } else if (m_radiusType == KBaseFrame::BorderRadiusStyle::Top) {
188 corner = TOP_LEFT_CORNER + TOP_RIGHT_CORNER;
189 } else if (m_radiusType == KBaseFrame::BorderRadiusStyle::Bottom) {
190 corner = BOTTOM_LEFT_CORNER + BOTTOM_RIGHT_CORNER;
191 } else {
192 corner = TOP_LEFT_CORNER + TOP_RIGHT_CORNER + BOTTOM_LEFT_CORNER + BOTTOM_RIGHT_CORNER;
193 }
194
195 QPainterPath path;
196 if (corner & TOP_LEFT_CORNER) {
197 path.moveTo(rect.x() + radius, rect.y());
198 path.arcTo(rect.x(), rect.y(), radius * 2, radius * 2, 90.f, 90.f); // 画左上圆角
199 } else {
200 path.moveTo(rect.x(), rect.y());
201 }
202
203 if (corner & BOTTOM_LEFT_CORNER) {
204 path.lineTo(rect.x(), rect.y() + rect.height() - radius);
205 path.arcTo(rect.x(), rect.y() + rect.height() - radius * 2, radius * 2, radius * 2, 180.0f, 90.f);
206 } else {
207 path.lineTo(rect.x(), rect.y() + rect.height());
208 }
209
210 if (corner & BOTTOM_RIGHT_CORNER) {
211 path.lineTo(rect.x() + rect.width() - radius, rect.y() + rect.height());
212 path.arcTo(rect.x() + rect.width() - 2 * radius, rect.y() + rect.height() - 2 * radius, radius * 2, radius * 2, 270.0f, 90.f);
213 } else {
214 path.lineTo(rect.x() + rect.width(), rect.y() + rect.height());
215 }
216
217 if (corner & TOP_RIGHT_CORNER) {
218 path.lineTo(rect.x() + rect.width(), rect.y() + radius);
219 path.arcTo(rect.x() + rect.width() - 2 * radius, rect.y(), radius * 2, radius * 2, 0.f, 90.f);
220 } else {
221 path.lineTo(rect.x() + rect.width(), rect.y());
222 }
223 return path;
224 }
225 }
226
227 #include "kaddbutton.moc"
228 #include "moc_kaddbutton.cpp"