summaryrefslogtreecommitdiff
path: root/gr-qtgui/src/python/forms/forms.py
blob: c6b7c9fcf8237f36d47a853c2cc1abfe216bec9a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#
# Copyright 2010 Free Software Foundation, Inc.
#
# This file is part of GNU Radio
#
# GNU Radio is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3, or (at your option)
# any later version.
#
# GNU Radio is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with GNU Radio; see the file COPYING.  If not, write to
# the Free Software Foundation, Inc., 51 Franklin Street,
# Boston, MA 02110-1301, USA.
#

from PyQt4 import QtGui
from PyQt4 import QtCore

import converters

class _form_base(QtGui.QWidget):
    def __init__(self, parent=None, converter=None, callback=None, value=None):
        QtGui.QWidget.__init__(self, parent)
        self._converter = converter
        self._callback = callback
        self.set_value(value)

    def get_value(self):
        return self._value

    def set_value(self, value):
        self._value = value
        self._base_update(self._converter.external_to_internal(value))

    def _base_update(self, int_val):
        self._update(int_val)

    def _base_handle(self, int_val):
        self._value = self._converter.internal_to_external(int_val)
        if self._callback: self._callback(self._value)

class _slider_base(_form_base):
    def __init__(self, label='', length=-1, num_steps=100, orient=QtCore.Qt.Horizontal, **kwargs):
        _form_base.__init__(self, **kwargs)
        self._slider = QtGui.QSlider(parent=self)
        self._slider.setOrientation(orient)
        self._slider.setRange(0, num_steps)
        if length > 0:
            if orient == QtCore.Qt.Horizontal:
                slider_size = self._slider.setWidth(length)
            if orient == QtCore.Qt.Vertical:
                slider_size = self._slider.setHeight(length)
        self.slider.valueChanged.connect(self._handle)

    def _handle(self, event): self._base_handle(self._slider.value())
    def _update(self, value): self._slider.setValue(int(round(value)))

class slider(_slider_base):
    """
    A generic linear slider.
    @param parent the parent widget
    @param value the default value
    @param label title label for this widget (optional)
    @param length the length of the slider in px (optional)
    @param orient Qt.Horizontal Veritcal (default=horizontal)
    @param start the start value
    @param stop the stop value
    @param num_steps the number of slider steps (or specify step_size)
    @param step_size the step between slider jumps (or specify num_steps)
    @param cast a cast function, int, or float (default=float)
    """
    def __init__(self, start, stop, num_steps=100, step_size=None, cast=float, **kwargs):
        assert step_size or num_steps
        if step_size is not None: num_steps = (stop - start)/step_size
        converter = converters.slider_converter(start=start, stop=stop, num_steps=num_steps, cast=cast)
        _slider_base.__init__(self, converter=converter, num_steps=num_steps, **kwargs)