32
32
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
33
33
OTHER DEALINGS IN THE SOFTWARE.
34
34
"""
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
40
35
41
36
# History:
42
37
# 1.0.10: added float validator (disable "Ok" and "Apply" button when not valid)
43
38
# 1.0.7: added support for "Apply" button
44
39
# 1.0.6: code cleaning
45
40
41
+ from __future__ import (absolute_import , division , print_function ,
42
+ unicode_literals )
43
+
46
44
__version__ = '1.0.10'
47
45
__license__ = __doc__
48
46
49
47
DEBUG = False
50
48
51
- import sys
52
- STDERR = sys .stderr
53
49
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
57
53
54
+ from matplotlib .colors import colorConverter , is_color_like , rgb2hex
58
55
from matplotlib .backends .qt_compat import QtGui , QtWidgets , QtCore
56
+ from matplotlib .externals import six
57
+
59
58
if not hasattr (QtWidgets , 'QFormLayout' ):
60
59
raise ImportError ("Warning: formlayout requires PyQt4 >v4.3 or PySide" )
61
60
62
- import datetime
63
61
64
62
BLACKLIST = set (["title" , "label" ])
65
63
66
64
67
- def col2hex (color ):
68
- """Convert matplotlib color to hex before passing to Qt"""
69
- return rgb2hex (colorConverter .to_rgb (color ))
70
-
71
-
72
65
class ColorButton (QtWidgets .QPushButton ):
73
66
"""
74
67
Color choosing push button
@@ -83,7 +76,8 @@ def __init__(self, parent=None):
83
76
self ._color = QtGui .QColor ()
<
8000
/code>
84
77
85
78
def choose_color (self ):
86
- color = QtWidgets .QColorDialog .getColor (self ._color , self .parentWidget (), '' )
79
+ color = QtWidgets .QColorDialog .getColor (
80
+ self ._color , self .parentWidget (), '' )
87
81
if color .isValid ():
88
82
self .set_color (color )
89
83
@@ -101,18 +95,20 @@ def set_color(self, color):
101
95
102
96
color = QtCore .Property (QtGui .QColor , get_color , set_color )
103
97
98
+
104
99
def col2hex (color ):
105
100
"""Convert matplotlib color to hex before passing to Qt"""
106
101
return rgb2hex (colorConverter .to_rgb (color ))
107
102
103
+
108
104
def to_qcolor (color ):
109
105
"""Create a QColor from a matplotlib color"""
110
106
qcolor = QtGui .QColor ()
111
107
color = str (color )
112
108
try :
113
109
color = col2hex (color )
114
110
except ValueError :
115
- #print('WARNING: ignoring invalid color %r' % color)
111
+ warnings . warn ( 'Ignoring invalid color %r' % color )
116
112
return qcolor # return invalid QColor
117
113
qcolor .setNamedColor (color ) # set using hex color
118
114
return qcolor # return valid QColor
@@ -146,19 +142,19 @@ def text(self):
146
142
def font_is_installed (font ):
147
143
"""Check if font is installed"""
148
144
return [fam for fam in QtGui .QFontDatabase ().families ()
149
- if six .text_type (fam ) == font ]
145
+ if six .text_type (fam ) == font ]
150
146
151
147
152
148
def tuple_to_qfont (tup ):
153
149
"""
154
150
Create a QFont from tuple:
155
151
(family [string], size [int], italic [bool], bold [bool])
156
152
"""
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 ) ):
162
158
return None
163
159
font = QtGui .QFont ()
164
160
family , size , italic , bold = tup
@@ -189,7 +185,7 @@ def __init__(self, value, parent=None):
189
185
# Font size
190
186
self .size = QtWidgets .QComboBox (parent )
191
187
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 ]
193
189
size = font .pointSize ()
194
190
if size not in sizelist :
195
191
sizelist .append (size )
@@ -227,8 +223,7 @@ class FormWidget(QtWidgets.QWidget):
227
223
update_buttons = QtCore .Signal ()
228
224
def __init__ (self , data , comment = "" , parent = None ):
229
225
QtWidgets .QWidget .__init__ (self , parent )
230
- from copy import deepcopy
231
- self .data = deepcopy (data )
226
+ self .data = copy .deepcopy (data )
232
227
self .widgets = []
233
228
self .formlayout = QtWidgets .QFormLayout (self )
234
229
if comment :
@@ -284,8 +279,9 @@ def setup(self):
284
279
elif selindex in keys :
285
280
selindex = keys .index (selindex )
286
281
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 ))
289
285
selindex = 0
290
286
field .setCurrentIndex (selindex )
291
287
elif isinstance (value , bool ):
@@ -431,8 +427,8 @@ def __init__(self, data, title="", comment="",
431
427
self .formwidget .setup ()
432
428
433
429
# 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 )
436
432
self .formwidget .update_buttons .connect (self .update_buttons )
437
433
if self .apply_callback is not None :
438
434
apply_btn = bbox .addButton (QtWidgets .QDialogButtonBox .Apply )
@@ -457,7 +453,8 @@ def update_buttons(self):
457
453
for field in self .float_fields :
458
454
if not is_edit_valid (field ):
459
455
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 ):
461
458
btn = self .bbox .button (btn_type )
462
459
if btn is not None :
463
460
btn .setEnabled (valid )
0 commit comments