Merge branch 'Designer/Editors/Collection/Hooks' into 'main'

IdeIntf: Notify on addition/removal of items in the Collection property editor. Issue #41586

See merge request freepascal.org/lazarus/lazarus!461
This commit is contained in:
n7800 2025-04-12 17:06:40 +00:00
commit 1d71dd9d24

View File

@ -111,11 +111,13 @@ begin
end; end;
procedure TCollectionPropertyEditorForm.actAddExecute(Sender: TObject); procedure TCollectionPropertyEditorForm.actAddExecute(Sender: TObject);
var
lItem: TCollectionItem;
begin begin
if Collection = nil then Exit; if Collection = nil then Exit;
Collection.Add; lItem := Collection.Add;
// notify about addition (this also call its own handler)
FillCollectionListBox; GlobalDesignHook.PersistentAdded(lItem, true);
if CollectionListBox.Items.Count > 0 then if CollectionListBox.Items.Count > 0 then
CollectionListBox.ItemIndex := CollectionListBox.Items.Count - 1; CollectionListBox.ItemIndex := CollectionListBox.Items.Count - 1;
SelectInObjectInspector(True); SelectInObjectInspector(True);
@ -128,6 +130,7 @@ procedure TCollectionPropertyEditorForm.actDelExecute(Sender: TObject);
var var
I : Integer; I : Integer;
NewItemIndex: Integer; NewItemIndex: Integer;
lItem: TCollectionItem;
begin begin
if Collection = nil then Exit; if Collection = nil then Exit;
@ -158,10 +161,9 @@ begin
//debugln('TCollectionPropertyEditorForm.DeleteClick A NewItemIndex=',dbgs(NewItemIndex),' ItemIndex=',dbgs(CollectionListBox.ItemIndex),' CollectionListBox.Items.Count=',dbgs(CollectionListBox.Items.Count),' Collection.Count=',dbgs(Collection.Count)); //debugln('TCollectionPropertyEditorForm.DeleteClick A NewItemIndex=',dbgs(NewItemIndex),' ItemIndex=',dbgs(CollectionListBox.ItemIndex),' CollectionListBox.Items.Count=',dbgs(CollectionListBox.Items.Count),' Collection.Count=',dbgs(Collection.Count));
// unselect all items in OI (collections can act strange on delete) // unselect all items in OI (collections can act strange on delete)
ClearSelectionInObjectInspector; ClearSelectionInObjectInspector;
// now delete // now delete (this also call its own handler)
Collection.Items[I].Free; lItem := Collection.Items[I];
// update listbox after whatever happened GlobalDesignHook.DeletePersistent(TPersistent(lItem));
FillCollectionListBox;
// set NewItemIndex // set NewItemIndex
if NewItemIndex < CollectionListBox.Items.Count then if NewItemIndex < CollectionListBox.Items.Count then
begin begin
@ -264,14 +266,18 @@ end;
procedure TCollectionPropertyEditorForm.PersistentDeleting(APersistent: TPersistent); procedure TCollectionPropertyEditorForm.PersistentDeleting(APersistent: TPersistent);
begin begin
// For some reason this is called only when the whole collection is deleted, if APersistent = OwnerPersistent then
// for example when changing to another project. Thus clear the whole collection. begin
DebugLn(['TCollectionPropertyEditorForm.PersistentDeleting: APersistent=', APersistent, SetCollection(nil, nil, '');
', OwnerPersistent=', OwnerPersistent]); end
SetCollection(nil, nil, ''); else if APersistent is TCollectionItem then
Hide; begin
UpdateButtons; if TCollectionItem(APersistent).Collection = Collection then
UpdateCaption; begin
TCollectionItem(APersistent).Collection := nil;
FillCollectionListBox;
end;
end;
end; end;
procedure TCollectionPropertyEditorForm.RefreshPropertyValues; procedure TCollectionPropertyEditorForm.RefreshPropertyValues;