Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FEATURE][needs-docs] new custom widget: password edit with built-in …
…show/hide password button.
- Loading branch information
Showing
3 changed files
with
144 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/*************************************************************************** | ||
qgspasswordlineedit.cpp | ||
------------------------ | ||
begin : March 13, 2017 | ||
copyright : (C) 2017 by Alexander Bruy | ||
email : alexander dot bruy at gmail dot com | ||
***************************************************************************/ | ||
|
||
/*************************************************************************** | ||
* * | ||
* This program is free software; you can redistribute it and/or modify * | ||
* it under the terms of the GNU General Public License as published by * | ||
* the Free Software Foundation; either version 2 of the License, or * | ||
* (at your option) any later version. * | ||
* * | ||
***************************************************************************/ | ||
|
||
#include "qgspasswordlineedit.h" | ||
#include "qgsapplication.h" | ||
|
||
QgsPasswordLineEdit::QgsPasswordLineEdit( QWidget *parent ) | ||
: QLineEdit( parent ) | ||
, mActionShowHidePassword( nullptr ) | ||
, mActionLock( nullptr ) | ||
, mLockIconVisible( false ) | ||
{ | ||
mShowPasswordIcon = QgsApplication::getThemeIcon( "/mActionShowAllLayers.svg" ); | ||
mHidePasswordIcon = QgsApplication::getThemeIcon( "/mActionHideAllLayers.svg" ); | ||
|
||
mActionShowHidePassword = addAction( mShowPasswordIcon, QLineEdit::TrailingPosition ); | ||
mActionShowHidePassword->setCheckable( true ); | ||
|
||
if ( mLockIconVisible ) | ||
{ | ||
mActionLock = addAction( QgsApplication::getThemeIcon( "/lockedGray.svg" ), QLineEdit::LeadingPosition ); | ||
} | ||
|
||
connect( mActionShowHidePassword, SIGNAL( triggered( bool ) ), this, SLOT( togglePasswordVisibility( bool ) ) ); | ||
} | ||
|
||
void QgsPasswordLineEdit::togglePasswordVisibility( bool toggled ) | ||
{ | ||
if ( toggled ) | ||
{ | ||
setEchoMode( QLineEdit::Normal ); | ||
mActionShowHidePassword->setIcon( mHidePasswordIcon ); | ||
} | ||
else | ||
{ | ||
setEchoMode( QLineEdit::Password ); | ||
mActionShowHidePassword->setIcon( mShowPasswordIcon ); | ||
} | ||
} | ||
|
||
void QgsPasswordLineEdit::setShowLockIcon( bool visible ) | ||
{ | ||
mLockIconVisible = visible; | ||
if ( mLockIconVisible ) | ||
{ | ||
if ( !mActionLock ) | ||
{ | ||
mActionLock = addAction( QgsApplication::getThemeIcon( "/lockedGray.svg" ), QLineEdit::LeadingPosition ); | ||
} | ||
} | ||
else | ||
{ | ||
if ( mActionLock ) | ||
{ | ||
removeAction( mActionLock ); | ||
mActionLock = nullptr; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/*************************************************************************** | ||
qgspasswordlineedit.h | ||
------------------------ | ||
begin : March 13, 2017 | ||
copyright : (C) 2017 by Alexander Bruy | ||
email : alexander dot bruy at gmail dot com | ||
***************************************************************************/ | ||
|
||
/*************************************************************************** | ||
* * | ||
* This program is free software; you can redistribute it and/or modify * | ||
* it under the terms of the GNU General Public License as published by * | ||
* the Free Software Foundation; either version 2 of the License, or * | ||
* (at your option) any later version. * | ||
* * | ||
***************************************************************************/ | ||
|
||
#ifndef QGSPASSWORDLINEEDIT_H | ||
#define QGSPASSWORDLINEEDIT_H | ||
|
||
#include <QLineEdit> | ||
#include <QAction> | ||
|
||
#include "qgis_gui.h" | ||
|
||
class QToolButton; | ||
|
||
/** \class QgsPasswordLineEdit | ||
* \ingroup gui | ||
* QLineEdit subclass with built in support for showing/hiding | ||
* entered password. | ||
* @note added in QGIS 3.0 | ||
**/ | ||
class GUI_EXPORT QgsPasswordLineEdit : public QLineEdit | ||
{ | ||
Q_OBJECT | ||
|
||
public: | ||
|
||
/** Constructor for QgsPasswordLineEdit. | ||
* @param parent parent widget | ||
*/ | ||
QgsPasswordLineEdit( QWidget *parent = nullptr ); | ||
|
||
/** Define if a lock icon shall be shown on the left of the widget | ||
* @param visible set to false to hide the lock icon | ||
*/ | ||
void setShowLockIcon( bool visible ); | ||
|
||
/** Returns if a lock icon shall be shown on the left of the widget | ||
*/ | ||
bool showLockIcon() const { return mLockIconVisible; } | ||
|
||
private slots: | ||
void togglePasswordVisibility( bool toggled ); | ||
|
||
private: | ||
|
||
QAction *mActionShowHidePassword = nullptr; | ||
QAction *mActionLock = nullptr; | ||
|
||
QIcon mShowPasswordIcon; | ||
QIcon mHidePasswordIcon; | ||
|
||
bool mLockIconVisible; | ||
QSize mIconsSize; | ||
}; | ||
|
||
#endif // QGSPASSWORDLINEEDIT_H |