Skip to content

Commit 438a1da

Browse files
authoredNov 16, 2018
Merge pull request #8442 from signedav/textfield_null
Range widget: Remove null representator during editing
2 parents 30692fa + c8fc7a2 commit 438a1da

File tree

5 files changed

+88
-2
lines changed

5 files changed

+88
-2
lines changed
 

‎src/gui/editorwidgets/qgsdoublespinbox.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ void QgsDoubleSpinBox::changed( double value )
107107
void QgsDoubleSpinBox::clear()
108108
{
109109
setValue( clearValue() );
110+
if ( mLineEdit->isNull() )
111+
mLineEdit->clear();
110112
}
111113

112114
void QgsDoubleSpinBox::setClearValue( double customValue, const QString &specialValueText )
@@ -155,9 +157,15 @@ void QgsDoubleSpinBox::setLineEditAlignment( Qt::Alignment alignment )
155157
void QgsDoubleSpinBox::setSpecialValueText( const QString &txt )
156158
{
157159
if ( txt.isEmpty() )
160+
{
158161
QDoubleSpinBox::setSpecialValueText( SPECIAL_TEXT_WHEN_EMPTY );
162+
mLineEdit->setNullValue( SPECIAL_TEXT_WHEN_EMPTY );
163+
}
159164
else
165+
{
160166
QDoubleSpinBox::setSpecialValueText( txt );
167+
mLineEdit->setNullValue( SPECIAL_TEXT_WHEN_EMPTY );
168+
}
161169
}
162170

163171
QString QgsDoubleSpinBox::stripped( const QString &originalText ) const

‎src/gui/editorwidgets/qgsspinbox.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ void QgsSpinBox::changed( int value )
104104
void QgsSpinBox::clear()
105105
{
106106
setValue( clearValue() );
107+
if ( mLineEdit->isNull() )
108+
mLineEdit->clear();
107109
}
108110

109111
void QgsSpinBox::setClearValue( int customValue, const QString &specialValueText )
@@ -152,9 +154,15 @@ void QgsSpinBox::setLineEditAlignment( Qt::Alignment alignment )
152154
void QgsSpinBox::setSpecialValueText( const QString &txt )
153155
{
154156
if ( txt.isEmpty() )
157+
{
155158
QSpinBox::setSpecialValueText( SPECIAL_TEXT_WHEN_EMPTY );
159+
mLineEdit->setNullValue( SPECIAL_TEXT_WHEN_EMPTY );
160+
}
156161
else
162+
{
157163
QSpinBox::setSpecialValueText( txt );
164+
mLineEdit->setNullValue( txt );
165+
}
158166
}
159167

160168
int QgsSpinBox::valueFromText( const QString &text ) const

‎src/gui/qgsfilterlineedit.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,6 @@ void QgsFilterLineEdit::focusInEvent( QFocusEvent *e )
8989
QLineEdit::focusInEvent( e );
9090
if ( e->reason() == Qt::MouseFocusReason && ( isNull() || mSelectOnFocus ) )
9191
{
92-
mFocusInEvent = true;
9392
mWaitingForMouseRelease = true;
9493
}
9594
}
@@ -213,3 +212,14 @@ bool QgsFilterLineEdit::event( QEvent *event )
213212

214213
return QLineEdit::event( event );;
215214
}
215+
216+
/// @cond PRIVATE
217+
void QgsSpinBoxLineEdit::focusInEvent( QFocusEvent *e )
218+
{
219+
QLineEdit::focusInEvent( e );
220+
if ( isNull() )
221+
{
222+
clear();
223+
}
224+
}
225+
/// @endcond

‎src/gui/qgsfilterlineedit.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,6 @@ class GUI_EXPORT QgsFilterLineEdit : public QLineEdit
286286
QString mNullValue;
287287
QString mDefaultValue;
288288
QString mStyleSheet;
289-
bool mFocusInEvent = false;
290289
bool mWaitingForMouseRelease = false;
291290
bool mSelectOnFocus = false;
292291

@@ -325,6 +324,9 @@ class SIP_SKIP QgsSpinBoxLineEdit : public QgsFilterLineEdit
325324
setModified( true );
326325
emit cleared();
327326
}
327+
328+
protected:
329+
void focusInEvent( QFocusEvent *e ) override;
328330
};
329331
/// @endcond
330332

‎tests/src/gui/testqgsrangewidgetwrapper.cpp

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ class TestQgsRangeWidgetWrapper : public QObject
5151
void test_setDoubleSmallerRange();
5252
void test_setDoubleLimits();
5353
void test_nulls();
54+
void test_focus();
5455

5556
private:
5657
std::unique_ptr<QgsRangeWidgetWrapper> widget0; // For field 0
@@ -328,7 +329,64 @@ void TestQgsRangeWidgetWrapper::test_nulls()
328329

329330
}
330331

332+
void TestQgsRangeWidgetWrapper::test_focus()
333+
{
334+
QgsApplication::setNullRepresentation( QString( "nope" ) );
335+
336+
QWidget *w = new QWidget(); //required for focus events
337+
QApplication::setActiveWindow( w );
338+
339+
QVariantMap cfg;
340+
cfg.insert( QStringLiteral( "AllowNull" ), true );
341+
342+
widget1->setConfig( cfg );
343+
QgsDoubleSpinBox *editor1 = qobject_cast<QgsDoubleSpinBox *>( widget1->createWidget( w ) );
344+
QVERIFY( editor1 );
345+
widget1->initWidget( editor1 );
331346

347+
widget2->setConfig( cfg );
348+
QgsDoubleSpinBox *editor2 = qobject_cast<QgsDoubleSpinBox *>( widget2->createWidget( w ) );
349+
QVERIFY( editor2 );
350+
widget2->initWidget( editor2 );
351+
352+
editor1->mLineEdit->setNullValue( QgsApplication::nullRepresentation() );
353+
editor2->mLineEdit->setNullValue( QgsApplication::nullRepresentation() );
354+
355+
QVERIFY( editor1->mLineEdit->isNull() );
356+
QVERIFY( editor2->mLineEdit->isNull() );
357+
QVERIFY( !editor1->mLineEdit->hasFocus() );
358+
QVERIFY( !editor2->mLineEdit->hasFocus() );
359+
QCOMPARE( editor1->mLineEdit->text(), QStringLiteral( "nope" ) );
360+
QCOMPARE( editor2->mLineEdit->text(), QStringLiteral( "nope" ) );
361+
362+
editor1->mLineEdit->setFocus();
363+
QVERIFY( editor1->mLineEdit->hasFocus() );
364+
QVERIFY( !editor2->mLineEdit->hasFocus() );
365+
QCOMPARE( editor1->mLineEdit->text(), QStringLiteral( "" ) );
366+
QCOMPARE( editor2->mLineEdit->text(), QStringLiteral( "nope" ) );
367+
368+
editor2->mLineEdit->setFocus();
369+
QVERIFY( !editor1->mLineEdit->hasFocus() );
370+
QVERIFY( editor2->mLineEdit->hasFocus() );
371+
QCOMPARE( editor1->mLineEdit->text(), QStringLiteral( "nope" ) );
372+
QCOMPARE( editor2->mLineEdit->text(), QStringLiteral( "" ) );
373+
374+
editor1->mLineEdit->setFocus();
375+
editor1->mLineEdit->setText( QString( "151.000000000" ) );
376+
QVERIFY( !editor1->mLineEdit->isNull() );
377+
QVERIFY( editor2->mLineEdit->isNull() );
378+
QVERIFY( editor1->mLineEdit->hasFocus() );
379+
QVERIFY( !editor2->mLineEdit->hasFocus() );
380+
QCOMPARE( editor1->mLineEdit->text(), QStringLiteral( "151.000000000" ) );
381+
QCOMPARE( editor2->mLineEdit->text(), QStringLiteral( "nope" ) );
382+
383+
editor2->mLineEdit->setFocus();
384+
QVERIFY( !editor1->mLineEdit->hasFocus() );
385+
QVERIFY( editor2->mLineEdit->hasFocus() );
386+
QCOMPARE( editor1->mLineEdit->text(), QStringLiteral( "151.000000000" ) );
387+
QCOMPARE( editor2->mLineEdit->text(), QStringLiteral( "" ) );
388+
389+
}
332390

333391
QGSTEST_MAIN( TestQgsRangeWidgetWrapper )
334392
#include "testqgsrangewidgetwrapper.moc"

0 commit comments

Comments
 (0)
Please sign in to comment.