File: lrformstorage.pas

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 (312 lines) | stat: -rw-r--r-- 9,370 bytes parent folder | download | duplicates (6)
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
{ LazReport dialogs control

  Copyright (C) 2012-2013 alexs alexs75.at.yandex.ru

  This library is free software; you can redistribute it and/or modify it
  under the terms of the GNU Library General Public License as published by
  the Free Software Foundation; either version 2 of the License, or (at your
  option) any later version with the following modification:

  As a special exception, the copyright holders of this library give you
  permission to link this library with independent modules to produce an
  executable, regardless of the license terms of these independent modules,and
  to copy and distribute the resulting executable under terms of your choice,
  provided that you also meet, for each linked independent module, the terms
  and conditions of the license of that module. An independent module is a
  module which is not derived from or based on this library. If you modify
  this library, you may extend this exception to your version of the library,
  but you are not obligated to do so. If you do not wish to do so, delete this
  exception statement from your version.

  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. See the GNU Library General Public License
  for more details.

  You should have received a copy of the GNU Library General Public License
  along with this library; if not, write to the Free Software Foundation,
  Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1335, USA.
}

unit lrFormStorage;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, LazUTF8, LazFileUtils, LR_Class, Graphics, Forms;

type

  { TLRFormStorage }

  TLRFormStorage = class(TfrNonVisualControl)
  private
    FActive: Boolean;
    FConfigFileName: string;
    FStoredProperties: TStringList;
    FVersion: integer;
    function GetStoredProperties: TStringList;
    procedure SetStoredProperties(AValue: TStringList);

    procedure ShowEditorForm(Sender: TObject);
    procedure StoreProps;
    procedure LoadProps;

    procedure OnFormClose(Sender: TObject; var CloseAction: TCloseAction);
  protected
    procedure AfterLoad;override;
    function ExecMetod(const AName: String; p1, p2, p3: Variant; var Val: Variant):boolean;override;
    function CfgFileName(ACreatePath:boolean):string;
    procedure AttachToParent; override;
  public
    constructor Create(AOwnerPage:TfrPage); override;
    destructor Destroy; override;

    procedure LoadFromXML(XML: TLrXMLConfig; const Path: String); override;
    procedure SaveToXML(XML: TLrXMLConfig; const Path: String); override;
  published
    property StoredProperties:TStringList read GetStoredProperties write SetStoredProperties;
    property Version:integer read FVersion write FVersion;
    property Active:Boolean read FActive write FActive;
    property ConfigFileName:string read FConfigFileName write FConfigFileName;
  end;

implementation

uses LResources, lrFormStorageEditor, Controls, typinfo, FileUtil, LR_Const,
  LazUtilsStrConsts;

var
  lrBMP_LRFormStorage:TBitmap = nil;
  lrFormStorageEditorForm: TlrFormStorageEditorForm;

procedure InitLRComp;
begin
  lrFormStorageEditorForm:=TlrFormStorageEditorForm.Create(nil);

  if not assigned(lrBMP_LRFormStorage) then
  begin
    lrBMP_LRFormStorage := TbitMap.Create;
    lrBMP_LRFormStorage.LoadFromResourceName(HInstance, 'TLRFormStorage');
    frRegisterObject(TLRFormStorage, lrBMP_LRFormStorage, 'TLRFormStorage', lrFormStorageEditorForm, otlUIControl, nil);
  end;

end;

{ TLRFormStorage }

procedure TLRFormStorage.SetStoredProperties(AValue: TStringList);
begin
  FStoredProperties.Assign(AValue);
end;

procedure TLRFormStorage.ShowEditorForm(Sender: TObject);
begin
  lrFormStorageEditorForm.ShowEditor(Self);
end;

procedure TLRFormStorage.StoreProps;
var
  PL:TStringList;
  i:integer;

procedure DoStore(PropLine:string);
var
  PropInfo:PPropInfo;
  frObj:TfrObject;
  FPropName, St:string;
  i: Integer;
begin
  PropInfo:=FindObjectProps(PropLine, frObj, FPropName, i);

  if Assigned(frObj) and Assigned(PropInfo) then
  begin
    case PropInfo^.PropType^.Kind of
      tkChar,tkAString,tkWString, tkSString,tkLString : St:=GetStrProp(frObj, PropInfo);
      tkBool,tkInt64,tkQWord, tkInteger : St:=IntToStr(GetOrdProp(frObj, PropInfo));
      tkSet: St:=GetSetProp(frObj, PropInfo, false);
      tkFloat : St:=FloatToStr(GetFloatProp(frObj, PropInfo));
      tkEnumeration : St:=GetEnumProp(frObj, PropInfo);
    else
      exit;
    end;
    PL.Values[PropLine]:=St;
  end;
end;

begin
  PL:=TStringList.Create;
  try
    for i:=0 to FStoredProperties.Count-1 do
      DoStore(FStoredProperties[i]);
    PL.SaveToFile(UTF8ToSys(CfgFileName(true)));
  finally
    PL.Free;
  end;
end;

procedure TLRFormStorage.LoadProps;
var
  PL:TStringList;
  i:integer;
  S:string;

procedure DoLoad(PropLine:string);
var
  PropInfo:PPropInfo;
  frObj:TfrObject;
  FPropName, St:string;
  i: Integer;
begin
  if PL.Values[PropLine]<>'' then
  begin
    PropInfo:=FindObjectProps(PropLine, frObj, FPropName, i);

    if Assigned(PropInfo) then
    begin
      case PropInfo^.PropType^.Kind of
        tkChar,tkAString,tkWString, tkSString,tkLString : SetStrProp(frObj, PropInfo, PL.Values[PropLine]);
        tkBool,tkInt64,tkQWord, tkInteger
                                    : begin
                                        if AnsiCompareText(PropInfo^.PropType^.Name,'TGraphicsColor')=0 then
                                          SetOrdProp(frObj, PropInfo, StringToColor(PL.Values[PropLine]))
                                        else
                                          SetOrdProp(frObj, PropInfo, StrToIntDef(PL.Values[PropLine], 0))
                                      end;
        tkSet                       : SetSetProp(frObj, PropInfo, PL.Values[PropLine]);
        tkFloat                     : SetFloatProp(frObj ,PropInfo, StrToFloatDef(PL.Values[PropLine], 0));
        tkEnumeration               : SetEnumProp(frObj, PropInfo, PL.Values[PropLine]);
      end;
    end
  end;
end;

begin
  S:=CfgFileName(false);
  if FileExistsUTF8(S) then
  begin
    PL:=TStringList.Create;
    try
      PL.LoadFromFile(UTF8ToSys(S));
      for i:=0 to FStoredProperties.Count-1 do
        DoLoad(FStoredProperties[i]);
    finally
      PL.Free;
    end;
  end;
end;


procedure TLRFormStorage.OnFormClose(Sender: TObject;
  var CloseAction: TCloseAction);
begin
  if Assigned(OwnerForm) then
  begin
    if (TForm(Sender).ModalResult = mrOk) and FActive then
      StoreProps;
  end;
end;

function TLRFormStorage.GetStoredProperties: TStringList;
begin
  Result:=FStoredProperties;
end;


procedure TLRFormStorage.AfterLoad;
begin
  inherited AfterLoad;
  //LoadProps;
end;

function TLRFormStorage.ExecMetod(const AName: String; p1, p2, p3: Variant;
  var Val: Variant): boolean;
begin
  Result:=inherited ExecMetod(AName, p1, p2, p3, Val);
  if AName = 'STOREPROPS' then
    StoreProps
  else
  if AName = 'LOADPROPS' then
    LoadProps
  else
  if AName = 'SHOWEDITORFORM' then
    ShowEditorForm(nil);
end;

function TLRFormStorage.CfgFileName(ACreatePath: boolean): string;
var
  Dir:string;
begin
  if FConfigFileName<>'' then
  begin
    Result:='';
    if ExtractFileDir(FConfigFileName) = '' then
      Result:=AppendPathDelim(GetAppConfigDirUTF8(false, true))+'LazReport'+PathDelim;
    Result:=Result + FConfigFileName;
  end
  else
  begin
    if CurReport.FileName <> SUntitled then
      Result:=AppendPathDelim(GetAppConfigDirUTF8(false, true))+'LazReport'+PathDelim+ ExtractFileNameOnly(CurReport.FileName)+'.cfglr';
  end;

  if ACreatePath then
  begin
    Dir := ExtractFilePath(Result);
    if Dir = '' then exit;
    if not ForceDirectoriesUTF8(Dir) then
      raise EInOutError.Create(SysUtils.Format(lrsUnableToCreateConfigDirectoryS,[Dir]));
  end;
end;

procedure TLRFormStorage.AttachToParent;
begin
  inherited AttachToParent;
  if OwnerForm is TForm then
    TForm(OwnerForm).OnClose:=@OnFormClose
end;

constructor TLRFormStorage.Create(AOwnerPage: TfrPage);
begin
  inherited Create(AOwnerPage);

  BaseName := 'lrFormStorage';
  FDesignOptions:=FDesignOptions + [doUndoDisable];
  FStoredProperties:=TStringList.Create;
end;

destructor TLRFormStorage.Destroy;
begin
  FreeAndNil(FStoredProperties);
  inherited Destroy;
end;

procedure TLRFormStorage.LoadFromXML(XML: TLrXMLConfig; const Path: String);
begin
  inherited LoadFromXML(XML, Path);
  FStoredProperties.Text:=XML.GetValue(Path + 'StoredProperties/Value'{%H-}, '');
  FVersion:=XML.GetValue(Path + 'Version/Value'{%H-}, 0);
  FConfigFileName:=XML.GetValue(Path + 'ConfigFileName/Value'{%H-}, '');
  FActive:=XML.GetValue(Path + 'Active/Value'{%H-}, true);
end;

procedure TLRFormStorage.SaveToXML(XML: TLrXMLConfig; const Path: String);
begin
  inherited SaveToXML(XML, Path);
  XML.SetValue(Path + 'StoredProperties/Value'{%H-}, FStoredProperties.Text);
  XML.SetValue(Path + 'Version/Value'{%H-}, FVersion);
  XML.SetValue(Path + 'ConfigFileName/Value'{%H-}, FConfigFileName);
  XML.SetValue(Path + 'Active/Value'{%H-}, FActive);
end;

initialization
  InitLRComp;
finalization
  if Assigned(lrBMP_LRFormStorage) then
    FreeAndNil(lrBMP_LRFormStorage);
  if Assigned(lrFormStorageEditorForm) then
    FreeAndNil(lrFormStorageEditorForm);
end.