8000 Backport qt editor improvements by anntzer · Pull Request #6717 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

Backport qt editor improvements #6717

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Qt editor alpha handling.
  • Loading branch information
anntzer committed Jul 10, 2016
commit c077c565dade84378196da5cfb5d0a050164f9dc
28 changes: 16 additions & 12 deletions lib/matplotlib/backends/qt_editor/figureoptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@
import os.path as osp
import re

import matplotlib
from matplotlib import cm, markers, colors as mcolors
import matplotlib.backends.qt_editor.formlayout as formlayout
from matplotlib.backends.qt_compat import QtGui
from matplotlib import cm, markers
from matplotlib.colors import colorConverter, rgb2hex


def get_icon(name):
import matplotlib
basedir = osp.join(matplotlib.rcParams['datapath'], 'images')
return QtGui.QIcon(osp.join(basedir, name))


LINESTYLES = {'-': 'Solid',
'--': 'Dashed',
'-.': 'DashDot',
Expand Down Expand Up @@ -112,23 +112,25 @@ def prepare_data(d, init):
curvelabels = sorted(linedict, key=cmp_key)
for label in curvelabels:
line = linedict[label]
color = rgb2hex(colorConverter.to_rgb(line.get_color()))
ec = rgb2hex(colorConverter.to_rgb(line.get_markeredgecolor()))
fc = rgb2hex(colorConverter.to_rgb(line.get_markerfacecolor()))
color = mcolors.to_hex(
mcolors.to_rgba(line.get_color(), line.get_alpha()),
keep_alpha=True)
ec = mcolors.to_hex(line.get_markeredgecolor(), keep_alpha=True)
fc = mcolors.to_hex(line.get_markerfacecolor(), keep_alpha=True)
curvedata = [
('Label', label),
sep,
(None, '<b>Line</b>'),
('Line Style', prepare_data(LINESTYLES, line.get_linestyle())),
('Draw Style', prepare_data(DRAWSTYLES, line.get_drawstyle())),
('Line style', prepare_data(LINESTYLES, line.get_linestyle())),
('Draw style', prepare_data(DRAWSTYLES, line.get_drawstyle())),
('Width', line.get_linewidth()),
('Color', color),
('Color (RGBA)', color),
sep,
(None, '<b>Marker</b>'),
('Style', prepare_data(MARKERS, line.get_marker())),
('Size', line.get_markersize()),
('Facecolor', fc),
('Edgecolor', ec)]
('Face color (RGBA)', fc),
('Edge color (RGBA)', ec)]
curves.append([curvedata, label, ""])
# Is there a curve displayed?
has_curve = bool(curves)
Expand Down Expand Up @@ -204,7 +206,9 @@ def apply_callback(data):
line.set_linestyle(linestyle)
line.set_drawstyle(drawstyle)
line.set_linewidth(linewidth)
line.set_color(color)
rgba = mcolors.to_rgba(color)
line.set_color(rgba[:3])
line.set_alpha(rgba[-1])
if marker is not 'none':
line.set_marker(marker)
line.set_markersize(markersize)
Expand Down
32 changes: 15 additions & 17 deletions lib/matplotlib/backends/qt_editor/formlayout.py
< B907 /tr>
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@

DEBUG = False

import six

import copy
import datetime
import warnings

from matplotlib.colors import colorConverter, is_color_like, rgb2hex
import six

from matplotlib import colors as mcolors
from matplotlib.backends.qt_compat import QtGui, QtWidgets, QtCore


Expand All @@ -74,7 +74,8 @@ def __init__(self, parent=None):

def choose_color(self):
color = QtWidgets.QColorDialog.getColor(
self._color, self.parentWidget(), '')
self._color, self.parentWidget(), "",
QtWidgets.QColorDialog.ShowAlphaChannel)
if color.isValid():
self.set_color(color)

Expand All @@ -93,30 +94,25 @@ def set_color(self, color):
color = QtCore.Property(QtGui.QColor, get_color, set_color)


def col2hex(color):
"""Convert matplotlib color to hex before passing to Qt"""
return rgb2hex(colorConverter.to_rgb(color))


def to_qcolor(color):
"""Create a QColor from a matplotlib color"""
qcolor = QtGui.QColor()
color = str(color)
try:
color = col2hex(color)
rgba = mcolors.to_rgba(color)
except ValueError:
warnings.warn('Ignoring invalid color %r' % color)
return qcolor # return invalid QColor
qcolor.setNamedColor(color) # set using hex color
return qcolor # return valid QColor
qcolor.setRgbF(*rgba)
return qcolor


class ColorLayout(QtWidgets.QHBoxLayout):
"""Color-specialized QLineEdit layout"""
def __init__(self, color, parent=None):
QtWidgets.QHBoxLayout.__init__(self)
assert isinstance(color, QtGui.QColor)
self.lineedit = QtWidgets.QLineEdit(color.name(), parent)
self.lineedit = QtWidgets.QLineEdit(
mcolors.to_hex(color.getRgbF(), keep_alpha=True), parent)
self.lineedit.editingFinished.connect(self.update_color)
self.addWidget(self.lineedit)
self.colorbtn = ColorButton(parent)
Expand All @@ -130,7 +126,7 @@ def update_color(self):
self.colorbtn.color = qcolor # defaults to black if not qcolor.isValid()

def update_text(self, color):
self.lineedit.setText(color.name())
self.lineedit.setText(mcolors.to_hex(color.getRgbF(), keep_alpha=True))

def text(self):
return self.lineedit.text()
Expand Down Expand Up @@ -256,7 +252,8 @@ def setup(self):
continue
elif tuple_to_qfont(value) is not None:
field = FontLayout(value, self)
elif label.lower() not in BLACKLIST and is_color_like(value):
elif (label.lower() not in BLACKLIST
and mcolors.is_color_like(value)):
field = ColorLayout(to_qcolor(value), self)
elif isinstance(value, six.string_types):
field = QtWidgets.QLineEdit(value, self)
Expand Down Expand Up @@ -319,7 +316,8 @@ def get(self):
continue
elif tuple_to_qfont(value) is not None:
value = field.get_font()
elif isinstance(value, six.string_types) or is_color_like(value):
elif (isinstance(value, six.string_types)
or mcolors.is_color_like(value)):
value = six.text_type(field.text())
elif isinstance(value, (list, tuple)):
index = int(field.currentIndex())
Expand Down
0