Skip to content

Commit b3a3d8e

Browse files
committed
curve/grid/... make functions: added possibility to set color from str
1 parent 5773285 commit b3a3d8e

File tree

5 files changed

+49
-10
lines changed

5 files changed

+49
-10
lines changed

qwt/plot_curve.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
)
2626
from .symbol import QwtSymbol
2727
from .plot_directpainter import QwtPlotDirectPainter
28+
from .qthelpers import qcolor_from_str
2829

2930
from .qt.QtGui import QPen, QBrush, QPainter, QPolygonF, QColor
3031
from .qt.QtCore import QSize, Qt, QRectF, QPointF
@@ -228,7 +229,7 @@ def make(
228229
:param symbol: curve symbol
229230
:type symbol: qwt.symbol.QwtSymbol or None
230231
:param linecolor: curve line color
231-
:type linecolor: QColor or None
232+
:type linecolor: QColor or str or None
232233
:param linewidth: curve line width
233234
:type linewidth: float or None
234235
:param linestyle: curve pen style
@@ -258,7 +259,7 @@ def make(
258259
item.setStyle(style)
259260
if symbol is not None:
260261
item.setSymbol(symbol)
261-
linecolor = Qt.black if linecolor is None else linecolor
262+
linecolor = qcolor_from_str(linecolor, Qt.black)
262263
linewidth = 1.0 if linewidth is None else linewidth
263264
linestyle = Qt.SolidLine if linestyle is None else linestyle
264265
item.setPen(QPen(linecolor, linewidth, linestyle))

qwt/plot_grid.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from .plot import QwtPlot, QwtPlotItem
1818
from .text import QwtText
1919
from ._math import qwtFuzzyGreaterOrEqual, qwtFuzzyLessOrEqual
20+
from .qthelpers import qcolor_from_str
2021

2122
from .qt.QtGui import QPen
2223
from .qt.QtCore import Qt
@@ -79,13 +80,13 @@ def make(
7980
:param enableminor: Tuple of two boolean values (x, y) for enabling minor grid lines
8081
:type enableminor: bool or None
8182
:param color: Pen color for both major and minor grid lines (default: Qt.gray)
82-
:type color: QColor or None
83+
:type color: QColor or str or None
8384
:param width: Pen width for both major and minor grid lines (default: 1.0)
8485
:type width: float or None
8586
:param style: Pen style for both major and minor grid lines (default: Qt.DotLine)
8687
:type style: Qt.PenStyle or None
8788
:param mincolor: Pen color for minor grid lines only (default: Qt.gray)
88-
:type mincolor: QColor or None
89+
:type mincolor: QColor or str or None
8990
:param minwidth: Pen width for minor grid lines only (default: 1.0)
9091
:type minwidth: float or None
9192
:param minstyle: Pen style for minor grid lines only (default: Qt.DotLine)
@@ -98,12 +99,12 @@ def make(
9899
item = cls()
99100
if z is not None:
100101
item.setZ(z)
101-
color = Qt.gray if color is None else color
102+
color = qcolor_from_str(color, Qt.gray)
102103
width = 1.0 if width is None else width
103104
style = Qt.DotLine if style is None else style
104105
item.setPen(QPen(color, width, style))
105106
if mincolor is not None or minwidth is not None or minstyle is not None:
106-
mincolor = Qt.gray if mincolor is None else mincolor
107+
mincolor = qcolor_from_str(mincolor, Qt.gray)
107108
minwidth = 1.0 if width is None else minwidth
108109
minstyle = Qt.DotLine if style is None else minstyle
109110
item.setMinorPen(QPen(mincolor, minwidth, minstyle))

qwt/plot_marker.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from .painter import QwtPainter
1919
from .graphic import QwtGraphic
2020
from .symbol import QwtSymbol
21+
from .qthelpers import qcolor_from_str
2122

2223
from .qt.QtGui import QPen, QPainter
2324
from .qt.QtCore import Qt, QPointF, QRectF, QSizeF, QRect
@@ -123,7 +124,8 @@ def make(
123124
:param spacing: Spacing (distance between the position and the label)
124125
:type spacing: int or None
125126
:param int linestyle: Line style
126-
:param QColor color: Pen color
127+
:param color: Pen color
128+
:type color: QColor or str or None
127129
:param float width: Pen width
128130
:param Qt.PenStyle style: Pen style
129131
:param bool antialiased: if True, enable antialiasing rendering
@@ -152,7 +154,7 @@ def make(
152154
item.setLabelOrientation(orientation)
153155
if spacing is not None:
154156
item.setSpacing(spacing)
155-
color = Qt.black if color is None else color
157+
color = qcolor_from_str(color, Qt.black)
156158
width = 1.0 if width is None else width
157159
style = Qt.SolidLine if style is None else style
158160
item.setLinePen(QPen(color, width, style))

qwt/qthelpers.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# -*- coding: utf-8 -*-
2+
#
3+
# Licensed under the terms of the MIT License
4+
# Copyright (c) 2015 Pierre Raybaut
5+
# (see LICENSE file for more details)
6+
7+
"""Qt helpers"""
8+
9+
from qwt.qt import QtGui as QG
10+
from qwt.qt.QtCore import Qt
11+
12+
13+
def qcolor_from_str(color, default):
14+
"""Return QColor object from str
15+
16+
:param color: Input color
17+
:type color: QColor or str or None
18+
:param QColor default: Default color (returned if color is None)
19+
20+
If color is already a QColor instance, simply return color.
21+
If color is None, return default color.
22+
If color is neither an str nor a QColor instance nor None, raise TypeError.
23+
"""
24+
if color is None:
25+
return default
26+
elif isinstance(color, QG.QColor):
27+
return color
28+
elif isinstance(color, str):
29+
try:
30+
return getattr(Qt, color)
31+
except AttributeError:
32+
raise ValueError("Unknown Qt color %r" % color)
33+
else:
34+
raise TypeError("Invalid color %r" % color)

qwt/text.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
from .qt.QtCore import Qt, QSizeF, QSize, QRectF
6868

6969
from .painter import QwtPainter
70+
from .qthelpers import qcolor_from_str
7071

7172
QWIDGETSIZE_MAX = (1 << 24) - 1
7273

@@ -638,7 +639,7 @@ def make(
638639
:param weight: Font weight (default: QFont.Normal)
639640
:type weight: int or None
640641
:param color: Pen color
641-
:type color: QColor or None
642+
:type color: QColor or str or None
642643
:param borderradius: Radius for the corners of the border frame
643644
:type borderradius: float or None
644645
:param borderpen: Background pen
@@ -661,7 +662,7 @@ def make(
661662
weight = QFont.Normal if weight is None else weight
662663
item.setFont(QFont(family, pointsize, weight))
663664
if color is not None:
664-
item.setColor(color)
665+
item.setColor(qcolor_from_str(color, Qt.black))
665666
if borderradius is not None:
666667
item.setBorderRadius(borderradius)
667668
if borderpen is not None:

0 commit comments

Comments
 (0)