QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
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 
26 
29  QgsAuthCertUtils::CertTrustPolicy defaultpolicy )
30  : QComboBox( parent )
31 {
33  policies << qMakePair( QgsAuthCertUtils::DefaultTrust,
34  defaultTrustText( defaultpolicy ) )
35  << qMakePair( QgsAuthCertUtils::Trusted,
37  << qMakePair( QgsAuthCertUtils::Untrusted,
39 
40  for ( int i = 0; i < policies.size(); i++ )
41  {
42  QgsAuthCertUtils::CertTrustPolicy polcy = policies.at( i ).first;
43  QString name = policies.at( i ).second;
44  addItem( name, QVariant(( int )polcy ) );
45  }
46 
47  setItemData( 1, QgsAuthGuiUtils::greenColor(), Qt::TextColorRole );
48  setItemData( 2, QgsAuthGuiUtils::redColor(), Qt::TextColorRole );
49 
50  // for styling closed state of combobox
51 // setEditable( true );
52 // lineEdit()->setReadOnly( true );
53 
54  connect( this, SIGNAL( currentIndexChanged( int ) ),
55  this, SLOT( highlightCurrentIndex( int ) ) );
56 
57  setTrustPolicy( policy );
58  setDefaultTrustPolicy( defaultpolicy );
59 }
60 
62 {
63 }
64 
66 {
68 }
69 
71 {
73 }
74 
76 {
77  int idx = findData( QVariant(( int )policy ) );
78  setCurrentIndex( idx == -1 ? 0 : idx );
79 }
80 
82 {
83  int idx = findData( QVariant(( int )QgsAuthCertUtils::DefaultTrust ) );
84  setItemText( idx, defaultTrustText( defaultpolicy ) );
85 }
86 
87 void QgsAuthCertTrustPolicyComboBox::highlightCurrentIndex( int indx )
88 {
90  QString ss( "" );
91 
92  // TODO: why are these widget state selectors backwards?
93  switch ( policy )
94  {
96  // ss = QgsAuthCertUtils::greenTextStyleSheet( "QLineEdit" );
97  ss = QgsAuthGuiUtils::greenTextStyleSheet( "QComboBox:open" ) + "\nQComboBox:!open{}";
98  break;
100  // ss = QgsAuthCertUtils::redTextStyleSheet( "QLineEdit" );
101  ss = QgsAuthGuiUtils::redTextStyleSheet( "QComboBox:open" ) + "\nQComboBox:!open{}";
102  break;
104  default:
105  break;
106  }
107  QgsDebugMsg( QString( "Set Stylesheet to : %1" ).arg( ss ) );
108  // lineEdit()->setStyleSheet( ss );
109  setStyleSheet( ss );
110 }
111 
112 const QString QgsAuthCertTrustPolicyComboBox::defaultTrustText( QgsAuthCertUtils::CertTrustPolicy defaultpolicy )
113 {
114  if ( defaultpolicy == QgsAuthCertUtils::DefaultTrust )
115  {
116  if ( !QgsAuthManager::instance()->isDisabled() )
117  {
119  }
120  else
121  {
122  defaultpolicy = QgsAuthCertUtils::Trusted;
123  }
124  }
125  return QString( "%1 (%2)" )
127  QgsAuthCertUtils::getCertTrustName( defaultpolicy ) );
128 }
void setStyleSheet(const QString &styleSheet)
static QgsAuthManager * instance()
Enforce singleton pattern.
QgsAuthCertUtils::CertTrustPolicy trustPolicy()
Get currently set trust policy.
#define QgsDebugMsg(str)
Definition: qgslogger.h:33
const T & at(int i) const
void setItemText(int index, const QString &text)
int size() const
void addItem(const QString &text, const QVariant &userData)
const char * name() const
static QColor redColor()
Red color representing invalid, untrusted, etc.
static QColor greenColor()
Green color representing valid, trusted, etc.
int toInt(bool *ok) const
static QString greenTextStyleSheet(const QString &selector="*")
Green text stylesheet representing valid, trusted, etc.
QgsAuthCertTrustPolicyComboBox(QWidget *parent=nullptr, QgsAuthCertUtils::CertTrustPolicy policy=QgsAuthCertUtils::DefaultTrust, QgsAuthCertUtils::CertTrustPolicy defaultpolicy=QgsAuthCertUtils::DefaultTrust)
Construct a combo box for defining certificate trust policy.
QVariant itemData(int index, int role) const
static QString redTextStyleSheet(const QString &selector="*")
Red text stylesheet representing invalid, untrusted, etc.
int findData(const QVariant &data, int role, QFlags< Qt::MatchFlag > flags) const
QgsAuthCertUtils::CertTrustPolicy trustPolicyForIndex(int indx)
Get trust policy for a given index of combobox.
void setItemData(int index, const QVariant &value, int role)
int currentIndex() const
void setDefaultTrustPolicy(QgsAuthCertUtils::CertTrustPolicy defaultpolicy)
Set default trust policy.
CertTrustPolicy
Type of certificate trust policy.
QgsAuthCertUtils::CertTrustPolicy defaultCertTrustPolicy()
Get the default certificate trust policy perferred by user.
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
static QString getCertTrustName(QgsAuthCertUtils::CertTrustPolicy trust)
Get the general name for certificate trust.
QString arg(qlonglong a, int fieldWidth, int base, const QChar &fillChar) const
void setTrustPolicy(QgsAuthCertUtils::CertTrustPolicy policy)
Set current trust policy.
void currentIndexChanged(int index)