File: lr_exportmatrix.pas

package info (click to toggle)
lazarus 2.0.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 214,460 kB
  • sloc: pascal: 1,862,622; xml: 265,709; cpp: 56,595; sh: 3,008; java: 609; makefile: 535; perl: 297; sql: 222; ansic: 137
file content (728 lines) | stat: -rw-r--r-- 16,493 bytes parent folder | download | duplicates (2)
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
{
  LazReport matrix for export reports

 Copyright (C) 2014-2015 alexs alexs75.at.yandex.ru

 The module is designed to create an image of the report with the exact
 positioning of objects and subsequent binding to the worksheet

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

 As a special exception, the copyright holders of this library give you
 permission to link this library with independent modules to produce an
 executable, regardless of the license terms of these independent modules,and
 to copy and distribute the resulting executable under terms of your choice,
 provided that you also meet, for each linked independent module, the terms
 and conditions of the license of that module. An independent module is a
 module which is not derived from or based on this library. If you modify
 this library, you may extend this exception to your version of the library,
 but you are not obligated to do so. If you do not wish to do so, delete this
 exception statement from your 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 Library General Public License
 for more details.

 You should have received a copy of the GNU Library General Public License
 along with this library; if not, write to the Free Software Foundation,
 Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1335, USA.
}

unit LR_ExportMatrix;
{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, LR_Class, Graphics;

type

  { TExportObject }

  TExportObject = class
  private
    FAlignment: TAlignment;
    FCol: Integer;
    FLayout: TTextLayout;
    FRow: Integer;
    FMergedCol: Integer;
    FMergedRow: Integer;

    FFillColor: TColor;
    FFrameColor: TColor;
    FFrames: TfrFrameBorders;
    FFrameStyle: TfrFrameStyle;
    FFrameWidth: Double;

    FHeight: integer;
    FLeft: integer;
    FTop: integer;
    FURLInfo: string;
    FWidht: integer;

    FObjType: integer;
    FTexts:TStringList;
    FFont:TFont;
    FName:string;
    FAngle:byte;
    FWordWrap: boolean;

    FPicture: TPicture;

    function GetText: string;
  public
    constructor Create(AObj:TfrView);
    destructor Destroy; override;
    procedure NeedPicture;

    property Top:integer read FTop write FTop;
    property Left:integer read FLeft write FLeft;
    property Height:integer read FHeight write FHeight;
    property Widht:integer read FWidht write FWidht;
    property Alignment : TAlignment read FAlignment;
    property Col:Integer read FCol;
    property Row:Integer read FRow;
    property MergedCol:Integer read FMergedCol;
    property MergedRow:Integer read FMergedRow;
    property Text:string read GetText;
    property Texts:TStringList read FTexts;
    property ObjType:integer read FObjType write FObjType;
    property FillColor:TColor read FFillColor;
    property Font:TFont read FFont;
    property Frames : TfrFrameBorders read FFrames;
    property FrameColor : TColor read FFrameColor;
    property FrameStyle : TfrFrameStyle read FFrameStyle;
    property FrameWidth : Double read FFrameWidth;
    property Angle:byte read FAngle;
    property Layout : TTextLayout read FLayout;
    property WordWrap:boolean read FWordWrap;
    property URLInfo: string read FURLInfo write FURLInfo;
    property Picture: TPicture read FPicture;

  end;

  { TExportRows }

  TExportRow = class
  private
    FCells:TFpList;
    FTop: integer;
    FRow:integer;
    procedure SortCells;
    function GetObjects(X: integer): TExportObject;
  public
    constructor Create;
    destructor Destroy; override;
    procedure Clear;
    function ExportObject(AObj:TfrView):TExportObject;

    property Top:integer read FTop write FTop;
    property Row:integer read FRow;

    property Cells:TFpList read FCells;
  end;

  { TExportMatrix }

  TExportMatrix =class
  private
    FDeleteEmptyRow: boolean;
    //FExportImages: boolean;
    FMergeCell: boolean;
    FRows:TFpList;
    FColWidth:TBoundArray;
    FRowHight:TBoundArray;
    FPageMargin:integer;
    function FindRow(FTop:integer):TExportRow;
    function GetColumnCount: integer;
    function GetColumnWidth(AColumn: integer): Integer;
    function GetColNumByLeft(ALeft:Integer):integer;
    function GetRowNumByTop(ATop:Integer):integer;

    function GetObjects(X, Y: integer): TExportObject;
    function GetRowCount: integer;
    function GetRowHight(ARow: integer): Integer;

    procedure SortRows;
    procedure UpdateColWidth;
    procedure UpdateRowHeight;
    procedure DoPostitionObject;
    procedure DoMergeCell;
    procedure DoDeleteEmptyRow;
  public
    constructor Create;
    procedure Clear;
    destructor Destroy; override;
    function FindObj(AObj:TfrView):TExportObject;
    function ExportObject(AObj:TfrView):TExportObject;
    procedure PrepareData;
    procedure NewPage;
    property ColumnCount:integer read GetColumnCount;
    property RowCount:integer read GetRowCount;
    property ColumnWidth[AColumn:integer]:Integer read GetColumnWidth;
    property RowHiht[ARow:integer]:Integer read GetRowHight;
    property Objects[X, Y:integer]:TExportObject read GetObjects;
    property DeleteEmptyRow:boolean read FDeleteEmptyRow write FDeleteEmptyRow;
    property MergeCell:boolean read FMergeCell write FMergeCell;
    property PageMargin:integer read FPageMargin write FPageMargin;
    //property ExportImages:boolean read FExportImages write FExportImages;

    property Rows:TFpList read FRows;
  end;

implementation
uses math;

{ TExportRows }

procedure TExportRow.SortCells;
var
  R, P:TExportObject;
  i: Integer;
  j: Integer;
begin
  for i:=0 to FCells.Count-2 do
  begin
    R:=TExportObject(FCells[i]);
    for j:=i+1 to FCells.Count-1 do
    begin
      if R.FLeft > TExportObject(FCells[j]).FLeft then
      begin
        P:=TExportObject(FCells[j]);
        FCells[j]:=R;
        R:=P;
      end;
    end;
    FCells[i]:=R;
  end;
end;

function TExportRow.GetObjects(X: integer): TExportObject;
var
  i: Integer;
begin
  Result:=nil;
  for i:=0 to FCells.Count-1 do
    if TExportObject(FCells[i]).FCol = X then
    begin
      Result:=TExportObject(FCells[i]);
      exit;
    end;
end;

constructor TExportRow.Create;
begin
  inherited Create;
  FCells:=TFPList.Create;
end;

destructor TExportRow.Destroy;
begin
  Clear;
  FreeAndNil(FCells);
  inherited Destroy;
end;

procedure TExportRow.Clear;
var
  i:integer;
begin
  for i:=0 to FCells.Count-1 do
    TExportObject(FCells[i]).Free;
  FCells.Clear;
end;

function TExportRow.ExportObject(AObj: TfrView): TExportObject;
begin
  Result:=TExportObject.Create(AObj);
  FCells.Add(Result);
end;

{ TExportObject }

function TExportObject.GetText: string;
var
  B: Char;
  L: Integer;
begin
  if Assigned(FTexts) and (FTexts.Count>0) then
    Result:=FTexts.Text
  else
    Result:='';
end;

constructor TExportObject.Create(AObj: TfrView);
var
  S: String;
begin
  Inherited Create;

  if Assigned(AObj) then
  begin
    FName:=AObj.Name;
    FTexts:=TStringList.Create;
    FFont:=TFont.Create;

    FLeft:=AObj.X;
    FTop:=AObj.Y;
    FWidht:=AObj.DX;
    FHeight:=AObj.DY;

    FFrames:=AObj.Frames;
    FFrameColor:=AObj.FrameColor;
    FFrameStyle:=AObj.FrameStyle;
    FFrameWidth:=AObj.FrameWidth;

    if AObj is TfrMemoView then
    begin
      Texts.Text:=AObj.Memo.Text;
      FObjType:=gtMemo;
      FFillColor:=AObj.FillColor;
      FFont.Assign(TfrMemoView(AObj).Font);
      FAlignment:=TfrMemoView(AObj).Alignment;
      FAngle:=TfrMemoView(AObj).Angle;
      FLayout:=TfrMemoView(AObj).Layout;
      FWordWrap:=TfrMemoView(AObj).WordWrap;
                                    //http://www.lazarus-ide.org/
      S:=UpperCase(TfrMemoView(AObj).URLInfo);
      if (S <> '') and ((Copy(S, 1,  7) = 'HTTP://') or (Copy(S, 1, 8) = 'HTTPS://')) then
          URLInfo:=TfrMemoView(AObj).URLInfo;
    end
    else
    if AObj is TfrPictureView then
    begin
      FObjType:=gtPicture;
      NeedPicture;
      FPicture.Assign(TfrPictureView(AObj).Picture);
    end
    else
    if AObj is TfrLineView then
      FObjType:=gtLine;
  end;
end;

destructor TExportObject.Destroy;
begin
  if Assigned(FTexts) then
    FreeAndNil(FTexts);
  if Assigned(FFont) then
    FreeAndNil(FFont);

  if Assigned(FPicture) then
    FreeAndNil(FPicture);
  inherited Destroy;
end;

procedure TExportObject.NeedPicture;
begin
  if not Assigned(FPicture) then
    FPicture := TPicture.Create;
end;

{ TExportMatrix }

function TExportMatrix.FindRow(FTop: integer): TExportRow;
var
  i: Integer;
begin
  Result:=nil;
  for i:=0 to FRows.Count-1 do
    if TExportRow(FRows[i]).Top = FTop + FPageMargin then
    begin
      Result:=TExportRow(FRows[i]);
      exit;
    end;
  Result:=TExportRow.Create;
  FRows.Add(Result);
  Result.Top:=FTop + FPageMargin;
end;

function TExportMatrix.FindObj(AObj: TfrView): TExportObject;
var
  i: Integer;
  F: TExportRow;
begin
  Result:=nil;
  F:=nil;
  for i:=0 to FRows.Count-1 do
    if TExportRow(FRows[i]).Top = AObj.Y + FPageMargin then
    begin
      F:=TExportRow(FRows[i]);
      break;
    end;

  if Assigned(F) then
    for i:=0 to F.FCells.Count-1 do
      if TExportObject(F.FCells[i]).Left = AObj.X then
      begin
        Result:=TExportObject(F.FCells[i]);
        exit;
      end;
end;

function TExportMatrix.GetColumnCount: integer;
begin
  Result:=Length(FColWidth);
end;

function TExportMatrix.GetColumnWidth(AColumn: integer): Integer;
begin
  if (AColumn>-1) and (AColumn<Length(FColWidth)) then
    Result:=FColWidth[AColumn]
  else
    Result:=0;
end;

function TExportMatrix.GetColNumByLeft(ALeft: Integer): integer;
var
  i: Integer;
begin
  Result:=0;
  for i:=0 to Length(FColWidth)-1 do
  begin
    if FColWidth[i]=ALeft then
    begin
      Result:=i + 1;
      exit;
    end;
  end;
end;

function TExportMatrix.GetRowNumByTop(ATop: Integer): integer;
var
  i: Integer;
begin
  Result:=0;
  for i:=0 to Length(FRowHight)-1 do
  begin
    if FRowHight[i]=ATop then
    begin
      Result:=i + 1;
      exit;
    end;
  end;
end;

function TExportMatrix.GetObjects(X, Y: integer): TExportObject;
var
  i: Integer;
begin
  Result:=nil;
  for i:=0 to FRows.Count-1 do
    if TExportRow(FRows[i]).FRow = Y then
      Result:=TExportRow(FRows[i]).GetObjects(X);
end;

function TExportMatrix.GetRowCount: integer;
begin
  Result:=Length(FRowHight);
end;

function TExportMatrix.GetRowHight(ARow: integer): Integer;
begin
  if (ARow>-1) and (ARow<Length(FRowHight)) then
    Result:=FRowHight[ARow]
  else
    Result:=0;
end;

procedure TExportMatrix.SortRows;
var
  R, P:TExportRow;
  i: Integer;
  j: Integer;
begin
  for i:=0 to FRows.Count-2 do
  begin
    R:=TExportRow(FRows[i]);
    for j:=i+1 to FRows.Count-1 do
    begin
      if R.FTop > TExportRow(FRows[j]).FTop then
      begin
        P:=TExportRow(FRows[j]);
        FRows[j]:=R;
        R:=P;
      end;
    end;
    FRows[i]:=R;
  end;

  for i:=0 to FRows.Count-1 do
    TExportRow(FRows[i]).SortCells;
end;

procedure TExportMatrix.UpdateColWidth;

procedure TestLeftPos(FLeft:Integer);
var
  i: Integer;
  j: Integer;
begin
  if Length(FColWidth)>0 then
  begin
    i:=0;
    while (i<Length(FColWidth)) and (FColWidth[i]<FLeft) do Inc(i);
    if (i<Length(FColWidth)) and (FColWidth[i] = FLeft) then exit;

    SetLength(FColWidth, Length(FColWidth) + 1);

    for j:=Length(FColWidth) - 1 downto i + 1 do
      FColWidth[j]:=FColWidth[j - 1];
    FColWidth[i]:=FLeft;
  end
  else
  begin
    SetLength(FColWidth, 1);
    FColWidth[0]:=FLeft;
  end;
end;

var
  i: Integer;
  R: TExportRow;
  j: Integer;
  C: TExportObject;
begin
  for i:=0 to FRows.Count - 1 do
  begin
    R:=TExportRow(FRows[i]);
    for j:=0 to R.FCells.Count-1 do
    begin
      C:=TExportObject(R.FCells[j]);
      TestLeftPos(C.FLeft);
      TestLeftPos(C.FLeft + C.Widht);
    end;
  end;
end;

procedure TExportMatrix.UpdateRowHeight;
procedure TestLeftPos(FTop:Integer);
var
  i: Integer;
  j: Integer;
begin
  if Length(FRowHight)>0 then
  begin
    i:=0;
    while (i<Length(FRowHight)) and (FRowHight[i]<FTop) do Inc(i);
    if (i<Length(FRowHight)) and (FRowHight[i] = FTop) then exit;

    SetLength(FRowHight, Length(FRowHight) + 1);

    for j:=Length(FRowHight) - 1 downto i + 1 do
      FRowHight[j]:=FRowHight[j - 1];
    FRowHight[i]:=FTop;
  end
  else
  begin
    SetLength(FRowHight, 1);
    FRowHight[0]:=FTop;
  end;
end;

var
  i: Integer;
  R: TExportRow;
  j: Integer;
  C: TExportObject;
begin
  for i:=0 to FRows.Count - 1 do
  begin
    R:=TExportRow(FRows[i]);
    for j:=0 to R.FCells.Count-1 do
    begin
      C:=TExportObject(R.FCells[j]);
      TestLeftPos(C.FTop);
      TestLeftPos(C.FTop + C.Height);
    end;
  end;
end;

procedure TExportMatrix.DoPostitionObject;
var
  i: Integer;
  j: Integer;
  FObj: TExportObject;
  R: TExportRow;
begin
  for i:=0 to FRows.Count-1 do
  begin
    for j:=0 to TExportRow(FRows[i]).FCells.Count - 1 do
    begin
      FObj:=TExportObject(TExportRow(FRows[i]).FCells[j]);
//      FObj.FRow:=i;
      FObj.FCol:=GetColNumByLeft(FObj.FLeft);
    end;
  end;

  for i:=0 to FRows.Count-1 do
  begin
    R:=TExportRow(FRows[i]);
    R.FRow:=GetRowNumByTop(R.FTop);
    for j:=0 to R.FCells.Count-1 do
      TExportObject(R.FCells[j]).FRow:=R.FRow;
  end;
end;

procedure TExportMatrix.DoMergeCell;

function FindCelNum(APos:integer):integer;
begin
  Result:=0;
  while (Result<Length(FColWidth)) and (FColWidth[Result]<APos) do Inc(Result);
end;

function FindRowNum(APos:integer):integer;
begin
  Result:=0;
  while (Result<Length(FRowHight)) and (FRowHight[Result]<APos) do Inc(Result);
end;

var
  i: Integer;
  j: Integer;
  C: TExportObject;
  R: TExportRow;
begin
  for i:=0 to FRows.Count - 1 do
  begin
    R:=TExportRow(FRows[i]);
    for j:=0 to R.FCells.Count-1 do
    begin
      C:=TExportObject(R.FCells[j]);
      C.FMergedCol:=FindCelNum(C.FLeft + C.FWidht);
      if C.FMergedCol < C.FCol then
        C.FMergedCol:=C.FCol;

      C.FMergedRow:=FindRowNum(C.FTop + C.FHeight);
      if C.FMergedRow < C.FRow then
        C.FMergedRow:=C.FRow;
    end;
  end;
end;

procedure TExportMatrix.DoDeleteEmptyRow;
var
  R: TExportRow;
  i: Integer;
begin
  for i:=FRows.Count-1 downto 0 do
  begin
    R:=TExportRow(FRows[i]);
    if R.FCells.Count = 0 then
    begin
      FRows.Delete(i);
      R.Free;
    end;
  end;
end;

constructor TExportMatrix.Create;
begin
  inherited Create;
  FPageMargin:=0;
  FRows:=TFpList.Create;
  SetLength(FColWidth, 0);
  SetLength(FRowHight, 0);
end;

procedure TExportMatrix.Clear;
var
  i:integer;
begin
  for i:=0 to FRows.Count-1 do
    TExportRow(FRows[i]).Free;
  FRows.Clear;
  SetLength(FColWidth, 0);
  SetLength(FRowHight, 0);
end;

destructor TExportMatrix.Destroy;
begin
  Clear;
  FreeAndNil(FRows);
  inherited Destroy;
end;

function TExportMatrix.ExportObject(AObj: TfrView): TExportObject;
var
  R: TExportRow;
begin
{  Result:=nil;
  if (AObj is TfrMemoView) or ((AObj is TfrPictureView) and FExportImages) then
  begin}
    R:=FindRow(AObj.Y);
    Result:=R.ExportObject(AObj);
    Result.Top:=R.Top;
//  end
end;

procedure TExportMatrix.PrepareData;
var
  i: Integer;
begin
  SortRows;
  if FDeleteEmptyRow then
    DoDeleteEmptyRow;

  UpdateRowHeight;
  UpdateColWidth;
  DoPostitionObject;

  if FMergeCell then
    DoMergeCell;

  for i:=Length(FColWidth) - 1 downto 1 do
    FColWidth[i]:=FColWidth[i] - FColWidth[i - 1];

  for i:=Length(FRowHight) - 1 downto 1 do
    FRowHight[i]:=FRowHight[i] - FRowHight[i - 1];
end;

procedure TExportMatrix.NewPage;
var
  R: TExportRow;
  i: Integer;
  imax: Integer;
  FTop: Integer;
  FHeight: Integer;
  C: TExportObject;
  O: TExportObject;
begin
  if FRows.Count = 0 then exit;

  FTop:=0;
  imax:=0;
  FHeight:=0;
  for i:=0 to FRows.Count-1 do
  begin
    R:=TExportRow(FRows[i]);
    if R.Top > FTop then
    begin
      FTop:=R.Top;
      imax:=i;
    end;
  end;

  R:=TExportRow(FRows[imax]);

  for i:=0 to R.FCells.Count-1 do
  begin
    C:=TExportObject(R.FCells[i]);
    if C.Height > FHeight then
      FHeight:=C.Height;
  end;

  R:=FindRow(FTop + FHeight);
  R.FTop:=FTop + FHeight + 1;

  FPageMargin:=R.FTop + FHeight + 1;
end;

end.