Skip to content

Commit

Permalink
Updates to QgsPasswordLineEdit
Browse files Browse the repository at this point in the history
- Add public func to set password visibility (instead of exposing slot)
- Add initial password visibility param; default to obfuscated (false)
- Fix bug where initial visibility was true, but show/hide was opposite
- Add generic tool tips to show/hide action
  • Loading branch information
dakcarto committed Mar 17, 2017
1 parent 16cf366 commit 921a0c7
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
4 changes: 4 additions & 0 deletions python/gui/qgspasswordlineedit.sip
Expand Up @@ -25,4 +25,8 @@ class QgsPasswordLineEdit : QLineEdit
/** Returns if a lock icon shall be shown on the left of the widget
*/
bool showLockIcon() const;

/** Set state of the password's visibility
*/
void setPasswordVisibility( bool visible );
};
10 changes: 9 additions & 1 deletion src/gui/qgspasswordlineedit.cpp
Expand Up @@ -18,7 +18,7 @@
#include "qgspasswordlineedit.h"
#include "qgsapplication.h"

QgsPasswordLineEdit::QgsPasswordLineEdit( QWidget *parent )
QgsPasswordLineEdit::QgsPasswordLineEdit( QWidget *parent, bool passwordVisible )
: QLineEdit( parent )
, mActionShowHidePassword( nullptr )
, mActionLock( nullptr )
Expand All @@ -35,20 +35,28 @@ QgsPasswordLineEdit::QgsPasswordLineEdit( QWidget *parent )
mActionLock = addAction( QgsApplication::getThemeIcon( "/lockedGray.svg" ), QLineEdit::LeadingPosition );
}

setPasswordVisibility( passwordVisible );
connect( mActionShowHidePassword, &QAction::triggered, this, &QgsPasswordLineEdit::togglePasswordVisibility );
}

void QgsPasswordLineEdit::setPasswordVisibility( bool visible )
{
togglePasswordVisibility( visible );
}

void QgsPasswordLineEdit::togglePasswordVisibility( bool toggled )
{
if ( toggled )
{
setEchoMode( QLineEdit::Normal );
mActionShowHidePassword->setIcon( mHidePasswordIcon );
mActionShowHidePassword->setToolTip( tr( "Hide text" ) );
}
else
{
setEchoMode( QLineEdit::Password );
mActionShowHidePassword->setIcon( mShowPasswordIcon );
mActionShowHidePassword->setToolTip( tr( "Show text" ) );
}
}

Expand Down
7 changes: 6 additions & 1 deletion src/gui/qgspasswordlineedit.h
Expand Up @@ -38,8 +38,9 @@ class GUI_EXPORT QgsPasswordLineEdit : public QLineEdit

/** Constructor for QgsPasswordLineEdit.
* @param parent parent widget
* @param passwordVisible Initial state of the password's visibility
*/
QgsPasswordLineEdit( QWidget *parent = nullptr );
QgsPasswordLineEdit( QWidget *parent = nullptr, bool passwordVisible = false );

/** Define if a lock icon shall be shown on the left of the widget
* @param visible set to false to hide the lock icon
Expand All @@ -50,6 +51,10 @@ class GUI_EXPORT QgsPasswordLineEdit : public QLineEdit
*/
bool showLockIcon() const { return mLockIconVisible; }

/** Set state of the password's visibility
*/
void setPasswordVisibility( bool visible );

private slots:
void togglePasswordVisibility( bool toggled );

Expand Down

0 comments on commit 921a0c7

Please sign in to comment.