djjd47130

List View Switch

May 8th, 2016
693
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 9.33 KB | None | 0 0
  1. unit JD.Fmx.ListViewObjects;
  2.  
  3. interface
  4.  
  5. uses
  6.   System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
  7.   FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs,
  8.   FMX.ListView.Types, FMX.ListView.Appearances, FMX.ListView.Adapters.Base,
  9.   FMX.ListView;
  10.  
  11. type
  12.   TJDListViewControl = class;
  13.   TListItemSwitch = class;
  14.   TListItemProgressBar = class;
  15.  
  16.   TLISwitchThumbStyle = (tsRect, tsRoundRect, tsElipse);
  17.  
  18.   TJDListViewControlClass = class of TJDListViewControl;
  19.  
  20.   { TJDListViewControl }
  21.  
  22.   TJDListViewControl = class(TListItemSimpleControl)
  23.   private
  24.  
  25.   public
  26.     constructor Create(const AOwner: TListItem); override;
  27.     destructor Destroy; override;
  28.   end;
  29.  
  30.   { TListItemSwitch }
  31.  
  32.   TListItemSwitch = class(TJDListViewControl)
  33.   private
  34.     FIsChecked: Boolean;
  35.     FOnSwitch: TNotifyEvent;
  36.     FThumbStyle: TLISwitchThumbStyle;
  37.     FThumbWidth: Single;
  38.     FThumbHeight: Single;
  39.     FThumbRound: Single;
  40.     FSwitchRound: Single;
  41.     FOffColor: TAlphaColor;
  42.     FOnColor: TAlphaColor;
  43.     FThumbColor: TAlphaColor;
  44.     FOnText: String;
  45.     FOffText: String;
  46.     function GetOffText: String;
  47.     function GetOnText: String;
  48.     procedure SetOffText(const Value: String);
  49.     procedure SetOnText(const Value: String);
  50.   protected
  51.     function MouseDown(const Button: TMouseButton; const Shift: TShiftState; const MousePos: TPointF): Boolean;
  52.       override;
  53.     procedure DoSwitch; virtual;
  54.     procedure Render(const Canvas: TCanvas; const DrawItemIndex: Integer; const DrawStates: TListItemDrawStates;
  55.       const SubPassNo: Integer = 0); override;
  56.   public
  57.     constructor Create(const AOwner: TListItem); override;
  58.     destructor Destroy; override;
  59.   public
  60.     function GetIsChecked: Boolean;
  61.     function GetOffColor: TAlphaColor;
  62.     function GetOnColor: TAlphaColor;
  63.     function GetOnSwitch: TNotifyEvent;
  64.     function GetSwitchRound: Single;
  65.     function GetThumbColor: TAlphaColor;
  66.     function GetThumbHeight: Single;
  67.     function GetThumbRound: Single;
  68.     function GetThumbStyle: TLISwitchThumbStyle;
  69.     function GetThumbWidth: Single;
  70.  
  71.     procedure SetIsChecked(const AValue: Boolean);
  72.     procedure SetThumbStyle(const Value: TLISwitchThumbStyle);
  73.     procedure SetThumbWidth(const Value: Single);
  74.     procedure SetThumbHeight(const Value: Single);
  75.     procedure SetThumbRound(const Value: Single);
  76.     procedure SetSwitchRound(const Value: Single);
  77.     procedure SetOffColor(const Value: TAlphaColor);
  78.     procedure SetOnColor(const Value: TAlphaColor);
  79.     procedure SetThumbColor(const Value: TAlphaColor);
  80.     procedure SetOnSwitch(const Value: TNotifyEvent);
  81.  
  82.     property IsChecked: Boolean read GetIsChecked write SetIsChecked;
  83.     property OnColor: TAlphaColor read GetOnColor write SetOnColor;
  84.     property OffColor: TAlphaColor read GetOffColor write SetOffColor;
  85.     property OnText: String read GetOnText write SetOnText;
  86.     property OffText: String read GetOffText write SetOffText;
  87.     property SwitchRound: Single read GetSwitchRound write SetSwitchRound;
  88.     property ThumbColor: TAlphaColor read GetThumbColor write SetThumbColor;
  89.     property ThumbHeight: Single read GetThumbHeight write SetThumbHeight;
  90.     property ThumbStyle: TLISwitchThumbStyle read GetThumbStyle write SetThumbStyle;
  91.     property ThumbWidth: Single read GetThumbWidth write SetThumbWidth;
  92.     property ThumbRound: Single read GetThumbRound write SetThumbRound;
  93.     property OnSwitch: TNotifyEvent read GetOnSwitch write SetOnSwitch;
  94.   end;
  95.  
  96.   { TListItemProgressBar }
  97.  
  98.   TLIProgressBarStyle = (psNormal, psFill);
  99.  
  100.   TListItemProgressBar = class(TJDListViewControl)
  101.   private
  102.     FMin: Double;
  103.     FMax: Double;
  104.     FPosition: Double;
  105.   protected
  106.     function MouseDown(const Button: TMouseButton; const Shift: TShiftState; const MousePos: TPointF): Boolean;
  107.       override;
  108.     procedure Render(const Canvas: TCanvas; const DrawItemIndex: Integer; const DrawStates: TListItemDrawStates;
  109.       const SubPassNo: Integer = 0); override;
  110.   public
  111.     constructor Create(const AOwner: TListItem); override;
  112.     destructor Destroy; override;
  113.   public
  114.  
  115.   end;
  116.  
  117.  
  118. implementation
  119.  
  120. { TJDListViewControl }
  121.  
  122. constructor TJDListViewControl.Create(const AOwner: TListItem);
  123. begin
  124.   inherited;
  125.  
  126. end;
  127.  
  128. destructor TJDListViewControl.Destroy;
  129. begin
  130.  
  131.   inherited;
  132. end;
  133.  
  134. { TListItemSwitch }
  135.  
  136. constructor TListItemSwitch.Create(const AOwner: TListItem);
  137. begin
  138.   inherited;
  139.   Width:= 50;
  140.   Height:= 20;
  141.   FIsChecked:= False;
  142.   FOnColor:= TAlphaColorRec.Lime;
  143.   FOffColor:= TAlphaColorRec.SkyBlue;
  144.   FSwitchRound:= 5;
  145.   FThumbColor:= TAlphaColorRec.Black;
  146.   FThumbWidth:= 14;
  147.   FThumbHeight:= 14;
  148.   FThumbRound:= 5;
  149. end;
  150.  
  151. destructor TListItemSwitch.Destroy;
  152. begin
  153.  
  154.   inherited;
  155. end;
  156.  
  157. function TListItemSwitch.MouseDown(const Button: TMouseButton;
  158.   const Shift: TShiftState; const MousePos: TPointF): Boolean;
  159. begin
  160.   if (Button = TMouseButton.mbLeft) and Enabled then begin
  161.     Result := inherited;
  162.     if Result then begin
  163.       DoSwitch;
  164.     end;
  165.   end;
  166. end;
  167.  
  168. procedure TListItemSwitch.DoSwitch;
  169. begin
  170.   FIsChecked:= not FIsChecked;
  171.   if Assigned(OnSwitch) then
  172.     OnSwitch(Self);
  173.   Invalidate;
  174. end;
  175.  
  176. function TListItemSwitch.GetIsChecked: Boolean;
  177. begin
  178.   Result:= FIsChecked;
  179. end;
  180.  
  181. function TListItemSwitch.GetOffColor: TAlphaColor;
  182. begin
  183.   Result:= FOffColor;
  184. end;
  185.  
  186. function TListItemSwitch.GetOffText: String;
  187. begin
  188.   Result:= FOffText;
  189. end;
  190.  
  191. function TListItemSwitch.GetOnColor: TAlphaColor;
  192. begin
  193.   Result:= FOnColor;
  194. end;
  195.  
  196. function TListItemSwitch.GetOnSwitch: TNotifyEvent;
  197. begin
  198.   Result := FOnSwitch;
  199. end;
  200.  
  201. function TListItemSwitch.GetOnText: String;
  202. begin
  203.   Result:= FOnText;
  204. end;
  205.  
  206. function TListItemSwitch.GetSwitchRound: Single;
  207. begin
  208.   Result:= FSwitchRound;
  209. end;
  210.  
  211. function TListItemSwitch.GetThumbColor: TAlphaColor;
  212. begin
  213.   Result:= FThumbColor;
  214. end;
  215.  
  216. function TListItemSwitch.GetThumbHeight: Single;
  217. begin
  218.   Result:= FThumbHeight;
  219. end;
  220.  
  221. function TListItemSwitch.GetThumbRound: Single;
  222. begin
  223.   Result:= FThumbRound;
  224. end;
  225.  
  226. function TListItemSwitch.GetThumbStyle: TLISwitchThumbStyle;
  227. begin
  228.   Result:= FThumbStyle;
  229. end;
  230.  
  231. function TListItemSwitch.GetThumbWidth: Single;
  232. begin
  233.   Result:= FThumbWidth;
  234. end;
  235.  
  236. procedure TListItemSwitch.SetIsChecked(const AValue: Boolean);
  237. begin
  238.   FIsChecked:= AValue;
  239.   Invalidate;
  240. end;
  241.  
  242. procedure TListItemSwitch.SetOffColor(const Value: TAlphaColor);
  243. begin
  244.   FOffColor := Value;
  245.   Invalidate;
  246. end;
  247.  
  248. procedure TListItemSwitch.SetOffText(const Value: String);
  249. begin
  250.   FOffText:= Value;
  251.   Invalidate;
  252. end;
  253.  
  254. procedure TListItemSwitch.SetOnColor(const Value: TAlphaColor);
  255. begin
  256.   FOnColor := Value;
  257.   Invalidate;
  258. end;
  259.  
  260. procedure TListItemSwitch.SetOnSwitch(const Value: TNotifyEvent);
  261. begin
  262.   FOnSwitch:= Value;
  263.   Invalidate;
  264. end;
  265.  
  266. procedure TListItemSwitch.SetOnText(const Value: String);
  267. begin
  268.   FOnText:= Value;
  269.   Invalidate;
  270. end;
  271.  
  272. procedure TListItemSwitch.SetSwitchRound(const Value: Single);
  273. begin
  274.   FSwitchRound := Value;
  275.   Invalidate;
  276. end;
  277.  
  278. procedure TListItemSwitch.SetThumbWidth(const Value: Single);
  279. begin
  280.   FThumbWidth := Value;
  281.   Invalidate;
  282. end;
  283.  
  284. procedure TListItemSwitch.SetThumbColor(const Value: TAlphaColor);
  285. begin
  286.   FThumbColor := Value;
  287.   Invalidate;
  288. end;
  289.  
  290. procedure TListItemSwitch.SetThumbHeight(const Value: Single);
  291. begin
  292.   FThumbHeight := Value;
  293.   Invalidate;
  294. end;
  295.  
  296. procedure TListItemSwitch.SetThumbRound(const Value: Single);
  297. begin
  298.   FThumbRound := Value;
  299.   Invalidate;
  300. end;
  301.  
  302. procedure TListItemSwitch.SetThumbStyle(const Value: TLISwitchThumbStyle);
  303. begin
  304.   FThumbStyle := Value;
  305.   Invalidate;
  306. end;
  307.  
  308. procedure TListItemSwitch.Render(const Canvas: TCanvas;
  309.   const DrawItemIndex: Integer; const DrawStates: TListItemDrawStates;
  310.   const SubPassNo: Integer);
  311. var
  312.   R, R2: TRectF;
  313.   D: Single;
  314. begin
  315.   inherited;
  316.   R:= Self.LocalRect;
  317.   R2:= R;
  318.   Canvas.BeginScene;
  319.   try
  320.     Canvas.Stroke.Kind:= TBrushKind.None;
  321.     Canvas.Fill.Kind:= TBrushKind.Solid;
  322.     if IsChecked then begin
  323.       Canvas.Fill.Color:= FOnColor;
  324.     end else begin
  325.       Canvas.Fill.Color:= FOffColor;
  326.     end;
  327.     Canvas.FillRect(R, FSwitchRound, FSwitchRound,
  328.       [TCorner.TopLeft, TCorner.TopRight, TCorner.BottomLeft, TCorner.BottomRight],
  329.       1.0, TCornerType.Round);
  330.     R2.Top:= R.Top + (R.Height / 2) - (FThumbHeight / 2);
  331.     R2.Height:= FThumbHeight;
  332.     D:= R2.Top - R.Top;
  333.     if IsChecked then begin
  334.       R2.Left:= R.Right - FThumbWidth - D;
  335.     end else begin
  336.       R2.Left:= R.Left + D;
  337.     end;
  338.     R2.Width:= FThumbWidth;
  339.     Canvas.Fill.Color:= FThumbColor;
  340.     Canvas.FillRect(R2, FThumbRound, FThumbRound,
  341.       [TCorner.TopLeft, TCorner.TopRight, TCorner.BottomLeft, TCorner.BottomRight],
  342.       1.0, TCornerType.Round);
  343.   finally
  344.     Canvas.EndScene;
  345.   end;
  346. end;
  347.  
  348. { TListItemProgressBar }
  349.  
  350. constructor TListItemProgressBar.Create(const AOwner: TListItem);
  351. begin
  352.   inherited;
  353.  
  354. end;
  355.  
  356. destructor TListItemProgressBar.Destroy;
  357. begin
  358.  
  359.   inherited;
  360. end;
  361.  
  362. function TListItemProgressBar.MouseDown(const Button: TMouseButton;
  363.   const Shift: TShiftState; const MousePos: TPointF): Boolean;
  364. begin
  365.  
  366. end;
  367.  
  368. procedure TListItemProgressBar.Render(const Canvas: TCanvas;
  369.   const DrawItemIndex: Integer; const DrawStates: TListItemDrawStates;
  370.   const SubPassNo: Integer);
  371. begin
  372.   inherited;
  373.  
  374. end;
  375.  
  376. end.
Advertisement
Add Comment
Please, Sign In to add comment