Recent

Author Topic: Draw on Canvas - cannot fathom the logic  (Read 10281 times)

Handoko

  • Hero Member
  • *****
  • Posts: 5122
  • My goal: build my own game engine using Lazarus
Re: Draw on Canvas - cannot fathom the logic
« Reply #15 on: May 22, 2017, 08:29:46 pm »
Interesting, tell me more. I rarely do database programming.  :-[

J-G

  • Hero Member
  • *****
  • Posts: 953
Re: Draw on Canvas - cannot fathom the logic
« Reply #16 on: May 22, 2017, 09:04:55 pm »
Interesting, tell me more. I rarely do database programming.  :-[

Nothing 'Magic' Handoko, I have a generic variable simply declared as 

  BackupFile  : File;

whilst all other files are defined as a specific 'type' (of Record).

Code: Pascal  [Select][+][-]
  1. procedure MakeBAKFile(F : byte);
  2. begin
  3.   case F of
  4.     1 : begin
  5.           assign(BackupFile,DataPath + 'Customers.BAK');
  6.           {$I-} Erase(BackupFile); {$I+}
  7.           if IOResult <> 0 then ;
  8.  
  9.           {$I-} ReName(Customers,DataPath+'Customers.BAK'); {$I+}
  10.           if IOResult <> 0 then
  11.             showmessage('Error Renaming Customers File : '+IntToStr(IOResult));
  12.         end;
  13.     2 : begin
  14.           assign(BackupFile,YearDataPath + 'Invoice.BAK');
  15.           {$I-} Erase(BackupFile); {$I+}
  16.           if IOResult <> 0 then ;
  17.  
  18.           {$I-} ReName(Invoices,YearDataPath+'Invoice.BAK'); {$I+}
  19.           if IOResult <> 0 then
  20.             showmessage('Error Renaming Invoice File : '+IntToStr(IOResult));
  21.         end;
  22. [...]
  23.  
  24. end;

I first check to see if a backup file already exists and if so, erase it, then I rename the existing live .DTA file as .BAK  -  also testing for IO Error  - just in case. :)

On return from this I re-assign the filename and 'reWrite'.

There will be readers here who will complain about using 'Magic Numbers' but I find it convenient to keep all the calls in one location.




FPC 3.0.0 - Lazarus 1.6 &
FPC 3.2.2  - Lazarus 2.2.0 
Win 7 Ult 64

Handoko

  • Hero Member
  • *****
  • Posts: 5122
  • My goal: build my own game engine using Lazarus
Re: Draw on Canvas - cannot fathom the logic
« Reply #17 on: May 22, 2017, 09:12:34 pm »
Quote
... This is called EVERY time data is written to any file.

It will cause noticeable program slowdown, won't it?

Thaddy

  • Hero Member
  • *****
  • Posts: 14157
  • Probably until I exterminate Putin.
Re: Draw on Canvas - cannot fathom the logic
« Reply #18 on: May 22, 2017, 09:34:16 pm »
It will cause noticeable program slowdown, won't it?
It will be measurable but hardly noticable, because both the hard-disk hardware and the OS perform caching. Unless you restructure a table or something...Or re-index.
Specialize a type, not a var.

J-G

  • Hero Member
  • *****
  • Posts: 953
Re: Draw on Canvas - cannot fathom the logic
« Reply #19 on: May 22, 2017, 09:53:33 pm »
Quote
... This is called EVERY time data is written to any file.
It will cause noticeable program slowdown, won't it?

It might do so if the data were very large, but I find that it is usually possible to compartmentalize most data - in my current project I keep separate files for each 'year' but it could be changed to each 'month' without much ado.

My largest single file is less than 300 records amounting to about 270kb and the time it takes to [save] that is imperceptible on a 3.5gHz AMD platform   ie. nothing extraordinary.

I haven't written code to check the actual time (Read 'NOW' - save - Read 'NOW') but (as Thaddy has mentioned) it is measurable (probably in milliseconds) but not noticeable. Also the nature of the data is such that sorting is seldom needed  -  only the 'Customers' and 'Suppliers' files are ever sorted and only after adding a new record.

Essentially  -  not a problem.
FPC 3.0.0 - Lazarus 1.6 &
FPC 3.2.2  - Lazarus 2.2.0 
Win 7 Ult 64

RAW

  • Hero Member
  • *****
  • Posts: 868
Re: Draw on Canvas - cannot fathom the logic
« Reply #20 on: May 23, 2017, 03:53:56 pm »
Quote
@ RAW
Your suggestion is beyond my comprehension, so maybe I also 'miss your mark'. The VATPanel is only shown as and when required rather than an integral part of the main form.
:D
What I got in mind was something like this:
Code: Pascal  [Select][+][-]
  1. Unit Unit1;
  2. {$MODE OBJFPC}{$H+}
  3.  
  4. Interface
  5.  USES
  6.   Classes,  SysUtils, Forms,
  7.   Controls, Graphics, ExtCtrls,
  8.   StdCtrls;
  9.  
  10.  TYPE
  11.   TForm1 = Class(TForm)
  12.  
  13.    Procedure FormCreate (Sender: TObject);
  14.    Procedure FormResize (Sender: TObject);
  15.    Procedure FormClick  (Sender: TObject);
  16.    Procedure ShowVAT;
  17.  
  18.     PRIVATE
  19.      PanVAT: TPanel;
  20.   End;
  21.  
  22.  VAR
  23.   Form1: TForm1;
  24.  
  25. Implementation
  26. {$R *.LFM}
  27.  
  28.  
  29. Procedure TForm1.ShowVAT;
  30.   Var
  31.    LabLeft  : TLabel;
  32.    iFontSize: Integer;
  33.  
  34.    IMG: TImage;
  35.    BMP: TBitmap;
  36.  Begin
  37.   PanVAT               := TPanel.Create(Self);
  38.   PanVAT.BorderStyle   := bsSingle;
  39.   PanVAT.BevelOuter    := bvRaised;
  40.   PanVAT.Color         := clBlack;
  41.   PanVAT.BorderWidth   := 0;
  42.   PanVAT.Caption       := '';
  43.   PanVAT.DoubleBuffered:= True;
  44.   PanVAT.SetBounds     (Self.ClientWidth Div 2,
  45.                         0,
  46.                         Self.ClientWidth Div 2,
  47.                         Self.ClientHeight);
  48.   PanVAT.Parent        := Self;
  49.  
  50.  
  51.   LabLeft             := TLabel.Create(PanVAT);
  52.   LabLeft.Transparent := True;
  53.   LabLeft.AutoSize    := True;
  54.   LabLeft.Font.Color  := clLime;
  55.   LabLeft.Font.Name   := 'Lucida Console';
  56.   LabLeft.Font.Quality:= fqAntialiased;
  57.   LabLeft.Font.Size   := PanVAT.ClientWidth Div 30;
  58.   LabLeft.Font.Style  := [fsBold];
  59.   LabLeft.Align       := alLeft;
  60.   LabLeft.Alignment   := taRightJustify;
  61.   LabLeft.Caption     := 'Current Period' +sLineBreak+
  62.                          'Return Due'     +sLineBreak+
  63.                                            sLineBreak+
  64.                                            sLineBreak+
  65.                          'Period Shown'   +sLineBreak+
  66.                          'Output VAT'     +sLineBreak+
  67.                          'Input VAT'      +sLineBreak+
  68.                          'VAT Due'        +sLineBreak+
  69.                          'Sales'          +sLineBreak+
  70.                          'Purchases'      +sLineBreak+
  71.                          'Adjustment VAT' +sLineBreak+
  72.                          'Adjustment Goods';
  73.   LabLeft.Parent      := PanVAT;
  74.  
  75.  
  76.   iFontSize:= LabLeft.Font.Size;
  77.  
  78.   BMP:= TBitmap.Create;
  79.    Try
  80.     BMP.Height            := iFontSize;
  81.     BMP.Width             := PanVAT.ClientWidth;
  82.     BMP.Canvas.Brush.Color:= clLime;
  83.     BMP.Canvas.Brush.Style:= bsSolid;
  84.     BMP.Canvas.FillRect      (BMP.Canvas.ClipRect);
  85.  
  86.     IMG:= TImage.Create(PanVAT);
  87.     IMG.Picture.Assign(BMP);
  88.     IMG.SetBounds(0,
  89.                   Trunc(iFontSize*3.5),
  90.                   BMP.Width,
  91.                   BMP.Height);
  92.     IMG.Parent:= PanVAT;
  93.    Finally
  94.     BMP.Free;
  95.    End;
  96.  End;
  97.  
  98.  
  99. Procedure TForm1.FormClick(Sender: TObject);
  100.  Begin
  101.   If Not Assigned(PanVAT)
  102.   Then ShowVAT
  103.   Else
  104.    Begin
  105.     PanVAT.Free;
  106.     PanVAT:= Nil;
  107.    End;
  108.  End;
  109.  
  110.  
  111. Procedure TForm1.FormResize(Sender: TObject);
  112.  Begin
  113.   If Assigned(PanVAT)
  114.   Then
  115.    Begin
  116.     PanVAT.Free;
  117.     ShowVAT;
  118.    End;
  119.  End;
  120.  
  121.  
  122. Procedure TForm1.FormCreate(Sender: TObject);
  123.  Begin
  124.   DoubleBuffered:= True;
  125.  
  126.   Constraints.MinHeight:= 200;
  127.   Constraints.MinWidth := 300;
  128.  End;
  129.  
  130. END.
  131.  

Windows 7 Pro (x64 Sp1) & Windows XP Pro (x86 Sp3).

J-G

  • Hero Member
  • *****
  • Posts: 953
Re: Draw on Canvas - cannot fathom the logic
« Reply #21 on: May 23, 2017, 08:12:19 pm »
What I got in mind was something like this:
That may well be a fully OOP method using a secondary 'Form' but my VATPanel is just one of 10 Panels and it seems far more complex than my simple VATPanel.show or .hide method.

@ Handoko
I've now done a 'timing' test and can report that my largest file (300 records of 848 bytes) takes 37.5 milliseconds to first make the Backup File then recreate and write the new file.

FPC 3.0.0 - Lazarus 1.6 &
FPC 3.2.2  - Lazarus 2.2.0 
Win 7 Ult 64

 

TinyPortal © 2005-2018