-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathQRFormUnit.pas
430 lines (389 loc) · 10.3 KB
/
QRFormUnit.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
unit QRFormUnit;
interface
uses
Winapi.Windows, System.SysUtils, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs,
Vcl.ExtCtrls, Vcl.ComCtrls, Vcl.StdCtrls,
DelphiZXingQRCode, SciSupport, NppForms, NppPlugin;
type
TQrPlugin = class(TNppPlugin)
private
{ Private declarations }
const cnstNoSelection = 'Please, select text before using';
const cnstFunctionCaption = 'Encode selection to QR code';
const cnstName = 'NppQrCode';
public
constructor Create;
procedure DoNppnToolbarModification; override;
procedure FuncQr;
end;
TQrForm = class(TNppForm)
cmbEncoding: TComboBox;
lblEncoding: TLabel;
lblQuietZone: TLabel;
edtQuietZone: TEdit;
cbbErrorCorrectionLevel: TComboBox;
lblErrorCorrectionLevel: TLabel;
edtCornerThickness: TEdit;
udCornerThickness: TUpDown;
lblCorner: TLabel;
udQuietZone: TUpDown;
grpSaveToFile: TGroupBox;
dlgSaveToFile: TSaveDialog;
edtFileName: TEdit;
lblScaleToSave: TLabel;
edtScaleToSave: TEdit;
udScaleToSave: TUpDown;
lblDrawingMode: TLabel;
cbbDrawingMode: TComboBox;
pnlDetails: TPanel;
pgcQRDetails: TPageControl;
tsPreview: TTabSheet;
pbPreview: TPaintBox;
tsEncodedData: TTabSheet;
mmoEncodedData: TMemo;
lblQRMetrics: TLabel;
btnSaveToFile: TButton;
pnlColors: TPanel;
clrbxBackground: TColorBox;
lblBackground: TLabel;
lblForeground: TLabel;
clrbxForeground: TColorBox;
bvlColors: TBevel;
btnCopy: TButton;
procedure FormCreate(Sender: TObject);
procedure cmbEncodingChange(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure btnSaveToFileClick(Sender: TObject);
procedure cbbDrawingModeChange(Sender: TObject);
procedure cmbEncodingMeasureItem(Control: TWinControl; Index: Integer;
var AHeight: Integer);
procedure cmbEncodingDrawItem(Control: TWinControl; Index: Integer;
Rect: TRect; State: TOwnerDrawState);
procedure FormKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
procedure pbPreviewPaint(Sender: TObject);
procedure clrbxBackgroundChange(Sender: TObject);
procedure btnCopyClick(Sender: TObject);
procedure pgcQRDetailsChange(Sender: TObject);
private
FQRCode: TDelphiZXingQRCode;
FText: string;
// to fix well-known Delphi 7 error with visually vanishing components
// under Windows Vista, 7, and later
FAltFixed: Boolean;
procedure RemakeQR;
procedure SetText(const Value: string);
public
property Text: string read FText write SetText;
end;
var
NPlugin: TQrPlugin;
implementation
uses
QRGraphics, QR_Win1251, QR_URL, jpeg, Clipbrd;
{$R *.dfm}
procedure _FuncQr; cdecl;
begin
NPlugin.FuncQr;
end;
{ TQrPlugin }
constructor TQrPlugin.Create;
var
sk: TShortcutKey;
begin
inherited;
PluginName := cnstName;
AddFuncItem(cnstFunctionCaption, _FuncQr);
end;
procedure TQrPlugin.DoNppnToolbarModification;
var
tb: TToolbarIcons;
begin
tb.ToolbarIcon := 0;
tb.ToolbarBmp := LoadImage(Hinstance, 'QRBITMAP', IMAGE_BITMAP, 0, 0, (LR_DEFAULTSIZE));
Npp_Send(NPPM_ADDTOOLBARICON, WPARAM(self.CmdIdFromDlgId(0)), LPARAM(@tb));
end;
procedure TQrPlugin.FuncQr;
var
S: string;
F: TQrForm;
begin
S := SelectedText;
if S.Length > 0 then
begin
F := TQrForm.Create(self);
try
F.Text := S;
F.ShowModal;
finally
F.Free;
end;
end
else
if Assigned(Application) then
Application.MessageBox(cnstNoSelection,nppPChar(PluginName),MB_ICONSTOP + MB_OK);
end;
{ TQrForm }
procedure TQrForm.cmbEncodingChange(Sender: TObject);
begin
RemakeQR;
end;
procedure TQrForm.FormCreate(Sender: TObject);
var
H: Integer;
begin
FAltFixed := False;
FQRCode := nil;
// number edit
SetWindowLong(edtQuietZone.Handle, GWL_STYLE,
GetWindowLong(edtQuietZone.Handle, GWL_STYLE) or ES_NUMBER);
SetWindowLong(edtCornerThickness.Handle, GWL_STYLE,
GetWindowLong(edtCornerThickness.Handle, GWL_STYLE) or ES_NUMBER);
SetWindowLong(edtScaleToSave.Handle, GWL_STYLE,
GetWindowLong(edtScaleToSave.Handle, GWL_STYLE) or ES_NUMBER);
Position := poScreenCenter;
with cmbEncoding do
begin
H := ItemHeight;
Style := csOwnerDrawVariable;
ItemHeight := H;
OnChange := nil;
ItemIndex := 0;
OnChange := cmbEncodingChange
end;
with cbbErrorCorrectionLevel do
begin
OnChange := nil;
ItemIndex := 0;
OnChange := cmbEncodingChange
end;
with cbbDrawingMode do
begin
OnChange := nil;
ItemIndex := 0;
OnChange := cbbDrawingModeChange
end;
// create and prepare QRCode component
FQRCode := TDelphiZXingQRCode.Create;
FQRCode.RegisterEncoder(ENCODING_WIN1251, TWin1251Encoder);
FQRCode.RegisterEncoder(ENCODING_URL, TURLEncoder);
end;
procedure TQrForm.RemakeQR;
// QR-code generation
begin
with FQRCode do
try
BeginUpdate;
Data := FText;
Encoding := cmbEncoding.ItemIndex;
ErrorCorrectionOrdinal := TErrorCorrectionOrdinal
(cbbErrorCorrectionLevel.ItemIndex);
QuietZone := StrToIntDef(edtQuietZone.Text, 4);
EndUpdate(True);
lblQRMetrics.Caption := IntToStr(Columns) + 'x' + IntToStr(Rows) + ' (' +
IntToStr(Columns - QuietZone * 2) + 'x' + IntToStr(Rows - QuietZone * 2) +
')';
finally
pbPreview.Repaint;
end;
end;
procedure TQrForm.SetText(const Value: string);
begin
FText := Value;
end;
procedure TQrForm.FormDestroy(Sender: TObject);
begin
FQRCode.Free;
end;
procedure TQrForm.btnSaveToFileClick(Sender: TObject);
var
Bmp: TBitmap;
M: TMetafile;
S: string;
J: TJPEGImage;
begin
if dlgSaveToFile.Execute then
begin
S := LowerCase(ExtractFileExt(dlgSaveToFile.FileName));
if S = '' then
begin
case dlgSaveToFile.FilterIndex of
0, 1: S := '.bmp';
2: S := '.emf';
3: S := '.jpg';
end;
dlgSaveToFile.FileName := dlgSaveToFile.FileName + S;
end;
edtFileName.Text := dlgSaveToFile.FileName;
Bmp := nil;
M := nil;
J := nil;
if S = '.bmp' then
try
Bmp := TBitmap.Create;
MakeBmp(Bmp, udScaleToSave.Position, FQRCode, clrbxBackground.Selected,
clrbxForeground.Selected, udCornerThickness.Position);
Bmp.SaveToFile(dlgSaveToFile.FileName);
Bmp.Free;
except
Bmp.Free;
raise;
end
else
if S = '.emf' then
try
M := TMetafile.Create;
MakeMetafile(M, udScaleToSave.Position, FQRCode,
clrbxBackground.Selected, clrbxForeground.Selected,
TQRDrawingMode(cbbDrawingMode.ItemIndex div 2),
udCornerThickness.Position);
M.SaveToFile(dlgSaveToFile.FileName);
M.Free;
except
M.Free;
raise;
end
else
if S = '.jpg' then
try
Bmp := TBitmap.Create;
MakeBmp(Bmp, udScaleToSave.Position, FQRCode,
clrbxBackground.Selected, clrbxForeground.Selected,
udCornerThickness.Position);
J := TJPEGImage.Create;
J.Assign(Bmp);
J.SaveToFile(dlgSaveToFile.FileName);
J.Free;
Bmp.Free;
except
J.Free;
Bmp.Free;
raise;
end
end;
end;
procedure TQrForm.cbbDrawingModeChange(Sender: TObject);
begin
dlgSaveToFile.FilterIndex := Ord(TQRDrawingMode(cbbDrawingMode.ItemIndex
div 2) <> drwBitmap) + 1;
pbPreview.Repaint;
end;
procedure TQrForm.cmbEncodingMeasureItem(Control: TWinControl;
Index: Integer; var AHeight: Integer);
begin
AHeight := cmbEncoding.ItemHeight;
if Index in [0, ENCODING_UTF8_BOM + 1] then
AHeight := AHeight * 2;
end;
procedure TQrForm.cmbEncodingDrawItem(Control: TWinControl; Index: Integer;
Rect: TRect; State: TOwnerDrawState);
var
R1, R2: TRect;
IsSpecialLine: Boolean;
OldColor, OldFontColor: TColor;
S: string;
begin
IsSpecialLine := (Index in [0, ENCODING_UTF8_BOM + 1]) and
not (odComboBoxEdit in State);
with Control as TComboBox do
begin
if IsSpecialLine then
begin
R1 := Rect;
R2 := R1;
R1.Bottom := (Rect.Bottom + Rect.Top) div 2;
R2.Top := R1.Bottom;
end
else
R2 := Rect;
Canvas.FillRect(R2);
if Index >= 0 then
begin
if IsSpecialLine then
begin
OldColor := Canvas.Brush.Color;
OldFontColor := Canvas.Font.Color;
Canvas.Brush.Color := clBtnFace;
Canvas.Font.Style := [fsBold];
Canvas.Font.Color := clGrayText;
Canvas.FillRect(R1);
if Index = 0 then
S := 'Default'
else
S := 'Extended';
Canvas.TextOut((R1.Left + R1.Right - Canvas.TextWidth(S)) div 2, R1.Top,
S);
Canvas.Font.Assign(Font);
Canvas.Brush.Color := OldColor;
Canvas.Font.Color := OldFontColor;
end;
Canvas.TextOut(R2.Left + 2, R2.Top, Items[Index]);
end;
if IsSpecialLine and (odFocused in State) then
with Canvas do
begin
DrawFocusRect(Rect);
DrawFocusRect(R2);
end;
end;
end;
procedure TQrForm.FormKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
procedure InvalidateControl(W: TWinControl);
var
I: Integer;
begin
with W do
begin
for I := 0 to ControlCount - 1 do
if Controls[I] is TWinControl then
InvalidateControl(Controls[I] as TWinControl);
Invalidate;
end;
end;
const KEY_ESC = 27;
begin
if not FAltFixed and (ssAlt in Shift) then
begin
InvalidateControl(Self);
FAltFixed := True;
end;
if Key = KEY_ESC then Close;
end;
procedure TQrForm.pbPreviewPaint(Sender: TObject);
begin
with pbPreview.Canvas do
begin
Pen.Color := clrbxForeground.Selected;
Brush.Color := clrbxBackground.Selected;
end;
DrawQR(pbPreview.Canvas, pbPreview.ClientRect, FQRCode,
udCornerThickness.Position, TQRDrawingMode(cbbDrawingMode.ItemIndex div 2),
Boolean(1 - cbbDrawingMode.ItemIndex mod 2));
end;
procedure TQrForm.pgcQRDetailsChange(Sender: TObject);
begin
mmoEncodedData.Text := FQRCode.FilteredData;
end;
procedure TQrForm.clrbxBackgroundChange(Sender: TObject);
begin
pbPreview.Repaint;
end;
procedure TQrForm.btnCopyClick(Sender: TObject);
var
Bmp: TBitmap;
begin
Bmp := nil;
try
Bmp := TBitmap.Create;
MakeBmp(Bmp, udScaleToSave.Position, FQRCode, clrbxBackground.Selected,
clrbxForeground.Selected, udCornerThickness.Position);
Clipboard.Assign(Bmp);
Bmp.Free;
except
Bmp.Free;
raise;
end;
end;
end.