Browse Source

Wizards: minor refactoring
- improved datasource inheritance pattern
- separate data/presentation via datasource and cell renderers
- added cell renderers
- other minor edits

Herman Schoenfeld 7 years ago
parent
commit
8f3e861d6a

+ 1 - 0
src/core.utils/UDataSources.pas

@@ -93,6 +93,7 @@ begin
     TDataColumn.From('AccountNumber', true),
     TDataColumn.From('Account'),
     TDataColumn.From('Name'),
+    TDataColumn.From('Display'),
     TDataColumn.From('Balance'),
     TDataColumn.From('BalanceDecimal'),
     TDataColumn.From('Key'),

+ 3 - 3
src/gui/UCTRLWallet.pas

@@ -203,7 +203,7 @@ begin
     DisplayBinding := 'Amount';
     Width := 150;
     HeaderAlignment := taRightJustify;
-    Renderer := TCellRenderers.PASC;
+    Renderer := TCellRenderers.PASC_CheckPendingBalance;
     Filters := SORTABLE_NUMERIC_FILTER;
   end;
   with FOperationsGrid.AddColumn('Fee') do
@@ -214,7 +214,7 @@ begin
     AutoWidth := True;
     HeaderAlignment := taRightJustify;
     DataAlignment := taRightJustify;
-    Renderer := TCellRenderers.PASC;
+    Renderer := TCellRenderers.PASC_CheckPendingBalance;
     Filters := SORTABLE_NUMERIC_FILTER;
   end;
   with FOperationsGrid.AddColumn('Balance') do
@@ -225,7 +225,7 @@ begin
     Width := 100;
     HeaderAlignment := taRightJustify;
     DataAlignment := taRightJustify;
-    Renderer := TCellRenderers.PASC;
+    Renderer := TCellRenderers.PASC_CheckPendingBalance;
     Filters := SORTABLE_NUMERIC_FILTER;
   end;
   with FOperationsGrid.AddColumn('Payload') do

+ 40 - 1
src/gui/UCellRenderers.pas

@@ -25,6 +25,8 @@ type
 
     // Cell Renderers
     class procedure OperationTime(Sender: TObject; ACol, ARow: Longint; Canvas: TCanvas; Rect: TRect; State: TGridDrawState; const CellData, RowData: Variant; var Handled: boolean);
+    class procedure PASC_CheckAllBalance(Sender: TObject; ACol, ARow: Longint; Canvas: TCanvas; Rect: TRect; State: TGridDrawState; const CellData, RowData: Variant; var Handled: boolean);
+    class procedure PASC_CheckPendingBalance (Sender: TObject; ACol, ARow: Longint; Canvas: TCanvas; Rect: TRect; State: TGridDrawState; const CellData, RowData: Variant; var Handled: boolean);
     class procedure PASC(Sender: TObject; ACol, ARow: Longint; Canvas: TCanvas; Rect: TRect; State: TGridDrawState; const CellData, RowData: Variant; var Handled: boolean);
     class procedure Payload(Sender: TObject; ACol, ARow: Longint; Canvas: TCanvas; Rect: TRect; State: TGridDrawState; const CellData, RowData: Variant; var Handled: boolean);
     class procedure OPHASH(Sender: TObject; ACol, ARow: Longint; Canvas: TCanvas; Rect: TRect; State: TGridDrawState; const CellData, RowData: Variant; var Handled: boolean);
@@ -86,18 +88,55 @@ begin
   Handled := true;
 end;
 
-class procedure TCellRenderers.PASC (Sender: TObject; ACol, ARow: Longint; Canvas: TCanvas; Rect: TRect; State: TGridDrawState; const CellData, RowData: Variant; var Handled: boolean);
+class procedure TCellRenderers.PASC_CheckAllBalance (Sender: TObject; ACol, ARow: Longint; Canvas: TCanvas; Rect: TRect; State: TGridDrawState; const CellData, RowData: Variant; var Handled: boolean);
 var
   LValue : Int64;
   LTextStyle: TTextStyle;
   LRowData : TDataRowData;
   LStr : AnsiString;
+  LAllBalance : boolean;
+begin
+  LRowData := TDataRowData(RowData);
+  if LRowData.HasData('AllBalance') AND TVariantTool.TryParseBool(LRowData['AllBalance'], LAllBalance) AND LAllBalance then begin
+    Canvas.Font.Style := [fsBold];
+    Canvas.TextRect(Rect, Rect.Left, Rect.Top, 'ALL BALANCE', Canvas.TextStyle);
+    Handled := true;
+    exit;
+  end else PASC(Sender, ACol, ARow, Canvas, Rect, State, CellData, RowData, Handled);
+end;
+
+class procedure TCellRenderers.PASC_CheckPendingBalance (Sender: TObject; ACol, ARow: Longint; Canvas: TCanvas; Rect: TRect; State: TGridDrawState; const CellData, RowData: Variant; var Handled: boolean);
+var
+  LValue : Int64;
+  LTextStyle: TTextStyle;
+  LRowData : TDataRowData;
+  LStr : AnsiString;
+  LAllBalance : boolean;
 begin
   if NOT TVariantTool.IsNumeric(CellData) then
     exit;
   LValue := CellData;
   LRowData := TDataRowData(RowData);
+  if LRowData.HasData('UnixTime')  AND (LRowData['UnixTime'] = 0) then begin
+    Canvas.Font.Color := CT_PASCBALANCE_0CONF_COL;
+    LStr := '(' + TAccountComp.FormatMoney(LValue) + ')';
+    Canvas.TextRect(Rect, Rect.Left, Rect.Top, LStr, Canvas.TextStyle);
+    Handled := true;
+    exit;
+  end else PASC(Sender, ACol, ARow, Canvas, Rect, State, CellData, RowData, Handled);
+end;
 
+class procedure TCellRenderers.PASC (Sender: TObject; ACol, ARow: Longint; Canvas: TCanvas; Rect: TRect; State: TGridDrawState; const CellData, RowData: Variant; var Handled: boolean);
+var
+  LValue : Int64;
+  LTextStyle: TTextStyle;
+  LRowData : TDataRowData;
+  LStr : AnsiString;
+begin
+  if NOT TVariantTool.IsNumeric(CellData) then
+    exit;
+  LValue := CellData;
+  LRowData := TDataRowData(RowData);
   if LRowData.HasData('UnixTime')  AND (LRowData['UnixTime'] = 0) then begin
     Canvas.Font.Color := CT_PASCBALANCE_0CONF_COL;
     LStr := '('+TAccountComp.FormatMoney(LValue)+')';

+ 40 - 45
src/gui/wizards/operations/UWIZChangeKey_Confirmation.pas

@@ -75,31 +75,39 @@ begin
 
   with FChangeKeyGrid.AddColumn('Account') do
   begin
+    StretchedToFill := true;
     Binding := 'Account';
+    SortBinding := 'AccountNumber';
+    DisplayBinding := 'Display';
     Filters := SORTABLE_NUMERIC_FILTER;
-    Width := 100;
     HeaderFontStyles := [fsBold];
     DataFontStyles := [fsBold];
   end;
 
   with FChangeKeyGrid.AddColumn('Current Key') do
   begin
-    Binding := 'CurrentKey';
+    Binding := 'Key';
     Filters := SORTABLE_TEXT_FILTER;
-    Width := 100;
+    Width := 130;
   end;
 
   with FChangeKeyGrid.AddColumn('New Key') do
   begin
     Binding := 'NewKey';
     Filters := SORTABLE_TEXT_FILTER;
-    Width := 100;
+    Width := 130;
   end;
 
   with FChangeKeyGrid.AddColumn('Fee') do
   begin
+    Binding := 'FeeDecimal';
+    SortBinding := 'Fee';
+    DisplayBinding := 'Fee';
     Filters := SORTABLE_TEXT_FILTER;
-    Width := 100;
+    Width := 50;
+    Renderer := TCellRenderers.PASC;
+    HeaderAlignment := taRightJustify;
+    DataAlignment := taRightJustify;
   end;
 
   Data := TAccountChangeKeyDataSource.Create(FChangeKeyGrid);
@@ -123,49 +131,38 @@ end;
 
 function TAccountChangeKeyDataSource.GetColumns: TDataColumns;
 begin
-  Result := TDataColumns.Create(
-    TDataColumn.From('Account'),
-    TDataColumn.From('CurrentKey'),
-    TDataColumn.From('NewKey'),
-    TDataColumn.From('Fee')
-    );
+  Result := TArrayTool<TDataColumn>.Concat([
+    Inherited,
+    // Additional Columns
+    TDataColumns.Create(
+      TDataColumn.From('NewKey'),
+      TDataColumn.From('Fee'),
+      TDataColumn.From('FeeDecimal')
+    )
+  ]);
 end;
 
 function TAccountChangeKeyDataSource.GetItemField(constref AItem: TAccount; const ABindingName: ansistring): variant;
-var
-  index: integer;
 begin
-  if ABindingName = 'Account' then
-    Result := TAccountComp.AccountNumberToAccountTxtNumber(AItem.account)
-  else if ABindingName = 'Fee' then
-    Result := TAccountComp.FormatMoney(Model.Fee.SingleOperationFee)
-  else
+  if ABindingName = 'Fee' then
+    Result := -Model.Fee.SingleOperationFee
+  else if ABindingName = 'FeeDecimal' then
+    Result := TAccountComp.FormatMoneyDecimal(-Model.Fee.SingleOperationFee)
+  else if ABindingName = 'NewKey' then
     case Model.ChangeKey.ChangeKeyMode of
-      akaTransferAccountOwnership:
-        if ABindingName = 'CurrentKey' then
-          Result := TAccountComp.AccountPublicKeyExport(AItem.accountInfo.accountKey)
-        else if ABindingName = 'NewKey' then
-          Result := TAccountComp.AccountPublicKeyExport(Model.TransferAccount.AccountKey)
-        else
-          raise Exception.Create(Format('Field not found [%s]', [ABindingName]));
-
-      akaChangeAccountPrivateKey:
-        if ABindingName = 'CurrentKey' then
-          { TODO : Check how to get the wallet name an account is in }
-          Result := '??? unknown'
-        else if ABindingName = 'NewKey' then
-        begin
-          Result := IIF(Model.ChangeAccountPrivateKey.NewWalletKey.Name = '',
-            TCrypto.ToHexaString(TAccountComp.AccountKey2RawString(
-            Model.ChangeAccountPrivateKey.NewWalletKey.AccountKey)), Model.ChangeAccountPrivateKey.NewWalletKey.Name);
-          if not Assigned(Model.ChangeAccountPrivateKey.NewWalletKey.PrivateKey) then
-            Result := Result + '(*)';
-        end
-        else
-          raise Exception.Create(Format('Field not found [%s]', [ABindingName]));
-
-    end;
-
+      akaTransferAccountOwnership: Result := TAccountComp.AccountPublicKeyExport(Model.TransferAccount.AccountKey);
+      akaChangeAccountPrivateKey: begin
+        Result := IIF(
+          Model.ChangeAccountPrivateKey.NewWalletKey.Name = '',
+          TCrypto.ToHexaString(TAccountComp.AccountKey2RawString(Model.ChangeAccountPrivateKey.NewWalletKey.AccountKey)),
+          Model.ChangeAccountPrivateKey.NewWalletKey.Name
+        );
+        if not Assigned(Model.ChangeAccountPrivateKey.NewWalletKey.PrivateKey) then
+          Result := Result + '(*)';
+      end
+      else raise ENotSupportedException.Create('ChangeKeyMode');
+    end
+  else Result := Inherited GetItemField(AItem, ABindingName);
 end;
 
 
@@ -175,8 +172,6 @@ var
 begin
   for i := Low(Model.Account.SelectedAccounts) to High(Model.Account.SelectedAccounts) do
     AContainer.Add(Model.Account.SelectedAccounts[i]);
-
 end;
 
-
 end.

+ 35 - 16
src/gui/wizards/operations/UWIZEnlistAccountForSale_Confirmation.pas

@@ -69,22 +69,35 @@ begin
   FEnlistAccountsGrid.SelectionType := stNone;
   FEnlistAccountsGrid.Options := [vgoColAutoFill, vgoColSizing, vgoSortDirectionAllowNone, vgoAutoHidePaging];
   with FEnlistAccountsGrid.AddColumn('Account') do begin
-    Binding := 'Account';
+    Binding := 'AccountNumber';
+    SortBinding := 'AccountNumber';
+    DisplayBinding := 'Display';
     Filters := SORTABLE_NUMERIC_FILTER;
-    Width := 100;
+    StretchedToFill := true;
     HeaderFontStyles := [fsBold];
     DataFontStyles := [fsBold];
   end;
+
    with FEnlistAccountsGrid.AddColumn('Sale Price') do begin
-    Binding := 'SalePrice';
+    Binding := 'SalePriceDecimal';
+    SortBinding := 'SalePrice';
+    DisplayBinding := 'SalePrice';
     Filters := SORTABLE_NUMERIC_FILTER;
     Width := 100;
     Renderer := TCellRenderers.PASC;
+    HeaderAlignment := taRightJustify;
+    DataAlignment := taRightJustify;
   end;
 
   with FEnlistAccountsGrid.AddColumn('Fee') do begin
-    Filters := SORTABLE_TEXT_FILTER;
-    Width := 100;
+    Binding := 'FeeDecimal';
+    SortBinding := 'Fee';
+    DisplayBinding := 'Fee';
+    Filters := SORTABLE_NUMERIC_FILTER;
+    Width := 50;
+    Renderer := TCellRenderers.PASC;
+    HeaderAlignment := taRightJustify;
+    DataAlignment := taRightJustify;
   end;
 
    data := TAccountSenderDataSource.Create(FEnlistAccountsGrid);
@@ -111,10 +124,15 @@ end;
 
 function TAccountSenderDataSource.GetColumns : TDataColumns;
 begin
-  Result := TDataColumns.Create(
-    TDataColumn.From('Account'),
-    TDataColumn.From('SalePrice'),
-    TDataColumn.From('Fee')
+  Result := TArrayTool<TDataColumn>.Concat([
+    Inherited,
+    // Additional columns
+    TDataColumns.Create(
+      TDataColumn.From('SalePrice'),
+      TDataColumn.From('SalePriceDecimal'),
+      TDataColumn.From('Fee'),
+      TDataColumn.From('FeeDecimal')
+    )]
   );
 end;
 
@@ -125,13 +143,16 @@ begin
    if ABindingName = 'Account' then
      Result := TAccountComp.AccountNumberToAccountTxtNumber(AItem.account)
    else if ABindingName = 'SalePrice' then
-     Result := TAccountComp.FormatMoney(Model.EnlistAccountForSale.SalePrice)
-     else if ABindingName = 'Fee' then
-     Result := TAccountComp.FormatMoney(Model.Fee.SingleOperationFee)
-   else raise Exception.Create(Format('Field not found [%s]', [ABindingName]));
+     Result := Model.EnlistAccountForSale.SalePrice
+   else if ABindingName = 'SalePriceDecimal' then
+     Result := TAccountComp.FormatMoneyDecimal(Model.EnlistAccountForSale.SalePrice)
+   else if ABindingName = 'Fee' then
+     Result := -Model.Fee.SingleOperationFee
+   else if ABindingName = 'FeeDecimal' then
+     Result := TAccountComp.FormatMoneyDecimal(-Model.Fee.SingleOperationFee)
+   else Result := Inherited GetItemField(AItem, ABindingName);
 end;
 
-
 procedure TAccountSenderDataSource.FetchAll(const AContainer : TList<TAccount>);
 var
   i: Integer;
@@ -142,6 +163,4 @@ begin
   end;
 end;
 
-
 end.
-

+ 1 - 1
src/gui/wizards/operations/UWIZSendPASC_ConfirmSender.pas

@@ -72,7 +72,7 @@ begin
     StretchedToFill := True;
     Binding := 'AccountNumber';
     SortBinding := 'AccountNumber';
-    DisplayBinding := 'Account';
+    DisplayBinding := 'Display';
     Width := 100;
     HeaderFontStyles := [fsBold];
     DataFontStyles := [fsBold];

+ 39 - 21
src/gui/wizards/operations/UWIZSendPASC_Confirmation.pas

@@ -76,9 +76,11 @@ begin
   FSendersGrid.Options := [vgoColAutoFill, vgoColSizing, vgoSortDirectionAllowNone, vgoAutoHidePaging];
   with FSendersGrid.AddColumn('Sender') do
   begin
-    Binding := 'SenderAccount';
+    StretchedToFill:=true;
+    Binding := 'Account';
+    SortBinding := 'AccountNumber';
+    DisplayBinding := 'Display';
     Filters := SORTABLE_NUMERIC_FILTER;
-    Width := 100;
     HeaderFontStyles := [fsBold];
     DataFontStyles := [fsBold];
   end;
@@ -87,17 +89,27 @@ begin
     Filters := SORTABLE_NUMERIC_FILTER;
     Width := 100;
     Renderer := TCellRenderers.PASC;
+    HeaderAlignment := taRightJustify;
+    DataAlignment := taRightJustify;
   end;
-  with FSendersGrid.AddColumn('Amount To Send') do
+  with FSendersGrid.AddColumn('Amount') do
   begin
-    Binding := 'AmountToSend';
+    Binding := 'AmountDecimal';
+    SortBinding := 'Amount';
+    DisplayBinding := 'Amount';
     Filters := SORTABLE_TEXT_FILTER;
     Width := 100;
+    Renderer := TCellRenderers.PASC_CheckAllBalance;
+    HeaderAlignment := taRightJustify;
+    DataAlignment := taRightJustify;
   end;
   with FSendersGrid.AddColumn('Fee') do
   begin
-    Filters := SORTABLE_TEXT_FILTER;
-    Width := 100;
+    Filters := SORTABLE_NUMERIC_FILTER;
+    Width := 50;
+    Renderer := TCellRenderers.PASC;
+    HeaderAlignment := taRightJustify;
+    DataAlignment := taRightJustify;
   end;
 
   Data := TAccountSenderDataSource.Create(FSendersGrid);
@@ -124,28 +136,34 @@ end;
 
 function TAccountSenderDataSource.GetColumns: TDataColumns;
 begin
-  Result := TDataColumns.Create(
-    TDataColumn.From('SenderAccount'),
-    TDataColumn.From('Balance'),
-    TDataColumn.From('AmountToSend'),
-    TDataColumn.From('Fee')
-    );
+  Result := TArrayTool<TDataColumn>.Concat([
+    Inherited,
+    // Additional columns
+    TDataColumns.Create(
+      TDataColumn.From('AllBalance'),
+      TDataColumn.From('Amount'),
+      TDataColumn.From('AmountDecimal'),
+      TDataColumn.From('Fee'),
+      TDataColumn.From('FeeDecimal')
+    )
+  ]);
 end;
 
 function TAccountSenderDataSource.GetItemField(constref AItem: TAccount; const ABindingName: ansistring): variant;
 var
   index: integer;
 begin
-  if ABindingName = 'SenderAccount' then
-    Result := AItem.DisplayString
-  else if ABindingName = 'Balance' then
-    Result := TAccountComp.FormatMoney(AItem.Balance)
-  else if ABindingName = 'AmountToSend' then
-    Result := IIF(Model.SendPASC.SendPASCMode = akaAllBalance, 'ALL BALANCE', TAccountComp.FormatMoney(Model.SendPASC.SingleAmountToSend))
+  if ABindingName = 'AllBalance' then
+    Result := IIF(Model.SendPASC.SendPASCMode = akaAllBalance, Variant(True), Variant(False))
+  else if ABindingName = 'Amount' then
+    Result := Variant(-Model.SendPASC.SingleAmountToSend)
+  else if ABindingName = 'AmountDecimal' then
+    Result := TAccountComp.FormatMoneyDecimal(-Model.SendPASC.SingleAmountToSend)
   else if ABindingName = 'Fee' then
-    Result := TAccountComp.FormatMoney(Model.Fee.SingleOperationFee)
-  else
-    raise Exception.Create(Format('Field not found [%s]', [ABindingName]));
+    Result := -Model.Fee.SingleOperationFee
+  else if ABindingName = 'FeeDecimal' then
+    Result := TAccountComp.FormatMoneyDecimal(-Model.Fee.SingleOperationFee)
+  else Result := Inherited GetItemField(AItem, ABindingName);
 end;
 
 

+ 1 - 1
src/libraries/sphere10/UCommon.Data.pas

@@ -556,7 +556,7 @@ begin
        SetLength(ADataTable.Rows, pageEnd - pageStart + 1);
        for i := pageStart to pageEnd do begin
          ADataTable.Rows[j] := TDataRow.New(FClassID, ADataTable.Columns);
-         DehydrateItem( data[i], ADataTable.Rows[j]);
+         DehydrateItem(data[i], ADataTable.Rows[j]);
          inc(j);
        end;
      end;