Skip to content

Commit

Permalink
Merge pull request #4256 from alexbruy/password-lineedit
Browse files Browse the repository at this point in the history
[FEATURE][needs-docs] new custom widget PasswordLineEdit
  • Loading branch information
alexbruy authored Mar 14, 2017
2 parents b132738 + 0dc61cb commit 85776a1
Show file tree
Hide file tree
Showing 19 changed files with 463 additions and 110 deletions.
1 change: 1 addition & 0 deletions python/gui/gui.sip
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@
%Include qgsowssourceselect.sip
%Include qgspanelwidget.sip
%Include qgspanelwidgetstack.sip
%Include qgspasswordlineedit.sip
%Include qgspixmaplabel.sip
%Include qgspluginmanagerinterface.sip
%Include qgspresetcolorrampdialog.sip
Expand Down
28 changes: 28 additions & 0 deletions python/gui/qgspasswordlineedit.sip
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/** \class QgsPasswordLineEdit
* \ingroup gui
* QLineEdit subclass with built in support for showing/hiding
* entered password.
* @note added in QGIS 3.0
**/
class QgsPasswordLineEdit : QLineEdit
{
%TypeHeaderCode
#include <qgspasswordlineedit.h>
%End

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;
};
4 changes: 3 additions & 1 deletion src/customwidgets/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ SET (QGIS_CUSTOMWIDGETS_SRCS
qgiscustomwidgets.cpp
qgscollapsiblegroupboxplugin.cpp
qgscolorbuttonplugin.cpp
qgsdatetimeeditplugin.cpp
qgsdatetimeeditplugin.cpp
qgsdockwidgetplugin.cpp
qgsdoublespinboxplugin.cpp
qgsexpressionbuilderwidgetplugin.cpp
Expand All @@ -25,6 +25,7 @@ SET (QGIS_CUSTOMWIDGETS_SRCS
qgsfilewidgetplugin.cpp
qgsfilterlineeditplugin.cpp
qgsmaplayercomboboxplugin.cpp
qgspasswordlineeditplugin.cpp
qgsprojectionselectionwidgetplugin.cpp
qgspropertyoverridebuttonplugin.cpp
qgsrelationeditorwidgetplugin.cpp
Expand All @@ -49,6 +50,7 @@ SET (QGIS_CUSTOMWIDGETS_MOC_HDRS
qgsfilewidgetplugin.h
qgsfilterlineeditplugin.h
qgsmaplayercomboboxplugin.h
qgspasswordlineeditplugin.h
qgsprojectionselectionwidgetplugin.h
qgspropertyoverridebuttonplugin.h
qgsrelationeditorwidgetplugin.h
Expand Down
97 changes: 97 additions & 0 deletions src/customwidgets/qgspasswordlineeditplugin.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/***************************************************************************
qgsfilterlineeditplugin.cpp
--------------------------------------
Date : March 13, 2017
Copyright : (C) 2017 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 "qgiscustomwidgets.h"
#include "qgspasswordlineedit.h"
#include "qgspasswordlineeditplugin.h"


QgsPasswordLineEditPlugin::QgsPasswordLineEditPlugin( QObject *parent )
: QObject( parent )
, mInitialized( false )
{
}


QString QgsPasswordLineEditPlugin::name() const
{
return "QgsPasswordLineEdit";
}

QString QgsPasswordLineEditPlugin::group() const
{
return QgisCustomWidgets::groupName();
}

QString QgsPasswordLineEditPlugin::includeFile() const
{
return "qgspasswordlineedit.h";
}

QIcon QgsPasswordLineEditPlugin::icon() const
{
return QIcon( ":/images/icons/qgis-icon-60x60.png" );
}

bool QgsPasswordLineEditPlugin::isContainer() const
{
return false;
}

QWidget *QgsPasswordLineEditPlugin::createWidget( QWidget *parent )
{
return new QgsPasswordLineEdit( parent );
}

bool QgsPasswordLineEditPlugin::isInitialized() const
{
return mInitialized;
}

void QgsPasswordLineEditPlugin::initialize( QDesignerFormEditorInterface *core )
{
Q_UNUSED( core );
if ( mInitialized )
return;
mInitialized = true;
}


QString QgsPasswordLineEditPlugin::toolTip() const
{
return "";
}

QString QgsPasswordLineEditPlugin::whatsThis() const
{
return "";
}

QString QgsPasswordLineEditPlugin::domXml() const
{
return QString( "<ui language=\"c++\">\n"
" <widget class=\"%1\" name=\"mLineEdit\">\n"
" <property name=\"geometry\">\n"
" <rect>\n"
" <x>0</x>\n"
" <y>0</y>\n"
" <width>60</width>\n"
" <height>27</height>\n"
" </rect>\n"
" </property>\n"
" </widget>\n"
"</ui>\n" )
.arg( name() );
}
51 changes: 51 additions & 0 deletions src/customwidgets/qgspasswordlineeditplugin.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/***************************************************************************
qgspasswordlineeditplugin.h
--------------------------------------
Date : March 13, 2017
Copyright : (C) 2017 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 QGSPASSWORDLINEEDITPLUGIN_H
#define QGSPASSWORDLINEEDITPLUGIN_H


#include <QtGlobal>
#include <QtUiPlugin/QDesignerCustomWidgetInterface>
#include <QtUiPlugin/QDesignerExportWidget>
#include "qgis_customwidgets.h"


class CUSTOMWIDGETS_EXPORT QgsPasswordLineEditPlugin : public QObject, public QDesignerCustomWidgetInterface
{
Q_OBJECT
Q_INTERFACES( QDesignerCustomWidgetInterface )

public:
explicit QgsPasswordLineEditPlugin( QObject *parent = 0 );

private:
bool mInitialized = false;

// QDesignerCustomWidgetInterface interface
public:
QString name() const override;
QString group() const override;
QString includeFile() const override;
QIcon icon() const override;
bool isContainer() const override;
QWidget *createWidget( QWidget *parent ) override;
bool isInitialized() const override;
void initialize( QDesignerFormEditorInterface *core ) override;
QString toolTip() const override;
QString whatsThis() const override;
QString domXml() const override;
};
#endif // QGSPASSWORDLINEEDITPLUGIN_H
2 changes: 2 additions & 0 deletions src/gui/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ SET(QGIS_GUI_SRCS
qgssourceselectdialog.cpp
qgspanelwidget.cpp
qgspanelwidgetstack.cpp
qgspasswordlineedit.cpp
qgspixmaplabel.cpp
qgspluginmanagerinterface.cpp
qgspresetcolorrampdialog.cpp
Expand Down Expand Up @@ -422,6 +423,7 @@ SET(QGIS_GUI_MOC_HDRS
qgssourceselectdialog.h
qgspanelwidget.h
qgspanelwidgetstack.h
qgspasswordlineedit.h
qgspixmaplabel.h
qgspluginmanagerinterface.h
qgspresetcolorrampdialog.h
Expand Down
10 changes: 0 additions & 10 deletions src/gui/auth/qgsauthmasterpassresetdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,16 +80,6 @@ void QgsMasterPasswordResetDialog::on_leMasterPassNew_textChanged( const QString
validatePasswords();
}

void QgsMasterPasswordResetDialog::on_chkPassShowCurrent_stateChanged( int state )
{
leMasterPassCurrent->setEchoMode( ( state > 0 ) ? QLineEdit::Normal : QLineEdit::Password );
}

void QgsMasterPasswordResetDialog::on_chkPassShowNew_stateChanged( int state )
{
leMasterPassNew->setEchoMode( ( state > 0 ) ? QLineEdit::Normal : QLineEdit::Password );
}

void QgsMasterPasswordResetDialog::validatePasswords()
{
QString ss1 = mPassCurOk ? QgsAuthGuiUtils::greenTextStyleSheet( QStringLiteral( "QLineEdit" ) )
Expand Down
3 changes: 0 additions & 3 deletions src/gui/auth/qgsauthmasterpassresetdialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,6 @@ class GUI_EXPORT QgsMasterPasswordResetDialog : public QDialog, private Ui::QgsM
void on_leMasterPassCurrent_textChanged( const QString &pass );
void on_leMasterPassNew_textChanged( const QString &pass );

void on_chkPassShowCurrent_stateChanged( int state );
void on_chkPassShowNew_stateChanged( int state );

private:
void validatePasswords();

Expand Down
73 changes: 73 additions & 0 deletions src/gui/qgspasswordlineedit.cpp
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, &QAction::triggered, this, &QgsPasswordLineEdit::togglePasswordVisibility );
}

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;
}
}
}
68 changes: 68 additions & 0 deletions src/gui/qgspasswordlineedit.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/***************************************************************************
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 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
Q_PROPERTY( bool showLockIcon READ showLockIcon WRITE setShowLockIcon )

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
Loading

0 comments on commit 85776a1

Please sign in to comment.