home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 December / Chip_2001-12_cd1.bin / zkuste / delphi / kolekce / d3456 / ALEXSOFT.ZIP / DBPRINT.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  2001-09-04  |  5.3 KB  |  155 lines

  1. (*////////////////////////////////////////////////////////////////////////////
  2. //   Part of AlexSoft VCL/DLL Library.                                      //
  3. //   All rights reserved. (c) Copyright 1998.                               //
  4. //   Created by: Alex Rabichooc                                             //
  5. //**************************************************************************//
  6. //  Users of this unit must accept this disclaimer of warranty:             //
  7. //    "This unit is supplied as is. The author disclaims all warranties,    //
  8. //    expressed or implied, including, without limitation, the warranties   //
  9. //    of merchantability and of fitness for any purpose.                    //
  10. //    The author assumes no liability for damages, direct or                //
  11. //    consequential, which may result from the use of this unit."           //
  12. //                                                                          //
  13. //  This Unit is donated to the public as public domain.                    //
  14. //                                                                          //
  15. //  This Unit can be freely used and distributed in commercial and          //
  16. //  private environments provided this notice is not modified in any way.   //
  17. //                                                                          //
  18. //  If you do find this Unit handy and you feel guilty for using such a     //
  19. //  great product without paying someone - sorry :-)                        //
  20. //                                                                          //
  21. //  Please forward any comments or suggestions to Alex Rabichooc at:        //
  22. //                                                                          //
  23. //  a_rabichooc@yahoo.com or alex@carmez.mldnet.com                         //
  24. /////////////////////////////////////////////////////////////////////////////*)
  25.  
  26. unit dbPrint;
  27.  
  28. interface
  29.  
  30. uses
  31.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  32.   RaReport, Qrctrls, QuickRpt, ExtCtrls, db, QRaCtrls;
  33.  
  34. type
  35.   TfmDBPrint = class(TfmRaReport)
  36.     QRSubDetail: TQRaSubDetail;
  37.     QRLbSubData: TQRaLabel;
  38.     QRaHLine1: TQRaHLine;
  39.     QRLbHeader: TQRaLabel;
  40.     QRaHLine2: TQRaHLine;
  41.     QRlbData: TQRaLabel;
  42.      procedure QRLbDataPrint(Sender: TObject; var Value: String);
  43.     procedure FormDestroy(Sender: TObject);
  44.   private
  45.     FBookmark: TBookmark;
  46.   protected
  47.     procedure SetFontSize; dynamic;
  48.   public
  49.     procedure DoPreview;
  50.     constructor Create(AOwner: TComponent); override;
  51.     constructor CreateWithDataSet(AOwner: TComponent; DataSet: TDataSet);
  52.   end;
  53.  
  54. implementation
  55. {$R *.DFM}
  56.  
  57. uses StdUtils, dbForms;
  58.  
  59. constructor TfmDBPrint.CreateWithDataSet(AOwner: TComponent; DataSet: TDataSet);
  60. begin
  61.   Inherited Create(AOwner);
  62.   Report.DataSet := DataSet;
  63.   DoPreview;
  64. end;
  65.  
  66. procedure TfmDBPrint.SetFontSize;
  67. var i, L: Integer;
  68.     HText: String;
  69. begin
  70.    if Report.DataSet <> nil then
  71.    with Report.DataSet do
  72.    begin
  73.       HText := ': ';
  74.       for i := 0 to FieldCount-1 do
  75.       begin
  76.         if Fields[i].Visible then
  77.         begin
  78.           L := Fields[i].DisplayWidth;
  79.           if L < Length(Fields[i].DisplayLabel) then
  80.              L := Length(Fields[i].DisplayLabel);
  81.           HText:= HText+Format('%-*.*s : ', [L, L, Fields[i].DisplayLabel]);
  82.         end;
  83.       end;
  84.       HText := Copy(HText, 1, Length(HText)-1);
  85.       QRLbHeader.Caption := HText;
  86.       QRLbHeader.WidthInChars := Length(HText);
  87.       QRLbData.WidthInChars := Length(HText);
  88.       QRaHLine1.WidthInChars := Length(HText);
  89.       QRaHLine2.WidthInChars := Length(HText);
  90.    end;
  91. end;
  92.  
  93. procedure TfmDBPrint.QRLbDataPrint(sender: TObject; var Value: String);
  94. var i, L: Integer;
  95.     FormatStr: String;
  96. begin
  97.    if Report.DataSet = nil then exit;
  98.    Value := ': ';
  99.    with Report.DataSet do
  100.    begin
  101.       for i := 0 to FieldCount-1 do
  102.       begin
  103.         if Fields[i].Visible then
  104.         begin
  105.           L := Fields[i].DisplayWidth;
  106.           if L < Length(Fields[i].DisplayLabel) then
  107.              L := Length(Fields[i].DisplayLabel);
  108.           if Fields[i].Alignment = taLeftJustify then
  109.             FormatStr := '%-*.*s : '
  110.            else
  111.             FormatStr := '%*.*s : ';
  112.           Value := Value+Format(FormatStr, [L, L, Fields[i].Text]);
  113.         end;
  114.       end;
  115.    end;
  116.    Value := Copy(Value, 1, Length(Value)-1);
  117. end;
  118.  
  119. procedure TfmDBPrint.DoPreview;
  120. begin
  121.   if Report.DataSet = nil then
  122.      exit;
  123.   FBookmark := Report.DataSet.GetBookmark;
  124.   SetFontSize;
  125.   Report.Preview;
  126. end;
  127.  
  128. constructor TfmDBPrint.Create(AOwner: TComponent);
  129. begin
  130.    Inherited;
  131.    if (Owner is TDefaultForm) and (Report.DataSet = nil) then
  132.    begin
  133.       Report.DataSet := (Owner as TDefaultForm).DataSource.DataSet;
  134.       Report.DataSet.DisableControls;
  135.       (Owner as TDefaultForm).Enabled := False;
  136.    end;
  137.    DoPreview;
  138. end;
  139.  
  140. procedure TfmDBPrint.FormDestroy(Sender: TObject);
  141. begin
  142.   if Report.DataSet = nil then
  143.      exit;
  144.   if (FBookmark <> nil) and Report.DataSet.BookmarkValid(FBookmark) then
  145.      Report.DataSet.GotoBookmark(FBookmark);
  146.   if (Owner is TDefaultForm) and
  147.      (Report.DataSet = (Owner as TDefaultForm).DataSource.DataSet) then
  148.   begin
  149.     Report.DataSet.EnableControls;
  150.     (Owner as TDefaultForm).Enabled := True;
  151.   end;
  152. end;
  153.  
  154. end.
  155.