IDE: Do not search for MainList index in ToolBarConfig, they are always synced. Improve selection logic. Reduce duplicate code. Refactoring.

git-svn-id: trunk@55972 -
This commit is contained in:
juha 2017-10-04 22:43:19 +00:00
parent d6b6fe9b4b
commit 06714edf26

View File

@ -95,8 +95,8 @@ type
MainList: TStringList; MainList: TStringList;
procedure AddCommand; procedure AddCommand;
procedure AddTailItem; procedure AddTailItem;
function GetMainListIndex(aLvIndex: Integer): Integer;
procedure InsertMainListItem(Item: TListItem); procedure InsertMainListItem(Item: TListItem);
procedure MoveUpDown(aOffset: integer);
function NewLvItem(aCaption: string): TListItem; function NewLvItem(aCaption: string): TListItem;
procedure RemoveCommand; procedure RemoveCommand;
procedure RemoveMainListItem(Item: TListItem); procedure RemoveMainListItem(Item: TListItem);
@ -109,6 +109,7 @@ type
procedure AddToolBarItem(CmdItem: TIDEButtonCommand); procedure AddToolBarItem(CmdItem: TIDEButtonCommand);
procedure AddDivider; procedure AddDivider;
procedure FillToolBar; procedure FillToolBar;
procedure UpdateMainListIndices(StartIndex, Offset: integer);
public public
procedure LoadSettings(SL: TStringList); procedure LoadSettings(SL: TStringList);
procedure SaveSettings(SL: TStringList); procedure SaveSettings(SL: TStringList);
@ -199,10 +200,10 @@ begin
btnAddDivider.Caption := '---'; btnAddDivider.Caption := '---';
btnAdd.Hint := lisCoolBarAddSelected; btnAdd.Hint := lisCoolBarAddSelected;
btnRemove.Hint := lisCoolBarRemoveSelected; btnRemove.Hint := lisCoolBarRemoveSelected;
btnMoveUp.Hint := lisCoolBarMoveSelectedUp; btnMoveUp.Hint := lisCoolBarMoveSelectedUp;
btnMoveDown.Hint := lisCoolBarMoveSelectedDown; btnMoveDown.Hint := lisCoolBarMoveSelectedDown;
btnAddDivider.Hint:= lisCoolBarAddDivider; btnAddDivider.Hint:= lisCoolBarAddDivider;
TV.Images := IDEImages.Images_16; TV.Images := IDEImages.Images_16;
@ -247,14 +248,14 @@ end;
procedure TToolBarConfig.UpdateButtonsState; procedure TToolBarConfig.UpdateButtonsState;
var var
i: Integer; I: Integer;
begin begin
i:=lvToolbar.ItemIndex; I := lvToolbar.ItemIndex;
btnAdd.Enabled:=(Assigned(TV.Selected) and Assigned((TV.Selected).Data)); btnAdd.Enabled := Assigned(TV.Selected) and Assigned(TV.Selected.Data);
btnRemove.Enabled:=(ActiveControl=lvToolbar) and (i>-1) and (i<lvToolbar.Items.Count-1); btnRemove.Enabled := (I>-1) and (I<lvToolbar.Items.Count-1);
btnMoveUp.Enabled:=(ActiveControl=lvToolbar) and (i>0) and (i<lvToolbar.Items.Count-1); btnMoveUp.Enabled := (I>0) and (I<lvToolbar.Items.Count-1);
btnMoveDown.Enabled:=(ActiveControl=lvToolbar) and (i>-1) and (i<lvToolbar.Items.Count-2); btnMoveDown.Enabled := (I>-1) and (I<lvToolbar.Items.Count-2);
btnAddDivider.Enabled:=(lvToolbar.Selected = nil) or (lvToolbar.Selected.Caption <> cIDEToolbarDivider); btnAddDivider.Enabled := True;
end; end;
procedure TToolBarConfig.TVSelectionChanged(Sender: TObject); procedure TToolBarConfig.TVSelectionChanged(Sender: TObject);
@ -262,19 +263,22 @@ begin
UpdateButtonsState; UpdateButtonsState;
end; end;
function TToolBarConfig.GetMainListIndex(aLvIndex: Integer): Integer; procedure TToolBarConfig.UpdateMainListIndices(StartIndex, Offset: integer);
// Update indices of existing MainList items.
var var
I: Integer; I: Integer;
mlItem: TLvItem;
begin begin
for I:= 0 to MainList.Count -1 do for I := StartIndex to MainList.Count-1 do
if TLvItem(MainList.Objects[I]).LvIndex = aLvIndex then begin
Exit(I); mlItem := TLvItem(MainList.Objects[I]);
Result := -1; mlItem.LvIndex := mlItem.LvIndex + Offset;
end;
end; end;
procedure TToolBarConfig.InsertMainListItem(Item: TListItem); procedure TToolBarConfig.InsertMainListItem(Item: TListItem);
var var
NextInd, I: Integer; NextInd: Integer;
aMainListItem: TLvItem; aMainListItem: TLvItem;
begin begin
// New selection. Clear previous selection to avoid double sel in Qt. // New selection. Clear previous selection to avoid double sel in Qt.
@ -288,29 +292,25 @@ begin
aMainListItem := TLvItem.Create; aMainListItem := TLvItem.Create;
aMainListItem.Command := TIDEButtonCommand(Item.Data); aMainListItem.Command := TIDEButtonCommand(Item.Data);
aMainListItem.LvIndex := Item.Index; aMainListItem.LvIndex := Item.Index;
NextInd := GetMainListIndex(Item.Index); NextInd := Item.Index;
MainList.InsertObject(NextInd, Item.Caption, aMainListItem); MainList.InsertObject(NextInd, Item.Caption, aMainListItem);
// Update indices of existing MainList items. UpdateMainListIndices(NextInd+1, 1);
for I := NextInd+1 to MainList.Count -1 do
begin
aMainListItem := TLvItem(MainList.Objects[I]);
aMainListItem.LvIndex := aMainListItem.LvIndex +1;
end;
end; end;
procedure TToolBarConfig.RemoveMainListItem(Item: TListItem); procedure TToolBarConfig.RemoveMainListItem(Item: TListItem);
var var
I, J: Integer; NextInd: Integer;
aMainListItem: TLvItem;
begin begin
I := GetMainListIndex(Item.Index); NextInd := Item.Index;
if I > -1 then begin MainList.Delete(NextInd);
MainList.Delete(I); UpdateMainListIndices(NextInd, -1);
for J := I to MainList.Count -1 do begin // Remove also from associated ListView.
aMainListItem := TLvItem(MainList.Objects[J]); lvToolbar.Items.Delete(NextInd);
aMainListItem.LvIndex := aMainListItem.LvIndex -1; {$IF DEFINED(LCLQt) or DEFINED(LCLQt5)}
end; lvToolbar.ItemIndex := -1; // Try to make LCLQt behave.
end; lvToolbar.ItemIndex := NextInd;
{$ENDIF}
lvToolbar.Selected := lvToolbar.Items[NextInd];
end; end;
procedure TToolBarConfig.ExchangeMainListItem(Item1, Item2: TListItem); procedure TToolBarConfig.ExchangeMainListItem(Item1, Item2: TListItem);
@ -318,8 +318,8 @@ var
MainIndex1,MainIndex2: Integer; MainIndex1,MainIndex2: Integer;
aMainListItem: TLvItem; aMainListItem: TLvItem;
begin begin
MainIndex1:= GetMainListIndex(Item1.Index); MainIndex1:= Item1.Index;
MainIndex2:= GetMainListIndex(Item2.Index); MainIndex2:= Item2.Index;
MainList.Exchange(MainIndex1,MainIndex2); MainList.Exchange(MainIndex1,MainIndex2);
aMainListItem := TLvItem(MainList.Objects[MainIndex1]); aMainListItem := TLvItem(MainList.Objects[MainIndex1]);
aMainListItem.LvIndex:= Item1.Index; aMainListItem.LvIndex:= Item1.Index;
@ -356,18 +356,16 @@ begin
DeleteAmpersands(CmdCaption); DeleteAmpersands(CmdCaption);
lvItem := NewLvItem(CmdCaption); lvItem := NewLvItem(CmdCaption);
lvItem.Data := Node.Data; lvItem.Data := Node.Data;
{$IF not DEFINED(LCLQt) and not DEFINED(LCLQt5)}
if Node.ImageIndex > -1 then if Node.ImageIndex > -1 then
lvItem.ImageIndex := Node.ImageIndex lvItem.ImageIndex := Node.ImageIndex
else else
lvItem.ImageIndex := defImageIndex; lvItem.ImageIndex := defImageIndex;
{$ENDIF}
//lvItem.SubItems.Add(IntToStr(CurrProfile)); //lvItem.SubItems.Add(IntToStr(CurrProfile));
// Add the newly created item to ListView. // Add the newly created item to ListView.
InsertMainListItem(lvItem); InsertMainListItem(lvItem);
// Update selection in TreeView. // Update selection in TreeView.
Node := TV.Selected.GetNext; Node := TV.Selected.GetNext;
TV.Selected.Visible:= False; TV.Selected.Visible := False;
if Node <> nil then if Node <> nil then
TV.Selected := Node; TV.Selected := Node;
UpdateButtonsState; UpdateButtonsState;
@ -378,9 +376,7 @@ var
lvItem: TListItem; lvItem: TListItem;
begin begin
lvItem := NewLvItem(cIDEToolbarDivider); lvItem := NewLvItem(cIDEToolbarDivider);
{$IF not DEFINED(LCLQt) and not DEFINED(LCLQt5)}
lvItem.ImageIndex := divImageIndex; lvItem.ImageIndex := divImageIndex;
{$ENDIF}
InsertMainListItem(lvItem); InsertMainListItem(lvItem);
UpdateButtonsState; UpdateButtonsState;
end; end;
@ -392,27 +388,24 @@ end;
procedure TToolBarConfig.RemoveCommand; procedure TToolBarConfig.RemoveCommand;
Var Var
mi: TIDEButtonCommand; Cmd: TIDEButtonCommand;
n: TTreeNode; Node: TTreeNode;
I: Integer; I: Integer;
lvItem: TListItem; lvItem: TListItem;
begin begin
I := lvToolbar.ItemIndex; I := lvToolbar.ItemIndex;
if (ActiveControl=lvToolbar) and (i>-1) and (i<lvToolbar.Items.Count-1) then if (I<0) or (I>=lvToolbar.Items.Count-1) then Exit;
lvItem := lvToolbar.Items[I];
Cmd := TIDEButtonCommand(lvItem.Data);
RemoveMainListItem(lvItem);
// Show the command as available again in TreeView.
if Assigned(Cmd) then
begin begin
lvItem := lvToolbar.Items[I]; Node:= TV.Items.FindNodeWithData(Cmd);
mi := TIDEButtonCommand(lvItem.Data); if Node<>nil then
RemoveMainListItem(lvItem); Node.Visible:= True;
lvToolbar.Items.Delete(lvToolbar.ItemIndex);
if I < lvToolbar.Items.Count then
lvToolbar.Selected := lvToolbar.Items[I]; // Qt Workaround
if assigned(mi) then begin
n:= TV.Items.FindNodeWithData(mi);
if n<>nil then
n.Visible:= True;
end;
UpdateButtonsState;
end; end;
UpdateButtonsState;
end; end;
procedure TToolBarConfig.lvToolbarDrawItem(Sender: TCustomListView; procedure TToolBarConfig.lvToolbarDrawItem(Sender: TCustomListView;
@ -452,55 +445,53 @@ end;
procedure TToolBarConfig.lvToolbarSelectItem(Sender: TObject; procedure TToolBarConfig.lvToolbarSelectItem(Sender: TObject;
Item: TListItem; Selected: Boolean); Item: TListItem; Selected: Boolean);
var
RealCount: integer;
begin begin
UpdateButtonsState; UpdateButtonsState;
if lvToolbar.ItemIndex<lvToolbar.Items.Count-1 then // Update selection status label.
lblSelect.Caption:=Format('%d / %d', [lvToolbar.ItemIndex+1, lvToolbar.Items.Count-1]) RealCount := lvToolbar.Items.Count-1;
if lvToolbar.ItemIndex < RealCount then
lblSelect.Caption := Format('%d / %d', [lvToolbar.ItemIndex+1, RealCount])
else else
lblSelect.Caption:=Format('%d+ / %d', [lvToolbar.ItemIndex, lvToolbar.Items.Count-1]) lblSelect.Caption := Format('%d+ / %d', [lvToolbar.ItemIndex, RealCount])
end;
procedure TToolBarConfig.MoveUpDown(aOffset: integer);
var
Index1,Index2: Integer;
begin
Index1 := lvToolbar.ItemIndex;
Index2 := Index1 + aOffset;
lvToolbar.Items.Exchange(Index1,Index2);
ExchangeMainListItem(lvToolbar.Items[Index1],lvToolbar.Items[Index2]);
lvToolbar.Items[Index1].Selected := False;
lvToolbar.Items[Index2].Selected := False;
lvToolbar.ItemIndex:= -1;
lvToolbar.Selected := Nil;
lvToolbar.ItemIndex:= Index2;
lvToolbar.Selected := lvToolbar.Items[Index2];
end; end;
procedure TToolBarConfig.btnMoveDownClick(Sender: TObject); procedure TToolBarConfig.btnMoveDownClick(Sender: TObject);
var
Index1,Index2: Integer;
begin begin
if (ActiveControl=lvToolbar) and (lvToolbar.ItemIndex>-1) if (lvToolbar.ItemIndex<0) or (lvToolbar.ItemIndex>=lvToolbar.Items.Count-2) then
and (lvToolbar.ItemIndex<lvToolbar.Items.Count-2) then Exit;
begin MoveUpDown(1);
Index1 := lvToolbar.ItemIndex;
Index2 := Index1+1;
lvToolbar.Items.Exchange(Index1,Index2);
ExchangeMainListItem(lvToolbar.Items[Index1],lvToolbar.Items[Index2]);
lvToolbar.Items[Index1].Selected := False;
lvToolbar.Items[Index2].Selected := False;
lvToolbar.Selected := nil;
lvToolbar.ItemIndex:= Index2;
end;
end; end;
procedure TToolBarConfig.btnMoveUpClick(Sender: TObject); procedure TToolBarConfig.btnMoveUpClick(Sender: TObject);
var
Index1,Index2: Integer;
begin begin
if (ActiveControl=lvToolbar) and (lvToolbar.ItemIndex>0) if (lvToolbar.ItemIndex<1) or (lvToolbar.ItemIndex>=lvToolbar.Items.Count-1) then
and (lvToolbar.ItemIndex<lvToolbar.Items.Count-1) then Exit;
begin MoveUpDown(-1);
Index1:= lvToolbar.ItemIndex;
Index2:= Index1-1;
lvToolbar.Items.Exchange(Index1, Index2);
ExchangeMainListItem(lvToolbar.Items[Index1],lvToolbar.Items[Index2]);
lvToolbar.Items[Index1].Selected := False;
lvToolbar.Items[Index2].Selected := False;
lvToolbar.Selected := nil;
lvToolbar.ItemIndex:= Index2;
end;
end; end;
procedure TToolBarConfig.SetupCaptions; procedure TToolBarConfig.SetupCaptions;
begin begin
Caption := lisToolbarConfiguration; Caption := lisToolbarConfiguration;
lblMenuTree.Caption := lisCoolbarAvailableCommands; lblMenuTree.Caption := lisCoolbarAvailableCommands;
lblToolbar.Caption := lisCoolbarToolbarCommands; lblToolbar.Caption := lisCoolbarToolbarCommands;
end; end;
procedure TToolBarConfig.LoadCategories; procedure TToolBarConfig.LoadCategories;
@ -540,21 +531,18 @@ begin
end; end;
function TToolBarConfig.RootNodeCaption(CmdItem: TIDEButtonCommand): string; function TToolBarConfig.RootNodeCaption(CmdItem: TIDEButtonCommand): string;
var
aCaption: string;
begin begin
aCaption:= CmdItem.Caption; case CmdItem.Caption of
case aCaption of 'IDEMainMenu': Result := lisCoolbarIDEMainMenu; // mnuMain
'IDEMainMenu': Result := lisCoolbarIDEMainMenu; // mnuMain 'SourceTab': Result := lisCoolbarSourceTab; // SourceTabMenuRootName
'SourceTab': Result := lisCoolbarSourceTab; // SourceTabMenuRootName 'SourceEditor': Result := lisCoolbarSourceEditor; // SourceEditorMenuRootName
'SourceEditor': Result := lisCoolbarSourceEditor; // SourceEditorMenuRootName 'Messages': Result := lisCoolbarMessages; // MessagesMenuRootName
'Messages': Result := lisCoolbarMessages; // MessagesMenuRootName 'Code Explorer': Result := lisCoolbarCodeExplorer; // CodeExplorerMenuRootName
'Code Explorer': Result := lisCoolbarCodeExplorer; // CodeExplorerMenuRootName 'CodeTemplates': Result := lisCoolbarCodeTemplates; // CodeTemplatesMenuRootName
'CodeTemplates': Result := lisCoolbarCodeTemplates; // CodeTemplatesMenuRootName 'Designer': Result := lisCoolbarDesigner; // DesignerMenuRootName
'Designer': Result := lisCoolbarDesigner; // DesignerMenuRootName 'PackageEditor': Result := lisCoolbarPackageEditor; // PackageEditorMenuRootName
'PackageEditor': Result := lisCoolbarPackageEditor; // PackageEditorMenuRootName 'PackageEditorFiles': Result := lisCoolbarPackageEditorFiles // PackageEditorMenuFilesRootName
'PackageEditorFiles': Result := lisCoolbarPackageEditorFiles // PackageEditorMenuFilesRootName else Result := CmdItem.Caption;
else Result := aCaption;
end; end;
end; end;
@ -582,13 +570,10 @@ begin
lvItem := lvToolbar.Items.Add; lvItem := lvToolbar.Items.Add;
lvItem.Caption := CmdItem.GetCaptionWithShortCut; lvItem.Caption := CmdItem.GetCaptionWithShortCut;
lvItem.Data := CmdItem; lvItem.Data := CmdItem;
{$IF not DEFINED(LCLQt) and not DEFINED(LCLQt5)}
if CmdItem.ImageIndex > -1 then if CmdItem.ImageIndex > -1 then
lvItem.ImageIndex := CmdItem.ImageIndex lvItem.ImageIndex := CmdItem.ImageIndex
else else
lvItem.ImageIndex := defImageIndex; lvItem.ImageIndex := defImageIndex;
{$ENDIF}
// lvItem.SubItems.Add(IntToStr(PMask));
Node := TV.Items.FindNodeWithData(CmdItem); Node := TV.Items.FindNodeWithData(CmdItem);
if Node<>nil then if Node<>nil then
Node.Visible := False; Node.Visible := False;
@ -599,11 +584,8 @@ var
lvItem: TListItem; lvItem: TListItem;
begin begin
lvItem := lvToolbar.Items.Add; lvItem := lvToolbar.Items.Add;
lvItem.Caption:= cIDEToolbarDivider; lvItem.Caption := cIDEToolbarDivider;
{$IF not DEFINED(LCLQt) and not DEFINED(LCLQt5)} lvItem.ImageIndex := divImageIndex;
lvItem.ImageIndex:= divImageIndex;
{$ENDIF}
// lvItem.SubItems.Add(IntToStr(PMask));
end; end;
procedure TToolBarConfig.AddTailItem; procedure TToolBarConfig.AddTailItem;
@ -788,7 +770,7 @@ end;
procedure TIDEToolbarBase.CopyFromOptions(Options: TIDEToolBarOptionsBase); procedure TIDEToolbarBase.CopyFromOptions(Options: TIDEToolBarOptionsBase);
var var
mi: TIDEButtonCommand; Cmd: TIDEButtonCommand;
ButtonName: string; ButtonName: string;
i: Integer; i: Integer;
begin begin
@ -801,10 +783,10 @@ begin
AddDivider AddDivider
else else
begin begin
mi := IDEToolButtonCategories.FindItemByMenuPathOrName(ButtonName); Cmd := IDEToolButtonCategories.FindItemByMenuPathOrName(ButtonName);
Options.ButtonNames[i] := ButtonName; Options.ButtonNames[i] := ButtonName;
if Assigned(mi) then if Assigned(Cmd) then
AddButton(mi); AddButton(Cmd);
end; end;
end; end;
PostCopyOptions; PostCopyOptions;