home *** CD-ROM | disk | FTP | other *** search
/ Delphi Anthology / aDELPHI.iso / Runimage / Delphi50 / Demos / Quickrpt / Qr3 / mdmain.pas < prev    next >
Pascal/Delphi Source File  |  1999-08-11  |  10KB  |  340 lines

  1. { :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  2.   :: QuickReport 3.0 for Delphi 3.0/4.0/5.0                  ::
  3.   ::                                                         ::
  4.   :: Example reports project                                 ::
  5.   ::                                                         ::
  6.   :: Copyright (c) 1995-1999 QuSoft AS                       ::
  7.   :: All Rights Reserved                                     ::
  8.   ::                                                         ::
  9.   :: web: http://www.qusoft.com  fax: +47 22 41 74 91        ::
  10.   ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: }
  11. unit mdmain;
  12.  
  13. interface
  14.  
  15. uses
  16.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  17.   ExtCtrls, StdCtrls, quickrpt, Db, DBTables, printers, qrextra, QRExport,
  18.   qrprntr;
  19.  
  20. const
  21.  Composite_Description = 'The composite control can be used to link several'+
  22.                          ' reports together as a single report.';
  23.  
  24. type
  25.   TfrmQR3Demo = class(TForm)
  26.     QRTextFilter1: TQRTextFilter;
  27.     QRCSVFilter1: TQRCSVFilter;
  28.     QRHTMLFilter1: TQRHTMLFilter;
  29.     QRCompositeReport1: TQRCompositeReport;
  30.     Label1: TLabel;
  31.     VersionLbl: TLabel;
  32.     Label3: TLabel;
  33.     GroupBox1: TGroupBox;
  34.     rbCreateList: TRadioButton;
  35.     btnPreview: TButton;
  36.     btnPrint: TButton;
  37.     rbMasterDetail: TRadioButton;
  38.     Description: TMemo;
  39.     OpenDialog1: TOpenDialog;
  40.     cbPreview: TComboBox;
  41.     Label4: TLabel;
  42.     rbExprMemo: TRadioButton;
  43.     rbImage: TRadioButton;
  44.     rbBasicMD: TRadioButton;
  45.     btnFilters: TButton;
  46.     rbComposite: TRadioButton;
  47.     Label5: TLabel;
  48.     rbNeedData: TRadioButton;
  49.     rbFormLetter: TRadioButton;
  50.     btnExport: TButton;
  51.     SaveDialog1: TSaveDialog;
  52.     rbAbout: TRadioButton;
  53.     rbGrouping: TRadioButton;
  54.     Image2: TImage;
  55.     procedure CreateList(Sender: TObject);
  56.     procedure QRCompositeReport1AddReports(Sender: TObject);
  57.     procedure btnCRClick(Sender: TObject);
  58.     procedure rbCreateListClick(Sender: TObject);
  59.     procedure rbMasterDetailClick(Sender: TObject);
  60.     procedure FormCreate(Sender: TObject);
  61.     procedure FormActivate(Sender: TObject);
  62.     procedure btnPreviewClick(Sender: TObject);
  63.     procedure rbExprMemoClick(Sender: TObject);
  64.     procedure rbImageClick(Sender: TObject);
  65.     procedure rbBasicMDClick(Sender: TObject);
  66.     procedure btnFiltersClick(Sender: TObject);
  67.     procedure rbCompositeClick(Sender: TObject);
  68.     procedure rbNeedDataClick(Sender: TObject);
  69.     procedure rbFormLetterClick(Sender: TObject);
  70.     procedure btnExportClick(Sender: TObject);
  71.     procedure rbAboutClick(Sender: TObject);
  72.     procedure rbGroupingClick(Sender: TObject);
  73.   private
  74.     { Private declarations }
  75.     FReport : TCustomQuickRep;
  76.     CreateListReport : TQuickRep;
  77.     procedure SetReport(Value : TCustomQuickRep);
  78.   public
  79.     { Public declarations }
  80.     property Report : TCustomQuickRep read FReport write SetReport;
  81.   end;
  82.  
  83. var
  84.   frmQR3Demo: TfrmQR3Demo;
  85.  
  86. implementation
  87.  
  88. uses mdrpt, exprmemo, image, basicmd, needdata, frmltr,
  89.   history, grouping;
  90.  
  91. {$R *.DFM}
  92.  
  93. procedure TfrmQR3Demo.SetReport(Value : TCustomQuickRep);
  94. begin
  95.   FReport := Value;
  96.   if Value <> nil then
  97.     if Value = TCustomQuickRep(QRCompositeReport1) then
  98.       Description.Lines.Text := Composite_Description
  99.     else
  100.       Description.Lines.Assign(Report.Description);
  101. end;
  102.  
  103. procedure TfrmQR3Demo.CreateList(Sender: TObject);
  104. var
  105.   aReport : TCustomQuickRep;
  106.   SomeFields: TStringList;
  107.   MyTable: TTable;
  108.   nIdx: integer;
  109. begin
  110.   { Create a table on the fly, this example uses a table from the demo database }
  111.   MyTable := TTable.Create(self);
  112.  
  113.   { create the list of fields to output from the table }
  114.   SomeFields := TStringList.Create;
  115.   with MyTable do
  116.   begin
  117.     DatabaseName := 'DBDEMOS';
  118.     TableName := 'COUNTRY.DB';
  119.     ReadOnly := True;
  120.     Active := True;
  121.  
  122.     // For this example, we will pull the field names from the table
  123.     // If you wanted to only use some of the fields, you would edit
  124.     // this list.
  125.     for nIdx := 0 to FieldCount - 1 do
  126.       SomeFields.Add(Fields[nIdx].FieldName);
  127.   end;
  128.  
  129.   // If you didn't create the report, you must set the report object to nil
  130.   // before calling QRCreateList
  131.  
  132.   areport := nil;
  133.  
  134.   { Build the report }
  135.  
  136.   // If you change the displaywidth, it will be reflecte in the created
  137.   // report
  138.  
  139.   with MyTable.Fields[1] do
  140.     DisplayWidth := DisplayWidth div 2;
  141.  
  142.   // create the report
  143.   QRCreateList(aReport, nil, MyTable, 'Country Listing', SomeFields);
  144.  
  145.   // Make the column header's font use bold attribute
  146.   aReport.Bands.ColumnHeaderBand.Font.Style := [fsBold];
  147.  
  148.   // Now adjust the spacing of the fields.  There isn't any reason to
  149.   // do this, this is just to show how to access the controls on the
  150.   // report.
  151.  
  152.   for nIdx := 0 to aReport.Bands.ColumnHeaderBand.ControlCount -1 do
  153.     if aReport.Bands.ColumnHeaderBand.Controls[nIdx] is TQRPrintable then
  154.       with TQRPrintable(aReport.Bands.ColumnHeaderBand.Controls[nIdx]) do
  155.         Left := Left - (5 * nIdx);
  156.  
  157.   for nIdx := 0 to aReport.Bands.DetailBand.ControlCount -1 do
  158.     if aReport.Bands.DetailBand.Controls[nIdx] is TQRPrintable then
  159.       with TQRPrintable(aReport.Bands.DetailBand.Controls[nIdx]) do
  160.         Left := Left - (5 * nIdx);
  161.  
  162.  
  163.   { You can change the report objects before calling the report }
  164. //  areport.page.orientation := poLandscape;
  165.   {preview or print the report}
  166.  
  167.   if sender = btnPreview then
  168.   begin
  169.     case cbPreview.ItemIndex of
  170.       0: areport.preview;
  171.       1: areport.previewModal;
  172.       2: areport.previewModeless;
  173.     end;
  174.   end
  175.   else if sender = btnPrint then
  176.     areport.print;
  177.  
  178.   { all done, free the objects }
  179.   aReport.Free;
  180.   MyTable.Free;
  181.   SomeFields.Free;
  182. end;
  183.  
  184. procedure TfrmQR3Demo.QRCompositeReport1AddReports(Sender: TObject);
  185. begin
  186.   // The OnAddReports event is called by the CompositeReport
  187.   // to add the reports to list of reports
  188.   with QRCompositeReport1.Reports do
  189.   begin
  190.     Add(frmMasterDetail.QuickRep1);
  191.     Add(frmBasicMD.QuickRep1);
  192.     Add(frmImageRpt.QuickRep1);
  193.   end;
  194. end;
  195.  
  196. procedure TfrmQR3Demo.btnCRClick(Sender: TObject);
  197. begin
  198.   QRCompositeReport1.Preview;
  199. end;
  200.  
  201. procedure TfrmQR3Demo.rbCreateListClick(Sender: TObject);
  202. begin
  203.   Report := CreateListReport;
  204. end;
  205.  
  206. procedure TfrmQR3Demo.rbMasterDetailClick(Sender: TObject);
  207. begin
  208.   Report := frmMasterDetail.QuickRep1;
  209. end;
  210.  
  211. procedure TfrmQR3Demo.FormCreate(Sender: TObject);
  212. var i: integer;
  213. begin
  214.   // Get the current QuickReport version number from
  215.   // the qrprntr unit
  216.   i := pos(' ', cQRName);
  217.   if i >=0 then
  218.     VersionLbl.Caption := 'Version' + copy(cQRName, i, 99);
  219.   // Create the report object used by the QRCreateList() function
  220.   CreateListReport := TQuickRep.Create(self);
  221.   CreateListReport.Description.Text := 'Example of how to call the QRCreateList function';
  222.   cbPreview.ItemIndex := 0;
  223. end;
  224.  
  225. procedure TfrmQR3Demo.FormActivate(Sender: TObject);
  226. begin
  227.   if Description.Lines.Count = 0 then
  228.   begin
  229.     rbCreateListClick(Self);
  230.     rbCreateList.Checked := True;
  231.   end;
  232. end;
  233.  
  234. procedure TfrmQR3Demo.btnPreviewClick(Sender: TObject);
  235. begin
  236.   // This code is more complicated than what you would
  237.   // typically need to run a report.  Most of this code
  238.   // is to handle the selection of the various types of
  239.   // reports.
  240.  
  241.   if report <> nil then
  242.   begin
  243.     if report = CreateListReport then
  244.       CreateList(Sender)
  245.     else if report = TCustomQuickRep(QRCompositeReport1) then
  246.     begin
  247.       if sender = btnPreview then
  248.         QRCompositeReport1.preview
  249.       else
  250.         QRCompositeReport1.print;
  251.     end
  252.     else
  253.     begin
  254.       if sender = btnPreview then
  255.       begin
  256.         case cbPreview.ItemIndex of
  257.           0: report.preview;
  258.           1: report.previewModal;
  259.           2: report.previewModeless;
  260.         end;
  261.       end
  262.       else if sender = btnPrint then
  263.         report.print;
  264.     end;
  265.   end;
  266. end;
  267.  
  268. procedure TfrmQR3Demo.rbExprMemoClick(Sender: TObject);
  269. begin
  270.   Report := frmExprmemo.QuickRep1;
  271. end;
  272.  
  273. procedure TfrmQR3Demo.rbImageClick(Sender: TObject);
  274. begin
  275.   Report := frmImageRpt.QuickRep1;
  276. end;
  277.  
  278. procedure TfrmQR3Demo.rbBasicMDClick(Sender: TObject);
  279. begin
  280.   Report := frmBasicMD.QuickRep1;
  281. end;
  282.  
  283. procedure TfrmQR3Demo.btnFiltersClick(Sender: TObject);
  284. begin
  285.   MessageDlg('When an export filter control is dropped on a form, it''s added to all of the previews',mtInformation, [mbok], 0);
  286. end;
  287.  
  288. procedure TfrmQR3Demo.rbCompositeClick(Sender: TObject);
  289. begin
  290.   Report := TCustomQuickRep(QRCompositeReport1);
  291. end;
  292.  
  293. procedure TfrmQR3Demo.rbNeedDataClick(Sender: TObject);
  294. begin
  295.   Report := frmNeedData.QuickRep1;
  296. end;
  297.  
  298. procedure TfrmQR3Demo.rbFormLetterClick(Sender: TObject);
  299. begin
  300.   Report := frmFormLetter.QuickRep1;
  301. end;
  302.  
  303. // The following code show how to explicitly call an export
  304. // filter without going through the preview
  305. procedure TfrmQR3Demo.btnExportClick(Sender: TObject);
  306. begin
  307.   btnExport.Enabled := False;
  308.   with SaveDialog1 do
  309.   begin
  310.     if Execute then
  311.     begin
  312.       frmFormLetter.QuickRep1.ExportToFilter(TQRCommaSeparatedFilter.Create(FileName));
  313. {
  314. Other filters:
  315. HTML: TQRHTMLDocumentFilter
  316. ASCII: TQRAsciiExportFilter
  317. CSV: TQRCommaSeparatedFilter
  318.  
  319. In Professional Version:
  320. RTF: TQRRTFExportFilter
  321. WMF: TQRWMFExportFilter
  322. Excel: TQRXLSFilter
  323. }
  324.     end;
  325.   end;
  326.   btnExport.Enabled := True;
  327. end;
  328.  
  329. procedure TfrmQR3Demo.rbAboutClick(Sender: TObject);
  330. begin
  331.   Report := frmHistory.QuickRep1;
  332. end;
  333.  
  334. procedure TfrmQR3Demo.rbGroupingClick(Sender: TObject);
  335. begin
  336.   Report := frmGrouping.QuickRep1;
  337. end;
  338.  
  339. end.
  340.