Skip to main content

umya_spreadsheet/structs/drawing/charts/
layout.rs

1// c:layout
2use std::io::Cursor;
3
4use quick_xml::{
5    Reader,
6    Writer,
7    events::{
8        BytesStart,
9        Event,
10    },
11};
12
13use super::ManualLayout;
14use crate::{
15    writer::driver::{
16        write_end_tag,
17        write_start_tag,
18    },
19    xml_read_loop,
20};
21
22#[derive(Clone, Default, Debug)]
23pub struct Layout {
24    manual_layout: Option<ManualLayout>,
25}
26
27impl Layout {
28    #[must_use]
29    pub fn manual_layout(&self) -> Option<&ManualLayout> {
30        self.manual_layout.as_ref()
31    }
32
33    #[must_use]
34    #[deprecated(since = "3.0.0", note = "Use manual_layout()")]
35    pub fn get_manual_layout(&self) -> Option<&ManualLayout> {
36        self.manual_layout()
37    }
38
39    pub fn manual_layout_mut(&mut self) -> Option<&mut ManualLayout> {
40        self.manual_layout.as_mut()
41    }
42
43    #[deprecated(since = "3.0.0", note = "Use manual_layout_mut()")]
44    pub fn get_manual_layout_mut(&mut self) -> Option<&mut ManualLayout> {
45        self.manual_layout_mut()
46    }
47
48    pub fn set_manual_layout(&mut self, value: ManualLayout) -> &mut Layout {
49        self.manual_layout = Some(value);
50        self
51    }
52
53    #[must_use]
54    pub fn is_empty(&self) -> bool {
55        self.manual_layout.is_none()
56    }
57
58    pub(crate) fn set_attributes<R: std::io::BufRead>(
59        &mut self,
60        reader: &mut Reader<R>,
61        _e: &BytesStart,
62        empty_flag: bool,
63    ) {
64        if empty_flag {
65            return;
66        }
67
68        xml_read_loop!(
69            reader,
70            Event::Start(ref e) => {
71                if e.name().into_inner() == b"c:manualLayout" {
72                    let mut obj = ManualLayout::default();
73                    obj.set_attributes(reader, e);
74                    self.set_manual_layout(obj);
75                }
76            },
77            Event::End(ref e) => {
78                if e.name().into_inner() == b"c:layout" {
79                    return;
80                }
81            },
82            Event::Eof => panic!("Error: Could not find {} end element", "c:layout"),
83        );
84    }
85
86    pub(crate) fn write_to(&self, writer: &mut Writer<Cursor<Vec<u8>>>) {
87        if self.is_empty() {
88            // c:layout
89            write_start_tag(writer, "c:layout", vec![], true);
90        } else {
91            // c:layout
92            write_start_tag(writer, "c:layout", vec![], false);
93
94            // c:manualLayout
95            if let Some(v) = &self.manual_layout {
96                v.write_to(writer);
97            }
98
99            write_end_tag(writer, "c:layout");
100        }
101    }
102}