umya_spreadsheet/structs/drawing/spreadsheet/
shape.rs

1// xdr:sp
2use super::super::super::Anchor;
3use super::NonVisualShapeProperties;
4use super::ShapeProperties;
5use super::ShapeStyle;
6use super::TextBody;
7use crate::reader::driver::*;
8use crate::structs::raw::RawRelationships;
9use crate::writer::driver::*;
10use crate::StringValue;
11use quick_xml::events::{BytesStart, Event};
12use quick_xml::Reader;
13use quick_xml::Writer;
14use std::io::Cursor;
15
16#[derive(Clone, Default, Debug)]
17pub struct Shape {
18    anchor: Anchor,
19    non_visual_shape_properties: NonVisualShapeProperties,
20    shape_properties: ShapeProperties,
21    shape_style: Option<Box<ShapeStyle>>,
22    text_body: Option<Box<TextBody>>,
23    r#macro: StringValue,
24}
25
26impl Shape {
27    #[inline]
28    pub fn get_anchor(&self) -> &Anchor {
29        &self.anchor
30    }
31
32    #[inline]
33    pub fn get_anchor_mut(&mut self) -> &mut Anchor {
34        &mut self.anchor
35    }
36
37    #[inline]
38    pub fn set_anchor(&mut self, value: Anchor) {
39        self.anchor = value;
40    }
41
42    #[inline]
43    pub fn get_non_visual_shape_properties(&self) -> &NonVisualShapeProperties {
44        &self.non_visual_shape_properties
45    }
46
47    pub fn get_non_visual_shape_properties_mut(&mut self) -> &mut NonVisualShapeProperties {
48        &mut self.non_visual_shape_properties
49    }
50
51    #[inline]
52    pub fn set_non_visual_shape_properties(&mut self, value: NonVisualShapeProperties) {
53        self.non_visual_shape_properties = value;
54    }
55
56    #[inline]
57    pub fn get_shape_properties(&self) -> &ShapeProperties {
58        &self.shape_properties
59    }
60
61    #[inline]
62    pub fn get_shape_properties_mut(&mut self) -> &mut ShapeProperties {
63        &mut self.shape_properties
64    }
65
66    #[inline]
67    pub fn set_shape_properties(&mut self, value: ShapeProperties) {
68        self.shape_properties = value;
69    }
70
71    pub fn get_shape_style(&self) -> Option<&ShapeStyle> {
72        self.shape_style.as_deref()
73    }
74
75    #[inline]
76    pub fn get_shape_style_mut(&mut self) -> Option<&mut ShapeStyle> {
77        self.shape_style.as_deref_mut()
78    }
79
80    #[inline]
81    pub fn set_shape_style(&mut self, value: ShapeStyle) {
82        self.shape_style = Some(Box::new(value));
83    }
84
85    #[inline]
86    pub fn get_text_body(&self) -> Option<&TextBody> {
87        self.text_body.as_deref()
88    }
89
90    #[inline]
91    pub fn get_text_body_mut(&mut self) -> Option<&mut TextBody> {
92        self.text_body.as_deref_mut()
93    }
94
95    #[inline]
96    pub fn set_text_body(&mut self, value: TextBody) {
97        self.text_body = Some(Box::new(value));
98    }
99
100    #[inline]
101    pub fn get_macro(&self) -> &str {
102        self.r#macro.get_value_str()
103    }
104
105    #[inline]
106    pub fn set_macro<S: Into<String>>(&mut self, value: S) -> &mut Self {
107        self.r#macro.set_value(value);
108        self
109    }
110
111    pub(crate) fn set_attributes<R: std::io::BufRead>(
112        &mut self,
113        reader: &mut Reader<R>,
114        e: &BytesStart,
115        drawing_relationships: Option<&RawRelationships>,
116    ) {
117        set_string_from_xml!(self, e, r#macro, "macro");
118
119        xml_read_loop!(
120            reader,
121                Event::Start(ref e) => {
122                    match e.name().into_inner() {
123                        b"xdr:nvSpPr" => {
124                            self.non_visual_shape_properties.set_attributes(reader, e);
125                        }
126                        b"xdr:spPr" => {
127                            self.shape_properties.set_attributes(reader, e, drawing_relationships);
128                        }
129                        b"xdr:style" => {
130                            let mut obj = ShapeStyle::default();
131                            obj.set_attributes(reader, e);
132                            self.set_shape_style(obj);
133                        }
134                        b"xdr:txBody" => {
135                            let mut obj = TextBody::default();
136                            obj.set_attributes(reader, e);
137                            self.set_text_body(obj);
138                        }
139                        _ => (),
140                    }
141                },
142                Event::End(ref e) => {
143                    if e.name().into_inner() == b"xdr:sp" {
144                        return;
145                    }
146                },
147                Event::Eof => panic!("Error: Could not find {} end element", "xdr:sp")
148        );
149    }
150
151    pub(crate) fn write_to(
152        &self,
153        writer: &mut Writer<Cursor<Vec<u8>>>,
154        rel_list: &mut Vec<(String, String)>,
155        ole_id: &usize,
156    ) {
157        // xdr:sp
158        write_start_tag(
159            writer,
160            "xdr:sp",
161            vec![("macro", self.r#macro.get_value_str()), ("textlink", "")],
162            false,
163        );
164
165        // xdr:nvSpPr
166        self.non_visual_shape_properties.write_to(writer, ole_id);
167
168        // xdr:spPr
169        self.shape_properties.write_to(writer, rel_list);
170
171        // xdr:style
172        if let Some(v) = &self.shape_style {
173            v.write_to(writer);
174        }
175
176        // xdr:txBody
177        if let Some(v) = &self.text_body {
178            v.write_to(writer);
179        }
180
181        write_end_tag(writer, "xdr:sp");
182    }
183}