Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add optional marker menu #24

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion tkintermapview/canvas_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ def __init__(self,
position_list, color="#3E69CB",
command=None,
name=None,
data: any = None):
data: any = None,
menu: any = None):

self.map_widget = map_widget
self.position_list = position_list
Expand All @@ -25,7 +26,9 @@ def __init__(self,
self.command = command
self.canvas_line = None
self.name = name
self.menu = menu
self.data = data
self.mouse_over = False

self.last_upper_left_tile_pos = None
self.last_position_list_length = len(self.position_list)
Expand Down Expand Up @@ -58,6 +61,7 @@ def get_canvas_pos(self, position, widget_tile_width, widget_tile_height):
return canvas_pos_x, canvas_pos_y

def mouse_enter(self, event=None):
self.mouse_over = True
if sys.platform == "darwin":
self.map_widget.canvas.config(cursor="pointinghand")
elif sys.platform.startswith("win"):
Expand All @@ -66,12 +70,23 @@ def mouse_enter(self, event=None):
self.map_widget.canvas.config(cursor="hand2") # not tested what it looks like on Linux!

def mouse_leave(self, event=None):
self.mouse_over = False
self.map_widget.canvas.config(cursor="arrow")

def click(self, event=None):
if self.command is not None:
self.command(self)

def click_right(self, event=None):
if self.menu is not None:
m = tkinter.Menu(self.map_widget, tearoff=0)
for title, cmd in self.menu.items():
if title == '-':
m.add_separator()
continue
m.add_command(label=f"{title}", command=cmd)
m.tk_popup(event.x_root, event.y_root) # display menu

def draw(self, move=False):
new_line_length = self.last_position_list_length != len(self.position_list)
self.last_position_list_length = len(self.position_list)
Expand Down Expand Up @@ -105,6 +120,10 @@ def draw(self, move=False):
self.map_widget.canvas.tag_bind(self.canvas_line, "<Enter>", self.mouse_enter)
self.map_widget.canvas.tag_bind(self.canvas_line, "<Leave>", self.mouse_leave)
self.map_widget.canvas.tag_bind(self.canvas_line, "<Button-1>", self.click)
if sys.platform == "darwin":
self.map_widget.canvas.tag_bind(self.canvas_line, "<Button-2>", self.click_right)
else:
self.map_widget.canvas.tag_bind(self.canvas_line, "<Button-3>", self.click_right)
else:
self.map_widget.canvas.coords(self.canvas_line, self.canvas_line_positions)
else:
Expand Down
21 changes: 20 additions & 1 deletion tkintermapview/canvas_polygon.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ def __init__(self,
border_width: int = 5,
command: Callable = None,
name: str = None,
data: any = None):
data: any = None,
menu: any = None):

self.map_widget = map_widget
self.position_list = position_list # list with decimal positions
Expand All @@ -26,7 +27,9 @@ def __init__(self,
self.deleted = False

self.name = name
self.menu = menu
self.data = data
self.mouse_over = False
self.outline_color = outline_color
self.fill_color = fill_color # can also be None for transparent fill
self.border_width = border_width
Expand Down Expand Up @@ -56,6 +59,7 @@ def remove_position(self, deg_x, deg_y):
self.draw()

def mouse_enter(self, event=None):
self.mouse_over = True
if sys.platform == "darwin":
self.map_widget.canvas.config(cursor="pointinghand")
elif sys.platform.startswith("win"):
Expand All @@ -64,12 +68,23 @@ def mouse_enter(self, event=None):
self.map_widget.canvas.config(cursor="hand2") # not tested what it looks like on Linux!

def mouse_leave(self, event=None):
self.mouse_over = False
self.map_widget.canvas.config(cursor="arrow")

def click(self, event=None):
if self.command is not None:
self.command(self)

def click_right(self, event=None):
if self.menu is not None:
m = tkinter.Menu(self.map_widget, tearoff=0)
for title, cmd in self.menu.items():
if title == '-':
m.add_separator()
continue
m.add_command(label=f"{title}", command=cmd)
m.tk_popup(event.x_root, event.y_root) # display menu

def get_canvas_pos(self, position, widget_tile_width, widget_tile_height):
tile_position = decimal_to_osm(*position, round(self.map_widget.zoom))

Expand Down Expand Up @@ -120,6 +135,10 @@ def draw(self, move=False):
self.map_widget.canvas.tag_bind(self.canvas_polygon, "<Enter>", self.mouse_enter)
self.map_widget.canvas.tag_bind(self.canvas_polygon, "<Leave>", self.mouse_leave)
self.map_widget.canvas.tag_bind(self.canvas_polygon, "<Button-1>", self.click)
if sys.platform == "darwin":
self.map_widget.canvas.tag_bind(self.canvas_polygon, "<Button-2>", self.click_right)
else:
self.map_widget.canvas.tag_bind(self.canvas_polygon, "<Button-3>", self.click_right)
else:
self.map_widget.canvas.coords(self.canvas_polygon, self.canvas_polygon_positions)
else:
Expand Down
29 changes: 28 additions & 1 deletion tkintermapview/canvas_position_marker.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ def __init__(self,
command: Callable = None,
image=None,
image_zoom_visibility: tuple = (13, float("inf")),
data: any = None):
data: any = None,
menu: any = None):

self.map_widget = map_widget
self.position = position
Expand All @@ -33,7 +34,9 @@ def __init__(self,
self.image_zoom_visibility = image_zoom_visibility
self.deleted = False
self.command = command
self.menu = menu
self.data = data
self.mouse_over = False

self.polygon = None
self.big_circle = None
Expand Down Expand Up @@ -70,6 +73,7 @@ def hide_image(self, image_hidden):
self.draw()

def mouse_enter(self, event=None):
self.mouse_over = True
if sys.platform == "darwin":
self.map_widget.canvas.config(cursor="pointinghand")
elif sys.platform.startswith("win"):
Expand All @@ -78,12 +82,23 @@ def mouse_enter(self, event=None):
self.map_widget.canvas.config(cursor="hand2") # not tested what it looks like on Linux!

def mouse_leave(self, event=None):
self.mouse_over = False
self.map_widget.canvas.config(cursor="arrow")

def click(self, event=None):
if self.command is not None:
self.command(self)

def click_right(self, event=None):
if self.menu is not None:
m = tkinter.Menu(self.map_widget, tearoff=0)
for title, cmd in self.menu.items():
if title == '-':
m.add_separator()
continue
m.add_command(label=f"{title}", command=cmd)
m.tk_popup(event.x_root, event.y_root) # display menu

def get_canvas_pos(self, position):
tile_position = decimal_to_osm(*position, round(self.map_widget.zoom))

Expand All @@ -110,6 +125,10 @@ def draw(self, event=None):
self.map_widget.canvas.tag_bind(self.polygon, "<Enter>", self.mouse_enter)
self.map_widget.canvas.tag_bind(self.polygon, "<Leave>", self.mouse_leave)
self.map_widget.canvas.tag_bind(self.polygon, "<Button-1>", self.click)
if sys.platform == "darwin":
self.map_widget.canvas.tag_bind(self.polygon, "<Button-2>", self.click_right)
else:
self.map_widget.canvas.tag_bind(self.polygon, "<Button-3>", self.click_right)
else:
self.map_widget.canvas.coords(self.polygon,
canvas_pos_x - 14, canvas_pos_y - 23,
Expand All @@ -124,6 +143,10 @@ def draw(self, event=None):
self.map_widget.canvas.tag_bind(self.big_circle, "<Enter>", self.mouse_enter)
self.map_widget.canvas.tag_bind(self.big_circle, "<Leave>", self.mouse_leave)
self.map_widget.canvas.tag_bind(self.big_circle, "<Button-1>", self.click)
if sys.platform == "darwin":
self.map_widget.canvas.tag_bind(self.big_circle, "<Button-2>", self.click_right)
else:
self.map_widget.canvas.tag_bind(self.big_circle, "<Button-3>", self.click_right)
else:
self.map_widget.canvas.coords(self.big_circle,
canvas_pos_x - 14, canvas_pos_y - 45,
Expand All @@ -141,6 +164,10 @@ def draw(self, event=None):
self.map_widget.canvas.tag_bind(self.canvas_text, "<Enter>", self.mouse_enter)
self.map_widget.canvas.tag_bind(self.canvas_text, "<Leave>", self.mouse_leave)
self.map_widget.canvas.tag_bind(self.canvas_text, "<Button-1>", self.click)
if sys.platform == "darwin":
self.map_widget.canvas.tag_bind(self.canvas_text, "<Button-2>", self.click_right)
else:
self.map_widget.canvas.tag_bind(self.canvas_text, "<Button-3>", self.click_right)
else:
self.map_widget.canvas.coords(self.canvas_text, canvas_pos_x, canvas_pos_y - 56)
self.map_widget.canvas.itemconfig(self.canvas_text, text=self.text)
Expand Down
6 changes: 6 additions & 0 deletions tkintermapview/map_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,12 @@ def convert_canvas_coords_to_decimal_coords(self, canvas_x: int, canvas_y: int)
def mouse_right_click(self, event):
coordinate_mouse_pos = self.convert_canvas_coords_to_decimal_coords(event.x, event.y)

# check if mouse is over a marker with menu
for m in self.canvas_marker_list + self.canvas_path_list + self.canvas_polygon_list:
if m.mouse_over and m.menu:
# don't show right click menu of map
return

def click_coordinates_event():
try:
pyperclip.copy(f"{coordinate_mouse_pos[0]:.7f} {coordinate_mouse_pos[1]:.7f}")
Expand Down