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
|
{
This file is part of the Free Component Library.
Copyright (c) 2017 Michael Van Canneyt, member of the Free Pascal development team
Frame to display the object inspector.
See the file COPYING.FPC, included in this distribution,
for details about the copyright.
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.
**********************************************************************}
unit fraReportObjectInspector;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, FileUtil, RTTIGrids, Forms, Controls, ComCtrls, ExtCtrls,
StdCtrls, fpreport, fpreportdesignobjectlist;
type
{ TObjectInspectorFrame }
TSelectElementEvent = Procedure (Sender : TObject; Selected : TComponent) of Object;
TObjectInspectorFrame = class(TFrame)
ILTree: TImageList;
LPropertyGrid: TLabel;
Panel1: TPanel;
Panel2: TPanel;
PGReport: TTIPropertyGrid;
Splitter1: TSplitter;
TVReport: TTreeView;
procedure PGReportModified(Sender: TObject);
procedure TVReportSelectionChanged(Sender: TObject);
private
FISC: Boolean;
FList: TReportObjectList;
FOnModified: TNotifyEvent;
FOnSelectElement: TSelectElementEvent;
FReport: TFPReport;
procedure AddElementChildren(AParentNode: TTreeNode; AParent: TFPReportElementWithChildren);
function AddElementNode(AIndex: Integer; AParentNode: TTreeNode; AElement: TFPReportElement): TTreeNode;
function GetNodeNameForElement(E: TComponent): String;
function GetSRT: Boolean;
procedure SetNodeProps(N: TTreeNode; E: TComponent);
procedure SetReport(AValue: TFPReport);
procedure SetSRT(AValue: Boolean);
procedure UpdateReportTree;
Protected
Property IgnoreSelectionChange : Boolean Read FISC Write FISC;
public
Procedure UpdateSelection;
procedure RefreshReportTree;
Procedure RefreshOI;
Procedure SelectControls(AList : TReportObjectList);
Property Objectlist : TReportObjectList Read FList;
Property ShowReportTree : Boolean Read GetSRT Write SetSRT;
Property Report : TFPReport Read FReport Write SetReport;
Property OnSelectElement : TSelectElementEvent Read FOnSelectElement Write FOnSelectElement;
Property OnModified : TNotifyEvent Read FOnModified Write FOnModified;
end;
implementation
uses propeditutils;
{$R *.lfm}
Const
iiReport = 0;
iiPage = 1;
iiband = 2;
iiElement = 3;
Resourcestring
SPage = 'Page %d';
SReport = 'Report';
SNoNameBand = '%d: Unnamed band %s';
SNoNameElement = '%d: Unnamed element %s';
SPGCaption = 'Properties of %s';
Smultiple = 'multiple (%d) elements';
SNoSelection = 'No selection';
{ TObjectInspectorFrame }
function TObjectInspectorFrame.GetSRT: Boolean;
begin
Result:=TVReport.Visible;
end;
function TObjectInspectorFrame.GetNodeNameForElement(E: TComponent): String;
begin
Result:=E.Name;
if (Result<>'') then
exit;
if e is TFPCustomReport then
Result:=SReport
else if E is TFPReportCustomPage then
Result:=Format(SPage,[(E as TFPReportCustomPage).PageIndex+1])
else if E is TFPReportCustomBand then
Result:=Format(SNoNameBand,[E.ComponentIndex,E.ClassName])
else
Result:=Format(SNoNameElement,[E.ComponentIndex,E.ClassName])
end;
procedure TObjectInspectorFrame.SetNodeProps(N : TTreeNode; E : TComponent);
begin
if e is TFPCustomReport then
N.ImageIndex:=iiReport
else if E is TFPReportCustomPage then
N.ImageIndex:=iiPage
else if E is TFPReportCustomBand then
N.ImageIndex:=iiBand
else
N.ImageIndex:=iiElement;
N.Text:=GetNodeNameForElement(E);
N.SelectedIndex:=N.ImageIndex;
N.Data:=E;
end;
function TObjectInspectorFrame.AddElementNode(AIndex: Integer;
AParentNode: TTreeNode; AElement: TFPReportElement): TTreeNode;
Var
N : TTreeNode;
begin
N:=TVReport.Items.AddChild(AParentNode,'');
SetNodeProps(N,AElement);
if AElement is TFPReportElementWithChildren then
AddElementChildren(N,AElement as TFPReportElementWithChildren);
Result:=N;
end;
procedure TObjectInspectorFrame.TVReportSelectionChanged(Sender: TObject);
Var
N : TTreeNode;
C : TComponent;
begin
if IgnoreSelectionChange then
exit;
N:=TVReport.Selected;
if Assigned(N) then
C:=TComponent(N.Data)
else
C:=Nil;
if Assigned(OnSelectElement) then
OnSelectElement(Self,C);
end;
procedure TObjectInspectorFrame.UpdateReportTree;
Var
I: Integer;
E : TFPReportElement;
N : TTreeNode;
begin
For I:=0 to ObjectList.Count-1 do
begin
E:=ObjectList[i].Element;
N:=TVReport.Items.FindNodeWithData(E);
if Assigned(N) then
N.Text:=GetNodeNameForElement(E);
end;
end;
procedure TObjectInspectorFrame.PGReportModified(Sender: TObject);
begin
UpdateReportTree;
If Assigned(FOnModified) then
FOnModified(Self);
end;
procedure TObjectInspectorFrame.AddElementChildren(AParentNode : TTreeNode; AParent : TFPReportElementWithChildren);
Var
I : Integer;
begin
For I:=0 to AParent.ChildCount-1 do
AddElementNode(I,AparentNode,AParent.Child[i]);
end;
procedure TObjectInspectorFrame.RefreshReportTree;
Var
PN,N : TTreeNode;
S : String;
I : Integer;
begin
With TVReport.Items do
try
IgnoreSelectionChange:=True;
BeginUpdate;
Clear;
if Not Assigned(FReport) then
exit;
PN:=AddChild(Nil,'Report');
SetNodeProps(PN,FReport);
For I:=0 to FReport.PageCount-1 do
begin
S:=Format(SPage,[I+1]);
if (FReport.Pages[i].Name<>'') then
S:=S+': '+FReport.Pages[i].Name;
N:=AddChild(PN,S);
SetNodeProps(N,FReport.Pages[i]);
AddElementChildren(N,FReport.Pages[i]);
end;
PN.Expand(True);
finally
EndUpdate;
IgnoreSelectionChange:=False;
end;
end;
procedure TObjectInspectorFrame.RefreshOI;
begin
PGReport.RefreshPropertyValues;
end;
procedure TObjectInspectorFrame.SetReport(AValue: TFPReport);
begin
if FReport=AValue then Exit;
FReport:=AValue;
if ShowReportTree then
RefreshReportTree;
end;
procedure TObjectInspectorFrame.SetSRT(AValue: Boolean);
begin
if AValue=GetSRT then exit;
TVReport.Visible:=AValue;
if AValue then
RefreshReportTree;
end;
procedure TObjectInspectorFrame.UpdateSelection;
Var
I : integer;
L : TPersistentSelectionList;
S : String;
begin
// Update grid
L:=TPersistentSelectionList.Create;
try
L.BeginUpdate;
try
if Assigned(ObjectList) then
For I:=0 to ObjectList.Count-1 do
if ObjectList[i].Selected then
L.Add(ObjectList[i].Element);
if (L.Count=0) and Assigned(FReport) then
L.Add(FReport);
PGReport.Selection:=L;
finally
L.EndUpdate;
end;
// Update tree, select first element
if L.Count>0 then
begin
IgnoreSelectionChange:=True;
try
TVReport.Selected:=TVReport.Items.FindNodeWithData(L.Items[0]);
finally
IgnoreSelectionChange:=False;
end;
end;
// Update caption
if L.Count>1 then
S:=Format(SPGCaption,[Format(Smultiple,[L.Count])])
else if L.Count=1 then
S:=Format(SPGCaption,[GetNodeNameForElement(TComponent(L.Items[0]))])
else
S:=SNoSelection;
LPropertyGrid.Caption:=S;
Finally
L.Free;
end;
end;
procedure TObjectInspectorFrame.SelectControls(AList: TReportObjectList);
begin
FList:=AList;
UpdateSelection;
end;
end.
|