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

Legend background and foreground colors can be modified #2146

Merged
merged 6 commits into from
May 26, 2020
Merged
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
4 changes: 2 additions & 2 deletions glue/viewers/histogram/qt/options_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ def __init__(self, viewer_state, session, parent=None):

self._connections = autoconnect_callbacks_to_qt(viewer_state, self.ui)
self._connections_axes = autoconnect_callbacks_to_qt(viewer_state, self.ui.axes_editor.ui)
connect_kwargs = {'legend_alpha': dict(value_range=(0, 1))}
self._connections_legend = autoconnect_callbacks_to_qt(viewer_state, self.ui.legend_editor.ui, connect_kwargs)
connect_kwargs = {'alpha': dict(value_range=(0, 1))}
self._connections_legend = autoconnect_callbacks_to_qt(viewer_state.legend, self.ui.legend_editor.ui, connect_kwargs)

self.viewer_state = viewer_state

Expand Down
2 changes: 1 addition & 1 deletion glue/viewers/histogram/qt/tests/test_data_viewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -672,7 +672,7 @@ def test_legend(self):
viewer_state = self.viewer.state

self.viewer.add_data(self.data)
self.viewer.state.show_legend = True
self.viewer.state.legend.visible = True

handles, labels, handler_dict = self.viewer.get_handles_legend()
assert len(handles) == 1
Expand Down
4 changes: 2 additions & 2 deletions glue/viewers/histogram/qt/tests/test_python_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def test_simple_visual(self, tmpdir):
self.assert_same(tmpdir)

def test_simple_visual_legend(self, tmpdir):
self.viewer.state.show_legend = True
self.viewer.state.legend.visible = True
self.viewer.state.layers[0].color = 'blue'
self.viewer.state.layers[0].alpha = 0.5
self.assert_same(tmpdir)
Expand All @@ -51,7 +51,7 @@ def test_subset(self, tmpdir):
self.assert_same(tmpdir)

def test_subset_legend(self, tmpdir):
self.viewer.state.show_legend = True
self.viewer.state.legend.visible = True
self.data_collection.new_subset_group('mysubset', self.data.id['a'] > 0.5)
self.assert_same(tmpdir)

Expand Down
4 changes: 2 additions & 2 deletions glue/viewers/image/qt/options_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ def __init__(self, viewer_state, session, parent=None):

self._connections = autoconnect_callbacks_to_qt(viewer_state, self.ui)
self._connections_axes = autoconnect_callbacks_to_qt(viewer_state, self.ui.axes_editor.ui)
connect_kwargs = {'legend_alpha': dict(value_range=(0, 1))}
self._connections_legend = autoconnect_callbacks_to_qt(viewer_state, self.ui.legend_editor.ui, connect_kwargs)
connect_kwargs = {'alpha': dict(value_range=(0, 1))}
self._connections_legend = autoconnect_callbacks_to_qt(viewer_state.legend, self.ui.legend_editor.ui, connect_kwargs)

self.viewer_state = viewer_state

Expand Down
2 changes: 1 addition & 1 deletion glue/viewers/image/qt/tests/test_data_viewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -726,7 +726,7 @@ def test_legend(self):

viewer_state = self.viewer.state
self.viewer.add_data(self.image1)
self.viewer.state.show_legend = True
self.viewer.state.legend.visible = True

handles, labels, handler_dict = self.viewer.get_handles_legend()
assert len(handles) == 1
Expand Down
4 changes: 2 additions & 2 deletions glue/viewers/image/qt/tests/test_python_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def test_simple_att(self, tmpdir):
self.assert_same(tmpdir)

def test_simple_visual(self, tmpdir):
self.viewer.state.show_legend = True
self.viewer.state.legend.visible = True
self.viewer.state.layers[0].cmap = plt.cm.RdBu
self.viewer.state.layers[0].v_min = 0.2
self.viewer.state.layers[0].v_max = 0.8
Expand All @@ -72,7 +72,7 @@ def test_subset(self, tmpdir):
self.assert_same(tmpdir)

def test_subset_legend(self, tmpdir):
self.viewer.state.show_legend = True
self.viewer.state.legend.visible = True
self.data_collection.new_subset_group('mysubset', self.data.id['cube'] > 0.5)
self.assert_same(tmpdir, tol=0.15) # transparency and such

Expand Down
101 changes: 78 additions & 23 deletions glue/viewers/matplotlib/qt/legend_editor.ui
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,29 @@
<property name="bottomMargin">
<number>5</number>
</property>
<item row="1" column="0">
<widget class="QLabel" name="label_3">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>enable</string>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QCheckBox" name="bool_visible">
<property name="text">
<string/>
</property>
</widget>
</item>
<item row="3" column="0">
<widget class="QLabel" name="label">
<property name="text">
Expand All @@ -43,7 +66,7 @@
</widget>
</item>
<item row="3" column="1">
<widget class="QComboBox" name="combosel_legend_location"/>
<widget class="QComboBox" name="combosel_location"/>
</item>
<item row="5" column="0">
<widget class="QLabel" name="label_2">
Expand All @@ -55,35 +78,31 @@
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QCheckBox" name="bool_show_legend">
<property name="text">
<string/>
<item row="5" column="1">
<widget class="QSlider" name="value_alpha">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label_3">
<item row="7" column="0">
<widget class="QLabel" name="label_5">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>enable</string>
<string>title</string>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</property>
</widget>
</item>
<item row="5" column="1">
<widget class="QSlider" name="value_legend_alpha">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</widget>
<item row="7" column="1">
<widget class="QLineEdit" name="text_title"/>
</item>
<item row="8" column="0">
<widget class="QLabel" name="label_4">
Expand All @@ -96,29 +115,65 @@
</widget>
</item>
<item row="8" column="1">
<widget class="QSpinBox" name="value_legend_fontsize"/>
<widget class="QSpinBox" name="value_fontsize"/>
</item>
<item row="7" column="1">
<widget class="QLineEdit" name="text_legend_title"/>
<item row="9" column="0">
<widget class="QLabel" name="label_6">
<property name="text">
<string>box color</string>
</property>
</widget>
</item>
<item row="7" column="0">
<widget class="QLabel" name="label_5">
<item row="9" column="1">
<widget class="QColorBox" name="color_frame_color">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Preferred">
<sizepolicy hsizetype="Preferred" vsizetype="Minimum">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>title</string>
<string/>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</widget>
</item>
<item row="10" column="1">
<widget class="QCheckBox" name="bool_show_edge">
<property name="text">
<string/>
</property>
</widget>
</item>
<item row="11" column="0">
<widget class="QLabel" name="label_7">
<property name="text">
<string>text color</string>
</property>
</widget>
</item>
<item row="11" column="1">
<widget class="QColorBox" name="color_text_color">
<property name="text">
<string/>
</property>
</widget>
</item>
<item row="10" column="0">
<widget class="QLabel" name="label_8">
<property name="text">
<string>box edge</string>
</property>
</widget>
</item>
</layout>
</widget>
<customwidgets>
<customwidget>
<class>QColorBox</class>
<extends>QLabel</extends>
<header>glue.utils.qt.colors</header>
</customwidget>
</customwidgets>
<resources/>
<connections/>
</ui>
6 changes: 3 additions & 3 deletions glue/viewers/matplotlib/qt/tests/test_data_viewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -625,7 +625,7 @@ def test_legend(self):
# no legend by default
assert self.viewer.axes.get_legend() is None

self.viewer.state.show_legend = True
self.viewer.state.legend.visible = True

# a legend appears
legend = self.viewer.axes.get_legend()
Expand All @@ -644,15 +644,15 @@ def test_legend(self):
# The next set of test check that the legend does not create extra draws !
def test_legend_single_draw(self):
# Make sure that the number of draws is kept to a minimum
self.viewer.show_legend = True
self.viewer.state.legend.visible = True
self.init_draw_count()
self.init_subset()
assert self.draw_count == 0
self.viewer.add_data(self.data)
assert self.draw_count == 1

def test_legend_numerical_data_changed(self):
self.viewer.show_legend = True
self.viewer.state.legend.visible = True
self.init_draw_count()
self.init_subset()
assert self.draw_count == 0
Expand Down
78 changes: 65 additions & 13 deletions glue/viewers/matplotlib/state.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
from echo import CallbackProperty, SelectionCallbackProperty, keep_in_sync, delay_callback

from matplotlib.colors import to_rgba

from glue.core.message import LayerArtistUpdatedMessage

from glue.core.state_objects import State
from glue.viewers.common.state import ViewerState, LayerState

from glue.utils import defer_draw, avoid_circular
Expand Down Expand Up @@ -34,13 +37,70 @@ def notify(self, *args, **kwargs):

VALID_WEIGHTS = ['light', 'normal', 'medium', 'semibold', 'bold', 'heavy', 'black']

VALID_LOCATIONS = ['best',

VALID_LOCATIONS = ['draggable', 'best',
'upper right', 'upper left',
'lower left', 'lower right',
'center left', 'center right',
'lower center', 'upper center']


class MatplotlibLegendState(State):
"""The legend state"""

visible = DeferredDrawCallbackProperty(False, docstring="Whether to show the legend")

location = DeferredDrawSelectionCallbackProperty(0, docstring="The location of the legend in the axis")

title = DeferredDrawCallbackProperty("", docstring='The title of the legend')
fontsize = DeferredDrawCallbackProperty(10, docstring='The font size of the title')

alpha = DeferredDrawCallbackProperty(0.6, docstring='Transparency of the legend frame')
frame_color = DeferredDrawCallbackProperty("#ffffff", docstring='Frame color of the legend')
show_edge = DeferredDrawCallbackProperty(True, docstring="Whether to show the edge of the frame ")
text_color = DeferredDrawCallbackProperty("#000000", docstring='Text color of the legend')

def __init__(self, *args, **kwargs):
MatplotlibLegendState.location.set_choices(self, VALID_LOCATIONS)

super().__init__(*args, **kwargs)
self._set_color_choices()

def _set_color_choices(self):
from glue.config import settings

self.frame_color = settings.BACKGROUND_COLOR
self.text_color = settings.FOREGROUND_COLOR

@property
def edge_color(self):
if self.show_edge:
return to_rgba(self.text_color, self.alpha)
else:
return None

@property
def draggable(self):
return self.location == 'draggable'

@property
def mpl_location(self):
if self.location == 'draggable':
return 'best'
else:
return self.location

def update_axes_settings_from(self, state):
self.visible = state.show_legend
self.loc_and_drag = state.loc_and_drag
self.alpha = state.alpha
self.title = state.title
self.fontsize = state.fontsize
self.frame_color = state.frame_color
self.show_edge = state.show_edge
self.text_color = state.text_color


class MatplotlibDataViewerState(ViewerState):
"""
A base class that includes common attributes for viewers based on
Expand Down Expand Up @@ -72,21 +132,15 @@ class MatplotlibDataViewerState(ViewerState):
x_ticklabel_size = DeferredDrawCallbackProperty(8, docstring='Size of the x-axis tick labels')
y_ticklabel_size = DeferredDrawCallbackProperty(8, docstring='Size of the y-axis tick labels')

show_legend = DeferredDrawCallbackProperty(False, docstring="Whether to show the legend")
legend_location = DeferredDrawSelectionCallbackProperty(0, docstring="The location of the legend in the axis")
legend_alpha = DeferredDrawCallbackProperty(0.8, docstring='Transparency of the legend frame')
legend_title = DeferredDrawCallbackProperty("", docstring='Transparency of the legend frame')
legend_fontsize = DeferredDrawCallbackProperty(10, docstring='Transparency of the legend frame')

def __init__(self, *args, **kwargs):

self._axes_aspect_ratio = None

MatplotlibDataViewerState.x_axislabel_weight.set_choices(self, VALID_WEIGHTS)
MatplotlibDataViewerState.y_axislabel_weight.set_choices(self, VALID_WEIGHTS)
MatplotlibDataViewerState.legend_location.set_choices(self, VALID_LOCATIONS)

super(MatplotlibDataViewerState, self).__init__(*args, **kwargs)
self.legend = MatplotlibLegendState(*args, **kwargs)

self.add_callback('aspect', self._adjust_limits_aspect, priority=10000)
self.add_callback('x_min', self._adjust_limits_aspect_x, priority=10000)
Expand Down Expand Up @@ -181,17 +235,15 @@ def _adjust_limits_aspect(self, *args, **kwargs):
self.y_max = y_max

def update_axes_settings_from(self, state):
# axis
self.x_axislabel_size = state.x_axislabel_size
self.y_axislabel_size = state.y_axislabel_size
self.x_axislabel_weight = state.x_axislabel_weight
self.y_axislabel_weight = state.y_axislabel_weight
self.x_ticklabel_size = state.x_ticklabel_size
self.y_ticklabel_size = state.y_ticklabel_size
self.show_legend = state.show_legend
self.legend_location = state.legend_location
self.legend_alpha = state.legend_alpha
self.legend_title = state.legend_title
self.legend_fontsize = state.legend_fontsize
# legend
self.legend.update_axes_settings_from(state.legend)

@defer_draw
def _notify_global(self, *args, **kwargs):
Expand Down
Loading