Recent

Author Topic: Saving a Form state (related to separating ui from logic)  (Read 2261 times)

Curt Carpenter

  • Hero Member
  • *****
  • Posts: 559
Saving a Form state (related to separating ui from logic)
« on: September 15, 2023, 10:22:59 pm »
I have a form representing an instrument control panel.  The virtual panel contains a significant number of different lazarus visual controls (sliders, switches, edit boxes, combo boxes etc.).  Is there a good way to take a "snapshot" of the state of all of those visual controls and store them to disk so that they can be read back from disk at a later time and restored to the form?  This is a routine operation not difficult to build into a procedure,  but writing the procedure is routine and tedious. Is there something like a "TSaveDialog" that saves the state of the controls on a form ("TSaveFormState" perhaps) rather than a file?

jamie

  • Hero Member
  • *****
  • Posts: 6734
Re: Saving a Form state (related to separating ui from logic)
« Reply #1 on: September 15, 2023, 11:22:29 pm »
Look at the "SessionsProperty" in the form.
The only true wisdom is knowing you know nothing

Curt Carpenter

  • Hero Member
  • *****
  • Posts: 559
Re: Saving a Form state (related to separating ui from logic)
« Reply #2 on: September 16, 2023, 01:59:19 am »
A starting point.   Thank you.

cdbc

  • Hero Member
  • *****
  • Posts: 1646
    • http://www.cdbc.dk
Re: Saving a Form state (related to separating ui from logic)
« Reply #3 on: September 16, 2023, 02:28:13 am »
Hi
Maybe use RTTI to save all the published properties, of all the controls on the form to an ...xml-file?!? Lends itself, on the account of structuring, layout etc...
Just an idea  8-)
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

Curt Carpenter

  • Hero Member
  • *****
  • Posts: 559
Re: Saving a Form state (related to separating ui from logic)
« Reply #4 on: September 21, 2023, 06:02:14 pm »
Amazing -- and works like a charm.  Decided to use the JSONProperties storage rather than XML.   Many thanks!

cdbc

  • Hero Member
  • *****
  • Posts: 1646
    • http://www.cdbc.dk
Re: Saving a Form state (related to separating ui from logic)
« Reply #5 on: September 21, 2023, 06:17:27 pm »
Hi
Cool  8-) Good on you mate  ;)
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

egsuh

  • Hero Member
  • *****
  • Posts: 1489
Re: Saving a Form state (related to separating ui from logic)
« Reply #6 on: September 26, 2023, 04:54:44 am »
You alrealy have the answer. I'm just adding.

I have defined "class", which contains the necessary fields and published properties, and forms have RTTI controls. In that way, data are controled within the "class" --- both retrieving from and saving to your storage (a file, database, whatever).

jcmontherock

  • Sr. Member
  • ****
  • Posts: 270
Re: Saving a Form state (related to separating ui from logic)
« Reply #7 on: September 26, 2023, 11:22:52 am »
Curt, could you give us a little example of JSONProperties storage for saving form ?
Windows 11 UTF8-64 - Lazarus 4RC1-64 - FPC 3.2.2

Curt Carpenter

  • Hero Member
  • *****
  • Posts: 559
Re: Saving a Form state (related to separating ui from logic)
« Reply #8 on: September 26, 2023, 03:50:01 pm »
With some stuff deleted for clarity and TJSONPropStorage components from the MISC palette placed on two different forms, my use looks like this:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.BSaveControlsClick(Sender: TObject);
  2. begin
  3.   if (SaveDialog1.Execute) then
  4.    begin
  5.     FMain.JSONPropMain.JSONFileName :=  SaveDialog1.FileName+'.1'{Main form};
  6.     FMain.JSONPropMain.Save;
  7.     FSampling.JSONPropSampling.JSONFileName := SaveDialog1.FileName+'.2'{Sampling controls};
  8.     FSampling.JSONPropSampling.Save;
  9.    end;
  10. end;
  11.  
  12. procedure TForm1.BLoadControlsClick(Sender: TObject);
  13. begin
  14.   if (OpenDialog1.Execute) then
  15.     FMain.JSONPropMain.JSONFileName :=  OpenDialog1.FileName+'.1'; {Main form}
  16.     FMain.JSONPropMain.Restore;
  17.     FSampling.JSONPropSampling.JSONFileName := OpenDialog1.FileName+'.2'; {Sampling Controls}
  18.     FSampling.JSONPropSampling.Restore;
  19.    end;
  20. end;  

Each pair of ".1" and ".2" files contains a different setup for an instrument I'm working on.

I'm using Linux, so I also get the file that is automatically created (in my home directory) each time I close my project that combines the JSON image of both of my forms. This feature is causing me some problems that I'm still working on.  Hope that makes sense and is helpful.

jcmontherock

  • Sr. Member
  • ****
  • Posts: 270
Re: Saving a Form state (related to separating ui from logic)
« Reply #9 on: September 28, 2023, 04:51:39 pm »
Nice, thanks a lot. :D :D

Could you give us more details about the definition of:
FMain
FSampling
and which unit should we add in uses definitions ?
« Last Edit: September 28, 2023, 05:29:03 pm by jcmontherock »
Windows 11 UTF8-64 - Lazarus 4RC1-64 - FPC 3.2.2

Curt Carpenter

  • Hero Member
  • *****
  • Posts: 559
Re: Saving a Form state (related to separating ui from logic)
« Reply #10 on: September 28, 2023, 08:10:45 pm »
Yes, sorry.

My project includes two forms:  One I've named "FMain" and one named "FSampling." 

Each of those forms contains a TJSONPropertyStorage component (dropped on it from the "Misc" section of the Lazarus component palette). 

On the FMain form, I've named the TJSONPropertyStorage component "JSONPropMain."  On the FSampling form, it is named "JSONPropSampling."

JSONFileName is a property of the TJSONPropertyStorage component.  .Save and .Restore are component methods. 

Placing a TJSONPropertyStorage component on the form causes a JSONPropStorage clause to be added to the form's USES statement.

More details are here: https://lazarus-ccr.sourceforge.io/docs/lcl/jsonpropstorage/tjsonpropstorage.html 

PS:  This is my first experience with this component, so I'm by no means an expert with it.

« Last Edit: September 29, 2023, 12:55:26 am by Curt Carpenter »

jcmontherock

  • Sr. Member
  • ****
  • Posts: 270
Re: Saving a Form state (related to separating ui from logic)
« Reply #11 on: September 29, 2023, 06:00:37 pm »
I will try it. Thanks a lot.
Windows 11 UTF8-64 - Lazarus 4RC1-64 - FPC 3.2.2

Thaddy

  • Hero Member
  • *****
  • Posts: 16151
  • Censorship about opinions does not belong here.
Re: Saving a Form state (related to separating ui from logic)
« Reply #12 on: September 29, 2023, 06:23:29 pm »
All a bit strange, because saving a form state comes as standard.
Simply study the form streaming mechanism, which is what you want.
Also note your own properties can have the stored modifier.
Storing form state dates even from Delphi 1.
There are multiple examples, also for Lazarus and on this forum, on how to do that.
It is basic..... and some of the answers are simply annoying.  >:D

Any component state can be saved for its published properties by just using the provided streaming mechanisims.

Of course reading back simply uses the same mechanism.
Again, all of this is plain standard Lazarus-Delphi standard.
« Last Edit: September 29, 2023, 06:33:15 pm by Thaddy »
If I smell bad code it usually is bad code and that includes my own code.

Curt Carpenter

  • Hero Member
  • *****
  • Posts: 559
Re: Saving a Form state (related to separating ui from logic)
« Reply #13 on: September 29, 2023, 07:46:53 pm »
It's a little more complicated that just saving a form -- but thanks for your standard obnoxious input anyway.
Have a nice day!

 

TinyPortal © 2005-2018