-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscientific_spinbox.py
1544 lines (1312 loc) · 60.1 KB
/
scientific_spinbox.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
"""
This file contains a wrapper to display the SpinBox in scientific way
Qudi is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Qudi is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Qudi. If not, see <http://www.gnu.org/licenses/>.
"""
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
import numpy as np
import re
from decimal import Decimal as D # Use decimal to avoid accumulating floating-point errors
from decimal import ROUND_FLOOR
import math
__all__ = ['ScienDSpinBox', 'ScienSpinBox']
class ErrorBox(QtWidgets.QWidget):
"""Red outline to draw around lineedit when value is invalid.
(for some reason, setting border from stylesheet does not work)
"""
def __init__(self, parent=None):
QtWidgets.QWidget.__init__(self, parent)
parent.installEventFilter(self)
self.setAttribute(QtCore.Qt.WA_TransparentForMouseEvents)
self._resize()
self.setVisible(False)
def eventFilter(self, obj, ev):
if ev.type() == QtCore.QEvent.Resize:
self._resize()
return False
def _resize(self):
self.setGeometry(0, 0, self.parent().width(), self.parent().height())
def paintEvent(self, ev):
p = QtGui.QPainter(self)
pen = QtGui.QPen(QtGui.QColor(255, 0, 0), 2)
p.setPen(pen)
p.drawRect(self.rect())
p.end()
class FloatValidator(QtGui.QValidator):
"""
This is a validator for float values represented as strings in scientific notation.
(i.e. "1.35e-9", ".24E+8", "14e3" etc.)
Also supports SI unit prefix like 'M', 'n' etc.
"""
float_re = re.compile(r'(\s*([+-]?)(\d+\.\d+|\.\d+|\d+\.?)([eE][+-]?\d+)?\s?([YZEPTGMkmµunpfazy]?)\s*)',
flags=re.UNICODE)
group_map = {'match': 0,
'sign': 1,
'mantissa': 2,
'exponent': 3,
'si': 4}
def validate(self, string, position):
"""
This is the actual validator. It checks whether the current user input is a valid string
every time the user types a character. There are 3 states that are possible.
1) Invalid: The current input string is invalid. The user input will not accept the last
typed character.
2) Acceptable: The user input in conform with the regular expression and will be accepted.
3) Intermediate: The user input is not a valid string yet but on the right track. Use this
return value to allow the user to type fill-characters needed in order to
complete an expression (i.e. the decimal point of a float value).
:param string: The current input string (from a QLineEdit for example)
:param position: The current position of the text cursor
:return: enum QValidator::State: the returned validator state,
str: the input string, int: the cursor position
"""
# Return intermediate status when empty string is passed or when incomplete "[+-]inf"
if string.strip() in '+.-.' or string.strip() in list('YZEPTGMkmµunpfazy') or re.match(
r'[+-]?(in$|i$)', string, re.IGNORECASE):
return self.Intermediate, string, position
# Accept input of [+-]inf. Not case sensitive.
if re.match(r'[+-]?\binf$', string, re.IGNORECASE):
return self.Acceptable, string.lower(), position
group_dict = self.get_group_dict(string)
if group_dict:
if group_dict['match'] == string:
return self.Acceptable, string, position
if string.count('.') > 1:
return self.Invalid, group_dict['match'], position
if position > len(string):
position = len(string)
if string[position-1] in 'eE-+' and 'i' not in string.lower():
return self.Intermediate, string, position
return self.Invalid, group_dict['match'], position
else:
if string[position-1] in 'eE-+.' and 'i' not in string.lower():
return self.Intermediate, string, position
return self.Invalid, '', position
def get_group_dict(self, string):
"""
This method will match the input string with the regular expression of this validator.
The match groups will be put into a dictionary with string descriptors as keys describing
the role of the specific group (i.e. mantissa, exponent, si-prefix etc.)
:param string: str, input string to be matched
:return: dictionary containing groups as items and descriptors as keys (see: self.group_map)
"""
match = self.float_re.search(string)
if not match:
return False
groups = match.groups()
group_dict = dict()
for group_key in self.group_map:
group_dict[group_key] = groups[self.group_map[group_key]]
return group_dict
def fixup(self, text):
match = self.float_re.search(text)
if match:
return match.groups()[0].strip()
else:
return ''
class IntegerValidator(QtGui.QValidator):
"""
This is a validator for int values represented as strings in scientific notation.
Using engineering notation only positive exponents are allowed
(i.e. "1e9", "2E+8", "14e+3" etc.)
Also supports non-fractional SI unit prefix like 'M', 'k' etc.
"""
int_re = re.compile(r'(([+-]?\d+)([eE]\+?\d+)?\s?([YZEPTGMk])?\s*)', flags=re.UNICODE)
group_map = {'match': 0,
'mantissa': 1,
'exponent': 2,
'si': 3
}
def validate(self, string, position):
"""
This is the actual validator. It checks whether the current user input is a valid string
every time the user types a character. There are 3 states that are possible.
1) Invalid: The current input string is invalid. The user input will not accept the last
typed character.
2) Acceptable: The user input in conform with the regular expression and will be accepted.
3) Intermediate: The user input is not a valid string yet but on the right track. Use this
return value to allow the user to type fill-characters needed in order to
complete an expression (i.e. the decimal point of a float value).
:param string: The current input string (from a QLineEdit for example)
:param position: The current position of the text cursor
:return: enum QValidator::State: the returned validator state,
str: the input string, int: the cursor position
"""
# Return intermediate status when empty string is passed or cursor is at index 0
if not string.strip() or string.strip() in list('YZEPTGMk'):
return self.Intermediate, string, position
group_dict = self.get_group_dict(string)
if group_dict:
if group_dict['match'] == string:
return self.Acceptable, string, position
if position > len(string):
position = len(string)
if string[position-1] in 'eE-+':
return self.Intermediate, string, position
return self.Invalid, group_dict['match'], position
else:
return self.Invalid, '', position
def get_group_dict(self, string):
"""
This method will match the input string with the regular expression of this validator.
The match groups will be put into a dictionary with string descriptors as keys describing
the role of the specific group (i.e. mantissa, exponent, si-prefix etc.)
:param string: str, input string to be matched
:return: dictionary containing groups as items and descriptors as keys (see: self.group_map)
"""
match = self.int_re.search(string)
if not match:
return False
groups = match.groups()
group_dict = dict()
for group_key in self.group_map:
group_dict[group_key] = groups[self.group_map[group_key]]
return group_dict
def fixup(self, text):
match = self.int_re.search(text)
if match:
return match.groups()[0].strip()
else:
return ''
class ScienDSpinBox(QtWidgets.QAbstractSpinBox):
"""
Wrapper Class from PyQt5 (or QtPy) to display a QDoubleSpinBox in Scientific way.
Fully supports prefix and suffix functionality of the QDoubleSpinBox.
Has built-in functionality to invoke the displayed number precision from the user input.
This class can be directly used in Qt Designer by promoting the QDoubleSpinBox to ScienDSpinBox.
State the path to this file (in python style, i.e. dots are separating the directories) as the
header file and use the name of the present class.
"""
valueChanged = QtCore.pyqtSignal(object)
returnPressed = QtCore.pyqtSignal()
# The maximum number of decimals to allow. Be careful when changing this number since
# the decimal package has by default a limited accuracy.
__max_decimals = 20
# Dictionary mapping the si-prefix to a scaling factor as decimal.Decimal (exact value)
_unit_prefix_dict = {
'y': D('1e-24'),
'z': D('1e-21'),
'a': D('1e-18'),
'f': D('1e-15'),
'p': D('1e-12'),
'n': D('1e-9'),
'µ': D('1e-6'),
'm': D('1e-3'),
'': D('1'),
'k': D('1e3'),
'M': D('1e6'),
'G': D('1e9'),
'T': D('1e12'),
'P': D('1e15'),
'E': D('1e18'),
'Z': D('1e21'),
'Y': D('1e24')
}
def __init__(self, *args, **kwargs):
super(ScienDSpinBox, self).__init__(*args, **kwargs)
self.__value = D(0)
self.__minimum = -np.inf
self.__maximum = np.inf
self.__decimals = 1 # default in QtDesigner
self.__prefix = ''
self.__suffix = ''
self.__singleStep = D('0.1') # must be precise Decimal always, no conversion from float
self.__minimalStep = D(0) # must be precise Decimal always, no conversion from float
self.__cached_value = None # a temporary variable for restore functionality
self._dynamic_stepping = True
self._dynamic_precision = True
self._is_valid = True # A flag property to check if the current value is valid.
self.validator = FloatValidator()
self.errorBox = ErrorBox(self.lineEdit())
self.lineEdit().textEdited.connect(self.update_value)
self.lineEdit().returnPressed.connect(self.returnPressed.emit)
self.update_display()
def sizeHint(self):
"""
Bug fix for Qt on macOS: ensure that the QLineEdit in a QDoubleSpinbox
has the same height as a stand-alone QLineEdit.
"""
width = QtWidgets.QDoubleSpinBox().sizeHint().width()
if sys.platform == 'darwin':
height = QtWidgets.QLineEdit().sizeHint().height() + 2
else:
height = QtWidgets.QDoubleSpinBox().sizeHint().height()
return QtCore.QSize(width, height)
@property
def dynamic_stepping(self):
"""
This property is a flag indicating if the dynamic (logarithmic) stepping should be used or
not (fixed steps).
:return: bool, use dynamic stepping (True) or constant steps (False)
"""
return bool(self._dynamic_stepping)
@dynamic_stepping.setter
def dynamic_stepping(self, use_dynamic_stepping):
"""
This property is a flag indicating if the dynamic (logarithmic) stepping should be used or
not (fixed steps).
:param use_dynamic_stepping: bool, use dynamic stepping (True) or constant steps (False)
"""
use_dynamic_stepping = bool(use_dynamic_stepping)
self._dynamic_stepping = use_dynamic_stepping
@property
def dynamic_precision(self):
"""
This property is a flag indicating if the dynamic (invoked from user input) decimal
precision should be used or not (fixed number of digits).
:return: bool, use dynamic precision (True) or fixed precision (False)
"""
return bool(self._dynamic_precision)
@dynamic_precision.setter
def dynamic_precision(self, use_dynamic_precision):
"""
This property is a flag indicating if the dynamic (invoked from user input) decimal
precision should be used or not (fixed number of digits).
:param use_dynamic_precision: bool, use dynamic precision (True) or fixed precision (False)
"""
use_dynamic_precision = bool(use_dynamic_precision)
self._dynamic_precision = use_dynamic_precision
@property
def is_valid(self):
"""
This property is a flag indicating if the currently available value is valid.
It will return False if there has been an attempt to set NaN as current value.
Will return True after a valid value has been set.
:return: bool, current value invalid (False) or current value valid (True)
"""
return bool(self._is_valid)
def update_value(self):
"""
This method will grab the currently shown text from the QLineEdit and interpret it.
Range checking is performed on the value afterwards.
If a valid value can be derived, it will set this value as the current value
(if it has changed) and emit the valueChanged signal.
Note that the comparison between old and new value is done by comparing the float
representations of both values and not by comparing them as Decimals.
The valueChanged signal will only emit if the actual float representation has changed since
Decimals are only internally used and the rest of the program won't notice a slight change
in the Decimal that can't be resolved in a float.
In addition it will cache the old value provided the cache is empty to be able to restore
it later on.
"""
text = self.cleanText()
value = self.valueFromText(text)
if value is False:
return
value, in_range = self.check_range(value)
# save old value to be able to restore it later on
if self.__cached_value is None:
self.__cached_value = self.__value
if float(value) != self.value():
self.__value = value
self.valueChanged.emit(self.value())
else:
self.__value = value
self._is_valid = True
def value(self):
"""
Getter method to obtain the current value as float.
:return: float, the current value of the SpinBox
"""
return float(self.__value)
def setValue(self, value):
"""
Setter method to programmatically set the current value. For best robustness pass the value
as string or Decimal in order to be lossless cast into Decimal.
Will perform range checking and ignore NaN values.
Will emit valueChanged if the new value is different from the old one.
When using dynamic decimals precision, this method will also try to invoke the optimal
display precision by checking for a change in the displayed text.
"""
try:
value = D(value)
except TypeError:
if 'int' in type(value).__name__:
value = int(value)
elif 'float' in type(value).__name__:
value = float(value)
else:
raise
value = D(value)
# catch NaN values and set the "is_valid" flag to False until a valid value is set again.
if value.is_nan():
self._is_valid = False
return
value, in_range = self.check_range(value)
if self.__value != value or not self.is_valid:
# Try to increase decimals when the value has changed but no change in display detected.
# This will only be executed when the dynamic precision flag is set
if self.value() != float(value) and self.dynamic_precision and not value.is_infinite():
old_text = self.cleanText()
new_text = self.textFromValue(value).strip()
current_dec = self.decimals()
while old_text == new_text:
if self.__decimals > self.__max_decimals:
self.__decimals = current_dec
break
self.__decimals += 1
new_text = self.textFromValue(value).strip()
self.__value = value
self._is_valid = True
self.update_display()
self.valueChanged.emit(self.value())
def setProperty(self, prop, val):
"""
For compatibility with QtDesigner. Somehow the value gets initialized through this method.
:param prop:
:param val:
"""
if prop == 'value':
self.setValue(val)
else:
raise UserWarning('setProperty in scientific spinboxes only works for "value".')
def check_range(self, value):
"""
Helper method to check if the passed value is within the set minimum and maximum value
bounds.
If outside of bounds the returned value will be clipped to the nearest boundary.
:param value: float|Decimal, number to be checked
:return: (Decimal, bool), the corrected value and a flag indicating if the value has been
changed (False) or not (True)
"""
if value < self.__minimum:
value = D(self.__minimum)
in_range = False
elif value > self.__maximum:
value = D(self.__maximum)
in_range = False
else:
in_range = True
return value, in_range
def minimum(self):
return float(self.__minimum)
def setMinimum(self, minimum):
"""
Setter method to set the minimum value allowed in the SpinBox.
Input will be converted to float before being stored.
:param minimum: float, the minimum value to be set
"""
# Ignore NaN values
if self._check_nan(float(minimum)):
return
self.__minimum = float(minimum)
if self.__minimum > self.value():
self.setValue(self.__minimum)
def maximum(self):
return float(self.__maximum)
def setMaximum(self, maximum):
"""
Setter method to set the maximum value allowed in the SpinBox.
Input will be converted to float before being stored.
:param maximum: float, the maximum value to be set
"""
# Ignore NaN values
if self._check_nan(float(maximum)):
return
self.__maximum = float(maximum)
if self.__maximum < self.value():
self.setValue(self.__maximum)
def setRange(self, minimum, maximum):
"""
Convenience method for compliance with Qt SpinBoxes.
Essentially a wrapper to call both self.setMinimum and self.setMaximum.
:param minimum: float, the minimum value to be set
:param maximum: float, the maximum value to be set
"""
self.setMinimum(minimum)
self.setMaximum(maximum)
def decimals(self):
return self.__decimals
def setDecimals(self, decimals, dynamic_precision=True):
"""
Method to set the number of displayed digits after the decimal point.
Also specifies if the dynamic precision functionality should be used or not.
If dynamic_precision=True the number of decimals will be invoked from the number of
decimals entered by the user in the QLineEdit of this spinbox. The set decimal value will
only be used before the first explicit user text input or call to self.setValue.
If dynamic_precision=False the specified number of decimals will be fixed and will not be
changed except by calling this method.
:param decimals: int, the number of decimals to be displayed
:param dynamic_precision: bool, flag indicating the use of dynamic_precision
"""
decimals = int(decimals)
# Restrict the number of fractional digits to a maximum of self.__max_decimals = 20.
# Beyond that the number is not very meaningful anyways due to machine precision.
if decimals < 0:
decimals = 0
elif decimals > self.__max_decimals:
decimals = self.__max_decimals
self.__decimals = decimals
# Set the flag for using dynamic precision (decimals invoked from user input)
self.dynamic_precision = dynamic_precision
def prefix(self):
return self.__prefix
def setPrefix(self, prefix):
"""
Set a string to be shown as non-editable prefix in the spinbox.
:param prefix: str, the prefix string to be set
"""
self.__prefix = str(prefix)
self.update_display()
def suffix(self):
return self.__suffix
def setSuffix(self, suffix):
"""
Set a string to be shown as non-editable suffix in the spinbox.
This suffix will come right after the si-prefix.
:param suffix: str, the suffix string to be set
"""
self.__suffix = str(suffix)
self.update_display()
def singleStep(self):
return float(self.__singleStep)
def setSingleStep(self, step, dynamic_stepping=True):
"""
Method to set the stepping behaviour of the spinbox (e.g. when moving the mouse wheel).
When dynamic_stepping=True the spinbox will perform logarithmic steps according to the
values' current order of magnitude. The step parameter is then referring to the step size
relative to the values order of magnitude. Meaning step=0.1 would step increment the second
most significant digit by one etc.
When dynamic_stepping=False the step parameter specifies an absolute step size. Meaning each
time a step is performed this value is added/subtracted from the current value.
For maximum robustness and consistency it is strongly recommended to pass step as Decimal
or string in order to be converted lossless to Decimal.
:param step: Decimal|str, the (relative) step size to set
:param dynamic_stepping: bool, flag indicating the use of dynamic stepping (True) or
constant stepping (False)
"""
try:
step = D(step)
except TypeError:
if 'int' in type(step).__name__:
step = int(step)
elif 'float' in type(step).__name__:
step = float(step)
else:
raise
step = D(step)
# ignore NaN and infinity values
if not step.is_nan() and not step.is_infinite():
self.__singleStep = step
self.dynamic_stepping = dynamic_stepping
def minimalStep(self):
return float(self.__minimalStep)
def setMinimalStep(self, step):
"""
Method used to set a minimal step size.
When the absolute step size has been calculated in either dynamic or constant step mode
this value is checked against the minimal step size. If it is smaller then the minimal step
size is chosen over the calculated step size. This ensures that no step taken can be
smaller than minimalStep.
Set this value to 0 for no minimal step size.
For maximum robustness and consistency it is strongly recommended to pass step as Decimal
or string in order to be converted lossless to Decimal.
:param step: Decimal|str, the minimal step size to be set
"""
try:
step = D(step)
except TypeError:
if 'int' in type(step).__name__:
step = int(step)
elif 'float' in type(step).__name__:
step = float(step)
else:
raise
step = D(step)
# ignore NaN and infinity values
if not step.is_nan() and not step.is_infinite():
self.__minimalStep = step
def cleanText(self):
"""
Compliance method from Qt SpinBoxes.
Returns the currently shown text from the QLineEdit without prefix and suffix and stripped
from leading or trailing whitespaces.
:return: str, currently shown text stripped from suffix and prefix
"""
text = self.text().strip()
if self.__prefix and text.startswith(self.__prefix):
text = text[len(self.__prefix):]
if self.__suffix and text.endswith(self.__suffix):
text = text[:-len(self.__suffix)]
return text.strip()
def update_display(self):
"""
This helper method updates the shown text based on the current value.
Because this method is only called upon finishing an editing procedure, the eventually
cached value gets deleted.
"""
text = self.textFromValue(self.value())
text = self.__prefix + text + self.__suffix
self.lineEdit().setText(text)
self.__cached_value = None # clear cached value
self.lineEdit().setCursorPosition(0) # Display the most significant part of the number
def keyPressEvent(self, event):
"""
This method catches all keyboard press events triggered by the user. Can be used to alter
the behaviour of certain key events from the default implementation of QAbstractSpinBox.
:param event: QKeyEvent, a Qt QKeyEvent instance holding the event information
"""
# Restore cached value upon pressing escape and lose focus.
if event.key() == QtCore.Qt.Key_Escape:
if self.__cached_value is not None:
self.__value = self.__cached_value
self.valueChanged.emit(self.value())
self.clearFocus() # This will also trigger editingFinished
return
# Update display upon pressing enter/return before processing the event in the default way.
if event.key() == QtCore.Qt.Key_Enter or event.key() == QtCore.Qt.Key_Return:
self.update_display()
if (QtCore.Qt.ControlModifier | QtCore.Qt.MetaModifier) & event.modifiers():
super(ScienDSpinBox, self).keyPressEvent(event)
return
# The rest is to avoid editing suffix and prefix
if len(event.text()) > 0:
# Allow editing of the number or SI-prefix even if part of the prefix/suffix is selected
if self.lineEdit().selectedText():
sel_start = self.lineEdit().selectionStart()
sel_end = sel_start + len(self.lineEdit().selectedText())
min_start = len(self.__prefix)
max_end = len(self.__prefix) + len(self.cleanText())
if sel_start < min_start:
sel_start = min_start
if sel_end > max_end:
sel_end = max_end
self.lineEdit().setSelection(sel_start, sel_end - sel_start)
else:
cursor_pos = self.lineEdit().cursorPosition()
begin = len(self.__prefix)
end = len(self.text()) - len(self.__suffix)
if cursor_pos < begin:
self.lineEdit().setCursorPosition(begin)
elif cursor_pos > end:
self.lineEdit().setCursorPosition(end)
if event.key() == QtCore.Qt.Key_Left:
if self.lineEdit().cursorPosition() == len(self.__prefix):
return
if event.key() == QtCore.Qt.Key_Right:
if self.lineEdit().cursorPosition() == len(self.text()) - len(self.__suffix):
return
if event.key() == QtCore.Qt.Key_Home:
self.lineEdit().setCursorPosition(len(self.__prefix))
return
if event.key() == QtCore.Qt.Key_End:
self.lineEdit().setCursorPosition(len(self.text()) - len(self.__suffix))
return
super(ScienDSpinBox, self).keyPressEvent(event)
def focusInEvent(self, event):
super(ScienDSpinBox, self).focusInEvent(event)
self.selectAll()
return
def focusOutEvent(self, event):
super(ScienDSpinBox, self).focusOutEvent(event)
self.update_display()
return
def paintEvent(self, ev):
"""
Add drawing of a red frame around the spinbox if the is_valid flag is False
"""
super(ScienDSpinBox, self).paintEvent(ev)
# draw red frame if is_valid = False
# self.errorBox.setVisible(not self.is_valid)
def validate(self, text, position):
"""
Access method to the validator. See FloatValidator class for more information.
:param text: str, string to be validated.
:param position: int, current text cursor position
:return: (enum QValidator::State) the returned validator state,
(str) the input string, (int) the cursor position
"""
begin = len(self.__prefix)
end = len(text) - len(self.__suffix)
if position < begin:
position = begin
elif position > end:
position = end
if self.__prefix and text.startswith(self.__prefix):
text = text[len(self.__prefix):]
if self.__suffix and text.endswith(self.__suffix):
text = text[:-len(self.__suffix)]
state, string, position = self.validator.validate(text, position)
text = self.__prefix + string + self.__suffix
end = len(text) - len(self.__suffix)
if position > end:
position = end
value = self.valueFromText(text)
_, in_range = self.check_range(value)
self.errorBox.setVisible(not in_range)
return state, text, position
def fixup(self, text):
"""
Takes an invalid string and tries to fix it in order to pass validation.
The returned string is not guaranteed to pass validation.
:param text: str, a string that has not passed validation in need to be fixed.
:return: str, the resulting string from the fix attempt
"""
return self.validator.fixup(text)
def valueFromText(self, text):
"""
This method is responsible for converting a string displayed in the SpinBox into a Decimal.
The input string is already stripped of prefix and suffix.
Just the si-prefix may be present.
:param text: str, the display string to be converted into a numeric value.
This string must be conform with the validator.
:return: Decimal, the numeric value converted from the input string.
"""
# Check for infinite value
if 'inf' in text.lower():
if text.startswith('-'):
return D('-inf')
else:
return D('inf')
# Handle "normal" (non-infinite) input
group_dict = self.validator.get_group_dict(text)
if not group_dict:
return False
if not group_dict['mantissa']:
return False
si_prefix = group_dict['si']
if si_prefix is None:
si_prefix = ''
si_scale = self._unit_prefix_dict[si_prefix.replace('u', 'µ')]
if group_dict['sign'] is not None:
unscaled_value_str = group_dict['sign'] + group_dict['mantissa']
else:
unscaled_value_str = group_dict['mantissa']
if group_dict['exponent'] is not None:
unscaled_value_str += group_dict['exponent']
value = D(unscaled_value_str) * si_scale
# Try to extract the precision the user intends to use
if self.dynamic_precision:
split_mantissa = group_dict['mantissa'].split('.')
if len(split_mantissa) == 2:
self.setDecimals(max(len(split_mantissa[1]), 1))
else:
self.setDecimals(1) # Minimum number of digits is 1
return value
def textFromValue(self, value):
"""
This method is responsible for the mapping of the underlying value to a string to display
in the SpinBox.
Suffix and Prefix must not be handled here, just the si-Prefix.
The main problem here is, that a scaled float with a suffix is represented by a different
machine precision than the total value.
This method is so complicated because it represents the actual precision of the value as
float and not the precision of the scaled si float.
'{:.20f}'.format(value) shows different digits than
'{:.20f} {}'.format(scaled_value, si_prefix)
:param value: float|decimal.Decimal, the numeric value to be formatted into a string
:return: str, the formatted string representing the input value
"""
# Catch infinity value
if np.isinf(float(value)):
if value < 0:
return '-inf '
else:
return 'inf '
sign = '-' if value < 0 else ''
fractional, integer = math.modf(abs(value))
integer = int(integer)
si_prefix = ''
prefix_index = 0
if integer != 0 or fractional >= 0.1:
integer_str = str(integer)
fractional_str = ''
while len(integer_str) > 3:
fractional_str = integer_str[-3:] + fractional_str
integer_str = integer_str[:-3]
if prefix_index < 8:
si_prefix = 'kMGTPEZY'[prefix_index]
else:
si_prefix = 'e{0:d}'.format(3 * (prefix_index + 1))
prefix_index += 1
# Truncate and round to set number of decimals
# Add digits from fractional if it's not already enough for set self.__decimals
if self.__decimals < len(fractional_str):
round_indicator = int(fractional_str[self.__decimals])
fractional_str = fractional_str[:self.__decimals]
if round_indicator >= 5:
if not fractional_str:
fractional_str = '1'
else:
fractional_str = str(int(fractional_str) + 1)
elif self.__decimals == len(fractional_str):
if fractional >= 0.5:
if fractional_str:
fractional_int = int(fractional_str) + 1
fractional_str = str(fractional_int)
else:
fractional_str = '1'
elif self.__decimals > len(fractional_str):
digits_to_add = self.__decimals - len(fractional_str) # number of digits to add
fractional_tmp_str = ('{0:.' + str(digits_to_add) + 'f}').format(fractional)
if fractional_tmp_str.startswith('1'):
if fractional_str:
fractional_str = str(int(fractional_str) + 1) + '0' * digits_to_add
else:
fractional_str = '1' + '0' * digits_to_add
else:
fractional_str += fractional_tmp_str.split('.')[1]
# Check if the rounding has overflown the fractional part into the integer part
if len(fractional_str) > self.__decimals:
integer_str = str(int(integer_str) + 1)
fractional_str = '0' * self.__decimals
elif fractional == 0.0:
fractional_str = '0' * self.__decimals
integer_str = '0'
else:
# determine the order of magnitude by comparing the fractional to unit values
prefix_index = 1
magnitude = 1e-3
si_prefix = 'm'
while magnitude > fractional:
prefix_index += 1
magnitude = magnitude ** prefix_index
if prefix_index <= 8:
si_prefix = 'mµnpfazy'[prefix_index - 1] # use si-prefix if possible
else:
si_prefix = 'e-{0:d}'.format(3 * prefix_index) # use engineering notation
# Get the string representation of all needed digits from the fractional part of value.
digits_needed = 3 * prefix_index + self.__decimals
helper_str = ('{0:.' + str(digits_needed) + 'f}').format(fractional)
overflow = bool(int(helper_str.split('.')[0]))
helper_str = helper_str.split('.')[1]
if overflow:
integer_str = '1000'
fractional_str = '0' * self.__decimals
elif (prefix_index - 1) > 0 and helper_str[3 * (prefix_index - 1) - 1] != '0':
integer_str = '1000'
fractional_str = '0' * self.__decimals
else:
integer_str = str(int(helper_str[:3 * prefix_index]))
fractional_str = helper_str[3 * prefix_index:3 * prefix_index + self.__decimals]
# Create the actual string representation of value scaled in a scientific way
space = '' if si_prefix.startswith('e') else ' '
if self.__decimals > 0:
string = '{0}{1}.{2}{3}{4}'.format(sign, integer_str, fractional_str, space, si_prefix)
else:
string = '{0}{1}{2}{3}'.format(sign, integer_str, space, si_prefix)
return string
def stepEnabled(self):
"""
Enables stepping (mouse wheel, arrow up/down, clicking, PgUp/Down) by default.
"""
return self.StepUpEnabled | self.StepDownEnabled
def stepBy(self, steps):
"""
This method is incrementing the value of the SpinBox when the user triggers a step
(by pressing PgUp/PgDown/Up/Down, MouseWheel movement or clicking on the arrows).
It should handle the case when the new to-set value is out of bounds.
Also the absolute value of a single step increment should be handled here.
It is absolutely necessary to avoid accumulating rounding errors and/or discrepancy between
self.value and the displayed text.
:param steps: int, Number of steps to increment (NOT the absolute step size)
"""
# Ignore stepping for infinity values
if self.__value.is_infinite():
return
n = D(int(steps)) # n must be integral number of steps.
s = [D(-1), D(1)][n >= 0] # determine sign of step
value = self.__value # working copy of current value
if self.dynamic_stepping:
for i in range(int(abs(n))):
if value == 0:
step = self.__minimalStep
else:
vs = [D(-1), D(1)][value >= 0]
# fudge factor: at some places, the step size depends on the step sign
fudge = D('1.01') ** (s * vs)
exp = abs(value * fudge).log10().quantize(1, rounding=ROUND_FLOOR)
step = self.__singleStep * D(10) ** exp
if self.__minimalStep > 0:
step = max(step, self.__minimalStep)
value += s * step
else:
value = value + max(self.__minimalStep * n, self.__singleStep * n)
self.setValue(value)
return
def selectAll(self):
begin = len(self.__prefix)
text = self.cleanText()
if text.endswith(' '):
selection_length = len(text) + 1
elif len(text) > 0 and text[-1] in self._unit_prefix_dict:
selection_length = len(text) - 1
else:
selection_length = len(text)
self.lineEdit().setSelection(begin, selection_length)
@staticmethod
def _check_nan(value):
"""
Helper method to check if the passed float value is NaN.
Makes use of the fact that NaN values will always compare to false, even with itself.
:param value: Decimal|float, value to be checked for NaN
:return: (bool) is NaN (True), is no NaN (False)
"""
return not value == value
class ScienSpinBox(QtWidgets.QAbstractSpinBox):
"""
Wrapper Class from PyQt5 (or QtPy) to display a QSpinBox in Scientific way.
Fully supports prefix and suffix functionality of the QSpinBox.