axislabels.py (5629B)
1 #Embedded file name: C:\BuildAgent\work\20d73f1b24eed06e\eve\release\V24.01\packages\carbonui\graphs\axislabels.py 2 from __future__ import absolute_import 3 from carbonui.primitives.container import Container 4 from eve.client.script.ui.control.eveLabel import Label 5 import carbonui.const as uiconst 6 from carbonui.graphs import axis 7 from .pool import Pool 8 9 class AxisLabels(Container): 10 default_clipChildren = True 11 default_textAlignment = uiconst.BOTTOMRIGHT 12 default_format = str 13 default_hints = False 14 15 def ApplyAttributes(self, attributes): 16 Container.ApplyAttributes(self, attributes) 17 self.preferredWidth = attributes.width 18 self.CreatePools() 19 self._labels = [] 20 self.textAlignment = attributes.get('textAlignment', self.default_textAlignment) 21 self._orientation = attributes['orientation'] 22 self._axis = attributes['axis'] 23 self._minFactor = attributes.get('minFactor', 0.0) 24 self._maxFactor = attributes.get('maxFactor', 1.0) 25 self._format = attributes.get('format', self.default_format) 26 self._hints = attributes.get('hints', self.default_hints) 27 self._prevTicks = None 28 self._containers = [] 29 self._Build() 30 self._axis.onChange.connect(self._Build) 31 32 def CreatePools(self): 33 self._label_pool = Pool(Label) 34 self._container_pool = Pool(Container) 35 36 def _GetTicks(self): 37 width, height = self.GetAbsoluteSize() 38 size = width if self._orientation == axis.AxisOrientation.HORIZONTAL else height 39 ticks = [] 40 offsets = [] 41 for tick in self._axis.GetTicks(): 42 v = self._minFactor + self._axis.MapToView(tick) * (self._maxFactor - self._minFactor) 43 offset = v * size 44 ticks.append(tick) 45 offsets.append(offset) 46 47 return (ticks, offsets) 48 49 def _Build(self, *_): 50 containerSize = 16 51 width, height = self.GetAbsoluteSize() 52 size = width if self._orientation == axis.AxisOrientation.HORIZONTAL else height 53 ticks, offsets = self._GetTicks() 54 if ticks == self._prevTicks: 55 for tick, offset, cont in zip(ticks, offsets, self._containers): 56 if offset < 0 or offset >= size: 57 cont.display = False 58 else: 59 cont.display = True 60 offset -= containerSize / 2 61 if offset < 0: 62 offset = 0 63 elif offset + containerSize > size: 64 offset = size - containerSize 65 if self._orientation == axis.AxisOrientation.HORIZONTAL: 66 cont.left = offset 67 else: 68 cont.top = offset 69 70 return 71 self.ReturnUIResources() 72 self.Flush() 73 self._containers = [] 74 self._labels = [] 75 self._prevTicks = ticks 76 maxLabelWidth = -1 77 for tick, offset in zip(ticks, offsets): 78 state = uiconst.UI_DISABLED 79 if offset < 0 or offset >= size: 80 displayState = False 81 else: 82 displayState = True 83 offset -= containerSize / 2 84 if offset < 0: 85 offset = 0 86 elif offset + containerSize > size: 87 offset = size - containerSize 88 if self._orientation == axis.AxisOrientation.HORIZONTAL: 89 cont = self._container_pool.get_instance() 90 cont.SetParent(self) 91 cont.SetAlign(uiconst.TOPLEFT) 92 cont.width = containerSize 93 cont.height = self.height 94 cont.padding = (3, 0, 3, 0) 95 cont.left = offset 96 else: 97 cont = self._container_pool.get_instance() 98 cont.SetParent(self) 99 cont.SetAlign(uiconst.TOPLEFT) 100 cont.width = self.width 101 cont.height = containerSize 102 cont.padding = (3, 0, 3, 0) 103 cont.top = offset 104 cont.state = state 105 cont.display = displayState 106 self._containers.append(cont) 107 label = self.AddLabel(cont, tick) 108 maxLabelWidth = max(maxLabelWidth, label.width + 4) 109 self._labels.append(label) 110 if self._hints: 111 label.state = uiconst.UI_NORMAL 112 label.hint = self._format(tick) 113 114 self.ResizeWidthIfNeeded(maxLabelWidth) 115 116 def AddLabel(self, cont, tick): 117 label = self._label_pool.get_instance() 118 label.SetParent(cont) 119 label.SetAlign(self.textAlignment) 120 label.SetTextColor((1.0, 1.0, 1.0, 0.6)) 121 label.SetText(self._axis.GetTickLabel(tick)) 122 return label 123 124 def ResizeWidthIfNeeded(self, maxLabelWidth): 125 if self._orientation == axis.AxisOrientation.HORIZONTAL: 126 return 127 if self.preferredWidth and self.preferredWidth < maxLabelWidth: 128 self.width = maxLabelWidth 129 for eachCont in self._containers: 130 eachCont.width = maxLabelWidth 131 132 def ReturnUIResources(self): 133 for label in self._labels: 134 self._label_pool.return_instance(label) 135 136 for container in self._containers: 137 self._container_pool.return_instance(container) 138 139 def Clear(self): 140 self.ReturnUIResources() 141 self._prevTicks = None 142 self.Flush() 143 144 def Close(self): 145 self._container_pool.empty() 146 self._label_pool.empty() 147 self._container_pool = None 148 self._label_pool = None 149 super(AxisLabels, self).Close()