Updates to QgsPasswordLineEdit

- 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
This commit is contained in:
Larry Shaffer 2017-03-17 16:32:17 -06:00
parent 16cf3665e1
commit 921a0c75e3
3 changed files with 19 additions and 2 deletions

View File

@ -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 );
};

View File

@ -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 )
@ -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" ) );
}
}

View File

@ -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
@ -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 );