QGIS API Documentation 3.99.0-Master (2fe06baccd8)
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 "moc_qgspasswordlineedit.cpp"
23
24QgsPasswordLineEdit::QgsPasswordLineEdit( QWidget *parent, bool passwordVisible )
25 : QLineEdit( parent )
26{
27 mShowPasswordIcon = QgsApplication::getThemeIcon( QStringLiteral( "/mActionShowAllLayers.svg" ) );
28 mHidePasswordIcon = QgsApplication::getThemeIcon( QStringLiteral( "/mActionHideAllLayers.svg" ) );
29
30 mActionShowHidePassword = addAction( mShowPasswordIcon, QLineEdit::TrailingPosition );
31 mActionShowHidePassword->setCheckable( true );
32
33 if ( mLockIconVisible )
34 {
35 mActionLock = addAction( QgsApplication::getThemeIcon( QStringLiteral( "/lockedGray.svg" ) ), QLineEdit::LeadingPosition );
36 }
37
38 setPasswordVisibility( passwordVisible );
39 connect( mActionShowHidePassword, &QAction::triggered, this, &QgsPasswordLineEdit::togglePasswordVisibility );
40}
41
43{
44 togglePasswordVisibility( visible );
45}
46
47void QgsPasswordLineEdit::togglePasswordVisibility( bool toggled )
48{
49 if ( toggled )
50 {
51 setEchoMode( QLineEdit::Normal );
52 mActionShowHidePassword->setIcon( mHidePasswordIcon );
53 mActionShowHidePassword->setToolTip( tr( "Hide text" ) );
54 }
55 else
56 {
57 setEchoMode( QLineEdit::Password );
58 mActionShowHidePassword->setIcon( mShowPasswordIcon );
59 mActionShowHidePassword->setToolTip( tr( "Show text" ) );
60 }
61}
62
64{
65 mLockIconVisible = visible;
66 if ( mLockIconVisible )
67 {
68 if ( !mActionLock )
69 {
70 mActionLock = addAction( QgsApplication::getThemeIcon( QStringLiteral( "/lockedGray.svg" ) ), QLineEdit::LeadingPosition );
71 }
72 }
73 else
74 {
75 if ( mActionLock )
76 {
77 removeAction( mActionLock );
78 mActionLock = nullptr;
79 }
80 }
81}
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.