umya_spreadsheet/structs/drawing/
gradient_fill.rs1use super::super::super::EnumValue;
3use super::super::BooleanValue;
4use super::GradientStopList;
5use super::LinearGradientFill;
6use super::TileFlipValues;
7use super::TileRectangle;
8use crate::reader::driver::*;
9use crate::writer::driver::*;
10use quick_xml::events::{BytesStart, Event};
11use quick_xml::Reader;
12use quick_xml::Writer;
13use std::io::Cursor;
14
15#[derive(Clone, Default, Debug)]
16pub struct GradientFill {
17 flip: EnumValue<TileFlipValues>,
18 rotate_with_shape: BooleanValue,
19 gradient_stop_list: GradientStopList,
20 linear_gradient_fill: Option<Box<LinearGradientFill>>,
21 tile_rectangle: Option<Box<TileRectangle>>,
22}
23
24impl GradientFill {
25 #[inline]
26 pub fn get_flip(&self) -> &TileFlipValues {
27 self.flip.get_value()
28 }
29
30 #[inline]
31 pub fn set_flip(&mut self, value: TileFlipValues) -> &mut GradientFill {
32 self.flip.set_value(value);
33 self
34 }
35
36 #[inline]
37 pub fn get_rotate_with_shape(&self) -> &bool {
38 self.rotate_with_shape.get_value()
39 }
40
41 #[inline]
42 pub fn set_rotate_with_shape(&mut self, value: bool) -> &mut GradientFill {
43 self.rotate_with_shape.set_value(value);
44 self
45 }
46
47 #[inline]
48 pub fn get_gradient_stop_list(&self) -> &GradientStopList {
49 &self.gradient_stop_list
50 }
51
52 #[inline]
53 pub fn get_gradient_stop_list_mut(&mut self) -> &mut GradientStopList {
54 &mut self.gradient_stop_list
55 }
56
57 #[inline]
58 pub fn set_gradient_stop_list(&mut self, value: GradientStopList) -> &mut GradientFill {
59 self.gradient_stop_list = value;
60 self
61 }
62
63 #[inline]
64 pub fn get_linear_gradient_fill(&self) -> Option<&LinearGradientFill> {
65 self.linear_gradient_fill.as_deref()
66 }
67
68 #[inline]
69 pub fn get_linear_gradient_fill_mut(&mut self) -> Option<&mut LinearGradientFill> {
70 self.linear_gradient_fill.as_deref_mut()
71 }
72
73 #[inline]
74 pub fn set_linear_gradient_fill(&mut self, value: LinearGradientFill) -> &mut GradientFill {
75 self.linear_gradient_fill = Some(Box::new(value));
76 self
77 }
78
79 #[inline]
80 pub fn get_tile_rectangle(&self) -> Option<&TileRectangle> {
81 self.tile_rectangle.as_deref()
82 }
83
84 #[inline]
85 pub fn get_tile_rectangle_mut(&mut self) -> Option<&mut TileRectangle> {
86 self.tile_rectangle.as_deref_mut()
87 }
88
89 #[inline]
90 pub fn set_tile_rectangle(&mut self, value: TileRectangle) -> &mut GradientFill {
91 self.tile_rectangle = Some(Box::new(value));
92 self
93 }
94
95 pub(crate) fn set_attributes<R: std::io::BufRead>(
96 &mut self,
97 reader: &mut Reader<R>,
98 e: &BytesStart,
99 ) {
100 set_string_from_xml!(self, e, flip, "flip");
101 set_string_from_xml!(self, e, rotate_with_shape, "rotWithShape");
102
103 xml_read_loop!(
104 reader,
105 Event::Empty(ref e) => {
106 match e.name().into_inner() {
107 b"a:lin" => {
108 let mut obj = LinearGradientFill::default();
109 obj.set_attributes(reader, e, true);
110 self.set_linear_gradient_fill(obj);
111 }
112 b"a:tileRect" => {
113 let mut obj = TileRectangle::default();
114 obj.set_attributes(reader, e, true);
115 self.set_tile_rectangle(obj);
116 }
117 _ => (),
118 }
119 },
120 Event::Start(ref e) => {
121 match e.name().into_inner() {
122 b"a:lin" => {
123 let mut obj = LinearGradientFill::default();
124 obj.set_attributes(reader, e, false);
125 self.set_linear_gradient_fill(obj);
126 }
127 b"a:tileRect" => {
128 let mut obj = TileRectangle::default();
129 obj.set_attributes(reader, e, false);
130 self.set_tile_rectangle(obj);
131 }
132 b"a:gsLst" => {
133 self.gradient_stop_list.set_attributes(reader, e);
134 }
135 _ => (),
136 }
137 },
138 Event::End(ref e) => {
139 if e.name().into_inner() == b"a:gradFill" {
140 return
141 }
142 },
143 Event::Eof => panic!("Error: Could not find {} end element", "a:gradFill")
144 );
145 }
146
147 pub(crate) fn write_to(&self, writer: &mut Writer<Cursor<Vec<u8>>>) {
148 let mut attributes: Vec<(&str, &str)> = Vec::new();
150 if self.flip.has_value() {
151 attributes.push(("flip", self.flip.get_value_string()));
152 }
153 if self.rotate_with_shape.has_value() {
154 attributes.push(("rotWithShape", self.rotate_with_shape.get_value_string()));
155 }
156 write_start_tag(writer, "a:gradFill", attributes, false);
157
158 self.gradient_stop_list.write_to(writer);
160
161 if let Some(v) = &self.linear_gradient_fill {
163 v.write_to(writer);
164 }
165
166 if let Some(v) = &self.tile_rectangle {
168 v.write_to(writer);
169 }
170
171 write_end_tag(writer, "a:gradFill");
172 }
173}