QGIS API Documentation  3.10.0-A Coruña (6c816b4204)
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 
25 QgsAuthConfigIdEdit::QgsAuthConfigIdEdit( QWidget *parent, const QString &authcfg, bool allowEmpty )
26  : QWidget( parent )
27  , mAuthCfgOrig( authcfg )
28  , mAllowEmpty( allowEmpty )
29 {
30  setupUi( this );
31  connect( btnLock, &QToolButton::toggled, this, &QgsAuthConfigIdEdit::btnLock_toggled );
32  connect( leAuthCfg, &QLineEdit::textChanged, this, &QgsAuthConfigIdEdit::leAuthCfg_textChanged );
33 
34  leAuthCfg->setReadOnly( true );
35 
36  connect( this, &QgsAuthConfigIdEdit::validityChanged, this, &QgsAuthConfigIdEdit::updateValidityStyle );
37 
38  leAuthCfg->setText( authcfg );
39  updateValidityStyle( validate() );
40 }
41 
43 {
44  if ( validate() )
45  {
46  return leAuthCfg->text();
47  }
48  return QString();
49 }
50 
52 {
53  QString authcfg( leAuthCfg->text() );
54  bool curvalid = ( ( authcfg == mAuthCfgOrig && authcfg.size() == 7 )
55  || ( mAllowEmpty && authcfg.isEmpty() ) );
56 
57  if ( !QgsApplication::authManager()->isDisabled() && !curvalid && authcfg.size() == 7 && isAlphaNumeric( authcfg ) )
58  {
59  curvalid = QgsApplication::authManager()->configIdUnique( authcfg );
60  }
61 
62  if ( mValid != curvalid )
63  {
64  mValid = curvalid;
65  emit validityChanged( curvalid );
66  }
67 
68  return curvalid;
69 }
70 
71 void QgsAuthConfigIdEdit::setAuthConfigId( const QString &authcfg )
72 {
73  if ( mAuthCfgOrig.isEmpty() )
74  {
75  mAuthCfgOrig = authcfg;
76  }
77  leAuthCfg->setText( authcfg );
78  validate();
79 }
80 
82 {
83  mAllowEmpty = allowed;
84  validate();
85 }
86 
88 {
89  leAuthCfg->setText( mAuthCfgOrig );
90  updateValidityStyle( true );
91 }
92 
93 void QgsAuthConfigIdEdit::updateValidityStyle( bool valid )
94 {
95  QString ss( QStringLiteral( "QLineEdit{" ) );
96  ss += valid ? QString() : QStringLiteral( "color: %1;" ).arg( QgsAuthGuiUtils::redColor().name() );
97  ss += !btnLock->isChecked() ? QString() : QStringLiteral( "background-color: %1;" ).arg( QgsAuthGuiUtils::yellowColor().name() );
98  ss += '}';
99 
100  leAuthCfg->setStyleSheet( ss );
101 }
102 
103 void QgsAuthConfigIdEdit::btnLock_toggled( bool checked )
104 {
105  leAuthCfg->setReadOnly( !checked );
106  if ( checked )
107  leAuthCfg->setFocus();
108 
109  updateValidityStyle( validate() );
110 }
111 
112 void QgsAuthConfigIdEdit::leAuthCfg_textChanged( const QString &txt )
113 {
114  Q_UNUSED( txt )
115  validate();
116 }
117 
118 bool QgsAuthConfigIdEdit::isAlphaNumeric( const QString &authcfg )
119 {
120  QRegExp rx( "([a-z]|[A-Z]|[0-9]){7}" );
121  return rx.indexIn( authcfg ) != -1;
122 }
void validityChanged(bool valid)
Validity of the ID has changed.
void setAuthConfigId(const QString &authcfg)
Sets the authentication configuration ID, storing it, and validating the passed value.
static QColor yellowColor()
Yellow color representing caution regarding action.
static QColor redColor()
Red color representing invalid, untrusted, etc. certificate.
void clear()
Clear all of the widget's editing state and contents.
QString const configId()
The authentication configuration ID, if valid, otherwise null QString.
bool validate()
Validate the widget state and ID.
static QgsAuthManager * authManager()
Returns the application's authentication manager instance.
void setAllowEmptyId(bool allowed)
Sets whether to allow no ID to be set.
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.
QgsAuthConfigIdEdit(QWidget *parent=nullptr, const QString &authcfg=QString(), bool allowEmpty=true)
Widget to unlock and edit an authentication configuration ID.