# ============================================================================
#
# Copyright (C) 2007-2013 Conceptive Engineering bvba. All rights reserved.
# www.conceptive.be / info@conceptive.be
#
# This file is part of the Camelot Library.
#
# This file may be used under the terms of the GNU General Public
# License version 2.0 as published by the Free Software Foundation
# and appearing in the file license.txt included in the packaging of
# this file. Please review this information to ensure GNU
# General Public Licensing requirements will be met.
#
# If you are unsure which license is appropriate for your use, please
# visit www.python-camelot.com or contact info@conceptive.be
#
# This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
# WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
#
# For use of this library in commercial applications, please contact
# info@conceptive.be
#
# ============================================================================
from PyQt4 import QtGui
from PyQt4.QtCore import Qt
from customeditor import CustomEditor, set_background_color_palette
from camelot.view.art import Icon
from camelot.core.utils import ugettext as _
from camelot.view.controls.decorated_line_edit import DecoratedLineEdit
[docs]class FileEditor(CustomEditor):
"""Widget for editing File fields"""
filter = 'All files (*)'
add_icon = Icon( 'tango/16x16/actions/list-add.png' )
open_icon = Icon( 'tango/16x16/actions/document-open.png' )
clear_icon = Icon( 'tango/16x16/actions/edit-delete.png' )
save_as_icon = Icon( 'tango/16x16/actions/document-save-as.png' )
document_pixmap = Icon( 'tango/16x16/mimetypes/x-office-document.png' )
def __init__(self, parent=None,
storage=None,
field_name='file',
remove_original=False,
**kwargs):
CustomEditor.__init__(self, parent)
self.setSizePolicy( QtGui.QSizePolicy.Preferred,
QtGui.QSizePolicy.Fixed )
self.setObjectName( field_name )
self.storage = storage
self.filename = None # the widget containing the filename
self.value = None
self.remove_original = remove_original
self.setup_widget()
def file_completion_activated(self, index):
from camelot.view.storage import create_stored_file
source_index = index.model().mapToSource( index )
if not self.completions_model.isDir( source_index ):
path = self.completions_model.filePath( source_index )
create_stored_file(
self,
self.storage,
self.stored_file_ready,
filter=self.filter,
remove_original=self.remove_original,
filename = path,
)
def set_value(self, value):
value = CustomEditor.set_value(self, value)
self.value = value
if value:
self.clear_button.setVisible(True)
self.save_as_button.setVisible(True)
self.open_button.setVisible(True)
self.add_button.setVisible(False)
self.filename.setText(value.verbose_name)
else:
self.clear_button.setVisible(False)
self.save_as_button.setVisible(False)
self.open_button.setVisible(False)
self.add_button.setVisible(True)
self.filename.setText('')
return value
def get_value(self):
return CustomEditor.get_value(self) or self.value
def set_field_attributes(self, editable = True,
background_color = None,
tooltip = None,
remove_original = False, **kwargs):
self.set_enabled(editable)
if self.filename:
set_background_color_palette( self.filename, background_color )
self.filename.setToolTip(unicode(tooltip or ''))
self.remove_original = remove_original
def set_enabled(self, editable=True):
self.clear_button.setEnabled(editable)
self.add_button.setEnabled(editable)
self.filename.setEnabled(editable)
self.filename.setReadOnly(not editable)
self.document_label.setEnabled(editable)
self.setAcceptDrops(editable)
[docs] def stored_file_ready(self, stored_file):
"""Slot to be called when a new stored_file has been created by
the storage"""
self.set_value(stored_file)
self.editingFinished.emit()
def save_as_button_clicked(self):
from camelot.view.storage import save_stored_file
value = self.get_value()
if value:
save_stored_file(self, value)
def add_button_clicked(self):
from camelot.view.storage import create_stored_file
create_stored_file(
self,
self.storage,
self.stored_file_ready,
filter=self.filter,
remove_original=self.remove_original,
)
def open_button_clicked(self):
from camelot.view.storage import open_stored_file
open_stored_file(self, self.value)
def clear_button_clicked(self):
answer = QtGui.QMessageBox.question( self,
_('Remove this file ?'),
_('If you continue, you will no longer be able to open this file.'),
QtGui.QMessageBox.Yes,
QtGui.QMessageBox.No )
if answer == QtGui.QMessageBox.Yes:
self.value = None
self.editingFinished.emit()
#
# Drag & Drop
#
def dragEnterEvent(self, event):
event.acceptProposedAction()
def dragMoveEvent(self, event):
event.acceptProposedAction()
def dropEvent(self, event):
from camelot.view.storage import create_stored_file
if event.mimeData().hasUrls():
url = event.mimeData().urls()[0]
filename = url.toLocalFile()
if filename:
create_stored_file(
self,
self.storage,
self.stored_file_ready,
filter=self.filter,
remove_original=self.remove_original,
filename = filename,
)