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

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

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

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

      9 -class CustomDoubleSpinBox(QtGui.QDoubleSpinBox):
     10      """Spinbox that doesn't accept mouse scrolling as input"""
     11

     12 -    def wheelEvent(self, wheel_event):
     13          wheel_event.ignore()
     14

     15 -    def textFromValue(self, value):
     16          return str( QtCore.QString("%L1").arg(float(value), 0, 'f', self.decimals()) )
     17

     18      # def valueFromText(self, text):

     19      # maybe construct some cases to make other input formats possible

     20      #   return text

     21

     22 -class FloatEditor(CustomEditor):
     23      """Widget for editing a float field, with a calculator"""
     24

     25 -    def __init__(self,

     26                   parent,

     27                   precision=2,

     28                   minimum=constants.camelot_minfloat,

     29                   maximum=constants.camelot_maxfloat,

     30                   editable=True,

     31                   prefix='',

     32                   suffix='',

     33                   calculator=True,

     34                   **kwargs):
     35          CustomEditor.__init__(self, parent)
     36          action = QtGui.QAction(self)
     37          action.setShortcut(Qt.Key_F3)
     38          self.setFocusPolicy(Qt.StrongFocus)
     39          self.precision = precision
     40          self.spinBox = CustomDoubleSpinBox(parent)
     41          self.spinBox.setReadOnly(not editable)
     42          self.spinBox.setEnabled(editable)
     43          self.spinBox.setDisabled(not editable)
     44          self.spinBox.setRange(minimum, maximum)
     45          self.spinBox.setDecimals(precision)
     46          self.spinBox.setAlignment(Qt.AlignRight|Qt.AlignVCenter)
     47          self.spinBox.setSingleStep(1.0)
     48

     49          prefix = str(prefix) + ' '
     50          prefix = prefix.lstrip()
     51

     52          suffix = ' ' + str(suffix)
     53          suffix = suffix.rstrip()
     54

     55          self.spinBox.setPrefix(prefix)
     56          self.spinBox.setSuffix(suffix)
     57          self.spinBox.addAction(action)
     58          self.calculatorButton = QtGui.QToolButton()
     59          icon = Icon('tango/16x16/apps/accessories-calculator.png').getQIcon()
     60          self.calculatorButton.setIcon(icon)
     61          self.calculatorButton.setAutoRaise(True)
     62          self.calculatorButton.setFixedHeight(self.get_height())
     63

     64          self.connect(self.calculatorButton,

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

     66                       lambda:self.popupCalculator(self.spinBox.value()))
     67          self.connect(action,

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

     69                       lambda:self.popupCalculator(self.spinBox.value()))
     70          self.connect(self.spinBox,

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

     72                       lambda:self.editingFinished(self.spinBox.value()))
     73

     74          self.releaseKeyboard()
     75

     76          layout = QtGui.QHBoxLayout()
     77          layout.setMargin(0)
     78          layout.setSpacing(0)
     79          layout.addWidget(self.spinBox)
     80          if editable and calculator:
     81              layout.addWidget(self.calculatorButton)
     82          if not editable:
     83              self.spinBox.setButtonSymbols(QtGui.QAbstractSpinBox.NoButtons)
     84              self.spinBox.setEnabled(False)
     85          self.setFocusProxy(self.spinBox)
     86          self.setLayout(layout)
     87

     88 -    def set_value(self, value):
     89          value = CustomEditor.set_value(self, value)
     90          if value:
     91              self.spinBox.setValue(value)
     92          else:
     93              self.spinBox.setValue(0.0)
     94

     95 -    def set_enabled(self, editable=True):
     96          if self.spinBox.isEnabled() != editable:
     97              if not editable:
     98                  self.layout().removeWidget(self.calculatorButton)
     99              else:
    100                  self.layout().addWidget(self.calculatorButton)
    101              self.spinBox.setEnabled(editable)
    102

    103 -    def get_value(self):
    104          self.spinBox.interpretText()
    105          value = self.spinBox.value()
    106          return CustomEditor.get_value(self) or value
    107

    108 -    def popupCalculator(self, value):
    109          from camelot.view.controls.calculator import Calculator
    110          calculator = Calculator(self)
    111          calculator.setValue(value)
    112          self.connect(calculator,

    113                       QtCore.SIGNAL('calculationFinished'),

    114                       self.calculationFinished)
    115          calculator.exec_()
    116

    117 -    def calculationFinished(self, value):
    118          self.spinBox.setValue(float(value))
    119          self.emit(QtCore.SIGNAL('editingFinished()'))
    120

    121 -    def editingFinished(self, value):
    122          self.emit(QtCore.SIGNAL('editingFinished()'))
    123

    124 -    def set_background_color(self, background_color):
    125          if background_color not in (None, ValueLoading):
    126              selfpalette = self.spinBox.palette()
    127              sbpalette = self.spinBox.palette()
    128              lepalette = self.spinBox.lineEdit().palette()
    129              for x in [QtGui.QPalette.Active, QtGui.QPalette.Inactive, QtGui.QPalette.Disabled]:
    130                  for y in [self.backgroundRole(), QtGui.QPalette.Window, QtGui.QPalette.Base]:
    131                      selfpalette.setColor(x, y, background_color)
    132                  for y in [self.spinBox.backgroundRole(), QtGui.QPalette.Window, QtGui.QPalette.Base]:
    133                      sbpalette.setColor(x, y, background_color)
    134                  for y in [self.spinBox.lineEdit().backgroundRole(), QtGui.QPalette.Window, QtGui.QPalette.Base]:
    135                      lepalette.setColor(x, y, background_color)
    136              self.setPalette(selfpalette)
    137              self.spinBox.setPalette(sbpalette)
    138              self.spinBox.lineEdit().setPalette(lepalette)
    139              return True
    140          else:
    141              return False
    142