File: tadiagramdrawing.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 (217 lines) | stat: -rw-r--r-- 5,358 bytes parent folder | download | duplicates (5)
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
{
 *****************************************************************************
  See the file COPYING.modifiedLGPL.txt, included in this distribution,
  for details about the license.
 *****************************************************************************

  Authors: Alexander Klenin

}
unit TADiagramDrawing;

{$H+}

interface

uses
  Classes, FPCanvas,
  TADrawUtils, TADiagram;

type
  TDiaContextDrawer = class(TDiaContext)
  private
    FDrawer: IChartDrawer;
  public
    property Drawer: IChartDrawer read FDrawer write FDrawer;
  end;

  IDiaDrawerDecorator = interface
    ['{A2AB054F-D725-401D-A249-18BF03CFF6FA}']
    procedure Apply(ADrawer: IChartDrawer);
  end;

  TDiaBrushDecorator = class(TDiaDecorator, IDiaDrawerDecorator)
  private
    FBrush: TFPCustomBrush;
  public
    constructor Create(AOwner: TDiaDecoratorList);
    destructor Destroy; override;
    procedure Apply(ADrawer: IChartDrawer);
    property Brush: TFPCustomBrush read FBrush;
  end;

  TDiaFontDecorator = class(TDiaDecorator, IDiaDrawerDecorator)
  private
    FFont: TFPCustomFont;
  public
    constructor Create(AOwner: TDiaDecoratorList);
    destructor Destroy; override;
    procedure Apply(ADrawer: IChartDrawer);
    property Font: TFPCustomFont read FFont;
  end;

  TDiaPenDecorator = class(TDiaDecorator, IDiaDrawerDecorator)
  private
    FPen: TFPCustomPen;
  public
    constructor Create(AOwner: TDiaDecoratorList);
    destructor Destroy; override;
    procedure Apply(ADrawer: IChartDrawer);
    property Pen: TFPCustomPen read FPen;
  end;

implementation

uses
  Math, Types, SysUtils,
  TAGeometry;

function ToImage(const AP: TDiaPoint): TPoint; inline;
begin
  Result := RoundPoint(AP.AsUnits(duPixels));
end;

procedure DrawDiaBox(ASelf: TDiaBox);
var
  id: IChartDrawer;
  d: IDiaDecorator;
begin
  id := (ASelf.Owner.Context as TDiaContextDrawer).Drawer;
  id.PrepareSimplePen($000000);
  id.SetBrushColor($FFFFFF);
  for d in ASelf.Decorators do
    if d is IDiaDrawerDecorator then
      (d as IDiaDrawerDecorator).Apply(id);
  with ASelf do
    id.Polygon([
      ToImage(FTopLeft), ToImage(FTopRight),
      ToImage(FBottomRight), ToImage(FBottomLeft)
    ], 0, 4);
  id.TextOut.Pos(ToImage(ASelf.FTopLeft) + Point(4, 4)).Text(ASelf.Caption).Done;
end;

procedure DrawEndPoint(
  ADrawer: IChartDrawer; AEndPoint: TDiaEndPoint;
  const APos: TPoint; AAngle: Double);
var
  da: Double;
  diag: Integer;
  pt1, pt2: TPoint;
  d: IDiaDecorator;
begin
  ADrawer.SetPenParams(psSolid, $000000);
  ADrawer.SetBrushColor($FFFFFF);
  for d in AEndPoint.Decorators do
    if d is IDiaDrawerDecorator then
      (d as IDiaDrawerDecorator).Apply(ADrawer);
  da := ArcTan2(AEndPoint.Width.Value, AEndPoint.Length.Value);

  diag := -Round(Sqrt(Sqr(AEndPoint.Length.Value) + Sqr(AEndPoint.Width.Value)));
  pt1 := APos + RotatePointX(diag, AAngle - da);
  pt2 := APos + RotatePointX(diag, AAngle + da);
  case AEndPoint.Shape of
    depsClosedArrow: ADrawer.Polygon([pt1, APos, pt2], 0, 3);
    depsOpenArrow: ADrawer.Polyline([pt1, APos, pt2], 0, 3);
  end;
end;

procedure DrawDiaLink(ASelf: TDiaLink);
var
  id: IChartDrawer;
  startPos, endPos, p: TPoint;
  d: IDiaDecorator;
begin
  if (ASelf.Start.Connector = nil) or (ASelf.Finish.Connector = nil) then exit;
  id := (ASelf.Owner.Context as TDiaContextDrawer).Drawer;
  id.PrepareSimplePen($000000);
  for d in ASelf.Decorators do
    if d is IDiaDrawerDecorator then
      (d as IDiaDrawerDecorator).Apply(id);
  startPos := ToImage(ASelf.Start.Connector.ActualPos);
  endPos := ToImage(ASelf.Finish.Connector.ActualPos);
  case ASelf.Routing of
    dlrStraight: begin
      id.Line(startPos, endPos);
      p := startPos;
    end;
    dlrXThenY: begin
      p := Point(endPos.X, startPos.Y);
      id.Polyline([startPos, p, endPos], 0, 3);
    end;
    dlrYThenX: begin
      p := Point(startPos.X, endPos.Y);
      id.Polyline([startPos, p, endPos], 0, 3);
    end;
  end;
  if ASelf.Start.Shape <> depsNone then
    with p - endPos do
      DrawEndPoint(id, ASelf.Start, startPos, ArcTan2(Y, X));
  if ASelf.Finish.Shape <> depsNone then
    with endPos - p do
      DrawEndPoint(id, ASelf.Finish, endPos, ArcTan2(Y, X));
end;

{ TDiaBrushDecorator }

procedure TDiaBrushDecorator.Apply(ADrawer: IChartDrawer);
begin
  ADrawer.Brush := Brush;
end;

constructor TDiaBrushDecorator.Create(AOwner: TDiaDecoratorList);
begin
  inherited Create(AOwner);
  FBrush := TFPCustomBrush.Create;
end;

destructor TDiaBrushDecorator.Destroy;
begin
  FreeAndNil(FBrush);
  inherited;
end;

{ TDiaFontDecorator }

procedure TDiaFontDecorator.Apply(ADrawer: IChartDrawer);
begin
  ADrawer.Font := Font;
end;

constructor TDiaFontDecorator.Create(AOwner: TDiaDecoratorList);
begin
  inherited Create(AOwner);
  FFont := TFPCustomFont.Create;
end;

destructor TDiaFontDecorator.Destroy;
begin
  FreeAndNil(FFont);
  inherited Destroy;
end;

{ TDiaPenDecorator }

procedure TDiaPenDecorator.Apply(ADrawer: IChartDrawer);
begin
  ADrawer.Pen := Pen;
end;

constructor TDiaPenDecorator.Create(AOwner: TDiaDecoratorList);
begin
  inherited Create(AOwner);
  FPen := TFPCustomPen.Create;
  FPen.Mode := pmCopy;
end;

destructor TDiaPenDecorator.Destroy;
begin
  FreeAndNil(FPen);
  inherited;
end;

initialization
  TDiaBox.FInternalDraw := @DrawDiaBox;
  TDiaLink.FInternalDraw := @DrawDiaLink;

end.