Skip to content
Permalink
Browse files

[FEATURE][needs-docs] new custom widget: password edit with built-in …

…show/hide

password button.
  • Loading branch information
alexbruy committed Mar 14, 2017
1 parent b132738 commit 1ef68358cb321926e660318200fd9b90beeab1a5
Showing with 144 additions and 0 deletions.
  1. +2 −0 src/gui/CMakeLists.txt
  2. +73 −0 src/gui/qgspasswordlineedit.cpp
  3. +69 −0 src/gui/qgspasswordlineedit.h
@@ -276,6 +276,7 @@ SET(QGIS_GUI_SRCS
qgssourceselectdialog.cpp
qgspanelwidget.cpp
qgspanelwidgetstack.cpp
qgspasswordlineedit.cpp
qgspixmaplabel.cpp
qgspluginmanagerinterface.cpp
qgspresetcolorrampdialog.cpp
@@ -422,6 +423,7 @@ SET(QGIS_GUI_MOC_HDRS
qgssourceselectdialog.h
qgspanelwidget.h
qgspanelwidgetstack.h
qgspasswordlineedit.h
qgspixmaplabel.h
qgspluginmanagerinterface.h
qgspresetcolorrampdialog.h
@@ -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;
}
}
}
@@ -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

0 comments on commit 1ef6835

Please sign in to comment.
You can’t perform that action at this time.