QGIS API Documentation  3.2.0-Bonn (bc43194)
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 #include "qgsapplication.h"
20 
21 QgsPasswordLineEdit::QgsPasswordLineEdit( QWidget *parent, bool passwordVisible )
22  : QLineEdit( parent )
23 {
24  mShowPasswordIcon = QgsApplication::getThemeIcon( QStringLiteral( "/mActionShowAllLayers.svg" ) );
25  mHidePasswordIcon = QgsApplication::getThemeIcon( QStringLiteral( "/mActionHideAllLayers.svg" ) );
26 
27  mActionShowHidePassword = addAction( mShowPasswordIcon, QLineEdit::TrailingPosition );
28  mActionShowHidePassword->setCheckable( true );
29 
30  if ( mLockIconVisible )
31  {
32  mActionLock = addAction( QgsApplication::getThemeIcon( QStringLiteral( "/lockedGray.svg" ) ), QLineEdit::LeadingPosition );
33  }
34 
35  setPasswordVisibility( passwordVisible );
36  connect( mActionShowHidePassword, &QAction::triggered, this, &QgsPasswordLineEdit::togglePasswordVisibility );
37 }
38 
40 {
41  togglePasswordVisibility( visible );
42 }
43 
44 void QgsPasswordLineEdit::togglePasswordVisibility( bool toggled )
45 {
46  if ( toggled )
47  {
48  setEchoMode( QLineEdit::Normal );
49  mActionShowHidePassword->setIcon( mHidePasswordIcon );
50  mActionShowHidePassword->setToolTip( tr( "Hide text" ) );
51  }
52  else
53  {
54  setEchoMode( QLineEdit::Password );
55  mActionShowHidePassword->setIcon( mShowPasswordIcon );
56  mActionShowHidePassword->setToolTip( tr( "Show text" ) );
57  }
58 }
59 
61 {
62  mLockIconVisible = visible;
63  if ( mLockIconVisible )
64  {
65  if ( !mActionLock )
66  {
67  mActionLock = addAction( QgsApplication::getThemeIcon( QStringLiteral( "/lockedGray.svg" ) ), QLineEdit::LeadingPosition );
68  }
69  }
70  else
71  {
72  if ( mActionLock )
73  {
74  removeAction( mActionLock );
75  mActionLock = nullptr;
76  }
77  }
78 }
static QIcon getThemeIcon(const QString &name)
Helper to get a theme icon.
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.
void setPasswordVisibility(bool visible)
Set state of the password's visibility.