Skip to content

Commit

Permalink
[SVN版本:801]
Browse files Browse the repository at this point in the history
* [QDB] 修正了转换器加载时由于引用计数错误造成存在内存泄露的问题(感谢软件高手报告)
[SVN版本:800]
* [QString]EncodeWebTime 增加对无效的日期值处理
* [QLog] CalcPerf 改成返回 IPerfCounter 接口,以便记录中间步骤时长
* [QJson] MoveTo 函数增加同级结点的支持
* [QHttpRequest] TQUrl 增加 EnableBookmark 属性,以兼容部分特殊 URL
* [QDialogBuilder] NewDialog
增加新的重载,以弹出用户自定义的窗口,并修正了弹出时,设置ModalResult不会关闭窗口的问题
[SVN版本:799]
+ 增加 Json AsExpression 的支持,将字符串当做表达式来计算,需要QMathExpr支持
[SVN版本:798]
* 修正了 Clear 函数中 AWaitRunningDone 参数未生效的问题(dragOn报告)
+ 增加 OnLookupMissed 事件支持,以便延迟添加变量
+ 增加不等 于(<>、!=) 运算符的支持
+ 增加比较操作符支持(>,>=,=,<,<=)
* 修正了StartPlugins 里,FindClose无效的问题
* 修改了TQVTBaseEditor的继承关系
  • Loading branch information
qdac committed Mar 8, 2019
1 parent 23bdb1b commit d36ebe2
Show file tree
Hide file tree
Showing 15 changed files with 643 additions and 200 deletions.
42 changes: 21 additions & 21 deletions Demos/Delphi/VCL/SimpleMathExpr/main.dfm
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ object Form1: TForm1
Top = 0
Caption = #25968#23398#34920#36798#24335#35745#31639#31034#20363
ClientHeight = 375
ClientWidth = 609
ClientWidth = 644
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Expand All @@ -12,18 +12,19 @@ object Form1: TForm1
Font.Style = []
OldCreateOrder = False
OnCreate = FormCreate
OnDestroy = FormDestroy
PixelsPerInch = 96
TextHeight = 13
object Panel1: TPanel
Left = 0
Top = 0
Width = 609
Width = 644
Height = 73
Align = alTop
BevelOuter = bvNone
ShowCaption = False
TabOrder = 0
ExplicitWidth = 571
ExplicitWidth = 609
object Edit1: TEdit
Left = 24
Top = 16
Expand Down Expand Up @@ -61,35 +62,39 @@ object Form1: TForm1
object chkNumIdentAsMultiply: TCheckBox
Left = 336
Top = 50
Width = 201
Width = 257
Height = 17
Caption = #25968#20540'+'#26631#24535#31526#35299#26512#20026#25968#20540'*'#26631#24535#31526
Caption = #25968#20540'+'#26631#24535#31526'('#22914':2x)'#35299#26512#20026#25968#20540'*'#26631#24535#31526'(2*x)'
TabOrder = 4
end
object Button2: TButton
Left = 554
Top = 14
Width = 75
Height = 25
Caption = 'Json '#31034#20363
TabOrder = 5
OnClick = Button2Click
end
end
object Panel2: TPanel
Left = 0
Top = 73
Width = 609
Width = 644
Height = 302
Align = alClient
BevelOuter = bvNone
TabOrder = 1
ExplicitLeft = 248
ExplicitTop = 176
ExplicitWidth = 185
ExplicitHeight = 41
ExplicitWidth = 609
object Memo1: TMemo
AlignWithMargins = True
Left = 115
Top = 3
Width = 491
Width = 526
Height = 255
Align = alClient
TabOrder = 0
ExplicitTop = 6
ExplicitWidth = 453
ExplicitHeight = 296
ExplicitWidth = 491
end
object ListBox1: TListBox
AlignWithMargins = True
Expand All @@ -106,22 +111,17 @@ object Form1: TForm1
ItemHeight = 13
ParentFont = False
TabOrder = 1
ExplicitLeft = 8
ExplicitTop = 6
ExplicitHeight = 296
end
object Panel3: TPanel
Left = 0
Top = 261
Width = 609
Width = 644
Height = 41
Align = alBottom
BevelOuter = bvLowered
Caption = #24038#19978#20989#25968#25110#21464#37327#21015#34920#20013#65292'X '#20026#27979#35797#29992#24120#37327#65292#20540#20026'100'#65292'Y'#20026#29992#25143#36755#20837#20540#65292'ST'#20026#22266#23450#38543#26426#20540#65292#20854#23427#20026#20869#32622#20989#25968#25110#21464#37327
TabOrder = 2
ExplicitLeft = 112
ExplicitTop = 240
ExplicitWidth = 185
ExplicitWidth = 609
end
end
end
120 changes: 111 additions & 9 deletions Demos/Delphi/VCL/SimpleMathExpr/main.pas
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ interface
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
System.Classes, Vcl.Graphics, Math,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, QMathExpr,
Vcl.ExtCtrls;
Vcl.ExtCtrls, QString, QJson;

type
TForm1 = class(TForm)
Expand All @@ -20,18 +20,35 @@ TForm1 = class(TForm)
ListBox1: TListBox;
Panel3: TPanel;
chkNumIdentAsMultiply: TCheckBox;
Button2: TButton;
procedure CalcClick(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
FExpr: IMathExpression;
class procedure DoGetYValue(Sender: TObject; AVar: TMathVar); static;
class procedure DoStableValue(Sender: TObject; AVar: TMathVar); static;
FExpr: IQMathExpression;
FJsonVars: TQJson;
class procedure DoGetYValue(Sender: TObject; AVar: TQMathVar); static;
class procedure DoStableValue(Sender: TObject; AVar: TQMathVar); static;
procedure DoGetJsonVar(Sender: IQMathExpression; const AVarName: String;
var AVar: TQMathVar);
class procedure DoGetJsonValue(Sender: TObject; AVar: TQMathVar); static;
public
{ Public declarations }
end;

TQJsonVarHelper = class helper for TQJson
private
procedure DoGetJsonVar(Sender: IQMathExpression; const AVarName: String;
var AVar: TQMathVar);
class procedure DoGetJsonValue(Sender: TObject; AVar: TQMathVar); static;
function GetAsExpression: Variant;
public
property AsExpression: Variant read GetAsExpression;
end;

var
Form1: TForm1;

Expand Down Expand Up @@ -63,6 +80,20 @@ procedure TForm1.Button1Click(Sender: TObject);
begin
Memo1.Lines.Add(TestExpr[I] + '=' + VarToStr(FExpr.Eval(TestExpr[I])));
end;
if FExpr.NumIdentAsMultiply then
begin
Memo1.Lines.Add('2PI+1=' + VarToStr(FExpr.Eval('2PI+1')));
Memo1.Lines.Add('1+2PI=' + VarToStr(FExpr.Eval('1+2PI')));
Memo1.Lines.Add('1+2PI+2=' + VarToStr(FExpr.Eval('1+2PI+2')));
end;
end;

procedure TForm1.Button2Click(Sender: TObject);
var
AItem: TQJson;
begin
AItem := FJsonVars.ItemByName('d');
Memo1.Lines.Add(AItem.AsString + '=>' + VarToStr(AItem.AsExpression));
end;

procedure TForm1.CalcClick(Sender: TObject);
Expand All @@ -72,15 +103,33 @@ procedure TForm1.CalcClick(Sender: TObject);
Memo1.Lines.Add(Edit1.Text + '=' + VarToStr(FExpr.Eval(Edit1.Text)));
end;

class procedure TForm1.DoGetYValue(Sender: TObject; AVar: TMathVar);
class procedure TForm1.DoGetJsonValue(Sender: TObject; AVar: TQMathVar);
var
AJson, AItem: TQJson;
begin
AJson := TQJson(AVar.Tag);
if AJson.HasChild(AVar.Name, AItem) then
AVar.Value := AItem.AsVariant
else
AVar.Value := Unassigned;
end;

procedure TForm1.DoGetJsonVar(Sender: IQMathExpression; const AVarName: String;
var AVar: TQMathVar);
begin
AVar := Sender.Add(AVarName, 0, 0, DoGetJsonValue);
AVar.Tag := FJsonVars;
end;

class procedure TForm1.DoGetYValue(Sender: TObject; AVar: TQMathVar);
var
S: String;
begin
if InputQuery('输入 Y 的值', '计算表达式需要知道Y值:', S) then
AVar.Value := StrToFloat(S);
end;

class procedure TForm1.DoStableValue(Sender: TObject; AVar: TMathVar);
class procedure TForm1.DoStableValue(Sender: TObject; AVar: TQMathVar);
var
V: Integer;
begin
Expand All @@ -92,12 +141,18 @@ class procedure TForm1.DoStableValue(Sender: TObject; AVar: TMathVar);
procedure TForm1.FormCreate(Sender: TObject);
var
I, J: Integer;
AVar: TMathVar;
AVar: TQMathVar;
S: String;
begin
ReportMemoryLeaksOnShutdown := True;
FExpr := TMathExpression.Create;
FExpr.Add('X', TMathVolatile.mvImmutable).Value := 100;
FJsonVars := TQJson.Create;
FJsonVars.Parse('{"a":{"b":1,"c":2},"f":20}');
// d的值通过表达式计算赋值
FJsonVars.Add('d').AsString := 'f+$a.c';
Memo1.Lines.Add('Json 变量:'#13#10 + FJsonVars.AsJson);
FExpr := TQMathExpression.Create;
FExpr.OnLookupMissed := DoGetJsonVar;
FExpr.Add('X', mvImmutable).Value := 100;
FExpr.Add('Y').OnGetValue := DoGetYValue;
FExpr.Add('ST', mvStable).OnGetValue := DoStableValue;
for I := 0 to FExpr.VarCount - 1 do
Expand Down Expand Up @@ -137,4 +192,51 @@ procedure TForm1.FormCreate(Sender: TObject);
end;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
FreeAndNil(FJsonVars);
end;

{ TQJsonVarHelper }

class procedure TQJsonVarHelper.DoGetJsonValue(Sender: TObject;
AVar: TQMathVar);
var
AItem: TQJson;
begin
AItem := TQJson(AVar.Tag);
if Assigned(AItem) then
AVar.Value := AItem.AsVariant
else
AVar.Value := Unassigned;
end;

procedure TQJsonVarHelper.DoGetJsonVar(Sender: IQMathExpression;
const AVarName: String; var AVar: TQMathVar);
var
AJson, AItem: TQJson;
begin
AJson := TQJson(Sender.Tag);
if StartWithW(PWideChar(AVarName), '$', false) then
AItem := AJson.ItemByPath(RightStrW(AVarName,
Length(AVarName) - 1, false))
else
AItem := AJson.ItemByPath(AVarName);
if Assigned(AItem) then
begin
AVar := Sender.Add(AVarName, 0, 0, DoGetJsonValue);
AVar.Tag := AItem;
end;
end;

function TQJsonVarHelper.GetAsExpression: Variant;
var
AExpr: IQMathExpression;
begin
AExpr := TQMathExpression.Create;
AExpr.OnLookupMissed := DoGetJsonVar;
AExpr.Tag := Parent;
Result := AExpr.Eval(Value);
end;

end.
20 changes: 10 additions & 10 deletions Demos/Delphi/VCL/SimpleMathExpr/simpexp.dproj.local
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<BorlandProject>
<Transactions>
<Transaction>1899-12-30 00:00:00.000.807,C:\user\SimpleMathExpr\QSimpleMathExpr.pas=C:\user\SimpleMathExpr\Unit1.pas</Transaction>
<Transaction>1899-12-30 00:00:00.000.329,C:\user\SimpleMathExpr\QSimple_math_expr.pas=C:\user\SimpleMathExpr\QMath_expr.pas</Transaction>
<Transaction>1899-12-30 00:00:00.000.716,=C:\user\SimpleMathExpr\Unit1.pas</Transaction>
<Transaction>1899-12-30 00:00:00.000.319,C:\user\SimpleMathExpr\mathexpr.dfm=C:\Users\Administrator\Documents\Embarcadero\Studio\Projects\Unit1.dfm</Transaction>
<Transaction>1899-12-30 00:00:00.000.378,C:\user\SimpleMathExpr\main.dfm=C:\user\SimpleMathExpr\mathexpr.dfm</Transaction>
<Transaction>1899-12-30 00:00:00.000.244,C:\user\SimpleMathExpr\simpexp.dproj=C:\Users\Administrator\Documents\Embarcadero\Studio\Projects\Project1.dproj</Transaction>
<Transaction>1899-12-30 00:00:00.000.378,C:\user\SimpleMathExpr\main.pas=C:\user\SimpleMathExpr\mathexpr.pas</Transaction>
<Transaction>1899-12-30 00:00:00.000.407,=C:\Users\Administrator\Documents\Embarcadero\Studio\Projects\Unit1.pas</Transaction>
<Transaction>1899-12-30 00:00:00.000.807,C:\user\SimpleMathExpr\Unit1.pas=C:\user\SimpleMathExpr\QSimpleMathExpr.pas</Transaction>
<Transaction>1899-12-30 00:00:00.000.244,C:\Users\Administrator\Documents\Embarcadero\Studio\Projects\Project1.dproj=C:\user\SimpleMathExpr\simpexp.dproj</Transaction>
<Transaction>1899-12-30 00:00:00.000.378,C:\user\SimpleMathExpr\mathexpr.dfm=C:\user\SimpleMathExpr\main.dfm</Transaction>
<Transaction>1899-12-30 00:00:00.000.532,C:\user\SimpleMathExpr\QSimpleMathExpr.pas=C:\user\SimpleMathExpr\QSimple_math_expr.pas</Transaction>
<Transaction>1899-12-30 00:00:00.000.319,C:\Users\Administrator\Documents\Embarcadero\Studio\Projects\Unit1.pas=C:\user\SimpleMathExpr\mathexpr.pas</Transaction>
<Transaction>1899-12-30 00:00:00.000.501,=C:\Users\Administrator\Documents\Embarcadero\Studio\Projects\Unit1.pas</Transaction>
<Transaction>1899-12-30 00:00:00.000.319,C:\user\SimpleMathExpr\mathexpr.pas=C:\Users\Administrator\Documents\Embarcadero\Studio\Projects\Unit1.pas</Transaction>
<Transaction>1899-12-30 00:00:00.000.532,C:\user\SimpleMathExpr\QSimple_math_expr.pas=C:\user\SimpleMathExpr\QSimpleMathExpr.pas</Transaction>
<Transaction>2019-01-12 11:03:32.329,C:\user\SimpleMathExpr\QMath_expr.pas=C:\user\SimpleMathExpr\QSimple_math_expr.pas</Transaction>
<Transaction>2019-01-12 11:03:52.642,C:\user\SimpleMathExpr\QMathExpr.pas=C:\user\SimpleMathExpr\QMath_expr.pas</Transaction>
<Transaction>1899-12-30 00:00:00.000.407,=C:\Users\Administrator\Documents\Embarcadero\Studio\Projects\Unit1.pas</Transaction>
<Transaction>1899-12-30 00:00:00.000.319,C:\Users\Administrator\Documents\Embarcadero\Studio\Projects\Unit1.dfm=C:\user\SimpleMathExpr\mathexpr.dfm</Transaction>
<Transaction>1899-12-30 00:00:00.000.378,C:\user\SimpleMathExpr\mathexpr.pas=C:\user\SimpleMathExpr\main.pas</Transaction>
<Transaction>1899-12-30 00:00:00.000.642,C:\user\SimpleMathExpr\QMath_expr.pas=C:\user\SimpleMathExpr\QMathExpr.pas</Transaction>
</Transactions>
</BorlandProject>
Binary file modified Demos/Delphi/VCL/SimpleMathExpr/simpexp.identcache
Binary file not shown.
12 changes: 6 additions & 6 deletions Demos/Delphi/VCL/SimpleMathExpr/simpexp.stat
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
[Stats]
EditorSecs=26770
DesignerSecs=222
InspectorSecs=307
CompileSecs=236154
OtherSecs=451
EditorSecs=37914
DesignerSecs=316
InspectorSecs=350
CompileSecs=360097
OtherSecs=855
StartTime=2019-01-11 16:31:53
RealKeys=0
EffectiveKeys=0
DebugSecs=21271
DebugSecs=28041
Loading

0 comments on commit d36ebe2

Please sign in to comment.