CISWizardForm.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  1. using FastReport.Barcode;
  2. using FastReport.Controls;
  3. using FastReport.Design.PageDesigners.Page;
  4. using FastReport.Utils;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.ComponentModel;
  8. using System.Data;
  9. using System.Drawing;
  10. using System.Drawing.Printing;
  11. using System.IO;
  12. using System.Linq;
  13. using System.Text;
  14. using System.Windows.Forms;
  15. namespace FastReport.Forms
  16. {
  17. internal partial class CISWizardForm : BaseDialogForm
  18. {
  19. const string MARKIROVKA_URL = "http://check.markirovka.nalog.ru/kc/?kiz=";
  20. private Report report;
  21. private PrinterSettings printerSettings;
  22. private Report sampleReport;
  23. Border border;
  24. bool update;
  25. private void CbxType_SelectedIndexChanged(object sender, EventArgs e)
  26. {
  27. cbxMethodСirculation.Visible = false;
  28. cbxSize.Visible = false;
  29. cbLeftMargin.Visible = false;
  30. tbCountry.Visible = false;
  31. tbID.Visible = false;
  32. lbMethodСirculation.Visible = false;
  33. lbCountry.Visible = false;
  34. lbID.Visible = false;
  35. lbSize.Visible = false;
  36. lbData.Visible = false;
  37. tbData.Visible = false;
  38. cbxTypeBarcodes.Visible = false;
  39. lbTypeBarcodes.Visible = false;
  40. switch (cbxType.SelectedIndex)
  41. {
  42. case 2:
  43. lbData.Visible = true;
  44. tbData.Visible = true;
  45. cbxTypeBarcodes.Visible = true;
  46. lbTypeBarcodes.Visible = true;
  47. cbxTypeBarcodes.Items.Clear();
  48. cbxTypeBarcodes.Items.AddRange(new object[]
  49. {
  50. "GS1-128 (UCC/EAN-128)",
  51. "Datamatrix"
  52. });
  53. cbxTypeBarcodes.SelectedIndex = 0;
  54. break;
  55. case 5:
  56. lbData.Visible = true;
  57. tbData.Visible = true;
  58. cbxTypeBarcodes.Visible = true;
  59. lbTypeBarcodes.Visible = true;
  60. cbxTypeBarcodes.Items.Clear();
  61. cbxTypeBarcodes.Items.AddRange(new object[]
  62. {
  63. "Datamatrix",
  64. "QR Code"
  65. });
  66. cbxTypeBarcodes.SelectedIndex = 0;
  67. break;
  68. case 6:
  69. cbxMethodСirculation.Visible = true;
  70. cbxSize.Visible = true;
  71. cbLeftMargin.Visible = true;
  72. tbCountry.Visible = true;
  73. tbID.Visible = true;
  74. lbMethodСirculation.Visible = true;
  75. lbCountry.Visible = true;
  76. lbID.Visible = true;
  77. lbSize.Visible = true;
  78. break;
  79. case 10:
  80. lbData.Visible = true;
  81. tbData.Visible = true;
  82. cbxTypeBarcodes.Visible = true;
  83. lbTypeBarcodes.Visible = true;
  84. cbxTypeBarcodes.Items.Clear();
  85. cbxTypeBarcodes.Items.AddRange(new object[]
  86. {
  87. "Code39",
  88. "GS1-128 (UCC/EAN-128)",
  89. "PDF417",
  90. "MaxiCode",
  91. "QR Code"
  92. });
  93. cbxTypeBarcodes.SelectedIndex = 0;
  94. break;
  95. default:
  96. lbData.Visible = true;
  97. tbData.Visible = true;
  98. break;
  99. }
  100. UpdateSample();
  101. }
  102. private void Tb_ButtonClick(object sender, EventArgs e)
  103. {
  104. using (TextEditorForm form = new TextEditorForm(report))
  105. {
  106. form.ExpressionText = (sender as TextBoxButton).Text;
  107. if (form.ShowDialog() == DialogResult.OK)
  108. {
  109. (sender as TextBoxButton).Text = form.ExpressionText;
  110. UpdateSample();
  111. }
  112. }
  113. }
  114. private void BtnBorder_Click(object sender, EventArgs e)
  115. {
  116. using (BorderEditorForm form = new BorderEditorForm())
  117. {
  118. form.Border = border;
  119. if (form.ShowDialog() == DialogResult.OK)
  120. {
  121. border = form.Border;
  122. }
  123. }
  124. }
  125. private void TbCountry_TextChanged(object sender, EventArgs e)
  126. {
  127. tbCountry.Text.ToUpper();
  128. }
  129. private void CISWizardForm_FormClosed(object sender, FormClosedEventArgs e)
  130. {
  131. if (DialogResult == DialogResult.OK)
  132. Done();
  133. }
  134. /// <inheritdoc/>
  135. public override void UpdateDpiDependencies()
  136. {
  137. base.UpdateDpiDependencies();
  138. tbID.Image = GetImage(68);
  139. tbData.Image = GetImage(68);
  140. }
  141. private void Done()
  142. {
  143. SetUpPapper(CreateReport());
  144. // tell the designer to reflect changes
  145. report.Designer.SetModified(null, "ChangeReport");
  146. }
  147. private DataBand CreateReport()
  148. {
  149. DataBand band = new DataBand();
  150. if (cbxType.SelectedIndex == 6)
  151. {
  152. Report report = DialogResult == DialogResult.OK ? this.report : sampleReport;
  153. report.Load(ResourceLoader.GetStream("CIS.frx"));
  154. band = (report.FindObject("Data1") as DataBand);
  155. BarcodeObject code128 = (BarcodeObject)report.FindObject("Code128");
  156. code128.Text = tbID.Text == "" ? code128.Barcode.GetDefaultValue() : tbID.Text;
  157. (code128.Barcode as Barcode128).Initialize(code128.Text, false, 0, 1);
  158. SizeF code128NewBounds = (code128.Barcode as Barcode128).CalcBounds();
  159. code128.Zoom = code128.Bounds.Width / code128NewBounds.Width;
  160. TextObject code128Text = (TextObject)report.FindObject("Code128Text");
  161. code128Text.Text = "№: " + code128.Text;
  162. BarcodeObject qr = (BarcodeObject)report.FindObject("QR");
  163. qr.Text = MARKIROVKA_URL + code128.Text;
  164. (qr.Barcode as BarcodeQR).Initialize(qr.Text, false, 0, 1);
  165. SizeF QRNewBounds = (qr.Barcode as BarcodeQR).CalcBounds();
  166. qr.Zoom = qr.Bounds.Height / QRNewBounds.Height;
  167. TextObject country = (TextObject)report.FindObject("Country");
  168. country.Text = tbCountry.Text;
  169. if (cbxMethodСirculation.SelectedIndex == 1)
  170. {
  171. (report.FindObject("Arrow") as ReportComponentBase).Visible = false;
  172. (report.FindObject("Shape1") as ReportComponentBase).FillColor = Color.ForestGreen;
  173. }
  174. else
  175. {
  176. (report.FindObject("Arrow") as ReportComponentBase).Visible = true;
  177. (report.FindObject("Shape1") as ReportComponentBase).Visible = true;
  178. }
  179. if (cbxSize.SelectedIndex == 0)
  180. {
  181. if (cbLeftMargin.Checked)
  182. {
  183. foreach (Base item in band.AllObjects)
  184. {
  185. if (item is ReportComponentBase)
  186. {
  187. (item as ReportComponentBase).Left += Units.Millimeters * 10;
  188. }
  189. }
  190. }
  191. }
  192. else
  193. {
  194. //(report.Pages[0] as ReportPage).PaperHeight = 25;
  195. (report.FindObject("Data1") as BandBase).Height = 2.5f * 37.8f;
  196. code128Text.Bounds = new RectangleF(311.85f, 0, 283.5f, 35.91f);
  197. code128.Bounds = new RectangleF(311.85f, 37.8f, 283.37f, 45.36f);
  198. (report.FindObject("Shape1") as ReportComponentBase).Height -= 0.3f * 38;
  199. if (cbLeftMargin.Checked)
  200. {
  201. foreach (Base item in band.AllObjects)
  202. {
  203. if (item is ReportComponentBase)
  204. {
  205. (item as ReportComponentBase).Left += Units.Millimeters * 10;
  206. }
  207. }
  208. }
  209. }
  210. }
  211. else
  212. {
  213. BarcodeObject barcode = new BarcodeObject();
  214. string barcodeType = "Datamatrix";
  215. if (cbxTypeBarcodes != null && (cbxType.SelectedIndex == 2 || cbxType.SelectedIndex == 5 || cbxType.SelectedIndex == 10))
  216. {
  217. barcodeType = cbxTypeBarcodes.SelectedItem.ToString();
  218. }
  219. barcode.Barcode = (BarcodeBase)Activator.CreateInstance(Barcodes.GetType(barcodeType));
  220. if (tbData.Text != null && tbData.Text != "")
  221. barcode.Text = tbData.Text;
  222. else
  223. barcode.Text = barcode.Barcode.GetDefaultValue();
  224. if (barcode.Barcode is LinearBarcodeBase)
  225. {
  226. barcode.Height = Units.Millimeters * 25;
  227. barcode.Padding = new Padding((int)(Units.Millimeters * 6.4), 0, (int)(Units.Millimeters * 6.4), 0);
  228. }
  229. else
  230. {
  231. barcode.Padding = new Padding((int)(Units.Millimeters * 2));
  232. }
  233. barcode.Parent = band;
  234. barcode.UpdateAutoSize();
  235. barcode.Name = barcodeType;
  236. }
  237. return band;
  238. }
  239. private void OnChangeControl(object sender, EventArgs e)
  240. {
  241. UpdateSample();
  242. }
  243. private float ConvertUnitsToMillimeters(string value)
  244. {
  245. float floatValue = Converter.StringToFloat(value, true);
  246. switch (ReportWorkspace.Grid.GridUnits)
  247. {
  248. case PageUnits.Centimeters:
  249. floatValue *= 10f;
  250. break;
  251. case PageUnits.HundrethsOfInch:
  252. floatValue *= 0.254f;
  253. break;
  254. case PageUnits.Inches:
  255. floatValue *= 25.4f;
  256. break;
  257. case PageUnits.Millimeters:
  258. // do nothing
  259. break;
  260. }
  261. return floatValue;
  262. }
  263. private void SetUpPapper(DataBand band)
  264. {
  265. ReportPage page = report.Pages[0] as ReportPage;
  266. // setup paper
  267. if(cbxType.SelectedIndex != 6) // avoid clear page for fur
  268. page.Clear();
  269. page.Landscape = cbLandscape.Checked;
  270. page.PaperWidth = ConvertUnitsToMillimeters(tbPageWidth.Text);
  271. page.PaperHeight = ConvertUnitsToMillimeters(tbPageHeight.Text);
  272. page.LeftMargin = ConvertUnitsToMillimeters(tbMarginLeft.Text);
  273. page.TopMargin = ConvertUnitsToMillimeters(tbMarginTop.Text);
  274. // setup data band
  275. page.Bands.Add(band);
  276. band.Columns.Count = (int)nudCountColumns.Value;
  277. band.RowCount = (int)nudCountColumns.Value * (int)nudCountRows.Value;
  278. band.Columns.Width = GetWidthBand(band) + ConvertUnitsToMillimeters(tbHorizontalPadding.Text) * Units.Millimeters;
  279. band.CanGrow = true;
  280. band.Border = border;
  281. band.CreateUniqueName();
  282. band.CalcHeight();
  283. band.Height += ConvertUnitsToMillimeters(tbVerticalPadding.Text) * Units.Millimeters;
  284. }
  285. private void cbxPaper_SelectedIndexChanged(object sender, EventArgs e)
  286. {
  287. foreach (PaperSize ps in printerSettings.PaperSizes)
  288. {
  289. if (ps.PaperName == (string)cbxPaper.SelectedItem)
  290. {
  291. float psWidth = ps.Width / 100f * 25.4f;
  292. float psHeight = ps.Height / 100f * 25.4f;
  293. if (ps.Kind == PaperKind.A4)
  294. {
  295. psWidth = 210;
  296. psHeight = 297;
  297. }
  298. if (cbLandscape.Checked)
  299. {
  300. float ex = psWidth;
  301. psWidth = psHeight;
  302. psHeight = ex;
  303. }
  304. // avoid reset papersize to custom
  305. update = true;
  306. tbPageWidth.Text = psWidth.ToString();
  307. tbPageHeight.Text = psHeight.ToString();
  308. update = false;
  309. break;
  310. }
  311. }
  312. UpdateSample();
  313. }
  314. private string GetUnits()
  315. {
  316. string unit = "";
  317. switch (ReportWorkspace.Grid.GridUnits)
  318. {
  319. case PageUnits.Millimeters:
  320. unit = Res.Get("Misc,ShortUnitsMm");
  321. break;
  322. case PageUnits.Centimeters:
  323. unit = Res.Get("Misc,ShortUnitsCm");
  324. break;
  325. case PageUnits.Inches:
  326. unit = Res.Get("Misc,ShortUnitsIn");
  327. break;
  328. case PageUnits.HundrethsOfInch:
  329. unit = Res.Get("Misc,ShortUnitsHi");
  330. break;
  331. }
  332. return unit;
  333. }
  334. private void CbLandscape_CheckedChanged(object sender, System.EventArgs e)
  335. {
  336. if (update)
  337. return;
  338. string w = tbPageWidth.Text;
  339. string h = tbPageHeight.Text;
  340. // avoid reset papersize to custom
  341. update = true;
  342. tbPageWidth.Text = h;
  343. tbPageHeight.Text = w;
  344. update = false;
  345. UpdateSample();
  346. }
  347. private void TbPageProps_TextChanged(object sender, System.EventArgs e)
  348. {
  349. if (update)
  350. return;
  351. cbxPaper.SelectedIndex = 0;
  352. UpdateSample();
  353. }
  354. private void TbWithUnits_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
  355. {
  356. e.Handled = !char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && e.KeyChar != '.' && e.KeyChar != ',' && e.KeyChar != ' ';
  357. }
  358. private void UpdateSample()
  359. {
  360. DataBand band1 = CreateReport();
  361. band1.CanGrow = true;
  362. band1.CalcHeight();
  363. float widthBands = GetWidthBand(band1);
  364. ReportPage page = sampleReport.Pages[0] as ReportPage;
  365. page.Clear();
  366. page.Landscape = cbLandscape.Checked;
  367. page.PaperWidth = ConvertUnitsToMillimeters(tbPageWidth.Text);
  368. page.PaperHeight = ConvertUnitsToMillimeters(tbPageHeight.Text);
  369. page.LeftMargin = ConvertUnitsToMillimeters(tbMarginLeft.Text);
  370. page.TopMargin = ConvertUnitsToMillimeters(tbMarginTop.Text);
  371. DataBand band = new DataBand();
  372. band.Parent = page;
  373. bool fit = true;
  374. for (int x = 0; x < (int)nudCountColumns.Value; x++)
  375. {
  376. for (int y = 0; y < (int)nudCountRows.Value; y++)
  377. {
  378. ShapeObject shape = new ShapeObject();
  379. shape.Parent = band;
  380. shape.Shape = ShapeKind.RoundRectangle;
  381. shape.Border.Color = Color.Gray;
  382. shape.Bounds = new RectangleF(x * (widthBands + ConvertUnitsToMillimeters(tbHorizontalPadding.Text)),
  383. y * (band1.Bounds.Height + ConvertUnitsToMillimeters(tbVerticalPadding.Text)),
  384. widthBands,
  385. band1.Bounds.Height);
  386. if (shape.Right / Units.Millimeters > page.PaperWidth - page.LeftMargin + 0.1f ||
  387. shape.Bottom / Units.Millimeters > page.PaperHeight - page.TopMargin + 0.1f)
  388. {
  389. (shape.Fill as SolidFill).Color = Color.Red;
  390. fit = false;
  391. }
  392. }
  393. }
  394. rcSample.Report = sampleReport;
  395. btnOk.Enabled = fit;
  396. lbWarning.Visible = !fit;
  397. }
  398. public void InitWizard(Report report)
  399. {
  400. this.report = report;
  401. }
  402. private float GetWidthBand(BandBase band)
  403. {
  404. float widthBand = 0;
  405. foreach(var item in band.AllObjects)
  406. {
  407. if (item is ReportComponentBase)
  408. widthBand = Math.Max(widthBand, (item as ReportComponentBase).Right);
  409. }
  410. return widthBand;
  411. }
  412. public override void Localize()
  413. {
  414. base.Localize();
  415. MyRes res = new MyRes("Forms,CISWizardForm");
  416. Text = res.Get("");
  417. gbCIS.Text = res.Get("CIS");
  418. lbType.Text = res.Get("ProductGroup");
  419. lbData.Text = res.Get("Data");
  420. lbTypeBarcodes.Text = res.Get("TypeBarcodes");
  421. cbLeftMargin.Text = res.Get("LeftMargin");
  422. lbMethodСirculation.Text = res.Get("MethodСirculation");
  423. lbCountry.Text = res.Get("Country");
  424. lbID.Text = res.Get("ID");
  425. lbSize.Text = res.Get("Size");
  426. gbPageProps.Text = res.Get("PageProps");
  427. cbLandscape.Text = res.Get("Landscape");
  428. lbMarginTop.Text = res.Get("MarginTop");
  429. lbMarginLeft.Text = res.Get("MarginLeft");
  430. lbMargin.Text = res.Get("Margins");
  431. lbPaper.Text = res.Get("Paper");
  432. lbPageWidth.Text = res.Get("PageWidth");
  433. lbPageHeight.Text = res.Get("PageHeight");
  434. lbCountColumns.Text = res.Get("CountColumns");
  435. lbCountRows.Text = res.Get("CountRows");
  436. lbHorizontalPadding.Text = res.Get("HorizontalPadding");
  437. lbVerticalPadding.Text = res.Get("VerticalPadding");
  438. gbSample.Text = res.Get("Sample");
  439. btnBorder.Text = res.Get("Border");
  440. lbWarning.Text = res.Get("Warning");
  441. cbxType.Items.Clear();
  442. cbxType.Items.AddRange(
  443. new object[]
  444. {
  445. res.Get("TransportPack"),
  446. res.Get("Pack"),
  447. res.Get("Block"),
  448. res.Get("Legprom"),
  449. res.Get("Perfumes"),
  450. res.Get("Footwear"),
  451. res.Get("Fur"),
  452. res.Get("Photo"),
  453. res.Get("Tires"),
  454. res.Get("Medicines"),
  455. res.Get("Dairy"),
  456. res.Get("Water"),
  457. }
  458. );
  459. cbxType.SelectedIndex = 0;
  460. cbxMethodСirculation.Items.Clear();
  461. cbxMethodСirculation.Items.AddRange(
  462. new object[]
  463. {
  464. res.Get("ImportProd"),
  465. res.Get("ManufactureProd")
  466. }
  467. );
  468. cbxMethodСirculation.SelectedIndex = 0;
  469. lbUnitsMarginTop.Text = GetUnits();
  470. lbUnitsMarginLeft.Text = GetUnits();
  471. lbUnitsHPadding.Text = GetUnits();
  472. lbUnitsVPadding.Text = GetUnits();
  473. lbUnitsPHeight.Text = GetUnits();
  474. lbUnitsPWidth.Text = GetUnits();
  475. }
  476. public CISWizardForm()
  477. {
  478. InitializeComponent();
  479. UpdateDpiDependencies();
  480. sampleReport = new Report();
  481. sampleReport.Pages.Add(new ReportPage());
  482. sampleReport.SmoothGraphics = true;
  483. // paper
  484. printerSettings = new PrinterSettings();
  485. PaperSize defaultPaper = printerSettings.DefaultPageSettings.PaperSize;
  486. cbxPaper.Items.Add(Res.Get("Forms,PageSetup,Custom"));
  487. foreach (PaperSize ps in printerSettings.PaperSizes)
  488. {
  489. cbxPaper.Items.Add(ps.PaperName);
  490. if (ps.PaperName == defaultPaper.PaperName)
  491. cbxPaper.SelectedIndex = cbxPaper.Items.Count - 1;
  492. }
  493. border = new Border();
  494. update = false;
  495. Localize();
  496. CbxType_SelectedIndexChanged(null, null);
  497. }
  498. }
  499. }