| 1 |
/*************************************************************************** |
| 2 |
* Copyright (c) 2015 Eivind Kvedalen <[email protected]> * |
| 3 |
* * |
| 4 |
* This file is part of the FreeCAD CAx development system. * |
| 5 |
* * |
| 6 |
* This library is free software; you can redistribute it and/or * |
| 7 |
* modify it under the terms of the GNU Library General Public * |
| 8 |
* License as published by the Free Software Foundation; either * |
| 9 |
* version 2 of the License, or (at your option) any later version. * |
| 10 |
* * |
| 11 |
* This library is distributed in the hope that it will be useful, * |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 14 |
* GNU Library General Public License for more details. * |
| 15 |
* * |
| 16 |
* You should have received a copy of the GNU Library General Public * |
| 17 |
* License along with this library; see the file COPYING.LIB. If not, * |
| 18 |
* write to the Free Software Foundation, Inc., 59 Temple Place, * |
| 19 |
* Suite 330, Boston, MA 02111-1307, USA * |
| 20 |
* * |
| 21 |
***************************************************************************/ |
| 22 |
|
| 23 |
#ifndef RANGE_H |
| 24 |
#define RANGE_H |
| 25 |
|
| 26 |
#include <string> |
| 27 |
#include <Base/Bitmask.h> |
| 28 |
#ifndef FC_GLOBAL_H |
| 29 |
#include <FCGlobal.h> |
| 30 |
#endif |
| 31 |
|
| 32 |
namespace App |
| 33 |
{ |
| 34 |
|
| 35 |
struct CellAddress; |
| 36 |
|
| 37 |
AppExport CellAddress stringToAddress(const char* strAddress, bool silent = false); |
| 38 |
AppExport int decodeColumn(const std::string& colstr, bool silent = false); |
| 39 |
AppExport int decodeRow(const std::string& rowstr, bool silent = false); |
| 40 |
AppExport bool validColumn(const std::string& colstr); |
| 41 |
AppExport int validRow(const std::string& rowstr); |
| 42 |
|
| 43 |
struct AppExport CellAddress |
| 44 |
{ |
| 45 |
// See call of ENABLE_BITMASK_OPERATORS |
| 46 |
enum class Cell |
| 47 |
{ |
| 48 |
Absolute = 1, |
| 49 |
ShowRow = 2, |
| 50 |
ShowColumn = 4, |
| 51 |
ShowRowColumn = ShowRow | ShowColumn, |
| 52 |
ShowFull = Absolute | ShowRow | ShowColumn |
| 53 |
}; |
| 54 |
|
| 55 |
explicit CellAddress(int row = -1, int col = -1, bool absRow = false, bool absCol = false) |
| 56 |
: _row(row) |
| 57 |
, _col(col) |
| 58 |
, _absRow(absRow) |
| 59 |
, _absCol(absCol) |
| 60 |
{} |
| 61 |
|
| 62 |
explicit CellAddress(const char* address) |
| 63 |
{ |
| 64 |
*this = stringToAddress(address); |
| 65 |
} |
| 66 |
|
| 67 |
explicit CellAddress(const std::string& address) |
| 68 |
{ |
| 69 |
*this = stringToAddress(address.c_str()); |
| 70 |
} |
| 71 |
|
| 72 |
bool parseAbsoluteAddress(const char* txt); |
| 73 |
|
| 74 |
inline int row() const |
| 75 |
{ |
| 76 |
return _row; |
| 77 |
} |
| 78 |
|
| 79 |
inline int col() const |
| 80 |
{ |
| 81 |
return _col; |
| 82 |
} |
| 83 |
|
| 84 |
void setRow(int r, bool clip = false) |
| 85 |
{ |
| 86 |
_row = (clip && r >= MAX_ROWS) ? MAX_ROWS - 1 : r; |
| 87 |
} |
| 88 |
|
| 89 |
void setCol(int c, bool clip = false) |
| 90 |
{ |
| 91 |
_col = (clip && c >= MAX_COLUMNS) ? MAX_COLUMNS - 1 : c; |
| 92 |
} |
| 93 |
|
| 94 |
inline bool operator<(const CellAddress& other) const |
| 95 |
{ |
| 96 |
return asInt() < other.asInt(); |
| 97 |
} |
| 98 |
|
| 99 |
inline bool operator>(const CellAddress& other) const |
| 100 |
{ |
| 101 |
return asInt() > other.asInt(); |
| 102 |
} |
| 103 |
|
| 104 |
inline bool operator==(const CellAddress& other) const |
| 105 |
{ |
| 106 |
return asInt() == other.asInt(); |
| 107 |
} |
| 108 |
|
| 109 |
inline bool operator!=(const CellAddress& other) const |
| 110 |
{ |
| 111 |
return asInt() != other.asInt(); |
| 112 |
} |
| 113 |
|
| 114 |
inline bool isValid() |
| 115 |
{ |
| 116 |
return (row() >= 0 && row() < MAX_ROWS && col() >= 0 && col() < MAX_COLUMNS); |
| 117 |
} |
| 118 |
|
| 119 |
inline bool isAbsoluteRow() const |
| 120 |
{ |
| 121 |
return _absRow; |
| 122 |
} |
| 123 |
|
| 124 |
inline bool isAbsoluteCol() const |
| 125 |
{ |
| 126 |
return _absCol; |
| 127 |
} |
| 128 |
|
| 129 |
std::string toString(Cell = Cell::ShowFull) const; |
| 130 |
|
| 131 |
// Static members |
| 132 |
|
| 133 |
static const int MAX_ROWS; |
| 134 |
|
| 135 |
static const int MAX_COLUMNS; |
| 136 |
|
| 137 |
protected: |
| 138 |
inline unsigned int asInt() const |
| 139 |
{ |
| 140 |
return ((_row << 16) | _col); |
| 141 |
} |
| 142 |
|
| 143 |
short _row; |
| 144 |
short _col; |
| 145 |
bool _absRow; |
| 146 |
bool _absCol; |
| 147 |
}; |
| 148 |
|
| 149 |
/** |
| 150 |
* @brief The Range class is a spreadsheet range iterator. It takes |
| 151 |
* a starting (row, col) and an ending (row, col). Notice that ranges |
| 152 |
* are always at least one element. The next() functions is therefore |
| 153 |
* used e.g as follows: |
| 154 |
* |
| 155 |
* do { |
| 156 |
* ... |
| 157 |
* while (range.next()); |
| 158 |
* |
| 159 |
*/ |
| 160 |
|
| 161 |
class AppExport Range |
| 162 |
{ |
| 163 |
public: |
| 164 |
explicit Range(const char* range, bool normalize = false); |
| 165 |
|
| 166 |
Range(int _row_begin, int _col_begin, int _row_end, int _col_end, bool normalize = false); |
| 167 |
|
| 168 |
Range(const CellAddress& from, const CellAddress& to, bool normalize = false); |
| 169 |
|
| 170 |
bool next(); |
| 171 |
|
| 172 |
/** Make sure the range starts from top left and ends with bottom right corner **/ |
| 173 |
void normalize(); |
| 174 |
|
| 175 |
/** Current row */ |
| 176 |
inline int row() const |
| 177 |
{ |
| 178 |
return row_curr; |
| 179 |
} |
| 180 |
|
| 181 |
/** Current column */ |
| 182 |
inline int column() const |
| 183 |
{ |
| 184 |
return col_curr; |
| 185 |
} |
| 186 |
|
| 187 |
/** Row count */ |
| 188 |
inline int rowCount() const |
| 189 |
{ |
| 190 |
return row_end - row_begin + 1; |
| 191 |
} |
| 192 |
|
| 193 |
/** Column count */ |
| 194 |
inline int colCount() const |
| 195 |
{ |
| 196 |
return col_end - col_begin + 1; |
| 197 |
} |
| 198 |
|
| 199 |
/** Position of start of range */ |
| 200 |
inline CellAddress from() const |
| 201 |
{ |
| 202 |
return CellAddress(row_begin, col_begin); |
| 203 |
} |
| 204 |
|
| 205 |
/** Position of end of range */ |
| 206 |
inline CellAddress to() const |
| 207 |
{ |
| 208 |
return CellAddress(row_end, col_end); |
| 209 |
} |
| 210 |
|
| 211 |
/** Start of range as a string */ |
| 212 |
inline std::string fromCellString() const |
| 213 |
{ |
| 214 |
return CellAddress(row_begin, col_begin).toString(); |
| 215 |
} |
| 216 |
|
| 217 |
/** End of range as a string */ |
| 218 |
inline std::string toCellString() const |
| 219 |
{ |
| 220 |
return CellAddress(row_end, col_end).toString(); |
| 221 |
} |
| 222 |
|
| 223 |
/** Current cell as a string */ |
| 224 |
inline std::string address() const |
| 225 |
{ |
| 226 |
return CellAddress(row_curr, col_curr).toString(); |
| 227 |
} |
| 228 |
|
| 229 |
/** The raneg as a string */ |
| 230 |
inline std::string rangeString() const |
| 231 |
{ |
| 232 |
return CellAddress(row_begin, col_begin).toString() + ":" |
| 233 |
+ CellAddress(row_end, col_end).toString(); |
| 234 |
} |
| 235 |
|
| 236 |
CellAddress operator*() const |
| 237 |
{ |
| 238 |
return CellAddress(row_curr, col_curr); |
| 239 |
} |
| 240 |
|
| 241 |
inline bool operator<(const Range& other) const |
| 242 |
{ |
| 243 |
if (from() < other.from()) { |
| 244 |
return true; |
| 245 |
} |
| 246 |
if (from() > other.from()) { |
| 247 |
return false; |
| 248 |
} |
| 249 |
return to() < other.to(); |
| 250 |
} |
| 251 |
|
| 252 |
/** Number of elements in range */ |
| 253 |
inline int size() const |
| 254 |
{ |
| 255 |
return (row_end - row_begin + 1) * (col_end - col_begin + 1); |
| 256 |
} |
| 257 |
|
| 258 |
private: |
| 259 |
int row_curr, col_curr; |
| 260 |
int row_begin, col_begin; |
| 261 |
int row_end, col_end; |
| 262 |
}; |
| 263 |
|
| 264 |
} // namespace App |
| 265 |
|
| 266 |
ENABLE_BITMASK_OPERATORS(App::CellAddress::Cell) |
| 267 |
|
| 268 |
#endif // RANGE_H |