LazMapViewer: Add OnStartDrag and OnEndDrag events to TMarkerSelectAndDragPlugin

git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@9694 8e941d3f-bd1b-0410-a28a-d453659cc2b4
This commit is contained in:
wp_xxyyzz 2025-03-21 14:03:12 +00:00
parent c3c6ce1223
commit 3d757daf7d

View File

@ -96,6 +96,8 @@ type
ADrawingEngine: TMvCustomDrawingEngine; AGPSPoint: TGPSPoint; ADrawingEngine: TMvCustomDrawingEngine; AGPSPoint: TGPSPoint;
AScreenPoint: TPoint; AMarkerSize: Integer) of object; AScreenPoint: TPoint; AMarkerSize: Integer) of object;
TMarkerStartDragEvent = procedure (AMapView: TMapView; var CanDrag: Boolean) of object;
TMarkerClickMode = (mcmAddPointToSelection, mcmAddShapeToSelection, mcmToggleSelectedPoint); TMarkerClickMode = (mcmAddPointToSelection, mcmAddShapeToSelection, mcmToggleSelectedPoint);
TMarkerSelectAndDragPlugin = class(TMarkerClickPlugin) TMarkerSelectAndDragPlugin = class(TMarkerClickPlugin)
@ -108,6 +110,8 @@ type
FOrigSelection: array of TRealPoint; // Selection before dragging starts FOrigSelection: array of TRealPoint; // Selection before dragging starts
FOnDrawPoint: TMarkerDrawPointEvent; FOnDrawPoint: TMarkerDrawPointEvent;
FOnSelectionChange: TNotifyEvent; FOnSelectionChange: TNotifyEvent;
FOnStartDrag: TMarkerStartDragEvent;
FOnEndDrag: TNotifyEvent;
procedure SetMultiSelect(AValue: Boolean); procedure SetMultiSelect(AValue: Boolean);
protected protected
procedure AddToSelection(AMapView: TMapView; APoint: TGPSPoint); procedure AddToSelection(AMapView: TMapView; APoint: TGPSPoint);
@ -121,7 +125,6 @@ type
procedure DrawSelection(AMapView: TMapView); procedure DrawSelection(AMapView: TMapView);
procedure FindContainerOfPoint(AMapView: TMapView; APoint: TGPSPoint; var AContainer: TGPSObj; var AIndex: Integer); procedure FindContainerOfPoint(AMapView: TMapView; APoint: TGPSPoint; var AContainer: TGPSObj; var AIndex: Integer);
procedure FindMapCollection(AMapView: TMapView; APoint: TGPSPoint; var ACollection: TMapCollectionBase; var AIndex: Integer); procedure FindMapCollection(AMapView: TMapView; APoint: TGPSPoint; var ACollection: TMapCollectionBase; var AIndex: Integer);
procedure MoveSelectionBy(AMapView: TMapView; dx, dy: Integer);
procedure ToggleSelected(AMapView: TMapView; APoint: TGPSPoint); procedure ToggleSelected(AMapView: TMapView; APoint: TGPSPoint);
protected protected
procedure AfterDrawObjects(AMapView: TMapView; var {%H-}Handled: Boolean); override; procedure AfterDrawObjects(AMapView: TMapView; var {%H-}Handled: Boolean); override;
@ -139,6 +142,8 @@ type
function ConvertSelectedPointsToGPSTrack(AMapView: TMapView; ATrackID: Integer): TGPSTrack; function ConvertSelectedPointsToGPSTrack(AMapView: TMapView; ATrackID: Integer): TGPSTrack;
function ConvertSelectedPointsToMapTrack(AMapView: TMapView; ALayer: TMapLayer): TMapTrack; function ConvertSelectedPointsToMapTrack(AMapView: TMapView; ALayer: TMapLayer): TMapTrack;
procedure DeleteSelectedPoints(AMapView: TMapView); procedure DeleteSelectedPoints(AMapView: TMapView);
procedure MoveSelectionBy(AMapView: TMapView; dx, dy: Double);
procedure MoveSelectionBy(AMapView: TMapView; dx, dy: Integer);
procedure SelectAllPointsOfShape(AMapView: TMapView; APoint: TGPSPoint); procedure SelectAllPointsOfShape(AMapView: TMapView; APoint: TGPSPoint);
property Selection: TGPSPointList read FSelection; property Selection: TGPSPointList read FSelection;
published published
@ -146,7 +151,9 @@ type
property DragCursor: TCursor read FDragCursor write FDragCursor default crSizeAll; property DragCursor: TCursor read FDragCursor write FDragCursor default crSizeAll;
property MultiSelect: Boolean read FMultiSelect write SetMultiSelect default false; property MultiSelect: Boolean read FMultiSelect write SetMultiSelect default false;
property OnDrawPoint: TMarkerDrawPointEvent read FOnDrawPoint write FOnDrawPoint; property OnDrawPoint: TMarkerDrawPointEvent read FOnDrawPoint write FOnDrawPoint;
property OnEndDrag: TNotifyEvent read FOnEndDrag write FOnEndDrag;
property OnSelectionChange: TNotifyEvent read FOnSelectionChange write FOnSelectionChange; property OnSelectionChange: TNotifyEvent read FOnSelectionChange write FOnSelectionChange;
property OnStartDrag: TMarkerStartDragEvent read FOnStartDrag write FOnStartDrag;
end; end;
@ -568,7 +575,14 @@ end;
procedure TMarkerSelectAndDragPlugin.DragStart(AMapView: TMapView); procedure TMarkerSelectAndDragPlugin.DragStart(AMapView: TMapView);
var var
i: Integer; i: Integer;
canDrag: Boolean;
begin begin
if Assigned(FOnStartDrag) then
begin
canDrag := true;
FOnStartDrag(AMapView, canDrag);
if not canDrag then exit;
end;
AMapView.Cursor := DragCursor; AMapView.Cursor := DragCursor;
FDragging := true; FDragging := true;
// Save original selection point coordinates in case they must be restored later. // Save original selection point coordinates in case they must be restored later.
@ -583,7 +597,6 @@ var
begin begin
if FDragging then if FDragging then
begin begin
// AMapView.Cursor := DragCursor;
dX := X - FMousePoint.X; dX := X - FMousePoint.X;
dY := Y - FMousePoint.Y; dY := Y - FMousePoint.Y;
MoveSelectionBy(AMapView, dX, dY); MoveSelectionBy(AMapView, dX, dY);
@ -596,6 +609,8 @@ procedure TMarkerSelectAndDragPlugin.DragEnd(AMapView: TMapView);
begin begin
FDragging := false; FDragging := false;
AMapView.Cursor := FSavedCursor; AMapView.Cursor := FSavedCursor;
if Assigned(FOnEndDrag) then
FOnEndDrag(AMapView);
end; end;
{ Draw the selection marker for the given point. The drawing engine already { Draw the selection marker for the given point. The drawing engine already
@ -651,6 +666,14 @@ begin
end; end;
end; end;
function IsSamePoint(AItem: TGPSObj; APoint: TGPSPoint): Boolean;
begin
Result := (AItem is TGPSPoint) and TGPSPoint(AItem).RealPoint.Equal(APoint.RealPoint);
end;
{ Tries to find the point in one of the gps-type lists.
If found, returns the found object and the index of the point in the list.
Otherwise, nil and -1 are returned, respectively. }
procedure TMarkerSelectAndDragPlugin.FindContainerOfPoint(AMapView: TMapView; procedure TMarkerSelectAndDragPlugin.FindContainerOfPoint(AMapView: TMapView;
APoint: TGPSPoint; var AContainer: TGPSObj; var AIndex: Integer); APoint: TGPSPoint; var AContainer: TGPSObj; var AIndex: Integer);
var var
@ -658,12 +681,6 @@ var
gpsLayer: TGPSObjectList; gpsLayer: TGPSObjectList;
gpsPolyline: TGPSPolyLine; gpsPolyline: TGPSPolyLine;
item: TGPSObj; item: TGPSObj;
function IsSamePoint(AItem: TGPSObj): Boolean;
begin
Result := (AItem is TGPSPoint) and TGPSPoint(AItem).RealPoint.Equal(APoint.RealPoint);
end;
begin begin
// Iterate over the 10 layers of GPSItems // Iterate over the 10 layers of GPSItems
for i := 0 to 9 do for i := 0 to 9 do
@ -672,7 +689,7 @@ begin
for j := 0 to gpsLayer.Count-1 do for j := 0 to gpsLayer.Count-1 do
begin begin
item := gpsLayer[j]; item := gpsLayer[j];
if IsSamePoint(item) then if IsSamePoint(item, APoint) then
begin begin
AContainer := gpsLayer; AContainer := gpsLayer;
AIndex := j; AIndex := j;
@ -685,7 +702,7 @@ begin
for k := 0 to gpsPolyLine.Points.Count-1 do for k := 0 to gpsPolyLine.Points.Count-1 do
begin begin
item := gpsPolyLine.Points[k]; item := gpsPolyLine.Points[k];
if IsSamePoint(item) then if IsSamePoint(item, APoint) then
begin begin
AContainer := gpsPolyLine; AContainer := gpsPolyLine;
AIndex := k; AIndex := k;
@ -712,12 +729,6 @@ var
mapLayer: TMapLayer; mapLayer: TMapLayer;
mapTrack: TMapTrack; mapTrack: TMapTrack;
mapArea: TMapArea; mapArea: TMapArea;
function IsSamePoint(AItem: TGPSObj): Boolean;
begin
Result := (AItem is TGPSPoint) and TGPSPoint(AItem).RealPoint.Equal(APoint.RealPoint);
end;
begin begin
// Iterate over all map-layers // Iterate over all map-layers
for i := 0 to AMapView.Layers.Count-1 do for i := 0 to AMapView.Layers.Count-1 do
@ -727,7 +738,7 @@ begin
for j := 0 to mapLayer.PointsOfInterest.Count-1 do for j := 0 to mapLayer.PointsOfInterest.Count-1 do
begin begin
p := mapLayer.PointsOfInterest[j]; p := mapLayer.PointsOfInterest[j];
if IsSamePoint(p.GPSObj) then if IsSamePoint(p.GPSObj, APoint) then
begin begin
ACollection := mapLayer.PointsOfInterest; ACollection := mapLayer.PointsOfInterest;
AIndex := j; AIndex := j;
@ -741,7 +752,7 @@ begin
for k := 0 to mapTrack.Points.Count-1 do for k := 0 to mapTrack.Points.Count-1 do
begin begin
p := mapTrack.Points[k]; p := mapTrack.Points[k];
if IsSamePoint(p.GPSObj) then if IsSamePoint(p.GPSObj, APoint) then
begin begin
ACollection := mapTrack.Points; ACollection := mapTrack.Points;
AIndex := k; AIndex := k;
@ -756,7 +767,7 @@ begin
for k := 0 to mapArea.Points.Count-1 do for k := 0 to mapArea.Points.Count-1 do
begin begin
p := mapArea.Points[k]; p := mapArea.Points[k];
if IsSamePoint(p.GPSObj) then if IsSamePoint(p.GPSObj, APoint) then
begin begin
ACollection := mapArea.Points; ACollection := mapArea.Points;
AIndex := k; AIndex := k;
@ -770,6 +781,7 @@ begin
AIndex := -1; AIndex := -1;
end; end;
{ Moves the selection by the given amound of pixels in x and y direction. }
procedure TMarkerSelectAndDragPlugin.MoveSelectionBy(AMapView: TMapView; dx, dy: Integer); procedure TMarkerSelectAndDragPlugin.MoveSelectionBy(AMapView: TMapView; dx, dy: Integer);
var var
i: Integer; i: Integer;
@ -786,6 +798,28 @@ begin
end; end;
end; end;
{ Moves the selection by the given amound of degrees in x and y direction }
procedure TMarkerSelectAndDragPlugin.MoveSelectionBy(AMapView: TMapView; dx, dy: Double);
var
i: Integer;
P: TPoint;
rPt: TRealPoint;
begin
for i := 0 to FSelection.Count-1 do
begin
rPt := FSelection[i].RealPoint;
rPt.Lon := FSelection[i].Lon + dX;
rPt.Lat := FSelection[i].Lat + dY;
if rPt.Lat > 90 then
rPt.Lat := rPt.Lat - 180;
if rPt.Lon < -90 then
rPt.Lon := rPt.Lon + 180;
P := AMapView.LatLonToScreen(rPt);
rPt := AMapView.ScreenToLatLon(P);
FSelection[i].MoveTo(rPt.Lon, rPt.Lat);
end;
end;
procedure TMarkerSelectAndDragPlugin.MouseDown(AMapView: TMapView; procedure TMarkerSelectAndDragPlugin.MouseDown(AMapView: TMapView;
{%H-}Button: TMouseButton; {%H-}AShift: TShiftState; {%H-}Button: TMouseButton; {%H-}AShift: TShiftState;
X, Y: Integer; var Handled: Boolean); X, Y: Integer; var Handled: Boolean);