File: graphstat.pp

package info (click to toggle)
lazarus 2.0.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 214,460 kB
  • sloc: pascal: 1,862,622; xml: 265,709; cpp: 56,595; sh: 3,008; java: 609; makefile: 535; perl: 297; sql: 222; ansic: 137
file content (487 lines) | stat: -rw-r--r-- 13,939 bytes parent folder | download | duplicates (2)
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
unit GraphStat;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, Types, FileUtil, Forms, Controls, Graphics, Dialogs,
  {$ifndef POCHECKERSTANDALONE}
  LazIDEIntf,
  {$else}
  Process, Utf8Process,
  {$endif}
  ExtCtrls, PoFamilies, PoFamilyLists, PoCheckerConsts, LCLProc, StdCtrls, ComCtrls, Menus,
  PoCheckerSettings, LCLIntf, Translations;


type

  { TGraphStatForm }

  TGraphStatForm = class(TForm)
    Separator1MenuItem: TMenuItem;
    RefreshCurrMenuItem: TMenuItem;
    RefreshAllMenuItem: TMenuItem;
    Separator2MenuItem: TMenuItem;
    POEditorMenuItem: TMenuItem;
    ExtEditorMenuItem: TMenuItem;
    IDEEditorMenuItem: TMenuItem;
    ContextPopupMenu: TPopupMenu;
    StatusLabel: TLabel;
    ListView: TListView;
    TranslatedLabel: TLabel;
    UnTranslatedLabel: TLabel;
    FuzzyLabel: TLabel;
    LegendPanel: TPanel;
    TranslatedShape: TShape;
    UnTranslatedShape: TShape;
    FuzzyShape: TShape;
    procedure ExtEditorMenuItemClick(Sender: TObject);
    procedure FormActivate(Sender: TObject);
    procedure FormClose(Sender: TObject; var {%H-}CloseAction: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure IDEEditorMenuItemClick(Sender: TObject);
    procedure ListViewMouseMove(Sender: TObject; {%H-}Shift: TShiftState; X,
      Y: Integer);
    procedure ListViewMouseUp(Sender: TObject; Button: TMouseButton; Shift:
      TShiftState; X, Y: Integer);
    procedure POEditorMenuItemClick(Sender: TObject);
    procedure RefreshAllMenuItemClick(Sender: TObject);
    procedure RefreshCurrMenuItemClick(Sender: TObject);
  private
    { private declarations }
    FPoFamilyList: TPoFamilyList;
    FPoFamilyStats: TPoFamilyStats;
    FImgList: TImageList;
    FOldHintHidePause: Integer;
    FSettings: TPoCheckerSettings;
    Fn: string;
    CurrentMasterPoFile: string;
    procedure LoadConfig;
    Procedure SaveConfig;
    function CreateBitmap(AStat: TStat): TBitmap;
    procedure AddToListView(AStat: TStat; ABmp: TBitmap);
    procedure DrawGraphs(Cnt: PtrInt);
    procedure MaybeOpenInLazIDE;
    procedure MaybeOpenInExternalEditor;
    procedure RefreshTranslations(All: boolean);
    procedure ConfigureContextPopUp(AdvancedMode: boolean);
  public
    { public declarations }
    property PoFamilyList: TPoFamilyList read FPoFamilyList write FPoFamilyList;
    property PoFamilyStats: TPoFamilyStats read FPoFamilyStats write FPoFamilyStats;
    property Settings: TPoCheckerSettings read FSettings write FSettings;
  end;

var
  GraphStatForm: TGraphStatForm;

implementation

{$R *.lfm}


{ TGraphStatForm }

const
  Radius = 40;   //when we have anti-aliasing we can reduce this
  BmpWH = 2 * Radius;
  clBackGround = clWhite;
  clTranslated = clGreen;
  clUnTranslated = clRed;
  clFuzzy = clFuchsia;

  {
  ListView
    @designtime
    AutoSort := False
    ReadOnly := True
    ScrollBars := ssAutoBoth
    ViewStyle := vsIcon

    @runtime
    Color := clBackGround
    LargeImages := FImgList;
    Align := alClient;

  }


procedure TGraphStatForm.FormShow(Sender: TObject);
begin
  FOldHintHidePause := Application.HintHidePause;
  Application.HintHidePause := 5000;
  LoadConfig;
  WindowState := Settings.GraphFormWindowState;
  Application.QueueAsyncCall(@DrawGraphs, FPoFamilyStats.Count);
end;

procedure TGraphStatForm.IDEEditorMenuItemClick(Sender: TObject);
begin
  MaybeOpenInLazIDE;
end;

procedure TGraphStatForm.ListViewMouseMove(Sender: TObject; Shift: TShiftState;
  X, Y: Integer);
var
  Item: TListItem;
  Index: Integer;
  AStat: TStat;
begin
  Item := Listview.GetItemAt(X, Y);
  if Assigned(Item) then
  begin
    Index := Item.Index;
    AStat := FPoFamilyStats.Items[Index];
    ListView.Hint := Format(sStatHint,[AStat.NrTranslated, AStat.PercTranslated,
                                       AStat.NrUnTranslated, AStat.PercUnTranslated,
                                       AStat.NrFuzzy, AStat.PercFuzzy, AStat.NrErrors]);
  end
  else
  begin
    ListView.Hint := '';
    Application.HideHint;
  end;

end;

procedure TGraphStatForm.ListViewMouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
var
  anItem: TListItem;
  anIndex: Integer;
  AStat: TStat;
  CurrScreenPoint: TPoint;
begin
  if Button = mbRight then
  begin
    anItem := Listview.GetItemAt(X, Y);
    if Assigned(anItem) then
    begin
      anIndex := anItem.Index;
      AStat := FPoFamilyStats.Items[anIndex];
      Fn := AStat.PoName;
      CurrentMasterPoFile := ExtractMasterNameFromChildName(AStat.PoName);
      ConfigureContextPopUp(ssShift in Shift);
      CurrScreenPoint := ListView.ClientToScreen(Point(X, Y));
      ContextPopupMenu.PopUp(CurrScreenPoint.X, CurrScreenPoint.Y);
    end;
  end;
end;

procedure TGraphStatForm.POEditorMenuItemClick(Sender: TObject);
begin
  if OpenDocument(Fn) = false then
    ShowMessage(Format(SOpenFail,[Fn]));
end;

procedure TGraphStatForm.RefreshAllMenuItemClick(Sender:
  TObject);
begin
  RefreshTranslations(true);
end;

procedure TGraphStatForm.RefreshCurrMenuItemClick(Sender: TObject);
begin
  RefreshTranslations(false);
end;

procedure TGraphStatForm.RefreshTranslations(All: boolean);
var
  i: integer;
  AbortUpdate: boolean;
begin
  //"All" parameter defines if we refresh current family only (false) or all families (true)
  StatusLabel.Visible := true;
  RefreshCurrMenuItem.Enabled := false;
  RefreshAllMenuItem.Enabled := false;
  try
    AbortUpdate := false;
    i := 0;
    while (i<PoFamilyList.Count) and (AbortUpdate=false) do
    begin
      try
        if All=true then
          StatusLabel.Caption := Format(sProcessingTranslationFamilyOf, [
            IntToStr(i + 1), IntToStr(PoFamilyList.Count)])
        else
          StatusLabel.Caption := sProcessingTranslationFamily;
        StatusLabel.Repaint;
        if All=true then
        begin
          UpdatePoFileTranslations(PoFamilyList.Items[i].MasterName);
          inc(i);
        end
        else
        begin
          UpdatePoFileTranslations(CurrentMasterPoFile);
          AbortUpdate := true;
        end;
      except
        on E: EPOFileError do
          if MessageDlg('POChecker', Format(
            sCannotWriteFileYouCanPressRetryToRefreshThisTransl, [LineEnding,
            E.ResFileName, LineEnding, LineEnding]), mtError, [mbRetry, mbAbort
            ], 0) = mrAbort then
            AbortUpdate := true;
      end;
    end;
  finally
    StatusLabel.Visible := false;
    RefreshCurrMenuItem.Enabled := true;
    RefreshAllMenuItem.Enabled := true;
  end;
end;

procedure TGraphStatForm.ConfigureContextPopUp(AdvancedMode: boolean);
begin
  //POEditorMenuItem is always shown, others only in advanced mode
  Separator1MenuItem.Visible := AdvancedMode;
  //ExtEditorMenuItem and IDEEditorMenuItem are invisible by default
  {$ifdef pocheckerstandalone}
  ExtEditorMenuItem.Visible := AdvancedMode;
  if Settings.ExternalEditorName <> '' then
    ExtEditorMenuItem.Enabled := true;
  {$else}
  IDEEditorMenuItem.Visible := AdvancedMode;
  {$endif}
  Separator2MenuItem.Visible := AdvancedMode;
  RefreshCurrMenuItem.Visible := AdvancedMode;
  RefreshAllMenuItem.Visible := AdvancedMode;
end;

procedure TGraphStatForm.LoadConfig;
var
  ARect: TRect;
begin
  if not Assigned(FSettings) then Exit;
  ARect := FSettings.GraphFormGeometry;
  //debugln('TGraphStatForm.LoadConfig: ARect = ',dbgs(ARect));
  if not IsDefaultRect(ARect) and IsValidRect(ARect) then
  begin
    ARect := FitToRect(ARect, Screen.WorkAreaRect);
    BoundsRect := ARect;
  end;
end;

procedure TGraphStatForm.SaveConfig;
begin
  //debugln('TGraphStatForm.SaveConfig: BoundsRect = ',dbgs(BoundsRect));
  if not Assigned(FSettings) then Exit;
  Settings.GraphFormWindowState := WindowState;
  if (WindowState = wsNormal) then
    Settings.GraphFormGeometry := BoundsRect
  else
    Settings.GraphFormGeometry := Rect(RestoredLeft, RestoredTop, RestoredLeft + RestoredWidth, RestoredTop + RestoredHeight);
end;

procedure TGraphStatForm.FormCreate(Sender: TObject);
begin
  Caption := sGrapStatFormCaption;
  POEditorMenuItem.Caption := sOpenFileInSystemPOEditor;
  ExtEditorMenuItem.Caption := sOpenFileInExternalEditor;
  IDEEditorMenuItem.Caption := sOpenFileInIDEEditor;
  RefreshCurrMenuItem.Caption := sRefreshCurrentTranslationFamily;
  RefreshAllMenuItem.Caption := sRefreshAllTranslationFamilies;
  TranslatedShape.Brush.Color := clTranslated;
  UnTranslatedShape.Brush.Color := clUntranslated;
  FuzzyShape.Brush.Color := clFuzzy;
  ListView.Color := clBackGround;
  ListView.Align := alClient;
end;

procedure TGraphStatForm.FormClose(Sender: TObject;
  var CloseAction: TCloseAction);
begin
  Application.HintHidePause := FOldHintHidePause;
end;

procedure TGraphStatForm.FormActivate(Sender: TObject);
begin
  //Doing this in TGraphStatForm.FormShow results in icons disappearing in Linux GTK2
  //DrawGraphs;
end;

procedure TGraphStatForm.ExtEditorMenuItemClick(Sender: TObject);
begin
  MaybeOpenInExternalEditor;
end;

procedure TGraphStatForm.FormDestroy(Sender: TObject);
begin
  if Assigned(FImgList) then FImgList.Free;
  SaveConfig;
end;

function TGraphStatForm.CreateBitmap(AStat: TStat): TBitmap;
const
  FullCircle = 16 * 360;
  QMark = ' ? ';
var
  Bmp: TBitmap;
  Translated16Angle, UnTranslated16Angle, Fuzzy16Angle: Integer;
  PieRect: TRect;
  TextSize: TSize;
begin
  Bmp := TBitmap.Create;
  Bmp.SetSize(BmpWH,BmpWH);
  Bmp.Canvas.AntialiasingMode := amOn; // currently effective only with Qt
  PieRect := Rect(0,0,BmpWH, BmpWH);
  with Bmp do
  begin
    //Draw background
    Canvas.Brush.Color := clBackGround;
    Canvas.FillRect(PieRect);
    Canvas.Pen.Color := clBackGround;
    //All angles in 16th of a degree
    Translated16Angle     := Round(AStat.FracTranslated * FullCircle);
    UnTranslated16Angle   := Round(AStat.FracUntranslated * FullCircle);
    Fuzzy16Angle          := Round(AStat.FracFuzzy * FullCircle);

    if Translated16Angle > 0 then
    begin
      Canvas.Brush.Color:= clTranslated;
      if Translated16Angle = FullCircle then
        Canvas.Ellipse(PieRect)
      else
        Canvas.RadialPie(PieRect.Left,PieRect.Top,PieRect.Right,PieRect.Bottom,0,Translated16Angle);;
    end;

    if UnTranslated16Angle > 0 then
    begin
      Canvas.Brush.Color:= clUnTranslated;
      if UnTranslated16Angle = FullCircle then
        Canvas.Ellipse(PieRect)
      else
        Canvas.RadialPie(PieRect.Left,PieRect.Top,PieRect.Right,PieRect.Bottom,Translated16Angle,UnTranslated16Angle);;
    end;

    if Fuzzy16Angle > 0 then
    begin
      Canvas.Brush.Color:= clFuzzy;
      if Fuzzy16Angle = FullCircle then
        Canvas.Ellipse(PieRect)
      else
        Canvas.RadialPie(PieRect.Left,PieRect.Top,PieRect.Right,PieRect.Bottom,Translated16Angle+UnTranslated16Angle,Fuzzy16Angle);
    end;
    if AStat.NrErrors <> 0 then begin
      Bmp.Canvas.Font := Font;
      Canvas.Font.Size := BmpWH div 6;
      Canvas.Font.Style:= [fsBold];
      Canvas.Font.Color:= clRed;
      TextSize := Bmp.Canvas.TextExtent(QMark);
      Canvas.Brush.Color:= clBlack;
      Canvas.Rectangle(0,PieRect.Bottom-TextSize.cy-4,TextSize.cx+4,PieRect.Bottom);
      Canvas.Brush.Color:= clYellow;
      Canvas.TextOut(2,PieRect.Bottom-TextSize.cy-2,QMark);
    end;
  end;
  Result := Bmp;
end;

procedure TGraphStatForm.AddToListView(AStat: TStat; ABmp: TBitmap);
var
  ImgIndex: Integer;
  ListItem: TListItem;
begin
  ImgIndex := FImgList.AddMasked(ABmp, clBackGround);
  ListItem := ListView.Items.Add;
  ListItem.Caption := AStat.ShortPoName;
  ListItem.ImageIndex := ImgIndex;
end;

procedure TGraphStatForm.DrawGraphs(Cnt: PtrInt);
var
  Bmp: TBitmap;
  AStat: TStat;
  Index: Integer;
  Cur: TCursor;
begin
  if Assigned(FImgList) then FImgList.Free;
  FImgList := TImageList.CreateSize(BmpWH, BmpWH);
  ListView.Clear;
  ListView.LargeImages := FImgList;
  ListView.BeginUpdate;
  Cur := Screen.Cursor;
  Screen.Cursor := crHourGlass;
  try
    StatusLabel.Visible := True;
    for Index := 0 to FPoFamilyStats.Count - 1 do
    begin
      StatusLabel.Caption := Format(sCreatingIconXofY,[Index + 1, Cnt]);
      StatusLabel.Repaint;
      AStat := FPoFamilyStats.Items[Index];
      Bmp := CreateBitmap(AStat);
      AddToListView(AStat, Bmp);

      //if there are many icons to draw, occasionally update the ListView as visual feedback
      if (((Index + 1) mod 25) = 0) then
      begin
        ListView.EndUpdate;
        ListView.Repaint;
        ListView.BeginUpdate;
      end;

      Bmp.Free;
    end;
  finally
    ListView.EndUpdate;
    Screen.Cursor := Cur;
    StatusLabel.Visible := False;
  end;
end;

procedure TGraphStatForm.MaybeOpenInExternalEditor;
{$ifdef POCHECKERSTANDALONE}
var
  Proc: TProcessUtf8;
{$endif}
begin
  {$ifdef POCHECKERSTANDALONE}
  Proc := TProcessUtf8.Create(nil);
  try
    Proc.Options := [];
    Proc.Executable := Settings.ExternalEditorName;
    Proc.Parameters.Add(Fn);
    try
      Proc.Execute;
    except
      on E: EProcess do
      begin
        debugln('TGraphStatForm.ListViewMouseUp:');
        debugln('  Exception occurred of type ',E.ClassName);
        debugln('  Message: ',E.Message);
        ShowMessage(Format(SOpenFailExternal,[Fn,Settings.ExternalEditorName]));
      end;
    end;
  finally
    Proc.Free;
  end;
  {$endif}
end;

procedure TGraphStatForm.MaybeOpenInLazIDE;
{$ifndef POCHECKERSTANDALONE}
var
  mr: TModalResult;
  PageIndex,WindowIndex: Integer;
  OpenFlags: TOpenFlags;
{$endif}
begin
  {$ifndef POCHECKERSTANDALONE}
  PageIndex:= -1;
  WindowIndex:= -1;
  OpenFlags:= [ofOnlyIfExists,ofAddToRecent,ofRegularFile,ofConvertMacros];
  mr := LazarusIde.DoOpenEditorFile(Fn,PageIndex,WindowIndex,OpenFlags);
  if mr = mrOk then
    ModalResult:= mrOpenEditorFile //To let caller know what we want to do
  else
    ShowMessage(Format(SOpenFail,[Fn]));
  {$endif}
end;

end.