summaryrefslogtreecommitdiff
path: root/extras/tiopf/demos/rtf_reporting/frmMain.pas
blob: 2cbe8593a5514e7aa9d0fec43a0e1fad335fd92f (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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
unit frmMain;

{$mode objfpc}{$H+}
  {$ASMMODE intel}

interface

uses
  SysUtils, Classes, fpg_base, fpg_main,
  fpg_form, fpg_label, fpg_button,
  tiRtfReport;

type

  //Add extra functions in your rtfreport
  TRtfPrivateParser = class(TtiRtfParser)
  protected
    procedure AddFunctions; override;
    procedure UdfBla(AArgument: TRtfArgument);
    procedure UdfCentreAddress(AArgument: TRtfArgument);
  end;


  TMainForm = class(TfpgForm)
  private
    ResultFile: string;
    TemplateFile: string;
    procedure btnEditClicked(Sender: TObject);
    procedure btnParseClicked(Sender: TObject);
    procedure btnShowClicked(Sender: TObject);
  public
    {@VFD_HEAD_BEGIN: MainForm}
    Label1: TfpgLabel;
    btnParse: TfpgButton;
    btnEdit: TfpgButton;
    btnShow: TfpgButton;
    {@VFD_HEAD_END: MainForm}
    constructor Create(AOwner: TComponent); override;
    procedure AfterCreate; override;
  end;

{@VFD_NEWFORM_DECL}


implementation

uses
  tiObject, process;


type
  // Simple BOM structure, nothing fancy
  TtiNestedDemoItem = class(TtiObject)
  private
    FId: integer;
    FName: string;
  published
    property Id: integer read FId write FId;
    property Name: string read FName write FName;
  end;


  TtiNestedDemoItems = class(TtiObjectList)
  protected
    function GetItems(Idx: integer): TtiNestedDemoItem; reintroduce;
  public
    property Items[Idx: integer]: TtiNestedDemoItem read GetItems;
  end;


  TtiDemoItem = class(TtiObject)
  private
    FId: integer;
    FName: string;
    FData: TtiNestedDemoItem;
    FList: TtiNestedDemoItems;
  public
    constructor Create; override;
    destructor Destroy; override;
  published
    property Id: integer read FId write FId;
    property Name: string read FName write FName;
    property oData: TtiNestedDemoItem read FData;
    property oList: TtiNestedDemoItems read FList;
  end;


  TtiDemoItems = class(TtiObjectList)
  protected
    function GetItems(Idx: integer): TtiDemoItem; reintroduce;
  public
    procedure Populate;
    property Items[Idx: integer]: TtiDemoItem read GetItems;
  end;


{ TtiNestedDemoItems }

function TtiNestedDemoItems.GetItems(Idx: integer): TtiNestedDemoItem;
begin
  Result := TtiNestedDemoItem(inherited GetItems(Idx));
end;


{ TtiDemoItem }

constructor TtiDemoItem.Create;
begin
  inherited;
  FData := TtiNestedDemoItem.Create;
  FList := TtiNestedDemoItems.Create;
end;

destructor TtiDemoItem.Destroy;
begin
  FData.Free;
  FList.Free;
  inherited;
end;


{ TtiDemoItems }

function TtiDemoItems.GetItems(Idx: integer): TtiDemoItem;
begin
  Result := TtiDemoItem(inherited GetItems(Idx));
end;

procedure TtiDemoItems.Populate;
var
  i, j: integer;
  ADemoItem: TtiDemoItem;
  ANestedDemoItem: TtiNestedDemoItem;
begin
  for i := 1 to 20 do begin
    ADemoItem := TtiDemoItem.Create;
    ADemoItem.Id := i;
    ADemoItem.Name := Format('This is demo item %d',[i]);
    ADemoItem.oData.Id := i * 1000;
    ADemoItem.oData.Name := Format('This is demo data item %d',[i * 1000]);
    Add(ADemoItem);

    for j := 1 to Random(5) do begin
      ANestedDemoItem := TtiNestedDemoItem.Create;
      ANestedDemoItem.Id := j;
      ANestedDemoItem.Name := Format('This is nested demo item %d',[j]);
      ADemoItem.oList.Add(ANestedDemoItem);
    end;
  end;
end;


{@VFD_NEWFORM_IMPL}

procedure TMainForm.btnEditClicked(Sender: TObject);
var
  p: TProcess;
begin
  p := TProcess.Create(nil);
  try
    p.CommandLine := 'xdg-open ' + TemplateFile;
    p.Execute;
  finally
    p.Free;
  end;
end;

procedure TMainForm.btnParseClicked(Sender: TObject);
var
  AStart: TDateTime;
  FParser: TRtfPrivateParser;
  ADemoItems: TtiDemoItems;
begin
  ADemoItems := TtiDemoItems.Create;
  try
    ADemoItems.Populate;

    try
      AStart := Now;

      Label1.Text := 'working';
      btnShow.Enabled := false;
      btnParse.Enabled := false;
      MouseCursor := mcHourGlass;
      FParser := TRtfPrivateParser.Create;
      try
//        FParser.OnPictureAttr := OnPictureAttr;
//        FParser.OnCreateDataset := OnCreateDataset;
        FParser.Datasets.Add(ADemoItems, 'DemoItems');
//        FParser.Datasets.Add(tbBioLife, 'BioLife');
        FParser.LoadFromFile(TemplateFile);
        FParser.Execute;
        FParser.SaveToFile(ResultFile);
      finally
        FParser.Free;
        MouseCursor := mcDefault;
        btnShow.Enabled := true;
        btnParse.Enabled := true;
      end;

      Label1.Text := Format('Session completed in %s',[FormatDateTime('hh:nn:ss:zzz', Now - AStart)]);
      btnShow.Click;
    except
      on E: Exception do begin
        fpgApplication.HandleException(E);
      end;
    end;

  finally
    ADemoItems.Free;
  end;
end;

procedure TMainForm.btnShowClicked(Sender: TObject);
var
  p: TProcess;
begin
  p := TProcess.Create(nil);
  try
    p.CommandLine := 'xdg-open ' + ResultFile;
    p.Execute;
  finally
    p.Free;
  end;
end;

constructor TMainForm.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  TemplateFile := 'demo.rtf';
//  TemplateFile := 'demo_ms.rtf';
  ResultFile := 'result.rtf';
  ShortDateFormat := 'yyyy-mm-dd';
end;

procedure TMainForm.AfterCreate;
begin
  {@VFD_BODY_BEGIN: MainForm}
  Name := 'MainForm';
  SetPosition(333, 208, 300, 124);
  WindowTitle := 'RTF Reporting Demo';

  Label1 := TfpgLabel.Create(self);
  with Label1 do
  begin
    Name := 'Label1';
    SetPosition(20, 16, 244, 16);
    FontDesc := '#Label1';
    Text := '--';
  end;

  btnParse := TfpgButton.Create(self);
  with btnParse do
  begin
    Name := 'btnParse';
    SetPosition(28, 80, 75, 24);
    Text := 'Parse';
    FontDesc := '#Label1';
    ImageName := '';
    TabOrder := 1;
    OnClick := @btnParseClicked;
  end;

  btnEdit := TfpgButton.Create(self);
  with btnEdit do
  begin
    Name := 'btnEdit';
    SetPosition(112, 80, 75, 24);
    Text := 'Edit';
    FontDesc := '#Label1';
    ImageName := '';
    TabOrder := 2;
    OnClick := @btnEditClicked;
  end;

  btnShow := TfpgButton.Create(self);
  with btnShow do
  begin
    Name := 'btnShow';
    SetPosition(196, 80, 75, 24);
    Text := 'Show';
    FontDesc := '#Label1';
    ImageName := '';
    TabOrder := 3;
    OnClick := @btnShowClicked;
  end;

  {@VFD_BODY_END: MainForm}
end;


{ TRtfPrivateParser }

procedure TRtfPrivateParser.AddFunctions;
begin
  inherited AddFunctions;
  Functions.Add(etFunction, 'Bla', 0, 0, @UdfBla);
  Functions.Add(etFunction, 'CentreAddress', 0, 0, @UdfCentreAddress);
end;

procedure TRtfPrivateParser.UdfBla(AArgument: TRtfArgument);
begin
  AArgument.Token := etLitString;
  AArgument.Value := 'Best value is 1.5e+400';
end;

procedure TRtfPrivateParser.UdfCentreAddress(AArgument: TRtfArgument);
begin
  AArgument.Token := etLitString;
  AArgument.Value := '8 Stellendal Road, Somerset West, 7130';
end;

end.