xlnt
sheet_view.hpp
1 // Copyright (c) 2014-2022 Thomas Fussell
2 // Copyright (c) 2010-2015 openpyxl
3 // Copyright (c) 2024 xlnt-community
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the "Software"), to deal
7 // in the Software without restriction, including without limitation the rights
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
11 //
12 // The above copyright notice and this permission notice shall be included in
13 // all copies or substantial portions of the Software.
14 //
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 // THE SOFTWARE
22 //
23 // @license: http://www.opensource.org/licenses/mit-license.php
24 // @author: see AUTHORS file
25 
26 #pragma once
27 
28 #include <xlnt/xlnt_config.hpp>
29 #include <xlnt/utils/optional.hpp>
30 #include <xlnt/worksheet/pane.hpp>
31 #include <xlnt/worksheet/selection.hpp>
32 
33 namespace xlnt {
34 
38 enum class sheet_view_type
39 {
40  normal,
41  page_break_preview,
42  page_layout
43 };
44 
49 class XLNT_API sheet_view
50 {
51 public:
55  void id(std::size_t new_id)
56  {
57  id_ = new_id;
58  }
59 
63  std::size_t id() const
64  {
65  return id_;
66  }
67 
71  bool has_pane() const
72  {
73  return pane_.is_set();
74  }
75 
79  struct pane &pane()
80  {
81  return pane_.get();
82  }
83 
87  const struct pane &pane() const
88  {
89  return pane_.get();
90  }
91 
95  void clear_pane()
96  {
97  pane_.clear();
98  }
99 
103  void pane(const struct pane &new_pane)
104  {
105  pane_ = new_pane;
106  }
107 
111  bool has_selections() const
112  {
113  return !selections_.empty();
114  }
115 
119  void add_selection(const class selection &new_selection)
120  {
121  selections_.push_back(new_selection);
122  }
123 
128  {
129  selections_.clear();
130  }
131 
135  std::vector<xlnt::selection> selections() const
136  {
137  return selections_;
138  }
139 
143  class xlnt::selection &selection(std::size_t index)
144  {
145  return selections_.at(index);
146  }
147 
151  void show_grid_lines(bool show)
152  {
153  show_grid_lines_ = show;
154  }
155 
159  bool show_grid_lines() const
160  {
161  return show_grid_lines_;
162  }
163 
167  void default_grid_color(bool is_default)
168  {
169  default_grid_color_ = is_default;
170  }
171 
175  bool default_grid_color() const
176  {
177  return default_grid_color_;
178  }
179 
183  void type(sheet_view_type new_type)
184  {
185  type_ = new_type;
186  }
187 
192  {
193  return type_;
194  }
195 
199  bool has_top_left_cell() const
200  {
201  return top_left_cell_.is_set();
202  }
203 
207  void top_left_cell(const cell_reference &ref)
208  {
209  top_left_cell_.set(ref);
210  }
211 
216  {
217  return top_left_cell_.get();
218  }
219 
224  bool operator==(const sheet_view &rhs) const
225  {
226  return id_ == rhs.id_
227  && show_grid_lines_ == rhs.show_grid_lines_
228  && default_grid_color_ == rhs.default_grid_color_
229  && pane_ == rhs.pane_
230  && selections_ == rhs.selections_
231  && top_left_cell_ == rhs.top_left_cell_;
232  }
233 
234 private:
238  std::size_t id_ = 0;
239 
243  bool show_grid_lines_ = true;
244 
248  bool default_grid_color_ = true;
249 
253  sheet_view_type type_ = sheet_view_type::normal;
254 
258  optional<xlnt::pane> pane_;
259 
263  optional<cell_reference> top_left_cell_;
264 
268  std::vector<xlnt::selection> selections_;
269 };
270 
271 } // namespace xlnt
void clear_selections()
Removes all selections.
Definition: sheet_view.hpp:127
void top_left_cell(const cell_reference &ref)
Sets the top left cell of this view.
Definition: sheet_view.hpp:207
bool show_grid_lines() const
Returns true if grid lines will be shown for sheets using this view.
Definition: sheet_view.hpp:159
class xlnt::selection & selection(std::size_t index)
Returns the selection at the given index.
Definition: sheet_view.hpp:143
struct pane & pane()
Returns a reference to this view&#39;s pane.
Definition: sheet_view.hpp:79
std::size_t id() const
Returns the ID of this view.
Definition: sheet_view.hpp:63
void add_selection(const class selection &new_selection)
Adds the given selection to the collection of selections.
Definition: sheet_view.hpp:119
bool has_top_left_cell() const
has a top left cell?
Definition: sheet_view.hpp:199
void pane(const struct pane &new_pane)
Sets the pane of this view to new_pane.
Definition: sheet_view.hpp:103
cell_reference top_left_cell() const
Returns the top left cell of this view.
Definition: sheet_view.hpp:215
sheet_view_type type() const
Returns the type of this view.
Definition: sheet_view.hpp:191
The selected area of a worksheet.
Definition: selection.hpp:37
Enumerates the possible types a cell can be determined by it&#39;s current value.
Definition: cell.hpp:37
void show_grid_lines(bool show)
If show is true, grid lines will be shown for sheets using this view.
Definition: sheet_view.hpp:151
sheet_view_type
Enumeration of possible types of sheet views
Definition: sheet_view.hpp:38
bool has_selections() const
Returns true if this view has any selections.
Definition: sheet_view.hpp:111
bool operator==(const sheet_view &rhs) const
Returns true if this view is equal to rhs based on its id, grid lines setting, default grid color...
Definition: sheet_view.hpp:224
An object used to refer to a cell. References have two parts, the column and the row. In Excel, the reference string A1 refers to the top-left-most cell. A cell_reference can be initialized from a string of this form or a 1-indexed ordered pair of the form column, row.
Definition: cell_reference.hpp:60
std::vector< xlnt::selection > selections() const
Returns the collection of selections as a vector.
Definition: sheet_view.hpp:135
Describes a view of a worksheet. Worksheets can have multiple views which show the data differently...
Definition: sheet_view.hpp:49
void default_grid_color(bool is_default)
If is_default is true, the default grid color will be used.
Definition: sheet_view.hpp:167
bool has_pane() const
Returns true if this view has a pane defined.
Definition: sheet_view.hpp:71
void type(sheet_view_type new_type)
Sets the type of this view.
Definition: sheet_view.hpp:183
A fixed portion of a worksheet.
Definition: pane.hpp:58
void clear_pane()
Removes the defined pane from this view.
Definition: sheet_view.hpp:95
void id(std::size_t new_id)
Sets the ID of this view to new_id.
Definition: sheet_view.hpp:55
const struct pane & pane() const
Returns a reference to this view&#39;s pane.
Definition: sheet_view.hpp:87
bool default_grid_color() const
Returns true if the default grid color will be used.
Definition: sheet_view.hpp:175