QGIS API Documentation 3.99.0-Master (09f76ad7019)
Loading...
Searching...
No Matches
qgspasswordlineedit.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgspasswordlineedit.cpp
3 ------------------------
4 begin : March 13, 2017
5 copyright : (C) 2017 by Alexander Bruy
6 email : alexander dot bruy at gmail dot com
7 ***************************************************************************/
8
9/***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
18#include "qgspasswordlineedit.h"
19
20#include "qgsapplication.h"
21
22#include <QString>
23
24#include "moc_qgspasswordlineedit.cpp"
25
26using namespace Qt::StringLiterals;
27
28QgsPasswordLineEdit::QgsPasswordLineEdit( QWidget *parent, bool passwordVisible )
29 : QLineEdit( parent )
30{
31 mShowPasswordIcon = QgsApplication::getThemeIcon( u"/mActionShowAllLayers.svg"_s );
32 mHidePasswordIcon = QgsApplication::getThemeIcon( u"/mActionHideAllLayers.svg"_s );
33
34 mActionShowHidePassword = addAction( mShowPasswordIcon, QLineEdit::TrailingPosition );
35 mActionShowHidePassword->setCheckable( true );
36
37 if ( mLockIconVisible )
38 {
39 mActionLock = addAction( QgsApplication::getThemeIcon( u"/lockedGray.svg"_s ), QLineEdit::LeadingPosition );
40 }
41
42 setPasswordVisibility( passwordVisible );
43 connect( mActionShowHidePassword, &QAction::triggered, this, &QgsPasswordLineEdit::togglePasswordVisibility );
44}
45
47{
48 togglePasswordVisibility( visible );
49}
50
51void QgsPasswordLineEdit::togglePasswordVisibility( bool toggled )
52{
53 if ( toggled )
54 {
55 setEchoMode( QLineEdit::Normal );
56 mActionShowHidePassword->setIcon( mHidePasswordIcon );
57 mActionShowHidePassword->setToolTip( tr( "Hide text" ) );
58 }
59 else
60 {
61 setEchoMode( QLineEdit::Password );
62 mActionShowHidePassword->setIcon( mShowPasswordIcon );
63 mActionShowHidePassword->setToolTip( tr( "Show text" ) );
64 }
65}
66
68{
69 mLockIconVisible = visible;
70 if ( mLockIconVisible )
71 {
72 if ( !mActionLock )
73 {
74 mActionLock = addAction( QgsApplication::getThemeIcon( u"/lockedGray.svg"_s ), QLineEdit::LeadingPosition );
75 }
76 }
77 else
78 {
79 if ( mActionLock )
80 {
81 removeAction( mActionLock );
82 mActionLock = nullptr;
83 }
84 }
85}
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
void setPasswordVisibility(bool visible)
Set state of the password's visibility.
QgsPasswordLineEdit(QWidget *parent=nullptr, bool passwordVisible=false)
Constructor for QgsPasswordLineEdit.
void setShowLockIcon(bool visible)
Define if a lock icon shall be shown on the left of the widget.