File size: 10,064 Bytes
985c397
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
// SPDX-License-Identifier: LGPL-2.1-or-later

/***************************************************************************

 *   Copyright (c) 2015 Eivind Kvedalen <eivind@kvedalen.name>             *

 *                                                                         *

 *   This file is part of the FreeCAD CAx development system.              *

 *                                                                         *

 *   This library is free software; you can redistribute it and/or         *

 *   modify it under the terms of the GNU Library General Public           *

 *   License as published by the Free Software Foundation; either          *

 *   version 2 of the License, or (at your option) any later version.      *

 *                                                                         *

 *   This library  is distributed in the hope that it will be useful,      *

 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *

 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *

 *   GNU Library General Public License for more details.                  *

 *                                                                         *

 *   You should have received a copy of the GNU Library General Public     *

 *   License along with this library; see the file COPYING.LIB. If not,    *

 *   write to the Free Software Foundation, Inc., 59 Temple Place,         *

 *   Suite 330, Boston, MA  02111-1307, USA                                *

 *                                                                         *

 ***************************************************************************/

#ifndef SRC_MOD_SPREADSHEET_APP_SHEET_H_
#define SRC_MOD_SPREADSHEET_APP_SHEET_H_

#ifdef signals
# undef signals
# define signals signals
#endif

#include <map>
#include <tuple>
#include <set>
#include <string>
#include <vector>

#include <App/DocumentObject.h>
#include <App/DynamicProperty.h>
#include <App/FeaturePython.h>
#include <App/PropertyUnits.h>
#include <App/Range.h>
#include <Base/Unit.h>

#include <Mod/Spreadsheet/SpreadsheetGlobal.h>

#include "PropertyColumnWidths.h"
#include "PropertyRowHeights.h"
#include "PropertySheet.h"


namespace Spreadsheet
{

class Sheet;
class Cell;
class SheetObserver;

/** Spreadsheet quantity property

 * This is a property for quantities, and unlike its ancestor implements

 * Copy() and Paste() methods. It is used by the spreadsheet to

 * create aliases in a generic way.

 */
class SpreadsheetExport PropertySpreadsheetQuantity: public App::PropertyQuantity
{
    TYPESYSTEM_HEADER_WITH_OVERRIDE();

public:
    PropertySpreadsheetQuantity() = default;
    ~PropertySpreadsheetQuantity() override = default;

    Property* Copy() const override;
    void Paste(const Property& from) override;
};

class SpreadsheetExport Sheet: public App::DocumentObject
{
    PROPERTY_HEADER_WITH_OVERRIDE(Spreadsheet::Sheet);

public:
    /// Constructor
    Sheet();
    ~Sheet() override;

    /// returns the type name of the ViewProvider
    const char* getViewProviderName() const override
    {
        return "SpreadsheetGui::ViewProviderSheet";
    }

    bool importFromFile(

        const std::string& filename,

        char delimiter = '\t',

        char quoteChar = '\0',

        char escapeChar = '\\'

    );

    bool getCharsFromPrefs(char& delimiter, char& quote, char& escape, std::string& errMsg);

    bool exportToFile(

        const std::string& filename,

        char delimiter = '\t',

        char quoteChar = '\0',

        char escapeChar = '\\'

    ) const;

    bool mergeCells(const App::Range& range);

    void splitCell(App::CellAddress address);

    Cell* getCell(App::CellAddress address);
    const Cell* getCell(App::CellAddress address) const;

    Cell* getNewCell(App::CellAddress address);

    enum Border

    {
        BorderTop = 1,
        BorderLeft = 2,
        BorderBottom = 4,
        BorderRight = 8,
        BorderAll = 15,
    };
    unsigned getCellBindingBorder(App::CellAddress address) const;

    PropertySheet::BindingType getCellBinding(

        App::Range& range,

        App::ExpressionPtr* pStart = nullptr,

        App::ExpressionPtr* pEnd = nullptr,

        App::ObjectIdentifier* pTarget = nullptr

    ) const;

    void setCell(const char* address, const char* value);

    void setCell(App::CellAddress address, const char* value);

    void clearAll();

    void clear(App::CellAddress address, bool all = true);

    void getSpans(App::CellAddress address, int& rows, int& cols) const;

    bool isMergedCell(App::CellAddress address) const;

    App::CellAddress getAnchor(App::CellAddress address) const;

    void setColumnWidth(int col, int width);

    int getColumnWidth(int col) const;

    void setRowHeight(int row, int height);

    int getRowHeight(int row) const;

    std::vector<std::string> getUsedCells() const;

    void insertColumns(int col, int count);

    void removeColumns(int col, int count);

    void insertRows(int row, int count);

    void removeRows(int row, int count);

    void setContent(App::CellAddress address, const char* value);

    void setAlignment(App::CellAddress address, int alignment);

    void setStyle(App::CellAddress address, const std::set<std::string>& style);

    void setForeground(App::CellAddress address, const Base::Color& color);

    void setBackground(App::CellAddress address, const Base::Color& color);

    void setDisplayUnit(App::CellAddress address, const std::string& unit);

    void setComputedUnit(App::CellAddress address, const Base::Unit& unit);

    void setAlias(App::CellAddress address, const std::string& alias);

    std::string getAddressFromAlias(const std::string& alias) const;

    bool isValidAlias(const std::string& candidate);

    void setSpans(App::CellAddress address, int rows, int columns);

    std::set<std::string> dependsOn(App::CellAddress address) const;

    void providesTo(App::CellAddress address, std::set<std::string>& result) const;

    bool hasCell(const std::vector<App::Range>& ranges) const;
    PyObject* getPyObject() override;

    PropertySheet* getCells()
    {
        return &cells;
    }

    App::Property* getPropertyByName(const char* name) const override;

    App::Property* getDynamicPropertyByName(const char* name) const override;

    void getPropertyNamedList(std::vector<std::pair<const char*, App::Property*>>& List) const override;

    /// See PropertyContainer::visitProperties for semantics
    void visitProperties(const std::function<void(App::Property*)>& visitor) const override;

    short mustExecute() const override;

    App::DocumentObjectExecReturn* execute() override;

    bool getCellAddress(const App::Property* prop, App::CellAddress& address);

    App::CellAddress getCellAddress(const char* name, bool silent = false) const;

    App::Range getRange(const char* name, bool silent = false) const;

    std::tuple<App::CellAddress, App::CellAddress> getUsedRange() const;

    std::map<int, int> getColumnWidths() const;

    std::map<int, int> getRowHeights() const;

    std::string getRow(int offset = 0) const;

    std::string getColumn(int offset = 0) const;

    void touchCells(App::Range range);

    void recomputeCells(App::Range range);

    // Signals

    fastsignals::signal<void(App::CellAddress)> cellUpdated;

    fastsignals::signal<void(App::Range)> rangeUpdated;

    fastsignals::signal<void(App::CellAddress)> cellSpanChanged;

    fastsignals::signal<void(int, int)> columnWidthChanged;

    fastsignals::signal<void(int, int)> rowHeightChanged;

    void renameObjectIdentifiers(

        const std::map<App::ObjectIdentifier, App::ObjectIdentifier>& paths

    ) override;

    void setCopyOrCutRanges(const std::vector<App::Range>& ranges, bool copy = true);
    const std::vector<App::Range>& getCopyOrCutRange(bool copy = true) const;
    unsigned getCopyOrCutBorder(App::CellAddress address, bool copy = true) const;

protected:
    void onChanged(const App::Property* prop) override;

    void updateColumnsOrRows(bool horizontal, int section, int count);

    std::set<App::CellAddress> providesTo(App::CellAddress address) const;

    void onDocumentRestored() override;

    void recomputeCell(App::CellAddress p);

    App::Property* getProperty(App::CellAddress key) const;

    App::Property* getProperty(const char* addr) const;

    void updateProperty(App::CellAddress key);

    App::Property* setStringProperty(App::CellAddress key, const std::string& value);

    App::Property* setObjectProperty(App::CellAddress key, Py::Object obj);

    App::Property* setFloatProperty(App::CellAddress key, double value);

    App::Property* setIntegerProperty(App::CellAddress key, long value);

    App::Property* setQuantityProperty(App::CellAddress key, double value, const Base::Unit& unit);

    void onSettingDocument() override;

    void updateBindings();

    /* Properties for used cells */
    App::DynamicProperty& props;

    /* Mapping of properties to cell position */
    std::map<const App::Property*, App::CellAddress> propAddress;

    /* Set of cells with errors */
    std::set<App::CellAddress> cellErrors;

    /* Properties */

    /* Cell data */
    PropertySheet cells;

    /* Column widths */
    PropertyColumnWidths columnWidths;

    /* Row heights */
    PropertyRowHeights rowHeights;

    int currentRow = -1;
    int currentCol = -1;

    std::vector<App::Range> boundRanges;

    std::vector<App::Range> copyCutRanges;
    bool hasCopyRange = false;

    friend class SheetObserver;

    friend class PropertySheet;
};

using SheetPython = App::FeaturePythonT<Sheet>;

}  // namespace Spreadsheet


#endif  // SRC_MOD_SPREADSHEET_APP_SHEET_H_