NovaBryan/EVE_Dump
Log | Files | 📦 下载最新源码 (.zip) | Refs | README

cardsContainer.py (8605B)


      1 #Embedded file name: C:\BuildAgent\work\20d73f1b24eed06e\eve\release\V24.01\packages\carbonui\primitives\cardsContainer.py
      2 from carbonui import uiconst
      3 from carbonui.uicore import uicore
      4 from carbonui.primitives.container import Container
      5 from carbonui.primitives.base import Base
      6 import math
      7 
      8 class _CardGroup(Container):
      9     default_fill_always = False
     10     default_contentSpacing = (0, 0)
     11 
     12     def ApplyAttributes(self, attributes):
     13         super(_CardGroup, self).ApplyAttributes(attributes)
     14         self.fill_always = attributes.get('fill_always', self.default_fill_always)
     15         self.contentSpacing = attributes.get('contentSpacing', self.default_contentSpacing)
     16 
     17     def get_num_children(self):
     18         return len(self.children)
     19 
     20     def _AppendChildRO(self, child):
     21         child.align = uiconst.TOALL
     22         child.pos = (0, 0, 0, 0)
     23         super(_CardGroup, self)._AppendChildRO(child)
     24 
     25     def _InsertChildRO(self, idx, child):
     26         child.align = uiconst.TOALL
     27         child.pos = (0, 0, 0, 0)
     28         super(_CardGroup, self)._InsertChildRO(idx, child)
     29 
     30     def UpdateAlignment(self, budgetLeft = 0, budgetTop = 0, budgetWidth = 0, budgetHeight = 0, updateChildrenOnly = False):
     31         if self.destroyed:
     32             return (budgetLeft,
     33              budgetTop,
     34              budgetWidth,
     35              budgetHeight,
     36              False)
     37         budgetLeft, budgetTop, budgetWidth, budgetHeight, updateChildrenOnly = super(_CardGroup, self).UpdateAlignment(budgetLeft, budgetTop, budgetWidth, budgetHeight, updateChildrenOnly)
     38         columnCount = len(self.children)
     39         spacingX = self.contentSpacing[0]
     40         step = (self.displayWidth - spacingX * (columnCount - 1)) / float(columnCount) if columnCount else 0
     41         for idx, child in enumerate(self.children):
     42             cellLeft = step * idx
     43             cellRight = step * (idx + 1)
     44             padLeft = spacingX if 0 < idx < columnCount else 0
     45             budget = (cellLeft + padLeft * idx,
     46              0,
     47              cellRight - cellLeft,
     48              self.displayHeight)
     49             child._alignmentDirty = True
     50             child.UpdateAlignment(*budget)
     51 
     52         return (budgetLeft,
     53          budgetTop,
     54          budgetWidth,
     55          budgetHeight,
     56          updateChildrenOnly)
     57 
     58 
     59 class CardsContainer(Container):
     60     default_cardHeight = 100
     61     default_cardMaxWidth = 350
     62     default_contentSpacing = (0, 0)
     63     default_maxColumnCount = None
     64     default_allow_stretch = False
     65 
     66     def ApplyAttributes(self, attributes):
     67         super(CardsContainer, self).ApplyAttributes(attributes)
     68         self._cardHeight = attributes.get('cardHeight', self.default_cardHeight)
     69         self._cardMaxWidth = attributes.get('cardMaxWidth', self.default_cardMaxWidth)
     70         self._contentSpacing = attributes.get('contentSpacing', self.default_contentSpacing)
     71         self.maxColumnCount = attributes.get('maxColumnCount', self.default_maxColumnCount)
     72         self.allow_stretch = attributes.get('allow_stretch', self.default_allow_stretch)
     73 
     74     @property
     75     def cardMaxWidth(self):
     76         return self._cardMaxWidth
     77 
     78     @cardMaxWidth.setter
     79     def cardMaxWidth(self, value):
     80         self._cardMaxWidth = value
     81         self.FlagAlignmentDirty()
     82 
     83     @property
     84     def cardHeight(self):
     85         return self._cardHeight
     86 
     87     @cardHeight.setter
     88     def cardHeight(self, value):
     89         self._cardHeight = value
     90         self.FlagAlignmentDirty()
     91 
     92     @property
     93     def contentSpacing(self):
     94         return self._contentSpacing
     95 
     96     @contentSpacing.setter
     97     def contentSpacing(self, value):
     98         self._contentSpacing = value
     99         self.FlagAlignmentDirty()
    100 
    101     def UpdateAlignment(self, budgetLeft = 0, budgetTop = 0, budgetWidth = 0, budgetHeight = 0, updateChildrenOnly = False):
    102         if self.destroyed:
    103             return (budgetLeft,
    104              budgetTop,
    105              budgetWidth,
    106              budgetHeight,
    107              False)
    108         numColumns = self._GetNumColumns()
    109         lines = self._GetLines(numColumns)
    110         numLines = len(lines)
    111         self.height = numLines * self.cardHeight + (numLines - 1) * self.contentSpacing[1]
    112         forceUpdate = self._forceUpdateAlignment
    113         if updateChildrenOnly:
    114             childrenDirty = True
    115             sizeChange = False
    116         else:
    117             budgetLeft, budgetTop, budgetWidth, budgetHeight, sizeChange = Base.UpdateAlignment(self, budgetLeft, budgetTop, budgetWidth, budgetHeight)
    118             childrenDirty = self._childrenAlignmentDirty
    119         self._childrenAlignmentDirty = False
    120         numChildren = len(self.children)
    121         if numChildren and (childrenDirty or forceUpdate or sizeChange):
    122             cellHeight = uicore.ScaleDpi(self.cardHeight)
    123             spacingX = uicore.ScaleDpi(self.contentSpacing[0])
    124             spacingY = uicore.ScaleDpi(self.contentSpacing[1])
    125             cellTop = 0
    126             padTop = 0
    127             for line in lines:
    128                 cellTop += padTop
    129                 cellLeft = 0
    130                 cellBottom = cellTop + cellHeight
    131                 lineColumns = 0
    132                 for group in line:
    133                     lineColumns += self._GetColCount(group)
    134 
    135                 if not self.allow_stretch and not self._GetLineFill(line):
    136                     lineColumns = max(lineColumns, numColumns)
    137                 cellWidth = (self.displayWidth - spacingX * (lineColumns - 1)) / float(lineColumns) if lineColumns else 0
    138                 lineIdx = 0
    139                 for group in line:
    140                     colCount = self._GetColCount(group)
    141                     cellRight = min(self.displayWidth, int(cellWidth * (lineIdx + colCount)))
    142                     if colCount > 1:
    143                         cellRight += spacingX * (colCount - 1)
    144                     padLeft = spacingX if 0 < lineIdx < lineColumns else 0
    145                     budget = (cellLeft + padLeft * lineIdx,
    146                      cellTop,
    147                      cellRight - cellLeft,
    148                      cellBottom - cellTop)
    149                     group._alignmentDirty = True
    150                     if forceUpdate:
    151                         group._forceUpdateAlignment = True
    152                     group.UpdateAlignment(*budget)
    153                     cellLeft = cellRight
    154                     lineIdx += colCount
    155 
    156                 cellTop = min(self.displayHeight, cellBottom)
    157                 padTop = spacingY
    158 
    159         return (budgetLeft,
    160          budgetTop,
    161          budgetWidth,
    162          budgetHeight,
    163          sizeChange)
    164 
    165     def _GetNumColumns(self):
    166         numColumns = int(math.ceil(max(0, uicore.ReverseScaleDpi(self.displayWidth)) / self.cardMaxWidth + 0.5))
    167         if self.maxColumnCount is not None:
    168             numColumns = min(numColumns, self.maxColumnCount)
    169         return numColumns
    170 
    171     def _GetLines(self, numColumns):
    172         lines = []
    173         currentLine = []
    174         currentColumnCount = 0
    175         for group in self.children:
    176             if self._GetFill(group):
    177                 if len(currentLine) > 0:
    178                     lines.append(currentLine)
    179                     currentLine = []
    180                 currentLine.append(group)
    181                 lines.append(currentLine)
    182                 currentLine = []
    183                 currentColumnCount = 0
    184                 continue
    185             groupColCount = self._GetColCount(group)
    186             if groupColCount + currentColumnCount > numColumns and currentColumnCount:
    187                 lines.append(currentLine)
    188                 currentLine = [group]
    189                 currentColumnCount = groupColCount
    190             else:
    191                 currentLine.append(group)
    192                 currentColumnCount += groupColCount
    193 
    194         if len(currentLine) > 0:
    195             lines.append(currentLine)
    196         return lines
    197 
    198     def _GetColCount(self, item):
    199         if isinstance(item, _CardGroup):
    200             return item.get_num_children()
    201         return 1
    202 
    203     def _GetFill(self, item):
    204         if isinstance(item, _CardGroup):
    205             return item.fill_always
    206         return False
    207 
    208     def _GetLineFill(self, line):
    209         for item in line:
    210             if self._GetFill(item):
    211                 return True
    212 
    213         return False
    214 
    215     def _AppendChildRO(self, child):
    216         child.align = uiconst.TOALL
    217         child.pos = (0, 0, 0, 0)
    218         super(CardsContainer, self)._AppendChildRO(child)
    219 
    220     def _InsertChildRO(self, idx, child):
    221         child.align = uiconst.TOALL
    222         child.pos = (0, 0, 0, 0)
    223         super(CardsContainer, self)._InsertChildRO(idx, child)
    224 
    225     def CreateGroup(self, fill_always = False):
    226         return _CardGroup(parent=self, fill_always=fill_always, contentSpacing=self.contentSpacing)