Branch data TLA Line data Source code
1 : : // Copyright (c) 2014-2022 Thomas Fussell
2 : : // Copyright (c) 2024-2025 xlnt-community
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, ARISING 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 <vector>
28 : :
29 : : #include <xlnt/xlnt_config.hpp>
30 : : #include <xlnt/cell/cell_reference.hpp>
31 : : #include <xlnt/worksheet/pane.hpp>
32 : : #include <xlnt/worksheet/range_reference.hpp>
33 : : #include <xlnt/utils/exceptions.hpp>
34 : : #include <xlnt/utils/optional.hpp>
35 : :
36 : : namespace xlnt {
37 : :
38 : : /// <summary>
39 : : /// The selected area of a worksheet.
40 : : /// </summary>
41 : : class XLNT_API selection
42 : : {
43 : : public:
44 : : /// <summary>
45 : : /// default ctor
46 : : /// </summary>
47 :CBC 75 : explicit selection() = default;
48 : :
49 : : /// <summary>
50 : : /// ctor when no range selected
51 : : /// sqref == active_cell
52 : : /// </summary>
53 : 15 : explicit selection(pane_corner quadrant, cell_reference active_cell)
54 [ + + ]: 30 : : active_cell_(active_cell), sqref_({range_reference(active_cell, active_cell)}), pane_(quadrant)
55 : : {
56 : 15 : }
57 : :
58 : : /// <summary>
59 : : /// ctor with selected range
60 : : /// sqref must contain active_cell
61 : : /// </summary>
62 : : explicit selection(pane_corner quadrant, cell_reference active_cell, range_reference selected)
63 : : : active_cell_(active_cell), sqref_({selected}), pane_(quadrant)
64 : : {
65 : : }
66 : :
67 : : /// <summary>
68 : : /// Returns true if this selection has a defined active cell.
69 : : /// </summary>
70 : 29 : bool has_active_cell() const
71 : : {
72 : 29 : return active_cell_.is_set();
73 : : }
74 : :
75 : : /// <summary>
76 : : /// Returns the cell reference of the active cell.
77 : : /// </summary>
78 : 32 : cell_reference active_cell() const
79 : : {
80 : 32 : return active_cell_.get();
81 : : }
82 : :
83 : : /// <summary>
84 : : /// Sets the active cell to that pointed to by ref.
85 : : /// </summary>
86 : 70 : void active_cell(const cell_reference &ref)
87 : : {
88 : 70 : active_cell_ = ref;
89 : 70 : }
90 : :
91 : : /// <summary>
92 : : /// Returns true if this selection has a defined sqref.
93 : : /// </summary>
94 : 36 : bool has_sqref() const
95 : : {
96 : 36 : return !sqref_.empty();
97 : : }
98 : :
99 : : /// <summary>
100 : : /// Returns the range encompassed by this selection.
101 : : /// If the range contains multiple (non-contiguous) regions, the first range is returned.
102 : : /// Use sqrefs to obtain the full selection.
103 : : /// </summary>
104 : : /// <deprecated>
105 : : /// Use sqrefs instead.
106 : : /// </deprecated>
107 : 7 : XLNT_DEPRECATED range_reference sqref() const
108 : : {
109 [ - + ]: 7 : if (!has_sqref())
110 [ # ]:UBC 0 : throw invalid_attribute();
111 : :
112 :CBC 7 : return sqref_.front();
113 : : }
114 : :
115 : : /// <summary>
116 : : /// Returns the range encompassed by this selection.
117 : : /// </summary>
118 : 38 : const std::vector<range_reference>& sqrefs() const
119 : : {
120 : 38 : return sqref_;
121 : : }
122 : :
123 : : /// <summary>
124 : : /// Sets the range encompassed by this selection.
125 : : /// </summary>
126 : : void sqref(const range_reference &ref)
127 : : {
128 : : sqref_ = {ref};
129 : : }
130 : :
131 : : /// <summary>
132 : : /// Sets the range encompassed by this selection.
133 : : /// </summary>
134 : : void sqref(const std::vector<range_reference> &ref)
135 : : {
136 : : sqref_ = ref;
137 : : }
138 : :
139 : : /// <summary>
140 : : /// Sets the range encompassed by this selection.
141 : : /// The provided range should be a space delimited list of range references.
142 : : /// E.g. "A1 B2:C3"
143 : : /// </summary>
144 : : void sqref(const std::string &ref);
145 : :
146 : : /// <summary>
147 : : /// Returns the sheet quadrant of this selection.
148 : : /// </summary>
149 : 36 : pane_corner pane() const
150 : : {
151 : 36 : return pane_;
152 : : }
153 : :
154 : : /// <summary>
155 : : /// Sets the sheet quadrant of this selection to corner.
156 : : /// </summary>
157 : 21 : void pane(pane_corner corner)
158 : : {
159 : 21 : pane_ = corner;
160 : 21 : }
161 : :
162 : : /// <summary>
163 : : /// Returns true if this selection is equal to rhs based on its active cell,
164 : : /// sqref, and pane.
165 : : /// </summary>
166 : 6 : bool operator==(const selection &rhs) const
167 : : {
168 : 6 : return active_cell_ == rhs.active_cell_
169 [ + - ]: 6 : && sqref_ == rhs.sqref_
170 [ + - + - ]: 12 : && pane_ == rhs.pane_;
171 : : }
172 : :
173 : : // <summary>
174 : : /// Returns the negation of the equality operator.
175 : : /// </summary>
176 : : bool operator!=(const selection &rhs) const
177 : : {
178 : : return !(*this == rhs);
179 : : }
180 : :
181 : : private:
182 : : /// <summary>
183 : : /// The last selected cell in the selection
184 : : /// </summary>
185 : : optional<cell_reference> active_cell_;
186 : :
187 : : /// <summary>
188 : : /// The last selected block in the selection
189 : : /// contains active_cell_, normally == to active_cell_
190 : : /// </summary>
191 : : std::vector<range_reference> sqref_;
192 : :
193 : : /// <summary>
194 : : /// The corner of the worksheet that this selection extends to
195 : : /// </summary>
196 : : pane_corner pane_ = pane_corner::top_left;
197 : : };
198 : :
199 : : } // namespace xlnt
|