shortcut editor, prevent a few indirections

This commit is contained in:
Basile Burg 2019-02-03 20:02:04 +01:00
parent 41881be499
commit 2ef299b91e
1 changed files with 50 additions and 34 deletions

View File

@ -160,7 +160,7 @@ end;
function TShortCutCollection.findIdentifier(const identifier: string): boolean; function TShortCutCollection.findIdentifier(const identifier: string): boolean;
var var
i: Integer; i: integer;
begin begin
result := false; result := false;
for i := 0 to count-1 do for i := 0 to count-1 do
@ -170,12 +170,16 @@ end;
function TShortCutCollection.findShortcut(aShortcut: Word): TShortcutItem; function TShortCutCollection.findShortcut(aShortcut: Word): TShortcutItem;
var var
i: Integer; i: integer;
s: TShortcutItem;
begin begin
result := nil; result := nil;
for i := 0 to count-1 do for i := 0 to count-1 do
if item[i].data = aShortcut then begin
exit(item[i]); s := item[i];
if s.data = aShortcut then
exit(s);
end;
end; end;
{$ENDREGION} {$ENDREGION}
@ -307,6 +311,7 @@ var
s: TShortCut; s: TShortCut;
d: TShortcutItem = nil; d: TShortcutItem = nil;
t: string; t: string;
n: TTreeNode;
o: TTreeNode; o: TTreeNode;
const const
m1 = 'warning, "%s" is already assigned in the "%s" category and it is not guaranteed to work properly'; m1 = 'warning, "%s" is already assigned in the "%s" category and it is not guaranteed to work properly';
@ -322,16 +327,17 @@ begin
// warn but accept a dup if already in another category // warn but accept a dup if already in another category
for i:= 0 to tree.Items.Count-1 do for i:= 0 to tree.Items.Count-1 do
begin begin
if tree.Items[i] = tree.Selected.Parent then n := tree.Items[i];
if n = tree.Selected.Parent then
continue; continue;
for j := 0 to Tree.Items[i].Count-1 do for j := 0 to n.Count-1 do
begin begin
o := Tree.Items[i].Items[j]; o := n.Items[j];
if o.Data.isNil then if o.Data.isNil then
continue; continue;
if TShortcutItem(o.Data).data = s then if TShortcutItem(o.Data).data = s then
begin begin
dlgOkInfo(format(m1, [t, Tree.Items[i].Text])); dlgOkInfo(format(m1, [t, n.Text]));
break; break;
end; end;
end; end;
@ -413,23 +419,31 @@ end;
function TShortcutEditor.findCategory(const aName: string; aData: Pointer): TTreeNode; function TShortcutEditor.findCategory(const aName: string; aData: Pointer): TTreeNode;
var var
i: integer; i: integer;
n: TTreeNode;
begin begin
result := nil; result := nil;
for i:= 0 to tree.Items.Count-1 do for i:= 0 to tree.Items.Count-1 do
if tree.Items[i].Text = aName then begin
if tree.Items[i].Data = aData then n := tree.Items[i];
exit(tree.Items[i]); if (n.Text = aName) and (n.Data = aData) then
exit(n);
end;
end; end;
function TShortcutEditor.findCategory(const aShortcutItem: TShortcutItem): string; function TShortcutEditor.findCategory(const aShortcutItem: TShortcutItem): string;
var var
i, j: integer; i: integer;
j: integer;
n: TTreeNode;
begin begin
result := ''; result := '';
for i := 0 to tree.Items.Count-1 do for i := 0 to tree.Items.Count-1 do
for j:= 0 to tree.Items.Item[i].Count-1 do begin
if tree.Items.Item[i].Items[j].Data = Pointer(aShortcutItem) then n := tree.Items.Item[i];
exit(tree.Items.Item[i].Text); for j:= 0 to n.Count-1 do
if n.Items[j].Data = Pointer(aShortcutItem) then
exit(n.Text);
end;
end; end;
function TShortcutEditor.sortCategories(Cat1, Cat2: TTreeNode): integer; function TShortcutEditor.sortCategories(Cat1, Cat2: TTreeNode): integer;
@ -445,25 +459,27 @@ var
sht: word; sht: word;
idt: string; idt: string;
itm: TShortcutItem; itm: TShortcutItem;
procedure addItem();
var procedure addItem();
prt: TTreeNode; var
begin prt: TTreeNode;
// root category begin
if cat.isEmpty or idt.isEmpty then // root category
exit; if cat.isEmpty or idt.isEmpty then
prt := findCategory(cat, obs); exit;
if prt.isNil then prt := findCategory(cat, obs);
prt := tree.Items.AddObject(nil, cat, obs); if prt.isNil then
// item as child prt := tree.Items.AddObject(nil, cat, obs);
itm := TShortcutItem(fShortcuts.items.Add); // item as child
itm.identifier := idt; itm := TShortcutItem(fShortcuts.items.Add);
itm.data:= sht; itm.identifier := idt;
itm.declarator := obs; itm.data:= sht;
tree.Items.AddChildObject(prt, idt, itm); itm.declarator := obs;
cat := ''; tree.Items.AddChildObject(prt, idt, itm);
idt := ''; cat := '';
end; idt := '';
end;
begin begin
tree.Items.Clear; tree.Items.Clear;
fShortcuts.items.Clear; fShortcuts.items.Clear;