QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
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 
23 
24 QgsAuthConfigIdEdit::QgsAuthConfigIdEdit( QWidget *parent, const QString &authcfg , bool allowEmpty )
25  : QWidget( parent )
26  , mAuthCfgOrig( authcfg )
27  , mValid( false )
28  , mAllowEmpty( allowEmpty )
29 {
30  setupUi( this );
31 
32  leAuthCfg->setReadOnly( true );
33 
34  connect( this, SIGNAL( validityChanged( bool ) ), this, SLOT( updateValidityStyle( bool ) ) );
35 
36  leAuthCfg->setText( authcfg );
37  updateValidityStyle( validate() );
38 }
39 
41 {
42 }
43 
45 {
46  if ( validate() )
47  {
48  return leAuthCfg->text();
49  }
50  return QString();
51 }
52 
54 {
55  QString authcfg( leAuthCfg->text() );
56  bool curvalid = (( authcfg == mAuthCfgOrig && authcfg.size() == 7 )
57  || ( mAllowEmpty && authcfg.isEmpty() ) );
58 
59  if ( !QgsAuthManager::instance()->isDisabled() && !curvalid && authcfg.size() == 7 && isAlphaNumeric( authcfg ) )
60  {
61  curvalid = QgsAuthManager::instance()->configIdUnique( authcfg );
62  }
63 
64  if ( mValid != curvalid )
65  {
66  mValid = curvalid;
67  emit validityChanged( curvalid );
68  }
69 
70  return curvalid;
71 }
72 
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( "QLineEdit{" );
98  ss += valid ? "" : QString( "color: %1;" ).arg( QgsAuthGuiUtils::redColor().name() );
99  ss += !btnLock->isChecked() ? "" : QString( "background-color: %1;" ).arg( QgsAuthGuiUtils::yellowColor().name() );
100  ss += '}';
101 
102  leAuthCfg->setStyleSheet( ss );
103 }
104 
105 void QgsAuthConfigIdEdit::on_btnLock_toggled( bool checked )
106 {
107  leAuthCfg->setReadOnly( !checked );
108  if ( checked )
109  leAuthCfg->setFocus();
110 
111  updateValidityStyle( validate() );
112 }
113 
114 void QgsAuthConfigIdEdit::on_leAuthCfg_textChanged( const QString &txt )
115 {
116  Q_UNUSED( txt );
117  validate();
118 }
119 
120 bool QgsAuthConfigIdEdit::isAlphaNumeric( const QString &authcfg )
121 {
122  QRegExp rx( "([a-z]|[A-Z]|[0-9]){7}" );
123  return rx.indexIn( authcfg ) != -1;
124 }
void setupUi(QWidget *widget)
static QgsAuthManager * instance()
Enforce singleton pattern.
void validityChanged(bool valid)
Validity of the ID has changed.
void setAuthConfigId(const QString &authcfg)
Set the authentication configuration ID, storing it, and validating the passed value.
int size() const
static QColor yellowColor()
Yellow color representing caution regarding action.
int indexIn(const QString &str, int offset, CaretMode caretMode) const
const char * name() const
static QColor redColor()
Red color representing invalid, untrusted, etc.
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 isEmpty() const
bool validate()
Validate the widget state and ID.
void setAllowEmptyId(bool allowed)
Set 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.
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QString arg(qlonglong a, int fieldWidth, int base, const QChar &fillChar) const