Package muntjac :: Package ui :: Module alignment_utils
[hide private]
[frames] | no frames]

Source Code for Module muntjac.ui.alignment_utils

  1  # Copyright (C) 2012 Vaadin Ltd.  
  2  # Copyright (C) 2012 Richard Lincoln 
  3  #  
  4  # Licensed under the Apache License, Version 2.0 (the "License");  
  5  # you may not use this file except in compliance with the License.  
  6  # You may obtain a copy of the License at  
  7  #  
  8  #     http://www.apache.org/licenses/LICENSE-2.0  
  9  #  
 10  # Unless required by applicable law or agreed to in writing, software  
 11  # distributed under the License is distributed on an "AS IS" BASIS,  
 12  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
 13  # See the License for the specific language governing permissions and  
 14  # limitations under the License. 
 15   
 16  """Defines a helper class for setting alignments using a short notation.""" 
 17   
 18  from muntjac.ui.alignment import Alignment 
 19  from muntjac.ui.layout import IAlignmentHandler 
20 21 22 -class AlignmentUtils(object):
23 """Helper class for setting alignments using a short notation. 24 25 Supported notation is: 26 27 - t, top for top alignment 28 - m, middle for vertical center alignment 29 - b, bottom for bottom alignment 30 - l, left for left alignment 31 - c, center for horizontal center alignment 32 - r, right for right alignment 33 34 @deprecated: replaced by L{Alignment}. 35 """ 36 37 _horizontalMask = (IAlignmentHandler.ALIGNMENT_LEFT 38 | IAlignmentHandler.ALIGNMENT_HORIZONTAL_CENTER 39 | IAlignmentHandler.ALIGNMENT_RIGHT) 40 41 _verticalMask = (IAlignmentHandler.ALIGNMENT_TOP 42 | IAlignmentHandler.ALIGNMENT_VERTICAL_CENTER 43 | IAlignmentHandler.ALIGNMENT_BOTTOM) 44 45 _alignmentStrings = dict() 46 47 @classmethod
48 - def addMapping(cls, alignment, *values):
49 for s in values: 50 cls._alignmentStrings[s] = alignment
51 52 53 @classmethod
54 - def setComponentAlignment(cls, parent, component, alignment):
55 """Set the alignment for the component using short notation. 56 57 @param parent: 58 @param component: 59 @param alignment: 60 String containing one or two alignment strings. If short 61 notation "r", "t", etc is used valid strings include 62 "r", "rt", "tr", "t". If the longer notation is used the 63 alignments should be separated by a space e.g. 64 "right", "right top", "top right", "top". It is valid to 65 mix short and long notation but they must be separated by a 66 space e.g. "r top". 67 @raise ValueError: 68 """ 69 if alignment is None or len(alignment) == 0: 70 raise ValueError, ('alignment for setComponentAlignment() ' 71 'cannot be null or empty') 72 73 currentAlignment = parent.getComponentAlignment( 74 component).getBitMask() 75 76 if len(alignment) == 1: 77 # Use short form "t","l",... 78 currentAlignment = cls.parseAlignment(alignment[:1], 79 currentAlignment) 80 81 elif len(alignment) == 2: 82 # Use short form "tr","lb",... 83 currentAlignment = cls.parseAlignment(alignment[:1], 84 currentAlignment) 85 currentAlignment = cls.parseAlignment(alignment[1:2], 86 currentAlignment) 87 88 else: 89 # Alignments are separated by space 90 strings = alignment.split(' ') 91 if len(strings) > 2: 92 raise ValueError, ('alignment for setComponentAlignment() ' 93 'should not contain more than 2 alignments') 94 95 for alignmentString in strings: 96 currentAlignment = cls.parseAlignment(alignmentString, 97 currentAlignment) 98 99 horizontalAlignment = currentAlignment & cls._horizontalMask 100 verticalAlignment = currentAlignment & cls._verticalMask 101 parent.setComponentAlignment(component, 102 Alignment(horizontalAlignment + verticalAlignment))
103 104 105 @classmethod
106 - def parseAlignment(cls, alignmentString, alignment):
107 """Parse alignmentString which contains one alignment (horizontal 108 or vertical) and return and updated version of the passed alignment 109 where the alignment in one direction has been changed. If the passed 110 alignmentString is unknown an exception is thrown 111 112 @raise ValueError: 113 """ 114 parsed = cls._alignmentStrings.get( alignmentString.lower() ) 115 116 if parsed is None: 117 raise ValueError, ('Could not parse alignment string \'' 118 + alignmentString + '\'') 119 120 if parsed & cls._horizontalMask != 0: 121 # Get the vertical alignment from the current alignment 122 vertical = alignment & cls._verticalMask 123 # Add the parsed horizontal alignment 124 alignment = vertical | parsed 125 126 else: 127 # Get the horizontal alignment from the current alignment 128 horizontal = alignment & cls._horizontalMask 129 # Add the parsed vertical alignment 130 alignment = horizontal | parsed 131 132 return alignment
133 134 135 AlignmentUtils.addMapping(IAlignmentHandler.ALIGNMENT_TOP, 't', 'top') 136 AlignmentUtils.addMapping(IAlignmentHandler.ALIGNMENT_BOTTOM, 'b', 'bottom') 137 AlignmentUtils.addMapping(IAlignmentHandler.ALIGNMENT_VERTICAL_CENTER, 'm', 'middle') 138 AlignmentUtils.addMapping(IAlignmentHandler.ALIGNMENT_LEFT, 'l', 'left') 139 AlignmentUtils.addMapping(IAlignmentHandler.ALIGNMENT_RIGHT, 'r', 'right') 140 AlignmentUtils.addMapping(IAlignmentHandler.ALIGNMENT_HORIZONTAL_CENTER, 'c', 'center') 141