umya_spreadsheet/structs/drawing/
gradient_fill.rs1use 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::EnumValue,
16 BooleanValue,
17 },
18 GradientStopList,
19 LinearGradientFill,
20 TileFlipValues,
21 TileRectangle,
22};
23use crate::{
24 reader::driver::{
25 get_attribute,
26 set_string_from_xml,
27 xml_read_loop,
28 },
29 writer::driver::{
30 write_end_tag,
31 write_start_tag,
32 },
33};
34
35#[derive(Clone, Default, Debug)]
36pub struct GradientFill {
37 flip: EnumValue<TileFlipValues>,
38 rotate_with_shape: BooleanValue,
39 gradient_stop_list: GradientStopList,
40 linear_gradient_fill: Option<Box<LinearGradientFill>>,
41 tile_rectangle: Option<Box<TileRectangle>>,
42}
43
44impl GradientFill {
45 #[inline]
46 #[must_use]
47 pub fn flip(&self) -> &TileFlipValues {
48 self.flip.value()
49 }
50
51 #[inline]
52 #[must_use]
53 #[deprecated(since = "3.0.0", note = "Use flip()")]
54 pub fn get_flip(&self) -> &TileFlipValues {
55 self.flip()
56 }
57
58 #[inline]
59 pub fn set_flip(&mut self, value: TileFlipValues) -> &mut GradientFill {
60 self.flip.set_value(value);
61 self
62 }
63
64 #[inline]
65 #[must_use]
66 pub fn rotate_with_shape(&self) -> bool {
67 self.rotate_with_shape.value()
68 }
69
70 #[inline]
71 #[must_use]
72 #[deprecated(since = "3.0.0", note = "Use rotate_with_shape()")]
73 pub fn get_rotate_with_shape(&self) -> bool {
74 self.rotate_with_shape()
75 }
76
77 #[inline]
78 pub fn set_rotate_with_shape(&mut self, value: bool) -> &mut GradientFill {
79 self.rotate_with_shape.set_value(value);
80 self
81 }
82
83 #[inline]
84 #[must_use]
85 pub fn gradient_stop_list(&self) -> &GradientStopList {
86 &self.gradient_stop_list
87 }
88
89 #[inline]
90 #[must_use]
91 #[deprecated(since = "3.0.0", note = "Use gradient_stop_list()")]
92 pub fn get_gradient_stop_list(&self) -> &GradientStopList {
93 self.gradient_stop_list()
94 }
95
96 #[inline]
97 pub fn gradient_stop_list_mut(&mut self) -> &mut GradientStopList {
98 &mut self.gradient_stop_list
99 }
100
101 #[inline]
102 #[deprecated(since = "3.0.0", note = "Use gradient_stop_list_mut()")]
103 pub fn get_gradient_stop_list_mut(&mut self) -> &mut GradientStopList {
104 self.gradient_stop_list_mut()
105 }
106
107 #[inline]
108 pub fn set_gradient_stop_list(&mut self, value: GradientStopList) -> &mut GradientFill {
109 self.gradient_stop_list = value;
110 self
111 }
112
113 #[inline]
114 #[must_use]
115 pub fn linear_gradient_fill(&self) -> Option<&LinearGradientFill> {
116 self.linear_gradient_fill.as_deref()
117 }
118
119 #[inline]
120 #[must_use]
121 #[deprecated(since = "3.0.0", note = "Use linear_gradient_fill()")]
122 pub fn get_linear_gradient_fill(&self) -> Option<&LinearGradientFill> {
123 self.linear_gradient_fill()
124 }
125
126 #[inline]
127 pub fn linear_gradient_fill_mut(&mut self) -> Option<&mut LinearGradientFill> {
128 self.linear_gradient_fill.as_deref_mut()
129 }
130
131 #[inline]
132 #[deprecated(since = "3.0.0", note = "Use linear_gradient_fill_mut()")]
133 pub fn get_linear_gradient_fill_mut(&mut self) -> Option<&mut LinearGradientFill> {
134 self.linear_gradient_fill_mut()
135 }
136
137 #[inline]
138 pub fn set_linear_gradient_fill(&mut self, value: LinearGradientFill) -> &mut GradientFill {
139 self.linear_gradient_fill = Some(Box::new(value));
140 self
141 }
142
143 #[inline]
144 #[must_use]
145 pub fn tile_rectangle(&self) -> Option<&TileRectangle> {
146 self.tile_rectangle.as_deref()
147 }
148
149 #[inline]
150 #[must_use]
151 #[deprecated(since = "3.0.0", note = "Use tile_rectangle()")]
152 pub fn get_tile_rectangle(&self) -> Option<&TileRectangle> {
153 self.tile_rectangle()
154 }
155
156 #[inline]
157 pub fn tile_rectangle_mut(&mut self) -> Option<&mut TileRectangle> {
158 self.tile_rectangle.as_deref_mut()
159 }
160
161 #[inline]
162 #[deprecated(since = "3.0.0", note = "Use tile_rectangle_mut()")]
163 pub fn get_tile_rectangle_mut(&mut self) -> Option<&mut TileRectangle> {
164 self.tile_rectangle_mut()
165 }
166
167 #[inline]
168 pub fn set_tile_rectangle(&mut self, value: TileRectangle) -> &mut GradientFill {
169 self.tile_rectangle = Some(Box::new(value));
170 self
171 }
172
173 pub(crate) fn set_attributes<R: std::io::BufRead>(
174 &mut self,
175 reader: &mut Reader<R>,
176 e: &BytesStart,
177 ) {
178 set_string_from_xml!(self, e, flip, "flip");
179 set_string_from_xml!(self, e, rotate_with_shape, "rotWithShape");
180
181 xml_read_loop!(
182 reader,
183 Event::Empty(ref e) => {
184 match e.name().into_inner() {
185 b"a:lin" => {
186 let mut obj = LinearGradientFill::default();
187 obj.set_attributes(reader, e, true);
188 self.set_linear_gradient_fill(obj);
189 }
190 b"a:tileRect" => {
191 let obj = TileRectangle::default();
192 TileRectangle::set_attributes(reader, e, true);
193 self.set_tile_rectangle(obj);
194 }
195 _ => (),
196 }
197 },
198 Event::Start(ref e) => {
199 match e.name().into_inner() {
200 b"a:lin" => {
201 let mut obj = LinearGradientFill::default();
202 obj.set_attributes(reader, e, false);
203 self.set_linear_gradient_fill(obj);
204 }
205 b"a:tileRect" => {
206 let obj = TileRectangle::default();
207 TileRectangle::set_attributes(reader, e, false);
208 self.set_tile_rectangle(obj);
209 }
210 b"a:gsLst" => {
211 self.gradient_stop_list.set_attributes(reader, e);
212 }
213 _ => (),
214 }
215 },
216 Event::End(ref e) => {
217 if e.name().into_inner() == b"a:gradFill" {
218 return
219 }
220 },
221 Event::Eof => panic!("Error: Could not find {} end element", "a:gradFill")
222 );
223 }
224
225 pub(crate) fn write_to(&self, writer: &mut Writer<Cursor<Vec<u8>>>) {
226 let mut attributes: crate::structs::AttrCollection = Vec::new();
228 if self.flip.has_value() {
229 attributes.push(("flip", self.flip.value_string()).into());
230 }
231 if self.rotate_with_shape.has_value() {
232 attributes.push(("rotWithShape", self.rotate_with_shape.value_string()).into());
233 }
234 write_start_tag(writer, "a:gradFill", attributes, false);
235
236 self.gradient_stop_list.write_to(writer);
238
239 if let Some(v) = &self.linear_gradient_fill {
241 v.write_to(writer);
242 }
243
244 if self.tile_rectangle.is_some() {
246 TileRectangle::write_to(writer);
247 }
248
249 write_end_tag(writer, "a:gradFill");
250 }
251}