forked from lmbelo/python4delphi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnit1.pas
More file actions
204 lines (171 loc) · 5.13 KB
/
Unit1.pas
File metadata and controls
204 lines (171 loc) · 5.13 KB
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
unit Unit1;
{.$I Definition.Inc}
interface
uses
SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ExtCtrls, ComCtrls,
PythonEngine, PythonGUIInputOutput, WrapDelphi, ActnList;
type
TForm1 = class(TForm)
Splitter1: TSplitter;
Memo1: TMemo;
PyEngine: TPythonEngine;
PythonModule: TPythonModule;
Panel1: TPanel;
Button1: TButton;
PythonGUIInputOutput1: TPythonGUIInputOutput;
Memo2: TMemo;
ActionList1: TActionList;
actTest: TAction;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure PythonModuleEvents0Execute(Sender: TObject; PSelf,
Args: PPyObject; var Result: PPyObject);
procedure actTestExecute(Sender: TObject);
private
public
PyDelphiWrapper : TPyDelphiWrapper;
end;
var
Form1: TForm1;
implementation
{$R *.lfm}
Uses
TypInfo,
Variants,
VarPyth;
//////////////////////////////////////////////////////////////////////////
// Using TPyDelphiObject you can wrap any Delphi object exposing published
// properties and methods. Note that the conditional defines TYPEINFO and
// METHODINFO need to be on
//////////////////////////////////////////////////////////////////////////
{$TYPEINFO OFF}
{$IFNDEF FPC}{$METHODINFO OFF}{$ENDIF}
Type
TTestBase = class
fdouble : double;
function DoubleDValue : double; // not visible in subclasses
published
property DValue : Double read fdouble write fdouble; // will be visible in subclasses
end;
function TTestBase.DoubleDValue : double;
begin
Result := 2 * fdouble;
end;
type
{$TYPEINFO ON}
{$IFNDEF FPC}{$METHODINFO ON}{$ENDIF}
TTestClass = class(TTestBase, IFreeNotification)
private
fSValue : string;
fIValue : integer;
fSL : TStrings;
fOnChange: TNotifyEvent;
fFreeNotifImpl : IFreeNotification;
// implementation of interface IUnknown
function QueryInterface(const IID: TGUID; out Obj): HResult; stdcall;
function _AddRef: Integer; stdcall;
function _Release: Integer; stdcall;
protected
property FreeNotifImpl : IFreeNotification read fFreeNotifImpl implements IFreeNotification;
public
constructor Create;
destructor Destroy; override;
procedure SetMeUp(S: string; I : Integer);
function DescribeMe(): string;
procedure TriggerChange;
published
property SL : TStrings read fSL;
property SValue : string read fSValue write fSValue;
property IValue : integer read fIValue write fIValue;
property OnChange : TNotifyEvent read fOnChange write fOnChange;
end;
{$TYPEINFO OFF}
{$IFNDEF FPC}{$METHODINFO OFF}{$ENDIF}
constructor TTestClass.Create;
begin
inherited;
fFreeNotifImpl := TFreeNotificationImpl.Create(Self);
fSL := TStringList.Create;
fSL.AddObject('Form1', Form1);
fSL.AddObject('Form1.Button1', Form1.Button1);
end;
destructor TTestClass.Destroy;
begin
fSL.Free;
inherited;
end;
procedure TTestClass.SetMeUp(S: string; I : Integer);
begin
SValue := S;
IValue := I;
end;
function TTestClass.Describeme() : string;
begin
Result := fSValue + ' : ' + IntToStr(IValue);
end;
procedure TTestClass.TriggerChange;
begin
if Assigned(fOnChange) then
fOnChange(Self);
end;
function TTestClass._AddRef: Integer;
begin
Result := -1; // we don't want reference counting
end;
function TTestClass._Release: Integer;
begin
Result := -1; // we don't want reference counting
end;
function TTestClass.QueryInterface(const IID: TGUID; out Obj): HResult;
begin
if GetInterface(IID, Obj) then
Result := 0
else
Result := E_NOINTERFACE;
end;
{ TForm1 }
procedure TForm1.Button1Click(Sender: TObject);
var
p : PPyObject;
begin
actTest.OnExecute := actTestExecute;
// Wrap the Form itself.
p := PyDelphiWrapper.Wrap(Self);
PythonModule.SetVar( 'MainForm', p );
PyEngine.Py_DecRef(p);
// Now wrap the an instance our TestClass
// This time we would like the object to be destroyed when the PyObject
// is destroyed, so we need to set its Owned property to True;
p := PyDelphiWrapper.Wrap(TTestClass.Create, soOwned);
PythonModule.SetVar( 'DVar', p );
PyEngine.Py_DecRef(p);
{$IF DEFINED(FPC_VER) and (FPC_VER >= 5)}
p := PyEngine.PyInt_FromLong(FPC_VER);
{ELSE}
p := PyEngine.PyInt_FromLong(4);
{$IFEND}
PythonModule.SetVar( 'DelphiVersion', p );
PyEngine.Py_DecRef(p);
// Excecute the script
PyEngine.ExecStrings( memo1.Lines );
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
PyDelphiWrapper := TPyDelphiWrapper.Create(Self); // no need to destroy
PyDelphiWrapper.Engine := PyEngine;
PyDelphiWrapper.Module := PythonModule;
PyDelphiWrapper.Initialize; // Should only be called if PyDelphiWrapper is created at run time
end;
procedure TForm1.PythonModuleEvents0Execute(Sender: TObject; PSelf,
Args: PPyObject; var Result: PPyObject);
begin
ShowMessage(VarPythonCreate(Args).GetItem(0));
Result := PyEngine.ReturnNone;
end;
procedure TForm1.actTestExecute(Sender: TObject);
begin
Memo2.Lines.Add('Delphi event actTestExecute fired');
Import('spam').DVar.IValue := 1;
end;
end.