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

Fix bug in view (q)settings handling #1705

Merged
merged 2 commits into from
Mar 22, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
49 changes: 12 additions & 37 deletions mslib/msui/linearview.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
limitations under the License.
"""

from mslib.utils.config import config_loader, save_settings_qsettings, load_settings_qsettings
from mslib.utils.config import config_loader
from PyQt5 import QtGui, QtWidgets
from mslib.utils.qt import ui_linearview_window as ui
from mslib.utils.qt import ui_linearview_options as ui_opt
Expand All @@ -42,7 +42,7 @@ class MSUI_LV_Options_Dialog(QtWidgets.QDialog, ui_opt.Ui_LinearViewOptionsDialo
Dialog class to specify Linear View Options.
"""

def __init__(self, parent=None, settings_dict=None):
def __init__(self, parent=None, settings=None):
"""
Arguments:
parent -- Qt widget that is parent to this widget.
Expand All @@ -51,33 +51,26 @@ def __init__(self, parent=None, settings_dict=None):
super(MSUI_LV_Options_Dialog, self).__init__(parent)
self.setupUi(self)

default_settings_dict = {
"plot_title_size": "default",
"axes_label_size": "default"
}

if settings_dict is not None:
default_settings_dict.update(settings_dict)
settings_dict = default_settings_dict
assert settings is not None

for i in range(self.lv_cbtitlesize.count()):
if self.lv_cbtitlesize.itemText(i) == settings_dict["plot_title_size"]:
if self.lv_cbtitlesize.itemText(i) == settings["plot_title_size"]:
self.lv_cbtitlesize.setCurrentIndex(i)

for i in range(self.lv_cbaxessize.count()):
if self.lv_cbaxessize.itemText(i) == settings_dict["axes_label_size"]:
if self.lv_cbaxessize.itemText(i) == settings["axes_label_size"]:
self.lv_cbaxessize.setCurrentIndex(i)

def get_settings(self):
"""
Returns the specified settings from the GUI elements.
"""
settings_dict = {
settings = {
"plot_title_size": self.lv_cbtitlesize.currentText(),
"axes_label_size": self.lv_cbaxessize.currentText()
}

return settings_dict
return settings


class MSUILinearViewWindow(MSUIMplViewWindow, ui.Ui_LinearWindow):
Expand All @@ -101,15 +94,12 @@ def __init__(self, parent=None, model=None, _id=None):

self.setFlightTrackModel(model)

self.settings_tag = "linearview"
self.load_settings()

# Connect slots and signals.
# ==========================

# Tool opener.
self.cbTools.currentIndexChanged.connect(self.openTool)
self.lvoptionbtn.clicked.connect(self.set_options)
self.lvoptionbtn.clicked.connect(self.open_settings_dialog)

self.openTool(WMS + 1)

Expand Down Expand Up @@ -147,26 +137,11 @@ def setFlightTrackModel(self, model):
if self.docks[WMS] is not None:
self.docks[WMS].widget().setFlightTrackModel(model)

def set_options(self):
settings = self.getView().plotter.get_settings()
dlg = MSUI_LV_Options_Dialog(parent=self, settings_dict=settings)
def open_settings_dialog(self):
settings = self.getView().get_settings()
dlg = MSUI_LV_Options_Dialog(parent=self, settings=settings)
dlg.setModal(True)
if dlg.exec_() == QtWidgets.QDialog.Accepted:
settings = dlg.get_settings()
self.getView().plotter.set_settings(settings)
self.save_settings()
self.getView().plotter.set_settings(settings, save=True)
dlg.destroy()

def save_settings(self):
"""
Save the current settings of plot options to the file self.settingsfile.
"""
settings = self.getView().plotter.get_settings()
save_settings_qsettings(self.settings_tag, settings)

def load_settings(self):
"""
Load settings from the file self.settingsfile.
"""
settings = load_settings_qsettings(self.settings_tag)
self.getView().plotter.set_settings(settings)
136 changes: 73 additions & 63 deletions mslib/msui/mpl_qtwidget.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@

from mslib.utils.thermolib import convert_pressure_to_vertical_axis_measure
from mslib.utils import thermolib, FatalUserError
from mslib.utils.config import config_loader
from mslib.utils.config import config_loader, save_settings_qsettings, load_settings_qsettings
from mslib.utils.units import units
from mslib.msui import mpl_pathinteractor as mpl_pi
from mslib.msui import mpl_map
Expand All @@ -56,12 +56,51 @@

matplotlib.rcParams['savefig.directory'] = LAST_SAVE_DIRECTORY

_DEFAULT_SETTINGS_TOPVIEW = {
"draw_graticule": True,
"draw_coastlines": True,
"fill_waterbodies": True,
"fill_continents": True,
"draw_flighttrack": True,
"draw_marker": True,
"label_flighttrack": True,
"tov_plot_title_size": "default",
"tov_axes_label_size": "default",
"colour_water": ((153 / 255.), (255 / 255.), (255 / 255.), (255 / 255.)),
"colour_land": ((204 / 255.), (153 / 255.), (102 / 255.), (255 / 255.)),
"colour_ft_vertices": (0, 0, 1, 1),
"colour_ft_waypoints": (1, 0, 0, 1)}

_DEFAULT_SETTINGS_SIDEVIEW = {
"vertical_extent": (1050, 180),
"vertical_axis": "pressure",
"secondary_axis": "no secondary axis",
"plot_title_size": "default",
"axes_label_size": "default",
"flightlevels": [300],
"draw_flightlevels": True,
"draw_flighttrack": True,
"fill_flighttrack": True,
"label_flighttrack": True,
"draw_verticals": True,
"draw_marker": True,
"draw_ceiling": True,
"colour_ft_vertices": (0, 0, 1, 1),
"colour_ft_waypoints": (1, 0, 0, 1),
"colour_ft_fill": (0, 0, 1, 0.15),
"colour_ceiling": (0, 0, 1, 0.15)}

_DEFAULT_SETTINGS_LINEARVIEW = {
"plot_title_size": "default",
"axes_label_size": "default"}


class ViewPlotter:
def __init__(self, fig=None, ax=None):
def __init__(self, fig=None, ax=None, settings_tag=None, settings=None):
# setup Matplotlib Figure and Axis
self.fig, self.ax = fig, ax
self.settings = {}
self.settings = settings
self.settings_tag = settings_tag
if self.fig is None:
assert ax is None
self.fig = figure.Figure(facecolor="w") # 0.75
Expand Down Expand Up @@ -115,19 +154,27 @@ def get_settings(self):
"""
return self.settings

def set_settings(self, settings):
def set_settings(self, settings, save=False):
"""Update local settings influencing the plotting

Args:
settings (dict): Dictionary of string/value pairs
"""
if settings is not None:
self.settings.update(settings)
if save:
self.save_settings()

def load_settings(self):
self.settings = load_settings_qsettings(self.settings_tag, self.settings)

def save_settings(self):
save_settings_qsettings(self.settings_tag, self.settings)


class TopViewPlotter(ViewPlotter):
def __init__(self, fig=None, ax=None):
super().__init__(fig, ax)
def __init__(self, fig=None, ax=None, settings=None):
super().__init__(fig, ax, settings_tag="topview", settings=_DEFAULT_SETTINGS_TOPVIEW)
self.map = None
self.legimg = None
self.legax = None
Expand All @@ -136,20 +183,8 @@ def __init__(self, fig=None, ax=None):
self.tov_als = None
# Sets the default fontsize parameters' values for topview from MSSDefaultConfig.
self.topview_size_settings = config_loader(dataset="topview")
# logging.debug("applying map appearance settings %s." % settings)
self.set_settings({"draw_graticule": True,
"draw_coastlines": True,
"fill_waterbodies": True,
"fill_continents": True,
"draw_flighttrack": True,
"draw_marker": True,
"label_flighttrack": True,
"tov_plot_title_size": "default",
"tov_axes_label_size": "default",
"colour_water": ((153 / 255.), (255 / 255.), (255 / 255.), (255 / 255.)),
"colour_land": ((204 / 255.), (153 / 255.), (102 / 255.), (255 / 255.)),
"colour_ft_vertices": (0, 0, 1, 1),
"colour_ft_waypoints": (1, 0, 0, 1)})
self.load_settings()
self.set_settings(settings)
self.ax.figure.canvas.draw()

def init_map(self, **kwargs):
Expand Down Expand Up @@ -196,11 +231,11 @@ def clear_figure(self):
self.ax.set_title("Top view", horizontalalignment="left", x=0)
self.ax.figure.canvas.draw()

def set_settings(self, settings):
def set_settings(self, settings, save=False):
"""Apply settings from dictionary 'settings' to the view.
If settings is None, apply default settings.
"""
super().set_settings(settings)
super().set_settings(settings, save)

# Stores the exact value of fontsize for topview plot title size(tov_pts)
self.tov_pts = (self.topview_size_settings["plot_title_size"]
Expand Down Expand Up @@ -327,26 +362,8 @@ def __init__(self, fig=None, ax=None, settings=None, numlabels=None, num_interpo
numlabels = config_loader(dataset='num_labels')
if num_interpolation_points is None:
num_interpolation_points = config_loader(dataset='num_interpolation_points')
super().__init__(fig, ax)

# Default settings.
self.set_settings({"vertical_extent": (1050, 180),
"vertical_axis": "pressure",
"secondary_axis": "no secondary axis",
"plot_title_size": "default",
"axes_label_size": "default",
"flightlevels": [],
"draw_flightlevels": True,
"draw_flighttrack": True,
"fill_flighttrack": True,
"label_flighttrack": True,
"draw_verticals": True,
"draw_marker": True,
"draw_ceiling": True,
"colour_ft_vertices": (0, 0, 1, 1),
"colour_ft_waypoints": (1, 0, 0, 1),
"colour_ft_fill": (0, 0, 1, 0.15),
"colour_ceiling": (0, 0, 1, 0.15)})
super().__init__(fig, ax, settings_tag="sideview", settings=_DEFAULT_SETTINGS_SIDEVIEW)
self.load_settings()
self.set_settings(settings)

self.numlabels = numlabels
Expand Down Expand Up @@ -604,17 +621,19 @@ class LinearViewPlotter(ViewPlotter):
flight track / list of waypoints.
"""

def __init__(self, model=None, numlabels=None):
def __init__(self, model=None, numlabels=None, settings=None):
"""
Arguments:
model -- WaypointsTableModel defining the linear section.
"""
if numlabels is None:
numlabels = config_loader(dataset='num_labels')
super().__init__()
super().__init__(settings_tag="linearview", settings=_DEFAULT_SETTINGS_LINEARVIEW)
self.load_settings()

self.settings = {"plot_title_size": "default",
"axes_label_size": "default"}
# Sets the default values of plot sizes from MissionSupportDefaultConfig.
self.linearview_size_settings = config_loader(dataset="linearview")
self.set_settings(settings)

# Setup the plot.
self.numlabels = numlabels
Expand All @@ -624,8 +643,6 @@ def __init__(self, model=None, numlabels=None):
self.waypoints_model = None
self.vertical_lines = []
self.basename = "linearview"
# Sets the default values of plot sizes from MissionSupportDefaultConfig.
self.linearview_size_settings = config_loader(dataset="linearview")

def setup_linear_view(self):
"""Set up a linear section view.
Expand Down Expand Up @@ -708,12 +725,12 @@ def draw_image(self, xmls, colors=None, scales=None):

par.yaxis.label.set_color(color.replace("0x", "#"))

def set_settings(self, settings):
def set_settings(self, settings, save=False):
"""
Apply settings from options ui to the linear view
"""

super().set_settings(settings)
super().set_settings(settings, save)

pts = (self.linearview_size_settings["plot_title_size"] if self.settings["plot_title_size"] == "default"
else int(self.settings["plot_title_size"]))
Expand Down Expand Up @@ -778,11 +795,11 @@ def get_plot_size_in_px(self):
def get_settings(self):
return self.plotter.get_settings()

def set_settings(self, settings):
def set_settings(self, settings, save=False):
"""
Apply settings from options ui to the linear view
"""
self.plotter.set_settings(settings)
self.plotter.set_settings(settings, save)


def _getSaveFileName(parent, title="Choose a filename to save to", filename="test.png",
Expand Down Expand Up @@ -1251,12 +1268,12 @@ def update_ceiling(self, visible, color):
line.set_visible(visible)
self.draw()

def set_settings(self, settings):
def set_settings(self, settings, save=False):
"""Apply settings to view.
"""
old_vertical_lines = self.plotter.settings["draw_verticals"]
if settings is not None:
self.plotter.set_settings(settings)
self.plotter.set_settings(settings, save)
settings = self.plotter.get_settings()
self.set_flight_levels(settings["flightlevels"])
self.set_flight_levels_visible(settings["draw_flightlevels"])
Expand Down Expand Up @@ -1362,9 +1379,6 @@ def __init__(self, model=None, numlabels=None):
self.plotter = LinearViewPlotter()
super(MplLinearViewCanvas, self).__init__(self.plotter)

settings = {"plot_title_size": "default",
"axes_label_size": "default"}

# Setup the plot.
self.numlabels = numlabels
self.plotter.setup_linear_view()
Expand All @@ -1377,10 +1391,6 @@ def __init__(self, model=None, numlabels=None):
if model:
self.set_waypoints_model(model)

# Sets the default values of plot sizes from MissionSupportDefaultConfig.
self.linearview_size_settings = config_loader(dataset="linearview")
self.plotter.set_settings(settings)

def set_waypoints_model(self, model):
"""Set the WaypointsTableModel defining the linear section.
If no model had been set before, create a new interactor object on the model
Expand Down Expand Up @@ -1629,12 +1639,12 @@ def plot_multiple_flightpath(self, multiple_flightpath):
"""
self.multiple_flightpath = multiple_flightpath

def set_settings(self, settings):
def set_settings(self, settings, save=False):
"""Apply settings from dictionary 'settings_dict' to the view.

If settings is None, apply default settings.
"""
self.plotter.set_settings(settings)
self.plotter.set_settings(settings, save)
settings = self.get_settings()
if self.waypoints_interactor is not None:
wpi_plotter = self.waypoints_interactor.plotter
Expand Down
7 changes: 5 additions & 2 deletions mslib/msui/multiple_flightpath_dockwidget.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,11 @@ def __init__(self, parent=None, view=None, listFlightTracks=None,
self.listFlightTracks = listFlightTracks
self.mscolab_server_url = mscolab_server_url
self.token = token
self.ft_settings_dict = self.ui.get_settings()
self.color = (0, 0, 1, 1)
if self.ui is not None:
ft_settings_dict = self.ui.getView().get_settings()
self.color = ft_settings_dict["colour_ft_vertices"]
else:
self.color = (0, 0, 1, 1)
self.obb = []

self.operation_list = False
Expand Down
Loading