Skip to content

Commit 1ef6835

Browse files
committed
[FEATURE][needs-docs] new custom widget: password edit with built-in show/hide
password button.
1 parent b132738 commit 1ef6835

File tree

3 files changed

+144
-0
lines changed

3 files changed

+144
-0
lines changed

src/gui/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,7 @@ SET(QGIS_GUI_SRCS
276276
qgssourceselectdialog.cpp
277277
qgspanelwidget.cpp
278278
qgspanelwidgetstack.cpp
279+
qgspasswordlineedit.cpp
279280
qgspixmaplabel.cpp
280281
qgspluginmanagerinterface.cpp
281282
qgspresetcolorrampdialog.cpp
@@ -422,6 +423,7 @@ SET(QGIS_GUI_MOC_HDRS
422423
qgssourceselectdialog.h
423424
qgspanelwidget.h
424425
qgspanelwidgetstack.h
426+
qgspasswordlineedit.h
425427
qgspixmaplabel.h
426428
qgspluginmanagerinterface.h
427429
qgspresetcolorrampdialog.h

src/gui/qgspasswordlineedit.cpp

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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 )
22+
: QLineEdit( parent )
23+
, mActionShowHidePassword( nullptr )
24+
, mActionLock( nullptr )
25+
, mLockIconVisible( false )
26+
{
27+
mShowPasswordIcon = QgsApplication::getThemeIcon( "/mActionShowAllLayers.svg" );
28+
mHidePasswordIcon = QgsApplication::getThemeIcon( "/mActionHideAllLayers.svg" );
29+
30+
mActionShowHidePassword = addAction( mShowPasswordIcon, QLineEdit::TrailingPosition );
31+
mActionShowHidePassword->setCheckable( true );
32+
33+
if ( mLockIconVisible )
34+
{
35+
mActionLock = addAction( QgsApplication::getThemeIcon( "/lockedGray.svg" ), QLineEdit::LeadingPosition );
36+
}
37+
38+
connect( mActionShowHidePassword, SIGNAL( triggered( bool ) ), this, SLOT( togglePasswordVisibility( bool ) ) );
39+
}
40+
41+
void QgsPasswordLineEdit::togglePasswordVisibility( bool toggled )
42+
{
43+
if ( toggled )
44+
{
45+
setEchoMode( QLineEdit::Normal );
46+
mActionShowHidePassword->setIcon( mHidePasswordIcon );
47+
}
48+
else
49+
{
50+
setEchoMode( QLineEdit::Password );
51+
mActionShowHidePassword->setIcon( mShowPasswordIcon );
52+
}
53+
}
54+
55+
void QgsPasswordLineEdit::setShowLockIcon( bool visible )
56+
{
57+
mLockIconVisible = visible;
58+
if ( mLockIconVisible )
59+
{
60+
if ( !mActionLock )
61+
{
62+
mActionLock = addAction( QgsApplication::getThemeIcon( "/lockedGray.svg" ), QLineEdit::LeadingPosition );
63+
}
64+
}
65+
else
66+
{
67+
if ( mActionLock )
68+
{
69+
removeAction( mActionLock );
70+
mActionLock = nullptr;
71+
}
72+
}
73+
}

src/gui/qgspasswordlineedit.h

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/***************************************************************************
2+
qgspasswordlineedit.h
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+
#ifndef QGSPASSWORDLINEEDIT_H
19+
#define QGSPASSWORDLINEEDIT_H
20+
21+
#include <QLineEdit>
22+
#include <QAction>
23+
24+
#include "qgis_gui.h"
25+
26+
class QToolButton;
27+
28+
/** \class QgsPasswordLineEdit
29+
* \ingroup gui
30+
* QLineEdit subclass with built in support for showing/hiding
31+
* entered password.
32+
* @note added in QGIS 3.0
33+
**/
34+
class GUI_EXPORT QgsPasswordLineEdit : public QLineEdit
35+
{
36+
Q_OBJECT
37+
38+
public:
39+
40+
/** Constructor for QgsPasswordLineEdit.
41+
* @param parent parent widget
42+
*/
43+
QgsPasswordLineEdit( QWidget *parent = nullptr );
44+
45+
/** Define if a lock icon shall be shown on the left of the widget
46+
* @param visible set to false to hide the lock icon
47+
*/
48+
void setShowLockIcon( bool visible );
49+
50+
/** Returns if a lock icon shall be shown on the left of the widget
51+
*/
52+
bool showLockIcon() const { return mLockIconVisible; }
53+
54+
private slots:
55+
void togglePasswordVisibility( bool toggled );
56+
57+
private:
58+
59+
QAction *mActionShowHidePassword = nullptr;
60+
QAction *mActionLock = nullptr;
61+
62+
QIcon mShowPasswordIcon;
63+
QIcon mHidePasswordIcon;
64+
65+
bool mLockIconVisible;
66+
QSize mIconsSize;
67+
};
68+
69+
#endif // QGSPASSWORDLINEEDIT_H

0 commit comments

Comments
 (0)