|
8 | 8 | absolute_import, division, print_function, unicode_literals |
9 | 9 | ) |
10 | 10 |
|
11 | | -from .autoshape import AutoShapeType |
| 11 | +from .autoshape import AutoShapeType, Shape |
| 12 | +from .base import BaseShape |
12 | 13 | from ..enum.shapes import PP_PLACEHOLDER |
13 | | -from .factory import BaseShapeFactory, SlideShapeFactory |
| 14 | +from .graphfrm import GraphicFrame |
14 | 15 | from ..oxml.ns import qn |
15 | 16 | from ..oxml.shapes.graphfrm import CT_GraphicalObjectFrame |
16 | 17 | from ..oxml.simpletypes import ST_Direction |
17 | | -from .placeholder import LayoutPlaceholder, MasterPlaceholder |
| 18 | +from .picture import Picture |
| 19 | +from .placeholder import ( |
| 20 | + ChartPlaceholder, LayoutPlaceholder, MasterPlaceholder, |
| 21 | + PicturePlaceholder, PlaceholderGraphicFrame, PlaceholderPicture, |
| 22 | + SlidePlaceholder, TablePlaceholder |
| 23 | +) |
18 | 24 | from ..shared import ParentedElementProxy |
19 | 25 |
|
20 | 26 |
|
| 27 | +def BaseShapeFactory(shape_elm, parent): |
| 28 | + """ |
| 29 | + Return an instance of the appropriate shape proxy class for *shape_elm*. |
| 30 | + """ |
| 31 | + tag_name = shape_elm.tag |
| 32 | + if tag_name == qn('p:sp'): |
| 33 | + return Shape(shape_elm, parent) |
| 34 | + if tag_name == qn('p:pic'): |
| 35 | + return Picture(shape_elm, parent) |
| 36 | + if tag_name == qn('p:graphicFrame'): |
| 37 | + return GraphicFrame(shape_elm, parent) |
| 38 | + return BaseShape(shape_elm, parent) |
| 39 | + |
| 40 | + |
21 | 41 | class _BaseShapes(ParentedElementProxy): |
22 | 42 | """ |
23 | 43 | Base class for a shape collection appearing in a slide-type object, |
@@ -203,6 +223,74 @@ def _MasterShapeFactory(shape_elm, parent): |
203 | 223 | return BaseShapeFactory(shape_elm, parent) |
204 | 224 |
|
205 | 225 |
|
| 226 | +def _SlidePlaceholderFactory(shape_elm, parent): |
| 227 | + """ |
| 228 | + Return a placeholder shape of the appropriate type for *shape_elm*. |
| 229 | + """ |
| 230 | + tag = shape_elm.tag |
| 231 | + if tag == qn('p:sp'): |
| 232 | + Constructor = { |
| 233 | + PP_PLACEHOLDER.BITMAP: PicturePlaceholder, |
| 234 | + PP_PLACEHOLDER.CHART: ChartPlaceholder, |
| 235 | + PP_PLACEHOLDER.PICTURE: PicturePlaceholder, |
| 236 | + PP_PLACEHOLDER.TABLE: TablePlaceholder, |
| 237 | + }.get(shape_elm.ph_type, SlidePlaceholder) |
| 238 | + elif tag == qn('p:graphicFrame'): |
| 239 | + Constructor = PlaceholderGraphicFrame |
| 240 | + elif tag == qn('p:pic'): |
| 241 | + Constructor = PlaceholderPicture |
| 242 | + else: |
| 243 | + Constructor = BaseShapeFactory |
| 244 | + return Constructor(shape_elm, parent) |
| 245 | + |
| 246 | + |
| 247 | +class SlidePlaceholders(ParentedElementProxy): |
| 248 | + """ |
| 249 | + Collection of placeholder shapes on a slide. Supports iteration, |
| 250 | + :func:`len`, and dictionary-style lookup on the `idx` value of the |
| 251 | + placeholders it contains. |
| 252 | + """ |
| 253 | + |
| 254 | + __slots__ = () |
| 255 | + |
| 256 | + def __getitem__(self, idx): |
| 257 | + """ |
| 258 | + Access placeholder shape having *idx*. Note that while this looks |
| 259 | + like list access, idx is actually a dictionary key and will raise |
| 260 | + |KeyError| if no placeholder with that idx value is in the |
| 261 | + collection. |
| 262 | + """ |
| 263 | + for e in self._element.iter_ph_elms(): |
| 264 | + if e.ph_idx == idx: |
| 265 | + return SlideShapeFactory(e, self) |
| 266 | + raise KeyError('no placeholder on this slide with idx == %d' % idx) |
| 267 | + |
| 268 | + def __iter__(self): |
| 269 | + """ |
| 270 | + Generate placeholder shapes in `idx` order. |
| 271 | + """ |
| 272 | + ph_elms = sorted( |
| 273 | + [e for e in self._element.iter_ph_elms()], key=lambda e: e.ph_idx |
| 274 | + ) |
| 275 | + return (SlideShapeFactory(e, self) for e in ph_elms) |
| 276 | + |
| 277 | + def __len__(self): |
| 278 | + """ |
| 279 | + Return count of placeholder shapes. |
| 280 | + """ |
| 281 | + return len(list(self._element.iter_ph_elms())) |
| 282 | + |
| 283 | + |
| 284 | +def SlideShapeFactory(shape_elm, parent): |
| 285 | + """ |
| 286 | + Return an instance of the appropriate shape proxy class for *shape_elm* |
| 287 | + on a slide. |
| 288 | + """ |
| 289 | + if shape_elm.has_ph_elm: |
| 290 | + return _SlidePlaceholderFactory(shape_elm, parent) |
| 291 | + return BaseShapeFactory(shape_elm, parent) |
| 292 | + |
| 293 | + |
206 | 294 | class SlideShapes(_BaseShapes): |
207 | 295 | """ |
208 | 296 | Sequence of shapes appearing on a slide. The first shape in the sequence |
|
0 commit comments