QGIS API Documentation  3.6.0-Noosa (5873452)
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  setItemData( 1, QgsAuthGuiUtils::greenColor(), Qt::TextColorRole );
49  setItemData( 2, QgsAuthGuiUtils::redColor(), Qt::TextColorRole );
50 
51  // for styling closed state of combobox
52 // setEditable( true );
53 // lineEdit()->setReadOnly( true );
54 
55  connect( this, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ),
56  this, &QgsAuthCertTrustPolicyComboBox::highlightCurrentIndex );
57 
58  setTrustPolicy( policy );
59  setDefaultTrustPolicy( defaultpolicy );
60 }
61 
63 {
64  return ( QgsAuthCertUtils::CertTrustPolicy )currentData().toInt();
65 }
66 
68 {
69  return ( QgsAuthCertUtils::CertTrustPolicy )itemData( indx ).toInt();
70 }
71 
73 {
74  int idx = findData( QVariant( static_cast<int>( policy ) ) );
75  setCurrentIndex( idx == -1 ? 0 : idx );
76 }
77 
79 {
80  int idx = findData( QVariant( static_cast<int>( QgsAuthCertUtils::DefaultTrust ) ) );
81  setItemText( idx, defaultTrustText( defaultpolicy ) );
82 }
83 
84 void QgsAuthCertTrustPolicyComboBox::highlightCurrentIndex( int indx )
85 {
86  QgsAuthCertUtils::CertTrustPolicy policy = ( QgsAuthCertUtils::CertTrustPolicy )itemData( indx ).toInt();
87  QString ss;
88 
89  // TODO: why are these widget state selectors backwards?
90  switch ( policy )
91  {
93  // ss = QgsAuthCertUtils::greenTextStyleSheet( "QLineEdit" );
94  ss = QgsAuthGuiUtils::greenTextStyleSheet( QStringLiteral( "QComboBox:open" ) ) + "\nQComboBox:!open{}";
95  break;
97  // ss = QgsAuthCertUtils::redTextStyleSheet( "QLineEdit" );
98  ss = QgsAuthGuiUtils::redTextStyleSheet( QStringLiteral( "QComboBox:open" ) ) + "\nQComboBox:!open{}";
99  break;
101  default:
102  break;
103  }
104  QgsDebugMsg( QStringLiteral( "Set Stylesheet to : %1" ).arg( ss ) );
105  // lineEdit()->setStyleSheet( ss );
106  setStyleSheet( ss );
107 }
108 
109 const QString QgsAuthCertTrustPolicyComboBox::defaultTrustText( QgsAuthCertUtils::CertTrustPolicy defaultpolicy )
110 {
111  if ( defaultpolicy == QgsAuthCertUtils::DefaultTrust )
112  {
113  if ( !QgsApplication::authManager()->isDisabled() )
114  {
116  }
117  else
118  {
119  defaultpolicy = QgsAuthCertUtils::Trusted;
120  }
121  }
122  return QStringLiteral( "%1 (%2)" )
124  QgsAuthCertUtils::getCertTrustName( defaultpolicy ) );
125 }
QgsAuthCertUtils::CertTrustPolicy trustPolicy()
Gets currently set trust policy.
#define QgsDebugMsg(str)
Definition: qgslogger.h:38
static QColor redColor()
Red color representing invalid, untrusted, etc. certificate.
static QColor greenColor()
Green color representing valid, trusted, etc. certificate.
static QString greenTextStyleSheet(const QString &selector="*")
Green text stylesheet representing valid, trusted, etc. certificate.
QgsAuthCertTrustPolicyComboBox(QWidget *parent=nullptr, QgsAuthCertUtils::CertTrustPolicy policy=QgsAuthCertUtils::DefaultTrust, QgsAuthCertUtils::CertTrustPolicy defaultpolicy=QgsAuthCertUtils::DefaultTrust)
Construct a combo box for defining certificate trust policy.
static QgsAuthManager * authManager()
Returns the application&#39;s authentication manager instance.
static QString redTextStyleSheet(const QString &selector="*")
Red text stylesheet representing invalid, untrusted, etc. certificate.
QgsAuthCertUtils::CertTrustPolicy trustPolicyForIndex(int indx)
Gets trust policy for a given index of combobox.
void setDefaultTrustPolicy(QgsAuthCertUtils::CertTrustPolicy defaultpolicy)
Sets default trust policy.
CertTrustPolicy
Type of certificate trust policy.
QgsAuthCertUtils::CertTrustPolicy defaultCertTrustPolicy()
Gets the default certificate trust policy preferred by user.
static QString getCertTrustName(QgsAuthCertUtils::CertTrustPolicy trust)
Gets the general name for certificate trust.
void setTrustPolicy(QgsAuthCertUtils::CertTrustPolicy policy)
Sets current trust policy.