Skip to content

Commit

Permalink
map: support saving and loading of map window layout
Browse files Browse the repository at this point in the history
  • Loading branch information
tridge committed Sep 30, 2018
1 parent 50e3b47 commit c17c6ef
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 1 deletion.
94 changes: 94 additions & 0 deletions MAVProxy/modules/lib/win_layout.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
#!/usr/bin/env python

from __future__ import print_function
import os, wx, pickle

'''
handle saving/loading of window positions
'''

window_list = {}
display_size = None

class WinLayout(object):
'''represent window layout'''
def __init__(self, name, pos, size, dsize):
self.name = name
self.pos = pos
self.size = size
self.dsize = dsize

class ManagedWindow(object):
'''a layout plus callback for setting window position and size'''
def __init__(self, layout, callback):
self.layout = layout
self.callback = callback

def get_wx_window_layout(wx_window):
'''get a WinLayout for a wx window'''
dsize = wx.DisplaySize()
pos = wx_window.GetPosition()
size = wx_window.GetSize()
name = wx_window.GetTitle()
return WinLayout(name, pos, size, dsize)

def set_wx_window_layout(wx_window, layout):
'''set a WinLayout for a wx window'''
try:
wx_window.SetSize(layout.size)
wx_window.SetPosition(layout.pos)
except Exception as ex:
print(ex)

def set_layout(wlayout, callback):
'''set window layout'''
global display_size
global window_list
window_list[wlayout.name] = ManagedWindow(wlayout, callback)
display_size = wlayout.dsize

def layout_filename():
'''get location of layout file'''
global display_size
(dw,dh) = display_size
if 'HOME' in os.environ:
return os.path.join(os.environ['HOME'], ".mavlayout-%ux%u" % (dw,dh))
if 'LOCALAPPDATA' in os.environ and not opts.setup:
return os.path.join(os.environ['LOCALAPPDATA'], "MAVProxy", "mavlayout-%ux%x.dat" % (dw,dh))
return None

def save_layout():
'''save window layout'''
global display_size
global window_list
if display_size is None:
print("No layouts to save")
return
fname = layout_filename()
if fname is None:
print("No file to save layout to")
return
layout = {}
for name in window_list:
layout[name] = window_list[name].layout
pickle.dump(layout, open(fname,"w"))

def load_layout():
'''load window layout'''
global display_size
global window_list
if display_size is None:
print("No layouts to load")
return
fname = layout_filename()
if fname is None:
print("No file to load layout from")
return
layout = pickle.load(open(fname,"r"))
for name in window_list:
if name in layout:
try:
window_list[name].callback(layout[name])
except Exception as ex:
print(ex)

11 changes: 11 additions & 0 deletions MAVProxy/modules/mavproxy_map/mp_slipmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from MAVProxy.modules.mavproxy_map import mp_elevation
from MAVProxy.modules.mavproxy_map import mp_tile
from MAVProxy.modules.lib import mp_util
from MAVProxy.modules.lib import win_layout
from MAVProxy.modules.mavproxy_map.mp_slipmap_util import *


Expand Down Expand Up @@ -142,10 +143,20 @@ def event_count(self):
'''return number of events waiting to be processed'''
return self.event_queue.qsize()

def set_layout(self, layout):
'''set window layout'''
self.object_queue.put(layout)

def get_event(self):
'''return next event or None'''
if self.event_queue.qsize() == 0:
return None
evt = self.event_queue.get()
while isinstance(evt, win_layout.WinLayout):
win_layout.set_layout(evt, self.set_layout)
if self.event_queue.qsize() == 0:
return None
evt = self.event_queue.get()
return self.event_queue.get()

def add_callback(self, callback):
Expand Down
12 changes: 11 additions & 1 deletion MAVProxy/modules/mavproxy_map/mp_slipmap_ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
from mp_slipmap_util import SlipFollow

from MAVProxy.modules.lib import mp_util
from MAVProxy.modules.lib import win_layout

from MAVProxy.modules.lib.mp_menu import MPMenuCheckbox
from MAVProxy.modules.lib.mp_menu import MPMenuItem
Expand All @@ -52,10 +53,11 @@ def __init__(self, state):
state.popup_started = False
state.default_popup = None
state.panel = MPSlipMapPanel(self, state)
self.last_layout_send = time.time()
self.Bind(wx.EVT_IDLE, self.on_idle)
self.Bind(wx.EVT_SIZE, state.panel.on_size)
self.legend_checkbox_menuitem_added = False

# create the View menu
self.menu = MPMenuTop([
MPMenuSubMenu('View', items=[
Expand Down Expand Up @@ -199,12 +201,20 @@ def on_idle(self, event):
if state.close_window.acquire(False):
self.state.app.ExitMainLoop()

now = time.time()
if now - self.last_layout_send > 1:
self.last_layout_send = now
state.event_queue.put(win_layout.get_wx_window_layout(self))

# receive any display objects from the parent
obj = None

while not state.object_queue.empty():
obj = state.object_queue.get()

if isinstance(obj, win_layout.WinLayout):
win_layout.set_wx_window_layout(self, obj)

if isinstance(obj, SlipObject):
self.add_object(obj)

Expand Down

0 comments on commit c17c6ef

Please sign in to comment.