Debian Codegraph @

kysdk-qtwidgets/src/kpasswordedit.cpp

raw
1 /*
2 * libkysdk-qtwidgets's Library
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 "kpasswordedit.h"
24 #include "ktoolbutton.h"
25 #include "parmscontroller.h"
26 #include "themeController.h"
27 #include <QAction>
28 #include <QDebug>
29 #include <QEvent>
30 #include <QGuiApplication>
31 #include <QHBoxLayout>
32 #include <QPalette>
33 #include <QPushButton>
34 #include <QTimer>
35 #include <QToolButton>
36 #include "accessinfohelper.h"
37
38 namespace kdk
39 {
40
41 class KPasswordEditPrivate : public QObject, public ThemeController
42 {
43 Q_OBJECT
44 Q_DECLARE_PUBLIC(KPasswordEdit)
45 public:
46 KPasswordEditPrivate(KPasswordEdit *parent);
47 void adjustLayout();
48 void repaintIcon();
49
50 protected:
51 void changeTheme();
52
53 private:
54 KPasswordEdit *q_ptr;
55 KToolButton *m_pEchoModeBtn;
56 LoginState m_state;
57 KToolButton *m_pLoadingBtn;
58 KToolButton *m_pClearBtn;
59 QWidget *m_pWidget;
60 QHBoxLayout *m_pWidgetLayout;
61 QTimer *m_pTimer;
62 bool m_isLoading;
63 bool m_hasFocus;
64 bool m_useCustomPalette;
65 int m_flashState;
66 };
67
68 KPasswordEdit::KPasswordEdit(QWidget *parent)
69 : QLineEdit(parent)
70 , d_ptr(new KPasswordEditPrivate(this))
71 {
72 Q_D(KPasswordEdit);
73 d->m_pEchoModeBtn->setType(KToolButtonType::Flat);
74 d->m_pClearBtn->setType(KToolButtonType::Flat);
75 d->m_pLoadingBtn->setType(KToolButtonType::Flat);
76 connect(d->m_pEchoModeBtn, &QToolButton::clicked, this, [=]() {
77 if (echoMode() == QLineEdit::Password)
78 setEchoMode(QLineEdit::Normal);
79 else
80 setEchoMode(QLineEdit::Password);
81 d->repaintIcon();
82 });
83 connect(this, &QLineEdit::textChanged, this, [=]() {
84 if (!text().isEmpty() && d->m_pClearBtn->isEnabled() && hasFocus()) {
85 d->m_pClearBtn->show();
86 d->adjustLayout();
87 } else {
88 d->m_pClearBtn->hide();
89 d->adjustLayout();
90 }
91 });
92 connect(d->m_pClearBtn, &QPushButton::clicked, this, [=]() {
93 this->clear();
94 });
95 connect(d->m_pTimer, &QTimer::timeout, this, [=]() {
96 if (d->m_flashState < 7)
97 d->m_flashState++;
98 else
99 d->m_flashState = 0;
100 d->m_pLoadingBtn->setIcon(QIcon::fromTheme(QString("ukui-loading-%1.symbolic").arg(d->m_flashState)));
101 });
102 connect(d->m_gsetting,&QGSettings::changed,this,[=](QString key){
103 if(key == "styleName" || key == "iconThemeName"||key == "widgetThemeName")
104 {
105 d->changeTheme();
106 }
107 });
108 connect(Parmscontroller::self(), &Parmscontroller::modeChanged, this, [=]() {
109 updateGeometry();
110 });
111 d->repaintIcon();
112 installEventFilter(this);
113 setContextMenuPolicy(Qt::NoContextMenu);
114 setFocusPolicy(Qt::StrongFocus);
115 setAttribute(Qt::WA_InputMethodEnabled, false);
116 d->changeTheme();
117 }
118
119 void KPasswordEdit::setState(LoginState state)
120 {
121 Q_D(KPasswordEdit);
122 d->m_state = state;
123
124 auto palette = this->palette();
125 switch (d->m_state) {
126 case Ordinary:
127 palette.setBrush(QPalette::Highlight, ThemeController::getCustomColorFromDT("kbrand-normal"));
128 setPalette(palette);
129 break;
130 case LoginSuccess:
131 palette.setBrush(QPalette::Highlight, ThemeController::getCustomColorFromDT("ksuccess-normal"));
132 setPalette(palette);
133 break;
134 case LoginFailed:
135 palette.setBrush(QPalette::Highlight, ThemeController::getCustomColorFromDT("kerror-normal"));
136 setPalette(palette);
137 break;
138 default:
139 break;
140 }
141 }
142
143 LoginState KPasswordEdit::state()
144 {
145 Q_D(KPasswordEdit);
146 return d->m_state;
147 }
148
149 void KPasswordEdit::setLoading(bool flag)
150 {
151 Q_D(KPasswordEdit);
152 d->m_isLoading = flag;
153 if (flag) {
154 d->m_pLoadingBtn->show();
155 d->m_pTimer->start();
156 QLineEdit::setEnabled(false);
157 } else {
158 d->m_pLoadingBtn->hide();
159 d->m_pTimer->stop();
160 QLineEdit::setEnabled(true);
161 }
162 d->adjustLayout();
163 }
164
165 bool KPasswordEdit::isLoading()
166 {
167 Q_D(KPasswordEdit);
168 return d->m_isLoading;
169 }
170
171 QString KPasswordEdit::placeholderText()
172 {
173 return QLineEdit::placeholderText();
174 }
175
176 void KPasswordEdit::setPlaceholderText(QString &text)
177 {
178 QLineEdit::setPlaceholderText(text);
179 }
180
181 void KPasswordEdit::setClearButtonEnabled(bool enable)
182 {
183 Q_D(KPasswordEdit);
184 d->m_pClearBtn->setEnabled(enable);
185 }
186
187 bool KPasswordEdit::isClearButtonEnabled() const
188 {
189 bool result = d_ptr->m_pClearBtn->isEnabled();
190 return result;
191 }
192
193 void KPasswordEdit::setEchoModeBtnVisible(bool enable)
194 {
195 Q_D(KPasswordEdit);
196 d->m_pEchoModeBtn->setVisible(enable);
197 d->adjustLayout();
198 }
199
200 bool KPasswordEdit::echoModeBtnVisible()
201 {
202 Q_D(KPasswordEdit);
203 return d->m_pEchoModeBtn->isVisible();
204 }
205
206 void KPasswordEdit::setClearBtnVisible(bool enable)
207 {
208 Q_D(KPasswordEdit);
209 d->m_pClearBtn->setEnabled(enable);
210 d->adjustLayout();
211 }
212
213 bool KPasswordEdit::clearBtnVisible()
214 {
215 Q_D(KPasswordEdit);
216 return d->m_pClearBtn->isVisible();
217 }
218
219 void KPasswordEdit::setEnabled(bool flag)
220 {
221 Q_D(KPasswordEdit);
222 if (!flag) {
223 d->m_pClearBtn->hide();
224 d->m_pLoadingBtn->hide();
225 }
226 QLineEdit::setEnabled(flag);
227 d->repaintIcon();
228 }
229
230 void KPasswordEdit::setEchoMode(QLineEdit::EchoMode mode)
231 {
232 Q_D(KPasswordEdit);
233 QLineEdit::setEchoMode(mode);
234 d->repaintIcon();
235 }
236
237 void KPasswordEdit::setUseCustomPalette(bool flag)
238 {
239 Q_D(KPasswordEdit);
240 d->m_useCustomPalette = flag;
241 }
242
243 void KPasswordEdit::resizeEvent(QResizeEvent *event)
244 {
245 Q_D(KPasswordEdit);
246
247 QLineEdit::resizeEvent(event);
248 d->adjustLayout();
249 }
250
251 bool KPasswordEdit::eventFilter(QObject *watched, QEvent *event)
252 {
253 Q_D(KPasswordEdit);
254 if (watched == this) {
255 if (event->type() == QEvent::FocusIn) {
256 d->m_hasFocus = true;
257 if (this->text().isEmpty())
258 d->m_pClearBtn->hide();
259 else {
260 if (d->m_pClearBtn->isEnabled())
261 d->m_pClearBtn->show();
262 }
263 d->adjustLayout();
264 d->changeTheme();
265 }
266 if (event->type() == QEvent::FocusOut) {
267 d->m_hasFocus = false;
268 d->m_pClearBtn->hide();
269 d->adjustLayout();
270 d->changeTheme();
271 }
272 }
273 return QLineEdit::eventFilter(watched, event);
274 }
275
276 QSize KPasswordEdit::sizeHint() const
277 {
278 auto size = QLineEdit::sizeHint();
279 size.setHeight(Parmscontroller::parm(Parmscontroller::Parm::PM_PasswordEditHeight));
280 return size;
281 }
282
283 KPasswordEditPrivate::KPasswordEditPrivate(KPasswordEdit *parent)
284 : q_ptr(parent)
285 , m_useCustomPalette(false)
286 {
287 Q_Q(KPasswordEdit);
288 m_flashState = 0;
289 m_state = Ordinary;
290 m_hasFocus = q->hasFocus();
291 m_isLoading = false;
292 m_pTimer = new QTimer(this);
293 m_pTimer->setInterval(100);
294 q->QLineEdit::setEchoMode(QLineEdit::Password);
295
296 QPalette btnPalette;
297 btnPalette.setBrush(QPalette::Active, QPalette::Button, Qt::transparent);
298 btnPalette.setBrush(QPalette::Inactive, QPalette::Button, Qt::transparent);
299 btnPalette.setBrush(QPalette::Disabled, QPalette::Button, Qt::transparent);
300
301 btnPalette.setBrush(QPalette::Active, QPalette::Highlight, Qt::transparent);
302 btnPalette.setBrush(QPalette::Inactive, QPalette::Highlight, Qt::transparent);
303 btnPalette.setBrush(QPalette::Disabled, QPalette::Highlight, Qt::transparent);
304
305 m_pEchoModeBtn = new KToolButton(q);
306 m_pEchoModeBtn->setAutoFillBackground(true);
307 m_pEchoModeBtn->setPalette(btnPalette);
308 m_pEchoModeBtn->setType(KToolButtonType::Background);
309 m_pEchoModeBtn->setIconSize(QSize(16, 16));
310 m_pEchoModeBtn->setFixedSize(QSize(16, 16));
311 m_pEchoModeBtn->setFocusPolicy(Qt::NoFocus);
312 m_pEchoModeBtn->setCursor(Qt::ArrowCursor);
313 m_pEchoModeBtn->setIcon(QIcon::fromTheme("ukui-eye-hidden-symbolic"));
314 m_pLoadingBtn = new KToolButton(q);
315 m_pLoadingBtn->setAutoFillBackground(true);
316 m_pLoadingBtn->setPalette(btnPalette);
317 m_pLoadingBtn->setType(KToolButtonType::Background);
318 m_pLoadingBtn->setIconSize(QSize(16, 16));
319 m_pLoadingBtn->setFixedSize(QSize(16, 16));
320 m_pLoadingBtn->setFocusPolicy(Qt::NoFocus);
321 m_pLoadingBtn->setCursor(Qt::ArrowCursor);
322 m_pLoadingBtn->setIcon(QIcon::fromTheme("ukui-loading-0"));
323 m_pLoadingBtn->hide();
324 m_pClearBtn = new KToolButton(q);
325 m_pClearBtn->setAutoFillBackground(true);
326 m_pClearBtn->setPalette(btnPalette);
327 m_pClearBtn->setType(KToolButtonType::Background);
328 m_pClearBtn->setIconSize(QSize(16, 16));
329 m_pClearBtn->setFixedSize(QSize(16, 16));
330 m_pClearBtn->setFocusPolicy(Qt::NoFocus);
331 m_pClearBtn->setCursor(Qt::ArrowCursor);
332 m_pClearBtn->setIcon(QIcon::fromTheme("application-exit-symbolic"));
333 m_pClearBtn->setVisible(false);
334 m_pWidget = new QWidget(q);
335 m_pWidgetLayout = new QHBoxLayout(m_pWidget);
336 m_pWidgetLayout->setContentsMargins(0, 0, 0, 0);
337 m_pWidgetLayout->setSpacing(5);
338 if (ThemeController::systemLang())
339 m_pWidget->setLayoutDirection(Qt::RightToLeft);
340 else
341 m_pWidget->setLayoutDirection(Qt::LeftToRight);
342 m_pWidgetLayout->addWidget(m_pLoadingBtn);
343 m_pWidgetLayout->addWidget(m_pClearBtn);
344 m_pWidgetLayout->addWidget(m_pEchoModeBtn);
345 m_pWidgetLayout->addSpacing(5);
346 m_pWidget->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
347 setParent(parent);
348
349 KDK_ALL_INFO_FORMAT(m_pEchoModeBtn,"");
350 KDK_ALL_INFO_FORMAT(m_pLoadingBtn,"");
351 KDK_ALL_INFO_FORMAT(m_pClearBtn,"");
352 KDK_ALL_INFO_FORMAT(m_pWidget,"");
353 }
354
355 void KPasswordEditPrivate::adjustLayout()
356 {
357 Q_Q(KPasswordEdit);
358 int spacing = 5;
359 int width = spacing;
360 if (!m_pEchoModeBtn->isHidden())
361 width += (spacing + m_pEchoModeBtn->iconSize().width());
362 if (!m_pClearBtn->isHidden())
363 width += (spacing + m_pClearBtn->iconSize().width());
364 if (!m_pLoadingBtn->isHidden())
365 width += (spacing + m_pLoadingBtn->iconSize().width());
366 m_pWidget->setFixedSize(width, q->height());
367 if (ThemeController::systemLang()) {
368 m_pWidget->move(spacing, 0);
369 q->setTextMargins(m_pWidget->width(), 0, 0, 0);
370 } else {
371 m_pWidget->move(q->width() - m_pWidget->width(), 0);
372 q->setTextMargins(0, 0, m_pWidget->width(), 0);
373 }
374 }
375
376 void KPasswordEditPrivate::repaintIcon()
377 {
378 Q_Q(KPasswordEdit);
379 if (ThemeController::themeMode() == LightTheme) {
380 m_pClearBtn->setIcon(drawColoredPixmap(QIcon::fromTheme("application-exit-symbolic").pixmap(16, 16), ThemeController::getCustomColorFromDT("buttontext-active")));
381 if (q->echoMode() == QLineEdit::Password)
382 m_pEchoModeBtn->setIcon(drawColoredPixmap(QIcon::fromTheme("ukui-eye-hidden-symbolic").pixmap(16, 16), ThemeController::getCustomColorFromDT("buttontext-active")));
383 else
384 m_pEchoModeBtn->setIcon(drawColoredPixmap(QIcon::fromTheme("ukui-eye-display-symbolic").pixmap(16, 16), ThemeController::getCustomColorFromDT("buttontext-active")));
385 } else {
386 m_pClearBtn->setIcon(drawColoredPixmap(QIcon::fromTheme("application-exit-symbolic").pixmap(16, 16), ThemeController::getCustomColorFromDT("buttontext-active")));
387 if (q->echoMode() == QLineEdit::Password) {
388 if (q->isEnabled())
389 m_pEchoModeBtn->setIcon(drawColoredPixmap(QIcon::fromTheme("ukui-eye-hidden-symbolic").pixmap(16, 16), ThemeController::getCustomColorFromDT("buttontext-active")));
390 else
391 m_pEchoModeBtn->setIcon(drawColoredPixmap(QIcon::fromTheme("ukui-eye-hidden-symbolic").pixmap(16, 16), ThemeController::getCustomColorFromDT("kgray-17")));
392 } else {
393 if (q->isEnabled())
394 m_pEchoModeBtn->setIcon(drawColoredPixmap(QIcon::fromTheme("ukui-eye-display-symbolic").pixmap(16, 16), ThemeController::getCustomColorFromDT("buttontext-active")));
395 else
396 m_pEchoModeBtn->setIcon(drawColoredPixmap(QIcon::fromTheme("ukui-eye-display-symbolic").pixmap(16, 16), ThemeController::getCustomColorFromDT("kgray-17")));
397 }
398 }
399 }
400
401 void KPasswordEditPrivate::changeTheme()
402 {
403 Q_Q(KPasswordEdit);
404 initThemeStyle();
405 if (m_useCustomPalette)
406 return;
407 repaintIcon();
408 QPalette palette = q->palette();
409 if (q->hasFocus()) {
410 QColor color = ThemeController::getCustomColorFromDT("base-active");
411 palette.setBrush(QPalette::Button, color);
412 if (ThemeController::themeMode() == LightTheme)
413 palette.setBrush(QPalette::Text, ThemeController::getCustomColorFromDT("kgray-17"));
414 else
415 palette.setBrush(QPalette::Active, QPalette::Text, ThemeController::getCustomColorFromDT("kwhite"));
416 q->setPalette(palette);
417 } else {
418 if (ThemeController::themeMode() == LightTheme) {
419 palette.setBrush(QPalette::Active, QPalette::Button, ThemeController::getCustomColorFromDT("kgray-6"));
420 palette.setBrush(QPalette::Inactive, QPalette::Button, ThemeController::getCustomColorFromDT("kgray-6"));
421 palette.setBrush(QPalette::Disabled, QPalette::Button, ThemeController::getCustomColorFromDT("kgray-5"));
422 palette.setBrush(QPalette::Active, QPalette::Text, QColor(140, 140, 140));
423 palette.setBrush(QPalette::Inactive, QPalette::Text, QColor(140, 140, 140));
424 palette.setBrush(QPalette::Disabled, QPalette::Text, ThemeController::getCustomColorFromDT("kgray-11"));
425 q->setPalette(palette);
426 } else {
427 palette.setBrush(QPalette::Active, QPalette::Button, ThemeController::getCustomColorFromDT("kgray-16"));
428 palette.setBrush(QPalette::Inactive, QPalette::Button, ThemeController::getCustomColorFromDT("kgray-16"));
429 palette.setBrush(QPalette::Disabled, QPalette::Button, ThemeController::getCustomColorFromDT("kgray-17"));
430 palette.setBrush(QPalette::Active, QPalette::Text, ThemeController::getCustomColorFromDT("kgray-13"));
431 palette.setBrush(QPalette::Inactive, QPalette::Text, ThemeController::getCustomColorFromDT("kgray-13"));
432 palette.setBrush(QPalette::Disabled, QPalette::Text, ThemeController::getCustomColorFromDT("kgray-15"));
433 q->setPalette(palette);
434 }
435 }
436 }
437 }
438
439 #include "kpasswordedit.moc"
440 #include "moc_kpasswordedit.cpp"