|
| 1 | +unit controlergridtorrentdata; |
| 2 | + |
| 3 | +{ |
| 4 | +The view string grid shows all the information of the torrent. |
| 5 | +The grid column position order can be rearange by the user. |
| 6 | +The updating and reading of the column position must be 'dynamic'. |
| 7 | +Must keep track of the position of the column even when the user rearange it. |
| 8 | +
|
| 9 | +There are 10 column that must be 'track' |
| 10 | +} |
| 11 | +{$mode objfpc}{$H+} |
| 12 | + |
| 13 | +interface |
| 14 | + |
| 15 | +uses |
| 16 | + Classes, SysUtils, Grids; |
| 17 | + |
| 18 | +type |
| 19 | + |
| 20 | + { TControlerGridTorrentData } |
| 21 | + |
| 22 | + TControlerGridTorrentData = class |
| 23 | + private |
| 24 | + //The view grid that must be controled. |
| 25 | + FStringGridTorrentData: TStringGrid; |
| 26 | + |
| 27 | + //The collumn must be in this design order. |
| 28 | + FTorrentFile, //0 |
| 29 | + FInfoFileName, //1 |
| 30 | + FInfoHash, //2 |
| 31 | + FCreatedOn, //3 |
| 32 | + FCreatedBy, //4 |
| 33 | + FComment, //5 |
| 34 | + FPrivateTorrent, //6 |
| 35 | + FPieceLength, //7 |
| 36 | + FTotaSize, //8 |
| 37 | + FIndexOrder //9 |
| 38 | + : TGridColumn; |
| 39 | + |
| 40 | + FRowIsMovedNeedUpdate: boolean; |
| 41 | + procedure StringGridTorrentDataColRowMoved(Sender: TObject; |
| 42 | + IsColumn: boolean; sIndex, tIndex: integer); |
| 43 | + procedure AddColumn(var GridColumn: TGridColumn; index: integer); |
| 44 | + procedure UpdateColumnTag; |
| 45 | + procedure ClearAllImageIndex; |
| 46 | + procedure WriteCell(GridColumn: TGridColumn; const Str: UTF8String); |
| 47 | + public |
| 48 | + //All the string that can be written to grid. |
| 49 | + TorrentFile, //0 |
| 50 | + InfoFileName, //1 |
| 51 | + InfoHash, //2 |
| 52 | + CreatedOn, //3 |
| 53 | + CreatedBy, //4 |
| 54 | + Comment, //5 |
| 55 | + PrivateTorrent, //6 |
| 56 | + PieceLength, //7 |
| 57 | + TotaSize, //8 |
| 58 | + IndexOrder //9 |
| 59 | + : UTF8String; |
| 60 | + |
| 61 | + procedure AppendRow; |
| 62 | + procedure ReorderGrid; |
| 63 | + function ReadComment(Rowindex: integer): UTF8String; |
| 64 | + constructor Create(StringGridTorrentData: TStringGrid); |
| 65 | + destructor Destroy; override; |
| 66 | + end; |
| 67 | + |
| 68 | +implementation |
| 69 | + |
| 70 | +{ TControlerGridTorrentData } |
| 71 | +const |
| 72 | + COLUMN_COUNT = 10; |
| 73 | + |
| 74 | +procedure TControlerGridTorrentData.StringGridTorrentDataColRowMoved(Sender: TObject; |
| 75 | + IsColumn: boolean; sIndex, tIndex: integer); |
| 76 | +begin |
| 77 | + //This is called before the column is moved 'rearrange' by the user. |
| 78 | + FRowIsMovedNeedUpdate := True; |
| 79 | +end; |
| 80 | + |
| 81 | +procedure TControlerGridTorrentData.AddColumn(var GridColumn: TGridColumn; |
| 82 | + index: integer); |
| 83 | +begin |
| 84 | + GridColumn := FStringGridTorrentData.Columns[index]; |
| 85 | +end; |
| 86 | + |
| 87 | +procedure TControlerGridTorrentData.UpdateColumnTag; |
| 88 | +var |
| 89 | + i: integer; |
| 90 | +begin |
| 91 | + //fill the 'tag' value as the position of the coulumn. |
| 92 | + //this methode must be only called when the user change the column order. |
| 93 | + for i := 0 to FStringGridTorrentData.ColCount - 1 do |
| 94 | + begin |
| 95 | + FStringGridTorrentData.Columns[i].Tag := |
| 96 | + FStringGridTorrentData.Columns.IndexOf(FStringGridTorrentData.Columns[i]); |
| 97 | + end; |
| 98 | + |
| 99 | + //The tag is now in sync column index. It is process. |
| 100 | + FRowIsMovedNeedUpdate := False; |
| 101 | +end; |
| 102 | + |
| 103 | +procedure TControlerGridTorrentData.ClearAllImageIndex; |
| 104 | +var |
| 105 | + i: integer; |
| 106 | +begin |
| 107 | + //The sort icon must be removed from the title bar |
| 108 | + for i := 0 to FStringGridTorrentData.ColCount - 1 do |
| 109 | + begin |
| 110 | + FStringGridTorrentData.Columns[i].Title.ImageIndex := -1; |
| 111 | + end; |
| 112 | +end; |
| 113 | + |
| 114 | +procedure TControlerGridTorrentData.WriteCell(GridColumn: TGridColumn; |
| 115 | + const Str: UTF8String); |
| 116 | +begin |
| 117 | + FStringGridTorrentData.Cells[GridColumn.Tag, |
| 118 | + FStringGridTorrentData.RowCount - 1] := Str; |
| 119 | +end; |
| 120 | + |
| 121 | +procedure TControlerGridTorrentData.AppendRow; |
| 122 | +begin |
| 123 | + //Add a new empty row, copy all the stings to this empty row. |
| 124 | + |
| 125 | + //Update Column.tag if row have change. |
| 126 | + if FRowIsMovedNeedUpdate then |
| 127 | + UpdateColumnTag; |
| 128 | + |
| 129 | + //Create a empty row to at the bottom. |
| 130 | + FStringGridTorrentData.InsertColRow(False, FStringGridTorrentData.RowCount); |
| 131 | + |
| 132 | + //write all the string to the cell. |
| 133 | + WriteCell(FTorrentFile, TorrentFile); |
| 134 | + WriteCell(FInfoFileName, InfoFileName); |
| 135 | + WriteCell(FInfoHash, InfoHash); |
| 136 | + WriteCell(FCreatedOn, CreatedOn); |
| 137 | + WriteCell(FCreatedBy, CreatedBy); |
| 138 | + WriteCell(FComment, Comment); |
| 139 | + WriteCell(FPrivateTorrent, PrivateTorrent); |
| 140 | + WriteCell(FPieceLength, PieceLength); |
| 141 | + WriteCell(FTotaSize, TotaSize); |
| 142 | + WriteCell(FIndexOrder, IndexOrder); |
| 143 | + |
| 144 | +end; |
| 145 | + |
| 146 | +procedure TControlerGridTorrentData.ReorderGrid; |
| 147 | +begin |
| 148 | + //Undo all posible sort column used by the user. Sort it back to 'begin state' |
| 149 | + //FIndexOrder is a non visible row use for this purpose. TGridColumnTitle |
| 150 | + FStringGridTorrentData.SortOrder := soAscending; |
| 151 | + FStringGridTorrentData.SortColRow(True, |
| 152 | + FStringGridTorrentData.Columns.IndexOf(FIndexOrder)); |
| 153 | + |
| 154 | + //The order are no longer the same. Hide the ascending/decending icon. |
| 155 | + ClearAllImageIndex; |
| 156 | +end; |
| 157 | + |
| 158 | +function TControlerGridTorrentData.ReadComment(Rowindex: integer): UTF8String; |
| 159 | +begin |
| 160 | + //Update Column.tag if row have change. |
| 161 | + if FRowIsMovedNeedUpdate then |
| 162 | + UpdateColumnTag; |
| 163 | + |
| 164 | + //Read the 'comment' grid cell. |
| 165 | + Result := FStringGridTorrentData.Cells[FComment.Tag, Rowindex]; |
| 166 | +end; |
| 167 | + |
| 168 | +constructor TControlerGridTorrentData.Create(StringGridTorrentData: TStringGrid); |
| 169 | +begin |
| 170 | + inherited Create; |
| 171 | + FStringGridTorrentData := StringGridTorrentData; |
| 172 | + |
| 173 | + //When user move the row, call StringGridTorrentDataColRowMoved. |
| 174 | + FStringGridTorrentData.OnColRowMoved := @StringGridTorrentDataColRowMoved; |
| 175 | + |
| 176 | + //The view and the controler part must have the same column count. |
| 177 | + Assert(FStringGridTorrentData.ColCount <> COLUMN_COUNT, 'Wrong column count'); |
| 178 | + |
| 179 | + //Track the column |
| 180 | + AddColumn(FTorrentFile, 0); |
| 181 | + AddColumn(FInfoFileName, 1); |
| 182 | + AddColumn(FInfoHash, 2); |
| 183 | + AddColumn(FCreatedOn, 3); |
| 184 | + AddColumn(FCreatedBy, 4); |
| 185 | + AddColumn(FComment, 5); |
| 186 | + AddColumn(FPrivateTorrent, 6); |
| 187 | + AddColumn(FPieceLength, 7); |
| 188 | + AddColumn(FTotaSize, 8); |
| 189 | + AddColumn(FIndexOrder, 9); |
| 190 | + |
| 191 | + //Fillin the tag value |
| 192 | + UpdateColumnTag; |
| 193 | +end; |
| 194 | + |
| 195 | +destructor TControlerGridTorrentData.Destroy; |
| 196 | +begin |
| 197 | + inherited Destroy; |
| 198 | +end; |
| 199 | + |
| 200 | +end. |
0 commit comments