Package Camelot :: Package camelot :: Package view :: Package controls :: Package editors :: Module coloredfloateditor |
[frames] | no frames]

# Source Code for Module Camelot.camelot.view.controls.editors.coloredfloateditor

      1  from PyQt4 import QtGui, QtCore
      2  from PyQt4.QtCore import Qt
      3

      4  from customeditor import CustomEditor
      5  from camelot.core import constants
      6  from camelot.view.art import Icon
      7

      8 -class ColoredFloatEditor(CustomEditor):
      9      """Widget for editing a float field, with a calculator"""
     10

     11 -    def __init__(self,

     12                   parent,

     13                   precision=2,

     14                   minimum=constants.camelot_minfloat,

     15                   maximum=constants.camelot_maxfloat,

     16                   prefix='',

     17                   suffix='',

     18                   editable=True,

     19                   reverse=False,

     20                   neutral=False,

     21                   **kwargs):
     22          CustomEditor.__init__(self, parent)
     23          action = QtGui.QAction(self)
     24          action.setShortcut(Qt.Key_F3)
     25          self.setFocusPolicy(Qt.StrongFocus)
     26          self.spinBox = QtGui.QDoubleSpinBox(parent)
     27          self.spinBox.setReadOnly(not editable)
     28          self.spinBox.setDisabled(not editable)
     29          self.spinBox.setEnabled(editable)
     30          self.spinBox.setPrefix(prefix)
     31          self.spinBox.setSuffix(suffix)
     32          self.spinBox.setRange(minimum, maximum)
     33          self.spinBox.setDecimals(precision)
     34          self.spinBox.setAlignment(Qt.AlignRight|Qt.AlignVCenter)
     35          self.spinBox.setSingleStep(1.0)
     36          self.spinBox.addAction(action)
     37          self.spinBox.setButtonSymbols(QtGui.QAbstractSpinBox.NoButtons)
     38          self.arrow = QtGui.QLabel()
     39          self.arrow.setPixmap(Icon('tango/16x16/actions/go-up.png').getQPixmap())
     40          self.arrow.setFixedHeight(self.get_height())
     41

     42          self.arrow.setAutoFillBackground(False)
     43          self.arrow.setMaximumWidth(19)
     44

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

     51          self.connect(self.calculatorButton,

     52                       QtCore.SIGNAL('clicked()'),

     53                       lambda:self.popupCalculator(self.spinBox.value()))
     54          self.connect(action,

     55                       QtCore.SIGNAL('triggered(bool)'),

     56                       lambda:self.popupCalculator(self.spinBox.value()))
     57          self.connect(self.spinBox,

     58                       QtCore.SIGNAL('editingFinished()'),

     59                       lambda:self.editingFinished(self.spinBox.value()))
     60

     61          self.releaseKeyboard()
     62

     63          layout = QtGui.QHBoxLayout()
     64          layout.setMargin(0)
     65          layout.setSpacing(0)
     66          layout.addSpacing(3.5)
     67          layout.addWidget(self.arrow)
     68          layout.addWidget(self.spinBox)
     69          if editable:
     70              layout.addWidget(self.calculatorButton)
     71          self.reverse = reverse
     72          self.neutral = neutral
     73          self.setFocusProxy(self.spinBox)
     74          self.setLayout(layout)
     75          if not self.reverse:
     76              if not self.neutral:
     77                  self.icons = {

     78                      -1:Icon('tango/16x16/actions/go-down-red.png').getQPixmap(),

     79                      1:Icon('tango/16x16/actions/go-up.png').getQPixmap(),

     80                      0:Icon('tango/16x16/actions/zero.png').getQPixmap()

     81                  }
     82              else:
     83                  self.icons = {

     84                      -1:Icon('tango/16x16/actions/go-down-blue.png').getQPixmap(),

     85                      1:Icon('tango/16x16/actions/go-up-blue.png').getQPixmap(),

     86                      0:Icon('tango/16x16/actions/zero.png').getQPixmap()

     87                  }
     88          else:
     89              self.icons = {

     90                  1:Icon('tango/16x16/actions/go-down-red.png').getQPixmap(),

     91                  -1:Icon('tango/16x16/actions/go-up.png').getQPixmap(),

     92                  0:Icon('tango/16x16/actions/zero.png').getQPixmap()

     93              }
     94

     95 -    def set_value(self, value):
     96          value = CustomEditor.set_value(self, value) or 0.0
     97          self.spinBox.setValue(value)
     98          self.arrow.setPixmap(self.icons[cmp(value,0)])
     99

    100 -    def get_value(self):
    101          self.spinBox.interpretText()
    102          value = self.spinBox.value()
    103          return CustomEditor.get_value(self) or value
    104

    105 -    def set_enabled(self, editable=True):
    106          if self.spinBox.isEnabled() != editable:
    107              if not editable:
    108                  self.layout().removeWidget(self.calculatorButton)
    109              else:
    110                  self.layout().addWidget(self.calculatorButton)
    111              self.spinBox.setEnabled(editable)
    112

    113 -    def popupCalculator(self, value):
    114          from camelot.view.controls.calculator import Calculator
    115          calculator = Calculator(self)
    116          calculator.setValue(value)
    117          self.connect(calculator,

    118                       QtCore.SIGNAL('calculationFinished'),

    119                       self.calculationFinished)
    120          calculator.exec_()
    121

    122 -    def calculationFinished(self, value):
    123          self.spinBox.setValue(float(value))
    124          self.emit(QtCore.SIGNAL('editingFinished()'))
    125

    126 -    def editingFinished(self, value):
    127          self.emit(QtCore.SIGNAL('editingFinished()'))
    128