Skip to content

Commit c74ddf1

Browse files
committedSep 26, 2015
[auth] Add widget for editing an authcfg within a data source URI
1 parent 2f8e684 commit c74ddf1

File tree

5 files changed

+518
-33
lines changed

5 files changed

+518
-33
lines changed
 

‎python/gui/auth/qgsauthconfigselect.sip

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,38 @@ class QgsAuthConfigSelect : QWidget
99
~QgsAuthConfigSelect();
1010

1111
void setConfigId( const QString& authcfg );
12+
1213
const QString configId() const;
14+
15+
void setDataProviderKey( const QString &key );
16+
17+
signals:
18+
void selectedConfigIdChanged( const QString& authcfg );
19+
20+
void selectedConfigIdRemoved( const QString& authcfg );
21+
22+
public slots:
23+
void showMessage( const QString &msg );
24+
25+
void clearMessage();
26+
};
27+
28+
29+
class QgsAuthConfigUriEdit : QDialog
30+
{
31+
%TypeHeaderCode
32+
#include <qgsauthconfigselect.h>
33+
%End
34+
35+
public:
36+
explicit QgsAuthConfigUriEdit( QWidget *parent /TransferThis/ = 0,
37+
const QString &datauri = QString(),
38+
const QString &dataprovider = QString() );
39+
~QgsAuthConfigUriEdit();
40+
41+
void setDataSourceUri( const QString &datauri );
42+
43+
QString dataSourceUri();
44+
45+
static bool hasConfigID( const QString &txt );
1346
};

‎src/gui/auth/qgsauthconfigselect.cpp

Lines changed: 212 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,13 @@
1919

2020
#include <QHash>
2121
#include <QMessageBox>
22+
#include <QTimer>
2223

2324
#include "qgsauthconfig.h"
25+
#include "qgsauthguiutils.h"
2426
#include "qgsauthmanager.h"
2527
#include "qgsauthconfigedit.h"
28+
#include "qgslogger.h"
2629

2730

2831
QgsAuthConfigSelect::QgsAuthConfigSelect( QWidget *parent, const QString &dataprovider )
@@ -44,7 +47,11 @@ QgsAuthConfigSelect::QgsAuthConfigSelect( QWidget *parent, const QString &datapr
4447
{
4548
setupUi( this );
4649

50+
leConfigMsg->setStyleSheet( QString( "QLineEdit{background-color: %1}" )
51+
.arg( QgsAuthGuiUtils::yellowColor().name() ) );
52+
4753
clearConfig();
54+
clearMessage();
4855
populateConfigSelector();
4956
}
5057
}
@@ -63,12 +70,20 @@ void QgsAuthConfigSelect::setConfigId( const QString& authcfg )
6370
else
6471
{
6572
if ( mAuthCfg != authcfg )
73+
{
6674
mAuthCfg = authcfg;
75+
}
6776
populateConfigSelector();
6877
loadConfig();
6978
}
7079
}
7180

81+
void QgsAuthConfigSelect::setDataProviderKey( const QString &key )
82+
{
83+
mDataProvider = key;
84+
populateConfigSelector();
85+
}
86+
7287
void QgsAuthConfigSelect::loadConfig()
7388
{
7489
clearConfig();
@@ -87,6 +102,7 @@ void QgsAuthConfigSelect::loadConfig()
87102
btnConfigEdit->setEnabled( true );
88103
btnConfigRemove->setEnabled( true );
89104
}
105+
emit selectedConfigIdChanged( mAuthCfg );
90106
}
91107

92108
void QgsAuthConfigSelect::clearConfig()
@@ -101,6 +117,7 @@ void QgsAuthConfigSelect::validateConfig()
101117
{
102118
if ( !mAuthCfg.isEmpty() && !mConfigs.contains( mAuthCfg ) )
103119
{
120+
showMessage( tr( "Configuration '%1' not in database" ).arg( mAuthCfg ) );
104121
mAuthCfg.clear();
105122
}
106123
}
@@ -137,6 +154,18 @@ void QgsAuthConfigSelect::populateConfigSelector()
137154
cmbConfigSelect->setCurrentIndex( indx > 0 ? indx : 0 );
138155
}
139156

157+
void QgsAuthConfigSelect::showMessage( const QString &msg )
158+
{
159+
leConfigMsg->setText( msg );
160+
frConfigMsg->setVisible( true );
161+
}
162+
163+
void QgsAuthConfigSelect::clearMessage()
164+
{
165+
leConfigMsg->clear();
166+
frConfigMsg->setVisible( false );
167+
}
168+
140169
void QgsAuthConfigSelect::loadAvailableConfigs()
141170
{
142171
mConfigs.clear();
@@ -173,7 +202,7 @@ void QgsAuthConfigSelect::on_btnConfigEdit_clicked()
173202
ace->setWindowModality( Qt::WindowModal );
174203
if ( ace->exec() )
175204
{
176-
qDebug( "Edit returned config Id: %s", ace->configId().toAscii().constData() );
205+
//qDebug( "Edit returned config Id: %s", ace->configId().toAscii().constData() );
177206
setConfigId( ace->configId() );
178207
}
179208
ace->deleteLater();
@@ -192,6 +221,188 @@ void QgsAuthConfigSelect::on_btnConfigRemove_clicked()
192221

193222
if ( QgsAuthManager::instance()->removeAuthenticationConfig( mAuthCfg ) )
194223
{
224+
emit selectedConfigIdRemoved( mAuthCfg );
195225
setConfigId( QString() );
196226
}
197227
}
228+
229+
void QgsAuthConfigSelect::on_btnConfigMsgClear_clicked()
230+
{
231+
clearMessage();
232+
}
233+
234+
235+
//////////////// Embed in dialog ///////////////////
236+
237+
#include <QPushButton>
238+
239+
QgsAuthConfigUriEdit::QgsAuthConfigUriEdit( QWidget *parent, const QString &datauri, const QString &dataprovider )
240+
: QDialog( parent )
241+
, mAuthCfg( QString() )
242+
, mDataUri( QString() )
243+
, mDataUriOrig( QString() )
244+
{
245+
setupUi( this );
246+
247+
setWindowTitle( tr( "Authentication Config ID String Editor" ) );
248+
249+
buttonBox->button( QDialogButtonBox::Close )->setDefault( true );
250+
connect( buttonBox, SIGNAL( rejected() ), this, SLOT( close() ) );
251+
connect( buttonBox, SIGNAL( accepted() ), this, SLOT( saveChanges() ) );
252+
253+
connect( buttonBox->button( QDialogButtonBox::Reset ), SIGNAL( clicked() ), this, SLOT( resetChanges() ) );
254+
255+
connect( wdgtAuthSelect, SIGNAL( selectedConfigIdChanged( QString ) ), this , SLOT( authCfgUpdated( QString ) ) );
256+
connect( wdgtAuthSelect, SIGNAL( selectedConfigIdRemoved( QString ) ), this , SLOT( authCfgRemoved( QString ) ) );
257+
258+
wdgtAuthSelect->setDataProviderKey( dataprovider );
259+
setDataSourceUri( datauri );
260+
}
261+
262+
QgsAuthConfigUriEdit::~QgsAuthConfigUriEdit()
263+
{
264+
}
265+
266+
void QgsAuthConfigUriEdit::setDataSourceUri( const QString &datauri )
267+
{
268+
if ( datauri.isEmpty() )
269+
return;
270+
271+
mDataUri = mDataUriOrig = datauri;
272+
273+
teDataUri->setPlainText( mDataUri );
274+
275+
if ( authCfgIndex() == -1 )
276+
{
277+
wdgtAuthSelect->showMessage( tr( "No authcfg in Data Source URI" ) );
278+
return;
279+
}
280+
281+
selectAuthCfgInUri();
282+
283+
mAuthCfg = authCfgFromUri();
284+
285+
QgsDebugMsg( QString( "Parsed authcfg ID: %1" ).arg( mAuthCfg ) );
286+
287+
wdgtAuthSelect->blockSignals( true );
288+
wdgtAuthSelect->setConfigId( mAuthCfg );
289+
wdgtAuthSelect->blockSignals( false );
290+
}
291+
292+
QString QgsAuthConfigUriEdit::dataSourceUri()
293+
{
294+
return mDataUri;
295+
}
296+
297+
bool QgsAuthConfigUriEdit::hasConfigID( const QString &txt )
298+
{
299+
return QgsAuthManager::instance()->hasConfigId( txt );
300+
}
301+
302+
void QgsAuthConfigUriEdit::saveChanges()
303+
{
304+
this->accept();
305+
}
306+
307+
void QgsAuthConfigUriEdit::resetChanges()
308+
{
309+
wdgtAuthSelect->clearMessage();
310+
setDataSourceUri( mDataUriOrig );
311+
}
312+
313+
void QgsAuthConfigUriEdit::authCfgUpdated( const QString &authcfg )
314+
{
315+
mAuthCfg = authcfg;
316+
317+
if ( mAuthCfg.size() != 7 )
318+
{
319+
mAuthCfg.clear();
320+
removeAuthCfgFromUri();
321+
}
322+
else
323+
{
324+
updateUriWithAuthCfg();
325+
}
326+
teDataUri->clear();
327+
teDataUri->setPlainText( mDataUri );
328+
selectAuthCfgInUri();
329+
}
330+
331+
void QgsAuthConfigUriEdit::authCfgRemoved( const QString &authcfg )
332+
{
333+
if ( authCfgFromUri() == authcfg )
334+
{
335+
removeAuthCfgFromUri();
336+
}
337+
}
338+
339+
int QgsAuthConfigUriEdit::authCfgIndex()
340+
{
341+
QRegExp rx( QgsAuthManager::instance()->configIdRegex() );
342+
return rx.indexIn( mDataUri );
343+
}
344+
345+
QString QgsAuthConfigUriEdit::authCfgFromUri()
346+
{
347+
int startindex = authCfgIndex();
348+
if ( startindex == -1 )
349+
return QString();
350+
351+
return mDataUri.mid( startindex + 8, 7 );
352+
}
353+
354+
void QgsAuthConfigUriEdit::selectAuthCfgInUri()
355+
{
356+
int startindex = authCfgIndex();
357+
if ( startindex == -1 )
358+
return;
359+
360+
// authcfg=.{7} will always be 15 chars
361+
QTextCursor tc = teDataUri->textCursor();
362+
tc.setPosition( startindex );
363+
tc.setPosition( startindex + 15, QTextCursor::KeepAnchor );
364+
teDataUri->setTextCursor( tc );
365+
teDataUri->setFocus();
366+
}
367+
368+
void QgsAuthConfigUriEdit::updateUriWithAuthCfg()
369+
{
370+
int startindex = authCfgIndex();
371+
if ( startindex == -1 )
372+
{
373+
if ( mAuthCfg.size() == 7 )
374+
{
375+
wdgtAuthSelect->showMessage( tr( "Adding authcfg to URI not supported" ) );
376+
}
377+
return;
378+
}
379+
380+
mDataUri = mDataUri.replace( startindex + 8, 7, mAuthCfg );
381+
}
382+
383+
void QgsAuthConfigUriEdit::removeAuthCfgFromUri()
384+
{
385+
int startindex = authCfgIndex();
386+
if ( startindex == -1 )
387+
return;
388+
389+
// add any preceding space so two spaces will not result after removal
390+
int rmvlen = 15;
391+
if ( startindex - 1 >= 0
392+
&& ( mDataUri.at( startindex - 1 ).isSpace()
393+
|| mDataUri.at( startindex - 1 ) == QChar( '&' ) ) )
394+
{
395+
startindex -= 1;
396+
rmvlen += 1;
397+
}
398+
399+
// trim any leftover spaces or & from ends
400+
mDataUri = mDataUri.remove( startindex, rmvlen ).trimmed();
401+
if ( mDataUri.at( 0 ) == QChar( '&' ) )
402+
mDataUri = mDataUri.remove( 0, 1 );
403+
404+
// trim any & from
405+
406+
mAuthCfg.clear();
407+
}
408+

‎src/gui/auth/qgsauthconfigselect.h

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,23 @@ class GUI_EXPORT QgsAuthConfigSelect : public QWidget, private Ui::QgsAuthConfig
4646
/** Get the authentication config id for the resource */
4747
const QString configId() const { return mAuthCfg; }
4848

49+
/** Set key of layer provider, if applicable */
50+
void setDataProviderKey( const QString &key );
51+
52+
signals:
53+
/** Emitted when authentication config is changed or missing */
54+
void selectedConfigIdChanged( const QString& authcfg );
55+
56+
/** Emitted when authentication config is removed */
57+
void selectedConfigIdRemoved( const QString& authcfg );
58+
59+
public slots:
60+
/** Show a small message bar with a close button */
61+
void showMessage( const QString &msg );
62+
63+
/** Clear and hide small message bar */
64+
void clearMessage();
65+
4966
private slots:
5067
void loadConfig();
5168
void clearConfig();
@@ -60,6 +77,8 @@ class GUI_EXPORT QgsAuthConfigSelect : public QWidget, private Ui::QgsAuthConfig
6077

6178
void on_btnConfigRemove_clicked();
6279

80+
void on_btnConfigMsgClear_clicked();
81+
6382
private:
6483
void loadAvailableConfigs();
6584

@@ -71,4 +90,65 @@ class GUI_EXPORT QgsAuthConfigSelect : public QWidget, private Ui::QgsAuthConfig
7190
QLabel *mAuthNotify;
7291
};
7392

93+
94+
//////////////// Embed in dialog ///////////////////
95+
96+
#include "ui_qgsauthconfiguriedit.h"
97+
98+
class QPushButton;
99+
100+
/** \ingroup gui
101+
* Dialog wrapper of select widget to edit an authcfg in a data source URI
102+
*/
103+
class GUI_EXPORT QgsAuthConfigUriEdit : public QDialog, private Ui::QgsAuthConfigUriEdit
104+
{
105+
Q_OBJECT
106+
107+
public:
108+
/**
109+
* Construct wrapper dialog for select widget to edit an authcfg in a data source URI
110+
* @param parent Parent widget
111+
* @param datauri URI QString with of without an authcfg=ID string
112+
* @param dataprovider The key of the calling layer provider, if applicable
113+
*/
114+
explicit QgsAuthConfigUriEdit( QWidget *parent = 0,
115+
const QString &datauri = QString(),
116+
const QString &dataprovider = QString() );
117+
~QgsAuthConfigUriEdit();
118+
119+
/** Set the data source URI to parse */
120+
void setDataSourceUri( const QString &datauri );
121+
122+
/** The returned, possibly edited data source URI */
123+
QString dataSourceUri();
124+
125+
/** Whether a string conatins an authcfg ID */
126+
static bool hasConfigID( const QString &txt );
127+
128+
private slots:
129+
void saveChanges();
130+
131+
void resetChanges();
132+
133+
void authCfgUpdated( const QString &authcfg );
134+
135+
void authCfgRemoved( const QString &authcfg );
136+
137+
private:
138+
int authCfgIndex();
139+
140+
QString authCfgFromUri();
141+
142+
void selectAuthCfgInUri();
143+
144+
void updateUriWithAuthCfg();
145+
146+
void removeAuthCfgFromUri();
147+
148+
QString mAuthCfg;
149+
QString mDataUri;
150+
QString mDataUriOrig;
151+
QPushButton *mResetButton;
152+
};
153+
74154
#endif // QGSAUTHCONFIGSELECT_H

‎src/ui/auth/qgsauthconfigselect.ui

Lines changed: 96 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,31 @@
1414
<string>Authentication Configuration</string>
1515
</property>
1616
<layout class="QVBoxLayout" name="verticalLayout">
17+
<property name="leftMargin">
18+
<number>6</number>
19+
</property>
20+
<property name="topMargin">
21+
<number>6</number>
22+
</property>
23+
<property name="rightMargin">
24+
<number>6</number>
25+
</property>
26+
<property name="bottomMargin">
27+
<number>6</number>
28+
</property>
1729
<item>
18-
<layout class="QHBoxLayout" name="horizontalLayout_2">
19-
<item>
30+
<layout class="QGridLayout" name="gridLayout">
31+
<property name="verticalSpacing">
32+
<number>10</number>
33+
</property>
34+
<item row="1" column="1">
35+
<widget class="QToolButton" name="btnConfigAdd">
36+
<property name="text">
37+
<string>Add</string>
38+
</property>
39+
</widget>
40+
</item>
41+
<item row="1" column="0">
2042
<widget class="QComboBox" name="cmbConfigSelect">
2143
<property name="sizePolicy">
2244
<sizepolicy hsizetype="MinimumExpanding" vsizetype="Fixed">
@@ -32,43 +54,73 @@
3254
</property>
3355
</widget>
3456
</item>
35-
<item>
36-
<spacer name="horizontalSpacer_2">
37-
<property name="orientation">
38-
<enum>Qt::Horizontal</enum>
57+
<item row="0" column="0" colspan="2">
58+
<widget class="QFrame" name="frConfigMsg">
59+
<property name="frameShape">
60+
<enum>QFrame::StyledPanel</enum>
3961
</property>
40-
<property name="sizeType">
41-
<enum>QSizePolicy::MinimumExpanding</enum>
42-
</property>
43-
<property name="sizeHint" stdset="0">
44-
<size>
45-
<width>0</width>
46-
<height>20</height>
47-
</size>
48-
</property>
49-
</spacer>
50-
</item>
51-
<item>
52-
<widget class="QToolButton" name="btnConfigAdd">
53-
<property name="text">
54-
<string>Add</string>
62+
<property name="frameShadow">
63+
<enum>QFrame::Raised</enum>
5564
</property>
65+
<layout class="QHBoxLayout" name="horizontalLayout_3">
66+
<property name="spacing">
67+
<number>0</number>
68+
</property>
69+
<property name="leftMargin">
70+
<number>0</number>
71+
</property>
72+
<property name="topMargin">
73+
<number>0</number>
74+
</property>
75+
<property name="rightMargin">
76+
<number>0</number>
77+
</property>
78+
<property name="bottomMargin">
79+
<number>0</number>
80+
</property>
81+
<item>
82+
<widget class="QLineEdit" name="leConfigMsg">
83+
<property name="readOnly">
84+
<bool>true</bool>
85+
</property>
86+
</widget>
87+
</item>
88+
<item>
89+
<widget class="QToolButton" name="btnConfigMsgClear">
90+
<property name="minimumSize">
91+
<size>
92+
<width>26</width>
93+
<height>22</height>
94+
</size>
95+
</property>
96+
<property name="maximumSize">
97+
<size>
98+
<width>26</width>
99+
<height>22</height>
100+
</size>
101+
</property>
102+
<property name="text">
103+
<string/>
104+
</property>
105+
<property name="icon">
106+
<iconset resource="../../../images/images.qrc">
107+
<normaloff>:/images/themes/default/mIconClose.png</normaloff>:/images/themes/default/mIconClose.png</iconset>
108+
</property>
109+
<property name="iconSize">
110+
<size>
111+
<width>14</width>
112+
<height>14</height>
113+
</size>
114+
</property>
115+
</widget>
116+
</item>
117+
</layout>
56118
</widget>
57119
</item>
58120
</layout>
59121
</item>
60122
<item>
61123
<layout class="QGridLayout" name="gridLayout_2">
62-
<item row="0" column="1">
63-
<widget class="QLineEdit" name="leConfigMethodDesc">
64-
<property name="styleSheet">
65-
<string notr="true">QLineEdit{color: rgb(128, 128, 128);}</string>
66-
</property>
67-
<property name="readOnly">
68-
<bool>true</bool>
69-
</property>
70-
</widget>
71-
</item>
72124
<item row="0" column="0">
73125
<widget class="QLabel" name="label">
74126
<property name="styleSheet">
@@ -126,6 +178,16 @@
126178
</item>
127179
</layout>
128180
</item>
181+
<item row="0" column="1">
182+
<widget class="QLineEdit" name="leConfigMethodDesc">
183+
<property name="styleSheet">
184+
<string notr="true">QLineEdit{color: rgb(128, 128, 128);}</string>
185+
</property>
186+
<property name="readOnly">
187+
<bool>true</bool>
188+
</property>
189+
</widget>
190+
</item>
129191
</layout>
130192
</item>
131193
<item>
@@ -143,6 +205,8 @@
143205
</item>
144206
</layout>
145207
</widget>
146-
<resources/>
208+
<resources>
209+
<include location="../../../images/images.qrc"/>
210+
</resources>
147211
<connections/>
148212
</ui>

‎src/ui/auth/qgsauthconfiguriedit.ui

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<ui version="4.0">
3+
<class>QgsAuthConfigUriEdit</class>
4+
<widget class="QDialog" name="QgsAuthConfigUriEdit">
5+
<property name="geometry">
6+
<rect>
7+
<x>0</x>
8+
<y>0</y>
9+
<width>449</width>
10+
<height>449</height>
11+
</rect>
12+
</property>
13+
<property name="windowTitle">
14+
<string>Dialog</string>
15+
</property>
16+
<layout class="QVBoxLayout" name="verticalLayout">
17+
<item>
18+
<widget class="QGroupBox" name="groupBox">
19+
<property name="title">
20+
<string>Edit authentication configuration ID</string>
21+
</property>
22+
<layout class="QVBoxLayout" name="verticalLayout_2">
23+
<property name="leftMargin">
24+
<number>6</number>
25+
</property>
26+
<property name="rightMargin">
27+
<number>6</number>
28+
</property>
29+
<property name="bottomMargin">
30+
<number>6</number>
31+
</property>
32+
<item>
33+
<widget class="QPlainTextEdit" name="teDataUri">
34+
<property name="maximumSize">
35+
<size>
36+
<width>16777215</width>
37+
<height>75</height>
38+
</size>
39+
</property>
40+
<property name="readOnly">
41+
<bool>true</bool>
42+
</property>
43+
</widget>
44+
</item>
45+
<item>
46+
<widget class="QgsAuthConfigSelect" name="wdgtAuthSelect" native="true"/>
47+
</item>
48+
<item>
49+
<widget class="QLabel" name="label">
50+
<property name="sizePolicy">
51+
<sizepolicy hsizetype="Preferred" vsizetype="Maximum">
52+
<horstretch>0</horstretch>
53+
<verstretch>0</verstretch>
54+
</sizepolicy>
55+
</property>
56+
<property name="font">
57+
<font>
58+
<italic>true</italic>
59+
</font>
60+
</property>
61+
<property name="styleSheet">
62+
<string notr="true">color: rgb(128, 128, 128);</string>
63+
</property>
64+
<property name="text">
65+
<string>Note: Button actions above affect authentication database</string>
66+
</property>
67+
<property name="alignment">
68+
<set>Qt::AlignCenter</set>
69+
</property>
70+
<property name="wordWrap">
71+
<bool>true</bool>
72+
</property>
73+
</widget>
74+
</item>
75+
</layout>
76+
</widget>
77+
</item>
78+
<item>
79+
<widget class="QDialogButtonBox" name="buttonBox">
80+
<property name="standardButtons">
81+
<set>QDialogButtonBox::Close|QDialogButtonBox::Reset|QDialogButtonBox::Save</set>
82+
</property>
83+
</widget>
84+
</item>
85+
</layout>
86+
</widget>
87+
<customwidgets>
88+
<customwidget>
89+
<class>QgsAuthConfigSelect</class>
90+
<extends>QWidget</extends>
91+
<header>qgsauthconfigselect.h</header>
92+
<container>1</container>
93+
</customwidget>
94+
</customwidgets>
95+
<resources/>
96+
<connections/>
97+
</ui>

0 commit comments

Comments
 (0)
Please sign in to comment.