Debian Codegraph @

src/librssguard/gui/reusable/baselineedit.cpp

raw symbols SCIP
1 // For license of this file, see <project-root-folder>/LICENSE.md.
2
3 #include "gui/reusable/baselineedit.h"
4
5 #include "miscellaneous/application.h"
6 #include "miscellaneous/iconfactory.h"
7
8 #include <QAction>
9 #include <QKeyEvent>
10
11 BaseLineEdit::BaseLineEdit(QWidget* parent)
12 : QLineEdit(parent), m_actShowPassword(new QAction(qApp->icons()->fromTheme(QSL("dialog-password")),
13 tr("Show/hide the password"),
14 this)) {
15
16 connect(m_actShowPassword, &QAction::triggered, this, [this]() {
17 setEchoMode(echoMode() == QLineEdit::EchoMode::Password ? QLineEdit::EchoMode::Normal
18 : QLineEdit::EchoMode::Password);
19 });
20 connect(this, &QLineEdit::textChanged, this, [this](const QString& text) {
21 if (actions().contains(m_actShowPassword)) {
22 m_actShowPassword->setVisible(!text.isEmpty());
23 }
24 });
25
26 setClearButtonEnabled(true);
27 }
28
29 BaseLineEdit::~BaseLineEdit() {}
30
31 void BaseLineEdit::setPasswordMode(bool is_password) {
32 if (is_password) {
33 setEchoMode(QLineEdit::EchoMode::Password);
34 addAction(m_actShowPassword, QLineEdit::ActionPosition::LeadingPosition);
35 }
36 else {
37 setEchoMode(QLineEdit::EchoMode::Normal);
38 removeAction(m_actShowPassword);
39 }
40
41 emit textChanged(text());
42 }
43
44 void BaseLineEdit::submit(const QString& text) {
45 setText(text);
46 emit submitted(text);
47 }
48
49 void BaseLineEdit::keyPressEvent(QKeyEvent* event) {
50 if (event->key() == Qt::Key::Key_Enter || event->key() == Qt::Key::Key_Return) {
51 emit submitted(text());
52 event->accept();
53 }
54
55 if (event->key() == Qt::Key::Key_Escape) {
56 submit(QString());
57 event->accept();
58 }
59
60 QLineEdit::keyPressEvent(event);
61 }