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

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

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

      4  from customeditor import CustomEditor, editingFinished
      5  from camelot.view.art import Icon
      6  from camelot.core import constants
      7  from math import floor
      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 -class IntegerEditor(CustomEditor):
     16      """Widget for editing an integer field, with a calculator"""
     17

     18 -    def __init__(self,

     19                   parent=None,

     20                   minimum=constants.camelot_minint,

     21                   maximum=constants.camelot_maxint,

     22                   editable=True,

     23                   prefix='',

     24                   suffix='',

     25                   calculator=True,

     26                   **kwargs):
     27          CustomEditor.__init__(self, parent)
     28          action = QtGui.QAction(self)
     29          action.setShortcut(Qt.Key_F3)
     30          self.setFocusPolicy(Qt.StrongFocus)
     31

     32          prefix = str(prefix) + ' '
     33          prefix = prefix.lstrip()
     34

     35          suffix = ' ' + str(suffix)
     36          suffix = suffix.rstrip()
     37

     38          self.spinBox = CustomDoubleSpinBox(parent)
     39          self.spinBox.setPrefix(prefix)
     40          self.spinBox.setSuffix(suffix)
     41          self.spinBox.setReadOnly(not editable)
     42          self.spinBox.setRange(minimum, maximum)
     43          self.spinBox.setDecimals(0)
     44          self.spinBox.setAlignment(Qt.AlignRight|Qt.AlignVCenter)
     45          self.spinBox.setSingleStep(1)
     46          self.spinBox.addAction(action)
     47          self.calculatorButton = QtGui.QToolButton()
     48          icon = Icon('tango/16x16/apps/accessories-calculator.png').getQIcon()
     49          self.calculatorButton.setIcon(icon)
     50          self.calculatorButton.setAutoRaise(True)
     51          self.calculatorButton.setFixedHeight(self.get_height())
     52

     53          self.connect(self.calculatorButton,

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

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

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

     58                       lambda:self.popupCalculator(self.spinBox.value()))
     59          self.connect(self.spinBox,

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

     61                       lambda:self.editingFinished(self.spinBox.value()))
     62

     63          layout = QtGui.QHBoxLayout()
     64          layout.setMargin(0)
     65          layout.setSpacing(0)
     66          layout.addWidget(self.spinBox)
     67          if editable and calculator:
     68              layout.addWidget(self.calculatorButton)
     69          if not editable:
     70              self.spinBox.setEnabled(False)
     71              self.spinBox.setButtonSymbols(QtGui.QAbstractSpinBox.NoButtons)
     72          self.setFocusProxy(self.spinBox)
     73          self.setLayout(layout)
     74

     75 -    def set_value(self, value):
     76          value = CustomEditor.set_value(self, value)
     77          if value!=None:
     78              value = str(value).replace(',', '.')
     79              self.spinBox.setValue(eval(value))
     80          else:
     81              self.spinBox.setValue(0)
     82

     83 -    def get_value(self):
     84          self.spinBox.interpretText()
     85          value = int(self.spinBox.value())
     86          return CustomEditor.get_value(self) or value
     87

     88 -    def set_enabled(self, editable=True):
     89          if self.spinBox.isEnabled() != editable:
     90              if not editable:
     91                  self.layout().removeWidget(self.calculatorButton)
     92              else:
     93                  self.layout().addWidget(self.calculatorButton)
     94              self.spinBox.setEnabled(editable)
     95

     96 -    def popupCalculator(self, value):
     97          from camelot.view.controls.calculator import Calculator
     98          calculator = Calculator(self)
     99          calculator.setValue(value)
    100          self.connect(calculator,

    101                       QtCore.SIGNAL('calculationFinished'),

    102                       self.calculationFinished)
    103          calculator.exec_()
    104

    105 -    def calculationFinished(self, value):
    106          self.spinBox.setValue(floor(float(value)))
    107          self.emit(editingFinished)
    108

    109 -    def editingFinished(self, value):
    110          self.emit(editingFinished)
    111