ProjGatherSelectFrm.pas 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. unit ProjGatherSelectFrm;
  2. interface
  3. uses
  4. sdIDTree, sdDB,
  5. Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  6. Dialogs, sdGridDBA, sdGridTreeDBA, StdCtrls, ZJGrid;
  7. type
  8. TProjGatherSelectForm = class(TForm)
  9. lblProjectList: TLabel;
  10. lblResult: TLabel;
  11. zgSelectProject: TZJGrid;
  12. zgResult: TZJGrid;
  13. btnOk: TButton;
  14. btnCancel: TButton;
  15. stdSelectProject: TsdGridTreeDBA;
  16. procedure zgSelectProjectGetCellText(Sender: TObject;
  17. const ACoord: TPoint; var Value: String; DisplayText: Boolean);
  18. procedure zgSelectProjectSetCellText(Sender: TObject;
  19. const ACoord: TPoint; var Value: String; DisplayText: Boolean);
  20. procedure zgSelectProjectCellTextChanged(Sender: TObject; Col,
  21. Row: Integer);
  22. procedure btnOkClick(Sender: TObject);
  23. procedure zgSelectProjectDrawCellText(ACanvas: TCanvas;
  24. const ARect: TRect; const ACoord: TPoint; AGrid: TZJGrid;
  25. const Text: String; var ADefaultDraw: Boolean);
  26. private
  27. FProjectID: Integer;
  28. FValidProjs: TList;
  29. FSelectProjs: TList;
  30. function HasSelect(AProjectID: Integer): Boolean;
  31. procedure AddProjs(ANode: TsdIDTreeNode);
  32. procedure RemoveProjs(ANode: TsdIDTreeNode);
  33. procedure AssignSelectTenders;
  34. function GetTopParent: TsdIDTreeNode;
  35. procedure AddValidProject(ANode: TsdIDTreeNode);
  36. procedure FilterValidProject;
  37. function IsValidProj(AID: Integer): Boolean;
  38. procedure DoOnFilterRecord(ARecord: TsdDataRecord; var AAllow: Boolean);
  39. procedure LoadHistorySelects(AProjs: TList);
  40. public
  41. constructor Create(AProjectID: Integer; AProjs: TList);
  42. destructor Destroy; override;
  43. procedure AssignResult(AProjs: TList);
  44. end;
  45. function SelectGatherProject(AProjectID: Integer; AOrgProjs, ANewProjs: TList): Boolean;
  46. implementation
  47. uses
  48. Globals, GatherProjInfo, MainFrm;
  49. {$R *.dfm}
  50. function SelectGatherProject(AProjectID: Integer; AOrgProjs, ANewProjs: TList): Boolean;
  51. var
  52. vSelectFrm: TProjGatherSelectForm;
  53. begin
  54. vSelectFrm := TProjGatherSelectForm.Create(AProjectID, AOrgProjs);
  55. try
  56. Result := vSelectFrm.ShowModal = mrOk;
  57. if Result then
  58. vSelectFrm.AssignResult(ANewProjs);
  59. finally
  60. vSelectFrm.Free;
  61. end;
  62. end;
  63. { TProjGatherSelectForm }
  64. procedure TProjGatherSelectForm.AddProjs(ANode: TsdIDTreeNode);
  65. var
  66. iChild: Integer;
  67. begin
  68. if FSelectProjs.IndexOf(Pointer(ANode.ID)) = -1 then
  69. FSelectProjs.Add(Pointer(ANode.id));
  70. for iChild := 0 to ANode.ChildCount - 1 do
  71. AddProjs(ANode.ChildNodes[iChild]);
  72. end;
  73. procedure TProjGatherSelectForm.AddValidProject(ANode: TsdIDTreeNode);
  74. var
  75. iChild: Integer;
  76. begin
  77. FValidProjs.Add(ANode);
  78. for iChild := 0 to ANode.ChildCount - 1 do
  79. AddValidProject(ANode.ChildNodes[iChild]);
  80. end;
  81. constructor TProjGatherSelectForm.Create(AProjectID: Integer;
  82. AProjs: TList);
  83. begin
  84. inherited Create(nil);
  85. ClientHeight := 523;
  86. ClientWidth := 727;
  87. FProjectID := AProjectID;
  88. FValidProjs := TList.Create;
  89. FilterValidProject;
  90. ProjectManager.sdvProjectsSpare.OnFilterRecord := DoOnFilterRecord;
  91. ProjectManager.sdvProjectsSpare.Filtered := True;
  92. stdSelectProject.DataView := ProjectManager.sdvProjectsSpare;
  93. FSelectProjs := TList.Create;
  94. LoadHistorySelects(AProjs);
  95. AssignSelectTenders;
  96. end;
  97. destructor TProjGatherSelectForm.Destroy;
  98. begin
  99. ProjectManager.sdvProjectsSpare.Filtered := False;
  100. ProjectManager.sdvProjectsSpare.OnFilterRecord := nil;
  101. FValidProjs.Free;
  102. FSelectProjs.Free;
  103. inherited;
  104. end;
  105. procedure TProjGatherSelectForm.DoOnFilterRecord(ARecord: TsdDataRecord;
  106. var AAllow: Boolean);
  107. begin
  108. AAllow := Assigned(ARecord) and IsValidProj(ARecord.ValueByName('ID').AsInteger);
  109. end;
  110. procedure TProjGatherSelectForm.FilterValidProject;
  111. var
  112. vTopParent: TsdIDTreeNode;
  113. i: Integer;
  114. begin
  115. vTopParent := GetTopParent;
  116. AddValidProject(vTopParent);
  117. end;
  118. function TProjGatherSelectForm.GetTopParent: TsdIDTreeNode;
  119. begin
  120. Result := ProjectManager.ProjectsTree.FindNode(FProjectID);
  121. while Assigned(Result.Parent) do
  122. Result := Result.Parent;
  123. end;
  124. function TProjGatherSelectForm.HasSelect(AProjectID: Integer): Boolean;
  125. begin
  126. Result := FSelectProjs.IndexOf(Pointer(AProjectID)) <> -1;
  127. end;
  128. function TProjGatherSelectForm.IsValidProj(AID: Integer): Boolean;
  129. var
  130. i: Integer;
  131. vNode: TsdIDTreeNode;
  132. begin
  133. Result := False;
  134. for i := 0 to FValidProjs.Count - 1 do
  135. begin
  136. vNode := TsdIDTreeNode(FValidProjs.Items[i]);
  137. if vNode.ID = AID then
  138. begin
  139. Result := True;
  140. Break;
  141. end;
  142. end;
  143. end;
  144. procedure TProjGatherSelectForm.RemoveProjs(ANode: TsdIDTreeNode);
  145. var
  146. iChild: Integer;
  147. begin
  148. if FSelectProjs.IndexOf(Pointer(ANode.ID)) <> -1 then
  149. FSelectProjs.Remove(Pointer(ANode.id));
  150. for iChild := 0 to ANode.ChildCount - 1 do
  151. RemoveProjs(ANode.ChildNodes[iChild]);
  152. end;
  153. procedure TProjGatherSelectForm.zgSelectProjectGetCellText(Sender: TObject;
  154. const ACoord: TPoint; var Value: String; DisplayText: Boolean);
  155. var
  156. stnNode: TsdIDTreeNode;
  157. begin
  158. if ACoord.X = 1 then
  159. begin
  160. stnNode := stdSelectProject.IDTree.Items[ACoord.Y - 1];
  161. if Assigned(stnNode) and HasSelect(stnNode.ID) then
  162. Value := 'True';
  163. end;
  164. end;
  165. procedure TProjGatherSelectForm.zgSelectProjectSetCellText(Sender: TObject;
  166. const ACoord: TPoint; var Value: String; DisplayText: Boolean);
  167. var
  168. stnNode: TsdIDTreeNode;
  169. begin
  170. if ACoord.X = 1 then
  171. begin
  172. stnNode := stdSelectProject.IDTree.Items[ACoord.Y - 1];
  173. if Value = 'True' then
  174. AddProjs(stnNode)
  175. else
  176. RemoveProjs(stnNode);
  177. end;
  178. zgSelectProject.InvalidateCol(1);
  179. end;
  180. procedure TProjGatherSelectForm.zgSelectProjectCellTextChanged(
  181. Sender: TObject; Col, Row: Integer);
  182. begin
  183. if (Col = 1) then
  184. AssignSelectTenders;
  185. end;
  186. procedure TProjGatherSelectForm.AssignSelectTenders;
  187. procedure InitResultGrid;
  188. begin
  189. zgResult.ColCount := 2;
  190. zgResult.RowCount := 1;
  191. zgResult.Cells[1, 0].Text := 'ËùÑ¡ÏîÄ¿';
  192. zgResult.ColWidths[1] := 270;
  193. end;
  194. var
  195. stnNode: TsdIDTreeNode;
  196. i, iProjectID: Integer;
  197. begin
  198. InitResultGrid;
  199. for i := 0 to FSelectProjs.Count - 1 do
  200. begin
  201. iProjectID := Integer(FSelectProjs.Items[i]);
  202. stnNode := stdSelectProject.IDTree.FindNode(iProjectID);
  203. if stnNode.Rec.ValueByName('Type').AsInteger = 1 then
  204. begin
  205. zgResult.RowCount := zgResult.RowCount + 1;
  206. zgResult.Cells[1, zgResult.RowCount - 1].Text :=
  207. stnNode.Rec.ValueByName('Name').AsString;
  208. zgResult.Cells[1, zgResult.RowCount - 1].Align := gaCenterLeft;
  209. zgResult.Rows[zgResult.RowCount - 1].Data := stnNode;
  210. end;
  211. end;
  212. end;
  213. procedure TProjGatherSelectForm.LoadHistorySelects(AProjs: TList);
  214. var
  215. i: Integer;
  216. begin
  217. for i := 0 to AProjs.Count - 1 do
  218. FSelectProjs.Add(Pointer(TGatherProjInfo(AProjs.Items[i]).ProjectID));
  219. end;
  220. procedure TProjGatherSelectForm.btnOkClick(Sender: TObject);
  221. begin
  222. if zgResult.RowCount > 1 then
  223. ModalResult := mrOk;
  224. end;
  225. procedure TProjGatherSelectForm.AssignResult(AProjs: TList);
  226. var
  227. iRow: Integer;
  228. stnNode: TsdIDTreeNode;
  229. vGatherProj: TGatherProjInfo;
  230. begin
  231. for iRow := 1 to zgResult.RowCount - 1 do
  232. begin
  233. stnNode := TsdIDTreeNode(zgResult.Rows[iRow].Data);
  234. vGatherProj := TGatherProjInfo.Create(stnNode.Rec);
  235. AProjs.Add(vGatherProj);
  236. end;
  237. end;
  238. procedure TProjGatherSelectForm.zgSelectProjectDrawCellText(
  239. ACanvas: TCanvas; const ARect: TRect; const ACoord: TPoint;
  240. AGrid: TZJGrid; const Text: String; var ADefaultDraw: Boolean);
  241. procedure GetBitmap(AImage: TBitmap);
  242. begin
  243. with stdSelectProject.IDTree.Items[ACoord.Y - 1] do
  244. if Rec.ValueByName('Type').AsInteger = 0 then
  245. if Expanded and HasChildren then
  246. MainForm.Images.GetBitmap(34, AImage)
  247. else
  248. MainForm.Images.GetBitmap(34, AImage)
  249. else
  250. MainForm.Images.GetBitmap(11, AImage);
  251. end;
  252. const
  253. rIconWidth = 16;
  254. rIconHeight = 16;
  255. var
  256. Img: TBitmap;
  257. Cell: TZjCell;
  258. rImg: TRect;
  259. begin
  260. if (ACoord.X = 2) and (ACoord.Y > zgSelectProject.FixedRowCount - 1) then
  261. begin
  262. Cell := zgSelectProject.Cells[ACoord.X, ACoord.Y];
  263. Img := TBitmap.Create;
  264. try
  265. GetBitmap(Img);
  266. case Cell.Align of
  267. gaTopLeft, gaTopCenter, gaTopRight:
  268. rImg := Rect(ARect.Left + 2, ARect.Top, ARect.Left + rIconWidth, ARect.Top + rIconHeight);
  269. gaCenterLeft, gaCenterCenter, gaCenterRight:
  270. rImg := Rect(ARect.Left + 2, ARect.Top + (ARect.Bottom - ARect.Top - rIconHeight) div 2, ARect.Left + rIconWidth, ARect.Bottom - (ARect.Bottom - ARect.Top - rIconHeight) div 2);
  271. gaBottomLeft, gaBottomCenter, gaBottomRight:
  272. rImg := Rect(ARect.Left + 2, ARect.Bottom - rIconHeight, ARect.Left + rIconWidth, ARect.Bottom);
  273. end;
  274. ACanvas.StretchDraw(rImg, Img);
  275. WriteText(ACanvas, Rect(ARect.Left + rIconWidth, ARect.Top, ARect.Right, ARect.Bottom)
  276. , 2, 2, Text, Cell.Align, False);
  277. ADefaultDraw := False;
  278. finally
  279. Img.Free;
  280. end;
  281. end;
  282. end;
  283. end.