Jump to content
uniGUI Discussion Forums


uniGUI Subscriber
  • Content Count

  • Joined

  • Last visited

  • Days Won


elGringo last won the day on October 27

elGringo had the most liked content!

Community Reputation

40 Excellent

About elGringo

  • Rank
    Advanced Member

Recent Profile Visitors

198 profile views
  1. elGringo

    GarbageCollector class - what do you think?

    Thnks! Just thought there is the way that shows "SomeUnit, string 157, memoryLeak 120 Kb, TStringList" ))
  2. elGringo

    GarbageCollector class - what do you think?

    And what about this example? Wrapper for classes, based on ARC ? Looks smart ))) So, you may avoid try... finally... and just Create Use Forget ))) unit uSmartPointer; interface type ISmartPointer<T> = reference to function: T; TSmartPointer<T: class, constructor> = class(TInterfacedObject, ISmartPointer<T>) private FValue: T; function Invoke: T; procedure SetValue(const Value: T); public constructor Create; overload; constructor Create(AValue: T); overload; destructor Destroy; override; function Extract: T; property Value: T read FValue write SetValue; end; implementation constructor TSmartPointer<T>.Create; begin inherited Create; FValue := T.Create; end; constructor TSmartPointer<T>.Create(AValue: T); begin inherited Create; FValue := AValue; end; destructor TSmartPointer<T>.Destroy; begin FValue.Free; inherited; end; function TSmartPointer<T>.Invoke: T; begin Result := FValue; end; procedure TSmartPointer<T>.SetValue(const Value: T); begin FValue := Value; end; function TSmartPointer<T>.Extract: T; begin Result := FValue; FValue := nil; end; end. Example of use procedure Smart; var sl: ISmartPointer<TStringList>; begin sl := TSmartPointer<TStringList>.Create(); sl.Add('I am inside automanaged StringList'); end;
  3. elGringo

    GarbageCollector class - what do you think?

    and besides, my topic start code here gives 2 possibilities -manage code manually as usual, by grouping instances by tag -100 % clear on App Close if you have forgotten to clear smth inside the scope (protection from human factor...) Regards...
  4. elGringo

    GarbageCollector class - what do you think?

    Another alternative to the class i showed is ARC, but here we should know about weak, unsafe links and raw pointers I'm reading now book Delphi Memory Management for Classic and ARC Compilers...
  5. elGringo

    GarbageCollector class - what do you think?

    hm... agree with you, just it was start for discussion about the memory management in Delphi. So, could you say how do you look for memory leaks with fastMM - i don't know that for the moment. I use only ReportMemoryLeaksOnShutdown := true; But this approach only says me that there is a memory leak and what is leaking TStringList or etc... But how, for example you find memory leak in special exact place of code? Regards, Stan
  6. look at this class unit uGC; interface uses System.Generics.Collections, Rtti, System.Classes; type TGarbageCollector = class(TComponent) public const DEFAULT_TAG = 'DEFAULT_TAG'; private items: TDictionary<TObject, string>; public destructor Destroy; override; constructor Create(AOwner: TComponent); override; function Add<T>(item: T): T; overload; function Add<T>(item: T; const tag: string): T; overload; procedure Collect(const tag: string); end; var GC: TGarbageCollector; implementation uses System.Types, System.SysUtils; constructor TGarbageCollector.Create(AOwner: TComponent); begin inherited; items := TObjectDictionary<TObject, string>.Create([doOwnsKeys]); end; destructor TGarbageCollector.Destroy; begin items.free(); inherited Destroy; end; function TGarbageCollector.Add<T>(item: T): T; begin result := Add(item, DEFAULT_TAG); end; function TGarbageCollector.Add<T>(item: T; const tag: string): T; var obj: TObject; v: TValue; begin v := TValue.From<T>(item); if v.IsObject then begin items.add(v.AsObject, tag); result := item; end else raise Exception.Create('not an Object'); end; procedure TGarbageCollector.Collect(const tag: string); var key: TObject; item: TPair<TObject, string>; gcList: TList<TObject>; begin gcList := TList<TObject>.Create(); try for item in items do begin if (item.Value = tag) then gcList.add(item.Key); end; for key in gcList do items.remove(key); finally gcList.free(); end; end; end. Create it program GarbageCollector; uses Vcl.Forms, uMain in 'uMain.pas' {Main}, uGC in 'uGC.pas', uSomeClass in 'uSomeClass.pas'; {$R *.res} begin Application.Initialize; Application.MainFormOnTaskbar := True; GC := TGarbageCollector.Create(Application); // <<< Application.CreateForm(TMain, Main); Application.Run; end. use it someInstance := GC.Add(TSomeClass.Create(nil), 'TSomeClassTag'); // do smth with someInstance //now destroy GC.Collect('TSomeClassTag'); // anotherInstance := GC.Add(TSomeClass.Create(nil), 'TSomeClassTag'); // do smth with anotherInstance // not destroying here - will be destroyed on app destroy... what do you think?
  7. elGringo

    Are you more than 40 years old?

    Great ))) We can open museum here ))) But as for seriously, we could be any age, any technology stack, more important is the love of what you are doing and how. On any language we can do good and bad things, popularity will always be changable. Choose yours! When i started this topic, i tried to understand, should i continue with Delphi or switch to C#, as for now, i understand - both of them will always have their lovers) And i have projects on both as for now. Knowledge of different technologies enriches your skills as a programmer. For ex. i love mvc pattern from asp mvc and use it in my current projects, 'cause it gives clear transperent code... And of course, UniGUI, the best framework for Delphi and Farshad is the Genious of Delphi ))) Regards...
  8. elGringo

    MVC pattern in UniGUI vs traditional coding to form

    Unit testing quite easily maybe done with DUnitX. But "Clean Code" and "Readability" - it is from practice... Which code pattern to use is also good question...
  9. Asked myself - answered myself my workAround procedure TLeftMenu2.OnClickElement(Sender: TObject); function getLeftMenuElement(aID: integer): TLeftMenu2Element; var i: Integer; begin Result := nil; for i := 0 to FOL.Count - 1 do if FOL[i].Tag = aID then begin result := FOL[i]; break; end; end; var leftMenu2Element: TLeftMenu2Element; begin if (Sender is TUniLabel) then leftMenu2Element := getLeftMenuElement((Sender as TUniLabel).Tag); showmessage(leftMenu2Element.lName.Caption); end;
  10. Hi, having 2 classes 1 frame is created on another Second frame on 1 frame there is TUniLabel for example i'm trying to handle onClick event of label in Second frame and assign it like FOL[i].lName.OnClick := OnClickElement; here i have "Invalid type Cast" procedure TLeftMenu2.OnClickElement(Sender: TObject); var i: integer; begin showmessage( // ((Sender as TUniLabel).Parent as TLeftMenu2Element).ClassName // << invalid type Cast ); // end; As Delphi coder I'm doing everything correct, but as UniGUI coder i'm being not so experienced i tried to do things like procedure TLeftMenu2.OnClickElement(Sender: TObject); var i: integer; begin showmessage( // ((Sender as TUniLabel).Parent.Parent).ClassName // ); // end; and on click i have TUniSimplePanel - which is not my class. Is any way to get instance of TLeftMenu2Element - 1 frame on click in 2 frame? Below full code of 2 frames unit uLeftMenu2Element; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, uniGUITypes, uniGUIAbstractClasses, uniGUIClasses, uniGUIFrame, uniGUIBaseClasses, uniPanel, uniImage, uniLabel; type TLeftMenu2Element = class(TUniFrame) iPic: TUniImage; lName: TUniLabel; procedure UniFrameCreate(Sender: TObject); procedure iPicClick(Sender: TObject); procedure lNameClick(Sender: TObject); private FIsExpanded: boolean; procedure SetIsExpanded(const Value: boolean); { Private declarations } public { Public declarations } property IsExpanded: boolean read FIsExpanded write SetIsExpanded; end; implementation {$R *.dfm} uses uLeftMenu2; procedure TLeftMenu2Element.iPicClick(Sender: TObject); begin if FIsExpanded then begin FIsExpanded := false; iPic.Picture.LoadFromFile('pics/arrowRight16x16.png') end else // if not expanded begin FIsExpanded := true; iPic.Picture.LoadFromFile('pics/arrowDown16x16.png'); end; end; procedure TLeftMenu2Element.lNameClick(Sender: TObject); begin // end; procedure TLeftMenu2Element.SetIsExpanded(const Value: boolean); begin FIsExpanded := Value; end; procedure TLeftMenu2Element.UniFrameCreate(Sender: TObject); begin iPic.Picture.LoadFromFile('pics/arrowRight16x16.png'); lName.Top := trunc(Self.Height / 2 - lName.Height / 2); iPic.Top := trunc(Self.Height / 2 - iPic.Height / 2); end; end. And another class unit uLeftMenu2; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, uniGUITypes, uniGUIAbstractClasses, uniGUIClasses, uniGUIFrame, uLeftMenu2Element, System.Generics.Collections, uniGUIBaseClasses, uniPanel; type TLeftMenu2 = class(TUniFrame) procedure UniFrameCreate(Sender: TObject); procedure UniFrameDestroy(Sender: TObject); private { Private declarations } FOL: TObjectList<TLeftMenu2Element>; procedure OnClickElement(Sender: TObject); public { Public declarations } procedure LoadGoodsTree; // procedure LoadChildren(aID: integer); end; implementation uses FireDAC.Comp.Client, MainModule, uniLabel; {$R *.dfm} procedure TLeftMenu2.LoadGoodsTree; procedure loadNullLevel(); var q: TFDquery; i: integer; h: integer; itemName: string; begin h := 0; q := TFDquery.Create(nil); try with q do begin Connection := UniMainModule.DB.FDConnection; sql.Text := 'SELECT * FROM sewworld_db.goods where idParent is NULL order by sortIndex'; Disconnect(); Open; while not EOF do begin i := FOL.Add(TLeftMenu2Element.Create(Self)); itemName := FieldByName('name').AsString; if itemName.Length > 15 then itemName := itemName.Substring(0, 15) + '...'; FOL[i].lName.Caption := itemName; FOL[i].Parent := Self; FOL[i].Left := 0; FOL[i].Top := h; FOL[i].Show; FOL[i].lName.OnClick := OnClickElement; h := h + FOL[i].Height; Next; end; Close; end; finally q.Free; end; end; begin loadNullLevel(); end; procedure TLeftMenu2.OnClickElement(Sender: TObject); var i: integer; begin showmessage( // ((Sender as TUniLabel).Parent as TLeftMenu2Element).ClassName // ); // end; procedure TLeftMenu2.UniFrameCreate(Sender: TObject); begin FOL := TObjectList<TLeftMenu2Element>.Create; end; procedure TLeftMenu2.UniFrameDestroy(Sender: TObject); begin FOL.Free; end; end.
  11. elGringo

    download file from HTML hyperLink

    Thank you delphiDude !!! I will watch your example. below - decision from DD procedure TMainForm.bTestClick(Sender: TObject); var html: string; begin html := '<a href="#" onclick="return myFunction();">Click Me</a>' + // '<script>' + // 'function myFunction() {' + // // 'replaceThis; ' + // 'ajaxRequest(MainForm.window, "getFile", [ "filePath=12345", "param1=B"]);' + // 'return;' + // '}' + // '</script>'; // FFilePath := f.qFiles.FieldByName('filePath').AsString; UniSession.AddJS(UniHTMLMemo.JSName + '.execCmd(''InsertHTML'',''' + html + ''')'); end; please try to use " instead of ' and '
  12. elGringo

    download file from HTML hyperLink

    the only problem is i have js exception (see picture) my temporary decision is using timer - but it is not Ok decision ! html := '<a href="#" onclick="return myFunction();">' + f.qFiles.FieldByName('fileName').AsString + '</a>' + // '<script>' + // 'function myFunction() {' + // 'replaceThis; ' + // <<<<<<<<<<<<<<<< !!! Here // 'ajaxRequest(MainForm.window, ''getFile'', [ ''filePath=12345'', ''param1=B'']);' + // 'return;' + // '}' + // '</script>'; FFilePath := f.qFiles.FieldByName('filePath').AsString; UniSession.AddJS(UniHTMLMemo.JSName + '.execCmd(''InsertHTML'',''' + html + ''')'); tReplace.Enabled := true; and on timer tReplace.Tag := tReplace.Tag + 1; // inc(tReplace.Tag); if tReplace.Tag = 2 then begin UniHTMLMemo.Text := StringReplace(UniHTMLMemo.Text, 'replaceThis', 'ajaxRequest(MainForm.window, ''getFile'', [ ''filePath=' + FFilePath + ''', ''param1=B'']);', [rfReplaceAll]); tReplace.Enabled := false; end; What could be the problem? You can easily reproduce the case by placing UniHTMLMemo and applying this code from previous message.
  13. elGringo

    download file from HTML hyperLink

    ok, my decision of my problem is <html> <a href="#" onclick="return myFunction();">Link</a> <script> function myFunction() { alert('here'); ajaxRequest(MainForm.window, 'getFile', [ 'filePath=12345', 'param1=B' ]); } </script> </html> and on Server Side if SameText(EventName, 'getFile') then begin showmessage(Params.Values['filePath']); end;
  14. elGringo

    download file from HTML hyperLink

    Yes, file in root/UploadFolder What you suggest?
  15. elGringo

    download file from HTML hyperLink

    ok, Progress, i can do like this in HTML <html> <a href="http://www.google.com" onclick="return myFunction();">Link</a> <script> function myFunction() { document.getElementById(".myDiv").style.flexGrow = "5"; } </script> </html> next step is to learn to request through ajaxRequest from free HTML - is that possible?