QGIS API Documentation  3.21.0-Master (5b68dc587e)
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 "ui_qgsauthconfigidedit.h"
19 
20 #include "qgsauthguiutils.h"
21 #include "qgsauthmanager.h"
22 #include "qgsapplication.h"
23 
24 #include <QRegularExpression>
25 
26 
27 QgsAuthConfigIdEdit::QgsAuthConfigIdEdit( QWidget *parent, const QString &authcfg, bool allowEmpty )
28  : QWidget( parent )
29  , mAuthCfgOrig( authcfg )
30  , mAllowEmpty( allowEmpty )
31 {
32  setupUi( this );
33  connect( btnLock, &QToolButton::toggled, this, &QgsAuthConfigIdEdit::btnLock_toggled );
34  connect( leAuthCfg, &QLineEdit::textChanged, this, &QgsAuthConfigIdEdit::leAuthCfg_textChanged );
35 
36  leAuthCfg->setReadOnly( true );
37 
38  connect( this, &QgsAuthConfigIdEdit::validityChanged, this, &QgsAuthConfigIdEdit::updateValidityStyle );
39 
40  leAuthCfg->setText( authcfg );
41  updateValidityStyle( validate() );
42 }
43 
45 {
46  if ( validate() )
47  {
48  return leAuthCfg->text();
49  }
50  return QString();
51 }
52 
54 {
55  const QString authcfg( leAuthCfg->text() );
56  bool curvalid = ( ( authcfg == mAuthCfgOrig && authcfg.size() == 7 )
57  || ( mAllowEmpty && authcfg.isEmpty() ) );
58 
59  if ( !QgsApplication::authManager()->isDisabled() && !curvalid && authcfg.size() == 7 && isAlphaNumeric( authcfg ) )
60  {
61  curvalid = QgsApplication::authManager()->configIdUnique( authcfg );
62  }
63 
64  if ( mValid != curvalid )
65  {
66  mValid = curvalid;
67  emit validityChanged( curvalid );
68  }
69 
70  return curvalid;
71 }
72 
73 void QgsAuthConfigIdEdit::setAuthConfigId( const QString &authcfg )
74 {
75  if ( mAuthCfgOrig.isEmpty() )
76  {
77  mAuthCfgOrig = authcfg;
78  }
79  leAuthCfg->setText( authcfg );
80  validate();
81 }
82 
84 {
85  mAllowEmpty = allowed;
86  validate();
87 }
88 
90 {
91  leAuthCfg->setText( mAuthCfgOrig );
92  updateValidityStyle( true );
93 }
94 
95 void QgsAuthConfigIdEdit::updateValidityStyle( bool valid )
96 {
97  QString ss( QStringLiteral( "QLineEdit{" ) );
98  ss += valid ? QString() : QStringLiteral( "color: %1;" ).arg( QgsAuthGuiUtils::redColor().name() );
99  ss += !btnLock->isChecked() ? QString() : QStringLiteral( "background-color: %1;" ).arg( QgsAuthGuiUtils::yellowColor().name() );
100  ss += '}';
101 
102  leAuthCfg->setStyleSheet( ss );
103 }
104 
105 void QgsAuthConfigIdEdit::btnLock_toggled( bool checked )
106 {
107  leAuthCfg->setReadOnly( !checked );
108  if ( checked )
109  leAuthCfg->setFocus();
110 
111  updateValidityStyle( validate() );
112 }
113 
114 void QgsAuthConfigIdEdit::leAuthCfg_textChanged( const QString &txt )
115 {
116  Q_UNUSED( txt )
117  validate();
118 }
119 
120 bool QgsAuthConfigIdEdit::isAlphaNumeric( const QString &authcfg )
121 {
122  const thread_local QRegularExpression alphaNumericRegExp( "([a-z]|[A-Z]|[0-9]){7}" );
123  return authcfg.indexOf( alphaNumericRegExp ) != -1;
124 }
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.