Skip to content

Commit 46dd689

Browse files
committed
Minor cleanups for FormLayout.
1 parent 5d179ba commit 46dd689

File tree

1 file changed

+28
-34
lines changed

1 file changed

+28
-34
lines changed

lib/matplotlib/backends/qt_editor/formlayout.py

Lines changed: 28 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -32,43 +32,33 @@
3232
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
3333
OTHER DEALINGS IN THE SOFTWARE.
3434
"""
35-
from __future__ import (absolute_import, division, print_function,
36-
unicode_literals)
37-
38-
import six
39-
from 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

4947
DEBUG = False
5048

51-
import sys
52-
STDERR = sys.stderr
49+
import six
5350

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
5754

55+
from matplotlib.colors import colorConverter, is_color_like, rgb2hex
5856
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")
6157

62-
import datetime
6358

6459
BLACKLIST = set(["title", "label"])
6560

6661

67-
def col2hex(color):
68-
"""Convert matplotlib color to hex before passing to Qt"""
69-
return rgb2hex(colorConverter.to_rgb(color))
70-
71-
7262
class ColorButton(QtWidgets.QPushButton):
7363
"""
7464
Color choosing push button
@@ -83,7 +73,8 @@ def __init__(self, parent=None):
8373
self._color = QtGui.QColor()
8474

8575
def choose_color(self):
86-
color = QtWidgets.QColorDialog.getColor(self._color, self.parentWidget(), '')
76+
color = QtWidgets.QColorDialog.getColor(
77+
self._color, self.parentWidget(), '')
8778
if color.isValid():
8879
self.set_color(color)
8980

@@ -101,18 +92,20 @@ def set_color(self, color):
10192

10293
color = QtCore.Property(QtGui.QColor, get_color, set_color)
10394

95+
10496
def col2hex(color):
10597
"""Convert matplotlib color to hex before passing to Qt"""
10698
return rgb2hex(colorConverter.to_rgb(color))
10799

100+
108101
def to_qcolor(color):
109102
"""Create a QColor from a matplotlib color"""
110103
qcolor = QtGui.QColor()
111104
color = str(color)
112105
try:
113106
color = col2hex(color)
114107
except ValueError:
115-
#print('WARNING: ignoring invalid color %r' % color)
108+
warnings.warn('Ignoring invalid color %r' % color)
116109
return qcolor # return invalid QColor
117110
qcolor.setNamedColor(color) # set using hex color
118111
return qcolor # return valid QColor
@@ -146,19 +139,19 @@ def text(self):
146139
def font_is_installed(font):
147140
"""Check if font is installed"""
148141
return [fam for fam in QtGui.QFontDatabase().families()
149-
if six.text_type(fam) == font]
142+
if six.text_type(fam) == font]
150143

151144

152145
def tuple_to_qfont(tup):
153146
"""
154147
Create a QFont from tuple:
155148
(family [string], size [int], italic [bool], bold [bool])
156149
"""
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)):
162155
return None
163156
font = QtGui.QFont()
164157
family, size, italic, bold = tup
@@ -189,7 +182,7 @@ def __init__(self, value, parent=None):
189182
# Font size
190183
self.size = QtWidgets.QComboBox(parent)
191184
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]
193186
size = font.pointSize()
194187
if size not in sizelist:
195188
sizelist.append(size)
@@ -227,8 +220,7 @@ class FormWidget(QtWidgets.QWidget):
227220
update_buttons = QtCore.Signal()
228221
def __init__(self, data, comment="", parent=None):
229222
QtWidgets.QWidget.__init__(self, parent)
230-
from copy import deepcopy
231-
self.data = deepcopy(data)
223+
self.data = copy.deepcopy(data)
232224
self.widgets = []
233225
self.formlayout = QtWidgets.QFormLayout(self)
234226
if comment:
@@ -284,8 +276,9 @@ def setup(self):
284276
elif selindex in keys:
285277
selindex = keys.index(selindex)
286278
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))
289282
selindex = 0
290283
field.setCurrentIndex(selindex)
291284
elif isinstance(value, bool):
@@ -431,8 +424,8 @@ def __init__(self, data, title="", comment="",
431424
self.formwidget.setup()
432425

433426
# 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)
436429
self.formwidget.update_buttons.connect(self.update_buttons)
437430
if self.apply_callback is not None:
438431
apply_btn = bbox.addButton(QtWidgets.QDialogButtonBox.Apply)
@@ -457,7 +450,8 @@ def update_buttons(self):
457450
for field in self.float_fields:
458451
if not is_edit_valid(field):
459452
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):
461455
btn = self.bbox.button(btn_type)
462456
if btn is not None:
463457
btn.setEnabled(valid)

0 commit comments

Comments
 (0)