forked from GerryFerdinandus/bittorrent-tracker-editor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontrolergridtorrentdata.pas
More file actions
219 lines (181 loc) · 6.75 KB
/
controlergridtorrentdata.pas
File metadata and controls
219 lines (181 loc) · 6.75 KB
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
{ MIT licence
Copyright (c) Gerry Ferdinandus
Permission is hereby granted, free of charge, to any person obtaining a copy of this software
and associated documentation files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions
of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
}
unit controlergridtorrentdata;
{
The view string grid shows all the information of the torrent.
The grid column position order can be rearange by the user.
The updating and reading of the column position must be 'dynamic'.
Must keep track of the position of the column even when the user rearange it.
There are 10 column that must be 'track'
}
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Grids;
type
{ TControlerGridTorrentData }
TControlerGridTorrentData = class
private
//The view grid that must be controled.
FStringGridTorrentData: TStringGrid;
//The collumn must be in this design order.
FTorrentFile, //0
FInfoFileName, //1
FInfoHash, //2
FCreatedOn, //3
FCreatedBy, //4
FComment, //5
FPrivateTorrent, //6
FPieceLength, //7
FTotaSize, //8
FIndexOrder //9
: TGridColumn;
FRowIsMovedNeedUpdate: boolean;
procedure StringGridTorrentDataColRowMoved(Sender: TObject;
{%H-}IsColumn: boolean; {%H-}sIndex, {%H-}tIndex: integer);
procedure AddColumn(var GridColumn: TGridColumn; index: integer);
procedure UpdateColumnTag;
procedure WriteCell(GridColumn: TGridColumn; const Str: UTF8String);
public
//All the string that can be written to grid.
TorrentFile, //0
InfoFileName, //1
InfoHash, //2
CreatedOn, //3
CreatedBy, //4
Comment, //5
PrivateTorrent, //6
PieceLength, //7
TotaSize, //8
IndexOrder //9
: UTF8String;
procedure ClearAllImageIndex;
procedure AppendRow;
procedure ReorderGrid;
function ReadComment(Rowindex: integer): UTF8String;
constructor Create(StringGridTorrentData: TStringGrid);
destructor Destroy; override;
end;
implementation
{ TControlerGridTorrentData }
const
COLUMN_COUNT = 10;
procedure TControlerGridTorrentData.StringGridTorrentDataColRowMoved(Sender: TObject;
IsColumn: boolean; sIndex, tIndex: integer);
begin
//This is called before the column is moved 'rearrange' by the user.
FRowIsMovedNeedUpdate := True;
end;
procedure TControlerGridTorrentData.AddColumn(var GridColumn: TGridColumn;
index: integer);
begin
GridColumn := FStringGridTorrentData.Columns[index];
end;
procedure TControlerGridTorrentData.UpdateColumnTag;
var
i: integer;
begin
//fill the 'tag' value as the position of the coulumn.
//this methode must be only called when the user change the column order.
for i := 0 to FStringGridTorrentData.Columns.Count - 1 do
begin
FStringGridTorrentData.Columns[i].Tag :=
FStringGridTorrentData.Columns.IndexOf(FStringGridTorrentData.Columns[i]);
end;
//The tag is now in sync column index. It is process.
FRowIsMovedNeedUpdate := False;
end;
procedure TControlerGridTorrentData.ClearAllImageIndex;
var
i: integer;
begin
//The sort icon must be removed from the title bar
for i := 0 to FStringGridTorrentData.Columns.Count - 1 do
begin
FStringGridTorrentData.Columns[i].Title.ImageIndex := -1;
end;
end;
procedure TControlerGridTorrentData.WriteCell(GridColumn: TGridColumn;
const Str: UTF8String);
begin
FStringGridTorrentData.Cells[GridColumn.Tag,
FStringGridTorrentData.RowCount - 1] := Str;
end;
procedure TControlerGridTorrentData.AppendRow;
begin
//Add a new empty row, copy all the stings to this empty row.
//Update Column.tag if row have change.
if FRowIsMovedNeedUpdate then
UpdateColumnTag;
//Create a empty row to at the bottom.
FStringGridTorrentData.InsertColRow(False, FStringGridTorrentData.RowCount);
//write all the string to the cell.
WriteCell(FTorrentFile, TorrentFile);
WriteCell(FInfoFileName, InfoFileName);
WriteCell(FInfoHash, InfoHash);
WriteCell(FCreatedOn, CreatedOn);
WriteCell(FCreatedBy, CreatedBy);
WriteCell(FComment, Comment);
WriteCell(FPrivateTorrent, PrivateTorrent);
WriteCell(FPieceLength, PieceLength);
WriteCell(FTotaSize, TotaSize);
WriteCell(FIndexOrder, IndexOrder);
end;
procedure TControlerGridTorrentData.ReorderGrid;
begin
//Undo all posible sort column used by the user. Sort it back to 'begin state'
//FIndexOrder is a non visible row use for this purpose. TGridColumnTitle
FStringGridTorrentData.SortOrder := soAscending;
FStringGridTorrentData.SortColRow(True,
FStringGridTorrentData.Columns.IndexOf(FIndexOrder));
//The order are no longer the same. Hide the ascending/decending icon.
ClearAllImageIndex;
end;
function TControlerGridTorrentData.ReadComment(Rowindex: integer): UTF8String;
begin
//Update Column.tag if row have change.
if FRowIsMovedNeedUpdate then
UpdateColumnTag;
//Read the 'comment' grid cell.
Result := FStringGridTorrentData.Cells[FComment.Tag, Rowindex];
end;
constructor TControlerGridTorrentData.Create(StringGridTorrentData: TStringGrid);
begin
inherited Create;
FStringGridTorrentData := StringGridTorrentData;
//When user move the row, call StringGridTorrentDataColRowMoved.
FStringGridTorrentData.OnColRowMoved := @StringGridTorrentDataColRowMoved;
//The view and the controler part must have the same column count.
Assert(FStringGridTorrentData.Columns.Count = COLUMN_COUNT, 'Wrong column count');
//Track the column
AddColumn(FTorrentFile, 0);
AddColumn(FInfoFileName, 1);
AddColumn(FInfoHash, 2);
AddColumn(FCreatedOn, 3);
AddColumn(FCreatedBy, 4);
AddColumn(FComment, 5);
AddColumn(FPrivateTorrent, 6);
AddColumn(FPieceLength, 7);
AddColumn(FTotaSize, 8);
AddColumn(FIndexOrder, 9);
//Fillin the tag value
UpdateColumnTag;
end;
destructor TControlerGridTorrentData.Destroy;
begin
inherited Destroy;
end;
end.