3232FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
3333OTHER DEALINGS IN THE SOFTWARE.
3434"""
35- from __future__ import (absolute_import , division , print_function ,
36- unicode_literals )
37-
38- from matplotlib .externals import six
39- from matplotlib .externals .six .moves import xrange
4035
4136# History:
4237# 1.0.10: added float validator (disable "Ok" and "Apply" button when not valid)
4338# 1.0.7: added support for "Apply" button
4439# 1.0.6: code cleaning
4540
41+ from __future__ import (absolute_import , division , print_function ,
42+ unicode_literals )
43+
4644__version__ = '1.0.10'
4745__license__ = __doc__
4846
4947DEBUG = False
5048
51- import sys
52- STDERR = sys .stderr
5349
54- from matplotlib . colors import is_color_like
55- from matplotlib . colors import rgb2hex
56- from matplotlib . colors import colorConverter
50+ import copy
51+ import datetime
52+ import warnings
5753
54+ from matplotlib .colors import colorConverter , is_color_like , rgb2hex
5855from matplotlib .backends .qt_compat import QtGui , QtWidgets , QtCore
56+ from matplotlib .externals import six
57+
5958if not hasattr (QtWidgets , 'QFormLayout' ):
6059 raise ImportError ("Warning: formlayout requires PyQt4 >v4.3 or PySide" )
6160
62- import datetime
6361
6462BLACKLIST = set (["title" , "label" ])
6563
6664
67- def col2hex (color ):
68- """Convert matplotlib color to hex before passing to Qt"""
69- return rgb2hex (colorConverter .to_rgb (color ))
70-
71-
7265class ColorButton (QtWidgets .QPushButton ):
7366 """
7467 Color choosing push button
@@ -83,7 +76,8 @@ def __init__(self, parent=None):
8376 self ._color = QtGui .QColor ()
8477
8578 def choose_color (self ):
86- color = QtWidgets .QColorDialog .getColor (self ._color , self .parentWidget (), '' )
79+ color = QtWidgets .QColorDialog .getColor (
80+ self ._color , self .parentWidget (), '' )
8781 if color .isValid ():
8882 self .set_color (color )
8983
@@ -101,18 +95,20 @@ def set_color(self, color):
10195
10296 color = QtCore .Property (QtGui .QColor , get_color , set_color )
10397
98+
10499def col2hex (color ):
105100 """Convert matplotlib color to hex before passing to Qt"""
106101 return rgb2hex (colorConverter .to_rgb (color ))
107102
103+
108104def to_qcolor (color ):
109105 """Create a QColor from a matplotlib color"""
110106 qcolor = QtGui .QColor ()
111107 color = str (color )
112108 try :
113109 color = col2hex (color )
114110 except ValueError :
115- #print('WARNING: ignoring invalid color %r' % color)
111+ warnings . warn ( 'Ignoring invalid color %r' % color )
116112 return qcolor # return invalid QColor
117113 qcolor .setNamedColor (color ) # set using hex color
118114 return qcolor # return valid QColor
@@ -146,19 +142,19 @@ def text(self):
146142def font_is_installed (font ):
147143 """Check if font is installed"""
148144 return [fam for fam in QtGui .QFontDatabase ().families ()
149- if six .text_type (fam ) == font ]
145+ if six .text_type (fam ) == font ]
150146
151147
152148def tuple_to_qfont (tup ):
153149 """
154150 Create a QFont from tuple:
155151 (family [string], size [int], italic [bool], bold [bool])
156152 """
157- if not isinstance (tup , tuple ) or len (tup ) != 4 \
158- or not font_is_installed (tup [0 ]) \
159- or not isinstance (tup [1 ], int ) \
160- or not isinstance (tup [2 ], bool ) \
161- or not isinstance (tup [3 ], bool ):
153+ if not ( isinstance (tup , tuple ) and len (tup ) == 4
154+ and font_is_installed (tup [0 ])
155+ and isinstance (tup [1 ], int )
156+ and isinstance (tup [2 ], bool )
157+ and isinstance (tup [3 ], bool ) ):
162158 return None
163159 font = QtGui .QFont ()
164160 family , size , italic , bold = tup
@@ -189,7 +185,7 @@ def __init__(self, value, parent=None):
189185 # Font size
190186 self .size = QtWidgets .QComboBox (parent )
191187 self .size .setEditable (True )
192- sizelist = list (xrange (6 , 12 )) + list (xrange (12 , 30 , 2 )) + [36 , 48 , 72 ]
188+ sizelist = list (range (6 , 12 )) + list (range (12 , 30 , 2 )) + [36 , 48 , 72 ]
193189 size = font .pointSize ()
194190 if size not in sizelist :
195191 sizelist .append (size )
@@ -227,8 +223,7 @@ class FormWidget(QtWidgets.QWidget):
227223 update_buttons = QtCore .Signal ()
228224 def __init__ (self , data , comment = "" , parent = None ):
229225 QtWidgets .QWidget .__init__ (self , parent )
230- from copy import deepcopy
231- self .data = deepcopy (data )
226+ self .data = copy .deepcopy (data )
232227 self .widgets = []
233228 self .formlayout = QtWidgets .QFormLayout (self )
234229 if comment :
@@ -284,8 +279,9 @@ def setup(self):
284279 elif selindex in keys :
285280 selindex = keys .index (selindex )
286281 elif not isinstance (selindex , int ):
287- print ("Warning: '%s' index is invalid (label: "
288- "%s, value: %s)" % (selindex , label , value ), file = STDERR )
282+ warnings .warn (
283+ "index '%s' is invalid (label: %s, value: %s)" %
284+ (selindex , label , value ))
289285 selindex = 0
290286 field .setCurrentIndex (selindex )
291287 elif isinstance (value , bool ):
@@ -431,8 +427,8 @@ def __init__(self, data, title="", comment="",
431427 self .formwidget .setup ()
432428
433429 # Button box
434- self .bbox = bbox = QtWidgets .QDialogButtonBox (QtWidgets . QDialogButtonBox . Ok
435- | QtWidgets .QDialogButtonBox .Cancel )
430+ self .bbox = bbox = QtWidgets .QDialogButtonBox (
431+ QtWidgets . QDialogButtonBox . Ok | QtWidgets .QDialogButtonBox .Cancel )
436432 self .formwidget .update_buttons .connect (self .update_buttons )
437433 if self .apply_callback is not None :
438434 apply_btn = bbox .addButton (QtWidgets .QDialogButtonBox .Apply )
@@ -457,7 +453,8 @@ def update_buttons(self):
457453 for field in self .float_fields :
458454 if not is_edit_valid (field ):
459455 valid = False
460- for btn_type in (QtWidgets .QDialogButtonBox .Ok , QtWidgets .QDialogButtonBox .Apply ):
456+ for btn_type in (QtWidgets .QDialogButtonBox .Ok ,
457+ QtWidgets .QDialogButtonBox .Apply ):
461458 btn = self .bbox .button (btn_type )
462459 if btn is not None :
463460 btn .setEnabled (valid )
0 commit comments