Skip to content

Commit 8fc4378

Browse files
authoredOct 29, 2018
Merge pull request #8114 from signedav/bugfix_not_deleting_relation
More intuitive relation reference widget text filter
2 parents d4cf8cf + 9768fdd commit 8fc4378

File tree

7 files changed

+89
-3
lines changed

7 files changed

+89
-3
lines changed
 

‎python/gui/auto_generated/qgsfilterlineedit.sip.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,8 @@ Will select all text when this widget receives the focus.
268268
protected:
269269
virtual void focusInEvent( QFocusEvent *e );
270270

271+
virtual void mouseReleaseEvent( QMouseEvent *e );
272+
271273

272274
};
273275

‎src/gui/qgsfeaturelistcombobox.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,10 @@ QgsFeatureListComboBox::QgsFeatureListComboBox( QWidget *parent )
5151

5252
connect( this, static_cast<void( QgsFeatureListComboBox::* )( int )>( &QgsFeatureListComboBox::currentIndexChanged ), this, &QgsFeatureListComboBox::onCurrentIndexChanged );
5353

54-
mLineEdit = new QgsFilterLineEdit();
54+
mLineEdit = new QgsFilterLineEdit( nullptr, QgsApplication::nullRepresentation() );
5555
mLineEdit->setSelectOnFocus( true );
56+
mLineEdit->setShowClearButton( true );
57+
5658
setEditable( true );
5759
setLineEdit( mLineEdit );
5860
setModel( mModel );
@@ -109,7 +111,8 @@ void QgsFeatureListComboBox::onItemSelected( const QModelIndex &index )
109111

110112
void QgsFeatureListComboBox::onCurrentIndexChanged( int i )
111113
{
112-
mIsCurrentlyEdited = false;
114+
if ( !mHasStoredEditState )
115+
mIsCurrentlyEdited = false;
113116
QModelIndex modelIndex = mModel->index( i, 0, QModelIndex() );
114117
mModel->setExtraIdentifierValue( mModel->data( modelIndex, QgsFeatureFilterModel::IdentifierValueRole ) );
115118
mLineEdit->setText( mModel->data( modelIndex, QgsFeatureFilterModel::ValueRole ).toString() );
@@ -128,13 +131,19 @@ void QgsFeatureListComboBox::onActivated( QModelIndex modelIndex )
128131
void QgsFeatureListComboBox::storeLineEditState()
129132
{
130133
if ( mIsCurrentlyEdited )
134+
{
135+
mHasStoredEditState = true;
131136
mLineEditState.store( mLineEdit );
137+
}
132138
}
133139

134140
void QgsFeatureListComboBox::restoreLineEditState()
135141
{
136142
if ( mIsCurrentlyEdited )
143+
{
144+
mHasStoredEditState = false;
137145
mLineEditState.restore( mLineEdit );
146+
}
138147
}
139148

140149
int QgsFeatureListComboBox::nullIndex() const

‎src/gui/qgsfeaturelistcombobox.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,10 @@ class GUI_EXPORT QgsFeatureListComboBox : public QComboBox
219219
QgsFilterLineEdit *mLineEdit;
220220
bool mPopupRequested = false;
221221
bool mIsCurrentlyEdited = false;
222+
bool mHasStoredEditState = false;
222223
LineEditState mLineEditState;
224+
225+
friend class TestQgsFeatureListComboBox;
223226
};
224227

225228
#endif // QGSFIELDLISTCOMBOBOX_H

‎src/gui/qgsfilterlineedit.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,16 @@ void QgsFilterLineEdit::focusInEvent( QFocusEvent *e )
9090
if ( e->reason() == Qt::MouseFocusReason && ( isNull() || mSelectOnFocus ) )
9191
{
9292
mFocusInEvent = true;
93+
mWaitingForMouseRelease = true;
94+
}
95+
}
96+
97+
void QgsFilterLineEdit::mouseReleaseEvent( QMouseEvent *e )
98+
{
99+
QLineEdit::mouseReleaseEvent( e );
100+
if ( mWaitingForMouseRelease )
101+
{
102+
mWaitingForMouseRelease = false;
93103
selectAll();
94104
}
95105
}
@@ -198,7 +208,7 @@ bool QgsFilterLineEdit::shouldShowClear() const
198208

199209
bool QgsFilterLineEdit::event( QEvent *event )
200210
{
201-
if ( event->type() == QEvent::ReadOnlyChange )
211+
if ( event->type() == QEvent::ReadOnlyChange || event->type() == QEvent::EnabledChange )
202212
updateClearIcon();
203213

204214
return QLineEdit::event( event );;

‎src/gui/qgsfilterlineedit.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,7 @@ class GUI_EXPORT QgsFilterLineEdit : public QLineEdit
265265

266266
protected:
267267
void focusInEvent( QFocusEvent *e ) override;
268+
void mouseReleaseEvent( QMouseEvent *e ) override;
268269

269270
private slots:
270271
void onTextChanged( const QString &text );
@@ -286,12 +287,15 @@ class GUI_EXPORT QgsFilterLineEdit : public QLineEdit
286287
QString mDefaultValue;
287288
QString mStyleSheet;
288289
bool mFocusInEvent = false;
290+
bool mWaitingForMouseRelease = false;
289291
bool mSelectOnFocus = false;
290292

291293
QgsAnimatedIcon *mBusySpinnerAnimatedIcon = nullptr;
292294

293295
//! Returns true if clear button should be shown
294296
bool shouldShowClear() const;
297+
298+
friend class TestQgsFeatureListComboBox;
295299
};
296300

297301
/// @cond PRIVATE

‎tests/src/gui/testqgsfeaturelistcombobox.cpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
#include "qgsapplication.h"
2020
#include "qgsfeaturelistcombobox.h"
21+
#include "qgsfilterlineedit.h"
2122
#include "qgsvectorlayer.h"
2223
#include "qgsfeaturefiltermodel.h"
2324
#include "qgsgui.h"
@@ -26,6 +27,8 @@
2627

2728
#include <QLineEdit>
2829

30+
class QgsFilterLineEdit;
31+
2932
class TestQgsFeatureListComboBox : public QObject
3033
{
3134
Q_OBJECT
@@ -41,12 +44,15 @@ class TestQgsFeatureListComboBox : public QObject
4144
void testSetGetLayer();
4245
void testSetGetForeignKey();
4346
void testAllowNull();
47+
void testValuesAndSelection();
4448
void nullRepresentation();
4549

4650
private:
4751
void waitForLoaded( QgsFeatureListComboBox *cb );
4852

4953
std::unique_ptr<QgsVectorLayer> mLayer;
54+
55+
friend class QgsFeatureListComboBox;
5056
};
5157

5258
void TestQgsFeatureListComboBox::initTestCase()
@@ -137,6 +143,46 @@ void TestQgsFeatureListComboBox::testAllowNull()
137143
// Note to self: implement this!
138144
}
139145

146+
void TestQgsFeatureListComboBox::testValuesAndSelection()
147+
{
148+
QgsApplication::setNullRepresentation( QStringLiteral( "nope" ) );
149+
std::unique_ptr<QgsFeatureListComboBox> cb( new QgsFeatureListComboBox() );
150+
151+
cb->setSourceLayer( mLayer.get() );
152+
cb->setDisplayExpression( QStringLiteral( "\"raccord\"" ) );
153+
cb->setAllowNull( true );
154+
155+
//check if everything is fine:
156+
waitForLoaded( cb.get() );
157+
QCOMPARE( cb->currentIndex(), cb->nullIndex() );
158+
QCOMPARE( cb->currentText(), QStringLiteral( "nope" ) );
159+
160+
//check if text correct, selected and if the clear button disappeared:
161+
cb->mLineEdit->clearValue();
162+
waitForLoaded( cb.get() );
163+
QCOMPARE( cb->currentIndex(), cb->nullIndex() );
164+
QCOMPARE( cb->currentText(), QStringLiteral( "nope" ) );
165+
QCOMPARE( cb->lineEdit()->selectedText(), QStringLiteral( "nope" ) );
166+
QVERIFY( ! cb->mLineEdit->mClearAction );
167+
168+
//check if text is selected after receiving focus
169+
cb->setFocus();
170+
waitForLoaded( cb.get() );
171+
QCOMPARE( cb->currentIndex(), cb->nullIndex() );
172+
QCOMPARE( cb->currentText(), QStringLiteral( "nope" ) );
173+
QCOMPARE( cb->lineEdit()->selectedText(), QStringLiteral( "nope" ) );
174+
QVERIFY( ! cb->mLineEdit->mClearAction );
175+
176+
//check with another entry, clear button needs to be there then:
177+
QTest::keyClicks( cb.get(), QStringLiteral( "sleeve" ) );
178+
//QTest::keyClick(cb.get(), Qt::Key_Enter );
179+
waitForLoaded( cb.get() );
180+
QCOMPARE( cb->currentText(), QStringLiteral( "sleeve" ) );
181+
QVERIFY( cb->mLineEdit->mClearAction );
182+
//QVERIFY( cb->currentIndex() != cb->nullIndex());
183+
//QCOMPARE( cb->model()->data( cb->currentModelIndex() ).toString(), QStringLiteral( "sleeve" ) );
184+
}
185+
140186
void TestQgsFeatureListComboBox::nullRepresentation()
141187
{
142188

‎tests/src/python/test_qgsfilterlineedit.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,18 @@ def testClearToDefault(self):
119119
self.assertEqual(w.text(), 'def')
120120
self.assertFalse(w.isNull())
121121

122+
def test_selectedText(self):
123+
""" test that NULL value is selected on focus and not-null value is not"""
124+
w = qgis.gui.QgsFilterLineEdit(nullValue='my_null_value')
125+
w.clearValue()
126+
self.assertEqual(w.selectedText(), 'my_null_value')
127+
128+
w.setValue('my new value')
129+
self.assertEqual(w.selectedText(), '')
130+
131+
w.clearValue()
132+
self.assertEqual(w.selectedText(), 'my_null_value')
133+
122134
@unittest.skipIf(not use_signal_spy, "No QSignalSpy available")
123135
def test_ChangedSignals(self):
124136
""" test that signals are correctly emitted when clearing"""

0 commit comments

Comments
 (0)
Please sign in to comment.