Skip to content
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 21 commits into from
Jul 23, 2020
Merged
Show file tree
Hide file tree
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 Jul 21, 2020
a0435e9
Add QLogSlider to magicgui/_qt/widgets/__init__.py
GenevieveBuckley Jul 21, 2020
8d6446a
Add forgotten numpy import
GenevieveBuckley Jul 21, 2020
5c9bc6a
Set minimum for log scale slider
GenevieveBuckley Jul 21, 2020
9e69e41
Add basic tests for QLogSlider and QDoubleSlider classes
GenevieveBuckley Jul 21, 2020
47edf4f
Remove unwanted space in filename of test_qt.py
GenevieveBuckley Jul 21, 2020
72906bd
Require numpy
GenevieveBuckley Jul 21, 2020
449f9fa
Linting, add docstrings to test_widgets.py
GenevieveBuckley Jul 21, 2020
0e7345c
Use math library instead of numpy, keeps dependencies minimal
GenevieveBuckley Jul 22, 2020
ad55bb3
Fix linting, no blank line allowed after function docstring
GenevieveBuckley Jul 22, 2020
021952f
No numpy, use math standard library instead
GenevieveBuckley Jul 22, 2020
d35e810
delete any pre-existing app
tlambert03 Jul 22, 2020
ca56761
don't test event_loop on pyqt5
tlambert03 Jul 22, 2020
0a87131
fix skip
tlambert03 Jul 22, 2020
9eaad18
modify skip
tlambert03 Jul 22, 2020
d72440a
Update magicgui/_qt/widgets/log_slider.py
GenevieveBuckley Jul 23, 2020
6e7a1e3
Update magicgui/_qt/widgets/log_slider.py
GenevieveBuckley Jul 23, 2020
98df338
Update magicgui/_qt/widgets/log_slider.py
GenevieveBuckley Jul 23, 2020
9e328f0
Add type hints to slider methods
GenevieveBuckley Jul 23, 2020
f4948bc
Another type hint
GenevieveBuckley Jul 23, 2020
981efac
May choose base for logarithmic slider, default base is math.e
GenevieveBuckley Jul 23, 2020
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
2 changes: 2 additions & 0 deletions magicgui/_qt/widgets/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Custom widgets for magicgui."""
from .data_combobox import QDataComboBox
from .double_slider import QDoubleSlider
from .log_slider import QLogSlider
from .eval_lineedit import LiteralEvalEdit
from .file_dialog import MagicFileDialog, MagicFilesDialog
from .widget import WidgetType
Expand All @@ -10,6 +11,7 @@
"MagicFileDialog",
"MagicFilesDialog",
"QDoubleSlider",
"QLogSlider",
"QDataComboBox",
"WidgetType",
]
4 changes: 2 additions & 2 deletions magicgui/_qt/widgets/double_slider.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ def value(self):
"""Get the slider value and convert to float."""
return super().value() / self.PRECISION

def setValue(self, value):
def setValue(self, value: float):
"""Set integer slider position from float ``value``."""
super().setValue(value * self.PRECISION)

def setMaximum(self, value):
def setMaximum(self, value: float):
"""Set maximum position of slider in float units."""
super().setMaximum(value * self.PRECISION)
33 changes: 33 additions & 0 deletions magicgui/_qt/widgets/log_slider.py
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)

Copy link
Member

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 convert math.exp(value) to math.pow(self.base, value) and math.log(value) to math.log(value, base=self.base)

Copy link
Contributor Author

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!

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))
9 changes: 7 additions & 2 deletions magicgui/_tests/test_ qt.py → magicgui/_tests/test_qt.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,22 @@
import datetime
from enum import Enum
from pathlib import Path, PosixPath, WindowsPath
from typing import List, Sequence, Tuple

import pytest
from qtpy import QtCore
from qtpy import QtCore, API_NAME
from qtpy import QtWidgets as QtW

from magicgui import _qt, event_loop
from typing import Sequence, List, Tuple


@pytest.mark.skipif(API_NAME == "PyQt5", reason="Couldn't delete app on PyQt")
def test_event():
"""Test that the event loop makes a Qt app."""
if QtW.QApplication.instance():
import shiboken2

shiboken2.delete(QtW.QApplication.instance())
assert not QtW.QApplication.instance()
with event_loop():
app = QtW.QApplication.instance()
Expand Down
20 changes: 20 additions & 0 deletions magicgui/_tests/test_widgets.py
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)