-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathutwosetattribruleframe.pas
349 lines (311 loc) · 11.2 KB
/
utwosetattribruleframe.pas
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
(******************************************************************************)
(* *)
(* Author : Uwe Schächterle (Corpsman) *)
(* *)
(* This file is part of Einstein *)
(* *)
(* See the file license.md, located under: *)
(* https://github.com/PascalCorpsman/Software_Licenses/blob/main/license.md *)
(* for details about the license. *)
(* *)
(* It is not allowed to change or remove this text from any *)
(* source file of the project. *)
(* *)
(******************************************************************************)
Unit utwosetattribruleframe;
{$MODE objfpc}{$H+}
Interface
Uses
Classes, SysUtils, FileUtil, Forms, Controls, StdCtrls, ueinstein, inifiles;
Type
{ TTwoSetAttribRuleFrame }
TTwoSetAttribRuleFrame = Class(TEinsteinRuleFrame)
Button1: TButton;
GroupBox1: TGroupBox;
Label1: TLabel;
Procedure Button1Click(Sender: TObject);
private
{ private declarations }
SetLabels: Array Of TLabel;
SetComboboxes: Array Of TComboBox;
Set2Comboboxes: Array Of TComboBox;
AttribLabels: Array Of TLabel;
AttribEdits: Array Of TEdit;
HintLabel: TLabel;
HintEdit: TEdit;
public
{ public declarations }
Constructor Create(TheOwner: TComponent); override;
Destructor Destroy; override;
Procedure SetCaption(Value: String); override;
Procedure RefreshTexts; override;
Procedure SetSet(Index: integer; Set_: TMemberSet); override;
Function GetSet(Index: integer): TMemberSet; override;
Procedure SaveToFile(Const Ini: TInifile; Index: integer); override;
Procedure LoadFromFile(Const Ini: TInifile; Index: integer); override;
Function ValidRule: Boolean; override;
Function DeriveRule: TRule; override;
Procedure SetAttribute(Index: integer; Value: integer); override;
Procedure SetAttributeCountAndLabels(Const Labels: Array Of String); override;
Procedure SetUserHint(Data: String); override;
End;
Implementation
Uses math;
{$R *.lfm}
{ TTwoSetAttribRuleFrame }
Procedure TTwoSetAttribRuleFrame.Button1Click(Sender: TObject);
Begin
// Del (damit ist die eigene Regel gemeint)
SelfDeleteCallback(self);
End;
Constructor TTwoSetAttribRuleFrame.Create(TheOwner: TComponent);
Var
i: Integer;
Begin
Inherited Create(TheOwner);
setlength(SetLabels, EinsteinHeight);
setlength(SetComboboxes, EinsteinHeight);
setlength(Set2Comboboxes, EinsteinHeight);
For i := 0 To EinsteinHeight - 1 Do Begin
SetLabels[i] := TLabel.Create(GroupBox1);
SetLabels[i].Parent := GroupBox1;
SetLabels[i].Name := 'SetLabel' + IntToStr(i);
SetLabels[i].Caption := 'Label' + inttostr(i + 1);
SetLabels[i].Top := 8;
SetLabels[i].Left := 8 + 112 * i;
SetComboboxes[i] := TComboBox.Create(GroupBox1);
SetComboboxes[i].Parent := GroupBox1;
SetComboboxes[i].Name := 'SetCombobox' + IntToStr(i);
SetComboboxes[i].Text := '';
SetComboboxes[i].Items.Clear;
SetComboboxes[i].Top := 32;
SetComboboxes[i].Left := 8 + 112 * i;
SetComboboxes[i].Width := 100;
SetComboboxes[i].ReadOnly := true;
SetComboboxes[i].OnChange := @MarkAsChanged;
Set2Comboboxes[i] := TComboBox.Create(GroupBox1);
Set2Comboboxes[i].Parent := GroupBox1;
Set2Comboboxes[i].Name := 'Set2Combobox' + IntToStr(i);
Set2Comboboxes[i].Text := '';
Set2Comboboxes[i].Items.Clear;
Set2Comboboxes[i].Top := 96;
Set2Comboboxes[i].Left := 8 + 112 * i;
Set2Comboboxes[i].Width := 100;
Set2Comboboxes[i].ReadOnly := true;
Set2Comboboxes[i].OnChange := @MarkAsChanged;
End;
GroupBox1.Width := 568 + (EinsteinHeight - 5) * 112;
Button1.left := 584 + (EinsteinHeight - 5) * 112;
self.width := 642 + (EinsteinHeight - 5) * 112;
HintLabel := TLabel.Create(GroupBox1);
HintLabel.Parent := GroupBox1;
HintLabel.Left := 8;
HintLabel.Top := 96 + 64 + 32;
HintLabel.caption := 'Hint : ';
HintLabel.Name := 'HintLabel';
HintEdit := TEdit.Create(GroupBox1);
HintEdit.Parent := GroupBox1;
HintEdit.Name := 'HintEdit';
HintEdit.Top := 96 + 64 + 32;
HintEdit.Left := 50;
HintEdit.text := '';
HintEdit.Width := GroupBox1.Width - 50 - 10;
HintEdit.OnChange := @MarkAsChanged;
End;
Destructor TTwoSetAttribRuleFrame.Destroy;
Begin
Inherited Destroy;
End;
Procedure TTwoSetAttribRuleFrame.SetCaption(Value: String);
Begin
GroupBox1.Caption := value;
End;
Procedure TTwoSetAttribRuleFrame.RefreshTexts;
Var
i, j, k: integer;
c: TComboBox;
Begin
For i := 0 To EinsteinHeight - 1 Do Begin
// Todo : Umbaun wie utwosetruleframe
SetLabels[i].Caption := ResolveXY(i, -2);
c := SetComboboxes[i];
k := c.ItemIndex;
c.Items.Clear;
c.items.add('-');
For j := 0 To EinsteinWidth - 1 Do Begin
c.items.add(ResolveXY(i, j));
End;
c.ItemIndex := max(0, k);
c := Set2Comboboxes[i];
k := c.ItemIndex;
c.Items.Clear;
c.items.add('-');
For j := 0 To EinsteinWidth - 1 Do Begin
c.items.add(ResolveXY(i, j));
End;
c.ItemIndex := max(0, k);
End;
label1.Caption := RuleSelectorToString(Rule);
End;
Procedure TTwoSetAttribRuleFrame.SetSet(Index: integer; Set_: TMemberSet);
Var
i: Integer;
Begin
If index = 0 Then Begin
For i := 0 To high(Set_) Do Begin
SetComboboxes[i].ItemIndex := Set_[i] + 1;
End;
fDataChanged := false;
exit;
End;
If index = 1 Then Begin
For i := 0 To high(Set_) Do Begin
Set2Comboboxes[i].ItemIndex := Set_[i] + 1;
End;
fDataChanged := false;
exit;
End;
Raise Exception.create('Error invalid index');
End;
Function TTwoSetAttribRuleFrame.GetSet(Index: integer): TMemberSet;
Var
i: Integer;
Begin
result := Nil;
If index = 0 Then Begin
setlength(result, EinsteinHeight);
For i := 0 To EinsteinHeight - 1 Do Begin
result[i] := SetComboboxes[i].ItemIndex - 1;
End;
End;
If index = 1 Then Begin
setlength(result, EinsteinHeight);
For i := 0 To EinsteinHeight - 1 Do Begin
result[i] := Set2Comboboxes[i].ItemIndex - 1;
End;
End;
End;
Procedure TTwoSetAttribRuleFrame.SaveToFile(Const Ini: TInifile; Index: integer
);
Var
Section: String;
i: Integer;
Begin
Inherited;
Section := 'Rule' + inttostr(Index);
ini.WriteString(Section, 'Name', RuleSelectorToString(Rule));
For i := 0 To EinsteinHeight - 1 Do Begin
ini.WriteInteger(Section, 'Set0_Element' + inttostr(i), SetComboboxes[i].ItemIndex - 1);
ini.WriteInteger(Section, 'Set1_Element' + inttostr(i), Set2Comboboxes[i].ItemIndex - 1);
End;
// Count muss nicht gespeichert werden, da der Loader in Form1 immer Richtig erzeugt ;)
For i := 0 To high(AttribEdits) Do Begin
ini.WriteString(section, 'Attrib' + inttostr(i), AttribEdits[i].Text);
End;
ini.WriteString(Section, 'Hint', HintEdit.Text);
End;
Procedure TTwoSetAttribRuleFrame.LoadFromFile(Const Ini: TInifile;
Index: integer);
Var
Section: String;
i: Integer;
s: String;
Begin
Section := 'Rule' + inttostr(Index);
s := ini.ReadString(Section, 'Name', '?');
Rule := StringToRuleSelector(s);
For i := 0 To EinsteinHeight - 1 Do Begin
SetComboboxes[i].ItemIndex := ini.ReadInteger(Section, 'Set0_Element' + inttostr(i), -1) + 1;
Set2Comboboxes[i].ItemIndex := ini.ReadInteger(Section, 'Set1_Element' + inttostr(i), -1) + 1;
End;
// Count muss nicht gespeichert werden, da der Loader in Form1 immer Richtig erzeugt ;)
For i := 0 To high(AttribEdits) Do Begin
AttribEdits[i].Text := ini.ReadString(section, 'Attrib' + inttostr(i), '');
End;
HintEdit.Text := ini.ReadString(Section, 'Hint', '');
Inherited;
End;
Function TTwoSetAttribRuleFrame.ValidRule: Boolean;
Var
i: Integer;
res2: Boolean;
Begin
result := false;
// 1. Prüfen ob mindestens 1 Element aus der Grundmenge gewählt wurde
For i := 0 To high(SetComboboxes) Do Begin
result := result Or (SetComboboxes[i].ItemIndex <> 0);
End;
res2 := false;
// 1. Prüfen ob mindestens 1 Element aus der Grundmenge gewählt wurde
For i := 0 To high(Set2Comboboxes) Do Begin
res2 := res2 Or (Set2Comboboxes[i].ItemIndex <> 0);
End;
result := result And res2;
// 2. Sind alle Attribute gesetzt ?
If result Then Begin
For i := 0 To high(AttribEdits) Do Begin
result := result And (trim(AttribEdits[i].Text) <> '');
End;
End;
End;
Function TTwoSetAttribRuleFrame.DeriveRule: TRule;
Begin
result := Nil;
Case Rule Of
rsDistanceLeftOf: Begin
result := TLeftDistanceDependsOf.Create(GetSet(0), GetSet(1));
TLeftDistanceDependsOf(result).Distance := strtointdef(AttribEdits[0].Text, 1);
End;
rsDistanceDependsOn: Begin
result := TDistanceDependsOn.Create(GetSet(0), GetSet(1));
TDistanceDependsOn(result).Distance := strtointdef(AttribEdits[0].Text, 1);
End;
rsDistanceRightOf: Begin
result := TRightDistanceDependsOf.Create(GetSet(0), GetSet(1));
TRightDistanceDependsOf(result).Distance := strtointdef(AttribEdits[0].Text, 1);
End;
rsDistanceEliminate: Begin
result := TDistanceEliminate.Create(GetSet(0), GetSet(1));
TDistanceEliminate(result).Distance := strtointdef(AttribEdits[0].Text, 1);
End;
Else Begin
Raise exception.Create('Fehler nicht implementierte Regel : TTwoSetAttribRuleFrame.DeriveRule.' + RuleSelectorToString(rule));
End;
End;
result.Hint := HintEdit.Text;
End;
Procedure TTwoSetAttribRuleFrame.SetAttribute(Index: integer; Value: integer);
Begin
AttribEdits[Index].Text := inttostr(Value);
fDataChanged := false;
End;
Procedure TTwoSetAttribRuleFrame.SetAttributeCountAndLabels(
Const Labels: Array Of String);
Var
i: Integer;
Begin
setlength(AttribLabels, length(Labels));
setlength(AttribEdits, length(Labels));
For i := 0 To high(labels) Do Begin
AttribLabels[i] := TLabel.Create(GroupBox1);
AttribLabels[i].Parent := GroupBox1;
AttribLabels[i].Name := 'AttributeLabel' + IntToStr(i);
AttribLabels[i].Top := 72 + 64;
AttribLabels[i].Left := 8 + i * 112;
AttribLabels[i].Caption := labels[i];
AttribEdits[i] := TEdit.Create(GroupBox1);
AttribEdits[i].Parent := GroupBox1;
AttribEdits[i].Name := 'AttributeEdit' + IntToStr(i);
AttribEdits[i].Top := 96 + 64;
AttribEdits[i].Width := 100;
AttribEdits[i].Left := 8 + i * 112;
AttribEdits[i].text := '';
AttribEdits[i].OnChange := @MarkAsChanged;
End;
End;
Procedure TTwoSetAttribRuleFrame.SetUserHint(Data: String);
Begin
HintEdit.Text := data;
fDataChanged := false;
End;
End.