| 1 | unit FormItem;
|
|---|
| 2 |
|
|---|
| 3 | interface
|
|---|
| 4 |
|
|---|
| 5 | uses
|
|---|
| 6 | Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, ItemList,
|
|---|
| 7 | ExtCtrls, ColorBox, Spin, Generics.Collections, FormEx;
|
|---|
| 8 |
|
|---|
| 9 | type
|
|---|
| 10 |
|
|---|
| 11 | { TFormItem }
|
|---|
| 12 |
|
|---|
| 13 | TFormItem = class(TFormEx)
|
|---|
| 14 | ButtonOk: TButton;
|
|---|
| 15 | ButtonCancel: TButton;
|
|---|
| 16 | ScrollBox1: TScrollBox;
|
|---|
| 17 | procedure ButtonOkClick(Sender: TObject);
|
|---|
| 18 | procedure FormCreate(Sender: TObject);
|
|---|
| 19 | procedure FormDestroy(Sender: TObject);
|
|---|
| 20 | private
|
|---|
| 21 | FItem: TItem;
|
|---|
| 22 | DataControls: TObjectList<TControl>;
|
|---|
| 23 | DataLabels: TObjectList<TLabel>;
|
|---|
| 24 | procedure ControlChangeExecute(Sender: TObject);
|
|---|
| 25 | procedure SetItem(AValue: TItem);
|
|---|
| 26 | procedure LoadData(Item: TItem); virtual;
|
|---|
| 27 | procedure SaveData(Item: TItem); virtual;
|
|---|
| 28 | procedure InitControls;
|
|---|
| 29 | public
|
|---|
| 30 | procedure UpdateInterface;
|
|---|
| 31 | property Item: TItem read FItem write SetItem;
|
|---|
| 32 | end;
|
|---|
| 33 |
|
|---|
| 34 |
|
|---|
| 35 | implementation
|
|---|
| 36 |
|
|---|
| 37 | {$R *.lfm}
|
|---|
| 38 |
|
|---|
| 39 | { TFormItem }
|
|---|
| 40 |
|
|---|
| 41 | procedure TFormItem.ButtonOkClick(Sender: TObject);
|
|---|
| 42 | begin
|
|---|
| 43 | SaveData(Item);
|
|---|
| 44 | end;
|
|---|
| 45 |
|
|---|
| 46 | procedure TFormItem.FormCreate(Sender: TObject);
|
|---|
| 47 | begin
|
|---|
| 48 | DataControls := TObjectList<TControl>.Create;
|
|---|
| 49 | DataLabels := TObjectList<TLabel>.Create;
|
|---|
| 50 | end;
|
|---|
| 51 |
|
|---|
| 52 | procedure TFormItem.FormDestroy(Sender: TObject);
|
|---|
| 53 | begin
|
|---|
| 54 | FreeAndNil(DataLabels);
|
|---|
| 55 | FreeAndNil(DataControls);
|
|---|
| 56 | end;
|
|---|
| 57 |
|
|---|
| 58 | procedure TFormItem.ControlChangeExecute(Sender: TObject);
|
|---|
| 59 | begin
|
|---|
| 60 | UpdateInterface;
|
|---|
| 61 | end;
|
|---|
| 62 |
|
|---|
| 63 | procedure TFormItem.SetItem(AValue: TItem);
|
|---|
| 64 | begin
|
|---|
| 65 | if FItem = AValue then Exit;
|
|---|
| 66 | FItem := AValue;
|
|---|
| 67 | if Assigned(FItem) then begin
|
|---|
| 68 | InitControls;
|
|---|
| 69 | LoadData(Item);
|
|---|
| 70 | Caption := Item.GetClassName;
|
|---|
| 71 | end else begin
|
|---|
| 72 | DataControls.Clear;
|
|---|
| 73 | end;
|
|---|
| 74 | end;
|
|---|
| 75 |
|
|---|
| 76 | procedure TFormItem.LoadData(Item: TItem);
|
|---|
| 77 | var
|
|---|
| 78 | Fields: TItemFields;
|
|---|
| 79 | I: Integer;
|
|---|
| 80 | J: Integer;
|
|---|
| 81 | Control: TControl;
|
|---|
| 82 | ReferenceList: TBaseItemList;
|
|---|
| 83 | ReferenceItem: TItem;
|
|---|
| 84 | begin
|
|---|
| 85 | Fields := Item.GetFields;
|
|---|
| 86 | for I := 0 to Fields.Count - 1 do
|
|---|
| 87 | with Fields[I] do begin
|
|---|
| 88 | Control := DataControls[I];
|
|---|
| 89 | if DataType = dtInteger then TSpinEdit(Control).Value := Item.GetValueInteger(Index)
|
|---|
| 90 | else if DataType = dtString then TEdit(Control).Text := Item.GetValueString(Index)
|
|---|
| 91 | else if DataType = dtColor then TColorBox(Control).Selected := Item.GetValueColor(Index)
|
|---|
| 92 | else if DataType = dtBoolean then TCheckBox(Control).Checked := Item.GetValueBoolean(Index)
|
|---|
| 93 | else if DataType = dtEnumeration then begin
|
|---|
| 94 | TComboBox(Control).Items.BeginUpdate;
|
|---|
| 95 | try
|
|---|
| 96 | TComboBox(Control).Items.Clear;
|
|---|
| 97 | for J := 0 to EnumStates.Count - 1 do
|
|---|
| 98 | TComboBox(Control).Items.Add(EnumStates[J]);
|
|---|
| 99 | TComboBox(Control).ItemIndex := Integer(Item.GetValueEnumeration(Index));
|
|---|
| 100 | finally
|
|---|
| 101 | TComboBox(Control).Items.EndUpdate;
|
|---|
| 102 | end;
|
|---|
| 103 | end
|
|---|
| 104 | else if DataType = dtReference then begin
|
|---|
| 105 | TComboBox(Control).Items.BeginUpdate;
|
|---|
| 106 | try
|
|---|
| 107 | TComboBox(Control).Items.Clear;
|
|---|
| 108 | ReferenceList := Item.GetReferenceList(Index);
|
|---|
| 109 | if Assigned(ReferenceList) then
|
|---|
| 110 | for J := 0 to ReferenceList.Count - 1 do
|
|---|
| 111 | TComboBox(Control).Items.AddObject(TItem(ReferenceList[J]).Name, ReferenceList[J]);
|
|---|
| 112 | ReferenceItem := TItem(Item.GetValueReference(Index));
|
|---|
| 113 | TComboBox(Control).ItemIndex := TComboBox(Control).Items.IndexOfObject(ReferenceItem);
|
|---|
| 114 | finally
|
|---|
| 115 | TComboBox(Control).Items.EndUpdate;
|
|---|
| 116 | end;
|
|---|
| 117 | end
|
|---|
| 118 | else raise Exception.Create(Format(SUnsupportedDataType, [DataTypeStr[DataType]]));
|
|---|
| 119 | end;
|
|---|
| 120 | Fields.Free;
|
|---|
| 121 | UpdateInterface;
|
|---|
| 122 | end;
|
|---|
| 123 |
|
|---|
| 124 | procedure TFormItem.SaveData(Item: TItem);
|
|---|
| 125 | var
|
|---|
| 126 | Fields: TItemFields;
|
|---|
| 127 | I: Integer;
|
|---|
| 128 | Control: TControl;
|
|---|
| 129 | begin
|
|---|
| 130 | Fields := Item.GetFields;
|
|---|
| 131 | for I := 0 to Fields.Count - 1 do
|
|---|
| 132 | with Fields[I] do begin
|
|---|
| 133 | Control := DataControls[I];
|
|---|
| 134 | if DataType = dtInteger then Item.SetValueInteger(Index, TSpinEdit(Control).Value)
|
|---|
| 135 | else if DataType = dtString then Item.SetValueString(Index, TEdit(Control).Text)
|
|---|
| 136 | else if DataType = dtColor then Item.SetValueColor(Index, TColorBox(Control).Selected)
|
|---|
| 137 | else if DataType = dtBoolean then Item.SetValueBoolean(Index, TCheckBox(Control).Checked)
|
|---|
| 138 | else if DataType = dtEnumeration then Item.SetValueEnumeration(Index, TUndefinedEnum(TComboBox(Control).ItemIndex))
|
|---|
| 139 | else if DataType = dtReference then begin
|
|---|
| 140 | if TComboBox(Control).ItemIndex <> -1 then
|
|---|
| 141 | Item.SetValueReference(Index, TItem(TComboBox(Control).Items.Objects[TComboBox(Control).ItemIndex]))
|
|---|
| 142 | else Item.SetValueReference(Index, nil);
|
|---|
| 143 | end
|
|---|
| 144 | else raise Exception.Create(Format(SUnsupportedDataType, [DataTypeStr[DataType]]));
|
|---|
| 145 | end;
|
|---|
| 146 | Fields.Free;
|
|---|
| 147 | end;
|
|---|
| 148 |
|
|---|
| 149 | procedure TFormItem.InitControls;
|
|---|
| 150 | var
|
|---|
| 151 | NewControl: TControl;
|
|---|
| 152 | NewLabel: TLabel;
|
|---|
| 153 | Fields: TItemFields;
|
|---|
| 154 | I: Integer;
|
|---|
| 155 | Y: Integer;
|
|---|
| 156 | begin
|
|---|
| 157 | DataControls.Clear;
|
|---|
| 158 | DataLabels.Clear;
|
|---|
| 159 | Y := Scale96ToScreen(8);
|
|---|
| 160 | Fields := Item.GetFields;
|
|---|
| 161 | for I := 0 to Fields.Count - 1 do
|
|---|
| 162 | with Fields[I] do begin
|
|---|
| 163 | NewLabel := TLabel.Create(ScrollBox1);
|
|---|
| 164 | NewLabel.Left := Scale96ToScreen(8);
|
|---|
| 165 | NewLabel.Top := Y;
|
|---|
| 166 | NewLabel.Parent := ScrollBox1;
|
|---|
| 167 | NewLabel.Caption := Name + ':';
|
|---|
| 168 | NewLabel.Visible := True;
|
|---|
| 169 | DataLabels.Add(NewLabel);
|
|---|
| 170 |
|
|---|
| 171 | if DataType = dtInteger then begin
|
|---|
| 172 | NewControl := TSpinEdit.Create(nil);
|
|---|
| 173 | NewControl.Width := Scale96ToScreen(100);
|
|---|
| 174 | TSpinEdit(NewControl).MaxValue := High(Integer);
|
|---|
| 175 | end else
|
|---|
| 176 | if DataType = dtString then begin
|
|---|
| 177 | NewControl := TEdit.Create(nil);
|
|---|
| 178 | NewControl.Width := Scale96ToScreen(200);
|
|---|
| 179 | end else
|
|---|
| 180 | if DataType = dtColor then begin
|
|---|
| 181 | NewControl := TColorBox.Create(nil);
|
|---|
| 182 | NewControl.Width := Scale96ToScreen(200);
|
|---|
| 183 | TColorBox(NewControl).Style := [cbStandardColors, cbExtendedColors,
|
|---|
| 184 | cbCustomColor, cbPrettyNames];
|
|---|
| 185 | end else
|
|---|
| 186 | if DataType = dtEnumeration then begin
|
|---|
| 187 | NewControl := TComboBox.Create(nil);
|
|---|
| 188 | NewControl.Width := Scale96ToScreen(200);
|
|---|
| 189 | TComboBox(NewControl).Style := csDropDownList;
|
|---|
| 190 | TComboBox(NewControl).OnChange := ControlChangeExecute;
|
|---|
| 191 | end else
|
|---|
| 192 | if DataType = dtReference then begin
|
|---|
| 193 | NewControl := TComboBox.Create(nil);
|
|---|
| 194 | NewControl.Width := Scale96ToScreen(200);
|
|---|
| 195 | TComboBox(NewControl).Style := csDropDownList;
|
|---|
| 196 | end else
|
|---|
| 197 | if DataType = dtBoolean then begin
|
|---|
| 198 | NewControl := TCheckBox.Create(nil);
|
|---|
| 199 | end else
|
|---|
| 200 | raise Exception.Create(Format(SUnsupportedDataType, [DataTypeStr[DataType]]));
|
|---|
| 201 | NewControl.Left := Scale96ToScreen(150);
|
|---|
| 202 | NewControl.Top := Y;
|
|---|
| 203 | NewControl.Parent := ScrollBox1;
|
|---|
| 204 | TFormEx.Translator.TranslateComponent(NewControl);
|
|---|
| 205 | TFormEx.ThemeManager.ApplyTheme(NewControl);
|
|---|
| 206 | NewControl.Visible := True;
|
|---|
| 207 | DataControls.Add(NewControl);
|
|---|
| 208 | Y := Y + Scale96ToScreen(30);
|
|---|
| 209 | end;
|
|---|
| 210 | Fields.Free;
|
|---|
| 211 | end;
|
|---|
| 212 |
|
|---|
| 213 | procedure TFormItem.UpdateInterface;
|
|---|
| 214 | var
|
|---|
| 215 | I: Integer;
|
|---|
| 216 | Fields: TItemFields;
|
|---|
| 217 | Condition: Boolean;
|
|---|
| 218 | begin
|
|---|
| 219 | Fields := Item.GetFields;
|
|---|
| 220 | try
|
|---|
| 221 | for I := 0 to Fields.Count - 1 do
|
|---|
| 222 | with Fields[I] do begin
|
|---|
| 223 | if VisibleIfIndex > 0 then begin
|
|---|
| 224 | if Fields[VisibleIfIndex].DataType = dtEnumeration then begin
|
|---|
| 225 | Condition := TComboBox(DataControls[VisibleIfIndex]).ItemIndex > 0;
|
|---|
| 226 | end else Condition := False;
|
|---|
| 227 | end;
|
|---|
| 228 | DataControls[I].Visible := (VisibleIfIndex = 0) or ((VisibleIfIndex > 0) and Condition);
|
|---|
| 229 | DataLabels[I].Visible := DataControls[I].Visible;
|
|---|
| 230 | end;
|
|---|
| 231 | finally
|
|---|
| 232 | Fields.Free;
|
|---|
| 233 | end;
|
|---|
| 234 | end;
|
|---|
| 235 |
|
|---|
| 236 | end.
|
|---|
| 237 |
|
|---|