Debian Codegraph @

kysdk-qtwidgets/src/ksecurityquestiondialog.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: SiSheng He <[email protected]>
20 *
21 */
22
23 #include "ksecurityquestiondialog.h"
24 #include <QObject>
25 #include <QScrollArea>
26 #include <QScrollBar>
27 #include "accessinfohelper.h"
28
29 #define LABELWIDTH 92
30 #define BUTTONWIDTH 96
31 #define DIALOGWIDTH 424
32 #define DIALOGHEIGHT 476
33
34 namespace kdk
35 {
36
37 class KSecurityQuestionDialogPrivate : public QObject
38 {
39 Q_OBJECT
40 Q_DECLARE_PUBLIC(KSecurityQuestionDialog)
41 public:
42 KSecurityQuestionDialogPrivate(KSecurityQuestionDialog *parent);
43
44 private:
45 KSecurityQuestionDialog *q_ptr;
46 QScrollArea *m_pScrollArea;
47 QVBoxLayout *m_pMainVLayout = nullptr;
48 QLabel *m_pTitleLabel = nullptr;
49 KPushButton *m_pCancelKBtn = nullptr;
50 KPushButton *m_pConfirmKBtn = nullptr;
51 QList<QComboBox *> m_questionComboxList;
52 QList<QLineEdit *> m_questionLineeditList;
53 QList<QLineEdit *> m_answerLineeditList;
54 QList<QLabel *> m_questionLabelList;
55 QList<QLabel *> m_answerLabelList;
56 QList<QLabel *> m_tipsLabelList;
57 QStringList m_questionlist;
58 QWidget *m_pScrollWidget;
59 bool m_isVerify = false;
60
61 public Q_SLOTS:
62 void setQustionCombox();
63 void refreshConfirmBtn();
64 };
65
66 KSecurityQuestionDialog::KSecurityQuestionDialog(QWidget *parent)
67 : KDialog(parent)
68 , d_ptr(new KSecurityQuestionDialogPrivate(this))
69 {
70 Q_D(KSecurityQuestionDialog);
71 setObjectName("KSecurityQuestionDialog");
72 }
73
74 KSecurityQuestionDialog::~KSecurityQuestionDialog()
75 {
76 }
77
78 KPushButton *KSecurityQuestionDialog::cancelButton()
79 {
80 Q_D(KSecurityQuestionDialog);
81 return d->m_pCancelKBtn;
82 }
83
84 KPushButton *KSecurityQuestionDialog::confirmButton()
85 {
86 Q_D(KSecurityQuestionDialog);
87 return d->m_pConfirmKBtn;
88 }
89
90 void KSecurityQuestionDialog::setTitleText(const QString &text)
91 {
92 Q_D(KSecurityQuestionDialog);
93 d->m_pTitleLabel->setText(text);
94 }
95
96 void KSecurityQuestionDialog::addSecurityQuestionItem(const int count, bool mutex)
97 {
98 Q_D(KSecurityQuestionDialog);
99 for (int i = 1; i <= count; i++) {
100 QLabel *questionLabel = new QLabel(tr("Security question%1").arg(i));
101 questionLabel->setFixedWidth(LABELWIDTH);
102 QHBoxLayout *questionHlayout = new QHBoxLayout();
103 questionHlayout->setContentsMargins(0, 0, 0, 0);
104 questionHlayout->addWidget(questionLabel);
105 questionHlayout->addSpacing(16);
106
107 QLabel *answerLabel = new QLabel(tr("Answer"));
108 answerLabel->setFixedWidth(LABELWIDTH);
109 QLineEdit *answerLineEdit = new QLineEdit();
110 answerLineEdit->setPlaceholderText(tr("Required"));
111 answerLineEdit->setFixedHeight(36);
112 QHBoxLayout *answerHlayout = new QHBoxLayout();
113 answerHlayout->setContentsMargins(0, 0, 0, 0);
114 answerHlayout->addWidget(answerLabel);
115 answerHlayout->addSpacing(16);
116 answerHlayout->addWidget(answerLineEdit);
117
118 d->m_answerLineeditList.append(answerLineEdit);
119 d->m_questionLabelList.append(questionLabel);
120 d->m_answerLabelList.append(answerLabel);
121
122 KDK_EXTEND_ALL_INFO_FORMAT(answerLineEdit,"",QString("%1").arg(i),"");
123 KDK_EXTEND_ALL_INFO_FORMAT(questionLabel,"",QString("%1").arg(i),"");
124 KDK_EXTEND_ALL_INFO_FORMAT(answerLabel,"",QString("%1").arg(i),"");
125
126 QVBoxLayout *itemVLayout = new QVBoxLayout;
127 itemVLayout->setContentsMargins(0, 0, 0, 0);
128 itemVLayout->setSpacing(8);
129 itemVLayout->addLayout(questionHlayout);
130 itemVLayout->addLayout(answerHlayout);
131
132 if (d->m_isVerify) {
133 QLineEdit *questionLineedit = new QLineEdit();
134 questionLineedit->setFixedHeight(36);
135 d->m_questionLineeditList.append(questionLineedit);
136
137 KDK_EXTEND_ALL_INFO_FORMAT(questionLineedit,"",QString("%1").arg(i),"");
138
139 questionLineedit->setEnabled(false);
140 QPalette palette = questionLineedit->palette();
141 QColor color = ThemeController::getCustomColorFromDT("text-active");
142 palette.setColor(QPalette::Disabled, QPalette::Text, color);
143 questionLineedit->setPalette(palette);
144 questionHlayout->addWidget(questionLineedit);
145 QLabel *tiplabel = new QLabel();
146 d->m_tipsLabelList.append(tiplabel);
147
148 KDK_EXTEND_ALL_INFO_FORMAT(tiplabel,"",QString("%1").arg(i),"");
149
150 QPalette pe;
151 pe.setColor(QPalette::WindowText, Qt::red);
152 tiplabel->setPalette(pe);
153 QLabel *placeholderLabel = new QLabel();
154 placeholderLabel->setFixedWidth(LABELWIDTH);
155 QHBoxLayout *tipHLayout = new QHBoxLayout();
156 tipHLayout->setContentsMargins(0, 0, 0, 0);
157 tipHLayout->addWidget(placeholderLabel);
158 tipHLayout->addSpacing(16);
159 tipHLayout->addWidget(tiplabel);
160
161 itemVLayout->addLayout(tipHLayout);
162
163 } else {
164 QComboBox *questionCombox = new QComboBox();
165 questionCombox->setFixedHeight(36);
166 d->m_questionComboxList.append(questionCombox);
167
168 KDK_EXTEND_ALL_INFO_FORMAT(questionCombox,"",QString("%1").arg(i),"");
169
170 questionHlayout->addWidget(questionCombox);
171 itemVLayout->addStretch();
172
173 if (mutex) {
174 connect(questionCombox, &QComboBox::currentTextChanged, d, &KSecurityQuestionDialogPrivate::setQustionCombox);
175 }
176 }
177
178 d->m_pMainVLayout->insertLayout(d->m_pMainVLayout->count() - 1, itemVLayout);
179
180 connect(answerLineEdit, &QLineEdit::textChanged, d, &KSecurityQuestionDialogPrivate::refreshConfirmBtn);
181 }
182 }
183
184 void KSecurityQuestionDialog::addSecurityQuestionItem(const int count, bool mutex, bool isVerify)
185 {
186 Q_D(KSecurityQuestionDialog);
187 d->m_isVerify = isVerify;
188 addSecurityQuestionItem(count, mutex);
189 }
190
191 void KSecurityQuestionDialog::initQustionCombox(const QStringList &questionlist)
192 {
193 Q_D(KSecurityQuestionDialog);
194 d->m_questionlist = questionlist;
195 for (int i = 0; i < d->m_questionComboxList.count(); i++) {
196 QComboBox *question = d->m_questionComboxList.at(i);
197 question->blockSignals(true);
198 question->addItems(questionlist);
199 question->blockSignals(false);
200 question->setCurrentIndex(i);
201 }
202
203 for (int i = 0; i < d->m_questionLineeditList.count(); i++) {
204 QLineEdit *question = d->m_questionLineeditList.at(i);
205 if (questionlist.count() > i) {
206 question->setText(questionlist.at(i));
207 }
208 }
209 }
210
211 QLabel *KSecurityQuestionDialog::tipsLabel(const int index)
212 {
213 Q_D(KSecurityQuestionDialog);
214 // questionIndex 最小为0 count最小为1,
215 if (index >= 0 && d->m_tipsLabelList.count() > index) {
216 return d->m_tipsLabelList.at(index);
217 }
218
219 return nullptr;
220 }
221
222 QLabel *KSecurityQuestionDialog::questionLabel(const int questionIndex)
223 {
224 Q_D(KSecurityQuestionDialog);
225 // questionIndex 最小为0 count最小为1,
226 if (questionIndex >= 0 && d->m_questionLabelList.count() > questionIndex) {
227 return d->m_questionLabelList.at(questionIndex);
228 }
229
230 return nullptr;
231 }
232
233 QLabel *KSecurityQuestionDialog::answerLabel(const int answerIndex)
234 {
235 Q_D(KSecurityQuestionDialog);
236 if (answerIndex >= 0 && d->m_answerLabelList.count() > answerIndex) {
237 return d->m_answerLabelList.at(answerIndex);
238 }
239
240 return nullptr;
241 }
242
243 QComboBox *KSecurityQuestionDialog::questionCombox(const int questionIndex)
244 {
245 Q_D(KSecurityQuestionDialog);
246 if (questionIndex >= 0 && d->m_questionComboxList.count() > questionIndex) {
247 return d->m_questionComboxList.at(questionIndex);
248 }
249
250 return nullptr;
251 }
252
253 QLineEdit *KSecurityQuestionDialog::answerLineedit(const int answerIndex)
254 {
255 Q_D(KSecurityQuestionDialog);
256 if (answerIndex >= 0 && d->m_answerLineeditList.count() > answerIndex) {
257 return d->m_answerLineeditList.at(answerIndex);
258 }
259
260 return nullptr;
261 }
262
263 void KSecurityQuestionDialog::keyPressEvent(QKeyEvent *event)
264 {
265 Q_D(KSecurityQuestionDialog);
266 switch (event->key()) {
267 case Qt::Key_Enter:
268 if (d->m_pConfirmKBtn->isEnabled())
269 d->m_pConfirmKBtn->clicked();
270 break;
271 case Qt::Key_Return:
272 if (d->m_pConfirmKBtn->isEnabled())
273 d->m_pConfirmKBtn->clicked();
274 break;
275 default:
276 KDialog::keyPressEvent(event);
277 }
278 }
279
280 KSecurityQuestionDialogPrivate::KSecurityQuestionDialogPrivate(KSecurityQuestionDialog *parent)
281 : q_ptr(parent)
282 {
283 Q_Q(KSecurityQuestionDialog);
284 m_pScrollArea = new QScrollArea(q->mainWidget());
285 m_pScrollArea->setFrameShape(QScrollArea::NoFrame);
286 m_pScrollWidget = new QWidget();
287
288 setParent(parent);
289 q->setWindowIcon("ukui-control-center");
290 q->setWindowTitle(tr("Security Question"));
291
292 m_pTitleLabel = new QLabel(tr("Setting security questions to reset password when forget it, "
293 "please remember answer."));
294 m_pTitleLabel->setWordWrap(true);
295 m_pCancelKBtn = new KPushButton();
296 m_pCancelKBtn->setFocusPolicy(Qt::FocusPolicy::NoFocus);
297 m_pCancelKBtn->setFixedWidth(BUTTONWIDTH);
298 m_pCancelKBtn->setText(tr("Cancel"));
299 m_pConfirmKBtn = new KPushButton();
300 m_pConfirmKBtn->setFixedWidth(BUTTONWIDTH);
301 m_pConfirmKBtn->setText(tr("Save"));
302 m_pConfirmKBtn->setEnabled(false);
303 m_pConfirmKBtn->setFocusPolicy(Qt::FocusPolicy::NoFocus);
304 q->closeButton()->setFocusPolicy(Qt::FocusPolicy::NoFocus);
305
306 QHBoxLayout *hLayout = new QHBoxLayout();
307 hLayout->setContentsMargins(0, 0, 0, 0);
308 hLayout->setSpacing(16);
309 hLayout->addStretch();
310 hLayout->addWidget(m_pCancelKBtn);
311 hLayout->addWidget(m_pConfirmKBtn);
312
313 QVBoxLayout *scrollAreaLayout = new QVBoxLayout();
314 scrollAreaLayout->setContentsMargins(0, 0, 0, 0);
315 scrollAreaLayout->addWidget(m_pScrollArea);
316 m_pScrollArea->setWidgetResizable(true);
317 m_pScrollArea->horizontalScrollBar()->setVisible(false);
318
319 m_pMainVLayout = new QVBoxLayout(m_pScrollWidget);
320 m_pMainVLayout->setContentsMargins(21, 16, 27, 24);
321 m_pMainVLayout->setSpacing(24);
322 m_pMainVLayout->addWidget(m_pTitleLabel);
323 m_pMainVLayout->addLayout(hLayout);
324
325 m_pScrollWidget->setLayout(m_pMainVLayout);
326 m_pScrollArea->setWidget(m_pScrollWidget);
327
328 q->mainWidget()->setLayout(scrollAreaLayout);
329 m_pScrollWidget->setFixedWidth(DIALOGWIDTH);
330 q->setFixedSize(DIALOGWIDTH, DIALOGHEIGHT);
331
332 QObject::connect(m_pCancelKBtn, SIGNAL(clicked(bool)), q, SLOT(reject()));
333
334 QObject::connect(m_pConfirmKBtn, &KPushButton::clicked, this, [=]() {
335 foreach (QLineEdit *tempLineedit, m_answerLineeditList) {
336 if (tempLineedit != nullptr) {
337 tempLineedit->clearFocus();
338 }
339 }
340 });
341
342 KDK_ALL_INFO_FORMAT(m_pScrollArea,"");
343 KDK_ALL_INFO_FORMAT(m_pTitleLabel,"");
344 KDK_ALL_INFO_FORMAT(m_pCancelKBtn,"");
345 KDK_ALL_INFO_FORMAT(m_pConfirmKBtn,"");
346 KDK_ALL_INFO_FORMAT(m_pScrollWidget,"");
347 }
348
349 // 安全问题下拉框问题不可重复,已选择问题下拉框中置灰
350 void KSecurityQuestionDialogPrivate::setQustionCombox()
351 {
352 Q_Q(KSecurityQuestionDialog);
353 QStringList allComboxCurrentText;
354 for (int i = 0; i < m_questionComboxList.count(); i++) {
355 QComboBox *tempCombox = m_questionComboxList.at(i);
356 // 获取所有combox当前选择的text
357 allComboxCurrentText.append(tempCombox->currentText());
358 }
359
360 for (int j = 0; j < m_questionComboxList.count(); j++) {
361 QComboBox *tempCombox = m_questionComboxList.at(j);
362 QString currentText = tempCombox->currentText();
363 if (m_questionlist.contains(currentText)) {
364 tempCombox->blockSignals(true);
365 tempCombox->clear();
366 tempCombox->addItems(m_questionlist);
367 tempCombox->setCurrentText(currentText);
368 tempCombox->blockSignals(false);
369 // 将在当前combox的其他combox的当前项置灰
370 foreach (QString text, allComboxCurrentText) {
371 if (text != currentText) {
372 QVariant v(0);
373 tempCombox->setItemData(tempCombox->findText(text), v, Qt::UserRole - 1);
374 }
375 }
376 }
377 }
378 }
379
380 void KSecurityQuestionDialogPrivate::refreshConfirmBtn()
381 {
382 foreach (QLabel *tempLabel, m_tipsLabelList) {
383 if (!tempLabel->text().isEmpty()) {
384 tempLabel->clear();
385 }
386 }
387
388 foreach (QLineEdit *tempLineedit, m_answerLineeditList) {
389 if (tempLineedit->text().isEmpty()) {
390 m_pConfirmKBtn->setEnabled(false);
391 m_pConfirmKBtn->setProperty("isImportant", false);
392 return;
393 }
394 }
395
396 m_pConfirmKBtn->setEnabled(true);
397 m_pConfirmKBtn->setProperty("isImportant", true);
398 }
399
400 }
401
402 #include "ksecurityquestiondialog.moc"
403 #include "moc_ksecurityquestiondialog.cpp"