test

progging - To wander about and beg; to seek food or other supplies by low arts; to seek for advantage by mean shift or tricks.
progging - Programmer slang for writing computer code.

tirsdag 6. september 2011

Define your custom TColor type in Delphi

Do define your own custom color in Delphi, do this:

const
  clChartRed = TColor($B3B3FF);       // 70 % gradient Red on White
  clChartYellow = TColor($B3FFFF);    // 70 % gradient Yellow on White
But remember that Delphi defines the colors as BGR and not the usual RGB so remember to swap numbers.
So the color red would be:
const
  clRed = TColor($0000FF);       // Red

tirsdag 30. august 2011

Dynamically adding TQRChart at run-time on a QuickReport

Handle the BeforePrint method in the TQuickRep class to clone the TeeChart graphs...

procedure TQReport.QReportBeforePrint(Sender: TCustomQuickRep;
  var PrintReport: Boolean);
var
  i: Integer;
  LV_Height: Integer;
  LV_Top: Integer;
  LV_Chart : TChart;
  tmp: TQRChart;
begin
  // Divide height between the N charts
  LV_Height := Round(DetailBand1.Height / ChartList.Count);
  LV_Top := 0; // Starting point

  for LV_Chart in ChartList do begin
    { Create the QRChart }
    tmp:=TQRChart.Create(Self);
    { Create the QRDBChart }
    With TQRDBChart.Create(tmp) do
    begin
      Parent:=TWinControl(tmp);
      Name:=TeeGetUniqueName(Owner,'QRChart');
      Title.Text.Clear;
      Title.Text.Add(tmp.ClassName);
    end;
    { add the QRChart to the QuickReport... }
    With tmp do
    begin
      ParentReport := Self;
      Parent := DetailBand1;
      Width := DetailBand1.Width;
      Height := LV_Height;
      Left := 0;
      Top := LV_Top;
      // Move next chart down
      LV_Top := LV_Top + LV_Height;
      { Copy Series and do special formatting}
      Chart.FreeAllSeries;
      Chart.Assign(LV_Chart);
      for i:=0 to LV_Chart.SeriesCount-1 do
        CloneChartSeries(LV_Chart[i]).ParentChart := Chart;

      Chart.Color := clWhite;
      // To include all data - and not only the last 10 years
      Chart.BottomAxis.Automatic := True;
      // Remove gradient background for printing
      Chart.BackWall.Gradient.Visible := False;
    end;
  end;
end;
The QReport tips are taken from here:
http://www.steema.com/support/faq/NewVCL/FAQ_VCL_QUICKREPORT.htm

Delphi Enumerable List - example

Just an example of a list of a specific type in delphi.
unit MyUtility;

interface

uses
  Classes,  {TList}
  Chart;    {TChart}

type
  TChartList = class;
  TChartListEnumerator = record
  private
    FIndex: Integer;
    FList: TChartList;
  public
    constructor Create(AList: TChartList);
    function MoveNext: Boolean; inline;
    function GetCurrent: TChart; inline;
    property Current: TChart read GetCurrent;
  end;
  TChartList = class(TList)
  public
    procedure Add(AChart: TChart);
    function GetItem(i:Integer): TChart;
    function GetEnumerator: TChartListEnumerator;
    property Items[i:Integer]: TChart read GetItem; default;
  end;

implementation

{TChartList}

procedure TChartList.Add(AChart: TChart);
begin
  inherited Add(AChart);
end;

function TChartList.GetItem(i:Integer): TChart;
begin
  Result := List[i];
end;

function TChartList.GetEnumerator: TChartListEnumerator;
begin
  Result := TChartListEnumerator.Create(Self);
end;

{ TChartListEnumerator }

constructor TChartListEnumerator.Create(AList: TChartList);
begin
  FIndex := -1;
  FList := AList;
end;

function TChartListEnumerator.GetCurrent: TChart;
begin
  Result := FList.List[FIndex];
end;

function TChartListEnumerator.MoveNext: Boolean;
begin
  Result := FIndex < FList.Count - 1;
  if Result then
    Inc(FIndex);
end;

end.
And it's use:
var
  Chart: TChart;
begin
  for Chart in ChartList do begin
    Chart.DoSomeThing();
  end;

Print/Preview of Quick Report problem

While testing some code that creates a Quick Report with a TeeChart on it, I changed the Print() call to a PreviewModal(). All though this seems to work at first, two problems occurred.

  1. The second time I clicked to see the preview dialog, the report did not show (only some black blocks)
  2. When I closed the application, I got several error messages.
Calling Preview() instead seem to fix the problem. Not sure what the problem was but this is from the QReport documentation:
Prepare
procedure Prepare.
Use ‘Prepare’ if you want to generate a report without automatically bring up a preview window or print it.
Preview
procedure Preview
Use ‘Preview’ to generate the report and bring up an on screen preview of it. From the preview window the user can choose to print the report.
PreviewModal
procedure PreviewModal
As Preview with the following differences -
When calling PreviewModal the report is generated in a background thread. Some database drivers are not thread safe and this might cause unexpected behavior or program crashes. Only use PreviewModal in situations where it is sure that a thread safe database driver will be used.

mandag 29. august 2011

Syntax highlighting in blog

Even though this has nothing to do with Delphi progging, I add a little post to test different syntax highlighting methods in blogger.

SyntaxHighlighter
Format: <pre class="brush:delphi">...</pre>

type
  TMyClass = class;
  if i < 0 then begin
    with DMSystem do
      Table.Create();
  end;
google code prettify Format: <pre class="prettyprint">...</pre>
type
  TMyClass = class;
  if i < 0 then begin
    with DMSystem do
      Table.Create();
  end;
int foo=0; NSLog(@"%i", foo); 
Links:
http://stackoverflow.com/questions/1852537/how-to-use-prettify-with-blogger-blogspot
http://www.cyberack.com/2007/07/adding-syntax-highlighter-to-blogger.html (but remember to add link to the delphi script)

Some SyntaxHighlighter config examples:
Format: <pre class="brush: delphi; gutter: false; toolbar: false; highlight: 5">...</pre>
type
  TMyClass = class;
  if i < 0 then begin
    with DMSystem do
      Table.Create();
  end;

How to clone a Delphi Form

In Visual Studio 2005 it's pretty easy to make a copy of an existing from directly from the project manager in the IDE (simply copy-paste with Ctrl-C and Ctrl-V). This is of course very useful instead of starting from scratch when you have a form that looks a lot like your new form will do.
The trick to do this in Delphi is:

  1. Do a "Save As" of the form in the file menu (.pas and .dfm will be copied and the unit name will be changed)
  2. Edit the saved as form to change the class name (easiest to do it in the designer)
  3. Add back the old form using "Add to Project...(Shift + F11)

Bazaar Bug

I encountered this bug in my Bazaar repository.
I'm not sure how the master branch got out of date with the bound branch, but what had happened was that some files got accidentally added at the root, then when I did a bzr rm things got screwed up. The error I got was:
bzr: ERROR: Bound branch BzrBranch7(file:///C:/Documents%20and%20Settings/xx/) is out of date with master branch BzrBranch7(file:///U:/Utvikling%20Backup/srv/bzr/xx/).
To commit to master branch, run update and then commit.
You can also pass --local to commit to continue working disconnected.

Doing a bzr update just said that
Tree is up to date at revision 1 of branch U:/Utvikling Backup/srv/bzr/xx

When I did a bzr log at the master branch it said it was up to date at revision 0.
So I knew the master branch was out of date and did a bzr push U:/Utvikling Backup/srv/bzr/xx and after that everything was fine :)