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

qt4_editor formlayout now works with colour tuples (fixes Issue #1690) #2017

Merged
merged 1 commit into from May 24, 2013
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 15 additions & 2 deletions lib/matplotlib/backends/qt4_editor/formlayout.py
Expand Up @@ -125,6 +125,19 @@ def text_to_qcolor(text):
color.setNamedColor(text)
return color

def is_matplotlib_color(value):
"""
Check if value is a color passed to us from matplotlib.
It could either be a valid color string or a 3-tuple of floats between 0. and 1.
"""
if text_to_qcolor(value).isValid():
return True
if isinstance(value,tuple) and len(value)==3 and all(map(lambda v: isinstance(v,float),value)):
for c in value:
if c < 0. or c > 1.:
return False
return True
return False

class ColorLayout(QHBoxLayout):
"""Color-specialized QLineEdit layout"""
Expand Down Expand Up @@ -268,7 +281,7 @@ def setup(self):
continue
elif tuple_to_qfont(value) is not None:
field = FontLayout(value, self)
elif text_to_qcolor(value).isValid():
elif is_matplotlib_color(value):
field = ColorLayout(QColor(value), self)
elif isinstance(value, (str, unicode)):
field = QLineEdit(value, self)
Expand Down Expand Up @@ -329,7 +342,7 @@ def get(self):
continue
elif tuple_to_qfont(value) is not None:
value = field.get_font()
elif isinstance(value, (str, unicode)):
elif isinstance(value, (str, unicode)) or is_matplotlib_color(value):
value = unicode(field.text())
elif isinstance(value, (list, tuple)):
index = int(field.currentIndex())
Expand Down