1 // Copyright (c) 2014-2020 Thomas Fussell
2 // Copyright (c) 2010-2015 openpyxl
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining a copy
5 // of this software and associated documentation files (the "Software"), to deal
6 // in the Software without restriction, including without limitation the rights
7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 // copies of the Software, and to permit persons to whom the Software is
9 // furnished to do so, subject to the following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, WRISING FROM,
19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 // THE SOFTWARE
21 //
22 // @license: http://www.opensource.org/licenses/mit-license.php
23 // @author: see AUTHORS file
24 
25 #pragma once
26 
27 #include <xlnt/xlnt_config.hpp>
28 #include <xlnt/utils/optional.hpp>
29 #include <xlnt/worksheet/pane.hpp>
30 #include <xlnt/worksheet/selection.hpp>
31 
32 namespace xlnt {
33 
34 /// <summary>
35 /// Enumeration of possible types of sheet views
36 /// </summary>
37 enum class sheet_view_type
38 {
39     normal,
40     page_break_preview,
41     page_layout
42 };
43 
44 /// <summary>
45 /// Describes a view of a worksheet.
46 /// Worksheets can have multiple views which show the data differently.
47 /// </summary>
48 class XLNT_API sheet_view
49 {
50 public:
51     /// <summary>
52     /// Sets the ID of this view to new_id.
53     /// </summary>
id(std::size_t new_id)54     void id(std::size_t new_id)
55     {
56         id_ = new_id;
57     }
58 
59     /// <summary>
60     /// Returns the ID of this view.
61     /// </summary>
id() const62     std::size_t id() const
63     {
64         return id_;
65     }
66 
67     /// <summary>
68     /// Returns true if this view has a pane defined.
69     /// </summary>
has_pane() const70     bool has_pane() const
71     {
72         return pane_.is_set();
73     }
74 
75     /// <summary>
76     /// Returns a reference to this view's pane.
77     /// </summary>
pane()78     struct pane &pane()
79     {
80         return pane_.get();
81     }
82 
83     /// <summary>
84     /// Returns a reference to this view's pane.
85     /// </summary>
pane() const86     const struct pane &pane() const
87     {
88         return pane_.get();
89     }
90 
91     /// <summary>
92     /// Removes the defined pane from this view.
93     /// </summary>
clear_pane()94     void clear_pane()
95     {
96         pane_.clear();
97     }
98 
99     /// <summary>
100     /// Sets the pane of this view to new_pane.
101     /// </summary>
pane(const struct pane & new_pane)102     void pane(const struct pane &new_pane)
103     {
104         pane_ = new_pane;
105     }
106 
107     /// <summary>
108     /// Returns true if this view has any selections.
109     /// </summary>
has_selections() const110     bool has_selections() const
111     {
112         return !selections_.empty();
113     }
114 
115     /// <summary>
116     /// Adds the given selection to the collection of selections.
117     /// </summary>
add_selection(const class selection & new_selection)118     void add_selection(const class selection &new_selection)
119     {
120         selections_.push_back(new_selection);
121     }
122 
123     /// <summary>
124     /// Removes all selections.
125     /// </summary>
clear_selections()126     void clear_selections()
127     {
128         selections_.clear();
129     }
130 
131     /// <summary>
132     /// Returns the collection of selections as a vector.
133     /// </summary>
selections() const134     std::vector<xlnt::selection> selections() const
135     {
136         return selections_;
137     }
138 
139     /// <summary>
140     /// Returns the selection at the given index.
141     /// </summary>
selection(std::size_t index)142     class xlnt::selection &selection(std::size_t index)
143     {
144         return selections_.at(index);
145     }
146 
147     /// <summary>
148     /// If show is true, grid lines will be shown for sheets using this view.
149     /// </summary>
show_grid_lines(bool show)150     void show_grid_lines(bool show)
151     {
152         show_grid_lines_ = show;
153     }
154 
155     /// <summary>
156     /// Returns true if grid lines will be shown for sheets using this view.
157     /// </summary>
show_grid_lines() const158     bool show_grid_lines() const
159     {
160         return show_grid_lines_;
161     }
162 
163     /// <summary>
164     /// If is_default is true, the default grid color will be used.
165     /// </summary>
default_grid_color(bool is_default)166     void default_grid_color(bool is_default)
167     {
168         default_grid_color_ = is_default;
169     }
170 
171     /// <summary>
172     /// Returns true if the default grid color will be used.
173     /// </summary>
default_grid_color() const174     bool default_grid_color() const
175     {
176         return default_grid_color_;
177     }
178 
179     /// <summary>
180     /// Sets the type of this view.
181     /// </summary>
type(sheet_view_type new_type)182     void type(sheet_view_type new_type)
183     {
184         type_ = new_type;
185     }
186 
187     /// <summary>
188     /// Returns the type of this view.
189     /// </summary>
type() const190     sheet_view_type type() const
191     {
192         return type_;
193     }
194 
195     /// <summary>
196     /// has a  top left cell?
197     /// </summary>
has_top_left_cell() const198     bool has_top_left_cell() const
199     {
200         return top_left_cell_.is_set();
201     }
202 
203     /// <summary>
204     /// Sets the top left cell of this view.
205     /// </summary>
top_left_cell(const cell_reference & ref)206     void top_left_cell(const cell_reference &ref)
207     {
208         top_left_cell_.set(ref);
209     }
210 
211     /// <summary>
212     /// Returns the top left cell of this view.
213     /// </summary>
top_left_cell() const214     cell_reference top_left_cell() const
215     {
216         return top_left_cell_.get();
217     }
218 
219     /// <summary>
220     /// Returns true if this view is equal to rhs based on its id, grid lines setting,
221     /// default grid color, pane, and selections.
222     /// </summary>
operator ==(const sheet_view & rhs) const223     bool operator==(const sheet_view &rhs) const
224     {
225         return id_ == rhs.id_
226             && show_grid_lines_ == rhs.show_grid_lines_
227             && default_grid_color_ == rhs.default_grid_color_
228             && pane_ == rhs.pane_
229             && selections_ == rhs.selections_
230             && top_left_cell_ == rhs.top_left_cell_;
231     }
232 
233 private:
234     /// <summary>
235     /// The id
236     /// </summary>
237     std::size_t id_ = 0;
238 
239     /// <summary>
240     /// Whether or not to show grid lines
241     /// </summary>
242     bool show_grid_lines_ = true;
243 
244     /// <summary>
245     /// Whether or not to use the default grid color
246     /// </summary>
247     bool default_grid_color_ = true;
248 
249     /// <summary>
250     /// The type of this view
251     /// </summary>
252     sheet_view_type type_ = sheet_view_type::normal;
253 
254     /// <summary>
255     /// The optional pane
256     /// </summary>
257     optional<xlnt::pane> pane_;
258 
259     /// <summary>
260     /// The top left cell
261     /// </summary>
262     optional<cell_reference> top_left_cell_;
263 
264     /// <summary>
265     /// The collection of selections
266     /// </summary>
267     std::vector<xlnt::selection> selections_;
268 };
269 
270 } // namespace xlnt
271