QGIS API Documentation 3.41.0-Master (fda2aa46e9a)
Loading...
Searching...
No Matches
qgsauthconfigidedit.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsauthconfigidedit.cpp
3 ---------------------
4 begin : September, 2015
5 copyright : (C) 2015 by Boundless Spatial, Inc. USA
6 author : Larry Shaffer
7 email : lshaffer at boundlessgeo dot com
8 ***************************************************************************
9 * *
10 * This program is free software; you can redistribute it and/or modify *
11 * it under the terms of the GNU General Public License as published by *
12 * the Free Software Foundation; either version 2 of the License, or *
13 * (at your option) any later version. *
14 * *
15 ***************************************************************************/
16
17#include "qgsauthconfigidedit.h"
18#include "moc_qgsauthconfigidedit.cpp"
19#include "ui_qgsauthconfigidedit.h"
20
21#include "qgsauthguiutils.h"
22#include "qgsauthmanager.h"
23#include "qgsapplication.h"
24
25#include <QRegularExpression>
26
27
28QgsAuthConfigIdEdit::QgsAuthConfigIdEdit( QWidget *parent, const QString &authcfg, bool allowEmpty )
29 : QWidget( parent )
30 , mAuthCfgOrig( authcfg )
31 , mAllowEmpty( allowEmpty )
32{
33 setupUi( this );
34 connect( btnLock, &QToolButton::toggled, this, &QgsAuthConfigIdEdit::btnLock_toggled );
35 connect( leAuthCfg, &QLineEdit::textChanged, this, &QgsAuthConfigIdEdit::leAuthCfg_textChanged );
36
37 leAuthCfg->setReadOnly( true );
38
39 connect( this, &QgsAuthConfigIdEdit::validityChanged, this, &QgsAuthConfigIdEdit::updateValidityStyle );
40
41 leAuthCfg->setText( authcfg );
42 updateValidityStyle( validate() );
43}
44
46{
47 if ( validate() )
48 {
49 return leAuthCfg->text();
50 }
51 return QString();
52}
53
55{
56 const QString authcfg( leAuthCfg->text() );
57 bool curvalid = ( ( authcfg == mAuthCfgOrig && authcfg.size() == 7 )
58 || ( mAllowEmpty && authcfg.isEmpty() ) );
59
60 if ( !QgsApplication::authManager()->isDisabled() && !curvalid && authcfg.size() == 7 && isAlphaNumeric( authcfg ) )
61 {
62 curvalid = QgsApplication::authManager()->configIdUnique( authcfg );
63 }
64
65 if ( mValid != curvalid )
66 {
67 mValid = curvalid;
68 emit validityChanged( curvalid );
69 }
70
71 return curvalid;
72}
73
74void QgsAuthConfigIdEdit::setAuthConfigId( const QString &authcfg )
75{
76 if ( mAuthCfgOrig.isEmpty() )
77 {
78 mAuthCfgOrig = authcfg;
79 }
80 leAuthCfg->setText( authcfg );
81 validate();
82}
83
85{
86 mAllowEmpty = allowed;
87 validate();
88}
89
91{
92 leAuthCfg->setText( mAuthCfgOrig );
93 updateValidityStyle( true );
94}
95
96void QgsAuthConfigIdEdit::updateValidityStyle( bool valid )
97{
98 QString ss( QStringLiteral( "QLineEdit{" ) );
99 ss += valid ? QString() : QStringLiteral( "color: %1;" ).arg( QgsAuthGuiUtils::redColor().name() );
100 ss += !btnLock->isChecked() ? QString() : QStringLiteral( "background-color: %1;" ).arg( QgsAuthGuiUtils::yellowColor().name() );
101 ss += '}';
102
103 leAuthCfg->setStyleSheet( ss );
104}
105
106void QgsAuthConfigIdEdit::btnLock_toggled( bool checked )
107{
108 leAuthCfg->setReadOnly( !checked );
109 if ( checked )
110 leAuthCfg->setFocus();
111
112 updateValidityStyle( validate() );
113}
114
115void QgsAuthConfigIdEdit::leAuthCfg_textChanged( const QString &txt )
116{
117 Q_UNUSED( txt )
118 validate();
119}
120
121bool QgsAuthConfigIdEdit::isAlphaNumeric( const QString &authcfg )
122{
123 const thread_local QRegularExpression alphaNumericRegExp( "([a-z]|[A-Z]|[0-9]){7}" );
124 return authcfg.indexOf( alphaNumericRegExp ) != -1;
125}
static QgsAuthManager * authManager()
Returns the application's authentication manager instance.
bool validate()
Validate the widget state and ID.
QgsAuthConfigIdEdit(QWidget *parent=nullptr, const QString &authcfg=QString(), bool allowEmpty=true)
Widget to unlock and edit an authentication configuration ID.
void setAuthConfigId(const QString &authcfg)
Sets the authentication configuration ID, storing it, and validating the passed value.
void validityChanged(bool valid)
Validity of the ID has changed.
void setAllowEmptyId(bool allowed)
Sets whether to allow no ID to be set.
void clear()
Clear all of the widget's editing state and contents.
QString const configId()
The authentication configuration ID, if valid, otherwise null QString.
Utility functions for use by authentication GUI widgets or standalone apps.
bool isDisabled() const
Whether QCA has the qca-ossl plugin, which a base run-time requirement.
bool configIdUnique(const QString &id) const
Verify if provided authentication id is unique.