diff --git a/src/calibre/gui2/dialogs/metadata_single.py b/src/calibre/gui2/dialogs/metadata_single.py index da60495207..c9e349a089 100644 --- a/src/calibre/gui2/dialogs/metadata_single.py +++ b/src/calibre/gui2/dialogs/metadata_single.py @@ -128,6 +128,8 @@ class MetadataSingleDialog(QDialog, Ui_MetadataSingleDialog): self.cover_changed = False self.cpixmap = None self.changed = False + self.cover.setAcceptDrops(True) + self.connect(self.cover, SIGNAL('cover_changed()'), self.cover_dropped) QObject.connect(self.cover_button, SIGNAL("clicked(bool)"), \ self.select_cover) QObject.connect(self.add_format_button, SIGNAL("clicked(bool)"), \ @@ -186,6 +188,9 @@ class MetadataSingleDialog(QDialog, Ui_MetadataSingleDialog): self.exec_() + def cover_dropped(self): + self.cover_changed = True + def initialize_series(self): all_series = self.db.all_series() all_series.sort(cmp=lambda x, y : cmp(x[1], y[1])) diff --git a/src/calibre/gui2/widgets.py b/src/calibre/gui2/widgets.py index f8fb73e4fc..054caac084 100644 --- a/src/calibre/gui2/widgets.py +++ b/src/calibre/gui2/widgets.py @@ -3,11 +3,11 @@ __copyright__ = '2008, Kovid Goyal ' ''' Miscellaneous widgets used in the GUI ''' -import re +import re, os from PyQt4.QtGui import QListView, QIcon, QFont, QLabel, QListWidget, \ QListWidgetItem, QTextCharFormat, QApplication, \ QSyntaxHighlighter, QCursor, QColor, QWidget, \ - QAbstractItemDelegate, QStyle + QAbstractItemDelegate, QPixmap from PyQt4.QtCore import QAbstractListModel, QVariant, Qt, QRect, SIGNAL, \ QObject, QRegExp, QRectF @@ -73,6 +73,41 @@ class ImageView(QLabel): MAX_WIDTH = 400 MAX_HEIGHT = 300 + DROPABBLE_EXTENSIONS = ('jpg', 'jpeg', 'gif', 'png', 'bmp') + + @classmethod + def paths_from_event(cls, event): + ''' + Accept a drop event and return a list of paths that can be read from + and represent files with extensions. + ''' + if event.mimeData().hasFormat('text/uri-list'): + urls = [qstring_to_unicode(u.toLocalFile()) for u in event.mimeData().urls()] + urls = [u for u in urls if os.path.splitext(u)[1] and os.access(u, os.R_OK)] + return [u for u in urls if os.path.splitext(u)[1][1:].lower() in cls.DROPABBLE_EXTENSIONS] + + def dragEnterEvent(self, event): + if int(event.possibleActions() & Qt.CopyAction) + \ + int(event.possibleActions() & Qt.MoveAction) == 0: + return + paths = self.paths_from_event(event) + if paths: + event.acceptProposedAction() + + def dropEvent(self, event): + paths = self.paths_from_event(event) + event.setDropAction(Qt.CopyAction) + for path in paths: + pmap = QPixmap() + pmap.load(path) + if not pmap.isNull(): + self.setPixmap(pmap) + event.accept() + self.emit(SIGNAL('cover_changed()'), paths, Qt.QueuedConnection) + break + + def dragMoveEvent(self, event): + event.acceptProposedAction() def setPixmap(self, pixmap): QLabel.setPixmap(self, pixmap)