-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Simplify and improve Qt borders/spacing tool. #8683
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
Conversation
Replaced sliders in Qt borders/spacing tool by spinboxes, which 1. should be easier to set to an exact value, and 2. do not continuously trigger redraws unless the user presses enter or uses the arrows to step the values (the redraws can be quite slow when working with a complex plot). The spinbox step size of 0.005 was chosen for consistency with the earlier choice of 5/1000. Greatly simplified the implementation. Attributes on the SubplotToolQt instance are not kept because it is impossible to keep back-compatibility (the sliders simply don't exist anymore). New attributes are all private; only `.default` (which has the same meaning) is kept as is. Tested with PyQt 4.12, PySide 1.2.4, PyQt 5.8.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Overall this looks like a good change, but I haven't tried running it yet.
|
||
self.defaults = { | ||
attr: getattr(self._figure.subplotpars, attr) | ||
for attr in ["left", "bottom", "right", "top", "wspace", "hspace"]} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suggest making this dictionary generation a method of Figure.SubplotParams, so the line would become, e.g.,
self.defaults = self._figure.subplotpars.dict()
for attr in self.defaults: | ||
widget = self._widgets[attr] | ||
widget.blockSignals(True) | ||
widget.setValue(getattr(self._figure.subplotpars, attr)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Then I would probably use the new dict() method to make a dictionary after calling tight_layout, and use that dictionary to replace the getattr calls. This is a minor matter of taste, and I wouldn't insist on it, but to me it would be more pythonic. Regardless, having something like the dict() method available would be generally useful.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would vars(self._figure.subplotpars)[attr]
be good enough for you?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've never used vars
with an argument--I didn't know it worked that way. Now that I do, I would say that using it is indeed more readable than using the call to getattr
, which I find rather ugly. Or one could use the equivalent self._figure.subplotpars.__dict__[attr]
. Or SubplotParams could be turned into a specialized Bunch, so it could be indexed directly.
self._setSliderPositions() | ||
self.targetfig.canvas.draw_idle() | ||
def _reset(self): | ||
for attr in self.defaults: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
self.defaults
keeps being used simply as a source of the list of parameters; for the sake of readability, I might replace those uses with a genuine list, e.g. self.params
. Yes, it would add one more line of code to the module.
It is outside the present scope, but you might consider a related functional enhancement. I often use the tool to tweak the parameters, and then I transfer the numbers to code in a script. This would be easier if there were a button that would pop up a window with a text string defining a dictionary that could be copied and pasted directly into the script under development. It could be just the dictionary, or it could be closer to a full line of code, e.g. fig.subplots_adjust(left=0.14, right=0.99) It would be simplest to include all of the parameters, even if only some of them are changed. |
One more idea related to the above: the string generation could be via a |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for adding the export. I have just a few more style tweaks, and I still haven't tried running it.
# the redraw callbacks. | ||
self._reset() | ||
|
||
for attr in self._defaults: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
self._defaults -> self._attrs
|
||
def _reset(self): | ||
for attr in self._defaults: | ||
self._widgets[attr].setValue(self._defaults[attr]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for key, value in self._defaults.items():
self._widgets[key].setValue(value)
right.addStretch(1) | ||
|
||
self._widgets["Export values"] = widget = \ | ||
QtWidgets.QPushButton("Export values") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We try to avoid backslash continuations, and avoiding this one is easy:
widget = QtWidgets.QPushButton("Export values")
self._widgets["Export values"] = widget
all covered |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've tested it, and it works for me. Nice!
Sweet! |
Replaced sliders in Qt borders/spacing tool by spinboxes, which
or uses the arrows to step the values (the redraws can be quite
slow when working with a complex plot).
The spinbox step size of 0.005 was chosen for consistency with the
earlier choice of 5/1000.
Greatly simplified the implementation. Attributes on the SubplotToolQt
instance are not kept because it is impossible to keep
back-compatibility (the sliders simply don't exist anymore). New
attributes are all private; only
.default
(which has the same meaning)is kept as is.
Tested with PyQt 4.12, PySide 1.2.4, PyQt 5.8.
The new layout:
