20
20
#include " qgsrangewidgetwrapper.h"
21
21
#include " qgsrangeconfigdlg.h"
22
22
#include " qgsdoublespinbox.h"
23
+ #include " qgsspinbox.h"
23
24
#include " qgsapplication.h"
24
25
#include " qgslogger.h"
25
26
#include " qgsvectorlayer.h"
26
27
#include " qgsdataprovider.h"
28
+ #include " qgsfilterlineedit.h"
27
29
28
-
30
+ # include < QLineEdit >
29
31
#include < QObject>
30
32
#include < QtTest/QSignalSpy>
31
33
@@ -48,14 +50,22 @@ class TestQgsRangeWidgetWrapper : public QObject
48
50
void test_setDoubleRange ();
49
51
void test_setDoubleSmallerRange ();
50
52
void test_setDoubleLimits ();
53
+ void test_nulls ();
54
+
51
55
private:
52
- std::unique_ptr<QgsRangeWidgetWrapper> widget; // For field 1
56
+ std::unique_ptr<QgsRangeWidgetWrapper> widget0; // For field 0
57
+ std::unique_ptr<QgsRangeWidgetWrapper> widget1; // For field 1
53
58
std::unique_ptr<QgsRangeWidgetWrapper> widget2; // For field 2
54
59
std::unique_ptr<QgsVectorLayer> vl;
55
60
};
56
61
57
62
void TestQgsRangeWidgetWrapper::initTestCase ()
58
63
{
64
+ // Set up the QgsSettings environment
65
+ QCoreApplication::setOrganizationName ( QStringLiteral ( " QGIS" ) );
66
+ QCoreApplication::setOrganizationDomain ( QStringLiteral ( " qgis.org" ) );
67
+ QCoreApplication::setApplicationName ( QStringLiteral ( " QGIS-TEST-RANGE-WIDGET" ) );
68
+
59
69
QgsApplication::init ();
60
70
QgsApplication::initQgis ();
61
71
}
@@ -108,9 +118,10 @@ void TestQgsRangeWidgetWrapper::init()
108
118
QCOMPARE ( vl->featureCount ( ), ( long )3 );
109
119
QgsFeature _feat1 ( vl->getFeature ( 1 ) );
110
120
QCOMPARE ( _feat1, feat1 );
111
- widget = qgis::make_unique<QgsRangeWidgetWrapper>( vl.get (), 1 , nullptr , nullptr );
121
+ widget0 = qgis::make_unique<QgsRangeWidgetWrapper>( vl.get (), 0 , nullptr , nullptr );
122
+ widget1 = qgis::make_unique<QgsRangeWidgetWrapper>( vl.get (), 1 , nullptr , nullptr );
112
123
widget2 = qgis::make_unique<QgsRangeWidgetWrapper>( vl.get (), 2 , nullptr , nullptr );
113
- QVERIFY ( widget .get () );
124
+ QVERIFY ( widget1 .get () );
114
125
}
115
126
116
127
void TestQgsRangeWidgetWrapper::cleanup ()
@@ -123,17 +134,17 @@ void TestQgsRangeWidgetWrapper::test_setDoubleRange()
123
134
// See https://issues.qgis.org/issues/17878
124
135
// QGIS 3 Vector Layer Fields Garbled when Clicking the Toggle Editing Icon
125
136
126
- QgsDoubleSpinBox *editor = qobject_cast<QgsDoubleSpinBox *>( widget ->createWidget ( nullptr ) );
137
+ QgsDoubleSpinBox *editor = qobject_cast<QgsDoubleSpinBox *>( widget1 ->createWidget ( nullptr ) );
127
138
QVERIFY ( editor );
128
- widget ->initWidget ( editor );
139
+ widget1 ->initWidget ( editor );
129
140
QgsDoubleSpinBox *editor2 = qobject_cast<QgsDoubleSpinBox *>( widget2->createWidget ( nullptr ) );
130
141
QVERIFY ( editor2 );
131
142
widget2->initWidget ( editor2 );
132
143
133
144
QgsFeature feat ( vl->getFeature ( 1 ) );
134
145
QVERIFY ( feat.isValid () );
135
146
QCOMPARE ( feat.attribute ( 1 ).toDouble (), 123.123456789 );
136
- widget ->setFeature ( vl->getFeature ( 1 ) );
147
+ widget1 ->setFeature ( vl->getFeature ( 1 ) );
137
148
widget2->setFeature ( vl->getFeature ( 1 ) );
138
149
QCOMPARE ( vl->fields ().at ( 1 ).precision (), 9 );
139
150
// Default is 0 !!! for double, really ?
@@ -151,12 +162,12 @@ void TestQgsRangeWidgetWrapper::test_setDoubleRange()
151
162
QCOMPARE ( editor->maximum ( ), std::numeric_limits<double >::max () );
152
163
QCOMPARE ( editor2->maximum ( ), std::numeric_limits<double >::max () );
153
164
154
- widget ->setFeature ( vl->getFeature ( 2 ) );
165
+ widget1 ->setFeature ( vl->getFeature ( 2 ) );
155
166
widget2->setFeature ( vl->getFeature ( 2 ) );
156
167
QCOMPARE ( editor->value ( ), editor->minimum () );
157
168
QCOMPARE ( editor2->value ( ), editor->minimum () );
158
169
159
- widget ->setFeature ( vl->getFeature ( 3 ) );
170
+ widget1 ->setFeature ( vl->getFeature ( 3 ) );
160
171
widget2->setFeature ( vl->getFeature ( 3 ) );
161
172
QCOMPARE ( editor->value ( ), -123.123456789 );
162
173
QCOMPARE ( editor2->value ( ), -123.0 );
@@ -169,10 +180,10 @@ void TestQgsRangeWidgetWrapper::test_setDoubleSmallerRange()
169
180
cfg.insert ( QStringLiteral ( " Min" ), -100.0 );
170
181
cfg.insert ( QStringLiteral ( " Max" ), 100.0 );
171
182
cfg.insert ( QStringLiteral ( " Step" ), 1 );
172
- widget ->setConfig ( cfg );
173
- QgsDoubleSpinBox *editor = qobject_cast<QgsDoubleSpinBox *>( widget ->createWidget ( nullptr ) );
183
+ widget1 ->setConfig ( cfg );
184
+ QgsDoubleSpinBox *editor = qobject_cast<QgsDoubleSpinBox *>( widget1 ->createWidget ( nullptr ) );
174
185
QVERIFY ( editor );
175
- widget ->initWidget ( editor );
186
+ widget1 ->initWidget ( editor );
176
187
177
188
widget2->setConfig ( cfg );
178
189
QgsDoubleSpinBox *editor2 = qobject_cast<QgsDoubleSpinBox *>( widget2->createWidget ( nullptr ) );
@@ -182,7 +193,7 @@ void TestQgsRangeWidgetWrapper::test_setDoubleSmallerRange()
182
193
QgsFeature feat ( vl->getFeature ( 1 ) );
183
194
QVERIFY ( feat.isValid () );
184
195
QCOMPARE ( feat.attribute ( 1 ).toDouble (), 123.123456789 );
185
- widget ->setFeature ( vl->getFeature ( 1 ) );
196
+ widget1 ->setFeature ( vl->getFeature ( 1 ) );
186
197
widget2->setFeature ( vl->getFeature ( 1 ) );
187
198
188
199
QCOMPARE ( vl->fields ().at ( 1 ).precision (), 9 );
@@ -202,13 +213,13 @@ void TestQgsRangeWidgetWrapper::test_setDoubleSmallerRange()
202
213
QCOMPARE ( editor2->maximum ( ), ( double )100 );
203
214
204
215
// NULL, NULL
205
- widget ->setFeature ( vl->getFeature ( 2 ) );
216
+ widget1 ->setFeature ( vl->getFeature ( 2 ) );
206
217
widget2->setFeature ( vl->getFeature ( 2 ) );
207
218
QCOMPARE ( editor->value ( ), editor->minimum () );
208
219
QCOMPARE ( editor2->value ( ), editor2->minimum () );
209
220
210
221
// negative, negative
211
- widget ->setFeature ( vl->getFeature ( 3 ) );
222
+ widget1 ->setFeature ( vl->getFeature ( 3 ) );
212
223
widget2->setFeature ( vl->getFeature ( 3 ) );
213
224
// value was changed to the minimum
214
225
QCOMPARE ( editor->value ( ), editor->minimum () );
@@ -223,10 +234,10 @@ void TestQgsRangeWidgetWrapper::test_setDoubleLimits()
223
234
cfg.insert ( QStringLiteral ( " Min" ), std::numeric_limits<double >::lowest () );
224
235
cfg.insert ( QStringLiteral ( " Max" ), std::numeric_limits<double >::max () );
225
236
cfg.insert ( QStringLiteral ( " Step" ), 1 );
226
- widget ->setConfig ( cfg );
227
- QgsDoubleSpinBox *editor = qobject_cast<QgsDoubleSpinBox *>( widget ->createWidget ( nullptr ) );
237
+ widget1 ->setConfig ( cfg );
238
+ QgsDoubleSpinBox *editor = qobject_cast<QgsDoubleSpinBox *>( widget1 ->createWidget ( nullptr ) );
228
239
QVERIFY ( editor );
229
- widget ->initWidget ( editor );
240
+ widget1 ->initWidget ( editor );
230
241
231
242
widget2->setConfig ( cfg );
232
243
QgsDoubleSpinBox *editor2 = qobject_cast<QgsDoubleSpinBox *>( widget2->createWidget ( nullptr ) );
@@ -241,7 +252,7 @@ void TestQgsRangeWidgetWrapper::test_setDoubleLimits()
241
252
QgsFeature feat ( vl->getFeature ( 1 ) );
242
253
QVERIFY ( feat.isValid () );
243
254
QCOMPARE ( feat.attribute ( 1 ).toDouble (), 123.123456789 );
244
- widget ->setFeature ( vl->getFeature ( 1 ) );
255
+ widget1 ->setFeature ( vl->getFeature ( 1 ) );
245
256
widget2->setFeature ( vl->getFeature ( 1 ) );
246
257
247
258
QCOMPARE ( vl->fields ().at ( 1 ).precision (), 9 );
@@ -253,20 +264,70 @@ void TestQgsRangeWidgetWrapper::test_setDoubleLimits()
253
264
QCOMPARE ( editor2->value ( ), 123.0 );
254
265
255
266
// NULL, NULL
256
- widget ->setFeature ( vl->getFeature ( 2 ) );
267
+ widget1 ->setFeature ( vl->getFeature ( 2 ) );
257
268
widget2->setFeature ( vl->getFeature ( 2 ) );
258
269
QCOMPARE ( editor->value ( ), editor->minimum () );
259
270
QCOMPARE ( editor2->value ( ), editor2->minimum () );
260
271
261
272
// negative, negative
262
- widget ->setFeature ( vl->getFeature ( 3 ) );
273
+ widget1 ->setFeature ( vl->getFeature ( 3 ) );
263
274
widget2->setFeature ( vl->getFeature ( 3 ) );
264
275
// value was changed to the minimum
265
276
QCOMPARE ( editor->value ( ), -123.123456789 );
266
277
QCOMPARE ( editor2->value ( ), -123.0 );
267
278
268
279
}
269
280
281
+ void TestQgsRangeWidgetWrapper::test_nulls ()
282
+ {
283
+
284
+ QgsApplication::setNullRepresentation ( QString ( " " ) );
285
+
286
+ QVariantMap cfg;
287
+ cfg.insert ( QStringLiteral ( " Min" ), 100.00 );
288
+ cfg.insert ( QStringLiteral ( " Max" ), 200.00 );
289
+ cfg.insert ( QStringLiteral ( " Step" ), 1 );
290
+ cfg.insert ( QStringLiteral ( " Precision" ), 0 );
291
+ widget1->setConfig ( cfg );
292
+ QgsDoubleSpinBox *editor1 = qobject_cast<QgsDoubleSpinBox *>( widget1->createWidget ( nullptr ) );
293
+ QVERIFY ( editor1 );
294
+ widget1->initWidget ( editor1 );
295
+ // Out of range
296
+ widget1->setFeature ( vl->getFeature ( 3 ) );
297
+ QCOMPARE ( editor1->value ( ), editor1->minimum () );
298
+ QCOMPARE ( widget1->value ( ), QVariant ( QVariant::Double ) );
299
+ widget1->setFeature ( QgsFeature ( vl->fields () ) );
300
+ // Null
301
+ QCOMPARE ( editor1->value ( ), editor1->minimum () );
302
+ QCOMPARE ( widget1->value ( ), QVariant ( QVariant::Double ) );
303
+ QCOMPARE ( editor1->mLineEdit ->text (), QgsDoubleSpinBox::SPECIAL_TEXT_WHEN_EMPTY );
304
+ editor1->mLineEdit ->setText ( QString ( " 151%1" ).arg ( QgsDoubleSpinBox::SPECIAL_TEXT_WHEN_EMPTY ) );
305
+ QCOMPARE ( widget1->value ( ).toInt (), 151 );
306
+ editor1->mLineEdit ->setText ( QString ( QgsDoubleSpinBox::SPECIAL_TEXT_WHEN_EMPTY ).append ( QStringLiteral ( " 161" ) ) );
307
+ QCOMPARE ( widget1->value ( ).toInt (), 161 );
308
+
309
+
310
+ QgsSpinBox *editor0 = qobject_cast<QgsSpinBox *>( widget0->createWidget ( nullptr ) );
311
+ QVERIFY ( editor0 );
312
+ widget0->setConfig ( cfg );
313
+ widget0->initWidget ( editor0 );
314
+ // Out of range
315
+ widget0->setFeature ( vl->getFeature ( 3 ) );
316
+ QCOMPARE ( editor0->value ( ), editor0->minimum () );
317
+ QCOMPARE ( widget0->value ( ), QVariant ( QVariant::Int ) );
318
+ widget0->setFeature ( QgsFeature ( vl->fields () ) );
319
+ // Null
320
+ QCOMPARE ( editor0->value ( ), editor0->minimum () );
321
+ QCOMPARE ( widget0->value ( ), QVariant ( QVariant::Int ) );
322
+ QCOMPARE ( editor0->mLineEdit ->text (), QgsDoubleSpinBox::SPECIAL_TEXT_WHEN_EMPTY );
323
+
324
+ editor0->mLineEdit ->setText ( QString ( " 150%1" ).arg ( QgsDoubleSpinBox::SPECIAL_TEXT_WHEN_EMPTY ) );
325
+ QCOMPARE ( widget0->value ( ).toInt (), 150 );
326
+ editor0->mLineEdit ->setText ( QString ( QgsDoubleSpinBox::SPECIAL_TEXT_WHEN_EMPTY ).append ( QStringLiteral ( " 160" ) ) );
327
+ QCOMPARE ( widget0->value ( ).toInt (), 160 );
328
+
329
+ }
330
+
270
331
271
332
272
333
QGSTEST_MAIN ( TestQgsRangeWidgetWrapper )
0 commit comments