Exemplo n.º 1
0
    def __init__(self, parent = None,
                       minimum = camelot_minint,
                       maximum = camelot_maxint,
                       calculator = True,
                       option = None, 
                       field_name = 'integer',
                       **kwargs):
        
        CustomEditor.__init__(self, parent)
        self.setObjectName( field_name )
        action = QtGui.QAction(self)
        action.setShortcut( QtGui.QKeySequence( Qt.Key_F4 ) )
        self.setFocusPolicy(Qt.StrongFocus)
        
        self.spinBox = CustomDoubleSpinBox(option, parent)
        self.spinBox.setRange(minimum, maximum)
        self.spinBox.setDecimals(0)
        self.spinBox.setAlignment(Qt.AlignRight|Qt.AlignVCenter)
        self.spinBox.addAction(action)
        self.spinBox.lineEdit().setText('')
        
        self.calculatorButton = QtGui.QToolButton()
        self.calculatorButton.setIcon(self.calculator_icon.getQIcon())
        self.calculatorButton.setAutoRaise(True)
        self.calculatorButton.setFocusPolicy(Qt.ClickFocus)
        self.calculatorButton.setFixedHeight(self.get_height())
        self.calculatorButton.clicked.connect(
            lambda:self.popupCalculator(self.spinBox.value())
        )
        action.triggered.connect(
            lambda:self.popupCalculator(self.spinBox.value())
        )
        self.spinBox.editingFinished.connect( self.spinbox_editing_finished )

        layout = QtGui.QHBoxLayout()
        layout.setContentsMargins( 0, 0, 0, 0)
        layout.setSpacing(0)
        layout.addWidget(self.spinBox)
        layout.addWidget(self.calculatorButton)
        self.setFocusProxy(self.spinBox)
        self.setLayout(layout)
        self._nullable = True
        self._calculator = calculator
        
        self.option = option
Exemplo n.º 2
0
    def __init__(self, parent, precision=2, reverse=False, neutral=False, option=None, field_name="float", **kwargs):
        CustomEditor.__init__(self, parent)
        self.setObjectName(field_name)
        action = QtGui.QAction(self)
        action.setShortcut(QtGui.QKeySequence(Qt.Key_F4))
        self.setFocusPolicy(Qt.StrongFocus)

        self.spinBox = CustomDoubleSpinBox(option, parent)
        self.spinBox.setDecimals(precision)
        self.spinBox.setAlignment(Qt.AlignRight | Qt.AlignVCenter)
        self.spinBox.addAction(action)
        self.spinBox.setButtonSymbols(QtGui.QAbstractSpinBox.NoButtons)

        self.arrow = QtGui.QLabel()
        self.arrow.setPixmap(self.go_up.getQPixmap())
        self.arrow.setFixedHeight(self.get_height())

        self.arrow.setAutoFillBackground(False)
        self.arrow.setMaximumWidth(19)

        self.calculatorButton = QtGui.QToolButton()
        self.calculatorButton.setIcon(self.calculator_icon.getQIcon())
        self.calculatorButton.setAutoRaise(True)
        self.calculatorButton.setFixedHeight(self.get_height())

        self.calculatorButton.clicked.connect(lambda: self.popupCalculator(self.spinBox.value()))
        action.triggered.connect(lambda: self.popupCalculator(self.spinBox.value()))
        self.spinBox.editingFinished.connect(self.spinbox_editing_finished)

        self.releaseKeyboard()

        layout = QtGui.QHBoxLayout()
        layout.setContentsMargins(0, 0, 0, 0)
        layout.setSpacing(0)
        layout.addSpacing(3.5)
        layout.addWidget(self.arrow)
        layout.addWidget(self.spinBox)
        layout.addWidget(self.calculatorButton)
        self.reverse = reverse
        self.neutral = neutral
        self.setFocusProxy(self.spinBox)
        self.setLayout(layout)
        if not self.reverse:
            if not self.neutral:
                self.icons = {-1: self.go_down_red, 1: self.go_up, 0: self.zero}
            else:
                self.icons = {-1: self.go_down_blue, 1: self.go_up_blue, 0: self.zero}
        else:
            self.icons = {1: self.go_down_red, -1: self.go_up, 0: self.zero}
Exemplo n.º 3
0
    def __init__(self,
                 parent,
                 precision=2,
                 reverse=False,
                 neutral=False,
                 **kwargs):
        CustomEditor.__init__(self, parent)
        action = QtGui.QAction(self)
        action.setShortcut(Qt.Key_F3)
        self.setFocusPolicy(Qt.StrongFocus)
        self.spinBox = CustomDoubleSpinBox(parent)

        self.spinBox.setDecimals(precision)
        self.spinBox.setAlignment(Qt.AlignRight | Qt.AlignVCenter)
        self.spinBox.addAction(action)
        self.spinBox.setButtonSymbols(QtGui.QAbstractSpinBox.NoButtons)
        self.arrow = QtGui.QLabel()
        self.arrow.setPixmap(
            Icon('tango/16x16/actions/go-up.png').getQPixmap())
        self.arrow.setFixedHeight(self.get_height())

        self.arrow.setAutoFillBackground(False)
        self.arrow.setMaximumWidth(19)

        self.calculatorButton = QtGui.QToolButton()
        icon = Icon('tango/16x16/apps/accessories-calculator.png').getQIcon()
        self.calculatorButton.setIcon(icon)
        self.calculatorButton.setAutoRaise(True)
        self.calculatorButton.setFixedHeight(self.get_height())

        self.calculatorButton.clicked.connect(
            lambda: self.popupCalculator(self.spinBox.value()))
        action.triggered.connect(
            lambda: self.popupCalculator(self.spinBox.value()))
        self.spinBox.editingFinished.connect(self.spinbox_editing_finished)

        self.releaseKeyboard()

        layout = QtGui.QHBoxLayout()
        layout.setMargin(0)
        layout.setSpacing(0)
        layout.addSpacing(3.5)
        layout.addWidget(self.arrow)
        layout.addWidget(self.spinBox)
        layout.addWidget(self.calculatorButton)
        self.reverse = reverse
        self.neutral = neutral
        self.setFocusProxy(self.spinBox)
        self.setLayout(layout)
        if not self.reverse:
            if not self.neutral:
                self.icons = {
                    -1:
                    Icon('tango/16x16/actions/go-down-red.png').getQPixmap(),
                    1: Icon('tango/16x16/actions/go-up.png').getQPixmap(),
                    0: Icon('tango/16x16/actions/zero.png').getQPixmap()
                }
            else:
                self.icons = {
                    -1:
                    Icon('tango/16x16/actions/go-down-blue.png').getQPixmap(),
                    1: Icon('tango/16x16/actions/go-up-blue.png').getQPixmap(),
                    0: Icon('tango/16x16/actions/zero.png').getQPixmap()
                }
        else:
            self.icons = {
                1: Icon('tango/16x16/actions/go-down-red.png').getQPixmap(),
                -1: Icon('tango/16x16/actions/go-up.png').getQPixmap(),
                0: Icon('tango/16x16/actions/zero.png').getQPixmap()
            }
Exemplo n.º 4
0
class ColoredFloatEditor(CustomEditor):
    """Widget for editing a float field, with a calculator"""
    def __init__(self,
                 parent,
                 precision=2,
                 reverse=False,
                 neutral=False,
                 **kwargs):
        CustomEditor.__init__(self, parent)
        action = QtGui.QAction(self)
        action.setShortcut(Qt.Key_F3)
        self.setFocusPolicy(Qt.StrongFocus)
        self.spinBox = CustomDoubleSpinBox(parent)

        self.spinBox.setDecimals(precision)
        self.spinBox.setAlignment(Qt.AlignRight | Qt.AlignVCenter)
        self.spinBox.addAction(action)
        self.spinBox.setButtonSymbols(QtGui.QAbstractSpinBox.NoButtons)
        self.arrow = QtGui.QLabel()
        self.arrow.setPixmap(
            Icon('tango/16x16/actions/go-up.png').getQPixmap())
        self.arrow.setFixedHeight(self.get_height())

        self.arrow.setAutoFillBackground(False)
        self.arrow.setMaximumWidth(19)

        self.calculatorButton = QtGui.QToolButton()
        icon = Icon('tango/16x16/apps/accessories-calculator.png').getQIcon()
        self.calculatorButton.setIcon(icon)
        self.calculatorButton.setAutoRaise(True)
        self.calculatorButton.setFixedHeight(self.get_height())

        self.calculatorButton.clicked.connect(
            lambda: self.popupCalculator(self.spinBox.value()))
        action.triggered.connect(
            lambda: self.popupCalculator(self.spinBox.value()))
        self.spinBox.editingFinished.connect(self.spinbox_editing_finished)

        self.releaseKeyboard()

        layout = QtGui.QHBoxLayout()
        layout.setMargin(0)
        layout.setSpacing(0)
        layout.addSpacing(3.5)
        layout.addWidget(self.arrow)
        layout.addWidget(self.spinBox)
        layout.addWidget(self.calculatorButton)
        self.reverse = reverse
        self.neutral = neutral
        self.setFocusProxy(self.spinBox)
        self.setLayout(layout)
        if not self.reverse:
            if not self.neutral:
                self.icons = {
                    -1:
                    Icon('tango/16x16/actions/go-down-red.png').getQPixmap(),
                    1: Icon('tango/16x16/actions/go-up.png').getQPixmap(),
                    0: Icon('tango/16x16/actions/zero.png').getQPixmap()
                }
            else:
                self.icons = {
                    -1:
                    Icon('tango/16x16/actions/go-down-blue.png').getQPixmap(),
                    1: Icon('tango/16x16/actions/go-up-blue.png').getQPixmap(),
                    0: Icon('tango/16x16/actions/zero.png').getQPixmap()
                }
        else:
            self.icons = {
                1: Icon('tango/16x16/actions/go-down-red.png').getQPixmap(),
                -1: Icon('tango/16x16/actions/go-up.png').getQPixmap(),
                0: Icon('tango/16x16/actions/zero.png').getQPixmap()
            }

    def set_field_attributes(self,
                             editable=True,
                             background_color=None,
                             prefix='',
                             suffix='',
                             minimum=constants.camelot_minfloat,
                             maximum=constants.camelot_maxfloat,
                             single_step=1.0,
                             **kwargs):
        self.set_enabled(editable)
        self.set_background_color(background_color)
        self.spinBox.setPrefix(u'%s ' % (unicode(prefix).lstrip()))
        self.spinBox.setSuffix(u' %s' % (unicode(suffix).rstrip()))
        self.spinBox.setRange(minimum, maximum)
        self.spinBox.setSingleStep(single_step)

    def set_enabled(self, editable=True):
        self.spinBox.setReadOnly(not editable)
        self.spinBox.setEnabled(editable)
        self.calculatorButton.setShown(editable)
        if editable:
            self.spinBox.setButtonSymbols(QtGui.QAbstractSpinBox.UpDownArrows)
        else:
            self.spinBox.setButtonSymbols(QtGui.QAbstractSpinBox.NoButtons)

    def set_value(self, value):
        value = CustomEditor.set_value(self, value) or 0.0
        self.spinBox.setValue(value)
        self.arrow.setPixmap(self.icons[cmp(value, 0)])

    def get_value(self):
        self.spinBox.interpretText()
        value = self.spinBox.value()
        return CustomEditor.get_value(self) or value

    def popupCalculator(self, value):
        from camelot.view.controls.calculator import Calculator
        calculator = Calculator()
        calculator.setValue(value)
        calculator.calculation_finished_signal.connect(
            self.calculation_finished)
        calculator.exec_()

    def calculation_finished(self, value):
        self.spinBox.setValue(float(unicode(value)))
        self.editingFinished.emit()

    @QtCore.pyqtSlot()
    def spinbox_editing_finished(self):
        self.editingFinished.emit()

    def set_background_color(self, background_color):
        if background_color not in (None, ValueLoading):
            selfpalette = self.spinBox.palette()
            sbpalette = self.spinBox.palette()
            lepalette = self.spinBox.lineEdit().palette()
            for x in [
                    QtGui.QPalette.Active, QtGui.QPalette.Inactive,
                    QtGui.QPalette.Disabled
            ]:
                for y in [
                        self.backgroundRole(), QtGui.QPalette.Window,
                        QtGui.QPalette.Base
                ]:
                    selfpalette.setColor(x, y, background_color)
                for y in [
                        self.spinBox.backgroundRole(), QtGui.QPalette.Window,
                        QtGui.QPalette.Base
                ]:
                    sbpalette.setColor(x, y, background_color)
                for y in [
                        self.spinBox.lineEdit().backgroundRole(),
                        QtGui.QPalette.Window, QtGui.QPalette.Base
                ]:
                    lepalette.setColor(x, y, background_color)
            self.setPalette(selfpalette)
            self.spinBox.setPalette(sbpalette)
            self.spinBox.lineEdit().setPalette(lepalette)
            return True
        else:
            return False
Exemplo n.º 5
0
class IntegerEditor(CustomEditor):
    """Widget for editing an integer field, with a calculator

Special use cases of the IntegerEditor :

case 1
------

we have a required integer field without a default.

so the model will do set_value( None )

since this is a required field, the user should be able
to enter a value, 0 is a legitimate value.

when get_value is called, 0 should be returned if the
user has set the editor to 0, and None if the user didn't
touch the editor.

so the editor should make a visual difference between
None and 0, so the user can see he didn't enter something
yet

case 2
------

we have a non required integer field without a default

the model will do set_value( None )

the get_value() should return None and not 0.  because
in case it returns 0, 0 will be written to the db, causing
an unneeded update of the db.

"""

    calculator_icon = Icon('tango/16x16/apps/accessories-calculator.png')
    
    def __init__(self, parent = None,
                       minimum = camelot_minint,
                       maximum = camelot_maxint,
                       calculator = True,
                       option = None, 
                       field_name = 'integer',
                       **kwargs):
        
        CustomEditor.__init__(self, parent)
        self.setObjectName( field_name )
        action = QtGui.QAction(self)
        action.setShortcut( QtGui.QKeySequence( Qt.Key_F4 ) )
        self.setFocusPolicy(Qt.StrongFocus)
        
        self.spinBox = CustomDoubleSpinBox(option, parent)
        self.spinBox.setRange(minimum, maximum)
        self.spinBox.setDecimals(0)
        self.spinBox.setAlignment(Qt.AlignRight|Qt.AlignVCenter)
        self.spinBox.addAction(action)
        self.spinBox.lineEdit().setText('')
        
        self.calculatorButton = QtGui.QToolButton()
        self.calculatorButton.setIcon(self.calculator_icon.getQIcon())
        self.calculatorButton.setAutoRaise(True)
        self.calculatorButton.setFocusPolicy(Qt.ClickFocus)
        self.calculatorButton.setFixedHeight(self.get_height())
        self.calculatorButton.clicked.connect(
            lambda:self.popupCalculator(self.spinBox.value())
        )
        action.triggered.connect(
            lambda:self.popupCalculator(self.spinBox.value())
        )
        self.spinBox.editingFinished.connect( self.spinbox_editing_finished )

        layout = QtGui.QHBoxLayout()
        layout.setContentsMargins( 0, 0, 0, 0)
        layout.setSpacing(0)
        layout.addWidget(self.spinBox)
        layout.addWidget(self.calculatorButton)
        self.setFocusProxy(self.spinBox)
        self.setLayout(layout)
        self._nullable = True
        self._calculator = calculator
        
        self.option = option

    def set_field_attributes(self, editable = True,
                                   background_color = None, 
                                   tooltip = None,
                                   prefix = '',
                                   suffix = '',
                                   nullable = True,
                                   single_step = 1, **kwargs):
        self.set_enabled(editable)
        set_background_color_palette(self.spinBox.lineEdit(), background_color )

        self.spinBox.setToolTip(unicode(tooltip or ''))
        
        if prefix:
            self.spinBox.setPrefix(u'%s '%(unicode(prefix).lstrip()))
        else:
            self.spinBox.setPrefix('')
        if suffix:
            self.spinBox.setSuffix(u' %s'%(unicode(suffix).rstrip()))
        else:
            self.spinBox.setSuffix(u'')
        
        self.spinBox.setSingleStep(single_step)
        self._nullable = nullable

    def set_value(self, value):
        value = CustomEditor.set_value(self, value)
        if value is None:
            self.spinBox.lineEdit().setText('')
        else:
            value = str(value).replace(',', '.')
            self.spinBox.setValue(eval(value))

    def get_value(self):
        value_loading = CustomEditor.get_value(self)
        if value_loading is not None:
            return value_loading

        if self.spinBox.text()=='':
            return None
        
        self.spinBox.interpretText()
        value = int(self.spinBox.value())
        return value

    def set_enabled(self, editable=True):
        self.spinBox.setReadOnly(not editable)
        self.spinBox.setEnabled(editable)
        
        # Version '5' indicates that this widget is put into a form.
        # If so, the calculatorButton and the spinBox's controls should be hidden.
        if self.option and self.option.version != 5:
            self.calculatorButton.hide()
            self.spinBox.setButtonSymbols(QtGui.QAbstractSpinBox.NoButtons)
        else:
            self.calculatorButton.setVisible(editable and self._calculator)
            if not editable:
                self.spinBox.setButtonSymbols(QtGui.QAbstractSpinBox.NoButtons)

    def popupCalculator(self, value):
        from camelot.view.controls.calculator import Calculator
        calculator = Calculator(self)
        calculator.setValue(value)
        calculator.calculation_finished_signal.connect( self.calculation_finished )
        calculator.exec_()

    @QtCore.pyqtSlot(QtCore.QString)
    def calculation_finished(self, value):
        self.spinBox.setValue(floor(float(unicode(value))))
        self.editingFinished.emit()

    @QtCore.pyqtSlot()
    def spinbox_editing_finished(self):
        self.editingFinished.emit()
Exemplo n.º 6
0
class ColoredFloatEditor(CustomEditor):
    """Widget for editing a float field, with a calculator"""

    calculator_icon = Icon("tango/16x16/apps/accessories-calculator.png")
    zero = Icon("tango/16x16/actions/zero.png")
    go_down_red = Icon("tango/16x16/actions/go-down-red.png")
    go_up = Icon("tango/16x16/actions/go-up.png")
    go_down_blue = Icon("tango/16x16/actions/go-down-blue.png")
    go_up_blue = Icon("tango/16x16/actions/go-up-blue.png")

    def __init__(self, parent, precision=2, reverse=False, neutral=False, option=None, field_name="float", **kwargs):
        CustomEditor.__init__(self, parent)
        self.setObjectName(field_name)
        action = QtGui.QAction(self)
        action.setShortcut(QtGui.QKeySequence(Qt.Key_F4))
        self.setFocusPolicy(Qt.StrongFocus)

        self.spinBox = CustomDoubleSpinBox(option, parent)
        self.spinBox.setDecimals(precision)
        self.spinBox.setAlignment(Qt.AlignRight | Qt.AlignVCenter)
        self.spinBox.addAction(action)
        self.spinBox.setButtonSymbols(QtGui.QAbstractSpinBox.NoButtons)

        self.arrow = QtGui.QLabel()
        self.arrow.setPixmap(self.go_up.getQPixmap())
        self.arrow.setFixedHeight(self.get_height())

        self.arrow.setAutoFillBackground(False)
        self.arrow.setMaximumWidth(19)

        self.calculatorButton = QtGui.QToolButton()
        self.calculatorButton.setIcon(self.calculator_icon.getQIcon())
        self.calculatorButton.setAutoRaise(True)
        self.calculatorButton.setFixedHeight(self.get_height())

        self.calculatorButton.clicked.connect(lambda: self.popupCalculator(self.spinBox.value()))
        action.triggered.connect(lambda: self.popupCalculator(self.spinBox.value()))
        self.spinBox.editingFinished.connect(self.spinbox_editing_finished)

        self.releaseKeyboard()

        layout = QtGui.QHBoxLayout()
        layout.setContentsMargins(0, 0, 0, 0)
        layout.setSpacing(0)
        layout.addSpacing(3.5)
        layout.addWidget(self.arrow)
        layout.addWidget(self.spinBox)
        layout.addWidget(self.calculatorButton)
        self.reverse = reverse
        self.neutral = neutral
        self.setFocusProxy(self.spinBox)
        self.setLayout(layout)
        if not self.reverse:
            if not self.neutral:
                self.icons = {-1: self.go_down_red, 1: self.go_up, 0: self.zero}
            else:
                self.icons = {-1: self.go_down_blue, 1: self.go_up_blue, 0: self.zero}
        else:
            self.icons = {1: self.go_down_red, -1: self.go_up, 0: self.zero}

    def set_field_attributes(
        self,
        editable=True,
        background_color=None,
        tooltip=None,
        prefix="",
        suffix="",
        minimum=constants.camelot_minfloat,
        maximum=constants.camelot_maxfloat,
        single_step=1.0,
        **kwargs
    ):
        self.set_enabled(editable)
        self.set_background_color(background_color)
        self.setToolTip(unicode(tooltip or ""))
        self.spinBox.setPrefix(u"%s " % (unicode(prefix).lstrip()))
        self.spinBox.setSuffix(u" %s" % (unicode(suffix).rstrip()))
        self.spinBox.setRange(minimum, maximum)
        self.spinBox.setSingleStep(single_step)

    def set_enabled(self, editable=True):
        self.spinBox.setReadOnly(not editable)
        self.spinBox.setEnabled(editable)
        self.calculatorButton.setShown(editable)
        if editable:
            self.spinBox.setButtonSymbols(QtGui.QAbstractSpinBox.UpDownArrows)
        else:
            self.spinBox.setButtonSymbols(QtGui.QAbstractSpinBox.NoButtons)

    def set_value(self, value):
        value = CustomEditor.set_value(self, value) or 0.0
        self.spinBox.setValue(value)
        self.arrow.setPixmap(self.icons[cmp(value, 0)].getQPixmap())

    def get_value(self):
        self.spinBox.interpretText()
        value = self.spinBox.value()
        return CustomEditor.get_value(self) or value

    def popupCalculator(self, value):
        from camelot.view.controls.calculator import Calculator

        calculator = Calculator()
        calculator.setValue(value)
        calculator.calculation_finished_signal.connect(self.calculation_finished)
        calculator.exec_()

    def calculation_finished(self, value):
        self.spinBox.setValue(float(unicode(value)))
        self.editingFinished.emit()

    @QtCore.pyqtSlot()
    def spinbox_editing_finished(self):
        self.editingFinished.emit()

    def set_background_color(self, background_color):
        set_background_color_palette(self.spinBox.lineEdit(), background_color)
Exemplo n.º 7
0
    def __init__(self,
                 parent,
                 precision=2,
                 reverse=False,
                 neutral=False,
                 option=None,
                 field_name='float',
                 **kwargs):
        CustomEditor.__init__(self, parent)
        self.setObjectName(field_name)
        action = QtGui.QAction(self)
        action.setShortcut(QtGui.QKeySequence(Qt.Key_F4))
        self.setFocusPolicy(Qt.StrongFocus)

        self.spinBox = CustomDoubleSpinBox(option, parent)
        self.spinBox.setDecimals(precision)
        self.spinBox.setAlignment(Qt.AlignRight | Qt.AlignVCenter)
        self.spinBox.addAction(action)
        self.spinBox.setButtonSymbols(QtGui.QAbstractSpinBox.NoButtons)

        self.arrow = QtGui.QLabel()
        self.arrow.setPixmap(self.go_up.getQPixmap())
        self.arrow.setFixedHeight(self.get_height())

        self.arrow.setAutoFillBackground(False)
        self.arrow.setMaximumWidth(19)

        self.calculatorButton = QtGui.QToolButton()
        self.calculatorButton.setIcon(self.calculator_icon.getQIcon())
        self.calculatorButton.setAutoRaise(True)
        self.calculatorButton.setFixedHeight(self.get_height())

        self.calculatorButton.clicked.connect(
            lambda: self.popupCalculator(self.spinBox.value()))
        action.triggered.connect(
            lambda: self.popupCalculator(self.spinBox.value()))
        self.spinBox.editingFinished.connect(self.spinbox_editing_finished)

        self.releaseKeyboard()

        layout = QtGui.QHBoxLayout()
        layout.setContentsMargins(0, 0, 0, 0)
        layout.setSpacing(0)
        layout.addSpacing(3.5)
        layout.addWidget(self.arrow)
        layout.addWidget(self.spinBox)
        layout.addWidget(self.calculatorButton)
        self.reverse = reverse
        self.neutral = neutral
        self.setFocusProxy(self.spinBox)
        self.setLayout(layout)
        if not self.reverse:
            if not self.neutral:
                self.icons = {
                    -1: self.go_down_red,
                    1: self.go_up,
                    0: self.zero,
                }
            else:
                self.icons = {
                    -1: self.go_down_blue,
                    1: self.go_up_blue,
                    0: self.zero,
                }
        else:
            self.icons = {
                1: self.go_down_red,
                -1: self.go_up,
                0: self.zero,
            }
Exemplo n.º 8
0
class ColoredFloatEditor(CustomEditor):
    """Widget for editing a float field, with a calculator"""

    calculator_icon = Icon('tango/16x16/apps/accessories-calculator.png')
    zero = Icon('tango/16x16/actions/zero.png')
    go_down_red = Icon('tango/16x16/actions/go-down-red.png')
    go_up = Icon('tango/16x16/actions/go-up.png')
    go_down_blue = Icon('tango/16x16/actions/go-down-blue.png')
    go_up_blue = Icon('tango/16x16/actions/go-up-blue.png')

    def __init__(self,
                 parent,
                 precision=2,
                 reverse=False,
                 neutral=False,
                 option=None,
                 field_name='float',
                 **kwargs):
        CustomEditor.__init__(self, parent)
        self.setObjectName(field_name)
        action = QtGui.QAction(self)
        action.setShortcut(QtGui.QKeySequence(Qt.Key_F4))
        self.setFocusPolicy(Qt.StrongFocus)

        self.spinBox = CustomDoubleSpinBox(option, parent)
        self.spinBox.setDecimals(precision)
        self.spinBox.setAlignment(Qt.AlignRight | Qt.AlignVCenter)
        self.spinBox.addAction(action)
        self.spinBox.setButtonSymbols(QtGui.QAbstractSpinBox.NoButtons)

        self.arrow = QtGui.QLabel()
        self.arrow.setPixmap(self.go_up.getQPixmap())
        self.arrow.setFixedHeight(self.get_height())

        self.arrow.setAutoFillBackground(False)
        self.arrow.setMaximumWidth(19)

        self.calculatorButton = QtGui.QToolButton()
        self.calculatorButton.setIcon(self.calculator_icon.getQIcon())
        self.calculatorButton.setAutoRaise(True)
        self.calculatorButton.setFixedHeight(self.get_height())

        self.calculatorButton.clicked.connect(
            lambda: self.popupCalculator(self.spinBox.value()))
        action.triggered.connect(
            lambda: self.popupCalculator(self.spinBox.value()))
        self.spinBox.editingFinished.connect(self.spinbox_editing_finished)

        self.releaseKeyboard()

        layout = QtGui.QHBoxLayout()
        layout.setContentsMargins(0, 0, 0, 0)
        layout.setSpacing(0)
        layout.addSpacing(3.5)
        layout.addWidget(self.arrow)
        layout.addWidget(self.spinBox)
        layout.addWidget(self.calculatorButton)
        self.reverse = reverse
        self.neutral = neutral
        self.setFocusProxy(self.spinBox)
        self.setLayout(layout)
        if not self.reverse:
            if not self.neutral:
                self.icons = {
                    -1: self.go_down_red,
                    1: self.go_up,
                    0: self.zero,
                }
            else:
                self.icons = {
                    -1: self.go_down_blue,
                    1: self.go_up_blue,
                    0: self.zero,
                }
        else:
            self.icons = {
                1: self.go_down_red,
                -1: self.go_up,
                0: self.zero,
            }

    def set_field_attributes(self,
                             editable=True,
                             background_color=None,
                             tooltip=None,
                             prefix='',
                             suffix='',
                             minimum=constants.camelot_minfloat,
                             maximum=constants.camelot_maxfloat,
                             single_step=1.0,
                             **kwargs):
        self.set_enabled(editable)
        self.set_background_color(background_color)
        self.setToolTip(unicode(tooltip or ''))
        self.spinBox.setPrefix(u'%s ' % (unicode(prefix).lstrip()))
        self.spinBox.setSuffix(u' %s' % (unicode(suffix).rstrip()))
        self.spinBox.setRange(minimum, maximum)
        self.spinBox.setSingleStep(single_step)

    def set_enabled(self, editable=True):
        self.spinBox.setReadOnly(not editable)
        self.spinBox.setEnabled(editable)
        self.calculatorButton.setShown(editable)
        if editable:
            self.spinBox.setButtonSymbols(QtGui.QAbstractSpinBox.UpDownArrows)
        else:
            self.spinBox.setButtonSymbols(QtGui.QAbstractSpinBox.NoButtons)

    def set_value(self, value):
        value = CustomEditor.set_value(self, value) or 0.0
        self.spinBox.setValue(value)
        self.arrow.setPixmap(self.icons[cmp(value, 0)].getQPixmap())

    def get_value(self):
        self.spinBox.interpretText()
        value = self.spinBox.value()
        return CustomEditor.get_value(self) or value

    def popupCalculator(self, value):
        from camelot.view.controls.calculator import Calculator
        calculator = Calculator()
        calculator.setValue(value)
        calculator.calculation_finished_signal.connect(
            self.calculation_finished)
        calculator.exec_()

    def calculation_finished(self, value):
        self.spinBox.setValue(float(unicode(value)))
        self.editingFinished.emit()

    @QtCore.pyqtSlot()
    def spinbox_editing_finished(self):
        self.editingFinished.emit()

    def set_background_color(self, background_color):
        set_background_color_palette(self.spinBox.lineEdit(), background_color)