From 0ecc70272452be72bfd2c39fa1b62f2e25278f5c Mon Sep 17 00:00:00 2001 From: drevicko Date: Thu, 16 May 2013 18:12:28 +1000 Subject: [PATCH] qt4_editor formlayout now works with colour tuples --- .../backends/qt4_editor/formlayout.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/backends/qt4_editor/formlayout.py b/lib/matplotlib/backends/qt4_editor/formlayout.py index eda292280677..2a7424f8f7af 100644 --- a/lib/matplotlib/backends/qt4_editor/formlayout.py +++ b/lib/matplotlib/backends/qt4_editor/formlayout.py @@ -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""" @@ -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) @@ -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())