QGIS API Documentation  3.25.0-Master (10b47c2603)
qgshttpheaderwidget.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgshttpheaderswidget.cpp
3  This class implements simple UI for http header.
4 
5  -------------------
6  begin : 2021-09-09
7  copyright : (C) 2021 B. De Mezzo
8  email : benoit dot de dot mezzo at oslandia dot com
9 
10 ***************************************************************************/
11 
12 /***************************************************************************
13  * *
14  * This program is free software; you can redistribute it and/or modify *
15  * it under the terms of the GNU General Public License as published by *
16  * the Free Software Foundation; either version 2 of the License, or *
17  * (at your option) any later version. *
18  * *
19  ***************************************************************************/
20 
21 #include "qgshttpheaderwidget.h"
22 #include "ui_qgshttpheaderwidget.h"
23 #include "qgsapplication.h"
24 
25 
27  : QWidget( parent )
28 {
29  setupUi( this );
30  btnAddQueryPair->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "/symbologyAdd.svg" ) ) );
31  btnRemoveQueryPair->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "/symbologyRemove.svg" ) ) );
32  grpbxAdvanced->setCollapsed( true );
33 
34  // Action and interaction connections
35  connect( btnAddQueryPair, &QToolButton::clicked, this, &QgsHttpHeaderWidget::addQueryPair );
36  connect( btnRemoveQueryPair, &QToolButton::clicked, this, &QgsHttpHeaderWidget::removeQueryPair );
37 }
38 
40 
41 void QgsHttpHeaderWidget::addQueryPairRow( const QString &key, const QString &val )
42 {
43  const int rowCnt = tblwdgQueryPairs->rowCount();
44  tblwdgQueryPairs->insertRow( rowCnt );
45 
46  const Qt::ItemFlags itemFlags = Qt::ItemIsEnabled | Qt::ItemIsSelectable
47  | Qt::ItemIsEditable | Qt::ItemIsDropEnabled;
48 
49  QTableWidgetItem *keyItem = new QTableWidgetItem( key );
50  keyItem->setFlags( itemFlags );
51  tblwdgQueryPairs->setItem( rowCnt, 0, keyItem );
52 
53  QTableWidgetItem *valueItem = new QTableWidgetItem( val );
54  keyItem->setFlags( itemFlags );
55  tblwdgQueryPairs->setItem( rowCnt, 1, valueItem );
56 }
57 
59 {
60  QgsHttpHeaders querypairs;
61  for ( int i = 0; i < tblwdgQueryPairs->rowCount(); ++i )
62  {
63  if ( tblwdgQueryPairs->item( i, 0 )->text().isEmpty() )
64  {
65  continue;
66  }
67  querypairs [ tblwdgQueryPairs->item( i, 0 )->text() ] = QVariant( tblwdgQueryPairs->item( i, 1 )->text() ) ;
68  }
69 
70  if ( !mRefererLineEdit->text().isEmpty() )
71  {
72  querypairs [ "referer" ] = QVariant( mRefererLineEdit->text() ) ;
73  }
74  return querypairs;
75 }
76 
77 
78 void QgsHttpHeaderWidget::addQueryPair()
79 {
80  addQueryPairRow( QString(), QString() );
81  tblwdgQueryPairs->setFocus();
82  tblwdgQueryPairs->setCurrentCell( tblwdgQueryPairs->rowCount() - 1, 0 );
83  tblwdgQueryPairs->edit( tblwdgQueryPairs->currentIndex() );
84 }
85 
86 
87 void QgsHttpHeaderWidget::removeQueryPair()
88 {
89  tblwdgQueryPairs->removeRow( tblwdgQueryPairs->currentRow() );
90 }
91 
92 
93 void QgsHttpHeaderWidget::setFromSettings( const QgsSettings &settings, const QString &key )
94 {
95  // load headers from settings
96  QgsHttpHeaders headers;
97  headers.setFromSettings( settings, key );
98 
99  // push headers to table
100  tblwdgQueryPairs->clearContents();
101  /*QTableWidgetItem * item;
102  int row = 0;*/
103  QList<QString> lst = headers.keys();
104  for ( auto ite = lst.constBegin(); ite != lst.constEnd(); ++ite )
105  {
106  if ( ite->compare( "referer" ) != 0 )
107  {
108  addQueryPairRow( *ite, headers[ *ite ].toString() );
109  }
110  else
111  {
112  mRefererLineEdit->setText( headers[ *ite ].toString() );
113  }
114  }
115 }
116 
117 void QgsHttpHeaderWidget::updateSettings( QgsSettings &settings, const QString &key ) const
118 {
120  h.updateSettings( settings, key );
121 }
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
QgsHttpHeaderWidget(QWidget *parent=nullptr)
Default constructor.
QgsHttpHeaders httpHeaders() const
void updateSettings(QgsSettings &settings, const QString &key) const
update the settings with the http headers present in the inner map.
void setFromSettings(const QgsSettings &settings, const QString &key)
fill the inner header map from the settings defined at key
This class implements simple http header management.
QList< QString > keys() const
void setFromSettings(const QgsSettings &settings, const QString &key=QString())
Loads headers from the settings.
void updateSettings(QgsSettings &settings, const QString &key=QString()) const
Updates the settings by adding all the http headers in the path "key/KEY_PREFIX/".
This class is a composition of two QSettings instances:
Definition: qgssettings.h:62