|
1 | 1 | from . import six |
2 | | -from . import Font, Fill, Format |
| 2 | +from . import Font, Fill, Format, Alignment |
3 | 3 | from . import Utility |
4 | 4 |
|
5 | 5 | class Style(object): |
6 | 6 | _DEFAULT_FORMAT = Format.Format() |
7 | 7 | _DEFAULT_FILL = Fill.Fill() |
8 | 8 | _DEFAULT_FONT = Font.Font() |
9 | | - def __init__(self, font=None, fill=None, format=None): |
| 9 | + def __init__(self, font=None, fill=None, format=None, alignment=None): |
10 | 10 | self._font = font |
11 | 11 | self._fill = fill |
12 | 12 | self._format = format |
| 13 | + self._alignment = alignment |
13 | 14 |
|
14 | 15 | @property |
15 | 16 | def is_default(self): |
16 | | - return not (self._font or self._fill or self._format) |
| 17 | + return not (self._font or self._fill or self._format or self._alignment) |
17 | 18 |
|
| 19 | + @property |
| 20 | + def alignment(self): |
| 21 | + return Utility.lazy_get(self, '_alignment', Alignment.Alignment()) |
| 22 | + |
| 23 | + @alignment.setter |
| 24 | + def alignment(self, value): |
| 25 | + Utility.lazy_set(self, '_alignment', None, value) |
| 26 | + |
18 | 27 | @property |
19 | 28 | def format(self): |
20 | 29 | # don't use default because default should be const |
21 | 30 | return Utility.lazy_get(self, '_format', Format.Format()) |
22 | 31 |
|
23 | 32 | @format.setter |
24 | 33 | def format(self, value): |
25 | | - Utility.lazy_set(self, '_format', Style._DEFAULT_FORMAT, value) |
| 34 | + Utility.lazy_set(self, '_format', None, value) |
26 | 35 |
|
27 | 36 | @property |
28 | 37 | def font(self): |
29 | 38 | return Utility.lazy_get(self, '_font', Font.Font()) |
30 | 39 |
|
31 | 40 | @font.setter |
32 | 41 | def font(self, value): |
33 | | - Utility.lazy_set(self, '_font', Style._DEFAULT_FONT, value) |
| 42 | + Utility.lazy_set(self, '_font', None, value) |
34 | 43 |
|
35 | 44 | @property |
36 | 45 | def fill(self): |
37 | 46 | return Utility.lazy_get(self, '_fill', Fill.Fill()) |
38 | 47 |
|
39 | 48 | @fill.setter |
40 | 49 | def fill(self, value): |
41 | | - Utility.lazy_set(self, '_fill', Style._DEFAULT_FILL, value) |
| 50 | + Utility.lazy_set(self, '_fill', None, value) |
42 | 51 |
|
43 | 52 | def get_xml_string(self): |
44 | 53 | # Precondition: Workbook._align_styles has been run. |
45 | 54 | # Be careful when using this function as id's may be inaccurate if precondition not met. |
46 | 55 | tag = [] |
47 | | - if not self.format.is_default: |
| 56 | + if not self._format is None: |
48 | 57 | tag.append("numFmtId=\"%d\"" % self.format.id) |
49 | | - if not self.font.is_default: |
| 58 | + if not self._font is None: |
50 | 59 | tag.append("applyFont=\"1\" fontId=\"%d\"" % (self.font.id)) |
51 | | - if not self.fill.is_default: |
| 60 | + if not self._fill is None: |
52 | 61 | tag.append("applyFill=\"1\" fillId=\"%d\"" % (self.fill.id + 1)) |
53 | | - return "<xf xfId=\"0\" borderId=\"0\" %s/>" % (" ".join(tag)) |
| 62 | + if self._alignment is None: |
| 63 | + return "<xf xfId=\"0\" borderId=\"0\" %s/>" % (" ".join(tag)) |
| 64 | + else: |
| 65 | + return "<xf xfId=\"0\" borderId=\"0\" %s applyAlignment=\"1\">%s</xf>" % (" ".join(tag), self._alignment.get_xml_string()) |
54 | 66 |
|
55 | 67 | def __hash__(self): |
56 | 68 | return hash(self._font) |
|
0 commit comments