-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcadiagram.pas
749 lines (639 loc) · 19.2 KB
/
cadiagram.pas
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
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
unit caDiagram;
{$INCLUDE ca.inc}
interface
uses
// Standard Delphi units
SysUtils,
Windows,
Messages,
Classes,
Graphics,
Controls,
Forms,
Dialogs,
ExtCtrls,
Menus,
Contnrs,
// ca units
caClasses,
caUtils,
caControls,
caSizeMovePanel;
type
TcaDiagramEdge = (deLeft, deRight, deTop, deBottom);
//```````````````````````````````````````````````````````````````````````````
// TcaDiagramLink
//```````````````````````````````````````````````````````````````````````````
TcaDiagramElement = class;
TcaDiagramLink = class(TCollectionItem)
private
// Property fields
FLinkFrom: TcaDiagramElement;
FLinkTo: TcaDiagramElement;
protected
// Protected methods
function GetDisplayName: string; override;
procedure SetDisplayName(const Value: string); override;
public
// Create/Destroy
constructor Create(ACollection: TCollection); override;
destructor Destroy; override;
// Public methods
procedure Assign(Source: TPersistent); override;
published
// Properties
property LinkFrom: TcaDiagramElement read FLinkFrom write FLinkFrom;
property LinkTo: TcaDiagramElement read FLinkTo write FLinkTo;
end;
//```````````````````````````````````````````````````````````````````````````
// TcaDiagramLinks
//```````````````````````````````````````````````````````````````````````````
TcaDiagramLinks = class(TOwnedCollection)
private
// Property methods
function GetItem(Index: Integer): TcaDiagramLink;
procedure SetItem(Index: Integer; Value: TcaDiagramLink);
protected
// Protected methods
procedure Update(Item: TCollectionItem); override;
public
// Public methods
function Add: TcaDiagramLink;
// Properties
property Items[Index: Integer]: TcaDiagramLink read GetItem write SetItem; default;
end;
//```````````````````````````````````````````````````````````````````````````
// TcaDiagramElement
//```````````````````````````````````````````````````````````````````````````
TcaDiagramElement = class(TcaSizeMovePanel)
private
// Property fields
FCenterPoint: TPoint;
FOldLeft: Integer;
FOldTop: Integer;
// Private methods
procedure UpdateCenterPoint;
procedure WMWindowPosChanging(var Message: TWMWindowPosChanging); message WM_WINDOWPOSCHANGING;
procedure WMWindowPosChanged(var Message: TWMWindowPosChanged); message WM_WINDOWPOSCHANGED;
protected
// Protected methods
procedure Moved; virtual;
// Protected properties
property OldLeft: Integer read FOldLeft;
property OldTop: Integer read FOldTop;
public
// Create/Destroy
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
// Properties
property CenterPoint: TPoint read FCenterPoint;
end;
//```````````````````````````````````````````````````````````````````````````
// TcaDiagramLineNode
//```````````````````````````````````````````````````````````````````````````
TcaDiagramBox = class;
TcaDiagramLineNode = class(TcaDiagramElement)
private
// Property fields
FDiagramBox: TcaDiagramBox;
FLinkedNode: TcaDiagramLineNode;
// Windows message handlers
procedure WMExitSizeMove(var Msg: TMessage); message WM_EXITSIZEMOVE;
public
// Create/Destroy
constructor Create(AOwner: TComponent); override;
// Properties
property DiagramBox: TcaDiagramBox read FDiagramBox write FDiagramBox;
property LinkedNode: TcaDiagramLineNode read FLinkedNode write FLinkedNode;
end;
//```````````````````````````````````````````````````````````````````````````
// TcaDiagramLineNodeList
//```````````````````````````````````````````````````````````````````````````
TcaDiagram = class;
TcaDiagramLineNodeList = class(TObject)
private
// Private fields
FDiagram: TcaDiagram;
FList: TObjectList;
// Property methods
function GetCount: Integer;
function GetItem(Index: Integer): TcaDiagramLineNode;
public
// Create/Destroy
constructor Create(ADiagram: TcaDiagram);
destructor Destroy; override;
// Public methods
function Add(ADiagramBox: TcaDiagramBox; ALeft, ATop: Integer): TcaDiagramLineNode;
function IndexOf(AItem: TcaDiagramLineNode): Integer;
procedure Clear;
// Properties
property Count: Integer read GetCount;
property Items[Index: Integer]: TcaDiagramLineNode read GetItem; default;
end;
//```````````````````````````````````````````````````````````````````````````
// TcaDiagramBox
//```````````````````````````````````````````````````````````````````````````
TcaDiagramBox = class(TcaDiagramElement)
private
// Private fields
FDiagram: TcaDiagram;
// Property fields
FNodes: TcaDiagramLineNodeList;
// Private methods
function GetNearestEdge(ANodeX, ANodeY: Integer): TcaDiagramEdge;
procedure MoveNodes;
protected
// Protected methods
function MakeEdgePoint: TPoint;
procedure DblClick; override;
procedure Moved; override;
procedure SetParent(AParent: TWinControl); override;
public
// Create/Destroy
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
// Properties
property Nodes: TcaDiagramLineNodeList read FNodes write FNodes;
end;
//```````````````````````````````````````````````````````````````````````````
// TcaDiagramController
//```````````````````````````````````````````````````````````````````````````
TcaDiagramController = class(TComponent)
private
// Property fields
FDiagram: TcaDiagram;
FFormCanvas: TCanvas;
FLinks: TcaDiagramLinks;
FNodeFrom: TcaDiagramLineNode;
FNodeTo: TcaDiagramLineNode;
// Property methods
procedure SetDiagram(const Value: TcaDiagram);
procedure SetLinks(const Value: TcaDiagramLinks);
// Private methods
procedure LinkNodes;
procedure ResetNodePoints;
protected
// Protected methods
procedure Notification(AComponent: TComponent; Operation: TOperation); override;
public
// Create/Destroy
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
// Public methods
procedure AddNode(ANode: TcaDiagramLineNode);
published
// Published properties
property Diagram: TcaDiagram read FDiagram write SetDiagram;
property Links: TcaDiagramLinks read FLinks write SetLinks;
end;
//```````````````````````````````````````````````````````````````````````````
// TcaDiagram
//```````````````````````````````````````````````````````````````````````````
TcaDiagram = class(TCustomPanel)
private
// Property fields
FController: TcaDiagramController;
// Property methods
procedure SetController(const Value: TcaDiagramController);
// Private methods
procedure PaintDiagram;
protected
// Protected methods
procedure Notification(AComponent: TComponent; Operation: TOperation); override;
procedure Paint; override;
public
// Create/Destroy
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
published
// Properties
property Controller: TcaDiagramController read FController write SetController;
end;
implementation
uses Types;
//```````````````````````````````````````````````````````````````````````````
// TcaDiagramLink
//```````````````````````````````````````````````````````````````````````````
// Create/Destroy
constructor TcaDiagramLink.Create(ACollection: TCollection);
begin
inherited Create(ACollection);
end;
destructor TcaDiagramLink.Destroy;
begin
inherited;
end;
// Public methods
procedure TcaDiagramLink.Assign(Source: TPersistent);
var
SourceLink: TcaDiagramLink;
begin
if Source is TcaDiagramLink then
begin
SourceLink := TcaDiagramLink(Source);
Utils.ShallowCopy(SourceLink, Self);
end
else
inherited;
end;
// Property methods
function TcaDiagramLink.GetDisplayName: string;
begin
Result := inherited GetDisplayName;
end;
procedure TcaDiagramLink.SetDisplayName(const Value: string);
begin
inherited;
end;
//```````````````````````````````````````````````````````````````````````````
// TcaDiagramLinks
//```````````````````````````````````````````````````````````````````````````
// Public methods
function TcaDiagramLinks.Add: TcaDiagramLink;
begin
Result := TcaDiagramLink(inherited Add);
end;
// Protected methods
procedure TcaDiagramLinks.Update(Item: TCollectionItem);
begin
inherited;
end;
// Property methods
function TcaDiagramLinks.GetItem(Index: Integer): TcaDiagramLink;
begin
Result := TcaDiagramLink(inherited GetItem(Index));
end;
procedure TcaDiagramLinks.SetItem(Index: Integer; Value: TcaDiagramLink);
begin
inherited SetItem(Index, Value);
end;
//```````````````````````````````````````````````````````````````````````````
// TcaDiagramElement
//```````````````````````````````````````````````````````````````````````````
// Create/Destroy
constructor TcaDiagramElement.Create(AOwner: TComponent);
begin
inherited;
end;
destructor TcaDiagramElement.Destroy;
begin
inherited;
end;
// Protected methods
procedure TcaDiagramElement.Moved;
begin
UpdateCenterPoint;
if Parent <> nil then
Parent.Invalidate;
end;
// Private methods
procedure TcaDiagramElement.UpdateCenterPoint;
begin
FCenterPoint := Point(Left, Top);
FCenterPoint.X := FCenterPoint.X + (Width div 2);
FCenterPoint.Y := FCenterPoint.Y + (Height div 2);
end;
// Windows messgae handlers
procedure TcaDiagramElement.WMWindowPosChanging(var Message: TWMWindowPosChanging);
begin
inherited;
FOldLeft := Left;
FOldTop := Top;
end;
procedure TcaDiagramElement.WMWindowPosChanged(var Message: TWMWindowPosChanged);
begin
inherited;
Moved;
end;
//```````````````````````````````````````````````````````````````````````````
// TcaDiagramLineNode
//```````````````````````````````````````````````````````````````````````````
// Create/Destroy
constructor TcaDiagramLineNode.Create(AOwner: TComponent);
begin
inherited;
Width := 3;
Height := 3;
Constraints.MinWidth := Width;
Constraints.MaxWidth := Width;
Constraints.MinHeight := Height;
Constraints.MaxHeight := Height;
Color := clRed;
Cursor := crSizeAll;
Options := [smMoving];
Frame.Sides := [];
end;
// Windows message handlers
procedure TcaDiagramLineNode.WMExitSizeMove(var Msg: TMessage);
var
EdgePoint: TPoint;
begin
inherited;
if Assigned(FDiagramBox) then
begin
if (Left >= (FDiagramBox.Left - 4))
and (Left <= (FDiagramBox.Left + FDiagramBox.Width))
and (Top >= (FDiagramBox.Top - 4))
and (Top <= (FDiagramBox.Top + FDiagramBox.Height)) then
begin
EdgePoint := DiagramBox.MakeEdgePoint;
if Abs(EdgePoint.X - FLinkedNode.Left) < 8 then
EdgePoint.X := FLinkedNode.Left;
if Abs(EdgePoint.Y - FLinkedNode.Top) < 8 then
EdgePoint.Y := FLinkedNode.Top;
Left := EdgePoint.X;
Top := EdgePoint.Y;
end;
end;
end;
//```````````````````````````````````````````````````````````````````````````
// TcaDiagramLineNodeList
//```````````````````````````````````````````````````````````````````````````
// Create/Destroy
constructor TcaDiagramLineNodeList.Create(ADiagram: TcaDiagram);
begin
inherited Create;
FDiagram := ADiagram;
FList := TObjectList.Create(False);
end;
destructor TcaDiagramLineNodeList.Destroy;
begin
FList.Free;
inherited;
end;
// Public methods
function TcaDiagramLineNodeList.Add(ADiagramBox: TcaDiagramBox; ALeft, ATop: Integer): TcaDiagramLineNode;
begin
Result := TcaDiagramLineNode.Create(nil);
Result.Left := ALeft;
Result.Top := ATop;
Result.Parent := FDiagram;
Result.DiagramBox := ADiagramBox;
FList.Add(Result);
end;
function TcaDiagramLineNodeList.IndexOf(AItem: TcaDiagramLineNode): Integer;
begin
Result := FList.IndexOf(AItem);
end;
procedure TcaDiagramLineNodeList.Clear;
begin
FList.Clear;
end;
// Property methods
function TcaDiagramLineNodeList.GetCount: Integer;
begin
Result := FList.Count;
end;
function TcaDiagramLineNodeList.GetItem(Index: Integer): TcaDiagramLineNode;
begin
Result := TcaDiagramLineNode(FList[Index]);
end;
//```````````````````````````````````````````````````````````````````````````
// TcaDiagramBox
//```````````````````````````````````````````````````````````````````````````
// Create/Destroy
constructor TcaDiagramBox.Create(AOwner: TComponent);
begin
inherited;
DoubleBuffered := True;
SizingPixels := 6;
Color := clCream;
end;
destructor TcaDiagramBox.Destroy;
begin
FNodes.Free;
inherited;
end;
// Protected methods
function TcaDiagramBox.MakeEdgePoint: TPoint;
var
CursorPos: TPoint;
NearestEdge: TcaDiagramEdge;
NodeX: Integer;
NodeY: Integer;
begin
CursorPos := ScreenToClient(Mouse.CursorPos);
NodeX := CursorPos.X + Left;
NodeY := CursorPos.Y + Top;
NearestEdge := GetNearestEdge(CursorPos.X, CursorPos.Y);
case NearestEdge of
deLeft: NodeX := Left - 3;
deRight: NodeX := Left + Width;
deTop: NodeY := Top - 3;
deBottom: NodeY := Top + Height;
end;
Result.X := NodeX;
Result.Y := NodeY;
end;
procedure TcaDiagramBox.DblClick;
var
EdgePoint: TPoint;
Node: TcaDiagramLineNode;
begin
inherited;
EdgePoint := MakeEdgePoint;
Node := FNodes.Add(Self, EdgePoint.X, EdgePoint.Y);
Node.BringToFront;
if FDiagram.Controller <> nil then
FDiagram.Controller.AddNode(Node);
end;
procedure TcaDiagramBox.Moved;
begin
inherited;
MoveNodes;
end;
procedure TcaDiagramBox.SetParent(AParent: TWinControl);
begin
inherited;
FDiagram := nil;
if AParent is TcaDiagram then
FNodes := TcaDiagramLineNodeList.Create(TcaDiagram(AParent));
FDiagram := TcaDiagram(AParent);
end;
// Private methods
function TcaDiagramBox.GetNearestEdge(ANodeX, ANodeY: Integer): TcaDiagramEdge;
var
LeftRight: TcaDiagramEdge;
TopBottom: TcaDiagramEdge;
begin
LeftRight := deLeft;
if ANodeX > (Width div 2 + 1) then LeftRight := deRight;
TopBottom := deTop;
if ANodeY > (Height div 2 + 1) then TopBottom := deBottom;
if LeftRight = deLeft then
begin
if TopBottom = deTop then
begin
if ANodeY < ANodeX then
Result := deTop
else
Result := deLeft;
end
else
begin
if (Height - ANodeY) < ANodeX then
Result := deBottom
else
Result := deLeft;
end;
end
else
begin
if TopBottom = deTop then
begin
if ANodeY < (Width - ANodeX) then
Result := deTop
else
Result := deRight;
end
else
begin
if (Height - ANodeY) < (Width - ANodeX) then
Result := deBottom
else
Result := deRight;
end;
end;
end;
procedure TcaDiagramBox.MoveNodes;
var
DeltaX: Integer;
DeltaY: Integer;
Index: Integer;
Node: TcaDiagramLineNode;
begin
DeltaX := Left - OldLeft;
DeltaY := Top - FOldTop;
for Index := 0 to Pred(FNodes.Count) do
begin
Node := FNodes[Index];
Node.Left := Node.Left + DeltaX;
Node.Top := Node.Top + DeltaY;
Node.BringToFront;
end;
end;
//```````````````````````````````````````````````````````````````````````````
// TcaDiagramController
//```````````````````````````````````````````````````````````````````````````
// Create/Destroy
constructor TcaDiagramController.Create(AOwner: TComponent);
begin
inherited;
FFormCanvas := TCanvas.Create;
FLinks := TcaDiagramLinks.Create(Self, TcaDiagramLink);
ResetNodePoints;
end;
destructor TcaDiagramController.Destroy;
begin
FFormCanvas.Free;
FLinks.Free;
inherited;
end;
// Public methods
procedure TcaDiagramController.AddNode(ANode: TcaDiagramLineNode);
begin
if not Assigned(FNodeFrom) then
FNodeFrom := ANode
else
begin
FNodeTo := ANode;
LinkNodes;
end;
end;
// Protected methods
procedure TcaDiagramController.Notification(AComponent: TComponent; Operation: TOperation);
begin
inherited;
if (Operation = opRemove) and (AComponent = FDiagram) then
FDiagram := nil;
end;
// Private methods
procedure TcaDiagramController.LinkNodes;
var
Link: TcaDiagramLink;
begin
Link := FLinks.Add;
Link.LinkFrom := FNodeFrom;
Link.LinkTo := FNodeTo;
FNodeFrom.LinkedNode := FNodeTo;
FNodeTo.LinkedNode := FNodeFrom;
ResetNodePoints;
FDiagram.Invalidate;
end;
procedure TcaDiagramController.ResetNodePoints;
begin
FNodeFrom := nil;
FNodeTo := nil;
end;
// Property methods
procedure TcaDiagramController.SetDiagram(const Value: TcaDiagram);
begin
if Value <> FDiagram then
begin
FDiagram := Value;
if Assigned(FDiagram) then
FDiagram.Controller := Self;
end;
end;
procedure TcaDiagramController.SetLinks(const Value: TcaDiagramLinks);
begin
FLinks.Assign(Value);
end;
//```````````````````````````````````````````````````````````````````````````
// TcaDiagram
//```````````````````````````````````````````````````````````````````````````
// Create/Destroy
constructor TcaDiagram.Create(AOwner: TComponent);
begin
inherited;
Caption := '';
DoubleBuffered := True;
end;
destructor TcaDiagram.Destroy;
begin
inherited;
end;
// Protected methods
procedure TcaDiagram.Notification(AComponent: TComponent; Operation: TOperation);
begin
inherited;
if (Operation = opRemove) and (AComponent = FController) then
FController := nil;
end;
procedure TcaDiagram.Paint;
begin
inherited;
if Assigned(FController) then
PaintDiagram;
end;
// Private methods
procedure TcaDiagram.PaintDiagram;
var
ElementFrom: TcaDiagramElement;
ElementTo: TcaDiagramElement;
Index: Integer;
Link: TcaDiagramLink;
begin
Canvas.Pen.Color := clRed;
Canvas.Pen.Width := 1;
Canvas.Pen.Style := psSolid;
for Index := 0 to Pred(FController.Links.Count) do
begin
Link := TcaDiagramLink(FController.Links[Index]);
ElementFrom := Link.LinkFrom;
ElementTo := Link.LinkTo;
Canvas.MoveTo(ElementFrom.CenterPoint.X, ElementFrom.CenterPoint.Y);
Canvas.LineTo(ElementTo.CenterPoint.X, ElementTo.CenterPoint.Y);
end;
end;
// Property methods
procedure TcaDiagram.SetController(const Value: TcaDiagramController);
begin
if Value <> FController then
begin
FController := Value;
if Assigned(FController) then
FController.Diagram := Self;
end;
end;
end.