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

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

      1  from fileeditor import FileEditor, CustomEditor
      2  from wideeditor import WideEditor
      3  from camelot.view.art import Icon
      4

      5  from PyQt4 import QtGui, QtCore
      6  from PyQt4.QtCore import Qt
      7  from camelot.core.utils import ugettext_lazy as _
      8

      9  from camelot.view.controls.liteboxview import LiteBoxView
     10

     11 -class ImageEditor(FileEditor, WideEditor):
     12      """Editor to view and edit image files, this is a customized implementation

     13      of a FileEditor"""
     14

     15      filter = """Image files (*.bmp *.jpg *.jpeg *.mng *.png *.pbm *.pgm *.ppm *.tiff *.xbm *.xpm)

     16  All files (*)"""
     17

     18 -    def __init__(self,

     19                   parent=None,

     20                   editable=True,

     21                   storage=None,

     22                   preview_width=100,

     23                   preview_height=100,

     24                   **kwargs):
     25          self.preview_width = preview_width
     26          self.preview_height = preview_height
     27          FileEditor.__init__(self, parent=parent, storage=storage, editable=editable, **kwargs)
     28

     29 -    def setup_widget(self):
     30          self.layout = QtGui.QHBoxLayout()
     31          self.layout.setContentsMargins( 0, 0, 0, 0 )
     32          #

     33          # Setup label

     34          #

     35          self.label = QtGui.QLabel(self)
     36          self.label.installEventFilter(self)
     37          self.label.setEnabled(self.editable)
     38          self.layout.addWidget(self.label)
     39          self.label.setAlignment(Qt.AlignHCenter|Qt.AlignVCenter)
     40          #

     41          # Setup buttons

     42          #

     43          button_layout = QtGui.QVBoxLayout()
     44          button_layout.setSpacing(0)
     45          button_layout.setMargin(0)
     46

     47          self.open_button = QtGui.QToolButton()
     48          self.open_button.setIcon(self.open_icon)
     49          self.open_button.setEnabled(self.editable)
     50          self.open_button.setAutoRaise(True)
     51          self.open_button.setToolTip(unicode(_('open image')))
     52          self.connect(self.open_button, QtCore.SIGNAL('clicked()'), self.open_button_clicked)
     53

     54          self.clear_button = QtGui.QToolButton()
     55          self.clear_button.setIcon(self.clear_icon)
     56          self.clear_button.setEnabled(self.editable)
     57          self.clear_button.setToolTip(unicode(_('delete image')))
     58          self.clear_button.setAutoRaise(True)
     59          self.connect(self.clear_button, QtCore.SIGNAL('clicked()'), self.clear_button_clicked)
     60

     61          button_layout.addStretch()
     62          button_layout.addWidget(self.open_button)
     63          button_layout.addWidget(self.clear_button)
     64

     65          self.layout.addLayout(button_layout)
     66          self.layout.addStretch()
     67          self.setLayout(self.layout)
     68          self.clear_image()
     69          self.lite_box = LiteBoxView()
     70

     71 -    def set_enabled(self, editable=True):
     72          self.clear_button.setEnabled(editable)
     73          self.open_button.setEnabled(editable)
     74

     75 -    def set_pixmap(self, pixmap):
     76          self.label.setPixmap(pixmap)
     77          self.draw_border()
     78

     79 -    def set_image(self, image):
     80          self.set_pixmap(QtGui.QPixmap.fromImage(image))
     81

     82 -    def clear_image(self):
     83          dummy_image = Icon('tango/32x32/mimetypes/image-x-generic.png')
     84          self.set_pixmap(dummy_image.getQPixmap())
     85

     86 -    def set_value(self, value):
     87          value = CustomEditor.set_value(self, value)
     88          if value:
     89              self.open_button.setIcon(self.open_icon)
     90              self.open_button.setToolTip(unicode(_('open file')))
     91              if value!=self.value:
     92                  from camelot.view.model_thread import post
     93                  post(lambda:value.checkout_thumbnail(self.preview_width,self.preview_height), self.set_image)
     94          else:
     95              self.clear_image()
     96              self.open_button.setIcon(self.new_icon)
     97              self.open_button.setToolTip(unicode(_('add file')))
     98          self.value = value
     99          return value
    100

    101 -    def draw_border(self):
    102          self.label.setFrameShape(QtGui.QFrame.Box)
    103          self.label.setFrameShadow(QtGui.QFrame.Plain)
    104          self.label.setLineWidth(1)
    105          self.label.setFixedSize(self.preview_width, self.preview_height)
    106

    107 -    def show_fullscreen(self, image):
    108          self.lite_box.show_fullscreen_image(image)
    109

    110 -    def eventFilter(self, object, event):
    111          from camelot.view.model_thread import post
    112          if not object.isWidgetType():
    113              return False
    114          if event.type() != QtCore.QEvent.MouseButtonPress:
    115              return False
    116          if event.modifiers() != QtCore.Qt.NoModifier:
    117              return False
    118          if event.buttons() == QtCore.Qt.LeftButton:
    119              if self.value:
    120                  post(lambda:self.value.checkout_thumbnail(640,480),

    121                       self.show_fullscreen)
    122              return True
    123          return False
    124