Skip to main content

umya_spreadsheet/structs/drawing/spreadsheet/
one_cell_anchor.rs

1// xdr:oneCellAnchor
2use std::io::Cursor;
3
4use quick_xml::{
5    Reader,
6    Writer,
7    events::{
8        BytesStart,
9        Event,
10    },
11};
12
13use super::{
14    Extent,
15    GroupShape,
16    MarkerType,
17    Picture,
18    Shape,
19};
20use crate::{
21    reader::driver::xml_read_loop,
22    structs::raw::RawRelationships,
23    traits::AdjustmentCoordinate,
24    writer::driver::{
25        write_end_tag,
26        write_start_tag,
27    },
28};
29
30#[derive(Clone, Default, Debug)]
31pub struct OneCellAnchor {
32    from_marker: MarkerType,
33    extent:      Extent,
34    group_shape: Option<Box<GroupShape>>,
35    shape:       Option<Box<Shape>>,
36    picture:     Option<Box<Picture>>,
37}
38
39impl OneCellAnchor {
40    #[inline]
41    #[must_use]
42    pub fn from_marker(&self) -> &MarkerType {
43        &self.from_marker
44    }
45
46    #[inline]
47    #[must_use]
48    #[deprecated(since = "3.0.0", note = "Use from_marker()")]
49    pub fn get_from_marker(&self) -> &MarkerType {
50        self.from_marker()
51    }
52
53    #[inline]
54    pub fn from_marker_mut(&mut self) -> &mut MarkerType {
55        &mut self.from_marker
56    }
57
58    #[inline]
59    #[deprecated(since = "3.0.0", note = "Use from_marker_mut()")]
60    pub fn get_from_marker_mut(&mut self) -> &mut MarkerType {
61        self.from_marker_mut()
62    }
63
64    #[inline]
65    pub fn set_from_marker(&mut self, value: MarkerType) -> &mut OneCellAnchor {
66        self.from_marker = value;
67        self
68    }
69
70    #[inline]
71    #[must_use]
72    pub fn extent(&self) -> &Extent {
73        &self.extent
74    }
75
76    #[inline]
77    #[must_use]
78    #[deprecated(since = "3.0.0", note = "Use extent()")]
79    pub fn get_extent(&self) -> &Extent {
80        self.extent()
81    }
82
83    #[inline]
84    pub fn extent_mut(&mut self) -> &mut Extent {
85        &mut self.extent
86    }
87
88    #[inline]
89    #[deprecated(since = "3.0.0", note = "Use extent_mut()")]
90    pub fn get_extent_mut(&mut self) -> &mut Extent {
91        self.extent_mut()
92    }
93
94    #[inline]
95    pub fn set_extent(&mut self, value: Extent) -> &mut OneCellAnchor {
96        self.extent = value;
97        self
98    }
99
100    #[inline]
101    #[must_use]
102    pub fn group_shape(&self) -> Option<&GroupShape> {
103        self.group_shape.as_deref()
104    }
105
106    #[inline]
107    #[must_use]
108    #[deprecated(since = "3.0.0", note = "Use group_shape()")]
109    pub fn get_group_shape(&self) -> Option<&GroupShape> {
110        self.group_shape()
111    }
112
113    #[inline]
114    pub fn group_shape_mut(&mut self) -> Option<&mut GroupShape> {
115        self.group_shape.as_deref_mut()
116    }
117
118    #[inline]
119    #[deprecated(since = "3.0.0", note = "Use group_shape_mut()")]
120    pub fn get_group_shape_mut(&mut self) -> Option<&mut GroupShape> {
121        self.group_shape_mut()
122    }
123
124    #[inline]
125    pub fn set_group_shape(&mut self, value: GroupShape) -> &mut Self {
126        self.group_shape = Some(Box::new(value));
127        self
128    }
129
130    #[inline]
131    #[must_use]
132    pub fn shape(&self) -> Option<&Shape> {
133        self.shape.as_deref()
134    }
135
136    #[inline]
137    #[must_use]
138    #[deprecated(since = "3.0.0", note = "Use shape()")]
139    pub fn get_shape(&self) -> Option<&Shape> {
140        self.shape()
141    }
142
143    #[inline]
144    pub fn shape_mut(&mut self) -> Option<&mut Shape> {
145        self.shape.as_deref_mut()
146    }
147
148    #[inline]
149    #[deprecated(since = "3.0.0", note = "Use shape_mut()")]
150    pub fn get_shape_mut(&mut self) -> Option<&mut Shape> {
151        self.shape_mut()
152    }
153
154    #[inline]
155    pub fn set_shape(&mut self, value: Shape) -> &mut OneCellAnchor {
156        self.shape = Some(Box::new(value));
157        self
158    }
159
160    #[inline]
161    #[must_use]
162    pub fn picture(&self) -> Option<&Picture> {
163        self.picture.as_deref()
164    }
165
166    #[inline]
167    #[must_use]
168    #[deprecated(since = "3.0.0", note = "Use picture()")]
169    pub fn get_picture(&self) -> Option<&Picture> {
170        self.picture()
171    }
172
173    #[inline]
174    pub fn picture_mut(&mut self) -> Option<&mut Picture> {
175        self.picture.as_deref_mut()
176    }
177
178    #[inline]
179    #[deprecated(since = "3.0.0", note = "Use picture_mut()")]
180    pub fn get_picture_mut(&mut self) -> Option<&mut Picture> {
181        self.picture_mut()
182    }
183
184    #[inline]
185    pub fn set_picture(&mut self, value: Picture) -> &mut Self {
186        self.picture = Some(Box::new(value));
187        self
188    }
189
190    #[inline]
191    pub(crate) fn is_image(&self) -> bool {
192        self.picture.is_some() || self.group_shape.is_some()
193    }
194
195    pub(crate) fn set_attributes<R: std::io::BufRead>(
196        &mut self,
197        reader: &mut Reader<R>,
198        _e: &BytesStart,
199        drawing_relationships: Option<&RawRelationships>,
200    ) {
201        xml_read_loop!(
202            reader,
203            Event::Start(ref e) => {
204                match e.name().into_inner() {
205                    b"xdr:from" | b"from" => {
206                        self.from_marker.set_attributes(reader, e);
207                    }
208                    b"xdr:grpSp" | b"grpSp" => {
209                        let mut obj = GroupShape::default();
210                        obj.set_attributes(reader, e, drawing_relationships);
211                        self.set_group_shape(obj);
212                    }
213                    b"xdr:sp" | b"sp" => {
214                        let mut obj = Shape::default();
215                        obj.set_attributes(reader, e, drawing_relationships);
216                        self.set_shape(obj);
217                    }
218                    b"xdr:pic" | b"pic" => {
219                        let mut obj = Picture::default();
220                        obj.set_attributes(reader, e, drawing_relationships);
221                        self.set_picture(obj);
222                    }
223                    _ => (),
224                }
225            },
226            Event::Empty(ref e) => {
227                if matches!(e.name().into_inner(), b"xdr:ext" | b"ext") {
228                    self.extent.set_attributes(reader, e);
229                }
230            },
231            Event::End(ref e) => {
232                if matches!(e.name().into_inner(), b"xdr:oneCellAnchor" | b"oneCellAnchor") {
233                    return
234                }
235            },
236            Event::Eof => panic!("Error: Could not find {} end element", "oneCellAnchor")
237        );
238    }
239
240    pub(crate) fn write_to(
241        &self,
242        writer: &mut Writer<Cursor<Vec<u8>>>,
243        rel_list: &mut Vec<(String, String)>,
244    ) {
245        // xdr:oneCellAnchor
246        write_start_tag(writer, "xdr:oneCellAnchor", vec![], false);
247
248        // xdr:from
249        self.from_marker.write_to_from(writer);
250
251        // xdr:ext
252        self.extent.write_to(writer);
253
254        // xdr:grpSp
255        if let Some(v) = &self.group_shape {
256            v.write_to(writer, rel_list);
257        }
258
259        // xdr:sp
260        if let Some(v) = &self.shape {
261            v.write_to(writer, rel_list, 0);
262        }
263
264        // xdr:pic
265        if let Some(v) = &self.picture {
266            v.write_to(writer, rel_list);
267        }
268
269        // xdr:clientData
270        write_start_tag(writer, "xdr:clientData", vec![], true);
271
272        write_end_tag(writer, "xdr:oneCellAnchor");
273    }
274}
275impl AdjustmentCoordinate for OneCellAnchor {
276    #[inline]
277    fn adjustment_insert_coordinate(
278        &mut self,
279        root_col_num: u32,
280        offset_col_num: u32,
281        root_row_num: u32,
282        offset_row_num: u32,
283    ) {
284        self.from_marker.adjustment_insert_coordinate(
285            root_col_num,
286            offset_col_num,
287            root_row_num,
288            offset_row_num,
289        );
290    }
291
292    #[inline]
293    fn adjustment_remove_coordinate(
294        &mut self,
295        root_col_num: u32,
296        offset_col_num: u32,
297        root_row_num: u32,
298        offset_row_num: u32,
299    ) {
300        self.from_marker.adjustment_remove_coordinate(
301            root_col_num,
302            offset_col_num,
303            root_row_num,
304            offset_row_num,
305        );
306    }
307
308    #[inline]
309    fn is_remove_coordinate(
310        &self,
311        root_col_num: u32,
312        offset_col_num: u32,
313        root_row_num: u32,
314        offset_row_num: u32,
315    ) -> bool {
316        self.from_marker.is_remove_coordinate(
317            root_col_num,
318            offset_col_num,
319            root_row_num,
320            offset_row_num,
321        )
322    }
323}