QGIS API Documentation  3.18.1-Zürich (202f1bf7e5)
qgsauthcerttrustpolicycombobox.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsauthcerttrustpolicycombobox.cpp
3  ---------------------
4  begin : May 02, 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 
18 
19 #include <QLineEdit>
20 
21 #include "qgsauthcertutils.h"
22 #include "qgsauthguiutils.h"
23 #include "qgsauthmanager.h"
24 #include "qgslogger.h"
25 #include "qgsapplication.h"
26 
27 
30  QgsAuthCertUtils::CertTrustPolicy defaultpolicy )
31  : QComboBox( parent )
32 {
33  QList < QPair<QgsAuthCertUtils::CertTrustPolicy, QString> > policies;
34  policies << qMakePair( QgsAuthCertUtils::DefaultTrust,
35  defaultTrustText( defaultpolicy ) )
36  << qMakePair( QgsAuthCertUtils::Trusted,
38  << qMakePair( QgsAuthCertUtils::Untrusted,
40 
41  for ( int i = 0; i < policies.size(); i++ )
42  {
43  QgsAuthCertUtils::CertTrustPolicy polcy = policies.at( i ).first;
44  QString name = policies.at( i ).second;
45  addItem( name, QVariant( static_cast<int>( polcy ) ) );
46  }
47 
48 #if QT_VERSION < QT_VERSION_CHECK(5, 13, 0)
49  setItemData( 1, QgsAuthGuiUtils::greenColor(), Qt::TextColorRole );
50  setItemData( 2, QgsAuthGuiUtils::redColor(), Qt::TextColorRole );
51 #else
52  setItemData( 1, QgsAuthGuiUtils::greenColor(), Qt::ForegroundRole );
53  setItemData( 2, QgsAuthGuiUtils::redColor(), Qt::ForegroundRole );
54 #endif
55 
56  // for styling closed state of combobox
57 // setEditable( true );
58 // lineEdit()->setReadOnly( true );
59 
60  connect( this, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ),
61  this, &QgsAuthCertTrustPolicyComboBox::highlightCurrentIndex );
62 
63  setTrustPolicy( policy );
64  setDefaultTrustPolicy( defaultpolicy );
65 }
66 
68 {
69  return ( QgsAuthCertUtils::CertTrustPolicy )currentData().toInt();
70 }
71 
73 {
74  return ( QgsAuthCertUtils::CertTrustPolicy )itemData( indx ).toInt();
75 }
76 
78 {
79  int idx = findData( QVariant( static_cast<int>( policy ) ) );
80  setCurrentIndex( idx == -1 ? 0 : idx );
81 }
82 
84 {
85  int idx = findData( QVariant( static_cast<int>( QgsAuthCertUtils::DefaultTrust ) ) );
86  setItemText( idx, defaultTrustText( defaultpolicy ) );
87 }
88 
89 void QgsAuthCertTrustPolicyComboBox::highlightCurrentIndex( int indx )
90 {
91  QgsAuthCertUtils::CertTrustPolicy policy = ( QgsAuthCertUtils::CertTrustPolicy )itemData( indx ).toInt();
92  QString ss;
93 
94  // TODO: why are these widget state selectors backwards?
95  switch ( policy )
96  {
98  // ss = QgsAuthCertUtils::greenTextStyleSheet( "QLineEdit" );
99  ss = QgsAuthGuiUtils::greenTextStyleSheet( QStringLiteral( "QComboBox:open" ) ) + "\nQComboBox:!open{}";
100  break;
102  // ss = QgsAuthCertUtils::redTextStyleSheet( "QLineEdit" );
103  ss = QgsAuthGuiUtils::redTextStyleSheet( QStringLiteral( "QComboBox:open" ) ) + "\nQComboBox:!open{}";
104  break;
106  default:
107  break;
108  }
109  QgsDebugMsg( QStringLiteral( "Set Stylesheet to : %1" ).arg( ss ) );
110  // lineEdit()->setStyleSheet( ss );
111  setStyleSheet( ss );
112 }
113 
114 const QString QgsAuthCertTrustPolicyComboBox::defaultTrustText( QgsAuthCertUtils::CertTrustPolicy defaultpolicy )
115 {
116  if ( defaultpolicy == QgsAuthCertUtils::DefaultTrust )
117  {
118  if ( !QgsApplication::authManager()->isDisabled() )
119  {
121  }
122  else
123  {
124  defaultpolicy = QgsAuthCertUtils::Trusted;
125  }
126  }
127  return QStringLiteral( "%1 (%2)" )
129  QgsAuthCertUtils::getCertTrustName( defaultpolicy ) );
130 }
static QgsAuthManager * authManager()
Returns the application's authentication manager instance.
QgsAuthCertUtils::CertTrustPolicy trustPolicyForIndex(int indx)
Gets trust policy for a given index of combobox.
QgsAuthCertUtils::CertTrustPolicy trustPolicy()
Gets currently set trust policy.
void setDefaultTrustPolicy(QgsAuthCertUtils::CertTrustPolicy defaultpolicy)
Sets default trust policy.
void setTrustPolicy(QgsAuthCertUtils::CertTrustPolicy policy)
Sets current trust policy.
QgsAuthCertTrustPolicyComboBox(QWidget *parent=nullptr, QgsAuthCertUtils::CertTrustPolicy policy=QgsAuthCertUtils::DefaultTrust, QgsAuthCertUtils::CertTrustPolicy defaultpolicy=QgsAuthCertUtils::DefaultTrust)
Construct a combo box for defining certificate trust policy.
static QString getCertTrustName(QgsAuthCertUtils::CertTrustPolicy trust)
Gets the general name for certificate trust.
CertTrustPolicy
Type of certificate trust policy.
static QString greenTextStyleSheet(const QString &selector="*")
Green text stylesheet representing valid, trusted, etc. certificate.
static QColor greenColor()
Green color representing valid, trusted, etc. certificate.
static QString redTextStyleSheet(const QString &selector="*")
Red text stylesheet representing invalid, untrusted, etc. certificate.
static QColor redColor()
Red color representing invalid, untrusted, etc. certificate.
QgsAuthCertUtils::CertTrustPolicy defaultCertTrustPolicy()
Gets the default certificate trust policy preferred by user.
#define QgsDebugMsg(str)
Definition: qgslogger.h:38