Recent

Author Topic: [Solved] Crosshair error when changing series  (Read 1233 times)

Marq01

  • New Member
  • *
  • Posts: 19
[Solved] Crosshair error when changing series
« on: December 10, 2024, 08:02:17 pm »
 I have this problem with the crosshair that is bugging me.
I know what is happening but not how to solve it. Tried many things but even AI did not give the answer i needed.
Example: in a chart are 6 lines and with the [Ctrl]+mousemove the crosshair wil give the datapoint below the crosshair. This works as expected and when the crosshair is not close enough tot he lines it disappears: as expected.
Then new lines are loaded in the same chart after deleting the previous with
 
Code: Pascal  [Select][+][-]
  1.   if (aChart.SeriesCount > 0) then
  2.     for i := aChart.SeriesCount - 1 downto 0 do
  3.        if (aChart.Series[i] is TChartSeries) then
  4.          TChartSeries(aChart.Series[i]).Free;
  5.  

Which works perfectly well.
UNLESS: the crosshair was visible when deleting and loading new lines. Then it trows an listindex error in the function
 

Code: Pascal  [Select][+][-]
  1. function TListChartSource.GetItem(AIndex: Integer): PChartDataItem;
  2. begin
  3.   Result := PChartDataItem(FData.Items[AIndex]);
  4. end;  
  5.  

of the TASources unit.

I can understand why it does this: the previous chart series are gone and new are in place. But how teach the crosshair not to display and reset itself?
As said I tried a lot (Enable/disable/hide).
Please find my program in the attachment. It it only the essentials but still big.
 
Start program – goto tab ‘injections’ – press [ctrl]+mousemove and set crosshair. – goto to tab ‘alingment’ and witness the problem.
 
Please what should i do to solve the crosshair?
 
« Last Edit: December 11, 2024, 07:48:40 pm by Marq01 »

wp

  • Hero Member
  • *****
  • Posts: 12525
Re: Crosshair error when changing series
« Reply #1 on: December 11, 2024, 11:41:50 am »
I don't fully understand the issue because your description of the issue seems to be different from the behaviour of the program. I need SHFT+mousemove to display the crosshair.

When I select the Alignment page with visible crosshair I do see the crash that you mention. I'd propse to hide the crosshair in the OnChange handler of the pagecontrol; you already have this in your code, commented out with message "ERROR // hide because sometimes it stays". Uncommenting this line does result in an error indeed.

Debugging into this error leads to TBasicChartTool.Deactivate in which properties of the internal FChart variable are accessed although FChart is nil. I could not find out why FChart became nil here, but I think it should not be harmful to perform a check for nil here:

Open unit TAGraph, find the implementation of TBasicChartTool.Deactivate, put all the code into an "if Assigned(FChart) then" condition. Probably it is helpful to do the same with TBasicChartTool.Activate:
Code: Pascal  [Select][+][-]
  1. procedure TBasicChartTool.Activate;
  2. begin
  3.   if Assigned(FChart) then
  4.   begin
  5.     FChart.FActiveToolIndex := Index;
  6.     FChart.MouseCapture := true;
  7.     FChart.FDisablePopupMenu := false;
  8.     FStartMousePos := Mouse.CursorPos;
  9.   end;
  10. end;
  11.  
  12. procedure TBasicChartTool.Deactivate;
  13. begin
  14.   if Assigned(FChart) then
  15.   begin
  16.     FChart.MouseCapture := false;
  17.     FChart.FActiveToolIndex := -1;
  18.     if PopupMenuConflict then
  19.       FChart.FDisablePopupMenu := true;
  20.   end;
  21. end;

This (together with the crosshairtool.Hide in the pagecontrol's OnChange handler) fixes the issue.

I noticed that the crosshair cursor does not disappear when the SHIFT key is released. Well, it should... This probably is because the chart does not have the focus at this moment and thus does not get the key events. You can avoid this by setting the chart's AutoFocus property to true - now the chart is focused whenever the mouse is moved inside the chart. But be aware that this is a two-sided sword: When you are typing something into an edit somewhere and incidentally move the mouse into the chart, the edit will lose focus, and when you continue typing the edit will not receive the key strokes any more because they are sent to the chart now.

Marq01

  • New Member
  • *
  • Posts: 19
Re: Crosshair error when changing series
« Reply #2 on: December 11, 2024, 07:47:59 pm »
Sorry for the error in Ctrl vs Shift key.

I had to add one more change in my original program to the TATools unit:

Code: Pascal  [Select][+][-]
  1. procedure TDataPointDrawTool.DoHide;
  2. begin
  3.   if Assigned(FChart) then
  4.   begin
  5.     case EffectiveDrawingMode of
  6.       tdmXor: begin
  7.         FChart.Drawer.SetXor(true);
  8.         DoDraw;
  9.         FChart.Drawer.SetXor(false);
  10.       end;
  11.       tdmNormal:
  12.         FChart.StyleChanged(Self);
  13.     end;
  14.   end;
  15. end;


And Yes the AutoFocus did add some more problems than it solved. For now the hide is the best option I think.
Thanks for pointing in the right way.

wp

  • Hero Member
  • *****
  • Posts: 12525
Re: Crosshair error when changing series
« Reply #3 on: December 11, 2024, 10:39:23 pm »
I had to add one more change in my original program to the TATools unit:

Code: Pascal  [Select][+][-]
  1. procedure TDataPointDrawTool.DoHide;
  2. begin
  3.   if Assigned(FChart) then
  4.   begin
  5.     case EffectiveDrawingMode of
  6.       tdmXor: begin
  7.         FChart.Drawer.SetXor(true);
  8.         DoDraw;
  9.         FChart.Drawer.SetXor(false);
  10.       end;
  11.       tdmNormal:
  12.         FChart.StyleChanged(Self);
  13.     end;
  14.   end;
  15. end;
What is the scenario why you want to change this? The original code in TAChart looks quite reasonable (except maybe for the unchecked call to FChart.StyleChanged).

Marq01

  • New Member
  • *
  • Posts: 19
Re: [Solved] Crosshair error when changing series
« Reply #4 on: December 12, 2024, 08:09:39 pm »
For some reason an error pops up at the line 1936 in TATools

Code: Pascal  [Select][+][-]
  1. FChart.Drawer.SetXor(true);

and close inspection shows that FChart=nil so I reasond that an extra check would be necessary in TDataPointDrawTool.Dohide


For completeness i use Lazarus version #2.0.12; FPC 3.2.0; SVN revision: 64642 win64
Not sure which version of TAChart I use (how to find out?).

wp

  • Hero Member
  • *****
  • Posts: 12525
Re: [Solved] Crosshair error when changing series
« Reply #5 on: December 12, 2024, 10:45:16 pm »
Ah, now I understand why I had not seen this crash here. Laz 2.0.12 is pretty old, and in Laz 2.2.0 a modification of DoHide had come in:
Code: Pascal  [Select][+][-]
  1. procedure TDataPointDrawTool.DoHide(ADrawer: IChartDrawer);
  2. begin
  3.   if ADrawer = nil then exit;
  4.   case EffectiveDrawingMode of
  5.     tdmXor: begin
  6.       ADrawer.SetXor(true);
  7.       DoDraw(ADrawer);
  8.       ADrawer.SetXor(false);
  9.     end;
  10.     tdmNormal:
  11.       if Assigned(FChart) then FChart.StyleChanged(Self);   // "if Assigned(FChart) then" added today.
  12.   end;
  13. end;

Marq01

  • New Member
  • *
  • Posts: 19
Re: [Solved] Crosshair error when changing series
« Reply #6 on: December 13, 2024, 09:55:14 pm »
Good point. I really need to update to keep up with all the stuff.
Thanks wp.

 

TinyPortal © 2005-2018