unit ufrmExportResults;
{
  COSBI: Comprehensive Open Source Benchmarking Initiative
  Copyright (c) 2004 Van Smith

  This program is free software; you can redistribute it and/or
  modify it under the terms of the GNU General Public License
  as published by the Free Software Foundation; either version 2
  of the License, or (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

  The current web address of the GNU General Public License is:
  http://www.gnu.org/licenses/gpl.html

  You can contact the authors of this software at:
  cosbi@vanshardware.com
  See www.vanshardware.com or www.cosbi.org for more contact details.
}

//==============================================================================
// Unit name: ufrmExportResults
// Unit description: This unit houses the OSMark Result Exporter
// Author: Van Smith
// Date: February 12, 2004
// OS dependent: Yes: Windows
// Resolution dependent: >= 1024x768
// External unit dependencies: midaslib, COSBI_Common
//==============================================================================
// Modification History
// ------------ -------
// Ver  Date   Who    Description
// ==== ====== ====== ==========================================================
// 1.0 021204 Van     Created.
//==============================================================================

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Buttons, Grids, DBGrids, DB, DBClient, COSBI_Common,
  midaslib;

type
  TfrmExportResults = class(TForm)
    DataSourceResults: TDataSource;
    DBGridResults: TDBGrid;
    Label1: TLabel;
    GroupBoxDelimiter: TGroupBox;
    RadioButtonTab: TRadioButton;
    RadioButtonComma: TRadioButton;
    RadioButtonColon: TRadioButton;
    MemoResults: TMemo;
    GroupBoxDataToSave: TGroupBox;
    RadioButtonResultsOnly: TRadioButton;
    RadioButtonLabelsAndResults: TRadioButton;
    BitBtnOK: TBitBtn;
    BitBtnCancel: TBitBtn;
    SaveDialogResults: TSaveDialog;
    procedure FormCreate(Sender: TObject);
    procedure RadioButtonResultsOnlyClick(Sender: TObject);
    procedure RadioButtonLabelsAndResultsClick(Sender: TObject);
    procedure BitBtnCancelClick(Sender: TObject);
    procedure DataSourceResultsDataChange(Sender: TObject; Field: TField);
    procedure RadioButtonTabClick(Sender: TObject);
    procedure BitBtnOKClick(Sender: TObject);
  private
    { Private declarations }
    fUseLabels : Boolean;
    fDelimiter : string;
    procedure PopulateMemo;
    procedure SelectDataToSave;
    procedure EnableDelimiter( abAnswer : Boolean );
    procedure ChangeDelimiter;
  public
    { Public declarations }
  end;

implementation

{$R *.dfm}

procedure TfrmExportResults.PopulateMemo;
var
  i : integer;
  s : string;
  lcdsResults : TClientDataSet;
  procedure AddResult(asFieldName : string);
  begin
    s := '';
    if fUseLabels then begin
      s := asFieldName + fDelimiter
         + lcdsResults.FieldByName(asFieldName).AsString;
    end else begin
      //s := FloatToStr( lcdsResults.FieldByName(asFieldName).AsFloat );
        s := lcdsResults.FieldByName(asFieldName).AsString;
    end; // if
    memoResults.Lines.Add( s );
  end; // procedure AddResult
begin
  memoResults.Visible := FALSE;
  lcdsResults := TClientDataSet( DataSourceResults.DataSet );
  memoResults.Clear;
  for i := 0 to lcdsResults.FieldCount-1 do begin
    AddResult( lcdsResults.Fields[ i ].FieldName );
  end;  // for
  memoResults.Visible := TRUE;
end; // procedure TfrmExportResults.PopulateMemo;

procedure TfrmExportResults.FormCreate(Sender: TObject);
begin
  fUseLabels := FALSE;
  fDelimiter := TAB;
end; // procedure TfrmExportResults.FormCreate

procedure TfrmExportResults.RadioButtonResultsOnlyClick(Sender: TObject);
begin
  SelectDataToSave;
end;

procedure TfrmExportResults.SelectDataToSave;
begin
  application.ProcessMessages;
  fUseLabels := RadioButtonLabelsAndResults.Checked;
  EnableDelimiter( RadioButtonLabelsAndResults.Checked );
  PopulateMemo;
end; // procedure TfrmExportResults.SelectDataToSave;

procedure TfrmExportResults.RadioButtonLabelsAndResultsClick(
  Sender: TObject);
begin
  SelectDataToSave;
end; // procedure TfrmExportResults.RadioButtonLabelsAndResultsClick

procedure TfrmExportResults.BitBtnCancelClick(Sender: TObject);
begin
  ModalResult := mrCancel;
end; // procedure TfrmExportResults.BitBtnCancelClick(Sender: TObject);

procedure TfrmExportResults.EnableDelimiter( abAnswer : Boolean );
begin
  GroupBoxDelimiter.Enabled := abAnswer;
  RadioButtonTab.Enabled := abAnswer;
  RadioButtonComma.Enabled := abAnswer;
  RadioButtonColon.Enabled := abAnswer;
end; // procedure EnableDelimiter( abYes );

procedure TfrmExportResults.DataSourceResultsDataChange(Sender: TObject;
  Field: TField);
begin
  PopulateMemo;
end; // procedure TfrmExportResults.DataSourceResultsDataChange

procedure TfrmExportResults.ChangeDelimiter;
begin
  application.ProcessMessages;
  if RadioButtonTab.Checked then begin
    fDelimiter := TAB;
  end else if RadioButtonComma.Checked then begin
    fDelimiter := ',';
  end else begin
    fDelimiter := ':';
  end; // if
  PopulateMemo;
end; // procedure TfrmExportResults.ChangeDelimiter( asDelimiter : string );

procedure TfrmExportResults.RadioButtonTabClick(Sender: TObject);
begin
  ChangeDelimiter;
end; // procedure TfrmExportResults.RadioButtonTabClick(Sender: TObject);

procedure TfrmExportResults.BitBtnOKClick(Sender: TObject);
begin
  if SaveDialogResults.Execute then begin
    SaveStringListToFile( MemoResults.Lines, SaveDialogResults.FileName );
  end; // if
end; // procedure TfrmExportResults.BitBtnOKClick(Sender: TObject);

end.
