Skip to content

Commit

Permalink
add optional marker menu
Browse files Browse the repository at this point in the history
A custom right click menu can be added to a marker with

myMenu = {
  # key is the title and value is the function to call
  'My fancy marker menu entry 1': function1,
  # separators can also be added
  '-': None,
  'And a second entry after the separator': function2
}
map_widget.set_marker(x, y, menu=myMenu)
  • Loading branch information
danielroedl committed Jun 14, 2022
1 parent eb84f60 commit 3ce10d6
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
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:
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

0 comments on commit 3ce10d6

Please sign in to comment.