-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCreateConfigItem.pas
More file actions
470 lines (416 loc) · 12.8 KB
/
CreateConfigItem.pas
File metadata and controls
470 lines (416 loc) · 12.8 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
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
unit CreateConfigItem;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes,
Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ExtCtrls,
Vcl.ComCtrls, System.UITypes;
type
// 配置项类型枚举
TConfigItemType = (
// 简单属性类型
citSection, // 节
citText, // 文本
citInteger, // 整数
citFloat, // 小数
citBoolean, // 布尔值
citDateTime, // 日期时间
citColor, // 颜色
// 特殊编辑器类型
citSpecialBase = 100, // 特殊编辑器基础值
citFont, // 字体编辑器
citPosition, // 位置编辑器
citBackground, // 背景编辑器
citDatabase, // 数据库连接编辑器
citPath, // 路径编辑器
citSpecialMax = 199 // 特殊编辑器最大值
);
// 配置项创建结构
TConfigItemCreateResult = record
Success: Boolean; // 是否成功
ItemType: TConfigItemType; // 项目类型
Name: string; // 项目名称
Value: string; // 项目值(简单类型)
Section: string; // 所属节(如适用)
Description: string; // 描述信息
SpecialParams: TStringList; // 特殊参数(用于专项编辑器)
// 构造和清理
class function CreateSimple(ItemType: TConfigItemType; const AName, AValue: string;
const ASection: string = ''): TConfigItemCreateResult; static;
class function CreateSpecial(ItemType: TConfigItemType; const AName: string;
AParams: TStringList = nil): TConfigItemCreateResult; static;
procedure Clear;
end;
TCreateConfigItemForm = class(TForm)
pgcMain: TPageControl;
tsSimple: TTabSheet;
tsSpecial: TTabSheet;
rgSimpleType: TRadioGroup;
lblName: TLabel;
edtName: TEdit;
lblValue: TLabel;
edtValue: TEdit;
lblSection: TLabel;
edtSection: TEdit;
btnOK: TButton;
btnCancel: TButton;
lblDescription: TLabel;
edtDescription: TEdit;
rgSpecialType: TRadioGroup;
pnlSpecialParams: TPanel;
lblSpecialName: TLabel;
edtSpecialName: TEdit;
pnlButtons: TPanel;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure pgcMainChange(Sender: TObject);
procedure rgSimpleTypeClick(Sender: TObject);
procedure rgSpecialTypeClick(Sender: TObject);
procedure btnOKClick(Sender: TObject);
procedure FormShow(Sender: TObject);
private
FSpecialParams: TStringList;
procedure UpdateControls;
procedure UpdateSpecialPanel;
function ValidateInput: Boolean;
function GetSelectedSimpleType: TConfigItemType;
function GetSelectedSpecialType: TConfigItemType;
function GetIsSectionType: Boolean;
public
function GetResult: TConfigItemCreateResult;
// 璁剧疆鍒濆鍙傛暟
procedure SetInitialType(ItemType: TConfigItemType);
procedure SetSection(const ASection: string);
end;
var
CreateConfigItemForm: TCreateConfigItemForm;
implementation
{$R *.dfm}
{ TCreateConfigItemForm }
procedure TCreateConfigItemForm.FormCreate(Sender: TObject);
begin
// 初始化特殊参数列表
FSpecialParams := TStringList.Create;
// 初始化无线电组
rgSimpleType.Items.Clear;
rgSimpleType.Items.Add(string('节'));
rgSimpleType.Items.Add(string('文本'));
rgSimpleType.Items.Add(string('整数'));
rgSimpleType.Items.Add(string('小数'));
rgSimpleType.Items.Add(string('布尔值'));
rgSimpleType.Items.Add(string('日期时间'));
rgSimpleType.Items.Add(string('颜色'));
rgSimpleType.ItemIndex := 0;
rgSpecialType.Items.Clear;
rgSpecialType.Items.Add(string('字体编辑器'));
rgSpecialType.Items.Add(string('位置编辑器'));
rgSpecialType.Items.Add(string('背景编辑器'));
rgSpecialType.Items.Add(string('数据库连接编辑器'));
rgSpecialType.Items.Add(string('路径编辑器'));
rgSpecialType.ItemIndex := 0;
// 初始化默认页面为简单属性
pgcMain.ActivePage := tsSimple;
// 更新控件状态
UpdateControls;
end;
procedure TCreateConfigItemForm.FormDestroy(Sender: TObject);
begin
// 释放特殊参数列表
if Assigned(FSpecialParams) then
FreeAndNil(FSpecialParams);
end;
procedure TCreateConfigItemForm.FormShow(Sender: TObject);
begin
// 设置初始焦点
if GetIsSectionType then
edtName.SetFocus
else
begin
if pgcMain.ActivePage = tsSimple then
begin
if edtSection.Enabled and (edtSection.Text = '') then
edtSection.SetFocus
else
edtName.SetFocus;
end
else
begin
edtSpecialName.SetFocus;
end;
end;
end;
function TCreateConfigItemForm.GetIsSectionType: Boolean;
begin
Result := (pgcMain.ActivePage = tsSimple) and (rgSimpleType.ItemIndex = 0);
end;
function TCreateConfigItemForm.GetResult: TConfigItemCreateResult;
begin
if pgcMain.ActivePage = tsSimple then
begin
// 创建简单类型结构
Result := TConfigItemCreateResult.CreateSimple(
GetSelectedSimpleType,
edtName.Text,
edtValue.Text,
edtSection.Text
);
Result.Description := edtDescription.Text;
end
else
begin
// 创建特殊类型结果
Result := TConfigItemCreateResult.CreateSpecial(
GetSelectedSpecialType,
edtSpecialName.Text,
FSpecialParams
);
Result.Description := edtDescription.Text;
end;
end;
function TCreateConfigItemForm.GetSelectedSimpleType: TConfigItemType;
begin
case rgSimpleType.ItemIndex of
0: Result := citSection;
1: Result := citText;
2: Result := citInteger;
3: Result := citFloat;
4: Result := citBoolean;
5: Result := citDateTime;
6: Result := citColor;
else Result := citText; // 默认为文本
end;
end;
function TCreateConfigItemForm.GetSelectedSpecialType: TConfigItemType;
begin
case rgSpecialType.ItemIndex of
0: Result := citFont;
1: Result := citPosition;
2: Result := citBackground;
3: Result := citDatabase;
4: Result := citPath;
else Result := citFont; // 默认为字体编辑器
end;
end;
procedure TCreateConfigItemForm.pgcMainChange(Sender: TObject);
begin
UpdateControls;
end;
procedure TCreateConfigItemForm.rgSimpleTypeClick(Sender: TObject);
begin
UpdateControls;
end;
procedure TCreateConfigItemForm.rgSpecialTypeClick(Sender: TObject);
begin
UpdateSpecialPanel;
end;
procedure TCreateConfigItemForm.SetInitialType(ItemType: TConfigItemType);
begin
// 根据项目类型设置初始页面和选择
if ItemType < citSpecialBase then
begin
// 简单类型
pgcMain.ActivePage := tsSimple;
// 设置简单类型的选择
case ItemType of
citSection: rgSimpleType.ItemIndex := 0;
citText: rgSimpleType.ItemIndex := 1;
citInteger: rgSimpleType.ItemIndex := 2;
citFloat: rgSimpleType.ItemIndex := 3;
citBoolean: rgSimpleType.ItemIndex := 4;
citDateTime: rgSimpleType.ItemIndex := 5;
citColor: rgSimpleType.ItemIndex := 6;
else rgSimpleType.ItemIndex := 0;
end;
end
else
begin
// 特殊类型
pgcMain.ActivePage := tsSpecial;
// 设置特殊类型的选择
case ItemType of
citFont: rgSpecialType.ItemIndex := 0;
citPosition: rgSpecialType.ItemIndex := 1;
citBackground:rgSpecialType.ItemIndex := 2;
citDatabase: rgSpecialType.ItemIndex := 3;
citPath: rgSpecialType.ItemIndex := 4;
else rgSpecialType.ItemIndex := 0;
end;
end;
// 更新控件状态
UpdateControls;
end;
procedure TCreateConfigItemForm.SetSection(const ASection: string);
begin
edtSection.Text := ASection;
end;
procedure TCreateConfigItemForm.UpdateControls;
var
IsSimple: Boolean;
IsSection: Boolean;
begin
// 获取当前页面类型
IsSimple := pgcMain.ActivePage = tsSimple;
// 显示/隐藏简单类型属性控件
lblName.Visible := IsSimple;
edtName.Visible := IsSimple;
lblValue.Visible := IsSimple;
edtValue.Visible := IsSimple;
lblSection.Visible := IsSimple;
edtSection.Visible := IsSimple;
// 显示/隐藏特殊类型属性控件
lblSpecialName.Visible := not IsSimple;
edtSpecialName.Visible := not IsSimple;
pnlSpecialParams.Visible := not IsSimple;
// 如果是简单类型,更新值输入框的状态
if IsSimple then
begin
IsSection := rgSimpleType.ItemIndex = 0; // 是否为节类型
// 节类型不需要值和节名称
lblValue.Enabled := not IsSection;
edtValue.Enabled := not IsSection;
// 如果是节,则不需要指定所属节
lblSection.Enabled := not IsSection;
edtSection.Enabled := not IsSection;
// 根据类型设置默认值提示
case rgSimpleType.ItemIndex of
0: edtValue.TextHint := string(''); // 节不需要值
1: edtValue.TextHint := string('请输入文本值');
2: edtValue.TextHint := string('请输入整数值');
3: edtValue.TextHint := string('请输入小数值');
4: edtValue.TextHint := string('True 或 False');
5: edtValue.TextHint := string('YYYY-MM-DD HH:NN:SS');
6: edtValue.TextHint := string('clRed, #FF0000 或 $00FF0000');
end;
end
else
begin
// 更新特殊类型面板
UpdateSpecialPanel;
end;
end;
procedure TCreateConfigItemForm.UpdateSpecialPanel;
begin
// 根据选择的特殊类型更新参数面板
// 这里可以根据不同的特殊类型动态创建不同的控件
// 暂时仅显示基本信息
pnlSpecialParams.Caption := Format('已选择: %s', [rgSpecialType.Items[rgSpecialType.ItemIndex]]);
end;
function TCreateConfigItemForm.ValidateInput: Boolean;
var
ErrorMsg: string;
IsSection: Boolean;
IntValue: Integer;
FloatValue: Double;
begin
Result := True;
ErrorMsg := '';
// 验证简单类型输入
if pgcMain.ActivePage = tsSimple then
begin
IsSection := rgSimpleType.ItemIndex = 0;
// 名称必须填写
if edtName.Text = '' then
begin
ErrorMsg := string('请输入名称');
edtName.SetFocus;
Result := False;
end
// 如果不是节,且需要值,则验证值
else if (not IsSection) and (edtValue.Text = '') then
begin
ErrorMsg := string('请输入值');
edtValue.SetFocus;
Result := False;
end
// 如果不是节,则需要指定所属节
else if (not IsSection) and (edtSection.Text = '') then
begin
ErrorMsg := string('请指定所属节');
edtSection.SetFocus;
Result := False;
end
// 验证数值类型
else if (rgSimpleType.ItemIndex = 2) and not TryStrToInt(string(edtValue.Text), IntValue) then
begin
ErrorMsg := string('请输入有效的整数值');
edtValue.SetFocus;
Result := False;
end
else if (rgSimpleType.ItemIndex = 3) and not TryStrToFloat(string(edtValue.Text), FloatValue) then
begin
ErrorMsg := string('请输入有效的小数值');
edtValue.SetFocus;
Result := False;
end
else if (rgSimpleType.ItemIndex = 4) and
(not SameText(string(edtValue.Text), 'True') and not SameText(string(edtValue.Text), 'False')) then
begin
ErrorMsg := string('布尔值必须为 True 或 False');
edtValue.SetFocus;
Result := False;
end;
end
else
begin
// 验证特殊类型输入
if edtSpecialName.Text = '' then
begin
ErrorMsg := string('请输入名称');
edtSpecialName.SetFocus;
Result := False;
end;
// 这里可以添加特殊类型的其他验证
end;
// 显示错误消息
if not Result then
MessageDlg(ErrorMsg, mtError, [mbOK], 0);
end;
procedure TCreateConfigItemForm.btnOKClick(Sender: TObject);
begin
// 验证输入
if ValidateInput then
ModalResult := mrOk
else
ModalResult := mrNone;
end;
{ TConfigItemCreateResult }
class function TConfigItemCreateResult.CreateSimple(ItemType: TConfigItemType;
const AName, AValue, ASection: string): TConfigItemCreateResult;
begin
Result.Success := True;
Result.ItemType := ItemType;
Result.Name := AName;
Result.Value := AValue;
Result.Section := ASection;
Result.Description := '';
Result.SpecialParams := nil;
end;
class function TConfigItemCreateResult.CreateSpecial(ItemType: TConfigItemType;
const AName: string; AParams: TStringList): TConfigItemCreateResult;
begin
Result.Success := True;
Result.ItemType := ItemType;
Result.Name := AName;
Result.Value := '';
Result.Section := '';
Result.Description := '';
// 复制特殊参数
if Assigned(AParams) then
begin
Result.SpecialParams := TStringList.Create;
Result.SpecialParams.Assign(AParams);
end
else
Result.SpecialParams := nil;
end;
procedure TConfigItemCreateResult.Clear;
begin
Success := False;
ItemType := citText;
Name := '';
Value := '';
Section := '';
Description := '';
if Assigned(SpecialParams) then
FreeAndNil(SpecialParams);
end;
end.