Skip to main content

umya_spreadsheet/structs/drawing/spreadsheet/
graphic_frame.rs

1// xdr:graphicFrame
2use std::io::Cursor;
3
4use quick_xml::{
5    Reader,
6    Writer,
7    events::{
8        BytesStart,
9        Event,
10    },
11};
12
13use super::{
14    super::{
15        super::StringValue,
16        Graphic,
17    },
18    NonVisualGraphicFrameProperties,
19    Transform,
20};
21use crate::{
22    reader::driver::{
23        get_attribute,
24        set_string_from_xml,
25        xml_read_loop,
26    },
27    structs::raw::RawRelationships,
28    traits::AdjustmentCoordinateWithSheet,
29    writer::driver::{
30        write_end_tag,
31        write_start_tag,
32    },
33};
34
35#[derive(Clone, Default, Debug)]
36pub struct GraphicFrame {
37    r#macro:                             StringValue,
38    non_visual_graphic_frame_properties: NonVisualGraphicFrameProperties,
39    transform:                           Transform,
40    graphic:                             Graphic,
41}
42
43impl GraphicFrame {
44    #[inline]
45    #[must_use]
46    pub fn r#macro(&self) -> &str {
47        self.r#macro.value_str()
48    }
49
50    #[inline]
51    #[must_use]
52    #[deprecated(since = "3.0.0", note = "Use macro()")]
53    pub fn get_macro(&self) -> &str {
54        self.r#macro()
55    }
56
57    #[inline]
58    pub fn set_macro<S: Into<String>>(&mut self, value: S) -> &mut GraphicFrame {
59        self.r#macro.set_value(value);
60        self
61    }
62
63    #[inline]
64    #[must_use]
65    pub fn non_visual_graphic_frame_properties(&self) -> &NonVisualGraphicFrameProperties {
66        &self.non_visual_graphic_frame_properties
67    }
68
69    #[inline]
70    #[must_use]
71    #[deprecated(since = "3.0.0", note = "Use non_visual_graphic_frame_properties()")]
72    pub fn get_non_visual_graphic_frame_properties(&self) -> &NonVisualGraphicFrameProperties {
73        self.non_visual_graphic_frame_properties()
74    }
75
76    #[inline]
77    pub fn non_visual_graphic_frame_properties_mut(
78        &mut self,
79    ) -> &mut NonVisualGraphicFrameProperties {
80        &mut self.non_visual_graphic_frame_properties
81    }
82
83    #[inline]
84    #[deprecated(
85        since = "3.0.0",
86        note = "Use non_visual_graphic_frame_properties_mut()"
87    )]
88    pub fn get_non_visual_graphic_frame_properties_mut(
89        &mut self,
90    ) -> &mut NonVisualGraphicFrameProperties {
91        self.non_visual_graphic_frame_properties_mut()
92    }
93
94    #[inline]
95    pub fn set_non_visual_graphic_frame_properties(
96        &mut self,
97        value: NonVisualGraphicFrameProperties,
98    ) -> &mut Self {
99        self.non_visual_graphic_frame_properties = value;
100        self
101    }
102
103    #[inline]
104    #[must_use]
105    pub fn transform(&self) -> &Transform {
106        &self.transform
107    }
108
109    #[inline]
110    #[must_use]
111    #[deprecated(since = "3.0.0", note = "Use transform()")]
112    pub fn get_transform(&self) -> &Transform {
113        self.transform()
114    }
115
116    #[inline]
117    pub fn transform_mut(&mut self) -> &mut Transform {
118        &mut self.transform
119    }
120
121    #[inline]
122    #[deprecated(since = "3.0.0", note = "Use transform_mut()")]
123    pub fn get_transform_mut(&mut self) -> &mut Transform {
124        self.transform_mut()
125    }
126
127    #[inline]
128    pub fn set_transform(&mut self, value: Transform) -> &mut Self {
129        self.transform = value;
130        self
131    }
132
133    #[inline]
134    #[must_use]
135    pub fn graphic(&self) -> &Graphic {
136        &self.graphic
137    }
138
139    #[inline]
140    #[must_use]
141    #[deprecated(since = "3.0.0", note = "Use graphic()")]
142    pub fn get_graphic(&self) -> &Graphic {
143        self.graphic()
144    }
145
146    #[inline]
147    pub fn graphic_mut(&mut self) -> &mut Graphic {
148        &mut self.graphic
149    }
150
151    #[inline]
152    #[deprecated(since = "3.0.0", note = "Use graphic_mut()")]
153    pub fn get_graphic_mut(&mut self) -> &mut Graphic {
154        self.graphic_mut()
155    }
156
157    #[inline]
158    pub fn set_graphic(&mut self, value: Graphic) -> &mut Self {
159        self.graphic = value;
160        self
161    }
162
163    pub(crate) fn set_attributes<R: std::io::BufRead>(
164        &mut self,
165        reader: &mut Reader<R>,
166        e: &BytesStart,
167        drawing_relationships: Option<&RawRelationships>,
168    ) {
169        set_string_from_xml!(self, e, r#macro, "macro");
170
171        xml_read_loop!(
172            reader,
173            Event::Start(ref e) => {
174                match e.name().into_inner() {
175                    b"xdr:nvGraphicFramePr" => {
176                        self.non_visual_graphic_frame_properties
177                            .set_attributes(reader, e);
178                        }
179                    b"xdr:xfrm" => {
180                        self.transform.set_attributes(reader, e);
181                    }
182                    b"a:graphic" => {
183                        self.graphic
184                            .set_attributes(reader, e, drawing_relationships);
185                        }
186                    _ => (),
187                }
188            },
189            Event::End(ref e) => {
190                if  e.name().into_inner() == b"xdr:graphicFrame" {
191                    return
192                }
193            },
194            Event::Eof => panic!("Error: Could not find {} end element", "xdr:graphicFrame")
195        );
196    }
197
198    pub(crate) fn write_to(
199        &self,
200        writer: &mut Writer<Cursor<Vec<u8>>>,
201        rel_list: &mut Vec<(String, String)>,
202    ) {
203        // xdr:graphicFrame
204        write_start_tag(
205            writer,
206            "xdr:graphicFrame",
207            vec![("macro", self.r#macro.value_str()).into()],
208            false,
209        );
210
211        // xdr:nvGraphicFramePr
212        self.non_visual_graphic_frame_properties.write_to(writer);
213
214        // xdr:xfrm
215        self.transform.write_to(writer);
216
217        // a:graphic
218        Graphic::write_to(writer, rel_list);
219
220        write_end_tag(writer, "xdr:graphicFrame");
221    }
222}
223impl AdjustmentCoordinateWithSheet for GraphicFrame {
224    #[inline]
225    fn adjustment_insert_coordinate_with_sheet(
226        &mut self,
227        sheet_name: &str,
228        root_col_num: u32,
229        offset_col_num: u32,
230        root_row_num: u32,
231        offset_row_num: u32,
232    ) {
233        self.graphic.adjustment_insert_coordinate_with_sheet(
234            sheet_name,
235            root_col_num,
236            offset_col_num,
237            root_row_num,
238            offset_row_num,
239        );
240    }
241
242    #[inline]
243    fn adjustment_remove_coordinate_with_sheet(
244        &mut self,
245        sheet_name: &str,
246        root_col_num: u32,
247        offset_col_num: u32,
248        root_row_num: u32,
249        offset_row_num: u32,
250    ) {
251        self.graphic.adjustment_remove_coordinate_with_sheet(
252            sheet_name,
253            root_col_num,
254            offset_col_num,
255            root_row_num,
256            offset_row_num,
257        );
258    }
259}