Changeset 285 for trunk/UItemList.pas
- Timestamp:
- Mar 10, 2019, 11:57:03 PM (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/UItemList.pas
r284 r285 6 6 7 7 uses 8 Classes, SysUtils, fgl, DOM, UXMLUtils, UCommon ;8 Classes, SysUtils, fgl, DOM, UXMLUtils, UCommon, Graphics; 9 9 10 10 type 11 TDataType = (dtNone, dtString, dtBoolean, dtInteger, dtFloat, dtColor, 12 dtTime, dtDate, dtDateTime); 11 13 12 14 TItemField = class 15 SysName: string; 13 16 Name: string; 14 17 Index: Integer; 18 DataType: TDataType; 19 Position: TPoint; 20 Size: TPoint; 15 21 end; 16 22 … … 18 24 19 25 TItemFields = class(TFPGObjectList<TItemField>) 20 function AddField( Name: string): TItemField;26 function AddField(Index: Integer; SysName, Name: string; DataType: TDataType): TItemField; 21 27 end; 22 28 … … 24 30 25 31 TItem = class 32 private 33 procedure AssignValue(Source: TItem; Field: TItemField); 34 procedure LoadValueFromNode(Node: TDOMNode; Field: TItemField); virtual; 35 procedure SaveValueToNode(Node: TDOMNode; Field: TItemField); virtual; 36 public 26 37 Id: Integer; 27 38 Name: string; … … 32 43 procedure LoadFromNode(Node: TDOMNode); virtual; 33 44 procedure SaveToNode(Node: TDOMNode); virtual; 45 class function GetClassSysName: string; virtual; 34 46 end; 35 47 … … 40 52 TItemList = class(TFPGObjectList<TItem>) 41 53 NewId: Integer; 42 function GetItemClass: TItemClass; virtual;54 class function GetItemClass: TItemClass; virtual; 43 55 function IncrementName(Name: string): string; 44 56 function GetNextAvailableName(Name: string): string; … … 48 60 procedure Assign(Source: TItemList); virtual; 49 61 function AddItem(Name: string): TItem; virtual; 62 procedure LoadFromNode(Node: TDOMNode); virtual; 63 procedure SaveToNode(Node: TDOMNode); virtual; 50 64 constructor Create(FreeObjects: Boolean = True); 51 65 end; … … 74 88 end; 75 89 90 procedure TItemList.LoadFromNode(Node: TDOMNode); 91 var 92 Node2: TDOMNode; 93 NewItem: TItem; 94 begin 95 Count := 0; 96 Node2 := Node.FirstChild; 97 while Assigned(Node2) and (Node2.NodeName = UnicodeString(GetItemClass.GetClassSysName)) do begin 98 NewItem := GetItemClass.Create; 99 NewItem.LoadFromNode(Node2); 100 Add(NewItem); 101 Node2 := Node2.NextSibling; 102 end; 103 end; 104 105 procedure TItemList.SaveToNode(Node: TDOMNode); 106 var 107 I: Integer; 108 NewNode2: TDOMNode; 109 begin 110 for I := 0 to Count - 1 do 111 with TItem(Items[I]) do begin 112 NewNode2 := Node.OwnerDocument.CreateElement(UnicodeString(GetItemClass.GetClassSysName)); 113 Node.AppendChild(NewNode2); 114 SaveToNode(NewNode2); 115 end; 116 end; 117 76 118 constructor TItemList.Create(FreeObjects: Boolean); 77 119 begin … … 80 122 end; 81 123 82 function TItemList.GetItemClass: TItemClass;124 class function TItemList.GetItemClass: TItemClass; 83 125 begin 84 126 Result := TItem; … … 133 175 { TItemFields } 134 176 135 function TItemFields.AddField( Name: string): TItemField;177 function TItemFields.AddField(Index: Integer; SysName, Name: string; DataType: TDataType): TItemField; 136 178 begin 137 179 Result := TItemField.Create; 180 Result.Index := Index; 138 181 Result.Name := Name; 182 Result.SysName := SysName; 183 Result.DataType := DataType; 139 184 Add(Result); 140 185 end; … … 142 187 { TItem } 143 188 189 procedure TItem.AssignValue(Source: TItem; Field: TItemField); 190 var 191 ValueString: string; 192 ValueColor: TColor; 193 ValueInteger: Integer; 194 begin 195 if Field.DataType = dtString then begin 196 Source.GetValue(Field.Index, ValueString); 197 SetValue(Field.Index, ValueString); 198 end else 199 if Field.DataType = dtColor then begin 200 Source.GetValue(Field.Index, ValueColor); 201 SetValue(Field.Index, ValueColor); 202 end else 203 if Field.DataType = dtInteger then begin 204 Source.GetValue(Field.Index, ValueInteger); 205 SetValue(Field.Index, ValueInteger); 206 end else 207 raise Exception.Create('Unsupported field value data type'); 208 end; 209 210 procedure TItem.LoadValueFromNode(Node: TDOMNode; Field: TItemField); 211 var 212 ValueString: string; 213 ValueColor: TColor; 214 ValueInteger: Integer; 215 begin 216 if Field.DataType = dtString then begin 217 ValueString := ReadString(Node, Field.SysName, ''); 218 SetValue(Field.Index, ValueString); 219 end else 220 if Field.DataType = dtColor then begin 221 ValueColor := ReadInteger(Node, Field.SysName, 0); 222 SetValue(Field.Index, ValueColor); 223 end else 224 if Field.DataType = dtInteger then begin 225 ValueInteger := ReadInteger(Node, Field.SysName, 0); 226 SetValue(Field.Index, ValueInteger); 227 end else 228 raise Exception.Create('Unsupported field value data type'); 229 end; 230 231 procedure TItem.SaveValueToNode(Node: TDOMNode; Field: TItemField); 232 var 233 ValueString: string; 234 ValueColor: TColor; 235 ValueInteger: Integer; 236 begin 237 if Field.DataType = dtString then begin 238 GetValue(Field.Index, ValueString); 239 WriteString(Node, Field.SysName, ValueString); 240 end else 241 if Field.DataType = dtColor then begin 242 GetValue(Field.Index, ValueColor); 243 WriteInteger(Node, Field.SysName, ValueColor); 244 end else 245 if Field.DataType = dtInteger then begin 246 GetValue(Field.Index, ValueInteger); 247 WriteInteger(Node, Field.SysName, ValueInteger); 248 end else 249 raise Exception.Create('Unsupported field value data type'); 250 end; 251 144 252 function TItem.GetFields: TItemFields; 145 253 begin 146 254 Result := TItemFields.Create; 255 Result.AddField(1, 'Name', 'Name', dtString); 147 256 end; 148 257 … … 156 265 157 266 procedure TItem.Assign(Source: TItem); 267 var 268 I: Integer; 269 Fields: TItemFields; 158 270 begin 159 271 Id := Source.Id; 160 Name := Source.Name; 272 if Source is ClassType then begin 273 Fields := GetFields; 274 try 275 for I := 0 to Fields.Count - 1 do 276 AssignValue(Source, Fields[I]); 277 finally 278 Fields.Free; 279 end; 280 end; 161 281 end; 162 282 163 283 procedure TItem.LoadFromNode(Node: TDOMNode); 284 var 285 Fields: TItemFields; 286 I: Integer; 164 287 begin 165 288 Id := ReadInteger(Node, 'Id', 0); 166 Name := ReadString(Node, 'Name', ''); 289 Fields := GetFields; 290 try 291 for I := 0 to Fields.Count - 1 do begin 292 LoadValueFromNode(Node, Fields[I]); 293 end; 294 finally 295 Fields.Free; 296 end; 167 297 end; 168 298 169 299 procedure TItem.SaveToNode(Node: TDOMNode); 300 var 301 Fields: TItemFields; 302 I: Integer; 170 303 begin 171 304 WriteInteger(Node, 'Id', Id); 172 WriteString(Node, 'Name', Name); 305 Fields := GetFields; 306 try 307 for I := 0 to Fields.Count - 1 do begin 308 SaveValueToNode(Node, Fields[I]); 309 end; 310 finally 311 Fields.Free; 312 end; 313 end; 314 315 class function TItem.GetClassSysName: string; 316 begin 317 Result := 'Item'; 173 318 end; 174 319
Note:
See TracChangeset
for help on using the changeset viewer.