home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 December / Chip_2001-12_cd1.bin / zkuste / delphi / kolekce / d56 / DM2KVCL.ZIP / SERIEDLG.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  2001-09-09  |  14.9 KB  |  407 lines

  1. {****************************************************************************}
  2. {                            Data Master 2000                                }
  3. {****************************************************************************}
  4. unit SerieDlg;
  5.  
  6. interface
  7.  
  8. uses
  9.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  10.   StdCtrls, Spin, ComCtrls, ImgList, Buttons, ExtCtrls, ColorGrd, Data, Plot;
  11.  
  12. type
  13.   TSeriePropsForm = class(TForm)
  14.     OkBitBtn: TBitBtn;
  15.     CancelBitBtn: TBitBtn;
  16.     HelpBitBtn: TBitBtn;
  17.     ImageList: TImageList;
  18.     ColorDialog: TColorDialog;
  19.     PageControl: TPageControl;
  20.     DataTabSheet: TTabSheet;
  21.     ViewTabSheet: TTabSheet;
  22.     XGroupBox: TGroupBox;
  23.     GridIntervalsLabel: TLabel;
  24.     XColumnRadioButton: TRadioButton;
  25.     XExpressionRadioButton: TRadioButton;
  26.     XGridRadioButton: TRadioButton;
  27.     XExpressionComboBox: TComboBox;
  28.     GridIntervalsSpinEdit: TSpinEdit;
  29.     YGroupBox: TGroupBox;
  30.     YColumnRadioButton: TRadioButton;
  31.     YExpressionRadioButton: TRadioButton;
  32.     YExpressionComboBox: TComboBox;
  33.     FirstLineLabel: TLabel;
  34.     FirstLineSpinEdit: TSpinEdit;
  35.     LastLineLabel: TLabel;
  36.     LastLineSpinEdit: TSpinEdit;
  37.     InterleaveSpinEdit: TSpinEdit;
  38.     InterleaveLabel: TLabel;
  39.     WorksheetLabel: TLabel;
  40.     WorksheetComboBox: TComboBox;
  41.     PointsCheckBox: TCheckBox;
  42.     PointsPanel: TPanel;
  43.     PointSizeLabel: TLabel;
  44.     PointSizeSpinEdit: TSpinEdit;
  45.     PointTypeComboBox: TComboBox;
  46.     PointTypeLabel: TLabel;
  47.     LinePanel: TPanel;
  48.     LineWidthLabel: TLabel;
  49.     LineTypeLabel: TLabel;
  50.     LineWidthSpinEdit: TSpinEdit;
  51.     LineTypeComboBox: TComboBox;
  52.     LineCheckBox: TCheckBox;
  53.     PreviewPaintBox: TPaintBox;
  54.     CaptionLabel: TLabel;
  55.     CaptionEdit: TEdit;
  56.     XColumnComboBox: TComboBox;
  57.     YColumnComboBox: TComboBox;
  58.     PointsColorGroupBox: TGroupBox;
  59.     PointsColorGrid: TColorGrid;
  60.     PointsColorBitBtn: TBitBtn;
  61.     LineColorGroupBox: TGroupBox;
  62.     LineColorGrid: TColorGrid;
  63.     LineColorBitBtn: TBitBtn;
  64.     InfoTabSheet: TTabSheet;
  65.     BestFitCheckBox: TCheckBox;
  66.     BestFitBevel: TBevel;
  67.     BestFitLabel: TLabel;
  68.     BestFitEdit: TEdit;
  69.     LimitsGroupBox: TGroupBox;
  70.     X1Label: TLabel;
  71.     X2Label: TLabel;
  72.     Y1Label: TLabel;
  73.     Y2Label: TLabel;
  74.     X1Edit: TEdit;
  75.     X2Edit: TEdit;
  76.     Y1Edit: TEdit;
  77.     Y2Edit: TEdit;
  78.     SetScaleBitBtn: TBitBtn;
  79.     XTextSpeedButton: TSpeedButton;
  80.     YTextSpeedButton: TSpeedButton;
  81.     procedure PointPanelCheckBoxClick(Sender: TObject);
  82.     procedure ColumnRadioButtonClick(Sender: TObject);
  83.     procedure XExpressionComboBoxChange(Sender: TObject);
  84.     procedure YExpressionComboBoxChange(Sender: TObject);
  85.     procedure PreviewPaintBoxPaint(Sender: TObject);
  86.     procedure ColorGridChange(Sender: TObject);
  87.     procedure PointsColorBitBtnClick(Sender: TObject);
  88.     procedure LineColorBitBtnClick(Sender: TObject);
  89.     procedure LineTypeComboBoxDrawItem(Control: TWinControl;
  90.       Index: Integer; R: TRect; State: TOwnerDrawState);
  91.     procedure PointTypeComboBoxDrawItem(Control: TWinControl;
  92.       Index: Integer; R: TRect; State: TOwnerDrawState);
  93.     procedure WorksheetComboBoxChange(Sender: TObject);
  94.     procedure SetScaleBitBtnClick(Sender: TObject);
  95.     procedure XTextSpeedButtonClick(Sender: TObject);
  96.     procedure YTextSpeedButtonClick(Sender: TObject);
  97.     procedure WorksheetComboBoxDropDown(Sender: TObject);
  98.   private
  99.     { Private declarations }
  100.     PointColorBuf,LineColorBuf: TColor;
  101.     FPlot: TPlot; FSerie: TSerie;
  102.     procedure UpdateBlockControls;
  103.   public
  104.     { Public declarations }
  105.     function Execute(Serie: TSerie): boolean;
  106.   end;
  107.  
  108. var SeriePropsForm: TSeriePropsForm;
  109.  
  110. resourcestring
  111.   strNotScaled='Unavailable (serie not scaled)';
  112.  
  113. implementation
  114.  
  115. uses TypInfo, TextDlg;
  116.  
  117. {$R *.DFM}
  118.  
  119. { TSeriePropsForm }
  120.  
  121. {prior to Execute() call, following comboboxes MUST be initialized:
  122. 1) Columns combos - with valid column number and labels;
  123. 2) Worksheet combo: Clear, then AddObjects(<Window caption/Filename>,
  124.    <Pointer to TContainer>) + ItemIndex set to serie's container (or -1);
  125. 3) Expressions combos - assigned expressions history list.}
  126. function TSeriePropsForm.Execute(Serie: TSerie): boolean;
  127. var BX,BY: boolean; I: integer;
  128. begin
  129.   FPlot:=(Serie.Collection as TSeries).Plot; {save reference for PaintPoint()}
  130.   FSerie:=Serie;                                   {for SetScaleBitBtnClick()}
  131.   XColumnRadioButton.Checked:=true; YColumnRadioButton.Checked:=true;  {reset}
  132.   XColumnComboBox.ItemIndex:=Serie.XColumn-1;     {copy properties from serie}
  133.   YColumnComboBox.ItemIndex:=Serie.YColumn-1;
  134.   XExpressionComboBox.Text:=Serie.XExpression; XExpressionComboBoxChange(nil);
  135.   YExpressionComboBox.Text:=Serie.YExpression; YExpressionComboBoxChange(nil);
  136.   if Assigned(Serie.Container) then
  137.   begin
  138.     FirstLineSpinEdit.MaxValue:=Serie.Container.Items.Count-1;
  139.     LastLineSpinEdit.MaxValue:=Serie.Container.Items.Count-1;
  140.   end else
  141.   begin
  142.     FirstLineSpinEdit.MaxValue:=-1; LastLineSpinEdit.MaxValue:=-1;
  143.   end;
  144.   FirstLineSpinEdit.Value:=Serie.FirstLine;
  145.   LastLineSpinEdit.Value:=Serie.LastLine;
  146.   InterleaveSpinEdit.Value:=Serie.Interleave;
  147.   PointsCheckBox.Checked:=Serie.PointVisible;     {handler called implicitly!}
  148.   LineCheckBox.Checked:=Serie.LineVisible;
  149.   CaptionEdit.Text:=Serie.Text;
  150.   PointsColorGrid.ForegroundIndex:=-1;
  151.   LineColorGrid.ForegroundIndex:=-1;
  152.   PointColorBuf:=Serie.Brush.Color;
  153.   LineColorBuf:=Serie.Pen.Color;
  154.   PointSizeSpinEdit.Value:=Serie.PointSize;
  155.   PointTypeComboBox.ItemIndex:=ord(Serie.PointType);
  156.   LineWidthSpinEdit.Value:=Serie.Pen.Width;
  157.   case Serie.Pen.Style of
  158.     psSolid: LineTypeComboBox.ItemIndex:=0;
  159.     psDash: LineTypeComboBox.ItemIndex:=1;
  160.     psDot: LineTypeComboBox.ItemIndex:=2;
  161.     psDashDot: LineTypeComboBox.ItemIndex:=3;
  162.   else LineTypeComboBox.ItemIndex:=-1;
  163.   end;
  164.   if Serie.IsFunction then
  165.   begin
  166.     XGridRadioButton.Checked:=true;
  167.     GridIntervalsSpinEdit.Value:=Serie.LastLine-Serie.FirstLine;
  168.   end;
  169.   if Serie.Scaled then
  170.   begin
  171.     BestFitEdit.Text:='Y=X*'+FloatToStr(Serie.bfA);
  172.     if Serie.bfB>0
  173.     then BestFitEdit.Text:=BestFitEdit.Text+'+'+FloatToStr(Serie.bfB)
  174.     else BestFitEdit.Text:=BestFitEdit.Text+FloatToStr(Serie.bfB);
  175.     X1Edit.Text:=FloatToStr(Serie.X1); X2Edit.Text:=FloatToStr(Serie.X2);
  176.     Y1Edit.Text:=FloatToStr(Serie.Y1); Y2Edit.Text:=FloatToStr(Serie.Y2);
  177.   end else
  178.   begin
  179.     X1Edit.Text:=strNotScaled; X2Edit.Text:=strNotScaled;
  180.     Y1Edit.Text:=strNotScaled; Y2Edit.Text:=strNotScaled;
  181.     BestFitEdit.Text:=strNotScaled; 
  182.   end;
  183.   LimitsGroupBox.Enabled:=Serie.Scaled; SetScaleBitBtn.Enabled:=Serie.Scaled;
  184.   BestFitCheckBox.Checked:=Serie.ShowBestFit;
  185.   Result:=ShowModal=mrOK; if not Result then Exit;                 {cancelled}
  186.   Serie.ShowBestFit:=BestFitCheckBox.Checked;
  187.   Serie.XColumn:=XColumnComboBox.ItemIndex+1;       {copy properties to serie}
  188.   Serie.YColumn:=YColumnComboBox.ItemIndex+1;
  189.   Serie.XExpression:=XExpressionComboBox.Text;
  190.   Serie.YExpression:=YExpressionComboBox.Text;
  191.   Serie.FirstLine:=FirstLineSpinEdit.Value;
  192.   Serie.LastLine:=LastLineSpinEdit.Value;
  193.   Serie.Interleave:=InterleaveSpinEdit.Value;
  194.   with WorksheetComboBox do if (ItemIndex>=0)
  195.   and (Items.Objects[ItemIndex] is TContainer)
  196.   then Serie.Container:=Items.Objects[ItemIndex] as TContainer;
  197.   Serie.IsFunction:=XGridRadioButton.Checked;
  198.   if Serie.IsFunction
  199.   then Serie.LastLine:=Serie.FirstLine+GridIntervalsSpinEdit.Value;
  200.   Serie.PointVisible:=PointsCheckBox.Checked;
  201.   Serie.LineVisible:=LineCheckBox.Checked;
  202.   Serie.Text:=CaptionEdit.Text;
  203.   Serie.Brush.Color:=PointColorBuf;
  204.   Serie.Pen.Color:=LineColorBuf;
  205.   Serie.PointSize:=PointSizeSpinEdit.Value;
  206.   Serie.PointType:=TPointType(PointTypeComboBox.ItemIndex);
  207.   Serie.Pen.Width:=LineWidthSpinEdit.Value;
  208.   case LineTypeComboBox.ItemIndex of
  209.     0: Serie.Pen.Style:=psSolid;
  210.     1: Serie.Pen.Style:=psDash;
  211.     2: Serie.Pen.Style:=psDot;
  212.     3: Serie.Pen.Style:=psDashDot;
  213.   end;
  214.   BX:=true; BY:=true;                         {add expressions to historylist}
  215.   with XExpressionComboBox.Items do for I:=0 to Count-1 do
  216.   begin
  217.     if Strings[I]=XExpressionComboBox.Text then BX:=false;
  218.     if Strings[I]=YExpressionComboBox.Text then BY:=false;
  219.   end;
  220.   with XExpressionComboBox do      {result saved only in XExpressionComboBox!}
  221.   begin
  222.     if BX and (Text<>'') then Items.Insert(0, Text);
  223.     if BY and (YExpressionComboBox.Text<>'')
  224.     then Items.Insert(0, YExpressionComboBox.Text);
  225.   end;
  226. end;
  227.  
  228. procedure TSeriePropsForm.PointPanelCheckBoxClick(Sender: TObject);
  229. var I: integer; B: boolean; P: TPanel; PropInfo: PPropInfo;
  230. begin
  231.   B:=(Sender as TCheckBox).Checked;
  232.   if Sender=PointsCheckBox then P:=PointsPanel else P:=LinePanel;
  233.   P.Enabled:=B;
  234.   for I:=0 to P.ControlCount-1 do
  235.   begin
  236.     PropInfo:=GetPropInfo(P.Controls[I].ClassInfo,'Enabled');
  237.     if (PropInfo<>nil) then SetOrdProp(P.Controls[I],PropInfo,integer(B));
  238.   end;
  239.   PreviewPaintBoxPaint(nil);
  240. end;
  241.  
  242. procedure TSeriePropsForm.UpdateBlockControls;
  243. begin
  244.   XColumnComboBox.Enabled:=XColumnRadioButton.Checked;
  245.   if XColumnRadioButton.Checked then XExpressionComboBox.Text:='';
  246.   if XExpressionRadioButton.Checked and (XColumnComboBox.ItemIndex<0)
  247.   then XColumnComboBox.ItemIndex:=0;
  248.   GridIntervalsLabel.Enabled:=XGridRadioButton.Checked;
  249.   GridIntervalsSpinEdit.Enabled:=XGridRadioButton.Checked;
  250.   XExpressionComboBox.Enabled:=not XGridRadioButton.Checked;
  251.   XTextSpeedButton.Enabled:=XExpressionComboBox.Enabled;
  252.   FirstLineSpinEdit.Enabled:=not XGridRadioButton.Checked;
  253.   LastLineSpinEdit.Enabled:=not XGridRadioButton.Checked;
  254.   InterleaveSpinEdit.Enabled:=not XGridRadioButton.Checked;
  255.   FirstLineLabel.Enabled:=not XGridRadioButton.Checked;
  256.   LastLineLabel.Enabled:=not XGridRadioButton.Checked;
  257.   InterleaveLabel.Enabled:=not XGridRadioButton.Checked;
  258.   YColumnRadioButton.Enabled:=not XGridRadioButton.Checked;
  259.   if XGridRadioButton.Checked then YExpressionRadioButton.Checked:=true;
  260.   YColumnComboBox.Enabled:=YColumnRadioButton.Checked;
  261.   if YColumnRadioButton.Checked then YExpressionComboBox.Text:='';
  262.   if YExpressionRadioButton.Checked  and (YColumnComboBox.ItemIndex<0)
  263.   then YColumnComboBox.ItemIndex:=0;
  264.   WorksheetComboBox.Enabled:=not XGridRadioButton.Checked;
  265.   WorksheetLabel.Enabled:=not XGridRadioButton.Checked;
  266.   if XGridRadioButton.Checked then WorksheetComboBox.ItemIndex:=-1;
  267. end;
  268.  
  269. procedure TSeriePropsForm.ColumnRadioButtonClick(Sender: TObject);
  270. begin UpdateBlockControls; end;
  271.  
  272. procedure TSeriePropsForm.XExpressionComboBoxChange(Sender: TObject);
  273. begin
  274.   if XExpressionComboBox.Text<>'' then XExpressionRadioButton.Checked:=true;
  275. end;
  276.  
  277. procedure TSeriePropsForm.YExpressionComboBoxChange(Sender: TObject);
  278. begin
  279.   if YExpressionComboBox.Text<>'' then YExpressionRadioButton.Checked:=true;
  280. end;
  281.  
  282. procedure TSeriePropsForm.PreviewPaintBoxPaint(Sender: TObject);
  283. begin
  284.   with PreviewPaintBox do
  285.   begin
  286.     Canvas.Brush.Color:=Color;
  287.     Canvas.FillRect(Rect(0,0,Width,Height));
  288.     Canvas.Pen.Color:=LineColorBuf;
  289.     case LineTypeComboBox.ItemIndex of
  290.       0: Canvas.Pen.Style:=psSolid;
  291.       1: Canvas.Pen.Style:=psDash;
  292.       2: Canvas.Pen.Style:=psDot;
  293.       3: Canvas.Pen.Style:=psDashDot;
  294.     end;
  295.     Canvas.Pen.Width:=LineWidthSpinEdit.Value;
  296.     Canvas.Brush.Color:=PointColorBuf;
  297.     Canvas.MoveTo(1, Height div 2);
  298.     if LineCheckBox.Checked then Canvas.LineTo(Width-1, Height div 2);
  299.     if PointsCheckBox.Checked then FPlot.PaintPoint(Width div 2, Height div 2,
  300.     TPointType(PointTypeComboBox.ItemIndex),PointSizeSpinEdit.Value,Canvas);
  301.   end;
  302. end;
  303.  
  304. procedure TSeriePropsForm.ColorGridChange(Sender: TObject);
  305. begin
  306.   if Sender=PointsColorGrid then
  307.   PointColorBuf:=PointsColorGrid.ForegroundColor;
  308.   if Sender=LineColorGrid then LineColorBuf:=LineColorGrid.ForegroundColor;
  309.   PreviewPaintBoxPaint(nil);
  310. end;
  311.  
  312. procedure TSeriePropsForm.PointsColorBitBtnClick(Sender: TObject);
  313. begin
  314.   ColorDialog.Color:=PointColorBuf; if not ColorDialog.Execute then Exit;
  315.   PointsColorGrid.ForegroundIndex:=-1; PointColorBuf:=ColorDialog.Color;
  316.   PreviewPaintBoxPaint(nil);
  317. end;
  318.  
  319. procedure TSeriePropsForm.LineColorBitBtnClick(Sender: TObject);
  320. begin
  321.   ColorDialog.Color:=LineColorBuf; if not ColorDialog.Execute then Exit;
  322.   LineColorGrid.ForegroundIndex:=-1; LineColorBuf:=ColorDialog.Color;
  323.   PreviewPaintBoxPaint(nil);
  324. end;
  325.  
  326. procedure TSeriePropsForm.LineTypeComboBoxDrawItem(Control: TWinControl;
  327.   Index: Integer; R: TRect; State: TOwnerDrawState);
  328. var PSBuf: TPenStyle;
  329. begin
  330.   with LineTypeComboBox do
  331.   begin
  332.     Canvas.FillRect(R); PSBuf:=Canvas.Pen.Style;
  333.     case Index of
  334.       0: Canvas.Pen.Style:=psSolid;
  335.       1: Canvas.Pen.Style:=psDash;
  336.       2: Canvas.Pen.Style:=psDot;
  337.       3: Canvas.Pen.Style:=psDashDot;
  338.     end;
  339.     Canvas.MoveTo(R.Left, R.Top+(R.Bottom-R.Top) div 2);
  340.     Canvas.LineTo(R.Right, R.Top+(R.Bottom-R.Top) div 2);
  341.     Canvas.Pen.Style:=PSBuf;
  342.   end;
  343. end;
  344.  
  345. procedure TSeriePropsForm.PointTypeComboBoxDrawItem(Control: TWinControl;
  346.   Index: Integer; R: TRect; State: TOwnerDrawState);
  347. begin
  348.   with PointTypeComboBox do
  349.   begin
  350.     Canvas.FillRect(R);
  351.     FPlot.PaintPoint((R.Left+R.Right) div 2, R.Top+((R.Bottom-R.Top) div 2),
  352.     TPointType(Index), ((R.Bottom-R.Top) div 2)+2, Canvas);
  353.   end;
  354. end;
  355.  
  356. procedure TSeriePropsForm.WorksheetComboBoxChange(Sender: TObject);
  357. begin
  358.   with WorksheetComboBox do if (ItemIndex>=0)
  359.   and (Items.Objects[ItemIndex] is TContainer) then
  360.   with (Items.Objects[ItemIndex] as TContainer) do
  361.   begin
  362.     FirstLineSpinEdit.MaxValue:=Items.Count-1;
  363.     with FirstLineSpinEdit do if Value>MaxValue then Value:=MaxValue;
  364.     LastLineSpinEdit.MaxValue:=Items.Count-1;
  365.     with LastLineSpinEdit do if Value>MaxValue then Value:=MaxValue;
  366.   end else
  367.   begin
  368.     FirstLineSpinEdit.MaxValue:=-1; LastLineSpinEdit.MaxValue:=-1;
  369.   end;
  370. end;
  371.  
  372. procedure TSeriePropsForm.SetScaleBitBtnClick(Sender: TObject);
  373. begin
  374.   FPlot.XAxis.Autoscale:=false; FPlot.YAxis.Autoscale:=false;
  375.   FPlot.XAxis.Min:=FSerie.X1; FPlot.XAxis.Max:=FSerie.X2;
  376.   FPlot.YAxis.Min:=FSerie.Y1; FPlot.YAxis.Max:=FSerie.Y2;
  377. end;
  378.  
  379. procedure TSeriePropsForm.XTextSpeedButtonClick(Sender: TObject);
  380. var S: string;
  381. begin
  382.   S:=XExpressionComboBox.Text;
  383.   if InputText(S,XExpressionComboBox) then XExpressionComboBox.Text:=S;
  384. end;
  385.  
  386. procedure TSeriePropsForm.YTextSpeedButtonClick(Sender: TObject);
  387. var S: string;
  388. begin
  389.   S:=YExpressionComboBox.Text;
  390.   if InputText(S,YExpressionComboBox) then YExpressionComboBox.Text:=S;
  391. end;
  392.  
  393. procedure TSeriePropsForm.WorksheetComboBoxDropDown(Sender: TObject);
  394. var I,N: integer;   // correct list size for long fnames (font must be fixed!)
  395. begin
  396.   N:=0; with Sender as TComboBox do
  397.   begin
  398.     for I:=0 to Items.Count-1 do
  399.     if N<length(Items[I]) then N:=length(Items[I]);   // get max string length
  400.     N:=Round(-N*Font.Height/1.5); if N<Width then N:=Width;
  401.     SendMessage(Handle,CB_SETDROPPEDWIDTH,N,0);                      // do it!
  402.   end;
  403. end;
  404.  
  405. end.
  406.  
  407.