Recent

Author Topic: Read-only TTIPropertyGrid [SOLVED]  (Read 353 times)

hdrz

  • New Member
  • *
  • Posts: 11
Read-only TTIPropertyGrid [SOLVED]
« on: March 29, 2020, 01:32:53 pm »
Hi all,

I have a program where I have a collection of objects with published properties. The properties are read+write, mainly because they are streamed to file at program load and exit time.
I want to also present to the user a single object in a RTTI property grid, but it should be read only, and if possible to disable the grid editors completely. Is there a way to achieve this?

Thanks!
« Last Edit: April 03, 2020, 02:29:27 pm by hdrz »

jamie

  • Hero Member
  • *****
  • Posts: 2901
Re: Read-only TTIPropertyGrid
« Reply #1 on: March 29, 2020, 01:39:58 pm »
if using a TstringGrid look in the OPTIONS property.  ensure goEditing is not   set..

etc..

 Also you maybe interested in using the TvalueListEditor which is a grid of only two columns, Key and Value etc.

Number 1 at blue screen app creations!

guest58172

  • Guest
Re: Read-only TTIPropertyGrid
« Reply #2 on: March 29, 2020, 02:18:45 pm »
There are several ways to do that but none is optimal.

1. register custom editor classes that reject the changes, by overriding isReadOnly().
2. set the "Enabled" property to false. Note that then you loose the ability scroll.
3. in your setters, ie the setters of the properties that are inspected reject the changes if they are made from the inspector, e.g use a global.

I would go for solution 1 although this requires to write a bit of code.
Example for integers:

Code: Pascal  [Select][+][-]
  1.   TReadOnlyIntegerEditor = class(TIntegerPropertyEditor)
  2.     function IsReadOnly: boolean; override;
  3.   end;  
  4.  
  5. function TReadOnlyIntegerEditor.IsReadOnly: boolean;
  6. begin
  7.   result := true;
  8. end;
  9.  
  10. initialization
  11.   RegisterPropertyEditor(TypeInfo(integer), nil, '', TReadOnlyIntegerEditor);
  12. end.
  13.  

and you need to do that for every type presents in your objects that are set as TIObject of the grid. Maybe use the generics to make this less repetitive.

jamie

  • Hero Member
  • *****
  • Posts: 2901
Re: Read-only TTIPropertyGrid
« Reply #3 on: March 29, 2020, 02:24:43 pm »
I guess it depends on how the data is going to be viewed.

 if you want a tree like view which maybe the case here then a TTreeView in read mode only is a good idea..

 Basically use the same editor as would be in Edit mode and just set the read only flag on it...

 I've already made such an editor for a HMI project that can display data only or allow editing too, of class properties etc..
Number 1 at blue screen app creations!

guest58172

  • Guest
Re: Read-only TTIPropertyGrid
« Reply #4 on: March 29, 2020, 02:33:01 pm »
there is no such flags unless I miss them (?). The grid uses the read/write property attributes and he cant change them as OP stream and destream the props.

jamie

  • Hero Member
  • *****
  • Posts: 2901
Re: Read-only TTIPropertyGrid
« Reply #5 on: March 29, 2020, 03:03:16 pm »
what ?

stringgrids have an option set you can disable editing..

I read it to be a READ only case...

what ever....


  Personally I would just use a TDrawGrid and custom display it...

what ever...
Number 1 at blue screen app creations!

guest58172

  • Guest
Re: Read-only TTIPropertyGrid
« Reply #6 on: March 29, 2020, 03:14:46 pm »
OP uses a property grid, which has not read only prop. By the way can someone test this bug (https://bugs.freepascal.org/view.php?id=36843)on FPC trunk ? This bug would make the workaround to have all props readonly less practicable.

jamie

  • Hero Member
  • *****
  • Posts: 2901
Re: Read-only TTIPropertyGrid
« Reply #7 on: March 29, 2020, 03:44:53 pm »
That's the fault of the coder, if you are going to build something then do it using standard controls.

Personally I've looked at those classes because I was going to use them for my own property editor and found to many issues that would hamper my operation.

  Those classes don't get used much out side of what they were originally created for so I wouldn't wrap a lot of code around them..


I suppose if you answer the call on the OnKeyPress and set it to #0 this more or less stops that..

but it does not stop a paste operation
« Last Edit: March 29, 2020, 03:51:14 pm by jamie »
Number 1 at blue screen app creations!

howardpc

  • Hero Member
  • *****
  • Posts: 3412
Re: Read-only TTIPropertyGrid
« Reply #8 on: March 29, 2020, 03:50:44 pm »
@jamie   OP writes about TIPropertyGrid, not TIGrid.

I tried this:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.TIPropertyGrid1Modified(Sender: TObject);
  2. var
  3.   grid: TTIPropertyGrid absolute Sender;
  4. begin
  5.   grid.GetActiveRow.Editor.Revert;
  6. end;

which compiles but has no effect, or at least not the desired effect.
I think for a simple one-shot solution for read-only property editors using an existing component you have to use a TIGrid with Options flags goEditing and goAlwaysShowEditor set to False as jamie proposes.
« Last Edit: March 29, 2020, 03:52:18 pm by howardpc »

hdrz

  • New Member
  • *
  • Posts: 11
Re: Read-only TTIPropertyGrid
« Reply #9 on: March 29, 2020, 04:07:37 pm »
Thank you all for taking the time read my question and respond.

@jamie - yes I try to avoid needless coding if possible, that's the reason for using RTTI widgets.

@howardpc - Thanks, I'll try TIGrid and see if it can be customized to fit my needs.

any more suggestions will be appreciated.

hdrz

  • New Member
  • *
  • Posts: 11
Re: Read-only TTIPropertyGrid [SOLVED]
« Reply #10 on: April 03, 2020, 02:28:41 pm »
OK, solved this using a TTIGrid as @howardpc suggested. On runtime I create a new collection, add only one collectionitem, then create the grid and set objects-as-cols, and assign the collection to the grid. Done!

Thanks.

 

TinyPortal © 2005-2018