-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathDW.VCL.Image.pas
More file actions
305 lines (271 loc) · 8.89 KB
/
DW.VCL.Image.pas
File metadata and controls
305 lines (271 loc) · 8.89 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
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
unit DW.VCL.Image;
interface
uses Classes, SysUtils, VCL.Controls, StrUtils, System.IOUtils, Graphics, Db,
DW.VCL.DBControl, DWElementTag;
type
TDWImageOption = (bsimResponsive, bsimCircle, bsimRounded, bsimThumbnail);
TDWImageOptions = set of TDWImageOption;
TDWImage = class(TDWCustomDbControl)
private
FActiveSrc: string;
FOldSrc: string;
FAltText: string;
FAutoFormGroup: boolean;
FEmbedBase64: boolean;
FImageFile: string;
FImageOptions: TDWImageOptions;
FimageSrc: string;
FMimeType: string;
FPicture: TPicture;
FUseSize: boolean;
procedure SetImageFile(const AValue: string);
procedure SetImageSrc(const AValue: string);
function GetPicture: TPicture;
procedure SetPicture(AValue: TPicture);
procedure SetUseSize(const AValue: boolean);
protected
procedure CheckData; override;
procedure PictureChanged(ASender: TObject);
procedure InternalRenderAsync(const AHTMLName: string); override;
procedure InternalRenderCss(var ACss: string); override;
procedure InternalRenderHTML(var AHTMLTag: TDWElementTag); override;
procedure InternalRenderStyle(AStyle: TStringList); override;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
property ActiveSrc: string read FActiveSrc;
procedure Refresh;
function GetFixedFilePath: string;
published
property AltText: string read FAltText write FAltText;
property AutoFormGroup: boolean read FAutoFormGroup write FAutoFormGroup default False;
property BSImageOptions: TDWImageOptions read FImageOptions write FImageOptions
default [bsimResponsive];
{ TODO 1 -oDELCIO -cIMPLEMENT : change EmbedBase64 default to false after implement cache }
property EmbedBase64: boolean read FEmbedBase64 write FEmbedBase64 default True;
property Enabled default True;
property ImageFile: string read FImageFile write SetImageFile;
property ImageSrc: string read FimageSrc write SetImageSrc;
property MimeType: string read FMimeType write FMimeType;
property Picture: TPicture read GetPicture write SetPicture;
property UseSize: boolean read FUseSize write SetUseSize default False;
end;
implementation
uses
DWUtils, DW.VCL.CustomForm, OverbyteIcsMimeUtils, DW.CORE.Cache, DW.VCL.Common,
DW.VCL.InputForm;
{$REGION 'TIWBSImage'}
constructor TDWImage.Create(AOwner: TComponent);
begin
inherited;
FAltText := '';
ImageFile := '';
FImageOptions := [bsimResponsive];
ImageSrc := '';
FMimeType := '';
FPicture := nil;
FUseSize := False;
Width := 89;
Height := 112;
FEmbedBase64 := True;
end;
destructor TDWImage.Destroy;
begin
FreeAndNil(FPicture);
inherited;
end;
function TDWImage.GetPicture: TPicture;
begin
if not Assigned(FPicture) then
begin
FPicture := TPicture.Create;
FPicture.OnChange := PictureChanged;
end;
Result := FPicture;
end;
procedure TDWImage.SetImageFile(const AValue: string);
begin
FImageFile := AValue;
FActiveSrc := '';
Invalidate;
end;
procedure TDWImage.SetImageSrc(const AValue: string);
begin
FimageSrc := AValue;
FActiveSrc := '';
Invalidate;
end;
procedure TDWImage.SetPicture(AValue: TPicture);
begin
Picture.Assign(AValue);
FActiveSrc := '';
end;
procedure TDWImage.PictureChanged(ASender: TObject);
begin
if not IsLoading then
begin
FActiveSrc := '';
Invalidate;
end;
end;
procedure TDWImage.SetUseSize(const AValue: boolean);
begin
if FUseSize <> AValue then
begin
FUseSize := AValue;
Invalidate;
end;
end;
procedure TDWImage.Refresh;
begin
FActiveSrc := '';
Invalidate;
end;
function TDWImage.GetFixedFilePath: string;
begin
if TPath.IsDriveRooted(FImageFile) then
begin
Result := TPath.Combine(DWServer.DocDir, FImageFile);
if not FileExists(Result) then
Result := FImageFile;
end
else
Result := FImageFile;
end;
procedure TDWImage.CheckData;
var
LField: TField;
LMimeType: string;
LFile: string;
LStream: TStream;
// LFileStream: TMemoryStream;
// LParentForm: TDWCustomForm;
begin
LFile := '';
if FMimeType <> '' then
LMimeType := FMimeType
else
LMimeType := 'image';
// if there is field data we show it, if not we fallback to other sources
if CheckDataSource(DataSource, DataField, LField) then
begin
FActiveSrc := '';
if Assigned(FPicture) then
FPicture.Graphic := nil;
if (LField is TBlobField) and not LField.IsNull then
begin
LStream := DWGetFieldBlobStream(DataSource.DataSet, TBlobField(LField));
try
if FEmbedBase64 then
{ TODO 1 -oDELCIO -cIMPLEMENT : !!!! Need to implement this }
// FActiveSrc := 'data:image;base64, '+ DoFileEncBase64(LStream, False)
else
begin
{ TODO 1 -oDELCIO -cIMPLEMENT : Need to implement TIWBSImage.CheckData for non EmbedBase64 }
// FActiveSrc := TDWCache.AddFileToCache(AContext.WebApplication, LFile, LMimeType);
// FActiveSrc := TDWCache.AddStreamToSessionFileCache(Self, LStream, AContext.WebApplication, LFile, LMimeType);
end;
finally
LStream.Free;
end;
end;
end;
if FActiveSrc = '' then
begin
if Assigned(FPicture) and Assigned(FPicture.Graphic) and (not FPicture.Graphic.Empty) then
begin
if FEmbedBase64 then
begin
LStream := TMemoryStream.Create;
try
FPicture.Graphic.SaveToStream(LStream);
LStream.Position := 0;
{ TODO 1 -oDELCIO -cIMPLEMENT : !!!! Need to implement this }
// FActiveSrc := 'data:image;base64, '+ DoFileEncBase64(LStream, False);
finally
LStream.Free;
end;
end
else
begin
{ TODO 1 -oDELCIO -cIMPLEMENT : Need to implement TIWBSImage.CheckData for non EmbedBase64 }
// LFile := TIWAppCache.NewTempFileName;
// FPicture.SaveToFile(LFile);
end;
end
else if FImageFile <> '' then
LFile := GetFixedFilePath
else if FimageSrc <> '' then
begin
{ TODO 1 -oDELCIO -cIMPLEMENT : Need to implement TIWBSImage.CheckData for ImageSrc }
(* if AnsiStartsStr('//', FImageSrc) or AnsiContainsStr('://', FImageSrc) then
FActiveSrc := FImageSrc
else
FActiveSrc := TURL.MakeValidFileUrl(AContext.WebApplication.AppUrlBase, FImageSrc); *)
end;
if LFile <> '' then
begin
{ TODO 1 -oDELCIO -cIMPLEMENT : Need to implement TIWBSImage.CheckData for File }
(*
LParentForm := TIWForm.FindParentForm(Self);
if LParentForm <> nil then
FActiveSrc := TIWAppCache.AddFileToCache(LParentForm, LFile, LMimeType, ctForm)
else
FActiveSrc := TIWAppCache.AddFileToCache(AContext.WebApplication, LFile, LMimeType); *)
end;
end;
end;
procedure TDWImage.InternalRenderAsync(const AHTMLName: string);
begin
inherited;
if FActiveSrc <> FOldSrc then
begin
DWApplication.CallBackResp.AddScriptToExecuteFirst('$("#' + AHTMLName + '").attr("src","' +
FActiveSrc + '");', False);
FOldSrc := FActiveSrc;
end;
end;
procedure TDWImage.InternalRenderCss(var ACss: string);
begin
if bsimResponsive in FImageOptions then
TDWBSCommon.AddCssClass(ACss, 'img-responsive');
if bsimCircle in FImageOptions then
TDWBSCommon.AddCssClass(ACss, 'img-circle');
if bsimRounded in FImageOptions then
TDWBSCommon.AddCssClass(ACss, 'img-rounded');
if bsimThumbnail in FImageOptions then
TDWBSCommon.AddCssClass(ACss, 'img-thumbnail');
inherited;
end;
procedure TDWImage.InternalRenderHTML(var AHTMLTag: TDWElementTag);
begin
inherited;
FOldSrc := FActiveSrc;
AHTMLTag := TDWElementTag.CreateHTMLTag('img');
AHTMLTag.AddClassParam(ActiveCss);
AHTMLTag.AddStringParam('id', HTMLName);
AHTMLTag.AddStringParam('style', ActiveStyle);
AHTMLTag.AddStringParam('src', FActiveSrc);
if AltText <> '' then
AHTMLTag.AddStringParam('alt', AltText, True)
else
AHTMLTag.AddStringParam('alt', FActiveSrc, True);
if not AutoSize then
begin
AHTMLTag.AddIntegerParam('width', Width);
AHTMLTag.AddIntegerParam('height', Height);
end;
{ TODO 1 -oDELCIO -cIMPLEMENT : SetEnabled False for TIWBSImage.InternalRenderHTML }
// if not Enabled then
// AContext.AddToInitProc('setEnabled("' + HTMLName + '", false);');
if FAutoFormGroup and not(Parent is TDWInputGroup) then
AHTMLTag := DWCreateInputFormGroup(Self, Parent, AHTMLTag, Caption, HTMLName);
end;
procedure TDWImage.InternalRenderStyle(AStyle: TStringList);
begin
inherited;
if Assigned(OnAsyncClick) and (Cursor = crDefault) then
AStyle.Values['cursor'] := 'pointer';
end;
{$ENDREGION}
end.