Browse Source

Quick.Config fpc samples updated

Unknown 6 years ago
parent
commit
01a7504051

+ 21 - 1
samples/fpc/QuickConfig/ConfigToFile/Config.json

@@ -1,11 +1,31 @@
 {
      "Title" :  "hola",
      "SessionName" :  "Session01",
+     "Sizes" :  [
+        1,
+         2,
+         3,
+         4,
+         5,
+         6,
+         7
+    ],
      "LastFilename" :  "C:\\library.txt",
+     "WindowPos" :  {
+         "PosX" :  100,
+         "PosY" :  200 
+    },
+     "History" :  [
+        {
+             "Id" :  1,
+             "Priority" :  "msLow",
+             "Redundant" :  true 
+        }
+    ],
      "Complex" :  {
          "Id" :  1,
          "Priority" :  "msMed",
          "Redundant" :  true 
     },
-     "ModifyDate" :  "2019-01-21T23:10:54.785Z " 
+     "ModifyDate" :  "2019-01-24T23:59:38.138Z" 
 }

+ 4 - 1
samples/fpc/QuickConfig/ConfigToFile/QuickConfig.lpi

@@ -65,7 +65,7 @@
     </Linking>
   </CompilerOptions>
   <Debugging>
-    <Exceptions Count="3">
+    <Exceptions Count="4">
       <Item1>
         <Name Value="EAbort"/>
       </Item1>
@@ -75,6 +75,9 @@
       <Item3>
         <Name Value="EFOpenError"/>
       </Item3>
+      <Item4>
+        <Name Value="Exception"/>
+      </Item4>
     </Exceptions>
   </Debugging>
 </CONFIG>

BIN
samples/fpc/QuickConfig/ConfigToFile/lib/i386-win32/QuickConfig.or


+ 1 - 0
samples/fpc/QuickConfig/ConfigToFile/lib/i386-win32/umain.lfm

@@ -23,6 +23,7 @@ object Form1: TForm1
     Height = 25
     Top = 424
     Width = 123
+    Cancel = True
     Caption = 'Load From Json'
     OnClick = btnLoadJsonClick
     TabOrder = 1

+ 1 - 0
samples/fpc/QuickConfig/ConfigToFile/umain.lfm

@@ -23,6 +23,7 @@ object Form1: TForm1
     Height = 25
     Top = 424
     Width = 123
+    Cancel = True
     Caption = 'Load From Json'
     OnClick = btnLoadJsonClick
     TabOrder = 1

+ 15 - 12
samples/fpc/QuickConfig/ConfigToFile/umain.pas

@@ -53,6 +53,7 @@ type
   end;
 
   TArraySizes = array of Integer;
+  TArrayHistory = array of TProcessType;
 
   TMyConfig = class(TAppConfigJson)
   private
@@ -62,10 +63,10 @@ type
     fSizes : TArraySizes;
     fLastFilename : string;
     fWindowPos : TWinPos;
-    fHistory : TArray<TProcessType>;
+    fHistory : TArrayHistory;
     fComplex : TProcessType;
     fModifyDate : TDateTime;
-    fWorkList : TObjectList<TWorker>;
+    //fWorkList : TObjectList<TWorker>;
   public
     constructor Create;
     destructor Destroy; override;
@@ -76,8 +77,8 @@ type
     property SessionName : string read fSessionName write fSessionName;
     property Sizes : TArraySizes read fSizes write fSizes;
     property LastFilename : string read fLastFilename write fLastFilename;
-    //property WindowPos : TWinPos read fWindowPos write fWindowPos;
-    property History : TArray<TProcessType> read fHistory write fHistory;
+    property WindowPos : TWinPos read fWindowPos write fWindowPos;
+    property History : TArrayHistory read fHistory write fHistory;
     property Complex : TProcessType read fComplex write fComplex;
     property ModifyDate : TDateTime read fModifyDate write fModifyDate;
     //property WorkList : TObjectList<TWorker> read fWorkList write fWorkList;
@@ -127,7 +128,6 @@ end;
 procedure TForm1.btnLoadJsonClick(Sender: TObject);
 begin
   meInfo.Lines.Add('Load ConfigJson');
-  ConfigJson := TMyConfig.Create;
   ConfigJson.Load;
   meInfo.Lines.Add(ConfigJson.ToJSON);
   if TestConfig(ConfigTest,ConfigJson) then meInfo.Lines.Add('Test passed successfully!');
@@ -141,8 +141,8 @@ begin
     Assert(cConfig1.LastFilename = cConfig2.LastFilename);
     for i := Low(cConfig1.Sizes) to High(cConfig1.Sizes) do
       Assert(cConfig1.Sizes[i] = cConfig2.Sizes[i]);
-    //Assert(cConfig1.WindowPos.PosX = cConfig2.WindowPos.PosX);
-    //Assert(cConfig1.WindowPos.PosX = cConfig2.WindowPos.PosX);
+    Assert(cConfig1.WindowPos.PosX = cConfig2.WindowPos.PosX);
+    Assert(cConfig1.WindowPos.PosX = cConfig2.WindowPos.PosX);
     Assert(cConfig1.Complex.Priority = cConfig2.Complex.Priority);
     Assert(cConfig1.Complex.Redundant  = cConfig2.Complex.Redundant);
     Assert(cConfig1.Title = cConfig2.Title);
@@ -189,11 +189,14 @@ begin
   cConfig.Complex.Id := 1;
   cConfig.Complex.Redundant := True;
   cConfig.Complex.Priority := TMyPriority.msMed;
-  //processtype := TProcessType.Create;
-  //processtype.Id := 1;
-  //processtype.Priority := msLow;
-  //processtype.Redundant := True;
-  //cConfig.History := [processtype];
+  cConfig.WindowPos := TWinPos.Create;
+  cConfig.WindowPos.PosX := 100;
+  cConfig.WindowPos.PosY := 200;
+  processtype := TProcessType.Create;
+  processtype.Id := 1;
+  processtype.Priority := msLow;
+  processtype.Redundant := True;
+  cConfig.History := [processtype];
   cConfig.ModifyDate := Now();
 end;
 

BIN
samples/fpc/QuickConfig/ConfigToRegistry/QuickConfig.ico


+ 83 - 0
samples/fpc/QuickConfig/ConfigToRegistry/QuickConfig.lpi

@@ -0,0 +1,83 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<CONFIG>
+  <ProjectOptions>
+    <Version Value="11"/>
+    <PathDelim Value="\"/>
+    <General>
+      <SessionStorage Value="InProjectDir"/>
+      <MainUnit Value="0"/>
+      <Title Value="QuickConfig"/>
+      <Scaled Value="True"/>
+      <ResourceType Value="res"/>
+      <UseXPManifest Value="True"/>
+      <XPManifest>
+        <DpiAware Value="True"/>
+      </XPManifest>
+      <Icon Value="0"/>
+    </General>
+    <BuildModes Count="1">
+      <Item1 Name="Default" Default="True"/>
+    </BuildModes>
+    <PublishOptions>
+      <Version Value="2"/>
+    </PublishOptions>
+    <RunParams>
+      <FormatVersion Value="2"/>
+      <Modes Count="0"/>
+    </RunParams>
+    <RequiredPackages Count="1">
+      <Item1>
+        <PackageName Value="LCL"/>
+      </Item1>
+    </RequiredPackages>
+    <Units Count="2">
+      <Unit0>
+        <Filename Value="QuickConfig.pas"/>
+        <IsPartOfProject Value="True"/>
+      </Unit0>
+      <Unit1>
+        <Filename Value="umain.pas"/>
+        <IsPartOfProject Value="True"/>
+        <ComponentName Value="Form1"/>
+        <HasResources Value="True"/>
+        <ResourceBaseClass Value="Form"/>
+        <UnitName Value="uMain"/>
+      </Unit1>
+    </Units>
+  </ProjectOptions>
+  <CompilerOptions>
+    <Version Value="11"/>
+    <PathDelim Value="\"/>
+    <Target>
+      <Filename Value="QuickConfig"/>
+    </Target>
+    <SearchPaths>
+      <IncludeFiles Value="$(ProjOutDir)"/>
+      <OtherUnitFiles Value="..\..\..\.."/>
+      <UnitOutputDirectory Value="lib\$(TargetCPU)-$(TargetOS)"/>
+    </SearchPaths>
+    <Linking>
+      <Options>
+        <Win32>
+          <GraphicApplication Value="True"/>
+        </Win32>
+      </Options>
+    </Linking>
+  </CompilerOptions>
+  <Debugging>
+    <Exceptions Count="4">
+      <Item1>
+        <Name Value="EAbort"/>
+      </Item1>
+      <Item2>
+        <Name Value="ECodetoolError"/>
+      </Item2>
+      <Item3>
+        <Name Value="EFOpenError"/>
+      </Item3>
+      <Item4>
+        <Name Value="Exception"/>
+      </Item4>
+    </Exceptions>
+  </Debugging>
+</CONFIG>

+ 14 - 0
samples/fpc/QuickConfig/ConfigToRegistry/QuickConfig.pas

@@ -0,0 +1,14 @@
+program QuickConfig;
+
+uses
+  Forms, Interfaces,
+  umain;
+
+{$R *.res}
+
+begin
+  Application.Scaled:=True;
+  Application.Initialize;
+  Application.CreateForm(TForm1, Form1);
+  Application.Run;
+end.

BIN
samples/fpc/QuickConfig/ConfigToRegistry/QuickConfig.res


BIN
samples/fpc/QuickConfig/ConfigToRegistry/lib/i386-win32/QuickConfig.or


BIN
samples/fpc/QuickConfig/ConfigToRegistry/lib/i386-win32/QuickConfig.res


+ 39 - 0
samples/fpc/QuickConfig/ConfigToRegistry/lib/i386-win32/umain.lfm

@@ -0,0 +1,39 @@
+object Form1: TForm1
+  Left = 379
+  Height = 457
+  Top = 208
+  Width = 592
+  Caption = 'Form1'
+  ClientHeight = 457
+  ClientWidth = 592
+  OnClose = FormClose
+  OnCreate = FormCreate
+  LCLVersion = '1.9.0.0'
+  object btnSaveJson: TButton
+    Left = 448
+    Height = 25
+    Top = 424
+    Width = 120
+    Caption = 'Save to Registry'
+    OnClick = btnSaveJsonClick
+    TabOrder = 0
+  end
+  object btnLoadJson: TButton
+    Left = 312
+    Height = 25
+    Top = 424
+    Width = 123
+    Caption = 'Load From Registry'
+    OnClick = btnLoadJsonClick
+    TabOrder = 1
+  end
+  object meInfo: TMemo
+    Left = 19
+    Height = 407
+    Top = 9
+    Width = 549
+    ReadOnly = True
+    ScrollBars = ssAutoBoth
+    TabOrder = 2
+  end
+end

+ 39 - 0
samples/fpc/QuickConfig/ConfigToRegistry/umain.lfm

@@ -0,0 +1,39 @@
+object Form1: TForm1
+  Left = 379
+  Height = 457
+  Top = 208
+  Width = 592
+  Caption = 'Form1'
+  ClientHeight = 457
+  ClientWidth = 592
+  OnClose = FormClose
+  OnCreate = FormCreate
+  LCLVersion = '1.9.0.0'
+  object btnSaveJson: TButton
+    Left = 448
+    Height = 25
+    Top = 424
+    Width = 120
+    Caption = 'Save to Registry'
+    OnClick = btnSaveJsonClick
+    TabOrder = 0
+  end
+  object btnLoadJson: TButton
+    Left = 312
+    Height = 25
+    Top = 424
+    Width = 123
+    Caption = 'Load From Registry'
+    OnClick = btnLoadJsonClick
+    TabOrder = 1
+  end
+  object meInfo: TMemo
+    Left = 19
+    Height = 407
+    Top = 9
+    Width = 549
+    ReadOnly = True
+    ScrollBars = ssAutoBoth
+    TabOrder = 2
+  end
+end

+ 216 - 0
samples/fpc/QuickConfig/ConfigToRegistry/umain.pas

@@ -0,0 +1,216 @@
+unit uMain;
+
+{$mode delphi}{$H+}
+
+interface
+
+uses
+  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls,
+  {$IFDEF FPC}
+  registry,
+  {$ENDIF}
+  Quick.Config.Registry,
+  Generics.Collections;
+
+type
+
+  TMyPriority = (msLow, msMed, msHigh);
+
+  TWinPos = class
+  private
+    fPosX : Integer;
+    fPosY : Integer;
+  published
+    property PosX : Integer read fPosX write fPosX;
+    property PosY : Integer read fPosY write fPosY;
+  end;
+
+  TProcessType = class
+  private
+    fId : Integer;
+    fPriority : TMyPriority;
+    fRedundant : Boolean;
+  published
+    property Id : Integer read fId write fId;
+    property Priority : TMyPriority read fPriority write fPriority;
+    property Redundant : Boolean read fRedundant write fRedundant;
+  end;
+
+  TWorker = class
+  private
+    fName : string;
+    fActive : Boolean;
+  published
+    property Name : string read fName write fName;
+    property Active : Boolean read fActive write fActive;
+  end;
+
+  TMyConfig2 = class(TAppConfigRegistry)
+  private
+    fhola : Integer;
+  published
+    property hola : Integer read fhola write fhola;
+  end;
+
+  TArraySizes = array of Integer;
+  TArrayHistory = array of TProcessType;
+
+  TMyConfig = class(TAppConfigRegistry)
+  private
+    fTitle : string;
+    fHidden : Boolean;
+    fSessionName: string;
+    fSizes : TArraySizes;
+    fLastFilename : string;
+    fWindowPos : TWinPos;
+    fHistory : TArrayHistory;
+    fComplex : TProcessType;
+    fModifyDate : TDateTime;
+    //fWorkList : TObjectList<TWorker>;
+  public
+    destructor Destroy; override;
+    procedure DefaultValues; override;
+    property Hidden : Boolean read fHidden write fHidden;
+  published
+    property Title : string read fTitle write fTitle;
+    property SessionName : string read fSessionName write fSessionName;
+    property Sizes : TArraySizes read fSizes write fSizes;
+    property LastFilename : string read fLastFilename write fLastFilename;
+    property WindowPos : TWinPos read fWindowPos write fWindowPos;
+    property History : TArrayHistory read fHistory write fHistory;
+    property Complex : TProcessType read fComplex write fComplex;
+    property ModifyDate : TDateTime read fModifyDate write fModifyDate;
+    //property WorkList : TObjectList<TWorker> read fWorkList write fWorkList;
+  end;
+
+  { TForm1 }
+
+  TForm1 = class(TForm)
+    btnSaveJson: TButton;
+    btnLoadJson: TButton;
+    meInfo: TMemo;
+    procedure btnLoadJsonClick(Sender: TObject);
+    procedure btnSaveJsonClick(Sender: TObject);
+    procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
+    procedure FormCreate(Sender: TObject);
+  private
+
+  public
+    procedure SetConfig(cConfig: TMyConfig);
+    function TestConfig(cConfig1, cConfig2 : TMyConfig) : Boolean;
+
+  end;
+
+var
+  Form1: TForm1;
+  ConfigReg : TMyConfig;
+  ConfigTest : TMyConfig;
+
+implementation
+
+{$R *.lfm}
+
+uses
+  Quick.Json.Serializer;
+
+{ TForm1 }
+
+procedure TForm1.btnSaveJsonClick(Sender: TObject);
+begin
+  SetConfig(ConfigReg);
+  ConfigReg.Save;
+
+  meInfo.Lines.Add(ConfigReg.ToJson);
+  meInfo.Lines.Add('Saved Config in Json at ' + DateTimeToStr(ConfigReg.LastSaved));
+end;
+
+procedure TForm1.btnLoadJsonClick(Sender: TObject);
+begin
+  meInfo.Lines.Add('Load ConfigJson');
+  ConfigReg.Load;
+  meInfo.Lines.Add(ConfigReg.ToJSON);
+  if TestConfig(ConfigTest,ConfigReg) then meInfo.Lines.Add('Test passed successfully!');
+end;
+
+function  TForm1.TestConfig(cConfig1, cConfig2 : TMyConfig) : Boolean;
+var
+  i : Integer;
+begin
+  try
+    Assert(cConfig1.LastFilename = cConfig2.LastFilename);
+    for i := Low(cConfig1.Sizes) to High(cConfig1.Sizes) do
+      Assert(cConfig1.Sizes[i] = cConfig2.Sizes[i]);
+    Assert(cConfig1.WindowPos.PosX = cConfig2.WindowPos.PosX);
+    Assert(cConfig1.WindowPos.PosX = cConfig2.WindowPos.PosX);
+    Assert(cConfig1.Complex.Priority = cConfig2.Complex.Priority);
+    Assert(cConfig1.Complex.Redundant  = cConfig2.Complex.Redundant);
+    Assert(cConfig1.Title = cConfig2.Title);
+    //for i := 0 to cConfig1.WorkList.Count - 1 do
+    //begin
+    //  Assert(cConfig1.WorkList[i].Name = cConfig2.WorkList[i].Name);
+    //  Assert(cConfig1.WorkList[i].Active = cConfig2.WorkList[i].Active);
+    //end;
+    for i := 0 to High(cConfig1.History) do
+    begin
+      Assert(cConfig1.History[i].Priority = cConfig2.History[i].Priority);
+      Assert(cConfig1.History[i].Redundant = cConfig2.History[i].Redundant);
+    end;
+    Result := True;
+  except
+    ShowMessage('Configuration not has been saved previously or has a corruption problem');
+  end;
+end;
+
+
+procedure TForm1.FormClose(Sender: TObject; var CloseAction: TCloseAction);
+begin
+  if Assigned(ConfigTest) then ConfigTest.Free;
+  if Assigned(ConfigReg) then ConfigReg.Free;
+end;
+
+procedure TForm1.FormCreate(Sender: TObject);
+begin
+  ConfigTest := TMyConfig.Create;
+  SetConfig(ConfigTest);
+  ConfigReg := TMyConfig.Create(HKEY_CURRENT_USER,'_AppConfig2');
+  //ConfigReg.Provider.CreateIfNotExists := True;
+end;
+
+procedure TForm1.SetConfig(cConfig: TMyConfig);
+var
+  processtype : TProcessType;
+begin
+  cConfig.Title := 'hola';
+  cConfig.SessionName := 'Session01';
+  cConfig.LastFileName := 'C:\library.txt';
+  cConfig.Sizes := [1,2,3,4,5,6,7];
+  cConfig.Complex := TProcessType.Create;
+  cConfig.Complex.Id := 1;
+  cConfig.Complex.Redundant := True;
+  cConfig.Complex.Priority := TMyPriority.msMed;
+  cConfig.WindowPos := TWinPos.Create;
+  cConfig.WindowPos.PosX := 100;
+  cConfig.WindowPos.PosY := 200;
+  processtype := TProcessType.Create;
+  processtype.Id := 1;
+  processtype.Priority := msLow;
+  processtype.Redundant := True;
+  cConfig.History := [processtype];
+  cConfig.ModifyDate := Now();
+end;
+
+{ TMyConfig }
+
+procedure TMyConfig.DefaultValues;
+begin
+  fTitle := 'Default value';
+end;
+
+destructor TMyConfig.Destroy;
+begin
+  //if Assigned(WorkList) then WorkList.Free;
+  inherited;
+end;
+
+end.
+