Skip to content

GridItem: Add custom line width based on pen option.#1709

Closed
nodedge wants to merge 1 commit intopyqtgraph:masterfrom
nodedge:feature/grid-item-custom-width
Closed

GridItem: Add custom line width based on pen option.#1709
nodedge wants to merge 1 commit intopyqtgraph:masterfrom
nodedge:feature/grid-item-custom-width

Conversation

@nodedge
Copy link
Copy Markdown

@nodedge nodedge commented Apr 11, 2021

Hello,

Thank you for this awesome library.

We wish to contribute to it via this small PR.

The point is to customize the width of the GridItem lines.

Happy review!

@j9ac9k
Copy link
Copy Markdown
Member

j9ac9k commented Apr 11, 2021

Hi @nodedge,

Thanks for the PR. I'm in support of the feature, but in its current implementation it breaks default behavior.

For example, running python example/GraphicsScene.py on this PR.

image

Same thing but on master right now:

image

This can likely be worked around by checking if the pen has a default width, in which case set the pen width to self.pixelWidth() but otherwise set to the width you're working with.

Or alternatively a "better" implementation IMO would be to multiply the pen width by self.pixelWidth(); as it makes sense to have the pen width of the GridItem be in screen pixels, and have nothing to do with the coordinate system it's in.

@NilsNemitz @ixjlyons do either of you have input.

@ixjlyons
Copy link
Copy Markdown
Member

Probably the simplest way to handle this is to just extract the width in setPen and use that in generatePicture. Something like:

diff --git a/pyqtgraph/graphicsItems/GridItem.py b/pyqtgraph/graphicsItems/GridItem.py
index efb1afdb..ff9f8b6f 100644
--- a/pyqtgraph/graphicsItems/GridItem.py
+++ b/pyqtgraph/graphicsItems/GridItem.py
@@ -35,6 +35,7 @@ class GridItem(UIGraphicsItem):
             self.opts['pen'] = fn.mkPen(*args, **kwargs)
 
         self.picture = None
+        self._penWidth = self.opts['pen'].width()
         self.update()
 
 
@@ -157,7 +158,6 @@ class GridItem(UIGraphicsItem):
 
                 linePen = self.opts['pen']
                 lineColor = self.opts['pen'].color()
-                lineWidth = self.opts['pen'].width()
                 lineColor.setAlpha(c)
                 linePen.setColor(lineColor)
 
@@ -171,9 +171,9 @@ class GridItem(UIGraphicsItem):
                 for x in range(0, int(nl[ax])):
                     linePen.setCosmetic(False)
                     if ax == 0:
-                        linePen.setWidthF(lineWidth)
+                        linePen.setWidthF(self._penWidth*self.pixelWidth())
                     else:
-                        linePen.setWidthF(lineWidth)
+                        linePen.setWidthF(self._penWidth*self.pixelHeight())
                     p.setPen(linePen)
                     p1 = np.array([0.,0.])
                     p2 = np.array([0.,0.])

@NilsNemitz
Copy link
Copy Markdown
Contributor

@nodedge , @j9ac9k
Without having the time to check in detail:
It might also be useful to make sure that the QPen.setCosmetic() gets set?
see: https://doc.qt.io/qt-5/qpen.html#isCosmetic
I assume that the line width is supposed to be constant, and not a function of the zoom-level?

@j9ac9k
Copy link
Copy Markdown
Member

j9ac9k commented Apr 11, 2021

Ahh yes, GridItem pens should most certainly be cosmetic.

@ixjlyons
Copy link
Copy Markdown
Member

It's explicitly set to False - not sure why.

@nodedge
Copy link
Copy Markdown
Author

nodedge commented Apr 11, 2021

Thank you for your answers.
@ixjlyons I have just tried your solution.

The example "GraphicsScene" is not broken anymore.
I added a custom pen in this same example, but the gridline width does not seem to be modified.
Instead, there is more space between the lines.
If we maximize the window, the grid spacing seems to be reset.

@j9ac9k
Copy link
Copy Markdown
Member

j9ac9k commented Aug 1, 2021

I think I got to the bottom of this issue. The problem is this line I believe:

https://github.com/pyqtgraph/pyqtgraph/blob/master/pyqtgraph/graphicsItems/GridItem.py#L152

Since QPen's are mutable, we change the width of the pen's further down here:

https://github.com/pyqtgraph/pyqtgraph/blob/master/pyqtgraph/graphicsItems/GridItem.py#L166-L169

That change in width sort of throws out the initial setting we made.

We can work around this by modifying

                linePen = fn.mkPen(self.opts['pen'])

This returns a copy of the pen, where the width that was initially set to is preserved.

@nodedge would you be ok with me pushing my changes to your branch?

@ixjlyons
Copy link
Copy Markdown
Member

ixjlyons commented Jun 6, 2022

I think this can be closed now, see #2304

Thanks for your efforts @nodedge

@ixjlyons ixjlyons closed this Jun 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants