singleLineEditFloat.py (5816B)
1 #Embedded file name: C:\BuildAgent\work\20d73f1b24eed06e\eve\release\V24.01\packages\carbonui\control\singlelineedits\singleLineEditFloat.py 2 import eveLocalization 3 import localization 4 from carbon.common.script.util.commonutils import StripTags 5 from carbonui import uiconst 6 from carbonui.control.singlelineedits.baseSingleLineEditNumber import BaseSingleLineEditNumbers 7 from carbonui.uicore import uicore 8 from eveprefs import prefs 9 from player_messaging.client.ui_message import prompt_player_fsd_dialog 10 from qtyTooltip.qtyConst import EDIT_INPUT_TYPE_FLOAT 11 from qtyTooltip.tooltip import LoadTooltipForNumber 12 13 class SingleLineEditFloat(BaseSingleLineEditNumbers): 14 default_name = 'SingleLineEditFloat' 15 default_minValue = 0.0 16 default_maxValue = 922337203685477.0 17 default_decimalPlaces = 1 18 default_dataType = float 19 default_legalCharacters = '-0123456789e.' 20 DECIMAL = '.' 21 22 def ApplyAttributes(self, attributes): 23 self.decimalPlaces = attributes.get('decimalPlaces', self.default_decimalPlaces) 24 super(SingleLineEditFloat, self).ApplyAttributes(attributes) 25 26 def ValidateNewText(self, newText): 27 if newText == self.DECIMAL: 28 return False 29 if newText.count(self.DECIMAL) > 1: 30 prompt_player_fsd_dialog('uiwarning03') 31 return False 32 significand, decimalPoint, decimals = newText.partition(self.DECIMAL) 33 hasTooManyDecimals = len(decimals) > self.decimalPlaces 34 if hasTooManyDecimals: 35 numDecimalsToCut = len(decimals) - self.decimalPlaces 36 return newText[:-numDecimalsToCut] 37 return newText 38 39 def SetText(self, text, format = True): 40 if not isinstance(text, basestring): 41 text = '%.*f' % (self.decimalPlaces, self.dataType(text)) 42 text = StripTags(text, stripOnly=['localized']) 43 decimalPlaces = 0 44 if self.DECIMAL in text: 45 significand, decimalPoint, decimals = text.partition(self.DECIMAL) 46 decimalPlaces = min(len(decimals), self.decimalPlaces) 47 if decimalPlaces: 48 text = significand + decimalPoint + decimals[:decimalPlaces] 49 displayText = text 50 if format: 51 displayText = self.FormatNumeric(text, decimalPlaces=decimalPlaces) 52 text = self.StripLeadingZeroesIfNecessary(text) 53 displayText = self.SanitizeText(displayText) 54 shouldAddHyphen = self.HYPHEN in text and self.HYPHEN not in displayText 55 if shouldAddHyphen: 56 displayText = self.HYPHEN + displayText 57 shouldAddDecimal = decimalPlaces == 0 and self.DECIMAL in text 58 if shouldAddDecimal: 59 displayText += self.GetLocalizedDecimal() 60 self.textLabel.SetText(displayText) 61 self.text = text 62 63 def GetLocalizedDecimal(self): 64 if session: 65 localizedDecimal = eveLocalization.GetDecimalSeparator(localization.SYSTEM_LANGUAGE) 66 else: 67 localizedDecimal = prefs.GetValue('decimal', '.') 68 return localizedDecimal 69 70 def SetValue(self, text, docallback = 1): 71 isString = isinstance(text, basestring) 72 if isString: 73 text = StripTags(text, stripOnly=['localized']) 74 text = self.PrepareFloatString(text) 75 return super(SingleLineEditFloat, self).SetValue(text, docallback) 76 77 def Paste(self, paste, deleteStart = None, deleteEnd = None, forceFocus = False): 78 paste = self.FilterIllegalChars(paste) 79 for char in paste: 80 if char not in self.legalCharacters: 81 prompt_player_fsd_dialog('uiwarning03') 82 return 83 84 paste = self.PrepareFloatString(paste) 85 super(SingleLineEditFloat, self).Paste(paste, deleteStart, deleteEnd, forceFocus) 86 87 def OnChar(self, char, flag): 88 if unichr(char) in ',.': 89 return False 90 return super(SingleLineEditFloat, self).OnChar(char, flag) 91 92 def OnKeyDown(self, vkey, flag): 93 if vkey in (uiconst.VK_DECIMAL, uiconst.VK_OEM_PERIOD, uiconst.VK_OEM_COMMA): 94 self.Insert(self.DECIMAL) 95 return 96 super(SingleLineEditFloat, self).OnKeyDown(vkey, flag) 97 98 def LoadTooltipPanel(self, tooltipPanel, *args): 99 if not self.text or self.text == '-': 100 return 101 return LoadTooltipForNumber(tooltipPanel, self.hint, self.dataType(self.text), EDIT_INPUT_TYPE_FLOAT) 102 103 def ChangeNumericValue(self, val): 104 if uicore.uilib.Key(uiconst.VK_CONTROL): 105 val *= 10 106 val *= 1 / float(10 ** self.decimalPlaces) 107 super(SingleLineEditFloat, self).ChangeNumericValue(val) 108 109 @staticmethod 110 def PrepareFloatString(numberString): 111 commasInString = numberString.count(',') 112 periodsInString = numberString.count('.') 113 if commasInString and periodsInString: 114 haveDecimal = False 115 stripped = u'' 116 for each in reversed(unicode(numberString)): 117 if each in '-0123456789e.': 118 if each in u',.': 119 if haveDecimal: 120 continue 121 haveDecimal = True 122 stripped = '.' + stripped 123 else: 124 stripped = each + stripped 125 126 return stripped 127 if commasInString >= 2: 128 numberString = filter(lambda x: x in '-0123456789e.', numberString) 129 return numberString 130 if periodsInString >= 2: 131 numberString = filter(lambda x: x in '-0123456789e,', numberString) 132 numberString = numberString.replace(',', '.') 133 return numberString 134 numberString = filter(lambda x: x in '-0123456789e,.', numberString) 135 numberString = numberString.replace(',', '.') 136 return numberString