summaryrefslogtreecommitdiff
path: root/src/gui/fpg_stringgridbuilder.pas
blob: fd3fe3b801aa47a3852ef3ac782446d879b71e5c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
{
    fpGUI  -  Free Pascal GUI Toolkit

    Copyright (C) 2006 - 2014 See the file AUTHORS.txt, included in this
    distribution, for details of the copyright.

    See the file COPYING.modifiedLGPL, included in this distribution,
    for details about redistributing fpGUI.

    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.

    Description:
      This unit defines a helper class that can populate a StringGrid
      from a CSV file. In future this could be expaned to other file
      types or even data structures.
}
unit fpg_StringGridBuilder;

{$mode objfpc}{$H+}

interface

uses
  Classes,
  SysUtils,
  fpg_base,
  fpg_grid;

type
  TStringGridBuilder = class(TObject)
  private
    FData: TStringList;
    FGrid: TfpgStringGrid;
    FCSVFile: TfpgString;
    FHasHeader: boolean;
  protected
    procedure   InternalSetupColumns; virtual;
    procedure   InternalSetupData; virtual;
    procedure   InternalRepaintRow(const AData: TfpgString; const ARow: integer); virtual;
  public
    constructor Create;
    constructor CreateCustom(const AGrid: TfpgStringGrid; const ACSVFile: TfpgString; const AWithHeader: boolean = True); virtual;
    destructor  Destroy; override;
    procedure   Run;
    property    Grid: TfpgStringGrid read FGrid;
  end;

implementation

uses
  fpg_main,
  fpg_utils,
  fpg_CSVParser;

{ TStringGridBuilder }

procedure TStringGridBuilder.InternalSetupColumns;
var
  x: integer;
  fields: TStringList;
begin
  fields := TStringList.Create;
  try
    gCsvParser.ExtractFields(FData[0], fields);
    // setup correct column count
    FGrid.ColumnCount := fields.Count;
    // initialize columns
    if FHasHeader then
    begin
      for x := 0 to fields.Count-1 do
      begin
        FGrid.ColumnTitle[x] := fields[x];
//        FGrid.ColumnWidth[x] := StrToInt(FColumns.ValueFromIndex[x]);
      end;
    end;
  finally
    fields.Free;
  end;
end;

procedure TStringGridBuilder.InternalSetupData;
var
  y: integer;
begin
  FGrid.BeginUpdate;
  FGrid.MouseCursor := mcHourGlass;
  try
    try
      // set correct row count. Columns have already been handled.
      if FHasHeader then
      begin
        FGrid.RowCount := FData.Count-1;
        for y := 1 to FData.Count-1 do  // rows
        begin
  //        writeln(' Row: ', y, '  Data: ', FData.Strings[y-1]);
          InternalRepaintRow(FData.Strings[y], y-1);
        end;
      end
      else
      begin
        FGrid.RowCount := FData.Count;
        for y := 0 to FData.Count-1 do  // rows
        begin
  //        writeln(' Row: ', y, '  Data: ', FData.Strings[y-1]);
          InternalRepaintRow(FData.Strings[y], y);
        end;
      end;
    except
      fpgApplication.HandleException(self);
    end;
  finally
    if FGrid.RowCount > 0 then
      FGrid.FocusRow := 0;
    FGrid.EndUpdate;
    FGrid.MouseCursor := mcDefault;
  end;
end;

procedure TStringGridBuilder.InternalRepaintRow(const AData: TfpgString; const ARow: integer);
var
  x: integer;
  fields: TStrings;
  value: string;
begin
  fields := TStringList.Create;
  try
    gCsvParser.ExtractFields(AData, fields);
    for x := 0 to FGrid.ColumnCount-1 do
    begin
      if x < fields.Count then
        value := fields.Strings[x]
      else
        value := '';
      FGrid.Cells[x, ARow] := value
    end;
  finally
    fields.Free;
  end;
end;

constructor TStringGridBuilder.Create;
begin
  FData := TStringList.Create;
end;

constructor TStringGridBuilder.CreateCustom(const AGrid: TfpgStringGrid; const ACSVFile: TfpgString; const AWithHeader: boolean);
begin
  Create;
  FGrid := AGrid;
  FCSVFile := ACSVFile;
  FGrid.Clear;
  FHasHeader := AWithHeader;
  FGrid.ShowHeader := AWithHeader;
end;

destructor TStringGridBuilder.Destroy;
begin
  FGrid := nil;
  FData.Free;
  inherited Destroy;
end;

procedure TStringGridBuilder.Run;
begin
  if FCSVFile = '' then
    raise Exception.Create('TStringGridBuilder: CSV filename is empty!');
  if not fpgFileExists(FCSVFile) then
    raise Exception.CreateFmt('TStringGridBuilder: The CSV file <%s> does not exist.', [FCSVFile]);
  FData.LoadFromFile(fpgToOSEncoding(FCSVFile));
  InternalSetupColumns;
  InternalSetupData;
end;


end.