Skip to content
Permalink
Browse files

backport of the range widget implementation

cherry-picked from:
8350fb4
6da99a1
a02fb08
f44af04
f96078d
c8fc7a2

unused variable removed
pass SpecialValueText as NullValue
Remove the null representator text on getting focus of `QgsSpinBoxLineEdit`
clear value on clear in case the value is the null representator (null)
tests for focus on null field
  • Loading branch information
signedav committed Nov 8, 2018
1 parent 7eff348 commit ccadd262924bccf44494e57978a5137023c73dc4
@@ -107,6 +107,8 @@ void QgsDoubleSpinBox::changed( double value )
void QgsDoubleSpinBox::clear()
{
setValue( clearValue() );
if ( mLineEdit->isNull() )
mLineEdit->clear();
}

void QgsDoubleSpinBox::setClearValue( double customValue, const QString &specialValueText )
@@ -155,9 +157,15 @@ void QgsDoubleSpinBox::setLineEditAlignment( Qt::Alignment alignment )
void QgsDoubleSpinBox::setSpecialValueText( const QString &txt )
{
if ( txt.isEmpty() )
{
QDoubleSpinBox::setSpecialValueText( SPECIAL_TEXT_WHEN_EMPTY );
mLineEdit->setNullValue( SPECIAL_TEXT_WHEN_EMPTY );
}
else
{
QDoubleSpinBox::setSpecialValueText( txt );
mLineEdit->setNullValue( SPECIAL_TEXT_WHEN_EMPTY );
}
}

QString QgsDoubleSpinBox::stripped( const QString &originalText ) const
@@ -104,6 +104,8 @@ void QgsSpinBox::changed( int value )
void QgsSpinBox::clear()
{
setValue( clearValue() );
if ( mLineEdit->isNull() )
mLineEdit->clear();
}

void QgsSpinBox::setClearValue( int customValue, const QString &specialValueText )
@@ -152,9 +154,15 @@ void QgsSpinBox::setLineEditAlignment( Qt::Alignment alignment )
void QgsSpinBox::setSpecialValueText( const QString &txt )
{
if ( txt.isEmpty() )
{
QSpinBox::setSpecialValueText( SPECIAL_TEXT_WHEN_EMPTY );
mLineEdit->setNullValue( SPECIAL_TEXT_WHEN_EMPTY );
}
else
{
QSpinBox::setSpecialValueText( txt );
mLineEdit->setNullValue( txt );
}
}

int QgsSpinBox::valueFromText( const QString &text ) const
@@ -89,7 +89,6 @@ void QgsFilterLineEdit::focusInEvent( QFocusEvent *e )
QLineEdit::focusInEvent( e );
if ( e->reason() == Qt::MouseFocusReason && ( isNull() || mSelectOnFocus ) )
{
mFocusInEvent = true;
mWaitingForMouseRelease = true;
}
}
@@ -213,3 +212,14 @@ bool QgsFilterLineEdit::event( QEvent *event )

return QLineEdit::event( event );;
}

/// @cond PRIVATE
void QgsSpinBoxLineEdit::focusInEvent( QFocusEvent *e )
{
QLineEdit::focusInEvent( e );
if ( isNull() )
{
clear();
}
}
/// @endcond
@@ -286,7 +286,6 @@ class GUI_EXPORT QgsFilterLineEdit : public QLineEdit
QString mNullValue;
QString mDefaultValue;
QString mStyleSheet;
bool mFocusInEvent = false;
bool mWaitingForMouseRelease = false;
bool mSelectOnFocus = false;

@@ -325,6 +324,9 @@ class SIP_SKIP QgsSpinBoxLineEdit : public QgsFilterLineEdit
setModified( true );
emit cleared();
}

protected:
void focusInEvent( QFocusEvent *e ) override;
};
/// @endcond

@@ -51,6 +51,7 @@ class TestQgsRangeWidgetWrapper : public QObject
void test_setDoubleSmallerRange();
void test_setDoubleLimits();
void test_nulls();
void test_focus();

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

}

void TestQgsRangeWidgetWrapper::test_focus()
{
QgsApplication::setNullRepresentation( QString( "nope" ) );

QWidget *w = new QWidget(); //required for focus events
QApplication::setActiveWindow( w );

QVariantMap cfg;
cfg.insert( QStringLiteral( "AllowNull" ), true );

widget1->setConfig( cfg );
QgsDoubleSpinBox *editor1 = qobject_cast<QgsDoubleSpinBox *>( widget1->createWidget( w ) );
QVERIFY( editor1 );
widget1->initWidget( editor1 );

widget2->setConfig( cfg );
QgsDoubleSpinBox *editor2 = qobject_cast<QgsDoubleSpinBox *>( widget2->createWidget( w ) );
QVERIFY( editor2 );
widget2->initWidget( editor2 );

editor1->mLineEdit->setNullValue( QgsApplication::nullRepresentation() );
editor2->mLineEdit->setNullValue( QgsApplication::nullRepresentation() );

QVERIFY( editor1->mLineEdit->isNull() );
QVERIFY( editor2->mLineEdit->isNull() );
QVERIFY( !editor1->mLineEdit->hasFocus() );
QVERIFY( !editor2->mLineEdit->hasFocus() );
QCOMPARE( editor1->mLineEdit->text(), QStringLiteral( "nope" ) );
QCOMPARE( editor2->mLineEdit->text(), QStringLiteral( "nope" ) );

editor1->mLineEdit->setFocus();
QVERIFY( editor1->mLineEdit->hasFocus() );
QVERIFY( !editor2->mLineEdit->hasFocus() );
QCOMPARE( editor1->mLineEdit->text(), QStringLiteral( "" ) );
QCOMPARE( editor2->mLineEdit->text(), QStringLiteral( "nope" ) );

editor2->mLineEdit->setFocus();
QVERIFY( !editor1->mLineEdit->hasFocus() );
QVERIFY( editor2->mLineEdit->hasFocus() );
QCOMPARE( editor1->mLineEdit->text(), QStringLiteral( "nope" ) );
QCOMPARE( editor2->mLineEdit->text(), QStringLiteral( "" ) );

editor1->mLineEdit->setFocus();
editor1->mLineEdit->setText( QString( "151.000000000" ) );
QVERIFY( !editor1->mLineEdit->isNull() );
QVERIFY( editor2->mLineEdit->isNull() );
QVERIFY( editor1->mLineEdit->hasFocus() );
QVERIFY( !editor2->mLineEdit->hasFocus() );
QCOMPARE( editor1->mLineEdit->text(), QStringLiteral( "151.000000000" ) );
QCOMPARE( editor2->mLineEdit->text(), QStringLiteral( "nope" ) );

editor2->mLineEdit->setFocus();
QVERIFY( !editor1->mLineEdit->hasFocus() );
QVERIFY( editor2->mLineEdit->hasFocus() );
QCOMPARE( editor1->mLineEdit->text(), QStringLiteral( "151.000000000" ) );
QCOMPARE( editor2->mLineEdit->text(), QStringLiteral( "" ) );

}

QGSTEST_MAIN( TestQgsRangeWidgetWrapper )
#include "testqgsrangewidgetwrapper.moc"

0 comments on commit ccadd26

Please sign in to comment.
You can’t perform that action at this time.