-
Notifications
You must be signed in to change notification settings - Fork 50
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
Add a logarithmic scale slider class #38
Merged
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
717f87b
Add a logarithmic scale slider class
GenevieveBuckley a0435e9
Add QLogSlider to magicgui/_qt/widgets/__init__.py
GenevieveBuckley 8d6446a
Add forgotten numpy import
GenevieveBuckley 5c9bc6a
Set minimum for log scale slider
GenevieveBuckley 9e69e41
Add basic tests for QLogSlider and QDoubleSlider classes
GenevieveBuckley 47edf4f
Remove unwanted space in filename of test_qt.py
GenevieveBuckley 72906bd
Require numpy
GenevieveBuckley 449f9fa
Linting, add docstrings to test_widgets.py
GenevieveBuckley 0e7345c
Use math library instead of numpy, keeps dependencies minimal
GenevieveBuckley ad55bb3
Fix linting, no blank line allowed after function docstring
GenevieveBuckley 021952f
No numpy, use math standard library instead
GenevieveBuckley d35e810
delete any pre-existing app
tlambert03 ca56761
don't test event_loop on pyqt5
tlambert03 0a87131
fix skip
tlambert03 9eaad18
modify skip
tlambert03 d72440a
Update magicgui/_qt/widgets/log_slider.py
GenevieveBuckley 6e7a1e3
Update magicgui/_qt/widgets/log_slider.py
GenevieveBuckley 98df338
Update magicgui/_qt/widgets/log_slider.py
GenevieveBuckley 9e328f0
Add type hints to slider methods
GenevieveBuckley f4948bc
Another type hint
GenevieveBuckley 981efac
May choose base for logarithmic slider, default base is math.e
GenevieveBuckley File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
"""A Slider with a natural logarithmic scale.""" | ||
import math | ||
|
||
from qtpy.QtCore import Qt | ||
from qtpy.QtWidgets import QSlider | ||
|
||
|
||
class QLogSlider(QSlider): | ||
"""A Slider Widget with a natural logarithmic scale.""" | ||
|
||
PRECISION = 1000 | ||
|
||
def __init__(self, parent=None, *, base=math.e): | ||
super().__init__(Qt.Horizontal, parent=parent) | ||
self.base = base | ||
self.setMinimum(0) | ||
self.setMaximum(10) | ||
|
||
def value(self): | ||
"""Get the natural log slider value as a float.""" | ||
return math.log(super().value() / self.PRECISION, self.base) | ||
|
||
def setValue(self, value: float): | ||
"""Set integer slier position from float ``value``.""" | ||
super().setValue(int(math.pow(self.base, value) * self.PRECISION)) | ||
|
||
def setMinimum(self, value: float): | ||
"""Set minimum position of slider in float units.""" | ||
super().setMinimum(int(math.pow(self.base, value) * self.PRECISION)) | ||
|
||
def setMaximum(self, value: float): | ||
"""Set maximum position of slider in float units.""" | ||
super().setMaximum(int(math.pow(self.base, value) * self.PRECISION)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#!/usr/bin/env python | ||
|
||
"""Tests for `magicgui` widgets.""" | ||
import math | ||
import pytest | ||
|
||
from magicgui._qt.widgets import QDoubleSlider, QLogSlider | ||
|
||
|
||
@pytest.mark.parametrize("SliderClass", [QDoubleSlider, QLogSlider]) | ||
def test_slider(qtbot, SliderClass): | ||
"""Test magicgui sliders.""" | ||
slider = SliderClass() | ||
qtbot.addWidget(slider) | ||
assert slider.value() == 0 # default slider value is zero | ||
slider.setValue(math.pi) | ||
assert math.isclose(slider.value(), math.pi, abs_tol=0.01) | ||
slider.setMaximum(10) | ||
slider.setValue(11) # above maximum allowed value, value will be clipped | ||
assert math.isclose(slider.value(), 10, abs_tol=1e-07) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
to make this slightly more flexible, perhaps we should add an attribute
self.base = math.e
... then convertmath.exp(value)
tomath.pow(self.base, value)
andmath.log(value)
tomath.log(value, base=self.base)
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.
Nice touch, I like this suggestion a lot. Done!