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

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

      1

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

      5  from customeditor import CustomEditor, editingFinished
      6  from camelot.view.art import Icon
      7  from camelot.core.utils import ugettext as _
      8

      9 -class FileEditor(CustomEditor):
     10      """Widget for editing File fields"""
     11

     12      filter = """All files (*)"""
     13

     14 -    def __init__(self, parent=None, storage=None, editable=True, **kwargs):
     15          CustomEditor.__init__(self, parent)
     16          self.storage = storage
     17          self.editable = editable
     18          self.document_pixmap = Icon('tango/16x16/mimetypes/x-office-document.png').getQPixmap()
     19          self.clear_icon = Icon('tango/16x16/actions/edit-delete.png').getQIcon()
     20          self.new_icon = Icon('tango/16x16/actions/list-add.png').getQIcon()
     21          self.open_icon = Icon('tango/16x16/actions/document-open.png').getQIcon()
     22          self.saveas_icon = Icon('tango/16x16/actions/document-save-as.png').getQIcon()
     23          self.value = None
     24          self.setup_widget()
     25          if self.editable:
     26              self.setAcceptDrops(True)
     27

     28 -    def setup_widget(self):
     29          """Called inside init, overwrite this method for custom

     30          file edit widgets"""
     31          self.layout = QtGui.QHBoxLayout()
     32          self.layout.setSpacing(0)
     33          self.layout.setMargin(0)
     34

     35          # Clear button

     36          self.clear_button = QtGui.QToolButton()
     37          self.clear_button.setFocusPolicy(Qt.ClickFocus)
     38          self.clear_button.setIcon(self.clear_icon)
     39          self.clear_button.setToolTip(_('delete file'))
     40          self.clear_button.setAutoRaise(True)
     41          self.clear_button.setEnabled(self.editable)
     42          self.connect(self.clear_button,

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

     44                       self.clear_button_clicked)
     45

     46          # Open button

     47          self.open_button = QtGui.QToolButton()
     48          self.open_button.setFocusPolicy(Qt.ClickFocus)
     49          self.open_button.setIcon(self.new_icon)
     50          self.open_button.setToolTip(_('add file'))
     51          self.open_button.setEnabled(self.editable)
     52          self.connect(self.open_button,

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

     54                       self.open_button_clicked)
     55          self.open_button.setAutoRaise(True)
     56

     57          # Filename

     58          self.filename = QtGui.QLineEdit(self)
     59          self.filename.setEnabled(self.editable  )
     60          self.filename.setReadOnly(not self.editable)
     61

     62          # Setup layout

     63          self.document_label = QtGui.QLabel(self)
     64          self.document_label.setPixmap(self.document_pixmap)
     65          self.document_label.setEnabled(self.editable)
     66          self.layout.addWidget(self.document_label)
     67          self.layout.addWidget(self.filename)
     68          self.layout.addWidget(self.clear_button)
     69          self.layout.addWidget(self.open_button)
     70          self.setLayout(self.layout)
     71

     72 -    def set_value(self, value):
     73          value = CustomEditor.set_value(self, value)
     74          self.value = value
     75          if value:
     76              self.filename.setText(value.verbose_name)
     77              self.open_button.setIcon(self.open_icon)
     78              self.open_button.setToolTip(_('open file'))
     79          else:
     80              self.filename.setText('')
     81              self.open_button.setIcon(self.new_icon)
     82              self.open_button.setToolTip(_('add file'))
     83          return value
     84

     85 -    def get_value(self):
     86          return CustomEditor.get_value(self) or self.value
     87

     88 -    def set_enabled(self, editable=True):
     89          self.clear_button.setEnabled(editable)
     90          self.open_button.setEnabled(editable)
     91          self.filename.setEnabled(editable)
     92          self.document_label.setEnabled(editable)
     93

     94 -    def stored_file_ready(self, stored_file):
     95          """Slot to be called when a new stored_file has been created by

     96          the storeage"""
     97          self.set_value(stored_file)
     98          self.emit(editingFinished)
     99

    100 -    def open_button_clicked(self):
    101          from camelot.view.storage import open_stored_file, create_stored_file
    102          if not self.value:
    103              create_stored_file(self, self.storage, self.stored_file_ready, filter=self.filter)
    104          else:
    105              open_stored_file(self, self.value)
    106

    107 -    def clear_button_clicked(self):
    108          self.value = None
    109          self.emit(editingFinished)
    110

    111      #

    112      # Drag & Drop

    113      #

    114 -    def dragEnterEvent(self, event):
    115          event.acceptProposedAction()
    116

    117 -    def dragMoveEvent(self, event):
    118          event.acceptProposedAction()
    119

    120 -    def dropEvent(self, event):
    121          if event.mimeData().hasUrls():
    122              url = event.mimeData().urls()[0]
    123              filename = url.toLocalFile()
    124              if filename != '':
    125                  from camelot.view.storage import SaveFileProgressDialog
    126                  from camelot.view.model_thread import post
    127                  progress = SaveFileProgressDialog()
    128

    129                  def checkin():
    130                      stored_file = self.storage.checkin(str(filename))
    131                      return lambda:self.stored_file_ready(stored_file)
    132

    133                  post(checkin, progress.finish)
    134                  progress.exec_()
    135