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 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988
|
{
*****************************************************************************
See the file COPYING.modifiedLGPL.txt, included in this distribution,
for details about the license.
*****************************************************************************
Authors: Alexander Klenin
}
unit TADiagram;
{$H+}
interface
uses
Types,
TAChartUtils;
type
TDiaObject = class(TInterfacedObject)
public
procedure Notify(ASender: TDiaObject); virtual; abstract;
procedure Changed(ASender: TDiaObject); virtual; abstract;
end;
TDiaCoordConverter = class
function AxisToGraph(AP: TDoublePoint): TDoublePoint; virtual;
function GraphToAxis(AP: TDoublePoint): TDoublePoint; virtual;
function GraphToImage(AP: TDoublePoint): TPoint; virtual;
function ImageToGraph(AP: TPoint): TDoublePoint; virtual;
function ImageToDevice(AP: TPoint): TDoublePoint; virtual;
function DeviceToImage(AP: TDoublePoint): TPoint; virtual;
end;
TDiaContext = class(TDiaObject)
private
FConverter: TDiaCoordConverter;
public
property Converter: TDiaCoordConverter read FConverter write FConverter;
end;
TDiaUnits = (duNone, duAxis, duGraph, duPixels, duCentimeters);
TDiaBoxSide = (dbsTop, dbsLeft, dbsRight, dbsBottom);
TDiaCoordinate = array [duAxis .. High(TDiaUnits)] of Double;
TDiaPoint = object
public
X, Y: TDiaCoordinate;
function AsUnits(AUnits: TDiaUnits): TDoublePoint; inline;
end;
TDiaElement = class;
TDiaDecoratorList = class;
IDiaDecorator = interface
end;
TDiaDecorator = class(TDiaObject, IDiaDecorator)
private
FOwner: TDiaObject;
public
constructor Create(AOwner: TDiaDecoratorList);
property Owner: TDiaObject read FOwner;
end;
TDiaDecoratorEnumerator = class;
TDiaDecoratorList = class(TDiaObject)
private
FDecorators: array of IDiaDecorator;
FOwner: TDiaObject;
procedure Add(ADecorator: IDiaDecorator);
public
constructor Create(AOwner: TDiaObject);
procedure Changed(ASender: TDiaObject); override;
procedure Notify(ASender: TDiaObject); override;
property Owner: TDiaObject read FOwner;
function GetEnumerator: TDiaDecoratorEnumerator;
end;
TDiaDecoratorEnumerator = class
FList: TDiaDecoratorList;
FPosition: Integer;
public
constructor Create(AList: TDiaDecoratorList);
function GetCurrent: IDiaDecorator;
function MoveNext: Boolean;
property Current: IDiaDecorator read GetCurrent;
end;
TDiagram = class(TDiaObject)
strict private
FBounds: array [TDiaBoxSide] of TDiaCoordinate;
FContext: TDiaContext;
FElements: array of TDiaElement;
function GetBounds(AIndex: TDiaBoxSide): TDiaCoordinate;
procedure SetBounds(AIndex: TDiaBoxSide; AValue: TDiaCoordinate);
procedure SetContext(AValue: TDiaContext);
public
destructor Destroy; override;
procedure Draw;
procedure Add(AElement: TDiaElement);
procedure SetPixelBounds(const ARect: TRect);
function FindElementByPoint(
const APoint: TDoublePoint; AUnits: TDiaUnits): TDiaElement;
procedure Changed(ASender: TDiaObject); override;
procedure Notify(ASender: TDiaObject); override;
property Context: TDiaContext read FContext write SetContext;
property Bounds[AIndex: TDiaBoxSide]: TDiaCoordinate
read GetBounds write SetBounds; default;
end;
TDiaConnector = class;
TDiaElement = class(TDiaObject)
strict private
FConnectors: array of TDiaConnector;
FOwner: TDiagram;
private
FDecorators: TDiaDecoratorList;
protected
procedure SetOwner(AValue: TDiagram); virtual;
public
constructor Create;
destructor Destroy; override;
function DiaPoint(AX, AY: TDiaBoxSide): TDiaPoint; inline;
procedure Add(AConnector: TDiaConnector);
procedure Changed(ASender: TDiaObject); override;
procedure Draw; virtual;
function IsPointInside(
const APoint: TDoublePoint; AUnits: TDiaUnits): Boolean; virtual;
procedure Notify(ASender: TDiaObject); override;
property Owner: TDiagram read FOwner;
property Decorators: TDiaDecoratorList read FDecorators;
end;
TDiaPosition = object
strict private
FPercentage: Boolean;
FReverse: Boolean;
FUnits: TDiaUnits;
FValue: Double;
procedure SetPercentage(AValue: Boolean);
procedure SetReverse(AValue: Boolean);
procedure SetUnits(AValue: TDiaUnits);
procedure SetValue(AValue: Double);
procedure Notify;
private
FOwner: TDiaElement;
public
constructor Init(AOwner: TDiaElement);
procedure Assign(const ASource: TDiaPosition);
class function Equals(const A, B: TDiaPosition): Boolean;
function Calc(const A, B: TDiaPoint): TDiaPoint;
property Owner: TDiaElement read FOwner;
public
function Defined: Boolean; inline;
property Percentage: Boolean read FPercentage write SetPercentage;
property Reverse: Boolean read FReverse write SetReverse;
property Units: TDiaUnits read FUnits write SetUnits;
property Value: Double read FValue write SetValue;
end;
TDiaCenterElement = class(TDiaElement)
strict private
FActualPos: TDiaPoint;
FLeft: TDiaPosition;
FTop: TDiaPosition;
procedure SetLeft(AValue: TDiaPosition);
procedure SetTop(AValue: TDiaPosition);
public
constructor Create;
procedure Changed(ASender: TDiaObject); override;
public
property ActualPos: TDiaPoint read FActualPos;
property Left: TDiaPosition read FLeft write SetLeft;
property Top: TDiaPosition read FTop write SetTop;
end;
TDiaBox = class(TDiaElement)
strict private
FBottom: TDiaPosition;
FCaption: String;
FHeight: TDiaPosition;
FLeft: TDiaPosition;
FRight: TDiaPosition;
FTop: TDiaPosition;
FWidth: TDiaPosition;
procedure SetBottom(const AValue: TDiaPosition);
procedure SetCaption(AValue: String);
procedure SetHeight(const AValue: TDiaPosition);
procedure SetLeft(const AValue: TDiaPosition);
procedure SetRight(const AValue: TDiaPosition);
procedure SetTop(const AValue: TDiaPosition);
procedure SetWidth(const AValue: TDiaPosition);
public
FBottomLeft: TDiaPoint;
FBottomRight: TDiaPoint;
FTopLeft: TDiaPoint;
FTopRight: TDiaPoint;
class var
FInternalDraw: procedure (ASelf: TDiaBox);
public
constructor Create;
procedure Draw; override;
procedure Changed(ASender: TDiaObject); override;
function IsPointInside(
const APoint: TDoublePoint; AUnits: TDiaUnits): Boolean; override;
public
property Left: TDiaPosition read FLeft write SetLeft;
property Top: TDiaPosition read FTop write SetTop;
property Right: TDiaPosition read FRight write SetRight;
property Bottom: TDiaPosition read FBottom write SetBottom;
property Caption: String read FCaption write SetCaption;
//property Height: TDiaPosition read FHeight write SetHeight;
//property Width: TDiaPosition read FWidth write SetWidth;
end;
TDiaConnector = class(TDiaObject)
strict private
FElement: TDiaElement;
FPosition: TDiaPosition;
procedure SetPosition(const AValue: TDiaPosition);
private
FActualPos: TDiaPoint;
procedure SetElement(AValue: TDiaElement);
public
constructor Create;
procedure Changed(ASender: TDiaObject); override;
procedure Notify(ASender: TDiaObject); override;
property ActualPos: TDiaPoint read FActualPos;
property Element: TDiaElement read FElement;
property Position: TDiaPosition read FPosition write SetPosition;
end;
TDiaCenterConnector = class(TDiaConnector)
public
procedure Changed(ASender: TDiaObject); override;
end;
TDiaBoxConnector = class(TDiaConnector)
private
FSide: TDiaBoxSide;
procedure SetSide(AValue: TDiaBoxSide);
public
procedure Changed(ASender: TDiaObject); override;
published
property Side: TDiaBoxSide read FSide write SetSide;
end;
TDiaEndPointShape = (depsNone, depsOpenArrow, depsClosedArrow);
TDiaEndPoint = class(TDiaElement)
private
FConnector: TDiaConnector;
FLength: TDiaPosition;
FShape: TDiaEndPointShape;
FWidth: TDiaPosition;
procedure SetConnector(AValue: TDiaConnector);
procedure SetLength(const AValue: TDiaPosition);
procedure SetShape(AValue: TDiaEndPointShape);
procedure SetWidth(const AValue: TDiaPosition);
public
constructor Create;
property Connector: TDiaConnector read FConnector write SetConnector;
property Length: TDiaPosition read FLength write SetLength;
property Shape: TDiaEndPointShape read FShape write SetShape;
property Width: TDiaPosition read FWidth write SetWidth;
end;
TDiaLinkRouting = (dlrStraight, dlrXThenY, dlrYThenX);
TDiaLink = class(TDiaElement)
private
FFinish: TDiaEndPoint;
FRouting: TDiaLinkRouting;
FStart: TDiaEndPoint;
procedure SetFinish(AValue: TDiaEndPoint);
procedure SetRouting(AValue: TDiaLinkRouting);
procedure SetStart(AValue: TDiaEndPoint);
protected
procedure SetOwner(AValue: TDiagram); override;
public
class var
FInternalDraw: procedure (ASelf: TDiaLink);
constructor Create;
destructor Destroy; override;
procedure Draw; override;
published
property Start: TDiaEndPoint read FStart write SetStart;
property Finish: TDiaEndPoint read FFinish write SetFinish;
property Routing: TDiaLinkRouting read FRouting write SetRouting;
end;
function DiaPos(
AValue: Double; AUnits: TDiaUnits = duNone;
AReverse: Boolean = false; APercentage: Boolean = false): TDiaPosition; inline;
operator =(const A, B: TDiaPosition): Boolean; overload; inline;
implementation
uses
SysUtils,
TAGeometry;
const
DEFAULT_DPI = 72;
CM_PER_INCH = 2.54;
function XY(const AX, AY: TDiaPoint): TDiaPoint;
var
u: TDiaUnits;
begin
for u in TDiaUnits do begin
Result.X[u] := AX.X[u];
Result.Y[u] := AY.Y[u];
end;
end;
function DiaPos(
AValue: Double; AUnits: TDiaUnits;
AReverse: Boolean; APercentage: Boolean): TDiaPosition;
begin
Result.Init(nil);
Result.Value := AValue;
Result.Units := AUnits;
Result.Reverse := AReverse;
Result.Percentage := APercentage;
end;
function PointDist(const A, B: TDoublePoint): Double; overload;
begin
Result := Sqrt(Sqr(A.X - B.X) + Sqr(A.Y - B.Y));
end;
function WeightedAverage(
const AP1, AP2: TDoublePoint; ACoeff: Double): TDoublePoint; overload; inline;
begin
Result.X := WeightedAverage(AP1.X, AP2.X, ACoeff);
Result.Y := WeightedAverage(AP1.Y, AP2.Y, ACoeff);
end;
operator = (const A, B: TDiaPosition): Boolean;
begin
Result := TDiaPosition.Equals(A, B);
end;
{ TDiaCenterElement }
procedure TDiaCenterElement.Changed(ASender: TDiaObject);
var
tl, tr, bl{, br}: TDiaPoint;
begin
tl := DiaPoint(dbsLeft, dbsTop);
tr := DiaPoint(dbsRight, dbsTop);
bl := DiaPoint(dbsLeft, dbsBottom);
// br := DiaPoint(dbsRight, dbsBottom); // Commenting to silence the compilation note on unused variable...
if Left.Defined and Top.Defined then
FActualPos := XY(Left.Calc(tl, tr), Top.Calc(tl, bl));
inherited;
end;
constructor TDiaCenterElement.Create;
begin
FLeft.Init(Self);
FTop.Init(Self);
end;
procedure TDiaCenterElement.SetLeft(AValue: TDiaPosition);
begin
if FLeft = AValue then exit;
FLeft.Assign(AValue);
end;
procedure TDiaCenterElement.SetTop(AValue: TDiaPosition);
begin
if FTop = AValue then exit;
FTop.Assign(AValue);
end;
{ TDiaPoint }
function TDiaPoint.AsUnits(AUnits: TDiaUnits): TDoublePoint;
begin
Result.X := X[AUnits];
Result.Y := Y[AUnits];
end;
{ TDiaEndPoint }
constructor TDiaEndPoint.Create;
begin
inherited;
FLength.Init(Self);
FWidth.Init(Self);
end;
procedure TDiaEndPoint.SetConnector(AValue: TDiaConnector);
begin
if FConnector = AValue then exit;
FConnector := AValue;
Notify(Self);
end;
procedure TDiaEndPoint.SetLength(const AValue: TDiaPosition);
begin
if FLength = AValue then exit;
FLength.Assign(AValue);
end;
procedure TDiaEndPoint.SetShape(AValue: TDiaEndPointShape);
begin
if FShape = AValue then exit;
FShape := AValue;
Notify(Self);
end;
procedure TDiaEndPoint.SetWidth(const AValue: TDiaPosition);
begin
if FWidth = AValue then exit;
FWidth.Assign(AValue);
end;
{ TDiaDecoratorEnumerator }
constructor TDiaDecoratorEnumerator.Create(AList: TDiaDecoratorList);
begin
FList := AList;
FPosition := -1;
end;
function TDiaDecoratorEnumerator.GetCurrent: IDiaDecorator;
begin
Result := FList.FDecorators[FPosition];
end;
function TDiaDecoratorEnumerator.MoveNext: Boolean;
begin
FPosition += 1;
Result := FPosition < Length(FList.FDecorators);
end;
{ TDiaDecoratorList }
procedure TDiaDecoratorList.Add(ADecorator: IDiaDecorator);
begin
SetLength(FDecorators, Length(FDecorators) + 1);
FDecorators[High(FDecorators)] := ADecorator;
//Notify(ADecorator);
end;
constructor TDiaDecoratorList.Create(AOwner: TDiaObject);
begin
FOwner := AOwner;
end;
procedure TDiaDecoratorList.Changed(ASender: TDiaObject);
begin
// Remove compiler warning of abstract method
Unused(ASender);
end;
function TDiaDecoratorList.GetEnumerator: TDiaDecoratorEnumerator;
begin
Result := TDiaDecoratorEnumerator.Create(Self);
end;
procedure TDiaDecoratorList.Notify(ASender: TDiaObject);
begin
// Remove compiler warning of abstract method
Unused(ASender);
end;
{ TDiaDecorator }
constructor TDiaDecorator.Create(AOwner: TDiaDecoratorList);
begin
if AOwner <> nil then
AOwner.Add(Self);
end;
{ TDiaLink }
constructor TDiaLink.Create;
begin
inherited;
FStart := TDiaEndPoint.Create;
FFinish := TDiaEndPoint.Create;
end;
destructor TDiaLink.Destroy;
begin
FreeAndNil(FStart);
FreeAndNil(FFinish);
inherited;
end;
procedure TDiaLink.Draw;
begin
inherited;
if FInternalDraw <> nil then
FInternalDraw(Self);
end;
procedure TDiaLink.SetFinish(AValue: TDiaEndPoint);
begin
if FFinish = AValue then exit;
FFinish := AValue;
Notify(Self);
end;
procedure TDiaLink.SetOwner(AValue: TDiagram);
begin
inherited SetOwner(AValue);
FStart.SetOwner(AValue);
FFinish.SetOwner(AValue);
end;
procedure TDiaLink.SetRouting(AValue: TDiaLinkRouting);
begin
if FRouting = AValue then exit;
FRouting := AValue;
Notify(Self);
end;
procedure TDiaLink.SetStart(AValue: TDiaEndPoint);
begin
if FStart = AValue then exit;
FStart := AValue;
Notify(Self);
end;
{ TDiaCenterConnector }
procedure TDiaCenterConnector.Changed(ASender: TDiaObject);
begin
inherited;
if Element is TDiaCenterElement then
FActualPos := (Element as TDiaCenterElement).ActualPos;
end;
{ TDiaBoxConnector }
procedure TDiaBoxConnector.Changed(ASender: TDiaObject);
var
b: TDiaBox;
begin
inherited;
if not (Element is TDiaBox) then exit;
b := Element as TDiaBox;
case Side of
dbsTop: FActualPos := Position.Calc(b.FTopLeft, b.FTopRight);
dbsBottom: FActualPos := Position.Calc(b.FBottomLeft, b.FBottomRight);
dbsLeft: FActualPos := Position.Calc(b.FTopLeft, b.FBottomLeft);
dbsRight: FActualPos := Position.Calc(b.FTopRight, b.FBottomRight);
end;
end;
procedure TDiaBoxConnector.SetSide(AValue: TDiaBoxSide);
begin
if FSide = AValue then Exit;
FSide := AValue;
Notify(Self);
end;
{ TDiaConnector }
procedure TDiaConnector.Changed(ASender: TDiaObject);
begin
Unused(ASender);
end;
constructor TDiaConnector.Create;
begin
FPosition.Init(Element);
end;
procedure TDiaConnector.Notify(ASender: TDiaObject);
begin
if Element <> nil then
Element.Notify(ASender);
end;
procedure TDiaConnector.SetElement(AValue: TDiaElement);
begin
if FElement = AValue then exit;
Notify(Self);
FElement := AValue;
FPosition.FOwner := FElement;
Notify(Self);
end;
procedure TDiaConnector.SetPosition(const AValue: TDiaPosition);
begin
if FPosition = AValue then exit;
FPosition.Assign(AValue);
end;
{ TDiaCoordConverter }
function TDiaCoordConverter.AxisToGraph(AP: TDoublePoint): TDoublePoint;
begin
Result := AP;
end;
function TDiaCoordConverter.DeviceToImage(AP: TDoublePoint): TPoint;
begin
Result := RoundPoint(AP * (DEFAULT_DPI / CM_PER_INCH));
end;
function TDiaCoordConverter.GraphToAxis(AP: TDoublePoint): TDoublePoint;
begin
Result := AP;
end;
function TDiaCoordConverter.GraphToImage(AP: TDoublePoint): TPoint;
begin
Result := RoundPoint(AP);
end;
function TDiaCoordConverter.ImageToDevice(AP: TPoint): TDoublePoint;
begin
Result := DoublePoint(AP) * (CM_PER_INCH / DEFAULT_DPI);
end;
function TDiaCoordConverter.ImageToGraph(AP: TPoint): TDoublePoint;
begin
Result := DoublePoint(AP);
end;
{ TDiaBox }
procedure TDiaBox.Changed(ASender: TDiaObject);
var
tl, tr, bl, br: TDiaPoint;
begin
tl := DiaPoint(dbsLeft, dbsTop);
tr := DiaPoint(dbsRight, dbsTop);
bl := DiaPoint(dbsLeft, dbsBottom);
br := DiaPoint(dbsRight, dbsBottom);
if Left.Defined and Top.Defined then
FTopLeft := XY(Left.Calc(tl, tr), Top.Calc(tl, bl));
if Right.Defined and Top.Defined then
FTopRight := XY(Right.Calc(tr, tl), Top.Calc(tr, br));
if Left.Defined and Bottom.Defined then
FBottomLeft := XY(Left.Calc(tl, tr), Bottom.Calc(bl, tl));
if Right.Defined and Bottom.Defined then
FBottomRight := XY(Right.Calc(br, bl), Bottom.Calc(br, tr));
inherited;
end;
constructor TDiaBox.Create;
begin
inherited;
FLeft.Init(Self);
FRight.Init(Self);
FTop.Init(Self);
FBottom.Init(Self);
end;
procedure TDiaBox.Draw;
begin
if FInternalDraw <> nil then
FInternalDraw(Self);
end;
function TDiaBox.IsPointInside(
const APoint: TDoublePoint; AUnits: TDiaUnits): Boolean;
begin
Result := IsPointInPolygon(RoundPoint(APoint), [
RoundPoint(FTopLeft.AsUnits(AUnits)),
RoundPoint(FTopRight.AsUnits(AUnits)),
RoundPoint(FBottomRight.AsUnits(AUnits)),
RoundPoint(FBottomLeft.AsUnits(AUnits))
]);
end;
procedure TDiaBox.SetBottom(const AValue: TDiaPosition);
begin
if FBottom = AValue then exit;
FBottom.Assign(AValue);
end;
procedure TDiaBox.SetCaption(AValue: String);
begin
if FCaption = AValue then exit;
FCaption := AValue;
Notify(Self);
end;
procedure TDiaBox.SetHeight(const AValue: TDiaPosition);
begin
if FHeight = AValue then exit;
FHeight.Assign(AValue);
end;
procedure TDiaBox.SetLeft(const AValue: TDiaPosition);
begin
if FLeft = AValue then exit;
FLeft.Assign(AValue);
end;
procedure TDiaBox.SetRight(const AValue: TDiaPosition);
begin
if FRight = AValue then exit;
FRight.Assign(AValue);
end;
procedure TDiaBox.SetTop(const AValue: TDiaPosition);
begin
if FTop = AValue then exit;
FTop.Assign(AValue);
end;
procedure TDiaBox.SetWidth(const AValue: TDiaPosition);
begin
if FWidth = AValue then exit;
FWidth.Assign(AValue);
end;
{ TDiagram }
procedure TDiagram.Add(AElement: TDiaElement);
begin
if AElement.Owner = Self then exit;
SetLength(FElements, Length(FElements) + 1);
FElements[High(FElements)] := AElement;
AElement.SetOwner(Self);
end;
procedure TDiagram.Changed(ASender: TDiaObject);
var
e: TDiaElement;
begin
for e in FElements do
e.Changed(ASender);
end;
destructor TDiagram.Destroy;
var
e: TDiaElement;
begin
for e in FElements do
e.Free;
inherited;
end;
procedure TDiagram.Draw;
var
e: TDiaElement;
begin
for e in FElements do
e.Draw;
end;
function TDiagram.FindElementByPoint(
const APoint: TDoublePoint; AUnits: TDiaUnits): TDiaElement;
begin
for Result in FElements do
if Result.IsPointInside(APoint, AUnits) then exit;
Result := nil;
end;
function TDiagram.GetBounds(AIndex: TDiaBoxSide): TDiaCoordinate;
begin
Result := FBounds[AIndex];
end;
procedure TDiagram.Notify(ASender: TDiaObject);
begin
Changed(ASender);
end;
procedure TDiagram.SetBounds(AIndex: TDiaBoxSide; AValue: TDiaCoordinate);
begin
FBounds[AIndex] := AValue;
Notify(Self);
end;
procedure TDiagram.SetContext(AValue: TDiaContext);
begin
if FContext = AValue then exit;
FContext := AValue;
Notify(AValue);
end;
procedure TDiagram.SetPixelBounds(const ARect: TRect);
begin
FBounds[dbsTop][duPixels] := ARect.Top;
FBounds[dbsBottom][duPixels] := ARect.Bottom;
FBounds[dbsLeft][duPixels] := ARect.Left;
FBounds[dbsRight][duPixels] := ARect.Right;
Notify(Self);
end;
{ TDiaElement }
procedure TDiaElement.Add(AConnector: TDiaConnector);
begin
if AConnector.Element = Self then exit;
SetLength(FConnectors, Length(FConnectors) + 1);
FConnectors[High(FConnectors)] := AConnector;
AConnector.SetElement(Self);
end;
procedure TDiaElement.Changed(ASender: TDiaObject);
var
c: TDiaConnector;
begin
for c in FConnectors do
c.Changed(ASender);
end;
constructor TDiaElement.Create;
begin
FDecorators := TDiaDecoratorList.Create(Self);
end;
destructor TDiaElement.Destroy;
begin
FreeAndNil(FDecorators);
inherited;
end;
function TDiaElement.DiaPoint(AX, AY: TDiaBoxSide): TDiaPoint;
begin
Result.X := Owner[AX];
Result.Y := Owner[AY];
end;
procedure TDiaElement.Draw;
begin
//
end;
function TDiaElement.IsPointInside(
const APoint: TDoublePoint; AUnits: TDiaUnits): Boolean;
begin
Unused(APoint, AUnits);
Result := false;
end;
procedure TDiaElement.Notify(ASender: TDiaObject);
begin
if Owner <> nil then
Owner.Notify(ASender);
end;
procedure TDiaElement.SetOwner(AValue: TDiagram);
begin
if FOwner = AValue then Exit;
Notify(Self);
FOwner := AValue;
Notify(Self);
end;
{ TDiaPosition }
procedure TDiaPosition.Assign(const ASource: TDiaPosition);
begin
Self.FPercentage := ASource.Percentage;
Self.FReverse := ASource.Reverse;
Self.FUnits := ASource.Units;
Self.FValue := ASource.Value;
Notify;
end;
function TDiaPosition.Calc(const A, B: TDiaPoint): TDiaPoint;
var
d: TDiagram;
conv: TDiaCoordConverter;
w: Double;
pa, pb: TDoublePoint;
ap, gp, dp: TDoublePoint;
ip: TPoint;
begin
if Owner = nil then exit;
d := Owner.Owner;
conv := d.Context.Converter;
pa := DoublePoint(A.X[Units], A.Y[Units]);
pb := DoublePoint(B.X[Units], B.Y[Units]);
if Percentage then
w := Value * PERCENT
else begin
w := PointDist(pa, pb);
if w > 0 then
w := Value / w;
end;
if Reverse then
w := 1.0 - w;
case Units of
duNone: ;
duAxis: begin
ap := WeightedAverage(pa, pb, w);
gp := conv.AxisToGraph(ap);
ip := conv.GraphToImage(gp);
dp := conv.ImageToDevice(ip);
end;
duGraph: begin
gp := WeightedAverage(pa, pb, w);
ap := conv.GraphToAxis(gp);
ip := conv.GraphToImage(gp);
dp := conv.ImageToDevice(ip);
end;
duPixels: begin
ip := RoundPoint(WeightedAverage(pa, pb, w));
gp := conv.ImageToGraph(ip);
ap := conv.GraphToAxis(gp);
dp := conv.ImageToDevice(ip);
end;
duCentimeters: begin
dp := WeightedAverage(pa, pb, w);
ip := conv.DeviceToImage(dp);
gp := conv.ImageToGraph(ip);
ap := conv.GraphToAxis(gp);
end;
end;
Result.X[duAxis] := ap.X;
Result.Y[duAxis] := ap.Y;
Result.X[duGraph] := gp.X;
Result.Y[duGraph] := gp.Y;
Result.X[duPixels] := ip.X;
Result.Y[duPixels] := ip.Y;
Result.X[duCentimeters] := dp.X;
Result.Y[duCentimeters] := dp.Y;
end;
function TDiaPosition.Defined: Boolean;
begin
Result := Units <> duNone;
end;
class function TDiaPosition.Equals(const A, B: TDiaPosition): Boolean;
begin
Result :=
(A.Percentage = B.Percentage) and
(A.Reverse = B.Reverse) and
(A.Units = B.Units) and
(A.Value = B.Value);
end;
constructor TDiaPosition.Init(AOwner: TDiaElement);
begin
FOwner := AOwner;
FPercentage := false;
FReverse := false;
FUnits := duNone;
FValue := 0.0;
end;
procedure TDiaPosition.Notify;
begin
if Owner <> nil then
Owner.Notify(Owner);
end;
procedure TDiaPosition.SetPercentage(AValue: Boolean);
begin
if FPercentage = AValue then Exit;
FPercentage := AValue;
Notify;
end;
procedure TDiaPosition.SetReverse(AValue: Boolean);
begin
if FReverse = AValue then exit;
FReverse := AValue;
Notify;
end;
procedure TDiaPosition.SetUnits(AValue: TDiaUnits);
begin
if FUnits = AValue then exit;
FUnits := AValue;
Notify;
end;
procedure TDiaPosition.SetValue(AValue: Double);
begin
if FValue = AValue then exit;
FValue := AValue;
Notify;
end;
end.
|