UCTRLWallet.pas 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. unit UCTRLWallet;
  2. {$mode delphi}
  3. interface
  4. uses
  5. Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
  6. ExtCtrls, PairSplitter, Buttons, UVisualGrid, UCommon.UI, UDataSources, UNode;
  7. type
  8. { TCTRLWallet }
  9. TCTRLWalletAccountView = (wavAllAccounts, wavMyAccounts, wavFirstAccount);
  10. TCTRLWalletDuration = (wd30Days, wdFullHistory);
  11. TCTRLWallet = class(TApplicationForm)
  12. cbAccounts: TComboBox;
  13. gpMyAccounts: TGroupBox;
  14. gpRecentOps: TGroupBox;
  15. GroupBox1: TGroupBox;
  16. Label1: TLabel;
  17. Label2: TLabel;
  18. lblTotalPASA: TLabel;
  19. lblTotalPASC: TLabel;
  20. PairSplitter1: TPairSplitter;
  21. PairSplitterSide1: TPairSplitterSide;
  22. PairSplitterSide2: TPairSplitterSide;
  23. paAccounts: TPanel;
  24. paOperations: TPanel;
  25. procedure cbAccountsChange(Sender: TObject);
  26. procedure cmbDurationChange(Sender: TObject);
  27. procedure FormCreate(Sender: TObject);
  28. procedure FormResize(Sender: TObject);
  29. private
  30. FNodeNotifyEvents : TNodeNotifyEvents;
  31. FAccountsView : TCTRLWalletAccountView;
  32. FDuration : TCTRLWalletDuration;
  33. FAccountsGrid : TVisualGrid;
  34. FOperationsGrid : TVisualGrid;
  35. FAccountsDataSource : TUserAccountsDataSource;
  36. FOperationsDataSource : TAccountsOperationsDataSource;
  37. procedure SetAccountsView(view: TCTRLWalletAccountView);
  38. procedure SetDuration(const ADuration: TCTRLWalletDuration);
  39. protected
  40. procedure ActivateFirstTime; override;
  41. procedure OnNodeBlocksChanged(Sender: TObject);
  42. procedure OnNodeNewOperation(Sender: TObject);
  43. procedure OnAccountsUpdated(Sender: TObject);
  44. procedure OnAccountsSelected(Sender: TObject; constref ASelection: TVisualGridSelection);
  45. procedure OnOperationSelected(Sender: TObject; constref ASelection: TVisualGridSelection);
  46. procedure OnAccountsGridColumnInitialize(Sender: TObject; AColIndex:Integer; AColumn: TVisualColumn);
  47. procedure OnOperationsGridColumnInitialize(Sender: TObject; AColIndex:Integer; AColumn: TVisualColumn);
  48. public
  49. property Duration : TCTRLWalletDuration read FDuration write SetDuration;
  50. property AccountsView : TCTRLWalletAccountView read FAccountsView write SetAccountsView;
  51. end;
  52. implementation
  53. uses
  54. UUserInterface, UAccounts, UBlockChain, UCommon, UAutoScope, Generics.Collections;
  55. {$R *.lfm}
  56. { TCTRLWallet }
  57. procedure TCTRLWallet.FormCreate(Sender: TObject);
  58. var cmbDuration : TComboBox;
  59. begin
  60. // event registrations
  61. FNodeNotifyEvents := TNodeNotifyEvents.Create (self);
  62. FNodeNotifyEvents.OnBlocksChanged := OnNodeBlocksChanged;
  63. FNodeNotifyEvents.OnOperationsChanged := OnNodeNewOperation;
  64. // data sources
  65. FAccountsDataSource := TUserAccountsDataSource.Create(Self);
  66. FOperationsDataSource:= TAccountsOperationsDataSource.Create(Self);
  67. // grids
  68. FAccountsGrid := TVisualGrid.Create(Self);
  69. FAccountsGrid.SortMode := smMultiColumn;
  70. FAccountsGrid.FetchDataInThread:= true;
  71. FAccountsGrid.AutoPageSize:= true;
  72. FAccountsGrid.SelectionType:= stMultiRow;
  73. FAccountsGrid.Options := [vgoColAutoFill, vgoColSizing, vgoSortDirectionAllowNone];
  74. FAccountsGrid.DefaultColumnWidths := TArray<Integer>.Create(
  75. 100, // Account
  76. CT_VISUALGRID_STRETCH, // Name
  77. 100 // Balance
  78. );
  79. FAccountsGrid.OnColumnInitialize:= OnAccountsGridColumnInitialize;
  80. FAccountsGrid.OnSelection := OnAccountsSelected;
  81. FAccountsGrid.OnFinishedUpdating := OnAccountsUpdated;
  82. FOperationsGrid := TVisualGrid.Create(Self);
  83. FOperationsGrid.SortMode := smMultiColumn;
  84. FOperationsGrid.FetchDataInThread:= true;
  85. FOperationsGrid.AutoPageSize:= true;
  86. FOperationsGrid.SelectionType:= stRow;
  87. FOperationsGrid.Options := [vgoColAutoFill, vgoColSizing, vgoSortDirectionAllowNone];
  88. FOperationsGrid.DefaultColumnWidths := TArray<Integer>.Create(
  89. 130, // Time
  90. CT_VISUALGRID_DEFAULT, // Block
  91. 100, // Account
  92. 150, // Type
  93. 130, // Amount
  94. CT_VISUALGRID_DEFAULT, // Fee
  95. 100, // Balance
  96. CT_VISUALGRID_DEFAULT, // Payload
  97. 80, // OPHASH
  98. CT_VISUALGRID_STRETCH // Description (stretch)
  99. );
  100. FOperationsGrid.OnColumnInitialize:= OnOperationsGridColumnInitialize;
  101. FOperationsGrid.OnSelection := OnOperationSelected;
  102. FOperationsGrid.Caption.Alignment:= taCenter;
  103. FOperationsGrid.Caption.Text := 'All Account Operations';
  104. FOperationsGrid.Caption.Visible := true;
  105. cmbDuration := TComboBox.Create(FOperationsGrid);
  106. FOperationsGrid.WidgetControl := cmbDuration;
  107. cmbDuration.Items.BeginUpdate;
  108. try
  109. cmbDuration.AddItem('30 Days', TObject(wd30Days));
  110. cmbDuration.AddItem('Maximum', TObject(wdFullHistory));
  111. finally
  112. cmbDuration.Items.EndUpdate;
  113. cmbDuration.ItemIndex:=0;;
  114. end;
  115. cmbDuration.OnChange:=cmbDurationChange;
  116. AccountsView := wavMyAccounts;
  117. paOperations.AddControlDockCenter(FOperationsGrid);
  118. Duration := wd30Days;
  119. end;
  120. procedure TCTRLWallet.FormResize(Sender: TObject);
  121. begin
  122. // Left hand panel is 50% the size up until a max size of 450
  123. end;
  124. procedure TCTRLWallet.ActivateFirstTime;
  125. begin
  126. end;
  127. procedure TCTRLWallet.OnAccountsGridColumnInitialize(Sender: TObject; AColIndex:Integer; AColumn: TVisualColumn);
  128. begin
  129. case AColIndex of
  130. 2: AColumn.InternalColumn.Alignment := taRightJustify;
  131. end;
  132. end;
  133. procedure TCTRLWallet.OnOperationsGridColumnInitialize(Sender: TObject; AColIndex:Integer; AColumn: TVisualColumn);
  134. begin
  135. case AColIndex of
  136. 4, 5, 6: AColumn.InternalColumn.Alignment := taRightJustify;
  137. end;
  138. end;
  139. procedure TCTRLWallet.SetAccountsView(view: TCTRLWalletAccountView);
  140. begin
  141. paAccounts.RemoveAllControls(false);
  142. case view of
  143. wavAllAccounts: raise Exception.Create('Not implemented');
  144. wavMyAccounts: begin
  145. FOperationsGrid.DataSource := FOperationsDataSource;
  146. FAccountsGrid.DataSource := FAccountsDataSource;
  147. FAccountsGrid.Caption.Text := 'My Accounts';
  148. paAccounts.AddControlDockCenter(FAccountsGrid);
  149. FAccountsGrid.RefreshGrid;
  150. end;
  151. wavFirstAccount: raise Exception.Create('Not implemented');
  152. end;
  153. end;
  154. procedure TCTRLWallet.SetDuration(const ADuration: TCTRLWalletDuration);
  155. begin
  156. FDuration:= ADuration;
  157. case FDuration of
  158. wd30Days: FOperationsDataSource.TimeSpan := TTimeSpan.FromDays(30);
  159. wdFullHistory: FOperationsDataSource.TimeSpan := TTimeSpan.FromDays(10 * 365);
  160. end;
  161. FOperationsGrid.RefreshGrid;
  162. end;
  163. procedure TCTRLWallet.OnNodeBlocksChanged(Sender: TObject);
  164. begin
  165. FAccountsGrid.RefreshGrid;
  166. SetDuration(FDuration);
  167. end;
  168. procedure TCTRLWallet.OnNodeNewOperation(Sender: TObject);
  169. begin
  170. FAccountsGrid.RefreshGrid;
  171. SetDuration(FDuration);
  172. end;
  173. procedure TCTRLWallet.OnAccountsUpdated(Sender: TObject);
  174. begin
  175. lblTotalPASC.Caption := TAccountComp.FormatMoney( FAccountsDataSource.Overview.TotalPASC );
  176. lblTotalPASA.Caption := Format('%d', [FAccountsDataSource.Overview.TotalPASA] );
  177. end;
  178. procedure TCTRLWallet.OnAccountsSelected(Sender: TObject; constref ASelection: TVisualGridSelection);
  179. var
  180. row : longint;
  181. selectedAccounts : Generics.Collections.TList<Cardinal>;
  182. acc : Cardinal;
  183. GC : TScoped;
  184. begin
  185. selectedAccounts := GC.AddObject( TList<Cardinal>.Create ) as TList<Cardinal>;
  186. for row := ASelection.Row to (ASelection.Row + ASelection.RowCount - 1) do begin
  187. if (TAccountComp.AccountTxtNumberToAccountNumber( FAccountsGrid.Rows[row].Account, acc)) then
  188. selectedAccounts.Add(acc);
  189. end;
  190. FOperationsDataSource.Accounts := selectedAccounts.ToArray;
  191. FOperationsGrid.RefreshGrid;
  192. end;
  193. procedure TCTRLWallet.OnOperationSelected(Sender: TObject; constref ASelection: TVisualGridSelection);
  194. var
  195. row : longint;
  196. v : Variant;
  197. ophash : AnsiString;
  198. begin
  199. row := ASelection.Row;
  200. if (row >= 0) AND (row < FOperationsGrid.RowCount) then begin
  201. v := FOperationsGrid.Rows[row];
  202. ophash := FOperationsGrid.Rows[row].OPHASH;
  203. if TPCOperation.IsValidOperationHash(ophash) then
  204. TUserInterface.ShowOperationInfoDialog(self, ophash);
  205. end;
  206. end;
  207. procedure TCTRLWallet.cbAccountsChange(Sender: TObject);
  208. begin
  209. case cbAccounts.ItemIndex of
  210. 0: AccountsView := wavAllAccounts;
  211. 1: AccountsView := wavMyAccounts;
  212. 2: AccountsView := wavFirstAccount;
  213. end;
  214. end;
  215. procedure TCTRLWallet.cmbDurationChange(Sender: TObject);
  216. var
  217. cmbDuration : TComboBox;
  218. newDuration : TCTRLWalletDuration;
  219. begin
  220. cmbDuration := Sender as TComboBox;
  221. if not Assigned(cmbDuration) then
  222. exit;
  223. case cmbDuration.ItemIndex of
  224. 0: newDuration := wd30Days;
  225. 1: newDuration := wdFullHistory;
  226. end;
  227. if Duration <> newDuration then begin
  228. Duration := newDuration;
  229. FOperationsGrid.RefreshGrid;
  230. end;
  231. end;
  232. end.