Skip to content

Commit

Permalink
add CheckBox widget to viewer. fix ComboBox widget docstring
Browse files Browse the repository at this point in the history
  • Loading branch information
grlee77 committed Sep 9, 2014
1 parent 865cc1b commit cfb913d
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 7 deletions.
17 changes: 15 additions & 2 deletions skimage/viewer/tests/test_widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from skimage import data, img_as_float, io
from skimage.viewer import ImageViewer, viewer_available
from skimage.viewer.widgets import (
Slider, OKCancelButtons, SaveButtons, ComboBox, Text)
Slider, OKCancelButtons, SaveButtons, ComboBox, CheckBox, Text)
from skimage.viewer.plugins.base import Plugin
from skimage.viewer.qt import QtGui, QtCore
from numpy.testing import assert_almost_equal, assert_equal
Expand All @@ -16,6 +16,20 @@ def get_image_viewer():
viewer += Plugin()
return viewer

@skipif(not viewer_available)
def test_check_box():
viewer = get_image_viewer()
cb = CheckBox('hello', value=True, alignment='left')
viewer.plugins[0] += cb

assert_equal(cb.val, True)
cb.val = False
assert_equal(cb.val, False)
cb.val = 1
assert_equal(cb.val, True)
cb.val = 0
assert_equal(cb.val, False)


@skipif(not viewer_available)
def test_combo_box():
Expand All @@ -29,7 +43,6 @@ def test_combo_box():
assert_equal(str(cb.val), 'c'),
assert_equal(cb.index, 2)


@skipif(not viewer_available)
def test_text_widget():
viewer = get_image_viewer()
Expand Down
65 changes: 60 additions & 5 deletions skimage/viewer/widgets/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from ..utils import RequiredAttr


__all__ = ['BaseWidget', 'Slider', 'ComboBox', 'Text']
__all__ = ['BaseWidget', 'Slider', 'ComboBox', 'CheckBox', 'Text']


class BaseWidget(QtGui.QWidget):
Expand Down Expand Up @@ -211,16 +211,16 @@ class ComboBox(BaseWidget):
Parameters
----------
name : str
Name of slider parameter. If this parameter is passed as a keyword
Name of ComboBox parameter. If this parameter is passed as a keyword
argument, it must match the name of that keyword argument (spaces are
replaced with underscores). In addition, this name is displayed as the
name of the slider.
items: list
name of the ComboBox.
items: list of str
Allowed parameter values.
ptype : {'arg' | 'kwarg' | 'plugin'}
Parameter type.
callback : function
Callback function called in response to slider changes. This function
Callback function called in response to ComboBox changes. This function
is typically set when the widget is added to a plugin.
"""

Expand Down Expand Up @@ -253,3 +253,58 @@ def index(self):
@index.setter
def index(self, i):
self._combo_box.setCurrentIndex(i)

class CheckBox(BaseWidget):
"""CheckBox widget
Parameters
----------
name : str
Name of CheckBox parameter. If this parameter is passed as a keyword
argument, it must match the name of that keyword argument (spaces are
replaced with underscores). In addition, this name is displayed as the
name of the CheckBox.
value: {False, True}
Initial state of the CheckBox.
alignment: {'center','left','right'}
Checkbox alignment
ptype : {'arg' | 'kwarg' | 'plugin'}
Parameter type.
callback : function
Callback function called in response to CheckBox changes. This function
is typically set when the widget is added to a plugin.
"""

def __init__(self, name, value=False, alignment='center', ptype='kwarg', callback=None):
super(CheckBox, self).__init__(name, ptype, callback)

self._check_box = QtGui.QCheckBox()
self._check_box.setChecked(value)
self._check_box.setText(self.name)

self.layout = QtGui.QHBoxLayout(self)
if alignment == 'center':
self.layout.setAlignment(QtCore.Qt.AlignCenter)
elif alignment == 'left':
self.layout.setAlignment(QtCore.Qt.AlignLeft)
elif alignment == 'right':
self.layout.setAlignment(QtCore.Qt.AlignRight)
else:
raise ValueError("Unexpected value %s for 'alignment'" % alignment)

self.layout.addWidget(self._check_box)

self._check_box.stateChanged.connect(self._value_changed)

@property
def val(self):
#return self._check_box.checkState()
return self._check_box.isChecked()


@val.setter
def val(self, i):
#self._check_box.setCheckState(i) # setCheckState has 3 states:
# 0=unchecked, 1=partial, 2=checked
self._check_box.setChecked(i) # setChecked has only two states:
# 0 = unchecked, 2 = checked

0 comments on commit cfb913d

Please sign in to comment.