Skip to content
Snippets Groups Projects
Commit 9ef0cc8c authored by Harry Saunders's avatar Harry Saunders
Browse files

Cleanup and rename files/variables for consistency

parent e975cf3a
No related branches found
No related tags found
No related merge requests found
......@@ -27,16 +27,15 @@ class AxesTabWidgetPresenter:
# Add axes names to "select axes" combo box
self.populate_select_axes_combo_box()
# Display top axes' properties in input fields
self.set_selected_ax_view_properties()
self.update_view()
# Signals
self.view.select_axes_combo_box.currentIndexChanged.connect(
self.set_selected_ax_view_properties
)
self.update_view)
def apply_properties(self):
"""Update the axes with the user inputted properties"""
ax = self.get_current_ax()
ax = self.get_selected_ax()
new_props = self.view.get_properties()
self.set_ax_title(ax, new_props.title)
ax.set_xlim(new_props.xlim)
......@@ -46,17 +45,17 @@ class AxesTabWidgetPresenter:
ax.set_ylabel(new_props.ylabel)
ax.set_yscale(new_props.yscale)
def get_current_ax(self):
"""Get Axes object of currently selected axes"""
def get_selected_ax(self):
"""Get Axes object of selected axes"""
return self.axes_names_dict[self.view.get_selected_ax_name()]
def get_current_ax_name(self):
"""Get name of currently selected axes"""
def get_selected_ax_name(self):
"""Get name of selected axes"""
return self.view.get_selected_ax_name()
def get_current_ax_properties(self):
"""Get axes properties from currently selected axes"""
return AxProperties.from_ax_object(self.get_current_ax())
def get_selected_ax_properties(self):
"""Get axes properties from selected axes"""
return AxProperties.from_ax_object(self.get_selected_ax())
def populate_select_axes_combo_box(self):
"""Add axes' names to the 'select axes' combo box"""
......@@ -65,21 +64,29 @@ class AxesTabWidgetPresenter:
key=lambda x: x[x.rfind("("):])
self.view.populate_select_axes_combo_box(names)
def rename_current_axes(self, new_name):
def rename_selected_axes(self, new_name):
"""
Rename the current axes, updating the axes_names_dict and
Rename the selected axes, updating the axes_names_dict and
the select_axes_combo_box
"""
old_name = self.get_current_ax_name()
self.view.set_current_axes_selector_text(new_name)
old_name = self.get_selected_ax_name()
self.view.set_selected_axes_selector_text(new_name)
self.axes_names_dict[new_name] = self.axes_names_dict.pop(old_name)
def set_ax_title(self, ax, new_title):
"""Set axes' title and update its entry in the axes selector"""
ax.set_title(new_title)
self.rename_current_axes(generate_ax_name(ax))
self.rename_selected_axes(generate_ax_name(ax))
def set_selected_ax_view_properties(self):
def update_view(self):
"""Update the properties in the view from the selected axes"""
ax_props = self.get_current_ax_properties()
self.view.set_properties(ax_props)
ax_props = self.get_selected_ax_properties()
self.view.set_title(ax_props.title)
self.view.set_xlower_limit(ax_props.xlim[0])
self.view.set_xupper_limit(ax_props.xlim[1])
self.view.set_xlabel(ax_props.xlabel)
self.view.set_xscale(ax_props.xscale)
self.view.set_ylower_limit(ax_props.ylim[0])
self.view.set_yupper_limit(ax_props.ylim[1])
self.view.set_ylabel(ax_props.ylabel)
self.view.set_yscale(ax_props.yscale)
......@@ -27,16 +27,20 @@ class AxesTabWidgetPresenterTest(unittest.TestCase):
cls.fig = figure()
ax = cls.fig.add_subplot(211)
ax.plot([0, 1], [10, 12], 'rx')
ax.set_title("My Axes \nnewline $\\mu$")
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_yscale('Log')
cls.title = "My Axes \nnewline $\\mu$"
ax.set_title(cls.title)
cls.x_label = 'X'
ax.set_xlabel(cls.x_label)
cls.x_scale = 'Linear'
cls.y_label = 'Y'
ax.set_ylabel(cls.y_label)
cls.y_scale = 'Log'
ax.set_yscale(cls.y_scale)
ax2 = cls.fig.add_subplot(212)
ax2.plot([-1000000, 10000], [10, 12], 'rx')
def _generate_presenter(self):
mock_view = mock.MagicMock()
mock_view.get_selected_ax_name.return_value = "My Axes: (0, 0)"
mock_view = mock.Mock(get_selected_ax_name=lambda: "My Axes: (0, 0)")
return Presenter(self.fig, view=mock_view)
def test_generate_ax_name_returns_correct_name(self):
......@@ -48,7 +52,7 @@ class AxesTabWidgetPresenterTest(unittest.TestCase):
def test_apply_properties_calls_setters_with_correct_properties(self):
ax_mock = mock.MagicMock()
presenter = self._generate_presenter()
with mock.patch.object(presenter, 'get_current_ax', lambda: ax_mock):
with mock.patch.object(presenter, 'get_selected_ax', lambda: ax_mock):
presenter.apply_properties()
# Mock properties object and view then test that the view's setters
# are called with the correct property values
......@@ -76,17 +80,17 @@ class AxesTabWidgetPresenterTest(unittest.TestCase):
"(1, 0)": self.fig.get_axes()[1]}
self.assertEqual(expected, actual_dict)
def test_get_current_ax(self):
def test_get_selected_ax(self):
presenter = self._generate_presenter()
self.assertEqual(self.fig.get_axes()[0], presenter.get_current_ax())
self.assertEqual(self.fig.get_axes()[0], presenter.get_selected_ax())
def test_get_current_ax_name(self):
def test_get_selected_ax_name(self):
presenter = self._generate_presenter()
self.assertEqual("My Axes: (0, 0)", presenter.get_current_ax_name())
self.assertEqual("My Axes: (0, 0)", presenter.get_selected_ax_name())
def test_get_current_ax_properties(self):
def test_get_selected_ax_properties(self):
presenter = self._generate_presenter()
actual_props = presenter.get_current_ax_properties()
actual_props = presenter.get_selected_ax_properties()
self.assertIsInstance(actual_props, AxProperties)
self.assertEqual("My Axes \\nnewline $\\\\mu$", actual_props.title)
self.assertEqual('X', actual_props.xlabel)
......@@ -101,18 +105,18 @@ class AxesTabWidgetPresenterTest(unittest.TestCase):
["My Axes: (0, 0)", "(1, 0)"]
)
def test_rename_current_axes_calls_set_current_axes_selector_text(self):
def test_rename_selected_axes_calls_set_selected_axes_selector_text(self):
presenter = self._generate_presenter()
new_name = "New Axes Name: (0, 0)"
presenter.rename_current_axes(new_name)
presenter.view.set_current_axes_selector_text.assert_called_once_with(
presenter.rename_selected_axes(new_name)
presenter.view.set_selected_axes_selector_text.assert_called_once_with(
new_name
)
def test_rename_current_axes_updates_axes_names_dict(self):
def test_rename_selected_axes_updates_axes_names_dict(self):
presenter = self._generate_presenter()
new_name = "New Axes Name: (0, 0)"
presenter.rename_current_axes(new_name)
presenter.rename_selected_axes(new_name)
self.assertEqual(presenter.axes_names_dict,
{new_name: self.fig.get_axes()[0],
"(1, 0)": self.fig.get_axes()[1]})
......@@ -120,7 +124,23 @@ class AxesTabWidgetPresenterTest(unittest.TestCase):
def test_set_ax_title_sets_title_and_updates_axes_names_dict(self):
presenter = self._generate_presenter()
new_title = "New Title"
presenter.set_ax_title(self.fig.get_axes()[0], new_title)
self.assertIn((new_title + ": (0, 0)", self.fig.get_axes()[0]),
ax = self.fig.get_axes()[1]
presenter.set_ax_title(ax, new_title)
self.assertIn((new_title + ": (1, 0)", ax),
get_axes_names_dict(self.fig).items())
self.assertEqual(new_title, self.fig.get_axes()[0].title.get_text())
self.assertEqual(new_title, ax.title.get_text())
def test_update_view_calls_correct_setters_with_correct_values(self):
presenter = self._generate_presenter()
new_view_mock = mock.Mock(get_selected_ax_name=lambda: "My Axes: (0, 0)")
ax = self.fig.get_axes()[0]
setters = ['set_title', 'set_xlower_limit', 'set_xupper_limit',
'set_ylower_limit', 'set_yupper_limit', 'set_xlabel',
'set_ylabel', 'set_xscale', 'set_yscale']
expected_vals = [self.title.encode('unicode_escape'), ax.get_xlim()[0],
ax.get_xlim()[1], ax.get_ylim()[0], ax.get_ylim()[1],
self.x_label, self.y_label, self.x_scale, self.y_scale]
with mock.patch.object(presenter, 'view', new_view_mock):
presenter.update_view()
for setter, value in zip(setters, expected_vals):
getattr(new_view_mock, setter).assert_called_once_with(value)
......@@ -8,7 +8,7 @@
from __future__ import (absolute_import, unicode_literals)
import numpy as np
from numpy import finfo, float32
from qtpy.QtCore import Qt
from qtpy.QtWidgets import QWidget
......@@ -30,16 +30,23 @@ class AxesTabWidgetView(QWidget):
for axis in ['x', 'y']:
for limit in ['upper', 'lower']:
spin_box = getattr(self, '%s%s_limit_spin_box' % (axis, limit))
spin_box.setRange(np.finfo(np.float32).min,
np.finfo(np.float32).max)
spin_box.setRange(finfo(float32).min,
finfo(float32).max)
def populate_select_axes_combo_box(self, axes_names):
self.select_axes_combo_box.addItems(axes_names)
def set_current_axes_selector_text(self, new_text):
def set_selected_axes_selector_text(self, new_text):
"""Replace the text of the selected item in the combo box"""
current_index = self.select_axes_combo_box.currentIndex()
self.select_axes_combo_box.setItemText(current_index, new_text)
def get_selected_ax_name(self):
return self.select_axes_combo_box.currentText()
def get_properties(self):
return AxProperties.from_view(self)
def get_title(self):
return self.axes_title_line_edit.text()
......@@ -97,20 +104,3 @@ class AxesTabWidgetView(QWidget):
def set_yscale(self, scale):
self.yscale_combo_box.setCurrentText(scale.title())
def get_selected_ax_name(self):
return self.select_axes_combo_box.currentText()
def set_properties(self, ax_props):
self.set_title(ax_props.title)
self.set_xlower_limit(ax_props.xlim[0])
self.set_xupper_limit(ax_props.xlim[1])
self.set_xlabel(ax_props.xlabel)
self.set_xscale(ax_props.xscale)
self.set_ylower_limit(ax_props.ylim[0])
self.set_yupper_limit(ax_props.ylim[1])
self.set_ylabel(ax_props.ylabel)
self.set_yscale(ax_props.yscale)
def get_properties(self):
return AxProperties.from_view(self)
......@@ -32,8 +32,7 @@ class CurvePropertiesTest(unittest.TestCase):
'markeredgecolor': 'g',
'markeredgewidth': 0.4,
'markerfacecolor': 'k',
'visible': False
}
'visible': False}
fig0 = figure()
ax0 = fig0.add_subplot(211)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment