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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
| ///////////////////////////////////////////////////////////////////////////
// //
// Copyright (c) 2015 by Charta Software B.V. //
// All rights reserved //
// //
// Version: 1.7.0.83525 //
// Web site: https://pascal.chartasoftware.com/ //
// //
// This code and information are provided "as is" without warranty of //
// any kind. Dissemination of this information or reproduction of //
// this material is strictly forbidden unless prior written permission //
// is obtained from Charta Software B.V.. //
// //
///////////////////////////////////////////////////////////////////////////
unit Table.Model;
interface
uses
Boolean,
Code.Expression,
Collection.List,
Geometry._2d.Integer32.Point,
Geometry._2d.Integer32.Rectangle,
Geometry._2d.Integer32.Size,
Integer._32,
Integer._32.Interval.List.OrderedDisjoint,
Listeners,
Object_.ReferenceCounted,
Table.Cells,
Table.EventGate,
Table.Header.Identifier,
Table.Header.Model,
Table.Row.Comparator,
Table.Row.Ordering.Column,
Table.Row.Projector,
Text,
Value,
Value.Path;
type
TTableModelQuery = class(TObject)
public
Rows: TInteger32OrderedDisjointIntervalList;
Columns: TInteger32OrderedDisjointIntervalList;
function GetSize(): TInteger32Size;
end;
TTableModelQueryResult = class(TObject)
public
Cells: TTableCells;
destructor Destroy(); override;
end;
TTableModel = class(TReferenceCountedObject)
private
FPath: TValuePath;
procedure SetPath(const Value: TValuePath);
protected
FListeners: TListeners;
FEventGate: TEventGate;
procedure TriggerCellsChangedEvent(const Cells: TInteger32Rectangle);
procedure TriggerChangedEvent();
function RetrieveThroughGetCells(Query: TTableModelQuery): TTableModelQueryResult;
function TextToValue(const Position: TInteger32Point; const Value: TText): TValue; virtual; deprecated;
public
constructor Create(); override;
destructor Destroy(); override;
function GetColumnModel(): TTableHeaderModel; virtual; abstract; // Should not change during lifetime of the TTableModel, convert to property?
function GetRowModel(): TTableHeaderModel; virtual; abstract; // Should not change during lifetime of the TTableModel, convert to property?
function GetCells(const Bounds: TInteger32Rectangle): TTableCells; virtual; abstract;
procedure SetValueForCells(const Bounds: TInteger32Rectangle; const Value: TValue); virtual; // TODO: This should be changed so we can provide multiple changed values for different cells at once
procedure ClearValuesForCells(const Bounds: TInteger32Rectangle); virtual;
function GetRows(Identifiers: TList<TTableHeaderIdentifier>): TTableCells; virtual;
function Retrieve(Query: TTableModelQuery): TTableModelQueryResult; virtual;
function SupportsRowSorting(): TBoolean; virtual;
function CreateRowSortingTableModel(): TTableModel; virtual;
function SupportsColumnSorting(): TBoolean; virtual;
procedure SortRows(Columns: TSortColumns); virtual;
function CreateRowComparator(Columns: TSortColumns): TTableRowComparator; virtual;
function CreateRowProjector(Columns: array of TInteger32): TTableRowProjector; virtual;
function SupportsRowFiltering(): TBoolean; virtual;
function CreateRowFilteringTableModel(): TTableModel; virtual;
procedure FilterRows(Expression: TExpression); virtual;
function CreateRowGroupingTableModel(Grouping: TObject): TTableModel; virtual; // TODO: Grouping should be a TTableRowGrouping instead but this would introduce a circular unit reference
procedure InsertTabDelimited(Lines: TList<TText>); virtual; deprecated;
function GetEventGate(): TEventGate; virtual;
function GetSize(): TInteger32Size;
procedure Refresh(); virtual;
procedure Save(); virtual;
function Copy(): TTableModel; virtual;
function Embellish(): TTableModel; virtual;
function EmbellishedModel(): TTableModel; virtual;
property Path: TValuePath read FPath write SetPath;
property Listeners: TListeners read FListeners;
end;
implementation
uses
Exception,
Exception.NotImplemented,
Integer._32.Interval,
Table.Model.Event,
Table.Model.RowFiltering,
Table.Model.RowGrouping,
Table.Model.RowSorting,
Table.Row.Comparator.Generic,
Table.Row.Grouping,
Table.Row.Projector.Generic,
Text.Value;
{ TTableModel }
procedure TTableModel.ClearValuesForCells(const Bounds: TInteger32Rectangle);
begin
end;
function TTableModel.Copy(): TTableModel;
begin
raise EException.Create(ClassName + '.Copy() not implemented');
end;
constructor TTableModel.Create();
begin
inherited Create();
FListeners := TListeners.Create();
FEventGate := TEventGate.Create();
Listeners.AddReference();
end;
function TTableModel.CreateRowComparator(Columns: TSortColumns): TTableRowComparator;
begin
Result := TGenericTableRowComparator.Create(Self, Columns);
end;
function TTableModel.CreateRowFilteringTableModel(): TTableModel;
var
RowFilteringTableModel: TRowFilteringTableModel;
begin
RowFilteringTableModel := TRowFilteringTableModel.Create();
RowFilteringTableModel.ModelToFilter := Self;
Result := RowFilteringTableModel;
end;
function TTableModel.CreateRowGroupingTableModel(Grouping: TObject): TTableModel;
begin
Result := TRowGroupingTableModel.Create(Grouping as TTableRowGrouping);
end;
function TTableModel.CreateRowProjector(Columns: array of TInteger32): TTableRowProjector;
begin
Result := TGenericTableRowProjector.Create(Self, Columns);
end;
function TTableModel.CreateRowSortingTableModel(): TTableModel;
var
RowSortingTableModel: TRowSortingTableModel;
begin
RowSortingTableModel := TRowSortingTableModel.Create();
RowSortingTableModel.ModelToSort := Self;
Result := RowSortingTableModel;
end;
destructor TTableModel.Destroy();
begin
Listeners.ReleaseReference();
Path.ReleaseReference();
FEventGate.Free();
inherited Destroy();
end;
function TTableModel.Embellish(): TTableModel;
begin
Result := Self;
if not Result.SupportsRowFiltering then
Result := Result.CreateRowFilteringTableModel();
if not Result.SupportsRowSorting then
Result := Result.CreateRowSortingTableModel();
end;
function TTableModel.EmbellishedModel(): TTableModel;
begin
Result := Self;
end;
procedure TTableModel.FilterRows(Expression: TExpression);
begin
end;
function TTableModel.GetSize(): TInteger32Size;
begin
Result := TInteger32Size.New(GetColumnModel().GetCount(), GetRowModel().GetCount());
end;
function TTableModel.GetEventGate(): TEventGate;
begin
Result := FEventGate;
end;
function TTableModel.GetRows(Identifiers: TList<TTableHeaderIdentifier>): TTableCells;
begin
raise ENotImplemented.Create(ClassType, 'GetRows');
end;
procedure TTableModel.InsertTabDelimited(Lines: TList<TText>);
var
Row: TInteger32;
Column: TInteger32;
Line: TText;
Cell: TText;
RowModel: TTableHeaderModel;
Value: TValue;
Position: TInteger32Point;
begin
RowModel := GetRowModel();
for Line in Lines do
begin
Row := RowModel.Insert(RowModel.GetCount());
Column := 0;
while Line <> '' do
begin
Position := TInteger32Point.New(Column, Row);
Cell := Line.Parse(#9);
Value := TextToValue(Position, Cell);
Value.AddReference();
SetValueForCells(Position, Value);
Value.ReleaseReference();
Inc(Column);
end;
end;
end;
procedure TTableModel.Refresh();
begin
TriggerChangedEvent();
end;
function TTableModel.Retrieve(Query: TTableModelQuery): TTableModelQueryResult;
begin
Result := RetrieveThroughGetCells(Query);
end;
function TTableModel.RetrieveThroughGetCells(Query: TTableModelQuery): TTableModelQueryResult;
var
Cells: TTableCells;
RowRangeIndex: TInteger32;
RowRange: TInteger32Interval;
ColumnRangeIndex: TInteger32;
ColumnRange: TInteger32Interval;
Block: TTableCells;
Target: TInteger32Point;
begin
Cells := TTableCells.Create();
Cells.Bounds := Query.GetSize();
Target := TInteger32Point.New(0, 0);
for RowRangeIndex := 0 to Query.Rows.Count - 1 do
begin
RowRange := Query.Rows[RowRangeIndex];
Target.X := 0;
for ColumnRangeIndex := 0 to Query.Columns.Count - 1 do
begin
ColumnRange := Query.Columns[ColumnRangeIndex];
Block := GetCells(TInteger32Rectangle.New(ColumnRange, RowRange));
Cells.MoveValuesFrom(Target, Block, TInteger32Point.New(0, 0));
Block.Free();
Inc(Target.X, ColumnRange.Size);
end;
Inc(Target.Y, RowRange.Size);
end;
Result := TTableModelQueryResult.Create();
Result.Cells := Cells;
end;
procedure TTableModel.Save();
begin
end;
procedure TTableModel.SetPath(const Value: TValuePath);
begin
if Value <> Path then
begin
Value.AddReference();
Path.ReleaseReference();
FPath := Value;
end;
end;
procedure TTableModel.SetValueForCells(const Bounds: TInteger32Rectangle; const Value: TValue);
begin
end;
procedure TTableModel.SortRows(Columns: TSortColumns);
begin
end;
function TTableModel.SupportsColumnSorting(): TBoolean;
begin
Result := False;
end;
function TTableModel.SupportsRowFiltering(): TBoolean;
begin
Result := False;
end;
function TTableModel.SupportsRowSorting(): TBoolean;
begin
Result := False;
end;
function TTableModel.TextToValue(const Position: TInteger32Point; const Value: TText): TValue;
begin
Result := TTextValue.Create(Value);
end;
procedure TTableModel.TriggerCellsChangedEvent(const Cells: TInteger32Rectangle);
begin
if GetEventGate().Enabled then
Listeners.DistributeEvent(TTableModelCellsChangedEvent.Create(Self, Cells));
end;
procedure TTableModel.TriggerChangedEvent();
begin
if GetEventGate().Enabled then
Listeners.DistributeEvent(TTableModelChangedEvent.Create(Self));
end;
{ TTableModelQueryResult }
destructor TTableModelQueryResult.Destroy();
begin
Cells.Free();
inherited Destroy();
end;
{ TTableModelQuery }
function TTableModelQuery.GetSize(): TInteger32Size;
begin
Result := TInteger32Size.New(Columns.GetTotalSize(), Rows.GetTotalSize());
end;
end.
|