Page 1 of 1

Collections Editor + & -

PostPosted: Thu May 29, 2014 8:53 am
by amarryat
Is there a way to hide/disable the ability to add and remove collection items in the collections editor? We have a fixed list of items and don't want the user to be able to change the number of items in the collection.

Also, is there a way to disable the + button when a limit is reached, such as we want to limit the number of traces to 15 for example, so the button should become dark once that limit is reached.

Thanks.

Re: Collections Editor + & -

PostPosted: Sat May 31, 2014 8:17 am
by Inspex
Hi,
Yes, it is possible to create a TCollection editor preset that only allows a limited number of items, though it might not be as easy as you had hoped. Basically, what you want to do is create another TIxTCollectionPropertyPreset and have it call the IxTCollectionEditor.ShowCollectionEditor function. This function will show the collection editor and return you a pointer to that form. Then, all you have to do is change the Add and Delete Action to be visible/not visible and/or enable/disable them as you need.

After you create a TIxTCollectionPropertyPreset, make sure it matches the property you want it to edit. Then, you would add the following code:

Code: Select all
uses
  IxTCollectionEditor;

type
  TfrmIxCollectionEditorHack = class(TfrmIxCollectionEditor);

procedure TForm40.AddItem1Update(Sender: TObject);
var
   Ed: TfrmIxCollectionEditor;
begin
  Ed := (Sender as TAction).Owner as TfrmIxCollectionEditor;
  (Sender as TAction).Enabled := (Ed.Collection <> nil) and (Ed.Collection.Count < 4);
end;

procedure TForm40.DeleteSelected1Update(Sender: TObject);
var
   Ed: TfrmIxCollectionEditor;
begin
  Ed := (Sender as TAction).Owner as TfrmIxCollectionEditor;
  (Sender as TAction).Enabled := (TfrmIxCollectionEditorHack(Ed).Objects <> nil) and (Ed.Collection <> nil)
    and (Ed.ListView1.SelCount > 0);
end;

procedure TForm40.IxObjectInspector1Presets20EditButtonClick(Sender: TObject);
var
   Item: TIxItemProperty;
   Ed: TfrmIxCollectionEditor;
begin
     if not (Sender is TIxItemProperty) then Exit;
     Item := TIxItemProperty(Sender);
     if IxObjectInspector1.Component <> nil then
       Ed := ShowCollectionEditor( IxObjectInspector1, Item.GetObjectProperty as TCollection,
                           IxObjectInspector1.Component.Name +
                           IxObjectInspector1.PathDelimiter +
                           Item.FullPathName )
     else
       Ed := ShowCollectionEditor( IxObjectInspector1, Item.GetObjectProperty as TCollection,
                           Item.FullPathName );
     // Set the Add and Delete actions to not visible if you don't want user to
     // see them at all
     //Ed.AddItem1.Visible := False;
     //Ed.DeleteSelected1.Visible := False;
     // Alternatively, you can override the Add and Delete actions to only be
     //  enabled when you want
     Ed.AddItem1.OnUpdate := AddItem1Update;
     Ed.DeleteSelected1.OnUpdate := DeleteSelected1Update;
end;



Regards,
Tom

Re: Collections Editor + & -

PostPosted: Tue Jun 10, 2014 3:16 pm
by amarryat
Thanks for the detailed answer!

Re: Collections Editor + & -

PostPosted: Tue Jun 10, 2014 5:49 pm
by Inspex
amarryat wrote:Thanks for the detailed answer!

You're quite welcome. :)
Tom

Re: Collections Editor + & -

PostPosted: Wed Jun 11, 2014 3:39 pm
by amarryat
This is doing the trick for me, but I have 2 comments/questions.

1). I have disabled the popupmenu in the collection editor by setting ListView1.PopupMenu=nil. If I use the popupmenu and select "Add New", I get an access violation. Maybe I have done something wrong, otherwise this looks like a bug.

2). When you are in a collection editor, how do you make the object inspector 'back out' to the previous level when you close the collection editor? I'm having to deselect my object and then select it again for the object inspector to reset.

Re: Collections Editor + & -

PostPosted: Thu Jun 12, 2014 8:32 am
by Inspex
amarryat wrote:This is doing the trick for me, but I have 2 comments/questions.

1). I have disabled the popupmenu in the collection editor by setting ListView1.PopupMenu=nil. If I use the popupmenu and select "Add New", I get an access violation. Maybe I have done something wrong, otherwise this looks like a bug.

I can confirm this is a bug. I have a fix for it. Until there is a point release, you can fix the bug by editing the IxTCollectionEditor.pas:
Code: Select all
procedure TfrmIxCollectionEditor.AddItem(AClass: TCollectionItemClass);
var
   Item: TCollectionItem;
begin
     DeselectObjects;
     if AClass = nil then
       Item := Collection.Add
     else
       // Tag contains ClassName
       Item := AClass.Create(Collection);
     ListView1.Selected := nil;
     RefreshView;
     SelectItem(Item);
end;


amarryat wrote:2). When you are in a collection editor, how do you make the object inspector 'back out' to the previous level when you close the collection editor? I'm having to deselect my object and then select it again for the object inspector to reset.

Yes, that is what you have to do.

Regards,
Tom