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
- import six
39
- from 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
49
+ import six
53
50
54
- from matplotlib . colors import is_color_like
55
- from matplotlib . colors import rgb2hex
56
- from matplotlib . colors import colorConverter
51
+ import copy
52
+ import datetime
53
+ import warnings
57
54
55
+ from matplotlib .colors import colorConverter , is_color_like , rgb2hex
58
56
from matplotlib .backends .qt_compat import QtGui , QtWidgets , QtCore
59
- if not hasattr (QtWidgets , 'QFormLayout' ):
60
- raise ImportError ("Warning: formlayout requires PyQt4 >v4.3 or PySide" )
61
57
62
- import datetime
63
58
64
59
BLACKLIST = set (["title" , "label" ])
65
60
66
61
67
- def col2hex (color ):
68
- """Convert matplotlib color to hex before passing to Qt"""
69
- return rgb2hex (colorConverter .to_rgb (color ))
70
-
71
-
72
62
class ColorButton (QtWidgets .QPushButton ):
73
63
"""
74
64
Color choosing push button
@@ -83,7 +73,8 @@ def __init__(self, parent=None):
83
73
self ._color = QtGui .QColor ()
84
74
85
75
def choose_color (self ):
86
- color = QtWidgets .QColorDialog .getColor (self ._color , self .parentWidget (), '' )
76
+ color = QtWidgets .QColorDialog .getColor (
77
+ self ._color , self .parentWidget (), '' )
87
78
if color .isValid ():
88
79
self .set_color (color )
89
80
@@ -101,18 +92,20 @@ def set_color(self, color):
101
92
102
93
color = QtCore .Property (QtGui .QColor , get_color , set_color )
103
94
95
+
104
96
def col2hex (color ):
105
97
"""Convert matplotlib color to hex before passing to Qt"""
106
98
return rgb2hex (colorConverter .to_rgb (color ))
107
99
100
+
108
101
def to_qcolor (color ):
109
102
"""Create a QColor from a matplotlib color"""
110
103
qcolor = QtGui .QColor ()
111
104
color = str (color )
112
105
try :
113
106
color = col2hex (color )
114
107
except ValueError :
115
- #print('WARNING: ignoring invalid color %r' % color)
108
+ warnings . warn ( 'Ignoring invalid color %r' % color )
116
109
return qcolor # return invalid QColor
117
110
qcolor .setNamedColor (color ) # set using hex color
118
111
return qcolor # return valid QColor
@@ -146,19 +139,19 @@ def text(self):
146
139
def font_is_installed (font ):
147
140
"""Check if font is installed"""
148
141
return [fam for fam in QtGui .QFontDatabase ().families ()
149
- if six .text_type (fam ) == font ]
142
+ if six .text_type (fam ) == font ]
150
143
151
144
152
145
def tuple_to_qfont (tup ):
153
146
"""
154
147
Create a QFont from tuple:
155
148
(family [string], size [int], italic [bool], bold [bool])
156
149
"""
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 ):
150
+ if not ( isinstance (tup , tuple ) and len (tup ) == 4
151
+ and font_is_installed (tup [0 ])
152
+ and isinstance (tup [1 ], int )
153
+ and isinstance (tup [2 ], bool )
154
+ and isinstance (tup [3 ], bool ) ):
162
155
return None
163
156
font = QtGui .QFont ()
164
157
family , size , italic , bold = tup
@@ -189,7 +182,7 @@ def __init__(self, value, parent=None):
189
182
# Font size
190
183
self .size = QtWidgets .QComboBox (parent )
191
184
self .size .setEditable (True )
192
- sizelist = list (xrange (6 , 12 )) + list (xrange (12 , 30 , 2 )) + [36 , 48 , 72 ]
185
+ sizelist = list (range (6 , 12 )) + list (range (12 , 30 , 2 )) + [36 , 48 , 72 ]
193
186
size = font .pointSize ()
194
187
if size not in sizelist :
195
188
sizelist .append (size )
@@ -227,8 +220,7 @@ class FormWidget(QtWidgets.QWidget):
227
220
update_buttons = QtCore .Signal ()
228
221
def __init__ (self , data , comment = "" , parent = None ):
229
222
QtWidgets .QWidget .__init__ (self , parent )
230
- from copy import deepcopy
231
- self .data = deepcopy (data )
223
+ self .data = copy .deepcopy (data )
232
224
self .widgets = []
233
225
self .formlayout = QtWidgets .QFormLayout (self )
234
226
if comment :
@@ -284,8 +276,9 @@ def setup(self):
284
276
elif selindex in keys :
285
277
selindex = keys .index (selindex )
286
278
elif not isinstance (selindex , int ):
287
- print ("Warning: '%s' index is invalid (label: "
288
- "%s, value: %s)" % (selindex , label , value ), file = STDERR )
279
+ warnings .warn (
280
+ "index '%s' is invalid (label: %s, value: %s)" %
281
+ (selindex , label , value ))
289
282
selindex = 0
290
283
field .setCurrentIndex (selindex )
291
284
elif isinstance (value , bool ):
@@ -431,8 +424,8 @@ def __init__(self, data, title="", comment="",
431
424
self .formwidget .setup ()
432
425
433
426
# Button box
434
- self .bbox = bbox = QtWidgets .QDialogButtonBox (QtWidgets . QDialogButtonBox . Ok
435
- | QtWidgets .QDialogButtonBox .Cancel )
427
+ self .bbox = bbox = QtWidgets .QDialogButtonBox (
428
+ QtWidgets . QDialogButtonBox . Ok | QtWidgets .QDialogButtonBox .Cancel )
436
429
self .formwidget .update_buttons .connect (self .update_buttons )
437
430
if self .apply_callback is not None :
438
431
apply_btn = bbox .addButton (QtWidgets .QDialogButtonBox .Apply )
@@ -457,7 +450,8 @@ def update_buttons(self):
457
450
for field in self .float_fields :
458
451
if not is_edit_valid (field ):
459
452
valid = False
460
- for btn_type in (QtWidgets .QDialogButtonBox .Ok , QtWidgets .QDialogButtonBox .Apply ):
453
+ for btn_type in (QtWidgets .QDialogButtonBox .Ok ,
454
+ QtWidgets .QDialogButtonBox .Apply ):
461
455
btn = self .bbox .button (btn_type )
462
456
if btn is not None :
463
457
btn .setEnabled (valid )
0 commit comments