2023년 5월 3일 수요일

A colorful graphic show made with Delphi Firemonkey.

A colorful graphic show made with Delphi Firemonkey. 





The video below has about 4 million views.




2023년 3월 13일 월요일

Using ChatGPT in Delphi

// ChatGPT Personal Key : https://beta.openai.com/account/api-keys





unit MainCGPT;

interface

uses

  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants, System.JSON,

  FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.Memo.Types, FMX.StdCtrls, FMX.Controls.Presentation, FMX.ScrollBox,

  FMX.Memo, System.Net.URLClient, System.Net.HttpClient, System.Net.HttpClientComponent, FMX.Layouts;

type

  TMForm = class(TForm)

    Memo_Ans: TMemo;

    Memo_HanQ: TMemo;

    BT_Question: TButton;

    NetHTTPClient1: TNetHTTPClient;

    NetHTTPRequest1: TNetHTTPRequest;

    Label1: TLabel;

    Label3: TLabel;

    Layout1: TLayout;

    Layout3: TLayout;

    SpeedButton1: TSpeedButton;

    procedure BT_QuestionClick(Sender: TObject);

    procedure NetHTTPClient1RequestCompleted(const Sender: TObject; const AResponse: IHTTPResponse);

    procedure Memo_HanQDblClick(Sender: TObject);

  private

    { Private declarations }

  public

    { Public declarations }

  end;


var

  MForm: TMForm;


  // ChatGPT key :  https://beta.openai.com/account/api-keys

  Const MyGPTKey = 'mykey_1234abcd';   // Input your key


implementation

{$R *.fmx}

procedure TMForm.Memo_HanQDblClick(Sender: TObject);

begin

  Memo_HanQ.Lines.Clear;

end;


// Question *********************************************

procedure TMForm.BT_QuestionClick(Sender: TObject);

var

  LPostdata: string;

  LPostDataStream: TStringStream;

begin

  LPostData := '{' +

    '"model": "text-davinci-003",'+

    '"prompt": "' + Memo_HanQ.Text + '",'+

    '"max_tokens": 2048,'+

    '"temperature": 0'+

    '}';


  LPostDataStream := TStringStream.Create( LPostData, TEncoding.UTF8);


  NetHTTPClient1.CustomHeaders['Authorization'] := 'Bearer ' + MyGPTKey;

  NetHTTPClient1.CustomHeaders['Content-Type'] := 'application/json';


  LPostDataStream.Position := 0;

  NetHTTPClient1.Post('https://api.openai.com/v1/completions', LPostDataStream );

end;



// Answer ********************************************************************************

procedure TMForm.NetHTTPClient1RequestCompleted(const Sender: TObject; const AResponse: IHTTPResponse);

var

  LString, ansStr : string;

  LJson: TJsonObject;


begin

  if AResponse.StatusCode = 200 then

  begin

     LString := AResponse.ContentAsString;

     LJson := TJSONObject.ParseJSONValue(LString) as TJSONObject;

     try

       ansStr := LJson.GetValue('choices').A[0].FindValue('text').Value;

     finally

       LJson.Free;

     end;

  end

  else

    ansStr := 'HTTP response code: ' + AResponse.StatusCode.ToString;


  Memo_Ans.Lines.Clear;

  Memo_Ans.Lines.Add( ansStr );

end;

end.




2022년 9월 22일 목요일

Delphi FMX Android run time permission sample project demo

 



unit PMUnit;


interface


uses

  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,  System.Permissions,

  FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.Controls.Presentation, FMX.StdCtrls;


type

  TForm1 = class(TForm)

    Button1: TButton;

    Button2: TButton;

    Button3: TButton;

    procedure Button1Click(Sender: TObject);

    procedure Button2Click(Sender: TObject);

    procedure Button3Click(Sender: TObject);

    procedure FormCreate(Sender: TObject);

  private

    procedure DisplayRationale(Sender: TObject; const APermissions: TClassicStringDynArray; const APostRationaleProc: TProc);

    procedure Loacation_PermissionRequestResult(Sender: TObject; const APermissions: TClassicStringDynArray;

      const AGrantResults: TClassicPermissionStatusDynArray);

    procedure Call_PermissionRequestResult(Sender: TObject; const APermissions: TClassicStringDynArray;

      const AGrantResults: TClassicPermissionStatusDynArray);

    procedure Camera_PermissionRequestResult(Sender: TObject; const APermissions: TClassicStringDynArray;

      const AGrantResults: TClassicPermissionStatusDynArray);

    { Private declarations }

  public

    { Public declarations }

    FPermissionLoacation, FPermissionCall, FPermissionCamera : string;

  end;


var

  Form1: TForm1;


implementation


uses

{$IFDEF ANDROID}

   Androidapi.JNI.Os,

   Androidapi.Helpers,

   AndroidApi.Jni.JavaTypes,

   FMX.DialogService;

{$ENDIF}



{$R *.fmx}


procedure TForm1.FormCreate(Sender: TObject);

begin

  FPermissionLoacation := JStringToString(TJManifest_permission.JavaClass.ACCESS_FINE_LOCATION );

  FPermissionCall      := JStringToString(TJManifest_permission.JavaClass.CALL_PHONE );

  FPermissionCamera    := JStringToString(TJManifest_permission.JavaClass.CAMERA );

end;




procedure TForm1.Button1Click(Sender: TObject);

begin

  PermissionsService.RequestPermissions([FPermissionLoacation], Loacation_PermissionRequestResult, DisplayRationale);

end;


procedure TForm1.Button2Click(Sender: TObject);

begin

  PermissionsService.RequestPermissions([FPermissionCall], Call_PermissionRequestResult, DisplayRationale);

end;



procedure TForm1.Button3Click(Sender: TObject);

begin

  PermissionsService.RequestPermissions([FPermissionCamera], Camera_PermissionRequestResult, DisplayRationale);

end;


procedure TForm1.DisplayRationale(Sender: TObject; const APermissions: TClassicStringDynArray; const APostRationaleProc: TProc);

var

  I: Integer;

  RationaleMsg: string;

begin

  for I := 0 to High(APermissions) do

  begin

    if APermissions[I] = FPermissionLoacation then

      RationaleMsg := RationaleMsg + 'The app needs to access the Permission Location' + SLineBreak + SLineBreak


    else if APermissions[I] = FPermissionCall then

      RationaleMsg := RationaleMsg + 'The app needs to access the Permission Call' + SLineBreak + SLineBreak


    else if APermissions[I] = FPermissionCamera then

      RationaleMsg := RationaleMsg + 'The app needs to access the Permission Camera';


  end;


  // Show an explanation to the user *asynchronously* - don't block this thread waiting for the user's response!

  // After the user sees the explanation, invoke the post-rationale routine to request the permissions

  TDialogService.ShowMessage(RationaleMsg,

    procedure(const AResult: TModalResult)

    begin

      APostRationaleProc;

    end)

end;



procedure TForm1.Loacation_PermissionRequestResult(Sender: TObject; const APermissions: TClassicStringDynArray; const AGrantResults: TClassicPermissionStatusDynArray);

begin

  // 3 permissions involved: CAMERA, READ_EXTERNAL_STORAGE, WRITE_EXTERNAL_STORAGE

//  if (Length(AGrantResults) = 3) and

//     (AGrantResults[0] = TPermissionStatus.Granted) and

//     (AGrantResults[1] = TPermissionStatus.Granted) and

//     (AGrantResults[2] = TPermissionStatus.Granted) then


  if ( Length(AGrantResults) = 1) and

     (AGrantResults[0] = TPermissionStatus.Granted) then

    TDialogService.ShowMessage('Location permissions OK ' )

  else

    TDialogService.ShowMessage('The required permissions are not granted');

end;



procedure TForm1.Call_PermissionRequestResult(Sender: TObject; const APermissions: TClassicStringDynArray; const AGrantResults: TClassicPermissionStatusDynArray);

begin

  if ( Length(AGrantResults) = 1) and

     (AGrantResults[0] = TPermissionStatus.Granted) then

    TDialogService.ShowMessage('Call permissions OK ' )

  else

    TDialogService.ShowMessage('The required permissions are not granted');

end;


procedure TForm1.Camera_PermissionRequestResult(Sender: TObject; const APermissions: TClassicStringDynArray; const AGrantResults: TClassicPermissionStatusDynArray);

begin

  if ( Length(AGrantResults) = 1) and

     (AGrantResults[0] = TPermissionStatus.Granted) then

    TDialogService.ShowMessage('Camera permissions OK ' )

  else

    TDialogService.ShowMessage('The required permissions are not granted');

end;




end.